pass.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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>
#include <vector>

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

namespace ir {

class IrContext;
class Operation;

namespace detail {
class PassAdaptor;
}

namespace detail {

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

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

42
  bool pass_failed;
43 44
  AnalysisManager am;
  PreservedAnalyses preserved_analyses;
45 46 47 48 49 50 51 52 53 54 55 56 57 58
};

struct PassInfo {
  PassInfo(const char* name,
           uint8_t opt_level,
           const std::vector<const char* /* pass name */>& dependents = {})
      : name(name), opt_level(opt_level), dependents(dependents) {}

  // Pass name.
  const char* name;

  // 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.
59
  // opt_level=3: layout, etc.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  uint8_t opt_level;

  // The list which pass depends on.
  // PassManager will check the constraint(TODO).
  std::vector<const char*> dependents;
};

}  // namespace detail

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

  virtual ~Pass() = default;

79
  const detail::PassInfo& pass_info() const { return pass_info_; }
80 81 82 83 84 85 86 87 88

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

  // TODO(liuyuanle): Add block/region judgement.
  virtual inline bool CanScheduleOn(ir::Operation* op) const { return true; }

  virtual bool Initialize(ir::IrContext* context) { return true; }

89 90 91 92 93 94 95 96 97 98
  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; }
99

100 101
 private:
  detail::PassInfo pass_info_;
102 103 104 105 106 107 108 109

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

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

}  // namespace ir