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/hlo_module.h" 27 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h" 28 #include "tensorflow/compiler/xla/statusor.h" 29 #include "tensorflow/compiler/xla/types.h" 30 #include "tensorflow/core/platform/macros.h" 31 32 namespace xla { 33 34 // Pipeline of HLO passes. 35 class HloPassPipeline : public HloPassInterface { 36 public: HloPassPipeline(const string & name)37 explicit HloPassPipeline(const string& name) : name_(name) {} name()38 absl::string_view name() const override { return name_; } 39 40 // Add a pass to the pipeline. It should be called with the arguments for the 41 // pass constructor: 42 // 43 // pipeline.AddPass<FooPass>(constructor_arg1, constructor_arg2); 44 // 45 // Returns a reference to the added pass. 46 template <typename T, typename... Args> AddPass(Args &&...args)47 T& AddPass(Args&&... args) { 48 CHECK(!run_called_) << "AddPass cannot be called after Run"; 49 auto pass = new T(std::forward<Args>(args)...); 50 passes_.push_back(std::unique_ptr<T>(pass)); 51 return *pass; 52 } 53 54 // Add an invariant-checking pass to the pipeline. It will be run before and 55 // after each HLO pass. The invariant checking pass must not mutate the graph 56 // (it is required to always return "false" from its Run() method). 57 template <typename T, typename... Args> AddInvariantChecker(Args &&...args)58 T& AddInvariantChecker(Args&&... args) { 59 CHECK(!run_called_) << "AddInvariantChecker cannot be called after Run"; 60 auto pass = new T(std::forward<Args>(args)...); 61 invariant_checkers_.push_back(std::unique_ptr<T>(pass)); 62 return *pass; 63 } 64 65 StatusOr<bool> Run(HloModule* module) override; 66 StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override; 67 68 private: 69 // Returns the set of passes which are enabled. DebugOptions can selectively 70 // disable passes via --xla_disable_hlo_passes flag. 71 std::vector<HloPassInterface*> GetEnabledPasses( 72 const DebugOptions& debug_options); 73 74 // Maybe dumps the given module or module group depending on flag values 75 // contained in DebugOptions of module config. 76 void MaybeDumpHlo(const HloModuleGroup& module_group, 77 absl::string_view after_pass_name, 78 absl::string_view before_pass_name); 79 void MaybeDumpHlo(const HloModule& module, absl::string_view after_pass_name, 80 absl::string_view before_pass_name); 81 82 // Runs the invariant checker on the given HLO. HloT can be either HloModule 83 // or HloModuleGroup. 84 template <typename HloT> 85 Status RunInvariantCheckers(HloT* hlo, absl::string_view after_pass_name); 86 87 // Helper which runs the given pass on the given HLO. HloT can be either 88 // HloModule or HloModuleGroup. 89 template <typename HloT> 90 StatusOr<bool> RunPassesInternal(HloT* hlo, 91 absl::Span<HloPassInterface* const> passes); 92 93 // Helpers which run the given passes on the given HLO construct. These 94 // helpers enable templating of the core of the pipeline logic by providing 95 // HloModule and HloModuleGroup specific methods with the same name. RunHelper(HloPassInterface * pass,HloModule * module)96 static StatusOr<bool> RunHelper(HloPassInterface* pass, HloModule* module) { 97 return pass->Run(module); 98 } RunHelper(HloPassInterface * pass,HloModuleGroup * module_group)99 static StatusOr<bool> RunHelper(HloPassInterface* pass, 100 HloModuleGroup* module_group) { 101 return pass->RunOnModuleGroup(module_group); 102 } 103 104 const string name_; 105 std::vector<std::unique_ptr<HloPassInterface>> passes_; 106 std::vector<std::unique_ptr<HloPassInterface>> invariant_checkers_; 107 bool run_called_ = false; 108 }; 109 110 } // namespace xla 111 112 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_PIPELINE_H_ 113