pass.h 2.8 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
#include "paddle/ir/core/enforce.h"
22
#include "paddle/ir/pass/analysis_manager.h"
23
#include "paddle/phi/core/enforce.h"
24 25 26 27 28 29 30 31 32 33 34 35 36

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

  // opt_level=0: the basic pass which framework need.
58 59
  // opt_level=1: constant fold, cse, memory optimize, etc.
  // opt_level=2: the fusion logical pass.
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
};

}  // namespace detail

/// We can access pass only from PassManager.
71
class IR_API Pass {
72
 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
  virtual ~Pass();

80
  std::string name() const { 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
  AnalysisManager analysis_manager() { return pass_state().am; }

  detail::PassExecutionState& pass_state() {
94
    IR_ENFORCE(pass_state_.has_value() == true, "pass state has no value");
95 96 97 98
    return *pass_state_;
  }

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

100 101
 private:
  detail::PassInfo pass_info_;
102

103
  std::optional<detail::PassExecutionState> pass_state_;
104 105 106 107 108 109

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

}  // namespace ir