Home | History | Annotate | Download | only in service
      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 "tensorflow/compiler/xla/ptr_util.h"
     25 #include "tensorflow/compiler/xla/service/hlo_module.h"
     26 #include "tensorflow/compiler/xla/service/hlo_pass_interface.h"
     27 #include "tensorflow/compiler/xla/statusor.h"
     28 #include "tensorflow/compiler/xla/types.h"
     29 #include "tensorflow/core/platform/macros.h"
     30 
     31 namespace xla {
     32 
     33 // Pipeline of HLO passes.
     34 class HloPassPipeline : public HloPassInterface {
     35  public:
     36   explicit HloPassPipeline(const string& name) : name_(name) {}
     37   tensorflow::StringPiece name() const override { return name_; }
     38 
     39   // Add a pass to the pipeline. It should be called with the arguments for the
     40   // pass constructor:
     41   //
     42   //   pipeline.AddPass<FooPass>(constructor_arg1, constructor_arg2);
     43   //
     44   // Returns a reference to the added pass.
     45   template <typename T, typename... Args>
     46   T& AddPass(Args&&... args) {
     47     CHECK(!run_called_) << "AddPass cannot be called after Run";
     48     auto pass = new T(std::forward<Args>(args)...);
     49     passes_.push_back(std::unique_ptr<T>(pass));
     50     return *pass;
     51   }
     52 
     53   // Add an invariant-checking pass to the pipeline. It will be run before and
     54   // after each HLO pass. The invariant checking pass must not mutate the graph
     55   // (it is required to always return "false" from its Run() method).
     56   template <typename T, typename... Args>
     57   T& AddInvariantChecker(Args&&... args) {
     58     CHECK(!run_called_) << "AddInvariantChecker cannot be called after Run";
     59     auto pass = new T(std::forward<Args>(args)...);
     60     invariant_checkers_.push_back(std::unique_ptr<T>(pass));
     61     return *pass;
     62   }
     63 
     64   // Run all passes on the given HLO module.
     65   StatusOr<bool> Run(HloModule* module) override;
     66 
     67  private:
     68   const string name_;
     69   std::vector<std::unique_ptr<HloPassInterface>> passes_;
     70   std::vector<std::unique_ptr<HloPassInterface>> invariant_checkers_;
     71   bool run_called_ = false;
     72 
     73   TF_DISALLOW_COPY_AND_ASSIGN(HloPassPipeline);
     74 };
     75 
     76 }  // namespace xla
     77 
     78 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_HLO_PASS_PIPELINE_H_
     79