aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/llvm_ir/tuple_ops.h
blob: cf6bf5d0b14ba71cbed67f9a1dc728c0eef5e393 (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
/* 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.
==============================================================================*/

#ifndef TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_
#define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_

#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Value.h"
#include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/platform/types.h"

// Utilities for emitting LLVM IR related to HLO tuples.

namespace xla {
namespace llvm_ir {

// Selection among tuples is special in how it's lowered, because a tuple is not
// an HLO array.
//
//      tuple_on_true                     tuple_on_false
//           |                                 |
//           V                                 V
// ------------------------          ------------------------
// | address of element 0 |          | address of element 0 |
// |----------------------|          |----------------------|
// | address of element 1 |          | address of element 1 |
// |----------------------|          |----------------------|
// | address of element 2 |          | address of element 2 |
// ------------------------          ------------------------
//                       \            /
//                        \          /
//                         ----------
//         pred ---------> | select |
//                         ----------
//                             |
//                             V
//      output ----> ------------------------
//                   | address of element 0 |
//                   |----------------------|
//                   | address of element 1 |
//                   |----------------------|
//                   | address of element 2 |
//                   ------------------------
//
// Only the addresses are copied to the output. For each element, we emit a copy
// of the address from the corresponding element in either
// tuple_on_true or tuple_on_false:
//   output[i] = pred ? tuple_on_true[i] : tuple_on_false[i]
void EmitTupleSelect(const IrArray& select, const IrArray& pred,
                     llvm::Value* on_true, llvm::Value* on_false,
                     llvm::IRBuilder<>* b, llvm::Module* module);

// A tuple is an array of pointers, one for each operand. Each pointer points to
// the output buffer of its corresponding operand.
void EmitTuple(const IrArray& tuple,
               tensorflow::gtl::ArraySlice<llvm::Value*> operands,
               llvm::IRBuilder<>* b, llvm::Module* module);

// A tuple is an array of pointers, one for each operand. Each pointer points to
// the output buffer of its corresponding operand. A GetTupleElement instruction
// forwards the pointer to underlying tuple element buffer at the given index.
// Returns an llvm value representing a pointer to the tuple element buffer.
llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
                                 int alignment, llvm::Value* operand,
                                 llvm::IRBuilder<>* b, llvm::Module* module);
}  // namespace llvm_ir
}  // namespace xla

#endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_