1 /* Copyright 2017 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_HLO_TO_IR_BINDINGS_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_GPU_HLO_TO_IR_BINDINGS_H_ 18 19 #include <unordered_map> 20 21 #include "absl/container/flat_hash_map.h" 22 #include "absl/types/span.h" 23 #include "llvm/IR/IRBuilder.h" 24 #include "llvm/IR/Value.h" 25 #include "tensorflow/compiler/xla/map_util.h" 26 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 27 #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h" 28 29 namespace xla { 30 namespace gpu { 31 32 // This class encapsulates the bindings between HloInstructions and LLVM IR 33 // values that represent their addresses. 34 class HloToIrBindings { 35 public: HloToIrBindings(llvm::IRBuilder<> * b,llvm::Module * llvm_module,bool is_nested)36 HloToIrBindings(llvm::IRBuilder<>* b, llvm::Module* llvm_module, 37 bool is_nested) 38 : is_nested_(is_nested), b_(b), module_(llvm_module) {} 39 40 void EmitBasePointersForHlos( 41 absl::Span<const HloInstruction* const> io_hlos, 42 absl::Span<const HloInstruction* const> non_io_hlos); 43 44 // Rebinds the given HLO to the LLVM IR value that represent its address. 45 void BindHloToIrValue(const HloInstruction& hlo, llvm::Value* ir_value, 46 ShapeIndexView shape_index = {}); 47 48 // Unbinds all IR values that's defined in an LLVM function, e.g., function 49 // arguments and stack variables. Global variables will be kept in bindings_. 50 // 51 // This method is called after emitting code for each top-level HLO. The local 52 // IR values are out of scope at that point and should not be used. 53 void UnbindAllLocalIrValues(); 54 55 // Returns whether `hlo` is bound to an LLVM IR value. BoundToIrValue(const HloInstruction & hlo)56 bool BoundToIrValue(const HloInstruction& hlo) const { 57 return base_ptrs_.contains(&hlo); 58 } 59 GetTempBufferBase()60 llvm::Value* GetTempBufferBase() const { return temp_buffer_base_; } SetTempBufferBase(llvm::Value * v)61 void SetTempBufferBase(llvm::Value* v) { temp_buffer_base_ = v; } 62 63 // A helper method that returns the base pointer of the IrArray containing the 64 // output of "inst".at the given ShapeIndex. 65 llvm::Value* GetBasePointer(const HloInstruction& hlo, 66 ShapeIndexView shape_index = {}) const { 67 auto it = base_ptrs_.find(&hlo); 68 CHECK(it != base_ptrs_.end()) << hlo.ToString(); 69 return it->second.element(shape_index); 70 } 71 72 // Returns the IrArray which contains the output of hlo. 73 // 74 // consumer is the HLO in which this IrArray is used -- we use this to (try 75 // to) add metadata indicating that the array is invariant within consumer. 76 // 77 // To get the buffer into which hlo should write its own output, call 78 // GetIrArray(hlo, hlo). 79 llvm_ir::IrArray GetIrArray(const HloInstruction& hlo, 80 const HloInstruction& consumer, 81 const ShapeIndex& shape_index = {}); 82 83 string ToString() const; 84 85 private: 86 // Emits IR to resolve (possibly) recursive GetTupleElement instructions. 87 llvm::Value* EmitGetTupleElement(const HloInstruction* gte, 88 llvm::Value* base_ptr); 89 90 // Returns an llvm typed ir representation of 'ir_value' based on 'hlo' shape. 91 llvm::Value* GetTypedIrValue(const HloInstruction& hlo, 92 ShapeIndexView shape_index, 93 llvm::Value* ir_value); 94 95 const bool is_nested_; 96 97 llvm::IRBuilder<>* b_; 98 llvm::Module* module_; 99 100 // Stores the underlying llvm::IrArray for each HloInstruction. 101 // For an instruction that generates multiple outputs, the root will be a 102 // tuple shape. The IrArray for each element output is stored in the subnode 103 // in the ShapeTree. 104 absl::flat_hash_map<const HloInstruction*, ShapeTree<llvm::Value*>> 105 base_ptrs_; 106 107 // The address of the memory block that contains all temporary buffers. 108 llvm::Value* temp_buffer_base_ = nullptr; 109 }; 110 111 // Converts `ir_value` with type i8* to a typed LLVM Value* based on `shape`. 112 llvm::Value* CastToTypedValue(const Shape& shape, llvm::Value* ir_value, 113 llvm::IRBuilder<>* b); 114 115 } // namespace gpu 116 } // namespace xla 117 118 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_GPU_HLO_TO_IR_BINDINGS_H_ 119