• 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_LLVM_IR_LOOP_EMITTER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LOOP_EMITTER_H_
18 
19 #include <functional>
20 
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/IRBuilder.h"
23 #include "llvm/IR/Value.h"
24 #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h"
25 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_loop.h"
26 #include "tensorflow/compiler/xla/statusor.h"
27 
28 namespace xla {
29 namespace llvm_ir {
30 
31 // A function type for emitting code that generates an element in the target
32 // array. The function gets a multi-dimensional index as its only input. This
33 // index specifies the target element for which a value needs to be computed.
34 // The function has to emit code to compute this value and return the resulting
35 // llvm::Value*.
36 using ElementGenerator =
37     std::function<StatusOr<llvm::Value*>(const IrArray::Index& index)>;
38 
39 // Emits a loop for every element in the given shape.
40 class LoopEmitter {
41  public:
42   using BodyEmitter = std::function<Status(const IrArray::Index& index)>;
43 
44   LoopEmitter(const BodyEmitter& body_emitter, const Shape& shape,
45               llvm::IRBuilder<>* b);
46 
47   // Constructs a LoopEmitter from an body_emitter that generates
48   // element of the given target array in the dynamic dimension.
49   LoopEmitter(const BodyEmitter& body_emitter, const Shape& shape,
50               std::vector<llvm::Value*> dynamic_dims, llvm::IRBuilder<>* b);
51 
52   // Constructs a LoopEmitter from an element generator that generates each
53   // element of the given target array.
54   LoopEmitter(const ElementGenerator& target_element_generator,
55               const IrArray& target_array, llvm::IRBuilder<>* b);
56 
57   // Constructs a LoopEmitter that emits one element into each of N separate
58   // arrays on each iteration of the loop.
59   //
60   // This is used for multi-output fusion.  target_element_generator must
61   // produce an LLVM struct with N elements.
62   LoopEmitter(const ElementGenerator& target_element_generator,
63               absl::Span<const IrArray> target_arrays, llvm::IRBuilder<>* b);
64 
65   LoopEmitter(const LoopEmitter&) = delete;
66   LoopEmitter& operator=(const LoopEmitter&) = delete;
67   virtual ~LoopEmitter() = default;
68 
69   // Emits a loop nest (with a yet-to-be-filled loop body) that iterates through
70   // every element in the given shape. Returns the multi-dimensional index that
71   // specifies the element, will return multiple indices if the loop is
72   // unrolled.
EmitIndexAndSetExitBasicBlock()73   std::vector<IrArray::Index> EmitIndexAndSetExitBasicBlock() {
74     return EmitIndexAndSetExitBasicBlock(/*loop_name=*/"", b_->getInt64Ty(),
75                                          /*base_index*/ nullptr);
76   }
77 
78   virtual std::vector<IrArray::Index> EmitIndexAndSetExitBasicBlock(
79       absl::string_view loop_name, llvm::Type* index_type,
80       llvm::Value* base_index);
81 
82   // Emits a complete loop nest for every element in the given shape.
83   Status EmitLoop(absl::string_view loop_name = "",
84                   llvm::Type* index_type = nullptr);
85 
86  protected:
87   // An IR emitter that generates the loop body.
88   BodyEmitter body_emitter_;
89 
90   // The shape that the emitted loop iterates through.
91   Shape shape_;
92 
93   // Dynamic dimensions that  emitted loop iterates through. Generate the
94   // loop based on the dynamic dimensions if this vector is not empty.
95   std::vector<llvm::Value*> dynamic_dims_;
96 
97   // Points to the exit block of the emitted loop. If the given shape is
98   // scalar, no loops are emitted and exit_bb_ is nullptr in that case.
99   llvm::BasicBlock* exit_bb_;
100 
101   llvm::IRBuilder<>* b_;
102 
103  private:
104   IrArray::Index EmitStaticIndex(ForLoopNest* loop_nest,
105                                  llvm::Type* index_type);
106   IrArray::Index EmitDynamicIndex(ForLoopNest* loop_nest,
107                                   llvm::Type* index_type);
108 };
109 
110 }  // namespace llvm_ir
111 }  // namespace xla
112 
113 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_LOOP_EMITTER_H_
114