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