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/lib/strings/stringprintf.h" 28 #include "tensorflow/core/platform/logging.h" 29 30 namespace xla { 31 namespace llvm_ir { 32 33 void EmitTupleSelect(IrArray select, IrArray pred, llvm::Value* on_true, 34 llvm::Value* on_false, llvm::IRBuilder<>* ir_builder, 35 llvm::Module* module) { 36 CHECK(ShapeUtil::IsScalar(pred.GetShape())); 37 38 llvm::LoadInst* pred_value = 39 ir_builder->CreateLoad(pred.GetBasePointer(), "load_predicate_value"); 40 llvm::Value* pred_cond = ir_builder->CreateICmpNE( 41 pred_value, 42 llvm::ConstantInt::get(PrimitiveTypeToIrType(PRED, module), 0), 43 "boolean_predicate"); 44 45 VLOG(2) << "HandleSelect for tuple:"; 46 VLOG(2) << " pred_value: " << DumpToString(*pred_value); 47 VLOG(2) << " pred_cond: " << DumpToString(*pred_cond); 48 49 for (int i = 0; i < ShapeUtil::TupleElementCount(select.GetShape()); ++i) { 50 std::vector<llvm::Value*> element_index = {ir_builder->getInt64(0), 51 ir_builder->getInt64(i)}; 52 llvm::Value* on_true_element_address = 53 ir_builder->CreateInBoundsGEP(on_true, element_index); 54 llvm::Value* on_true_element = ir_builder->CreateLoad( 55 on_true_element_address, 56 tensorflow::strings::Printf("on_true_element_%d", i).c_str()); 57 llvm::Value* on_false_element_address = 58 ir_builder->CreateInBoundsGEP(on_false, element_index); 59 llvm::Value* on_false_element = ir_builder->CreateLoad( 60 on_false_element_address, 61 tensorflow::strings::Printf("on_false_element_%d", i).c_str()); 62 63 llvm::Value* output_element_address = 64 ir_builder->CreateInBoundsGEP(select.GetBasePointer(), element_index); 65 ir_builder->CreateStore( 66 ir_builder->CreateSelect( 67 pred_cond, on_true_element, on_false_element, 68 tensorflow::strings::Printf("select_output_element_%d", i).c_str()), 69 output_element_address); 70 } 71 } 72 73 void EmitTuple(IrArray tuple, 74 tensorflow::gtl::ArraySlice<llvm::Value*> operands, 75 llvm::IRBuilder<>* ir_builder, llvm::Module* module) { 76 for (size_t i = 0; i < operands.size(); ++i) { 77 auto* store = ir_builder->CreateStore( 78 ir_builder->CreatePointerCast(operands[i], 79 PrimitiveTypeToIrType(TUPLE, module)), 80 ir_builder->CreateInBoundsGEP( 81 tuple.GetBasePointer(), 82 {ir_builder->getInt64(0), ir_builder->getInt64(i)})); 83 tuple.AnnotateLoadStoreInstructionWithMetadata(store); 84 } 85 } 86 87 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index, 88 int alignment, llvm::Value* operand, 89 llvm::IRBuilder<>* ir_builder, 90 llvm::Module* module) { 91 llvm::Value* element_ptr = ir_builder->CreateInBoundsGEP( 92 operand, {ir_builder->getInt64(0), ir_builder->getInt64(index)}); 93 llvm::LoadInst* src_buffer = ir_builder->CreateLoad(element_ptr); 94 95 // Mark the loaded pointer as dereferenceable if we know its shape. 96 if (!ShapeUtil::IsOpaque(target_shape)) { 97 SetDereferenceableMetadataForLoad( 98 src_buffer, 99 ByteSizeOf(target_shape, src_buffer->getModule()->getDataLayout())); 100 } 101 SetAlignmentMetadataForLoad(src_buffer, alignment); 102 103 llvm::Type* element_type = ShapeToIrType(target_shape, module); 104 llvm::Value* ret_val = 105 ir_builder->CreateBitCast(src_buffer, element_type->getPointerTo()); 106 return ret_val; 107 } 108 109 } // namespace llvm_ir 110 } // namespace xla 111