• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #include "tensorflow/compiler/xla/service/root_instruction_sinker.h"
17 
18 #include "tensorflow/compiler/xla/service/tuple_util.h"
19 namespace xla {
20 
21 namespace {
22 
23 // Sinks the root of the given computation for tuple root types.
SinkTupleRoot(HloComputation * computation)24 void SinkTupleRoot(HloComputation* computation) {
25   HloInstruction* root = computation->root_instruction();
26   CHECK(root->shape().IsTuple());
27   HloInstruction* new_root = TupleUtil::Duplicate(root);
28   // Add the new instructions to the schedule.
29   HloInstructionSequence& sequence =
30       computation->parent()->schedule().GetOrCreateSequence(computation);
31   for (HloInstruction* operand : new_root->operands()) {
32     sequence.push_back(operand);
33   }
34   sequence.push_back(new_root);
35   computation->set_root_instruction(new_root);
36 }
37 
38 // Sinks the root of the given computation for not-tuple root types.
SinkNontupleRoot(HloComputation * computation)39 void SinkNontupleRoot(HloComputation* computation) {
40   HloInstruction* root = computation->root_instruction();
41   CHECK(!root->shape().IsTuple());
42   HloInstruction* new_root = computation->AddInstruction(
43       HloInstruction::CreateBitcast(root->shape(), root));
44   HloInstructionSequence& sequence =
45       computation->parent()->schedule().GetOrCreateSequence(computation);
46   sequence.push_back(new_root);
47   computation->set_root_instruction(new_root);
48 }
49 
50 }  // namespace
51 
Run(HloModule * module)52 StatusOr<bool> RootInstructionSinker::Run(HloModule* module) {
53   TF_RET_CHECK(module->has_schedule());
54 
55   bool modified = false;
56   for (HloComputation* computation : module->MakeNonfusionComputations()) {
57     HloInstructionSequence& sequence =
58         module->schedule().GetOrCreateSequence(computation);
59     if (computation->root_instruction() ==
60         sequence.instructions().at(sequence.size() - 1)) {
61       continue;
62     }
63     if (computation->root_instruction()->shape().IsTuple()) {
64       SinkTupleRoot(computation);
65     } else {
66       SinkNontupleRoot(computation);
67     }
68     modified = true;
69   }
70   return modified;
71 }
72 
73 }  // namespace xla
74