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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_
18 
19 #include "absl/types/span.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/Value.h"
22 #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h"
23 #include "tensorflow/core/platform/types.h"
24 
25 // Utilities for emitting LLVM IR related to HLO tuples.
26 
27 namespace xla {
28 namespace llvm_ir {
29 
30 // Selection among tuples is special in how it's lowered, because a tuple is not
31 // an HLO array.
32 //
33 //      tuple_on_true                     tuple_on_false
34 //           |                                 |
35 //           V                                 V
36 // ------------------------          ------------------------
37 // | address of element 0 |          | address of element 0 |
38 // |----------------------|          |----------------------|
39 // | address of element 1 |          | address of element 1 |
40 // |----------------------|          |----------------------|
41 // | address of element 2 |          | address of element 2 |
42 // ------------------------          ------------------------
43 //                       \            /
44 //                        \          /
45 //                         ----------
46 //         pred ---------> | select |
47 //                         ----------
48 //                             |
49 //                             V
50 //      output ----> ------------------------
51 //                   | address of element 0 |
52 //                   |----------------------|
53 //                   | address of element 1 |
54 //                   |----------------------|
55 //                   | address of element 2 |
56 //                   ------------------------
57 //
58 // Only the addresses are copied to the output. For each element, we emit a copy
59 // of the address from the corresponding element in either
60 // tuple_on_true or tuple_on_false:
61 //   output[i] = pred ? tuple_on_true[i] : tuple_on_false[i]
62 void EmitTupleSelect(const IrArray& select, const IrArray& pred,
63                      llvm::Value* on_true, llvm::Value* on_false,
64                      llvm::IRBuilder<>* b);
65 
66 // A tuple is an array of pointers, one for each operand. Each pointer points to
67 // the output buffer of its corresponding operand.
68 void EmitTuple(const IrArray& tuple, absl::Span<llvm::Value* const> operands,
69                llvm::IRBuilder<>* b);
70 
71 // Emits one alloca for each element in the tuple of shape tuple_shape,
72 // returns the emitted allocas.
73 // Precondition: tuple_shape should be a tuple of scalars.
74 std::vector<llvm::Value*> EmitTupleAllocasAtFunctionEntry(
75     const Shape& tuple_shape, llvm::IRBuilder<>* b);
76 
77 // Similar to EmitTuple above, except that the output buffers are provided in
78 // the form of IrArray.
79 void EmitTuple(const IrArray& tuple, absl::Span<const IrArray> buffers,
80                llvm::IRBuilder<>* b);
81 
82 // A tuple is an array of pointers, one for each operand. Each pointer points to
83 // the output buffer of its corresponding operand. A GetTupleElement instruction
84 // forwards the pointer to underlying tuple element buffer at the given index.
85 // Returns an llvm value representing a pointer to the tuple element buffer.
86 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64 index,
87                                  int alignment, llvm::Value* operand,
88                                  llvm::IRBuilder<>* b);
89 }  // namespace llvm_ir
90 }  // namespace xla
91 
92 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_
93