xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/llvm_ir/tuple_ops.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
24 // Utilities for emitting LLVM IR related to HLO tuples.
25 
26 namespace xla {
27 namespace llvm_ir {
28 
29 // A tuple is an array of pointers, one for each operand. Each pointer points to
30 // the output buffer of its corresponding operand.
31 void EmitTuple(const IrArray& tuple, absl::Span<llvm::Value* const> operands,
32                llvm::IRBuilder<>* b);
33 
34 // Emits one alloca for each element in the tuple of shape tuple_shape,
35 // returns the emitted allocas.
36 // Precondition: tuple_shape should be a tuple of scalars.
37 std::vector<llvm::Value*> EmitTupleAllocasAtFunctionEntry(
38     const Shape& tuple_shape, llvm::IRBuilder<>* b);
39 
40 // Similar to EmitTuple above, except that the output buffers are provided in
41 // the form of IrArray.
42 void EmitTuple(const IrArray& tuple, absl::Span<const IrArray> buffers,
43                llvm::IRBuilder<>* b);
44 
45 // A tuple is an array of pointers, one for each operand. Each pointer points to
46 // the output buffer of its corresponding operand. A GetTupleElement instruction
47 // forwards the pointer to underlying tuple element buffer at the given index.
48 // Returns an llvm value representing a pointer to the tuple element buffer.
49 llvm::Value* EmitGetTupleElement(const Shape& target_shape, int64_t index,
50                                  int alignment, llvm::Value* operand,
51                                  llvm::Type* operand_pointee_type,
52                                  llvm::IRBuilder<>* b);
53 }  // namespace llvm_ir
54 }  // namespace xla
55 
56 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_TUPLE_OPS_H_
57