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

namespace ir {

class IrContext;
class Operation;

namespace detail {
class PassAdaptor;
}

namespace detail {

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

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

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

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

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

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

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

}  // namespace detail

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

79 80
  virtual ~Pass();

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

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

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

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

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

92 93 94
  AnalysisManager analysis_manager() { return pass_state().am; }

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

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

101 102
 private:
  detail::PassInfo pass_info_;
103

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

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

}  // namespace ir