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_INTERFACE_H_ 17 #define TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_INTERFACE_H_ 18 19 #include "tensorflow/compiler/xla/service/hlo_module.h" 20 #include "tensorflow/compiler/xla/service/hlo_module_group.h" 21 #include "tensorflow/compiler/xla/status_macros.h" 22 #include "tensorflow/compiler/xla/statusor.h" 23 #include "tensorflow/compiler/xla/types.h" 24 #include "tensorflow/core/platform/macros.h" 25 26 namespace xla { 27 28 // Base class for HLO passes. These are used with the HloPassPipeline to 29 // organize a sequence of passes. An HLO pass should not extend this class 30 // directly; it should extend HloModulePass or HloModuleGroupPass. 31 class HloPassInterface { 32 public: 33 virtual ~HloPassInterface() = default; 34 virtual absl::string_view name() const = 0; 35 36 // Run the pass on the given HLO module. Returns whether it modified the 37 // module. 38 virtual StatusOr<bool> Run(HloModule* module) = 0; 39 40 // Run the pass on the given HLO module group. Returns whether it modified the 41 // module group. Ideally, the module group variant would be named "Run" as 42 // well, but C++ does not handle overloaded virtual methods well. 43 virtual StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) = 0; 44 }; 45 46 // Base class for passes which are module-scoped. 47 class HloModulePass : public HloPassInterface { 48 public: 49 // Runs the pass on a module group by iterating through each module in the 50 // group. RunOnModuleGroup(HloModuleGroup * module_group)51 StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override { 52 bool changed = false; 53 for (HloModule* module : module_group->modules()) { 54 TF_ASSIGN_OR_RETURN(bool module_changed, Run(module)); 55 changed |= module_changed; 56 } 57 return changed; 58 }; 59 }; 60 61 // Base class for passes which are module-group scoped. These passes cannot run 62 // on an HLO module. 63 class HloModuleGroupPass : public HloPassInterface { 64 public: Run(HloModule * module)65 StatusOr<bool> Run(HloModule* module) override { 66 return InternalError("Module group pass cannot be run on a module"); 67 } 68 }; 69 70 } // namespace xla 71 72 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_INTERFACE_H_ 73