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/accessor.h>
36#include <tvm/ffi/reflection/registry.h>
41#include <unordered_map>
60class Attrs :
public ffi::ObjectRef {
62 explicit Attrs(ffi::ObjectPtr<AttrsObj> n) : ffi::ObjectRef(ffi::UnsafeInit{}) {
63 TVM_FFI_ICHECK(n !=
nullptr);
79 ffi::Map<ffi::String, ffi::Any>
dict;
82 namespace rfl = ffi::reflection;
116 explicit DictAttrs(ffi::Map<ffi::String, Any> dict = {}) :
Attrs(ffi::UnsafeInit{}) {
117 ffi::ObjectPtr<DictAttrsObj> n = ffi::make_object<DictAttrsObj>();
118 n->dict = std::move(dict);
119 data_ = std::move(n);
128 data_ = std::move(other.data_);
129 other.data_ = ffi::make_object<DictAttrsObj>();
138 if (
this != &other) {
139 data_ = std::move(other.data_);
140 other.data_ = ffi::make_object<DictAttrsObj>();
170 template <
typename TObjectRef>
172 const std::string& attr_key,
173 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt))
const {
175 auto it = node->
dict.find(attr_key);
176 if (it != node->
dict.end()) {
177 return (*it).second.cast<TObjectRef>();
179 return default_value;
188 template <
typename T>
189 ffi::Optional<T>
GetAttr(ffi::reflection::AttrKey<T> key,
190 ffi::Optional<
typename ffi::reflection::AttrKey<T>::ValueType>
191 default_value = std::nullopt)
const {
192 return GetAttr<T>(key.name, std::move(default_value));
195 template <
typename T>
196 ffi::Optional<T>
GetAttr(ffi::reflection::AttrKey<T> key,
197 typename ffi::reflection::AttrKey<T>::ValueType default_value)
const {
198 return GetAttr(key, ffi::Optional<T>(std::move(default_value)));
202 template <
typename TObjectRef>
203 ffi::Optional<TObjectRef>
GetAttr(
const std::string& attr_key, TObjectRef default_value)
const {
204 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
226 return GetAttr<int64_t>(attr_key, 0).value_or(0) != 0;
234 std::conditional_t<DictAttrsObj::_type_mutable, DictAttrsObj*, const DictAttrsObj*>;
270template <
typename TFunc>
271inline TFunc
WithAttr(TFunc input,
const std::string& attr_key, Any attr_value) {
272 using TNode =
typename TFunc::ContainerType;
273 if (!input.unique()) {
274 static ffi::reflection::TypeAttrColumn shallow_copy(ffi::type_attr::kShallowCopy);
275 ffi::AnyView copy_func = shallow_copy[input->type_index()];
276 TVM_FFI_CHECK(copy_func.type_index() == ffi::TypeIndex::kTVMFFIFunction, TypeError)
277 <<
"Type " << input->GetTypeKey()
278 <<
" must register an ffi.Function for __ffi_shallow_copy__";
279 Any copy = copy_func.cast<ffi::Function>()(input);
280 TVM_FFI_CHECK(copy.type_index() == input->type_index() && copy.as<ffi::Object>() != input.get(),
282 <<
"Shallow copy must return a distinct object of type " << input->GetTypeKey();
283 input = std::move(copy).cast<TFunc>();
285 TNode* node =
const_cast<TNode*
>(input.operator->());
288 if (!node->attrs.defined()) node->attrs =
DictAttrs();
289 node->attrs.CopyOnWrite()->dict.Set(attr_key, std::move(attr_value));
296template <
typename TFunc,
typename T,
typename Value,
297 std::enable_if_t<(std::is_convertible_v<Value&&, T> &&
298 ((!std::is_same_v<std::decay_t<Value>, ffi::Any> &&
299 !std::is_same_v<std::decay_t<Value>, ffi::AnyView>) ||
300 std::is_same_v<T, ffi::Any> || std::is_same_v<T, ffi::AnyView>)),
302inline TFunc
WithAttr(TFunc input, ffi::reflection::AttrKey<T> key, Value&& value) {
303 T typed_value(std::forward<Value>(value));
304 return WithAttr(std::move(input), key.name, ffi::Any(typed_value));
318template <
typename TFunc>
319inline TFunc
WithAttrs(TFunc input, ffi::Map<ffi::String, Any> attrs) {
320 using TNode =
typename TFunc::ContainerType;
321 if (attrs.empty())
return input;
322 if (!input.unique()) {
323 static ffi::reflection::TypeAttrColumn shallow_copy(ffi::type_attr::kShallowCopy);
324 ffi::AnyView copy_func = shallow_copy[input->type_index()];
325 TVM_FFI_CHECK(copy_func.type_index() == ffi::TypeIndex::kTVMFFIFunction, TypeError)
326 <<
"Type " << input->GetTypeKey()
327 <<
" must register an ffi.Function for __ffi_shallow_copy__";
328 Any copy = copy_func.cast<ffi::Function>()(input);
329 TVM_FFI_CHECK(copy.type_index() == input->type_index() && copy.as<ffi::Object>() != input.get(),
331 <<
"Shallow copy must return a distinct object of type " << input->GetTypeKey();
332 input = std::move(copy).cast<TFunc>();
334 TNode* node =
const_cast<TNode*
>(input.operator->());
337 if (!node->attrs.defined()) node->attrs =
DictAttrs();
338 auto* dict_node = node->attrs.CopyOnWrite();
339 for (
const auto& [k, v] : attrs) {
340 dict_node->dict.Set(k, v);
372template <
typename TFunc>
373inline TFunc
WithoutAttr(TFunc input,
const std::string& attr_key) {
374 using TNode =
typename TFunc::ContainerType;
375 if (!input.unique()) {
376 static ffi::reflection::TypeAttrColumn shallow_copy(ffi::type_attr::kShallowCopy);
377 ffi::AnyView copy_func = shallow_copy[input->type_index()];
378 TVM_FFI_CHECK(copy_func.type_index() == ffi::TypeIndex::kTVMFFIFunction, TypeError)
379 <<
"Type " << input->GetTypeKey()
380 <<
" must register an ffi.Function for __ffi_shallow_copy__";
381 Any copy = copy_func.cast<ffi::Function>()(input);
382 TVM_FFI_CHECK(copy.type_index() == input->type_index() && copy.as<ffi::Object>() != input.get(),
384 <<
"Shallow copy must return a distinct object of type " << input->GetTypeKey();
385 input = std::move(copy).cast<TFunc>();
387 TNode* node =
const_cast<TNode*
>(input.operator->());
391 if (!node->attrs.defined()) {
395 node->attrs.CopyOnWrite()->dict.erase(attr_key);
400template <
typename TFunc,
typename T>
401inline TFunc
WithoutAttr(TFunc input, ffi::reflection::AttrKey<T> key) {
Base class of all attribute class.
Definition attrs.h:50
TVM_FFI_DECLARE_OBJECT_INFO("ir.Attrs", AttrsObj, ffi::Object)
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition attrs.h:52
Managed reference to AttrsObj.
Definition attrs.h:60
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Attrs, ffi::ObjectRef, AttrsObj)
Attrs(ffi::ObjectPtr< AttrsObj > n)
Definition attrs.h:62
Specialized attribute type that is backed by a map. The DictAttrsObj implements the Attrs behavior,...
Definition attrs.h:76
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.DictAttrs", DictAttrsObj, AttrsObj)
ffi::Map< ffi::String, ffi::Any > dict
internal attrs map
Definition attrs.h:79
static void RegisterReflection()
Definition attrs.h:81
Managed reference to DictAttrsObj.
Definition attrs.h:108
TVM_DEFINE_OBJECT_REF_COW_METHOD(DictAttrsObj)
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:171
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:225
__PtrType operator->() const
Definition attrs.h:235
DictAttrs(::tvm::ffi::UnsafeInit tag)
Definition attrs.h:232
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, ffi::Optional< typename ffi::reflection::AttrKey< T >::ValueType > default_value=std::nullopt) const
Get an attribute with its value type deduced only from the key.
Definition attrs.h:189
DictAttrs(const DictAttrs &other)=default
std::conditional_t< DictAttrsObj::_type_mutable, DictAttrsObj *, const DictAttrsObj * > __PtrType
Definition attrs.h:234
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition attrs.h:203
DictAttrs(ffi::Map< ffi::String, Any > dict={})
Construct a DictAttrs backed by DictAttrsObj.
Definition attrs.h:116
DictAttrs(DictAttrs &&other) noexcept
Move constructor that leaves the source in a defined-but-empty state rather than null,...
Definition attrs.h:127
DictAttrs & operator=(DictAttrs &&other) noexcept
Move assignment that leaves the source in a defined-but-empty state rather than null,...
Definition attrs.h:137
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, typename ffi::reflection::AttrKey< T >::ValueType default_value) const
Accept a default convertible to the key's value type.
Definition attrs.h:196
__PtrType get() const
Definition attrs.h:236
static constexpr bool _type_is_nullable
Definition attrs.h:237
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 analysis.h:41
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:319
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:271
TFunc WithoutAttr(TFunc input, const std::string &attr_key)
Copy the function or module, but removes the specified attribute.
Definition attrs.h:373