• 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_CPU_PARALLEL_LOOP_EMITTER_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_PARALLEL_LOOP_EMITTER_H_
18 
19 #include "llvm/IR/IRBuilder.h"
20 #include "llvm/IR/Value.h"
21 #include "tensorflow/compiler/xla/service/cpu/ir_emission_utils.h"
22 #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h"
23 #include "tensorflow/compiler/xla/service/llvm_ir/loop_emitter.h"
24 
25 namespace xla {
26 namespace cpu {
27 
28 // ParallelLoopEmitter emits a loop nest for the target array shape.
29 // The outer loop bounds of the loop nest are passed as ir values at runtime
30 // (specified in 'dynamic_loop_bounds'), and the inner loop bounds are static.
31 // Dynamic loop bounds are specified as an array of dimension index
32 // [start, limit) pairs of ir values (one for each partitioned outer dimension).
33 //
34 // EX: Let 'shape' = [8, 16, 32], with the loop bounds of the two-most major
35 //     dimensions dynamic. Then 'dynamic_loop_bounds' will contain the
36 //     following ir values for the two most-major dimensions:
37 //       [dim0_index_start_ir_value, dim0_index_limit_ir_value]
38 //       [dim1_index_start_ir_value, dim1_index_limit_ir_value]
39 //
40 // Code emitted by ParallelLoopEmitter will be called in a multi-threaded
41 // context where each thread will be assigned a different set of outer dimension
42 // partitions, and where all threads will collectively iterate over the
43 // entire target array shape.
44 //
45 // Outer dimension partitions can be generated using the ShapePartitionAssigner
46 // and ShapePartitionIterator utility classes from shape_partition.cc.
47 //
48 class ParallelLoopEmitter : public llvm_ir::LoopEmitter {
49  public:
50   // Constructs a ParallelLoopEmitter which uses 'target_element_generator' to
51   // generate elements, 'dynamic_loop_bounds' to set the loop bounds of the
52   // most-major dimensions, and 'target_array.' shape to set the static loop
53   // bounds for the most-minor dimensions.
54   ParallelLoopEmitter(const llvm_ir::ElementGenerator& target_element_generator,
55                       const llvm_ir::IrArray& target_array,
56                       const DynamicLoopBounds* dynamic_loop_bounds,
57                       llvm::IRBuilder<>* b);
58 
59   ParallelLoopEmitter(const ParallelLoopEmitter&) = delete;
60   ParallelLoopEmitter& operator=(const ParallelLoopEmitter&) = delete;
61   ~ParallelLoopEmitter() override = default;
62 
63   std::vector<llvm_ir::IrArray::Index> EmitIndexAndSetExitBasicBlock(
64       absl::string_view loop_name, llvm::Type* index_type) override;
65 
66  private:
67   const DynamicLoopBounds* dynamic_loop_bounds_;
68 };
69 
70 }  // namespace cpu
71 }  // namespace xla
72 
73 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_PARALLEL_LOOP_EMITTER_H_
74