aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/llvm_ir/tuple_ops.cc
blob: 11ed6ee59f1bf8e7004b8bef7319b37ef41a304c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/llvm_ir/tuple_ops.h"

#include <stddef.h>
#include <string>
#include <vector>

#include "llvm/IR/Instructions.h"
#include "tensorflow/compiler/xla/service/llvm_ir/llvm_util.h"
#include "tensorflow/compiler/xla/shape_util.h"
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/platform/logging.h"

namespace xla {
namespace llvm_ir {

void EmitTupleSelect(const IrArray& select, const IrArray& pred,
                     llvm::Value* on_true, llvm::Value* on_false,
                     llvm::IRBuilder<>* b, llvm::Module* module) {
  CHECK(ShapeUtil::IsScalar(pred.GetShape()));

  llvm::LoadInst* pred_value =
      b->CreateLoad(pred.GetBasePointer(), "load_predicate_value");
  llvm::Value* pred_cond = b->CreateICmpNE(
      pred_value,
      llvm::ConstantInt::get(PrimitiveTypeToIrType(PRED, module), 0),
      "boolean_predicate");

  VLOG(2) << "HandleSelect for tuple:";
  VLOG(2) << "  pred_value: " << DumpToString(*pred_value);
  VLOG(2) << "  pred_cond: " << DumpToString(*pred_cond);

  for (int i = 0; i < ShapeUtil::TupleElementCount(select.GetShape()); ++i) {
    llvm::Value* const element_index[] = {b->getInt64(0), b->getInt64(i)};
    llvm::Value* on_true_element_address =
        b->CreateInBoundsGEP(on_true, element_index);
    llvm::Value* on_true_element = b->CreateLoad(
        on_true_element_address, "on_true_element_" + llvm::Twine(i));
    llvm::Value* on_false_element_address =
        b->CreateInBoundsGEP(on_false, element_index);
    llvm::Value* on_false_element = b->CreateLoad(
        on_false_element_address, "on_false_element_" + llvm::Twine(i));

    llvm::Value* output_element_address =
        b->CreateInBoundsGEP(select.GetBasePointer(), element_index);
    b->CreateStore(b->CreateSelect(pred_cond, on_true_element, on_false_element,
                                   "select_output_element_" + llvm::Twine(i)),
                   output_element_address);
  }
}

void EmitTuple(const IrArray& tuple,
               tensorflow::gtl::ArraySlice<llvm::Value*> operands,
               llvm::IRBuilder<>* b, llvm::Module* module) {
  for (size_t i = 0; i < operands.size(); ++i) {
    auto* store = b->CreateStore(
        b->CreatePointerCast(operands[i], PrimitiveTypeToIrType(TUPLE, module)),
        b->CreateInBoundsGEP(tuple.GetBasePointer(),
                             {b->getInt64(0), b->getInt64(i)}));
    tuple.AnnotateLoadStoreInstructionWithMetadata(store);
  }
}

llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
                                 int alignment, llvm::Value* operand,
                                 llvm::IRBuilder<>* b, llvm::Module* module) {
  llvm::Value* element_ptr =
      b->CreateInBoundsGEP(operand, {b->getInt64(0), b->getInt64(index)});
  llvm::LoadInst* src_buffer = b->CreateLoad(element_ptr);

  // Mark the loaded pointer as dereferenceable if we know its shape.
  if (!ShapeUtil::IsOpaque(target_shape)) {
    SetDereferenceableMetadataForLoad(
        src_buffer,
        ByteSizeOf(target_shape, src_buffer->getModule()->getDataLayout()));
  }
  SetAlignmentMetadataForLoad(src_buffer, alignment);

  llvm::Type* element_type = ShapeToIrType(target_shape, module);
  llvm::Value* ret_val =
      b->CreateBitCast(src_buffer, element_type->getPointerTo());
  return ret_val;
}

}  // namespace llvm_ir
}  // namespace xla