Home | History | Annotate | Download | only in cpu
      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 #include "tensorflow/compiler/xla/service/cpu/parallel_loop_emitter.h"
     17 
     18 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_loop.h"
     19 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
     20 #include "tensorflow/core/lib/strings/stringprintf.h"
     21 
     22 namespace xla {
     23 namespace cpu {
     24 
     25 ParallelLoopEmitter::ParallelLoopEmitter(
     26     const llvm_ir::ElementGenerator& target_element_generator,
     27     const llvm_ir::IrArray& target_array,
     28     const DynamicLoopBounds* dynamic_loop_bounds, llvm::IRBuilder<>* ir_builder)
     29     : LoopEmitter(target_element_generator, target_array, ir_builder),
     30       dynamic_loop_bounds_(dynamic_loop_bounds) {}
     31 
     32 llvm_ir::IrArray::Index ParallelLoopEmitter::EmitIndexAndSetExitBasicBlock(
     33     tensorflow::StringPiece loop_name) {
     34   CHECK(!ShapeUtil::IsTuple(shape_));
     35   CHECK(!ShapeUtil::IsScalar(shape_));
     36 
     37   llvm_ir::ForLoopNest loop_nest(loop_name, ir_builder_);
     38   const int64 num_dims = shape_.dimensions_size();
     39   llvm_ir::IrArray::Index array_index(num_dims);
     40 
     41   // Add loops from outer-most to inner-most dimensions.
     42   for (int i = LayoutUtil::MinorToMajor(shape_).size() - 1; i >= 0; --i) {
     43     const int64 dimension = LayoutUtil::Minor(shape_.layout(), i);
     44     const int bounds_index = num_dims - 1 - i;
     45     if (bounds_index < dynamic_loop_bounds_->size()) {
     46       // Emit dynamic loop bounds for this dimension. Dynamic loop bounds
     47       // are read from ir function dynamic loop bounds argument.
     48       llvm::Value* start_index = (*dynamic_loop_bounds_)[bounds_index].first;
     49       llvm::Value* end_index = (*dynamic_loop_bounds_)[bounds_index].second;
     50 
     51       std::unique_ptr<llvm_ir::ForLoop> loop = loop_nest.AddLoop(
     52           /*suffix=*/tensorflow::strings::Printf("dim.%lld", dimension),
     53           start_index, end_index);
     54       array_index[dimension] = loop->GetIndVarValue();
     55     } else {
     56       // Emit static loop bounds for this dimension.
     57       std::unique_ptr<llvm_ir::ForLoop> loop = loop_nest.AddLoop(
     58           /*start_index=*/0,
     59           /*end_index=*/shape_.dimensions(dimension),
     60           /*suffix=*/tensorflow::strings::Printf("dim.%lld", dimension));
     61       array_index[dimension] = loop->GetIndVarValue();
     62     }
     63   }
     64   // Point IR builder at inner loop BB.
     65   llvm_ir::SetToFirstInsertPoint(loop_nest.GetInnerLoopBodyBasicBlock(),
     66                                  ir_builder_);
     67 
     68   // Set exit_bb_ to the exit block of the loop nest.
     69   exit_bb_ = loop_nest.GetOuterLoopExitBasicBlock();
     70   CHECK(exit_bb_ != nullptr);
     71 
     72   return array_index;
     73 }
     74 
     75 }  // namespace cpu
     76 }  // namespace xla
     77