1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/compiler/xla/service/cpu/elemental_ir_emitter.h" 17 18#include <string> 19 20#include "llvm/IR/Instructions.h" 21#include "llvm/IR/Module.h" 22#include "tensorflow/compiler/xla/service/hlo_opcode.h" 23#include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h" 24#include "tensorflow/compiler/xla/types.h" 25#include "tensorflow/compiler/xla/util.h" 26#include "tensorflow/compiler/xla/xla_data.pb.h" 27 28namespace xla { 29namespace cpu { 30 31StatusOr<llvm::Value*> CpuElementalIrEmitter::EmitFloatUnaryOp( 32 const HloInstruction* op, llvm::Value* operand_value) const { 33 switch (op->opcode()) { 34 case HloOpcode::kTanh: { 35 PrimitiveType element_type = op->shape().element_type(); 36 bool cast_result_to_fp16 = false; 37 string function_name; 38 switch (element_type) { 39 case F16: 40 cast_result_to_fp16 = true; 41 operand_value = ir_builder_->CreateFPCast(operand_value, 42 ir_builder_->getFloatTy()); 43 TF_FALLTHROUGH_INTENDED; 44 case F32: 45 function_name = "tanhf"; 46 break; 47 case F64: 48 function_name = "tanh"; 49 break; 50 default: 51 return Unimplemented("tanh"); 52 } 53 // Create a function declaration. 54 llvm::Function* function = 55 llvm::cast<llvm::Function>(module_->getOrInsertFunction( 56 llvm_ir::AsStringRef(function_name), operand_value->getType(), 57 operand_value->getType())); 58 function->setCallingConv(llvm::CallingConv::C); 59 function->setDoesNotThrow(); 60 function->setDoesNotAccessMemory(); 61 // Create an instruction to call the function. 62 llvm::Value* result = ir_builder_->CreateCall(function, operand_value); 63 if (cast_result_to_fp16) { 64 result = ir_builder_->CreateFPCast(result, ir_builder_->getHalfTy()); 65 } 66 return result; 67 } 68 default: 69 return ElementalIrEmitter::EmitFloatUnaryOp(op, operand_value); 70 } 71} 72 73StatusOr<llvm::Value*> CpuElementalIrEmitter::EmitAtan2( 74 PrimitiveType prim_type, llvm::Value* lhs, llvm::Value* rhs) const { 75 string function_name; 76 bool cast_result_to_fp16 = false; 77 switch (prim_type) { 78 case F16: 79 cast_result_to_fp16 = true; 80 lhs = ir_builder_->CreateFPCast(lhs, ir_builder_->getFloatTy()); 81 rhs = ir_builder_->CreateFPCast(rhs, ir_builder_->getFloatTy()); 82 TF_FALLTHROUGH_INTENDED; 83 case F32: 84 function_name = "atan2f"; 85 break; 86 case F64: 87 function_name = "atan2"; 88 break; 89 default: 90 return Unimplemented("atan2"); 91 } 92 // Create a function declaration. 93 llvm::Function* function = 94 llvm::cast<llvm::Function>(module_->getOrInsertFunction( 95 llvm_ir::AsStringRef(function_name), lhs->getType(), lhs->getType(), 96 rhs->getType())); 97 function->setCallingConv(llvm::CallingConv::C); 98 function->setDoesNotThrow(); 99 function->setDoesNotAccessMemory(); 100 // Create an instruction to call the function. 101 llvm::Value* result = ir_builder_->CreateCall(function, {lhs, rhs}); 102 if (cast_result_to_fp16) { 103 result = ir_builder_->CreateFPCast(result, ir_builder_->getHalfTy()); 104 } 105 return result; 106} 107 108llvm_ir::ElementGenerator CpuElementalIrEmitter::MakeElementGenerator( 109 const HloInstruction* hlo, 110 const HloToElementGeneratorMap& operand_to_generator) const { 111 if (hlo->opcode() == HloOpcode::kMap) { 112 return [this, hlo, &operand_to_generator]( 113 const llvm_ir::IrArray::Index& index) -> StatusOr<llvm::Value*> { 114 std::vector<llvm::Value*> operands; 115 for (int i = 0; i < hlo->operand_count(); i++) { 116 TF_ASSIGN_OR_RETURN(llvm::Value * operand_value, 117 operand_to_generator.at(hlo->operand(i))( 118 ElementwiseSourceIndex(index, *hlo, 0))); 119 operands.push_back(operand_value); 120 } 121 return ir_emitter_->EmitScalarCall(hlo->shape().element_type(), 122 hlo->to_apply(), operands, 123 llvm_ir::IrName(hlo)); 124 }; 125 } 126 return ElementalIrEmitter::MakeElementGenerator(hlo, operand_to_generator); 127} 128} // namespace cpu 129} // namespace xla 130