value.h 4.8 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

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

///
31 32
/// \brief OpOperand class represents the op_operand of operation. This class
/// only provides interfaces, for specific implementation, see Impl class.
33
///
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 59
  Type type() const;

60
  void set_source(Value value);
61

62
  Operation *owner() const;
63

64 65 66 67
  void RemoveFromUdChain();

  friend Operation;

68
 private:
69 70 71 72
  // The interface shoule ensure impl_ isn't nullptr.
  // if the user can accept impl_ is nullptr, shoule use impl_ member directly.
  detail::OpOperandImpl *impl() const;

73 74 75
  detail::OpOperandImpl *impl_{nullptr};
};

76 77 78 79 80 81 82 83 84 85 86
///
/// \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_;
  }
87 88 89 90
  bool operator!=(const ValueUseIterator<OperandType> &rhs) const {
    return !(*this == rhs);
  }

91
  ir::Operation *owner() const { return current_.owner(); }
92

93
  OperandType &operator*() { return current_; }
94

95
  OperandType *operator->() { return &operator*(); }
96 97

  ValueUseIterator<OperandType> &operator++() {
98
    current_ = current_.next_use();
99 100 101 102 103 104 105 106 107 108 109 110 111
    return *this;
  }

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

 protected:
  OperandType current_;
};

112 113 114 115
///
/// \brief Value class represents the SSA value in the IR system. This class
/// only provides interfaces, for specific implementation, see Impl class.
///
116
class IR_API Value {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
 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);
  }

142
  Type type() const;
143

144
  void set_type(Type type);
145 146 147

  Operation *GetDefiningOp() const;

148
  std::string PrintUdChain();
149

150 151 152 153 154 155 156 157 158
  ///
  /// \brief Provide iterator interface to access Value use chain.
  ///
  using use_iterator = ValueUseIterator<OpOperand>;

  use_iterator begin() const;

  use_iterator end() const;

159 160
  OpOperand first_use() const;

161 162
  bool use_empty() const;

163 164 165 166
  bool HasOneUse() const;

  friend struct std::hash<Value>;

167 168 169
  void ReplaceUsesWithIf(
      Value new_value,
      const std::function<bool(OpOperand)> &should_replace) const;
170
  void ReplaceAllUsesWith(Value new_value) const;
171 172 173 174 175

  // 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;

176 177 178 179 180 181 182 183 184
 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.
///
185
class IR_API OpResult : public Value {
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
 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