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 #include "tensorflow/compiler/xla/service/hlo_dce.h" 17 18 #include <memory> 19 #include <unordered_set> 20 #include <utility> 21 #include <vector> 22 23 #include "absl/container/flat_hash_set.h" 24 #include "tensorflow/compiler/xla/service/hlo_computation.h" 25 #include "tensorflow/compiler/xla/service/hlo_instruction.h" 26 #include "tensorflow/compiler/xla/service/hlo_module.h" 27 #include "tensorflow/compiler/xla/service/hlo_opcode.h" 28 #include "tensorflow/compiler/xla/status.h" 29 #include "tensorflow/compiler/xla/status_macros.h" 30 #include "tensorflow/compiler/xla/statusor.h" 31 #include "tensorflow/compiler/xla/types.h" 32 #include "tensorflow/compiler/xla/util.h" 33 #include "tensorflow/core/lib/core/errors.h" 34 #include "tensorflow/core/platform/logging.h" 35 36 namespace xla { 37 Run(HloModule * module)38StatusOr<bool> HloDCE::Run(HloModule* module) { 39 bool changed = false; 40 41 VLOG(2) << "Before dce:"; 42 XLA_VLOG_LINES(2, module->ToString()); 43 44 for (auto* computation : module->MakeComputationPostOrder()) { 45 // Remove any dead roots and their dead transitive operands. Collect them 46 // into a separate list first to avoid problems with iterating through the 47 // computation's instruction while simultaneously removing instructions. 48 std::vector<HloInstruction*> dead_roots; 49 for (auto* instruction : computation->instructions()) { 50 if (instruction != computation->root_instruction() && 51 instruction->user_count() == 0 && 52 computation->IsRemovable(instruction) && 53 !instruction->HasSideEffect()) { 54 dead_roots.push_back(instruction); 55 } 56 } 57 58 for (HloInstruction* dead_root : dead_roots) { 59 VLOG(1) << "Removing dead root " << dead_root->ToString() 60 << " and it's unused operands"; 61 TF_RETURN_IF_ERROR( 62 computation->RemoveInstructionAndUnusedOperands(dead_root)); 63 changed = true; 64 } 65 } 66 67 // Now DCE HloComputations. First, collect the computations that are 68 // referenced by some remaining instruction. 69 absl::flat_hash_set<HloComputation*> live_computations; 70 if (HloComputation* entry_computation = module->entry_computation()) { 71 live_computations.insert(entry_computation); 72 } 73 for (auto* computation : module->MakeComputationPostOrder()) { 74 for (auto* instruction : computation->instructions()) { 75 for (auto* subcomp : instruction->called_computations()) { 76 live_computations.insert(subcomp); 77 } 78 } 79 } 80 81 // Remove dead computations. 82 for (auto* computation : module->MakeComputationPostOrder()) { 83 if (!live_computations.contains(computation)) { 84 TF_RETURN_IF_ERROR(module->RemoveEmbeddedComputation(computation)); 85 changed = true; 86 } 87 } 88 89 VLOG(2) << "After dce:"; 90 XLA_VLOG_LINES(2, module->ToString()); 91 92 return changed; 93 } 94 95 } // namespace xla 96