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=*/1, src, /*SrcAlign=*/1,
58 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* store = b->CreateStore(
66 b->CreatePointerCast(operands[i], PrimitiveTypeToIrType(TUPLE, module)),
67 b->CreateInBoundsGEP(tuple.GetBasePointer(),
68 {b->getInt64(0), b->getInt64(i)}));
69 tuple.AnnotateLoadStoreInstructionWithMetadata(store);
70 }
71 }
72
EmitTuple(const IrArray & tuple,absl::Span<const IrArray> buffers,llvm::IRBuilder<> * b)73 void EmitTuple(const IrArray& tuple, absl::Span<const IrArray> buffers,
74 llvm::IRBuilder<>* b) {
75 std::vector<llvm::Value*> buffer_ptrs;
76 buffer_ptrs.reserve(buffers.size());
77 absl::c_transform(
78 buffers, std::back_inserter(buffer_ptrs),
79 [](const llvm_ir::IrArray& buffer) { return buffer.GetBasePointer(); });
80 llvm_ir::EmitTuple(tuple, buffer_ptrs, b);
81 }
82
EmitTupleAllocasAtFunctionEntry(const Shape & tuple_shape,llvm::IRBuilder<> * b)83 std::vector<llvm::Value*> EmitTupleAllocasAtFunctionEntry(
84 const Shape& tuple_shape, llvm::IRBuilder<>* b) {
85 llvm::Module* module = b->GetInsertBlock()->getModule();
86
87 llvm::IRBuilder<>::InsertPointGuard guard(*b);
88 llvm::Function* function = b->GetInsertBlock()->getParent();
89 b->SetInsertPoint(&function->getEntryBlock(),
90 function->getEntryBlock().getFirstInsertionPt());
91 CHECK(tuple_shape.IsTuple());
92 int tuple_size = tuple_shape.tuple_shapes_size();
93
94 std::vector<llvm::Value*> generated_allocas;
95 for (int i = 0; i < tuple_size; i++) {
96 const Shape& element_shape = tuple_shape.tuple_shapes(i);
97 CHECK(ShapeUtil::IsScalar(element_shape));
98 llvm::Type* type =
99 llvm_ir::PrimitiveTypeToIrType(element_shape.element_type(), module);
100 llvm::AllocaInst* alloca = b->CreateAlloca(
101 type,
102 /*ArraySize=*/nullptr, AsStringRef(absl::StrCat("tuple_element_", i)));
103 generated_allocas.push_back(alloca);
104 }
105
106 return generated_allocas;
107 }
108
EmitGetTupleElement(const Shape & target_shape,int64 index,int alignment,llvm::Value * operand,llvm::IRBuilder<> * b)109 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
110 int alignment, llvm::Value* operand,
111 llvm::IRBuilder<>* b) {
112 llvm::Module* module = getModuleFromBuilder(b);
113 llvm::Value* element_ptr =
114 b->CreateInBoundsGEP(operand, {b->getInt64(0), b->getInt64(index)});
115 llvm::LoadInst* src_buffer = b->CreateLoad(element_ptr);
116
117 // Mark the loaded pointer as dereferenceable if we know its shape.
118 if (!target_shape.IsOpaque()) {
119 SetDereferenceableMetadataForLoad(
120 src_buffer,
121 ByteSizeOf(target_shape, src_buffer->getModule()->getDataLayout()));
122 }
123 SetAlignmentMetadataForLoad(src_buffer, alignment);
124
125 llvm::Type* element_type = ShapeToIrType(target_shape, module);
126 llvm::Value* ret_val =
127 b->CreateBitCast(src_buffer, element_type->getPointerTo());
128 return ret_val;
129 }
130
131 } // namespace llvm_ir
132 } // namespace xla
133