pass.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#pragma once

#include <cstdint>
18
#include <string>
19 20
#include <vector>

21 22
#include "paddle/ir/pass/analysis_manager.h"
#include "paddle/phi/core/enforce.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "paddle/utils/optional.h"

namespace ir {

class IrContext;
class Operation;

namespace detail {
class PassAdaptor;
}

namespace detail {

struct PassExecutionState {
37
  explicit PassExecutionState(Operation* ir, const AnalysisManager& am)
38
      : ir(ir), pass_failed(false), am(am) {}
39

40
  // The IR currently being processed by pass.
41
  Operation* ir;
42

43
  bool pass_failed;
44 45
  AnalysisManager am;
  PreservedAnalyses preserved_analyses;
46 47 48
};

struct PassInfo {
49
  PassInfo(const std::string& name,
50
           uint8_t opt_level,
51
           const std::vector<std::string /* pass name */>& dependents = {})
52 53 54
      : name(name), opt_level(opt_level), dependents(dependents) {}

  // Pass name.
55
  std::string name;
56 57 58 59

  // opt_level=0: the basic pass which framework need.
  // opt_level=1: the fusion logical pass.
  // opt_level=2: constant fold, cse, memory optimize, etc.
60
  // opt_level=3: layout, etc.
61 62 63 64
  uint8_t opt_level;

  // The list which pass depends on.
  // PassManager will check the constraint(TODO).
65
  std::vector<std::string> dependents;
66 67 68 69 70 71 72
};

}  // namespace detail

/// We can access pass only from PassManager.
class Pass {
 public:
73
  explicit Pass(const std::string& name,
74
                uint8_t opt_level,
75
                const std::vector<std::string>& dependents = {})
76
      : pass_info_(name, opt_level, dependents) {}
77

78 79 80
  virtual ~Pass();

  std::string name() { return pass_info().name; }
81

82
  const detail::PassInfo& pass_info() const { return pass_info_; }
83 84

 protected:
85
  virtual void Run(Operation* op) = 0;
86

87
  virtual inline bool CanApplyOn(Operation* op) const;
88

89
  virtual bool Initialize(IrContext* context) { return true; }
90

91 92 93 94 95 96 97 98 99 100
  AnalysisManager analysis_manager() { return pass_state().am; }

  detail::PassExecutionState& pass_state() {
    PADDLE_ENFORCE_EQ(pass_state_.is_initialized(),
                      true,
                      phi::errors::Fatal("pass state was never initialized"));
    return *pass_state_;
  }

  void SignalPassFailure() { pass_state().pass_failed = true; }
101

102 103
 private:
  detail::PassInfo pass_info_;
104 105 106 107 108 109 110 111

  paddle::optional<detail::PassExecutionState> pass_state_;

  friend class PassManager;
  friend class detail::PassAdaptor;
};

}  // namespace ir