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_PASS_PIPELINE_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_PIPELINE_H_ 18 19 #include <algorithm> 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "absl/memory/memory.h" 25 #include "absl/strings/str_cat.h" 26 #include "tensorflow/compiler/xla/service/compilation_stats.h" 27 #include "tensorflow/compiler/xla/service/hlo_module.h" 28 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 29 #include "tensorflow/compiler/xla/statusor.h" 30 #include "tensorflow/compiler/xla/types.h" 31 #include "tensorflow/core/platform/macros.h" 32 33 namespace xla { 34 35 // Pipeline of HLO passes. 36 class HloPassPipeline : public HloPassInterface { 37 public: 38 explicit HloPassPipeline(const string& name, 39 CompilationStats* compilation_stats = nullptr) name_(name)40 : name_(name), compilation_stats_(compilation_stats) { 41 if (compilation_stats == nullptr) { 42 empty_compilation_stats_ = CompilationStats::MakeNoopStats(); 43 compilation_stats_ = empty_compilation_stats_.get(); 44 } 45 } name()46 absl::string_view name() const override { return name_; } 47 48 // Add a pass to the pipeline. It should be called with the arguments for the 49 // pass constructor: 50 // 51 // pipeline.AddPass<FooPass>(constructor_arg1, constructor_arg2); 52 // 53 // Returns a reference to the added pass. 54 template <typename T, typename... Args> AddPass(Args &&...args)55 T& AddPass(Args&&... args) { 56 CHECK(!run_called_) << "AddPass cannot be called after Run"; 57 auto pass = new T(std::forward<Args>(args)...); 58 passes_.push_back(std::unique_ptr<T>(pass)); 59 return *pass; 60 } 61 62 // Add an invariant-checking pass to the pipeline. It will be run before and 63 // after each HLO pass. The invariant checking pass must not mutate the graph 64 // (it is required to always return "false" from its Run() method). 65 template <typename T, typename... Args> AddInvariantChecker(Args &&...args)66 T& AddInvariantChecker(Args&&... args) { 67 CHECK(!run_called_) << "AddInvariantChecker cannot be called after Run"; 68 auto pass = new T(std::forward<Args>(args)...); 69 invariant_checkers_.push_back(std::unique_ptr<T>(pass)); 70 return *pass; 71 } 72 73 // Add an invariant-checking pass to the pipeline on debug builds only. 74 template <typename T, typename... Args> AddInvariantCheckerDebug(Args &&...args)75 void AddInvariantCheckerDebug(Args&&... args) { 76 #ifndef NDEBUG 77 AddInvariantChecker<T>(std::forward<Args>(args)...); 78 #endif // NDEBUG 79 } 80 81 StatusOr<bool> Run(HloModule* module) override; 82 StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override; 83 IsPassPipeline()84 bool IsPassPipeline() override { return true; } 85 86 private: 87 // Returns the set of passes which are enabled. DebugOptions can selectively 88 // disable passes via --xla_disable_hlo_passes flag. 89 std::vector<HloPassInterface*> GetEnabledPasses( 90 const DebugOptions& debug_options); 91 92 // Maybe dumps the given module or module group depending on flag values 93 // contained in DebugOptions of module config. If it is dumped, saves the 94 // filenames of the dumps into module metadata. 95 void MaybeDumpHloAndSaveFilenames(HloModuleGroup& module_group, 96 absl::string_view after_pass_name, 97 absl::string_view before_pass_name); 98 void MaybeDumpHloAndSaveFilenames(HloModule& module, 99 absl::string_view after_pass_name, 100 absl::string_view before_pass_name); 101 102 // Runs the invariant checker on the given HLO. HloT can be either HloModule 103 // or HloModuleGroup. 104 template <typename HloT> 105 Status RunInvariantCheckers(HloT* hlo, absl::string_view after_pass_name); 106 107 // Helper which runs the given pass on the given HLO. HloT can be either 108 // HloModule or HloModuleGroup. 109 template <typename HloT> 110 StatusOr<bool> RunPassesInternal(HloT* hlo, 111 absl::Span<HloPassInterface* const> passes); 112 113 // Helpers which run the given passes on the given HLO construct. These 114 // helpers enable templating of the core of the pipeline logic by providing 115 // HloModule and HloModuleGroup specific methods with the same name. RunHelper(HloPassInterface * pass,HloModule * module)116 static StatusOr<bool> RunHelper(HloPassInterface* pass, HloModule* module) { 117 TF_ASSIGN_OR_RETURN(bool changed, pass->Run(module)); 118 module->Cleanup(); 119 return changed; 120 } RunHelper(HloPassInterface * pass,HloModuleGroup * module_group)121 static StatusOr<bool> RunHelper(HloPassInterface* pass, 122 HloModuleGroup* module_group) { 123 TF_ASSIGN_OR_RETURN(bool changed, pass->RunOnModuleGroup(module_group)); 124 module_group->Cleanup(); 125 return changed; 126 } 127 128 const string name_; 129 std::vector<std::unique_ptr<HloPassInterface>> passes_; 130 std::vector<std::unique_ptr<HloPassInterface>> invariant_checkers_; 131 bool run_called_ = false; 132 133 CompilationStats* compilation_stats_; 134 // Default stats instance for when one is not passed in the constructor. 135 // Use via compilation_stats_, not directly. 136 std::unique_ptr<CompilationStats> empty_compilation_stats_; 137 }; 138 139 } // namespace xla 140 141 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_PIPELINE_H_ 142