28#ifndef TVM_IR_ATTRS_H_
29#define TVM_IR_ATTRS_H_
31#include <tvm/ffi/container/map.h>
32#include <tvm/ffi/extra/structural_equal.h>
33#include <tvm/ffi/extra/structural_hash.h>
34#include <tvm/ffi/function.h>
35#include <tvm/ffi/reflection/registry.h>
40#include <unordered_map>
59class Attrs :
public ffi::ObjectRef {
73 ffi::Map<ffi::String, ffi::Any>
dict;
76 namespace rfl = ffi::reflection;
110 explicit DictAttrs(ffi::Map<ffi::String, Any> dict = {}) {
111 ffi::ObjectPtr<DictAttrsNode>
n = ffi::make_object<DictAttrsNode>();
112 n->dict = std::move(dict);
113 data_ = std::move(
n);
122 data_ = std::move(
other.data_);
123 other.data_ = ffi::make_object<DictAttrsNode>();
132 if (
this != &
other) {
133 data_ = std::move(
other.data_);
134 other.data_ = ffi::make_object<DictAttrsNode>();
164 template <
typename TObjectRef>
166 const std::string& attr_key,
167 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt))
const {
169 auto it = node->
dict.find(attr_key);
170 if (
it != node->
dict.end()) {
173 return default_value;
177 template <
typename TObjectRef>
178 ffi::Optional<TObjectRef>
GetAttr(
const std::string& attr_key,
TObjectRef default_value)
const {
209 std::conditional_t<DictAttrsNode::_type_mutable, DictAttrsNode*, const DictAttrsNode*>;
244template <
typename TFunc>
246 using TNode =
typename TFunc::ContainerType;
247 static_assert(TNode::_type_final,
"Can only operate on the leaf nodes");
251 if (!node->attrs.defined()) node->attrs =
DictAttrs();
252 node->attrs.CopyOnWrite()->dict.Set(attr_key, std::move(
attr_value));
266template <
typename TFunc>
268 using TNode =
typename TFunc::ContainerType;
269 static_assert(TNode::_type_final,
"Can only operate on the leaf nodes");
270 if (attrs.empty())
return input;
274 if (!node->attrs.defined()) node->attrs =
DictAttrs();
275 auto*
dict_node = node->attrs.CopyOnWrite();
276 for (
const auto& [k,
v] : attrs) {
308template <
typename TFunc>
310 using TNode =
typename TFunc::ContainerType;
311 static_assert(TNode::_type_final,
"Can only operate on the leaf nodes");
316 if (!node->attrs.defined()) {
320 node->attrs.CopyOnWrite()->dict.erase(attr_key);
Base class of all attribute class.
Definition attrs.h:49
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition attrs.h:51
TVM_FFI_DECLARE_OBJECT_INFO("ir.Attrs", AttrsNode, ffi::Object)
Managed reference to AttrsNode.
Definition attrs.h:59
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Attrs, ffi::ObjectRef, AttrsNode)
Specialized attribute type that is backed by a map. The DictAttrsNode implements the Attrs behavior,...
Definition attrs.h:70
static void RegisterReflection()
Definition attrs.h:75
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.DictAttrs", DictAttrsNode, AttrsNode)
ffi::Map< ffi::String, ffi::Any > dict
internal attrs map
Definition attrs.h:73
Managed reference to DictAttrsNode.
Definition attrs.h:102
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get a function attribute.
Definition attrs.h:165
DictAttrs & operator=(const DictAttrs &other)=default
bool HasNonzeroAttr(const std::string &attr_key) const
Check whether the function has an non-zero integer attr.
Definition attrs.h:200
__PtrType operator->() const
Definition attrs.h:210
DictAttrs(::tvm::ffi::UnsafeInit tag)
Definition attrs.h:207
DictAttrs(const DictAttrs &other)=default
std::conditional_t< DictAttrsNode::_type_mutable, DictAttrsNode *, const DictAttrsNode * > __PtrType
Definition attrs.h:209
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition attrs.h:178
DictAttrs(ffi::Map< ffi::String, Any > dict={})
Construct a DictAttrs backed by DictAttrsNode.
Definition attrs.h:110
DictAttrs(DictAttrs &&other) noexcept
Move constructor that leaves the source in a defined-but-empty state rather than null,...
Definition attrs.h:121
DictAttrs & operator=(DictAttrs &&other) noexcept
Move assignment that leaves the source in a defined-but-empty state rather than null,...
Definition attrs.h:131
__PtrType get() const
Definition attrs.h:211
TVM_DEFINE_OBJECT_REF_COW_METHOD(DictAttrsNode)
static constexpr bool _type_is_nullable
Definition attrs.h:212
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Copy-on-write helper macro for IR ffi::ObjectRef types.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
TFunc WithAttrs(TFunc input, ffi::Map< ffi::String, Any > attrs)
Copy the function or module, but overrides the attributes with the entries from attrs.
Definition attrs.h:267
TFunc WithAttr(TFunc input, const std::string &attr_key, Any attr_value)
Copy the function or module, but overrides the attribute value key with the value.
Definition attrs.h:245
TFunc WithoutAttr(TFunc input, const std::string &attr_key)
Copy the function or module, but removes the specified attribute.
Definition attrs.h:309