value.h 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

17 18
#include "paddle/ir/core/cast_utils.h"
#include "paddle/ir/core/type.h"
19 20 21

namespace ir {
class Operation;
22
class Value;
23 24 25 26 27 28 29 30 31 32 33

namespace detail {
class OpOperandImpl;
class ValueImpl;
class OpResultImpl;
}  // namespace detail

///
/// \brief OpOperand class represents the operand of operation. This class only
/// provides interfaces, for specific implementation, see Impl class.
///
34
class IR_API OpOperand {
35 36 37 38 39 40 41 42 43 44 45
 public:
  OpOperand() = default;

  OpOperand(const OpOperand &other) = default;

  OpOperand(const detail::OpOperandImpl *impl);  // NOLINT

  OpOperand &operator=(const OpOperand &rhs);

  OpOperand &operator=(const detail::OpOperandImpl *impl);

46
  bool operator==(const OpOperand &other) const { return impl_ == other.impl_; }
47

48
  bool operator!=(const OpOperand &other) const { return !operator==(other); }
49

50
  bool operator!() const { return impl_ == nullptr; }
51

K
kangguangli 已提交
52
  operator bool() const;
53

54 55 56 57
  OpOperand next_use() const;

  Value source() const;

58
  void set_source(Value value);
59

60
  Operation *owner() const;
61 62 63 64 65

 private:
  detail::OpOperandImpl *impl_{nullptr};
};

66 67 68 69 70 71 72 73 74 75 76
///
/// \brief Value Iterator
///
template <typename OperandType>
class ValueUseIterator {
 public:
  ValueUseIterator(OperandType use = nullptr) : current_(use) {}  // NOLINT

  bool operator==(const ValueUseIterator<OperandType> &rhs) const {
    return current_ == rhs.current_;
  }
77 78 79 80
  bool operator!=(const ValueUseIterator<OperandType> &rhs) const {
    return !(*this == rhs);
  }

81
  ir::Operation *owner() const { return current_.owner(); }
82

83
  OperandType &operator*() { return current_; }
84

85
  OperandType *operator->() { return &operator*(); }
86 87

  ValueUseIterator<OperandType> &operator++() {
88
    current_ = current_.next_use();
89 90 91 92 93 94 95 96 97 98 99 100 101
    return *this;
  }

  ValueUseIterator<OperandType> operator++(int) {
    ValueUseIterator<OperandType> tmp = *this;
    ++*(this);
    return tmp;
  }

 protected:
  OperandType current_;
};

102 103 104 105
///
/// \brief Value class represents the SSA value in the IR system. This class
/// only provides interfaces, for specific implementation, see Impl class.
///
106
class IR_API Value {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
 public:
  Value() = default;

  Value(const detail::ValueImpl *impl);  // NOLINT

  Value(const Value &other) = default;

  bool operator==(const Value &other) const;

  bool operator!=(const Value &other) const;

  bool operator!() const;

  explicit operator bool() const;

  template <typename T>
  bool isa() const {
    return ir::isa<T>(*this);
  }

  template <typename U>
  U dyn_cast() const {
    return ir::dyn_cast<U>(*this);
  }

132
  Type type() const;
133

134
  void set_type(Type type);
135 136 137

  Operation *GetDefiningOp() const;

138
  std::string PrintUdChain();
139

140 141 142 143 144 145 146 147 148
  ///
  /// \brief Provide iterator interface to access Value use chain.
  ///
  using use_iterator = ValueUseIterator<OpOperand>;

  use_iterator begin() const;

  use_iterator end() const;

149 150
  OpOperand first_use() const;

151 152
  friend struct std::hash<Value>;

153 154 155 156 157 158 159 160 161 162
  bool use_empty() const;

  void ReplaceUsesWithIf(
      Value new_value,
      const std::function<bool(OpOperand)> &should_replace) const;

  // The interface shoule ensure impl_ isn't nullptr.
  // if the user can accept impl_ is nullptr, shoule use impl_ member directly.
  detail::ValueImpl *impl() const;

163 164 165 166 167 168 169 170 171
 protected:
  detail::ValueImpl *impl_{nullptr};
};

///
/// \brief OpResult class represents the value defined by a result of operation.
/// This class only provides interfaces, for specific implementation, see Impl
/// class.
///
172
class IR_API OpResult : public Value {
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
 public:
  using Value::Value;

  static bool classof(Value value);

  Operation *owner() const;

  uint32_t GetResultIndex() const;

  friend Operation;

 private:
  static uint32_t GetValidInlineIndex(uint32_t index);

  detail::OpResultImpl *impl() const;
};

}  // namespace ir

namespace std {
template <>
struct hash<ir::Value> {
  std::size_t operator()(const ir::Value &obj) const {
    return std::hash<const ir::detail::ValueImpl *>()(obj.impl_);
  }
};
}  // namespace std