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