tvm
Loading...
Searching...
No Matches
attrs.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
28#ifndef TVM_IR_ATTRS_H_
29#define TVM_IR_ATTRS_H_
30
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>
37#include <tvm/ir/cow.h>
38
39#include <string>
40#include <type_traits>
41#include <unordered_map>
42#include <utility>
43
44namespace tvm {
45
50class AttrsObj : public ffi::Object {
51 public:
52 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
53 TVM_FFI_DECLARE_OBJECT_INFO("ir.Attrs", AttrsObj, ffi::Object);
54};
55
60class Attrs : public ffi::ObjectRef {
61 public:
62 explicit Attrs(ffi::ObjectPtr<AttrsObj> n) : ffi::ObjectRef(ffi::UnsafeInit{}) {
63 TVM_FFI_ICHECK(n != nullptr);
64 data_ = std::move(n);
65 }
66
68};
69
76class DictAttrsObj : public AttrsObj {
77 public:
79 ffi::Map<ffi::String, ffi::Any> dict;
80
81 static void RegisterReflection() {
82 namespace rfl = ffi::reflection;
83 rfl::ObjectDef<DictAttrsObj>().def_ro("dict", &DictAttrsObj::dict);
84 }
85
86 // type info
88};
89
108class DictAttrs : public Attrs {
109 public:
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);
120 }
121
127 DictAttrs(DictAttrs&& other) noexcept : Attrs(ffi::UnsafeInit{}) {
128 data_ = std::move(other.data_);
129 other.data_ = ffi::make_object<DictAttrsObj>();
130 }
131
137 DictAttrs& operator=(DictAttrs&& other) noexcept {
138 if (this != &other) {
139 data_ = std::move(other.data_);
140 other.data_ = ffi::make_object<DictAttrsObj>();
141 }
142 return *this;
143 }
144
145 // Explicit copy ctor/assign defaults. Declaring the move members above
146 // would otherwise suppress the implicit copy members.
147 DictAttrs(const DictAttrs& other) = default;
148 DictAttrs& operator=(const DictAttrs& other) = default;
149
150 // Utils for accessing attributes
170 template <typename TObjectRef>
171 ffi::Optional<TObjectRef> GetAttr(
172 const std::string& attr_key,
173 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
174 const DictAttrsObj* node = get();
175 auto it = node->dict.find(attr_key);
176 if (it != node->dict.end()) {
177 return (*it).second.cast<TObjectRef>();
178 } else {
179 return default_value;
180 }
181 }
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));
193 }
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)));
199 }
200
201 // variant that uses TObjectRef to enable implicit conversion to 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));
205 }
225 bool HasNonzeroAttr(const std::string& attr_key) const {
226 return GetAttr<int64_t>(attr_key, 0).value_or(0) != 0;
227 }
228
229 // Inline-expand TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE here, minus
230 // the default copy/move it normally injects (we define our own move members
231 // above so the moved-from instance stays defined-but-empty).
232 explicit DictAttrs(::tvm::ffi::UnsafeInit tag) : Attrs(tag) {}
233 using __PtrType =
234 std::conditional_t<DictAttrsObj::_type_mutable, DictAttrsObj*, const DictAttrsObj*>;
235 __PtrType operator->() const { return static_cast<__PtrType>(data_.get()); }
236 __PtrType get() const { return static_cast<__PtrType>(data_.get()); }
237 static constexpr bool _type_is_nullable = false;
240};
241
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(),
281 TypeError)
282 << "Shallow copy must return a distinct object of type " << input->GetTypeKey();
283 input = std::move(copy).cast<TFunc>();
284 }
285 TNode* node = const_cast<TNode*>(input.operator->());
286 // node->attrs is NOTNULLABLE by contract, but defend against a caller
287 // that left a moved-from DictAttrs in place by re-initializing here.
288 if (!node->attrs.defined()) node->attrs = DictAttrs();
289 node->attrs.CopyOnWrite()->dict.Set(attr_key, std::move(attr_value));
290 return input;
291}
292
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>)),
301 int> = 0>
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));
305}
306
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(),
330 TypeError)
331 << "Shallow copy must return a distinct object of type " << input->GetTypeKey();
332 input = std::move(copy).cast<TFunc>();
333 }
334 TNode* node = const_cast<TNode*>(input.operator->());
335 // node->attrs is NOTNULLABLE by contract, but defend against a caller
336 // that left a moved-from DictAttrs in place by re-initializing here.
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);
341 }
342 return input;
343}
344
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(),
383 TypeError)
384 << "Shallow copy must return a distinct object of type " << input->GetTypeKey();
385 input = std::move(copy).cast<TFunc>();
386 }
387 TNode* node = const_cast<TNode*>(input.operator->());
388 // node->attrs is NOTNULLABLE by contract, but defend against a caller
389 // that left a moved-from DictAttrs in place; nothing to erase from an
390 // empty dict.
391 if (!node->attrs.defined()) {
392 node->attrs = DictAttrs();
393 return input;
394 }
395 node->attrs.CopyOnWrite()->dict.erase(attr_key);
396 return input;
397}
398
400template <typename TFunc, typename T>
401inline TFunc WithoutAttr(TFunc input, ffi::reflection::AttrKey<T> key) {
402 return WithoutAttr(std::move(input), key.name);
403}
404
405} // namespace tvm
406#endif // TVM_IR_ATTRS_H_
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