/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_ELEMENTAL_IR_EMITTER_H_ #define TENSORFLOW_COMPILER_XLA_SERVICE_ELEMENTAL_IR_EMITTER_H_ #include #include #include "absl/strings/string_view.h" #include "absl/types/span.h" #include "llvm/IR/IRBuilder.h" #include "llvm/IR/Module.h" #include "llvm/IR/Value.h" #include "tensorflow/compiler/xla/service/hlo_instruction.h" #include "tensorflow/compiler/xla/service/hlo_instructions.h" #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h" #include "tensorflow/compiler/xla/service/llvm_ir/ir_builder_mixin.h" #include "tensorflow/compiler/xla/service/llvm_ir/loop_emitter.h" #include "tensorflow/compiler/xla/statusor.h" namespace xla { class ElementalIrEmitter : public IrBuilderMixin { public: using HloToElementGeneratorMap = std::unordered_map; ElementalIrEmitter(llvm::Module* module, llvm::IRBuilder<>* b) : b_(b), module_(module) {} virtual ~ElementalIrEmitter() = default; // Returns a function to generate an element of the output of `hlo`, given a // map of functions to generate elements of its operands. llvm_ir::ElementGenerator MakeElementGenerator( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator); llvm::IRBuilder<>* b() { return b_; } // builder() is for IrBuilderMixin. llvm::IRBuilder<>* builder() { return b_; } llvm::Module* module() { return module_; } protected: virtual StatusOr EmitFloatBinaryOp(const HloInstruction* op, llvm::Value* lhs_value, llvm::Value* rhs_value); virtual llvm::Value* EmitExtractReal(llvm::Value* value); virtual llvm::Value* EmitExtractImag(llvm::Value* value); private: virtual StatusOr EmitUnaryOp(const HloInstruction* op, llvm::Value* operand_value); virtual StatusOr EmitBinaryOp(const HloInstruction* op, llvm::Value* lhs_value, llvm::Value* rhs_value); virtual StatusOr EmitIntegerUnaryOp(const HloInstruction* op, llvm::Value* operand_value); virtual StatusOr EmitFloatUnaryOp(const HloInstruction* op, llvm::Value* operand_value); virtual StatusOr EmitComplexUnaryOp(const HloInstruction* op, llvm::Value* operand_value); llvm::Value* IsZero(llvm::Value* v); llvm::Value* IsIntMinDivisionOverflow(llvm::Value* lhs, llvm::Value* rhs); llvm::Value* GetZero(llvm::Type* type); llvm::Value* GetOne(llvm::Type* type); llvm::Value* GetIntSMin(llvm::Type* type); llvm::Value* GetMinusOne(llvm::Type* type); llvm::Value* EmitIntegerDivide(llvm::Value* lhs, llvm::Value* rhs, bool is_signed); llvm::Value* EmitIntegerRemainder(llvm::Value* lhs, llvm::Value* rhs, bool is_signed); llvm::Value* EmitIntegerPow(llvm::Value* lhs, llvm::Value* rhs, bool is_signed); virtual StatusOr EmitIntegerBinaryOp(const HloInstruction* op, llvm::Value* lhs_value, llvm::Value* rhs_value, bool is_signed); virtual StatusOr EmitComplexBinaryOp(const HloInstruction* op, llvm::Value* lhs_value, llvm::Value* rhs_value); virtual llvm::Value* EmitFloatMax(llvm::Value* lhs_value, llvm::Value* rhs_value, absl::string_view name); virtual llvm::Value* EmitFloatMin(llvm::Value* lhs_value, llvm::Value* rhs_value, absl::string_view name); llvm::Value* EmitIntegralMax(llvm::Value* lhs_value, llvm::Value* rhs_value, bool is_signed); llvm::Value* EmitIntegralMin(llvm::Value* lhs_value, llvm::Value* rhs_value, bool is_signed); virtual StatusOr EmitAtan2(PrimitiveType prim_type, llvm::Value* lhs, llvm::Value* rhs, absl::string_view name); virtual StatusOr EmitLog(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitSqrt(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitCbrt(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitRsqrt(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitLog1p(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitSin(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitCos(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitExp(PrimitiveType prim_type, llvm::Value* value, absl::string_view name); virtual StatusOr EmitExpm1(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitPow(PrimitiveType prim_type, llvm::Value* lhs, llvm::Value* rhs, absl::string_view name); virtual StatusOr EmitTanh(PrimitiveType prim_type, llvm::Value* value); virtual StatusOr EmitReducePrecision(const HloInstruction* hlo, llvm::Value* x); virtual StatusOr> EmitComplexAbsHelper(PrimitiveType prim_type, llvm::Value* operand_value, bool return_sqrt); virtual StatusOr EmitComplexAbs(PrimitiveType prim_type, llvm::Value* operand_value); virtual StatusOr EmitSqrtComplexAbs(PrimitiveType prim_type, llvm::Value* operand_value); virtual StatusOr EmitRsqrtComplexAbs( PrimitiveType prim_type, llvm::Value* operand_value); virtual StatusOr EmitComplexSqrt(const HloInstruction* op, PrimitiveType prim_type, llvm::Value* operand_value); virtual StatusOr EmitComplexCbrt(const HloInstruction* op, PrimitiveType prim_type, llvm::Value* operand_value); virtual StatusOr EmitComplexRsqrt(const HloInstruction* op, PrimitiveType prim_type, llvm::Value* operand_value); StatusOr EmitAccumResult( absl::Span accumulator_addrs, llvm::ArrayRef accumulator_types, bool is_variadic); // Composes a complex struct. imag may be nullptr for simple cast operations. llvm::Value* EmitComposeComplex(const HloInstruction* op, llvm::Value* real, llvm::Value* imag); // Emit `accumulator + lhs * rhs` for the given primitive type. llvm::Value* EmitMulAdd(llvm::Value* lhs, llvm::Value* rhs, llvm::Value* accumulator, xla::PrimitiveType primitive_type); // Identifier of the thread unique among all threads on the device virtual llvm::Value* EmitThreadId() { return b_->getIntN(128, 0); } StatusOr EmitElementalSelect( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalClamp( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalConcatenate( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& target_index); StatusOr EmitElementalDynamicSlice( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalGather( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalDynamicUpdateSlice( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalPad( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& padded_index); StatusOr EmitElementalDot( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& dot_result_index); virtual StatusOr> EmitThreadLocalCall( const HloComputation& callee, absl::Span parameters, absl::string_view name) = 0; StatusOr EmitElementalMap( const HloMapInstruction* map_instr, absl::Span elemental_operands); StatusOr EmitElementalReduceWindow( const HloReduceWindowInstruction* reduce_window, std::vector input_generators, std::vector initial_value_generators, const llvm_ir::IrArray::Index& index); StatusOr EmitElementalReduce( const HloReduceInstruction* reduce, std::vector input_generators, std::vector initial_value_generators, const llvm_ir::IrArray::Index& index); virtual StatusOr EmitConvolution( const HloInstruction* hlo, const HloToElementGeneratorMap& operand_to_generator, const llvm_ir::IrArray::Index& index); // Computes the complex power function, returns (a + i*b)^(c + i*d). StatusOr EmitComplexPower(const HloInstruction* op, llvm::Value* a, llvm::Value* b, llvm::Value* c, llvm::Value* d); // Evaluates a polynomial using Horner's method. StatusOr EvaluatePolynomial( llvm::Type* type, llvm::Value* x, absl::Span coefficients); virtual bool fast_min_max() = 0; llvm::IRBuilder<>* const b_; llvm::Module* module_; }; } // namespace xla #endif // TENSORFLOW_COMPILER_XLA_SERVICE_ELEMENTAL_IR_EMITTER_H_