• 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 #include "tensorflow/compiler/xla/service/llvm_ir/tuple_ops.h"
17 
18 #include <stddef.h>
19 #include <string>
20 #include <vector>
21 
22 #include "llvm/IR/Instructions.h"
23 #include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
24 #include "tensorflow/compiler/xla/shape_util.h"
25 #include "tensorflow/compiler/xla/types.h"
26 #include "tensorflow/compiler/xla/xla_data.pb.h"
27 #include "tensorflow/core/platform/logging.h"
28 
29 namespace xla {
30 namespace llvm_ir {
31 
getModuleFromBuilder(llvm::IRBuilder<> * b)32 static llvm::Module* getModuleFromBuilder(llvm::IRBuilder<>* b) {
33   return b->GetInsertBlock()->getModule();
34 }
35 
EmitTupleSelect(const IrArray & select,const IrArray & pred,llvm::Value * on_true,llvm::Value * on_false,llvm::IRBuilder<> * b)36 void EmitTupleSelect(const IrArray& select, const IrArray& pred,
37                      llvm::Value* on_true, llvm::Value* on_false,
38                      llvm::IRBuilder<>* b) {
39   llvm::Module* module = getModuleFromBuilder(b);
40   CHECK(ShapeUtil::IsScalar(pred.GetShape()));
41 
42   llvm::LoadInst* pred_value =
43       b->CreateLoad(pred.GetBasePointer(), "load_predicate_value");
44   llvm::Value* pred_cond = b->CreateICmpNE(
45       pred_value,
46       llvm::ConstantInt::get(PrimitiveTypeToIrType(PRED, module), 0),
47       "boolean_predicate");
48 
49   VLOG(2) << "HandleSelect for tuple:";
50   VLOG(2) << "  pred_value: " << DumpToString(*pred_value);
51   VLOG(2) << "  pred_cond: " << DumpToString(*pred_cond);
52 
53   llvm::Value* src = b->CreateSelect(pred_cond, on_true, on_false);
54   llvm::Value* dst = select.GetBasePointer();
55   int64 table_size = ShapeUtil::ByteSizeOfTupleIndexTable(
56       select.GetShape(), module->getDataLayout().getPointerSize());
57   b->CreateMemCpy(dst, /*DstAlign=*/llvm::Align(1), src,
58                   /*SrcAlign=*/llvm::Align(1), b->getInt64(table_size));
59 }
60 
EmitTuple(const IrArray & tuple,absl::Span<llvm::Value * const> operands,llvm::IRBuilder<> * b)61 void EmitTuple(const IrArray& tuple, absl::Span<llvm::Value* const> operands,
62                llvm::IRBuilder<>* b) {
63   llvm::Module* module = getModuleFromBuilder(b);
64   for (size_t i = 0; i < operands.size(); ++i) {
65     auto* cast =
66         b->CreatePointerCast(operands[i], PrimitiveTypeToIrType(TUPLE, module));
67     auto* store = b->CreateStore(
68         cast, b->CreateInBoundsGEP(tuple.GetBasePointer(),
69                                    {b->getInt64(0), b->getInt64(i)}));
70     tuple.AnnotateLoadStoreInstructionWithMetadata(store);
71   }
72 }
73 
EmitTuple(const IrArray & tuple,absl::Span<const IrArray> buffers,llvm::IRBuilder<> * b)74 void EmitTuple(const IrArray& tuple, absl::Span<const IrArray> buffers,
75                llvm::IRBuilder<>* b) {
76   std::vector<llvm::Value*> buffer_ptrs;
77   buffer_ptrs.reserve(buffers.size());
78   absl::c_transform(
79       buffers, std::back_inserter(buffer_ptrs),
80       [](const llvm_ir::IrArray& buffer) { return buffer.GetBasePointer(); });
81   llvm_ir::EmitTuple(tuple, buffer_ptrs, b);
82 }
83 
EmitTupleAllocasAtFunctionEntry(const Shape & tuple_shape,llvm::IRBuilder<> * b)84 std::vector<llvm::Value*> EmitTupleAllocasAtFunctionEntry(
85     const Shape& tuple_shape, llvm::IRBuilder<>* b) {
86   llvm::Module* module = b->GetInsertBlock()->getModule();
87 
88   llvm::IRBuilder<>::InsertPointGuard guard(*b);
89   llvm::Function* function = b->GetInsertBlock()->getParent();
90   b->SetInsertPoint(&function->getEntryBlock(),
91                     function->getEntryBlock().getFirstInsertionPt());
92   CHECK(tuple_shape.IsTuple());
93   int tuple_size = tuple_shape.tuple_shapes_size();
94 
95   std::vector<llvm::Value*> generated_allocas;
96   for (int i = 0; i < tuple_size; i++) {
97     const Shape& element_shape = tuple_shape.tuple_shapes(i);
98     CHECK(ShapeUtil::IsScalar(element_shape));
99     llvm::Type* type =
100         llvm_ir::PrimitiveTypeToIrType(element_shape.element_type(), module);
101     llvm::AllocaInst* alloca = b->CreateAlloca(
102         type,
103         /*ArraySize=*/nullptr, AsStringRef(absl::StrCat("tuple_element_", i)));
104     generated_allocas.push_back(alloca);
105   }
106 
107   return generated_allocas;
108 }
109 
EmitGetTupleElement(const Shape & target_shape,int64 index,int alignment,llvm::Value * operand,llvm::IRBuilder<> * b)110 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
111                                  int alignment, llvm::Value* operand,
112                                  llvm::IRBuilder<>* b) {
113   llvm::Module* module = getModuleFromBuilder(b);
114   llvm::Value* element_ptr =
115       b->CreateInBoundsGEP(operand, {b->getInt64(0), b->getInt64(index)});
116   llvm::LoadInst* src_buffer = b->CreateLoad(element_ptr);
117 
118   // Mark the loaded pointer as dereferenceable if we know its shape.
119   if (!target_shape.IsOpaque()) {
120     SetDereferenceableMetadataForLoad(
121         src_buffer,
122         ByteSizeOf(target_shape, src_buffer->getModule()->getDataLayout()));
123   }
124   SetAlignmentMetadataForLoad(src_buffer, alignment);
125 
126   llvm::Type* element_type = ShapeToIrType(target_shape, module);
127   llvm::Value* ret_val =
128       b->CreateBitCast(src_buffer, element_type->getPointerTo());
129   return ret_val;
130 }
131 
132 }  // namespace llvm_ir
133 }  // namespace xla
134