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