1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_NESTED_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_NESTED_H_ 18 19 #include "llvm/IR/Function.h" 20 #include "tensorflow/compiler/xla/service/gpu/ir_emitter.h" 21 22 namespace xla { 23 namespace gpu { 24 25 // Emits LLVM IR for a "nested computation" into a non-kernel device function. 26 // 27 // This is used to emit code for HloComputations that don't require a separate 28 // kernel call. For example, IrEmitterNested is used to emit code for a kReduce 29 // HLO's elementwise reduction computation. Notably, IrEmitterNested is *not* 30 // used to emit code for fusion nodes -- fusion nodes use FusedIrEmitter, which 31 // is a different beast altogether. 32 // 33 // IrEmitterNested generates a non-kernel function with the following 34 // parameters: 35 // 36 // - N pointers to the buffers of each of the N parameters to the computation, 37 // - a pointer to the output buffer of the computation, and 38 // - a pointer to the top-level temp buffer. 39 // 40 class IrEmitterNested : public IrEmitter { 41 public: 42 static StatusOr<std::unique_ptr<IrEmitterNested>> Create( 43 const HloModuleConfig& hlo_module_config, 44 const HloComputation& nested_computation, 45 IrEmitterContext* ir_emitter_context); 46 47 IrEmitterNested(const IrEmitterNested&) = delete; 48 IrEmitterNested& operator=(const IrEmitterNested&) = delete; 49 50 // Overrides the default empty implementation. Binds the given instruction 51 // "parameter" with the parameter of the IR function. 52 Status HandleParameter(HloInstruction* parameter) override; 53 GetEmittedFunction()54 llvm::Function* GetEmittedFunction() const { return emitted_function_; } 55 56 Status EmitTargetElementLoop( 57 const HloInstruction& hlo, 58 const llvm_ir::ElementGenerator& body_emitter) override; 59 60 // Generate the code for the computation passed in the constructor. 61 Status CodegenNestedComputation(); 62 63 private: 64 // Constructs an LLVM IR emitter for a nested HLO computation. `function` is 65 // the containing IR function this emitter produces IR to. See 66 // IrEmitter::IrEmitter for the meanings of other arguments. 67 IrEmitterNested(const HloModuleConfig& hlo_module_config, 68 const HloComputation& nested_computation, 69 IrEmitterContext* ir_emitter_context); 70 71 const HloComputation& nested_computation_; 72 llvm::Function* emitted_function_; 73 }; 74 75 } // namespace gpu 76 } // namespace xla 77 78 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_IR_EMITTER_NESTED_H_ 79