tvm
Loading...
Searching...
No Matches
op.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 */
19
24#ifndef TVM_IR_OP_H_
25#define TVM_IR_OP_H_
26
27#include <tvm/ffi/container/list.h>
28#include <tvm/ffi/expected.h>
29#include <tvm/ffi/optional.h>
30#include <tvm/ffi/reflection/native_function.h>
31#include <tvm/ffi/reflection/registry.h>
32#include <tvm/ir/attrs.h>
33#include <tvm/ir/env_func.h>
34#include <tvm/ir/expr.h>
35#include <tvm/ir/type.h>
36
37#include <string>
38#include <type_traits>
39#include <utility>
40
41namespace tvm {
42
43template <typename>
44class OpAttrMap;
45
47class ArgumentInfoObj : public ffi::Object {
48 public:
50 ffi::String name;
52 ffi::String doc;
53
54 static void RegisterReflection() {
55 namespace refl = ffi::reflection;
56 refl::ObjectDef<ArgumentInfoObj>()
57 .def_ro("name", &ArgumentInfoObj::name)
58 .def_ro("doc", &ArgumentInfoObj::doc);
59 }
60
61 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
62 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.ArgumentInfo", ArgumentInfoObj, ffi::Object);
63};
64
66class ArgumentInfo : public ffi::ObjectRef {
67 public:
68 explicit ArgumentInfo(ffi::UnsafeInit tag, ffi::ObjectPtr<ArgumentInfoObj> n)
69 : ffi::ObjectRef(tag) {
70 data_ = std::move(n);
71 }
72
74};
75
77class OpObj : public ExprObj {
78 private:
79 // Dense process-local index into attribute columns; never serialized.
80 uint32_t index_{0};
81
82 public:
84 ffi::String name;
86 ffi::String doc;
88 ffi::Array<ArgumentInfo> args_info;
90 ffi::Array<ArgumentInfo> ty_args_info;
92 ffi::Optional<ArgumentInfo> var_args_info;
94 ffi::Optional<ArgumentInfo> var_ty_args_info;
96 ffi::String attrs_type_key;
98 uint32_t attrs_type_index{0};
100 ffi::Any validator;
103
105
106 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUniqueInstance;
108
109 private:
110 friend class OpRegistry;
111 friend class OpDef;
112 friend class Op;
113 template <typename>
114 friend class OpAttrMap;
115};
116
140class Op : public Expr {
141 public:
143 explicit Op(ffi::ObjectPtr<OpObj> n) : Expr(std::move(n)) {
144 TVM_FFI_CHECK(defined(), ValueError) << "Op expects a defined OpObj";
145 }
146
154 template <typename ValueType>
155 static OpAttrMap<ValueType> GetAttrMap(const ffi::String& attr_name);
157 template <typename ValueType>
158 static OpAttrMap<ValueType> GetAttrMap(ffi::reflection::AttrKey<ValueType> key);
159
165 TVM_TIRX_DLL static bool HasAttrMap(const ffi::String& attr_name);
167 template <typename ValueType>
168 static bool HasAttrMap(ffi::reflection::AttrKey<ValueType> key) {
169 return HasAttrMap(key.name);
170 }
171
178 TVM_TIRX_DLL static Op Get(const ffi::String& op_name);
180 TVM_TIRX_DLL static ffi::Array<ffi::String> ListNames();
181
183 TVM_FFI_INLINE void Validate(const CallObj* call) const {
184 const auto& validator = get()->validator;
185 if (validator != nullptr) {
186 if (TVM_FFI_PREDICT_FALSE(!call)) {
187 ThrowInvalidCall(get());
188 }
189 using View = ffi::reflection::NativeFunctionView<void(const CallObj*)>;
190 // set_validator stores only an owning NativeFunction with this signature.
191 ffi::detail::AnyUnsafe::CopyFromAnyViewAfterCheck<View>(validator).CallExpected(call).value();
192 }
193 }
195
196 private:
197 friend class OpDef;
205 TVM_TIRX_DLL static void Expose(const ffi::String& op_name);
206
207 TVM_TIRX_DLL static void ThrowInvalidCall(const OpObj* op);
208 TVM_TIRX_DLL static ffi::List<ffi::Any> GetAttrColumn(const ffi::String& attr_name);
209};
210
211namespace op_attr {
213inline constexpr ffi::reflection::AttrKey<
214 ffi::reflection::NativeFunctionView<Type(const CallObj* call)>>
215 kInferType{"FInferType"};
220inline constexpr ffi::reflection::AttrKey<Type> kFixedReturnType{"TFixedReturnType"};
221} // namespace op_attr
222
223// Compatibility names; new callers can deduce the value type from the key.
224using FInferType = decltype(op_attr::kInferType)::ValueType;
225using TFixedReturnType = decltype(op_attr::kFixedReturnType)::ValueType;
226
227namespace sig {
228
230struct SignatureTrait {
232 static constexpr size_t kArgsCount = 0;
234 static constexpr size_t kTyArgsCount = 0;
236 static constexpr size_t kVarArgsCount = 0;
238 static constexpr size_t kVarTyArgsCount = 0;
240 static constexpr size_t kCallAttrsCount = 0;
241};
242
244struct ValidateState {
246 const CallObj* call;
248 const OpObj* op;
250 size_t args_index = 0;
252 size_t ty_args_index = 0;
253};
254
255namespace detail {
256
257template <typename T>
258inline bool ReportTypeMismatch(ffi::Expected<void>* out, const ValidateState* state, bool type_arg,
259 size_t index, ffi::AnyView actual) {
260 const auto& fixed = type_arg ? state->op->ty_args_info : state->op->args_info;
261 const auto& tail = type_arg ? state->op->var_ty_args_info : state->op->var_args_info;
262 std::string name;
263 if (index < fixed.size()) {
264 name = fixed[index]->name;
265 } else if (tail.has_value()) {
266 name = tail.value()->name;
267 }
268 std::string name_clause = name.empty() ? "" : " (`" + name + "`)";
269 TVMFFIAny raw = actual.CopyToTVMFFIAny();
270 *out = TVM_FFI_UNEXPECTED(TypeError)
271 << "Op `" << state->op->name << "`: `" << (type_arg ? "Call.ty_args[" : "Call.args[")
272 << index << "]`" << name_clause << " expected `" << ffi::TypeTraits<T>::TypeStr()
273 << "`, got `" << ffi::TypeTraits<T>::GetMismatchTypeInfo(&raw) << "`.";
274 return false;
275}
276
277} // namespace detail
278
281template <typename T = Expr>
282struct arg : SignatureTrait {
283 static_assert(std::is_base_of_v<Expr, T>, "arg<T> expects an expression view");
285 static constexpr size_t kArgsCount = 1;
287 ffi::String name;
289 ffi::String doc;
290
291 explicit arg(ffi::String name, ffi::String doc = "")
292 : name(std::move(name)), doc(std::move(doc)) {}
293
294 void CollectMetadata(OpObj* op) const {
295 auto info = ffi::make_object<ArgumentInfoObj>();
296 info->name = name;
297 info->doc = doc;
298 op->args_info.push_back(ArgumentInfo(ffi::UnsafeInit{}, std::move(info)));
299 }
300
301 static bool Validate(ValidateState* state, ffi::Expected<void>* out) {
302 if constexpr (!std::is_same_v<T, Expr>) {
303 ffi::AnyView value(state->call->args.GetArrayObj()->begin()[state->args_index]);
304 if (TVM_FFI_PREDICT_FALSE(value == nullptr ||
305 !ffi::detail::AnyUnsafe::CheckAnyViewStrict<T>(value))) {
306 return detail::ReportTypeMismatch<T>(out, state, false, state->args_index, value);
307 }
308 }
309 ++state->args_index;
310 return true;
311 }
312};
313
316template <typename T = Expr>
317struct var_args : SignatureTrait {
318 static_assert(std::is_base_of_v<Expr, T>, "var_args<T> expects an expression view");
320 static constexpr size_t kVarArgsCount = 1;
322 ffi::String name;
324 ffi::String doc;
325
326 explicit var_args(ffi::String name, ffi::String doc = "")
327 : name(std::move(name)), doc(std::move(doc)) {}
328
329 void CollectMetadata(OpObj* op) const {
330 auto info = ffi::make_object<ArgumentInfoObj>();
331 info->name = name;
332 info->doc = doc;
333 op->var_args_info = ArgumentInfo(ffi::UnsafeInit{}, std::move(info));
334 }
335
336 static bool Validate(ValidateState* state, ffi::Expected<void>* out) {
337 if constexpr (!std::is_same_v<T, Expr>) {
338 const ffi::Any* values = state->call->args.GetArrayObj()->begin();
339 for (; state->args_index != state->call->args.size(); ++state->args_index) {
340 ffi::AnyView value(values[state->args_index]);
341 if (TVM_FFI_PREDICT_FALSE(value == nullptr ||
342 !ffi::detail::AnyUnsafe::CheckAnyViewStrict<T>(value))) {
343 return detail::ReportTypeMismatch<T>(out, state, false, state->args_index, value);
344 }
345 }
346 } else {
347 state->args_index = state->call->args.size();
348 }
349 return true;
350 }
351};
352
355template <typename T = Type>
356struct ty_arg : SignatureTrait {
357 static_assert(std::is_base_of_v<Type, T>, "ty_arg<T> expects a type view");
359 static constexpr size_t kTyArgsCount = 1;
361 ffi::String name;
363 ffi::String doc;
364
365 explicit ty_arg(ffi::String name, ffi::String doc = "")
366 : name(std::move(name)), doc(std::move(doc)) {}
367
368 void CollectMetadata(OpObj* op) const {
369 auto info = ffi::make_object<ArgumentInfoObj>();
370 info->name = name;
371 info->doc = doc;
372 op->ty_args_info.push_back(ArgumentInfo(ffi::UnsafeInit{}, std::move(info)));
373 }
374
375 static bool Validate(ValidateState* state, ffi::Expected<void>* out) {
376 if constexpr (!std::is_same_v<T, Type>) {
377 ffi::AnyView value(state->call->ty_args.GetArrayObj()->begin()[state->ty_args_index]);
378 if (TVM_FFI_PREDICT_FALSE(value == nullptr ||
379 !ffi::detail::AnyUnsafe::CheckAnyViewStrict<T>(value))) {
380 return detail::ReportTypeMismatch<T>(out, state, true, state->ty_args_index, value);
381 }
382 }
383 ++state->ty_args_index;
384 return true;
385 }
386};
387
390template <typename T = Type>
391struct var_ty_args : SignatureTrait {
392 static_assert(std::is_base_of_v<Type, T>, "var_ty_args<T> expects a type view");
394 static constexpr size_t kVarTyArgsCount = 1;
396 ffi::String name;
398 ffi::String doc;
399
400 explicit var_ty_args(ffi::String name, ffi::String doc = "")
401 : name(std::move(name)), doc(std::move(doc)) {}
402
403 void CollectMetadata(OpObj* op) const {
404 auto info = ffi::make_object<ArgumentInfoObj>();
405 info->name = name;
406 info->doc = doc;
407 op->var_ty_args_info = ArgumentInfo(ffi::UnsafeInit{}, std::move(info));
408 }
409
410 static bool Validate(ValidateState* state, ffi::Expected<void>* out) {
411 if constexpr (!std::is_same_v<T, Type>) {
412 const ffi::Any* values = state->call->ty_args.GetArrayObj()->begin();
413 for (; state->ty_args_index != state->call->ty_args.size(); ++state->ty_args_index) {
414 ffi::AnyView value(values[state->ty_args_index]);
415 if (TVM_FFI_PREDICT_FALSE(value == nullptr ||
416 !ffi::detail::AnyUnsafe::CheckAnyViewStrict<T>(value))) {
417 return detail::ReportTypeMismatch<T>(out, state, true, state->ty_args_index, value);
418 }
419 }
420 } else {
421 state->ty_args_index = state->call->ty_args.size();
422 }
423 return true;
424 }
425};
426
435template <typename T>
436struct call_attrs : SignatureTrait {
437 static_assert(std::is_base_of_v<AttrsObj, T>, "call_attrs<T> expects an attribute node type");
439 static constexpr size_t kCallAttrsCount = 1;
440
441 void CollectMetadata(OpObj* op) const {
442 uint32_t index = T::RuntimeTypeIndex();
443 op->attrs_type_key = T::_type_key;
444 op->attrs_type_index = index;
445 }
446
447 static bool Validate(ValidateState* state, ffi::Expected<void>* out) {
448 ffi::AnyView attrs(state->call->attrs);
449 if (TVM_FFI_PREDICT_FALSE(!state->call->attrs.has_value() ||
450 !ffi::detail::AnyUnsafe::CheckAnyViewStrict<const T*>(attrs))) {
451 TVMFFIAny raw = attrs.CopyToTVMFFIAny();
452 std::string actual = state->call->attrs.has_value()
453 ? "`" + ffi::TypeTraits<const T*>::GetMismatchTypeInfo(&raw) + "`"
454 : "None";
455 *out = TVM_FFI_UNEXPECTED(TypeError)
456 << "Op `" << state->op->name << "`: Call.attrs expected `"
457 << ffi::TypeTraits<const T*>::TypeStr() << "`, got " << actual;
458 return false;
459 }
460 return true;
461 }
462};
463
464} // namespace sig
465
467class OpDef {
468 public:
473 TVM_TIRX_DLL explicit OpDef(const ffi::String& name);
479 TVM_TIRX_DLL OpDef(const ffi::String& name, const ffi::String& doc);
480 OpDef(const OpDef&) = delete;
481 OpDef& operator=(const OpDef&) = delete;
482 OpDef(OpDef&&) = delete;
483 OpDef& operator=(OpDef&&) = delete;
485 Op op() const { return op_; }
490 inline void expose() const { Op::Expose(op_->name); }
497 TVM_TIRX_DLL OpDef& add_arg(const ffi::String& name, const ffi::String& doc);
503 template <typename AttrsType>
505 uint32_t index = AttrsType::RuntimeTypeIndex();
506 get()->attrs_type_key = AttrsType::_type_key;
507 get()->attrs_type_index = index;
508 return *this;
509 }
515 TVM_TIRX_DLL OpDef& set_attrs_type_key(const ffi::String& key);
522 TVM_TIRX_DLL OpDef& add_ty_arg(const ffi::String& name, const ffi::String& doc);
545 template <typename... Specs>
546 OpDef& signature(const Specs&... specs) {
547 auto updated = ffi::make_object<OpObj>();
548 (ApplySignatureTrait(updated.get(), specs), ...);
549 if (op_->validator == nullptr) {
550 using View = ffi::reflection::NativeFunctionView<void(const CallObj*)>;
551 set_validator(View::FromNative<&ValidateSignature<Specs...>>());
552 get()->validator_is_custom = false;
553 }
554 OpObj* target = get();
555 target->args_info = std::move(updated->args_info);
556 target->ty_args_info = std::move(updated->ty_args_info);
557 target->var_args_info = std::move(updated->var_args_info);
558 target->var_ty_args_info = std::move(updated->var_ty_args_info);
559 target->attrs_type_key = std::move(updated->attrs_type_key);
560 target->attrs_type_index = updated->attrs_type_index;
561 return *this;
562 }
571 template <typename ValueType>
572 OpDef& set_attr(const ffi::String& attr_name, const ValueType& value, bool override = false) {
573 UpdateAttr(attr_name, ffi::Any(value), override);
574 return *this;
575 }
583 template <typename ValueType, typename Value,
584 std::enable_if_t<(std::is_convertible_v<Value&&, ValueType> &&
585 ((!std::is_same_v<std::decay_t<Value>, ffi::Any> &&
586 !std::is_same_v<std::decay_t<Value>, ffi::AnyView>) ||
587 std::is_same_v<ValueType, ffi::Any> ||
588 std::is_same_v<ValueType, ffi::AnyView>)),
589 int> = 0>
590 OpDef& set_attr(ffi::reflection::AttrKey<ValueType> key, Value&& value, bool override = false) {
591 ValueType typed_value(std::forward<Value>(value));
592 return set_attr(key.name, typed_value, override);
593 }
601 template <auto Fn, typename ValueType>
602 OpDef& set_native_fn_attr(ffi::reflection::AttrKey<ValueType> key, bool override = false) {
603 return set_attr(key, ValueType::template FromNative<Fn>(), override);
604 }
624 OpDef& set_validator(ffi::reflection::NativeFunctionView<void(const CallObj*)> validator,
625 bool override = false) {
626 TVM_FFI_CHECK(override || op_->validator == nullptr, ValueError)
627 << "Validator of " << op_->name << " is already registered";
628 get()->validator = ffi::reflection::NativeFunction<void(const CallObj*)>::From(validator);
629 get()->validator_is_custom = true;
630 return *this;
631 }
637 TVM_TIRX_DLL OpDef& reset_attr(const ffi::String& attr_name);
639 template <typename ValueType>
640 OpDef& reset_attr(ffi::reflection::AttrKey<ValueType> key) {
641 return reset_attr(key.name);
642 }
643
644 private:
645 template <typename Spec>
646 static void ApplySignatureTrait(OpObj* op, const Spec& spec) {
647 static_assert(std::is_base_of_v<sig::SignatureTrait, Spec>, "Unknown signature descriptor");
648 spec.CollectMetadata(op);
649 }
650
651 template <typename... Specs>
652 TVM_FFI_INLINE static ffi::Expected<void> ValidateSignature(const CallObj* call) noexcept {
653 static_assert((std::is_base_of_v<sig::SignatureTrait, Specs> && ...),
654 "Unknown signature descriptor");
655 constexpr size_t n_args = (size_t{0} + ... + Specs::kArgsCount);
656 constexpr size_t n_ty_args = (size_t{0} + ... + Specs::kTyArgsCount);
657 constexpr size_t n_var_args = (size_t{0} + ... + Specs::kVarArgsCount);
658 constexpr size_t n_var_ty_args = (size_t{0} + ... + Specs::kVarTyArgsCount);
659 constexpr size_t n_attrs = (size_t{0} + ... + Specs::kCallAttrsCount);
660 static_assert(n_var_args <= 1 && n_var_ty_args <= 1 && n_attrs <= 1,
661 "Duplicate signature tail or call_attrs");
662 const OpObj* op = call ? call->op.as<OpObj>() : nullptr;
663 if (TVM_FFI_PREDICT_FALSE(!op)) {
664 return TVM_FFI_UNEXPECTED(TypeError) << "Invalid Op Call";
665 }
666 if (TVM_FFI_PREDICT_FALSE(!call->args.defined() || !call->ty_args.defined())) {
667 return TVM_FFI_UNEXPECTED(TypeError) << "Op `" << op->name << "`: invalid Call";
668 }
669 ffi::Expected<void> result;
670 auto* out = &result;
671 if (TVM_FFI_PREDICT_FALSE(n_var_args ? call->args.size() < n_args
672 : call->args.size() != n_args)) {
673 *out = TVM_FFI_UNEXPECTED(TypeError)
674 << "Op `" << op->name << "`: Call.args expected " << (n_var_args ? "at least " : "")
675 << n_args << (n_args == 1 ? " argument" : " arguments") << ", got "
676 << call->args.size();
677 return result;
678 }
679 if (TVM_FFI_PREDICT_FALSE(n_var_ty_args ? call->ty_args.size() < n_ty_args
680 : call->ty_args.size() != n_ty_args)) {
681 *out = TVM_FFI_UNEXPECTED(TypeError)
682 << "Op `" << op->name << "`: Call.ty_args expected "
683 << (n_var_ty_args ? "at least " : "") << n_ty_args
684 << (n_ty_args == 1 ? " type argument" : " type arguments") << ", got "
685 << call->ty_args.size();
686 return result;
687 }
688 if constexpr (sizeof...(Specs) != 0) {
689 sig::ValidateState state{call, op};
690 if (TVM_FFI_PREDICT_FALSE(!(Specs::Validate(&state, out) && ...))) {
691 return result;
692 }
693 }
694 return result;
695 }
696
697 OpObj* get() { return const_cast<OpObj*>(op_.operator->()); }
698 TVM_TIRX_DLL void UpdateAttr(const ffi::String& attr_name, ffi::Any value, bool override);
699 Op op_;
700};
701
710template <typename ValueType>
712 public:
718 int count(const Op& op) const {
719 return op->index_ < column_.size() && column_[op->index_] != nullptr;
720 }
726 ValueType operator[](const Op& op) const {
727 TVM_FFI_ICHECK(count(op)) << "Attribute " << attr_name_ << " has not been registered for "
728 << op->name;
729 if constexpr (std::is_same_v<ValueType, ffi::Any>) {
730 return column_[op->index_];
731 } else {
732 return column_[op->index_].template cast<ValueType>();
733 }
734 }
741 ValueType get(const Op& op, ValueType def_value) const {
742 return count(op) ? (*this)[op] : def_value;
743 }
750 ValueType get(const Expr& expr, ValueType def_value) const {
751 TVM_FFI_ICHECK(expr.defined());
752 if (const auto* op = expr.as<OpObj>()) {
753 return get(ffi::GetRef<Op>(op), def_value);
754 }
755 return def_value;
756 }
757
758 private:
759 friend class Op;
760 OpAttrMap(ffi::List<ffi::Any> column, ffi::String attr_name)
761 : column_(std::move(column)), attr_name_(std::move(attr_name)) {}
762 ffi::List<ffi::Any> column_;
763 ffi::String attr_name_;
764};
765
766template <typename ValueType>
767OpAttrMap<ValueType> Op::GetAttrMap(const ffi::String& attr_name) {
768 return OpAttrMap<ValueType>(GetAttrColumn(attr_name), attr_name);
769}
770
771template <typename ValueType>
772OpAttrMap<ValueType> Op::GetAttrMap(ffi::reflection::AttrKey<ValueType> key) {
773 return GetAttrMap<ValueType>(key.name);
774}
775
776} // namespace tvm
777#endif // TVM_IR_OP_H_
Base expr nodes in TVM.
IR/AST nodes for TVM types shared across IR variants.
#define TVM_TIRX_DLL
Definition base.h:55
Helpers for attribute objects.
An operator argument's name and documentation.
Definition op.h:47
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.ArgumentInfo", ArgumentInfoObj, ffi::Object)
ffi::String doc
Argument documentation.
Definition op.h:52
static void RegisterReflection()
Definition op.h:54
ffi::String name
Argument name.
Definition op.h:50
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition op.h:61
Managed reference to an argument descriptor.
Definition op.h:66
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ArgumentInfo, ffi::ObjectRef, ArgumentInfoObj)
ArgumentInfo(ffi::UnsafeInit tag, ffi::ObjectPtr< ArgumentInfoObj > n)
Definition op.h:68
Call corresponds to callable invocation.
Definition expr.h:788
Base type of all the expressions.
Definition expr.h:66
Managed reference to ExprObj.
Definition expr.h:101
Typed live view of an Op attribute column.
Definition op.h:711
int count(const Op &op) const
Check whether an operator has a value in this column.
Definition op.h:718
ValueType operator[](const Op &op) const
Look up an operator's attribute, raising InternalError if absent.
Definition op.h:726
ValueType get(const Expr &expr, ValueType def_value) const
Look up an expression's attribute with a fallback.
Definition op.h:750
ValueType get(const Op &op, ValueType def_value) const
Look up an operator's attribute with a fallback.
Definition op.h:741
Noncopyable temporary builder for a canonical Op; see Op for the example.
Definition op.h:467
void expose() const
Request API exposure after this operator's signature and attributes are complete.
Definition op.h:490
TVM_TIRX_DLL OpDef(const ffi::String &name, const ffi::String &doc)
Get or create the named Op and set its documentation.
OpDef & set_attr(ffi::reflection::AttrKey< ValueType > key, Value &&value, bool override=false)
Register a value using the key's declared type.
Definition op.h:590
TVM_TIRX_DLL OpDef & reset_attr(const ffi::String &attr_name)
Remove the current attribute value, with no priority fallback.
OpDef & operator=(const OpDef &)=delete
TVM_TIRX_DLL OpDef(const ffi::String &name)
Get or create the named Op without changing its documentation.
OpDef & attrs_type()
Set the attribute object type key and runtime index together.
Definition op.h:504
OpDef & set_validator(ffi::reflection::NativeFunctionView< void(const CallObj *)> validator, bool override=false)
Register a validator that checks a Call with this operator.
Definition op.h:624
OpDef(const OpDef &)=delete
TVM_TIRX_DLL OpDef & add_arg(const ffi::String &name, const ffi::String &doc)
Append a required value-argument descriptor.
TVM_TIRX_DLL OpDef & set_attrs_type_key(const ffi::String &key)
Resolve and set the attribute object type key and runtime index together.
OpDef(OpDef &&)=delete
TVM_TIRX_DLL OpDef & add_ty_arg(const ffi::String &name, const ffi::String &doc)
Append a descriptor for a type argument in Call.ty_args.
OpDef & set_attr(const ffi::String &attr_name, const ValueType &value, bool override=false)
Register an extensible attribute, rejecting duplicates unless overridden.
Definition op.h:572
OpDef & reset_attr(ffi::reflection::AttrKey< ValueType > key)
Remove the value named by a typed key. Cached views observe the removal.
Definition op.h:640
OpDef & operator=(OpDef &&)=delete
Op op() const
Get the canonical operator.
Definition op.h:485
OpDef & set_native_fn_attr(ffi::reflection::AttrKey< ValueType > key, bool override=false)
Register a native callback using the key's declared adapter.
Definition op.h:602
OpDef & signature(const Specs &... specs)
Register a complete typed Call signature and its validator.
Definition op.h:546
Metadata for a canonical primitive operator invoked through Call.
Definition op.h:77
static TVM_TIRX_DLL void RegisterReflection()
ffi::Array< ArgumentInfo > ty_args_info
Descriptors for the ordered required type-argument prefix.
Definition op.h:90
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("ir.Op", OpObj, ExprObj)
ffi::Optional< ArgumentInfo > var_ty_args_info
Named variadic type-argument tail, excluded from ty_args_info.
Definition op.h:94
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition op.h:106
ffi::Any validator
Process-local validation callback, omitted from reflection.
Definition op.h:100
bool validator_is_custom
Whether the validator was explicitly registered rather than generated.
Definition op.h:102
ffi::Optional< ArgumentInfo > var_args_info
Named variadic value tail, excluded from the required args_info prefix.
Definition op.h:92
uint32_t attrs_type_index
Runtime index corresponding to attrs_type_key; not serialized.
Definition op.h:98
ffi::String name
Canonical operator name.
Definition op.h:84
ffi::String doc
Operator documentation.
Definition op.h:86
ffi::String attrs_type_key
Attribute object type key, or empty when no attrs type is declared.
Definition op.h:96
friend class OpRegistry
Definition op.h:110
ffi::Array< ArgumentInfo > args_info
Descriptors for the ordered required value-argument prefix.
Definition op.h:88
Reference-counted handle to a canonical named operator.
Definition op.h:140
static TVM_TIRX_DLL bool HasAttrMap(const ffi::String &attr_name)
Check whether an attribute column exists, even if some Ops lack a value.
Op(ffi::ObjectPtr< OpObj > n)
Construct a handle from a defined node.
Definition op.h:143
TVM_FFI_INLINE void Validate(const CallObj *call) const
Validate a Call with this Op's callback, when one is registered.
Definition op.h:183
static bool HasAttrMap(ffi::reflection::AttrKey< ValueType > key)
Check whether the column named by a typed key exists.
Definition op.h:168
static TVM_TIRX_DLL Op Get(const ffi::String &op_name)
Look up a registered operator by name.
static TVM_TIRX_DLL ffi::Array< ffi::String > ListNames()
List registered operator names.
static OpAttrMap< ValueType > GetAttrMap(const ffi::String &attr_name)
Get a typed live view of a registered attribute column.
Definition op.h:767
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Op, Expr, OpObj)
Managed reference to TypeObj.
Definition type.h:75
Serializable global function used in IR.
bool ReportTypeMismatch(ffi::Expected< void > *out, const ValidateState *state, bool type_arg, size_t index, ffi::AnyView actual)
Definition op.h:258
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
decltype(op_attr::kFixedReturnType)::ValueType TFixedReturnType
Definition op.h:225
decltype(op_attr::kInferType)::ValueType FInferType
Definition op.h:224