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 for (int i = 0; i < ShapeUtil::TupleElementCount(select.GetShape()); ++i) {
54 llvm::Value* const element_index[] = {b->getInt64(0), b->getInt64(i)};
55 llvm::Value* on_true_element_address =
56 b->CreateInBoundsGEP(on_true, element_index);
57 llvm::Value* on_true_element = b->CreateLoad(
58 on_true_element_address, "on_true_element_" + llvm::Twine(i));
59 llvm::Value* on_false_element_address =
60 b->CreateInBoundsGEP(on_false, element_index);
61 llvm::Value* on_false_element = b->CreateLoad(
62 on_false_element_address, "on_false_element_" + llvm::Twine(i));
63
64 llvm::Value* output_element_address =
65 b->CreateInBoundsGEP(select.GetBasePointer(), element_index);
66 b->CreateStore(b->CreateSelect(pred_cond, on_true_element, on_false_element,
67 "select_output_element_" + llvm::Twine(i)),
68 output_element_address);
69 }
70 }
71
EmitTuple(const IrArray & tuple,absl::Span<llvm::Value * const> operands,llvm::IRBuilder<> * b)72 void EmitTuple(const IrArray& tuple, absl::Span<llvm::Value* const> operands,
73 llvm::IRBuilder<>* b) {
74 llvm::Module* module = getModuleFromBuilder(b);
75 for (size_t i = 0; i < operands.size(); ++i) {
76 auto* store = b->CreateStore(
77 b->CreatePointerCast(operands[i], PrimitiveTypeToIrType(TUPLE, module)),
78 b->CreateInBoundsGEP(tuple.GetBasePointer(),
79 {b->getInt64(0), b->getInt64(i)}));
80 tuple.AnnotateLoadStoreInstructionWithMetadata(store);
81 }
82 }
83
EmitTuple(const IrArray & tuple,absl::Span<const IrArray> buffers,llvm::IRBuilder<> * b)84 void EmitTuple(const IrArray& tuple, absl::Span<const IrArray> buffers,
85 llvm::IRBuilder<>* b) {
86 std::vector<llvm::Value*> buffer_ptrs;
87 buffer_ptrs.reserve(buffers.size());
88 absl::c_transform(
89 buffers, std::back_inserter(buffer_ptrs),
90 [](const llvm_ir::IrArray& buffer) { return buffer.GetBasePointer(); });
91 llvm_ir::EmitTuple(tuple, buffer_ptrs, b);
92 }
93
EmitGetTupleElement(const Shape & target_shape,int64 index,int alignment,llvm::Value * operand,llvm::IRBuilder<> * b)94 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
95 int alignment, llvm::Value* operand,
96 llvm::IRBuilder<>* b) {
97 llvm::Module* module = getModuleFromBuilder(b);
98 llvm::Value* element_ptr =
99 b->CreateInBoundsGEP(operand, {b->getInt64(0), b->getInt64(index)});
100 llvm::LoadInst* src_buffer = b->CreateLoad(element_ptr);
101
102 // Mark the loaded pointer as dereferenceable if we know its shape.
103 if (!target_shape.IsOpaque()) {
104 SetDereferenceableMetadataForLoad(
105 src_buffer,
106 ByteSizeOf(target_shape, src_buffer->getModule()->getDataLayout()));
107 }
108 SetAlignmentMetadataForLoad(src_buffer, alignment);
109
110 llvm::Type* element_type = ShapeToIrType(target_shape, module);
111 llvm::Value* ret_val =
112 b->CreateBitCast(src_buffer, element_type->getPointerTo());
113 return ret_val;
114 }
115
116 } // namespace llvm_ir
117 } // namespace xla
118