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_DYNAMIC_UPDATE_SLICE_UTIL_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_DYNAMIC_UPDATE_SLICE_UTIL_H_
18
19 #include "tensorflow/compiler/xla/service/buffer_assignment.h"
20 #include "tensorflow/compiler/xla/service/elemental_ir_emitter.h"
21 #include "tensorflow/compiler/xla/service/gpu/partition_assignment.h"
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23 #include "tensorflow/compiler/xla/service/llvm_ir/ir_array.h"
24
25 // Utilities related to emitting LLVM IR for various HLO ops.
26
27 namespace xla {
28 namespace llvm_ir {
29
30 using GeneratorForOperandIrArrays =
31 std::function<std::vector<llvm_ir::IrArray>()>;
32
33 // Checks if we can emit code for the given DynamicUpdateSlice node that updates
34 // its input in place. Returns true if the dynamic-update-slice's
35 // array-to-be-updated and output share the same BufferAllocation::Slice.
36 //
37 // dynamic_update_slice must be a DynamicUpdateSlice op.
38 bool CanUpdateDynamicSliceInPlace(HloInstruction* dynamic_update_slice,
39 const BufferAssignment& assignment);
40
41 // Checks if the given fusion node is amenable to being implemented by
42 // EmitFusedDynamicUpdateSliceInPlace.
CanEmitFusedDynamicUpdateSliceInPlace(HloInstruction * fusion,const BufferAssignment & assignment)43 inline bool CanEmitFusedDynamicUpdateSliceInPlace(
44 HloInstruction* fusion, const BufferAssignment& assignment) {
45 CHECK_EQ(fusion->opcode(), HloOpcode::kFusion);
46 HloInstruction* fused_root = fusion->fused_expression_root();
47 if (fused_root->opcode() != HloOpcode::kDynamicUpdateSlice ||
48 fusion->fusion_kind() != HloInstruction::FusionKind::kLoop) {
49 return false;
50 }
51 // Walk DynamicUpdateSlice operand(0) to fused parameter and get its
52 // associated operand. See if it shares an allocation with this operand.
53 HloInstruction* fusion_operand;
54 ShapeIndex index;
55 std::tie(fusion_operand, index) =
56 fused_root->mutable_operand(0)->LatestNonGteAncestorAndIndex();
57 if (fusion_operand->opcode() != HloOpcode::kParameter) {
58 return false;
59 }
60 auto* operand = fusion->operand(fusion_operand->parameter_number());
61 return assignment.HasAllocationAt(operand, index) &&
62 assignment.HasAllocationAt(fusion, {}) &&
63 assignment.SharesSliceAtIndex(fusion, {}, operand, index);
64 }
65
66 // Emits IR for running the given dynamic-update-slice op in-place -- that is,
67 // where the input and output buffers share the same slice, so we can simply
68 // modify the input/output buffer without touching any of the other elements.
69 Status EmitDynamicUpdateSliceInPlace(absl::Span<const IrArray> operand_arrays,
70 const IrArray& output_array,
71 absl::string_view name,
72 llvm::IRBuilder<>* b);
73
74 // Given a loop-fusion node whose root is a dynamic-update-slice op whose
75 // array-to-be-updated and output share the same buffer slice, emits
76 // (sequential) code for a fusion node that does the dynamic-update-slice in
77 // place.
78 Status EmitFusedDynamicUpdateSliceInPlace(
79 HloInstruction* fusion,
80 GeneratorForOperandIrArrays operand_arrays_generator,
81 const IrArray& fusion_output_array, ElementalIrEmitter* elemental_emitter,
82 llvm::IRBuilder<>* b);
83
84 // Same as EmitFusedDynamicUpdateSliceInPlace, except emits a parallel loop with
85 // the given launch dimensions.
86 Status EmitParallelFusedDynamicUpdateSliceInPlace(
87 HloInstruction* fusion,
88 GeneratorForOperandIrArrays operand_arrays_generator,
89 const IrArray& fusion_output_array, ElementalIrEmitter* elemental_emitter,
90 const gpu::LaunchDimensions& launch_dimensions, llvm::IRBuilder<>* b);
91
92 } // namespace llvm_ir
93 } // namespace xla
94
95 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_LLVM_IR_DYNAMIC_UPDATE_SLICE_UTIL_H_
96