xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/hlo_dce.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_HLO_DCE_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DCE_H_
18 
19 #include "absl/container/flat_hash_map.h"
20 #include "tensorflow/compiler/xla/service/hlo_computation.h"
21 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
22 #include "tensorflow/compiler/xla/service/hlo_module.h"
23 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
24 #include "tensorflow/compiler/xla/statusor.h"
25 
26 namespace xla {
27 
28 // HLO pass which removes dead instructions from each computation in the module
29 // and removes dead computations from the module.
30 //
31 // An instruction is dead if it is not reachable from the root. A computation is
32 // dead if it is not the entry computation of the module and it is not reachable
33 // from the entry computation.
34 //
35 // This pass does not remove dead parameter instructions, as parameter
36 // instructions cannot be deleted.
37 class HloDCE : public HloModulePass {
38  public:
HloDCE()39   HloDCE() : remove_cross_partition_collective_ops_(false) {}
HloDCE(bool remove_cross_partition_collective_ops)40   explicit HloDCE(bool remove_cross_partition_collective_ops)
41       : remove_cross_partition_collective_ops_(
42             remove_cross_partition_collective_ops) {}
~HloDCE()43   ~HloDCE() override {}
name()44   absl::string_view name() const override { return "dce"; }
45 
46   // Run DCE on a computation.
47   static StatusOr<bool> RunOnComputation(
48       HloComputation* computation, bool remove_cross_partition_collective_ops);
49 
50   // Run the pass on the given module. Returns whether the module was changed
51   // (instructions were removed).
52   using HloPassInterface::Run;
53   StatusOr<bool> Run(
54       HloModule* module,
55       const absl::flat_hash_set<absl::string_view>& execution_threads) override;
56 
57  private:
58   // Finds all computations that are not called by any instruction and removes
59   // them from the module. Returns whether any dead code was removed.
60   StatusOr<bool> RecursivelyRemoveDeadComputations(
61       HloModule* module,
62       const absl::flat_hash_set<absl::string_view>& execution_threads);
63 
64   // Given a dead computation, decrements the ref count of all its called
65   // computations and checks if any of the subcomputations become dead after the
66   // removal. Returns whether all dead computations were successfully removed
67   // from the module.
68   Status RecursivelyRemoveDeadComputation(
69       HloModule* module, HloComputation* computation,
70       absl::flat_hash_map<HloComputation*, int>& live_call_counts);
71 
72   bool remove_cross_partition_collective_ops_;
73 };
74 
75 }  // namespace xla
76 
77 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_DCE_H_
78