value.h 3.9 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
#include "paddle/ir/core/use_iterator.h"
20 21 22

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

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

///
32 33
/// \brief OpOperand class represents the op_operand of operation. This class
/// only provides interfaces, for specific implementation, see Impl class.
34
///
35
class IR_API OpOperand {
36 37 38 39 40 41 42 43 44 45 46
 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);

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

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

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

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

55 56 57 58
  OpOperand next_use() const;

  Value source() const;

59 60
  Type type() const;

61
  void set_source(Value value);
62

63
  Operation *owner() const;
64

65 66 67 68
  void RemoveFromUdChain();

  friend Operation;

69 70 71 72 73 74 75 76
 private:
  detail::OpOperandImpl *impl_{nullptr};
};

///
/// \brief Value class represents the SSA value in the IR system. This class
/// only provides interfaces, for specific implementation, see Impl class.
///
77
class IR_API Value {
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
 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);
  }

103
  Type type() const;
104

105
  void set_type(Type type);
106 107 108

  Operation *GetDefiningOp() const;

109
  std::string PrintUdChain();
110

111 112 113
  ///
  /// \brief Provide iterator interface to access Value use chain.
  ///
114
  using UseIterator = ValueUseIterator<OpOperand>;
115

116
  UseIterator use_begin() const;
117

118
  UseIterator use_end() const;
119

120 121
  OpOperand first_use() const;

122 123
  bool use_empty() const;

124 125 126 127
  bool HasOneUse() const;

  friend struct std::hash<Value>;

128 129 130
  void ReplaceUsesWithIf(
      Value new_value,
      const std::function<bool(OpOperand)> &should_replace) const;
131
  void ReplaceAllUsesWith(Value new_value) const;
132

133 134
  detail::ValueImpl *impl() { return impl_; }
  const detail::ValueImpl *impl() const { return impl_; }
135

136 137 138 139 140 141 142 143 144
 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.
///
145
class IR_API OpResult : public Value {
146 147 148 149 150 151 152 153 154
 public:
  using Value::Value;

  static bool classof(Value value);

  Operation *owner() const;

  uint32_t GetResultIndex() const;

X
xiaoguoguo626807 已提交
155 156
  bool operator==(const OpResult &other) const;

157 158
  friend Operation;

X
xiaoguoguo626807 已提交
159
  detail::ValueImpl *value_impl() const;
160
  detail::OpResultImpl *impl() const;
X
xiaoguoguo626807 已提交
161

162 163 164 165 166 167 168 169 170 171 172 173 174
 private:
  static uint32_t GetValidInlineIndex(uint32_t index);
};

}  // 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_);
  }
};
X
xiaoguoguo626807 已提交
175

176
}  // namespace std