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