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 IsPassPipeline()45 virtual bool IsPassPipeline() { return false; } 46 }; 47 48 // Base class for passes which are module-scoped. 49 class HloModulePass : public HloPassInterface { 50 public: 51 // Runs the pass on a module group by iterating through each module in the 52 // group. RunOnModuleGroup(HloModuleGroup * module_group)53 StatusOr<bool> RunOnModuleGroup(HloModuleGroup* module_group) override { 54 bool changed = false; 55 for (HloModule* module : module_group->modules()) { 56 TF_ASSIGN_OR_RETURN(bool module_changed, Run(module)); 57 changed |= module_changed; 58 } 59 return changed; 60 }; 61 62 // Update the layout of a Shape to one that is supported by a given backend. 63 // One can call this function after modifying the Shape in case that modifying 64 // the Shape requires changes to the layout for the given Backend. 65 // 66 // TODO(b/129084868): Make this Backend dependent instead of requiring 67 // deriving from the pass the and overriding this function. UpdateLayout(Shape * shape)68 virtual void UpdateLayout(Shape* shape) {} 69 }; 70 71 // Base class for passes which are module-group scoped. These passes cannot run 72 // on an HLO module. 73 class HloModuleGroupPass : public HloPassInterface { 74 public: Run(HloModule * module)75 StatusOr<bool> Run(HloModule* module) override { 76 return InternalError("Module group pass cannot be run on a module"); 77 } 78 }; 79 80 } // namespace xla 81 82 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_INTERFACE_H_ 83