28#ifndef TVM_IR_TRANSFORM_H_
29#define TVM_IR_TRANSFORM_H_
31#include <tvm/ffi/container/array.h>
32#include <tvm/ffi/function.h>
33#include <tvm/ffi/reflection/creator.h>
34#include <tvm/ffi/reflection/registry.h>
35#include <tvm/ffi/rvalue_ref.h>
36#include <tvm/ffi/string.h>
70 explicit PassContext(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
74 explicit PassContext(ffi::UnsafeInit tag, ffi::ObjectPtr<PassContextObj> n);
152 template <
typename ValueType>
154 if constexpr (std::is_base_of_v<ffi::ObjectRef, ValueType>) {
156 int32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
157 auto type_key = ffi::TypeIndexToTypeKey(tindex);
158 auto legalization = [=](ffi::Any value) -> ffi::Any {
159 if (
auto opt_map = value.try_cast<ffi::Map<ffi::String, ffi::Any>>()) {
160 return ffi::reflection::ObjectCreator(type_key)(opt_map.value());
162 auto opt_val = value.try_cast<ValueType>();
163 if (!opt_val.has_value()) {
164 TVM_FFI_THROW(AttributeError)
165 <<
"Expect config " << key <<
" to have type " << type_key <<
", but instead get "
166 << ffi::detail::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
174 std::string type_str = ffi::TypeTraits<ValueType>::TypeStr();
175 auto legalization = [=](ffi::Any value) -> ffi::Any {
176 auto opt_val = value.try_cast<ValueType>();
177 if (!opt_val.has_value()) {
178 TVM_FFI_THROW(AttributeError)
179 <<
"Expect config " << key <<
" to have type " << type_str <<
", but instead get "
180 << ffi::detail::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
181 TVM_FFI_UNREACHABLE();
201 const char* key, ffi::String value_type_str,
202 ffi::TypedFunction<ffi::Any(ffi::Any)> legalization);
215template <
typename TConfig>
217 static_assert(std::is_base_of_v<ffi::ObjectRef, TConfig>,
218 "Can only create ObjectRef-derived types");
219 using ContainerType =
typename TConfig::ContainerType;
220 static auto finit_object = ffi::Function::GetGlobalRequired(
"ffi.MakeObjectFromPackedArgs");
221 ffi::AnyView packed_args[1];
222 packed_args[0] = ContainerType::RuntimeTypeIndex();
224 finit_object.CallPacked(ffi::PackedArgs(packed_args, 1), &rv);
225 return rv.cast<TConfig>();
243 namespace refl = tvm::ffi::reflection;
244 refl::ObjectDef<PassInfoObj>()
263 auto pass_info = ffi::make_object<PassInfoObj>();
264 pass_info->opt_level = opt_level;
265 pass_info->name = std::move(name);
266 data_ = std::move(pass_info);
307class Pass :
public ffi::ObjectRef {
364 namespace refl = tvm::ffi::reflection;
365 refl::ObjectDef<SequentialObj>()
426 int opt_level, ffi::String name);
437 int opt_level, ffi::String name) {
439 [pass_func = std::move(pass_func)](ffi::RValueRef<IRModule> mod,
441 return pass_func(*std::move(mod), std::move(ctx));
466 ffi::String context =
"");
490 const ffi::Error& err,
const IRModule& mod, ffi::String pass_name,
491 ffi::Optional<GlobalVar> func = ffi::Optional<GlobalVar>(std::nullopt));
587 namespace refl = tvm::ffi::reflection;
630 template <
typename T>
631 ffi::Optional<T>
GetConfig(
const std::string& key)
const {
633 auto it =
config.find(key);
635 return (*it).second.as_or_throw<ffi::Optional<T>>();
647 template <
typename TConfig>
649 auto config = GetConfig<TConfig>(key);
653 return PassConfigWithDefaults<TConfig>();
657 namespace refl = tvm::ffi::reflection;
658 refl::ObjectDef<PassContextObj>()
671 : ffi::ObjectRef(tag) {
672 data_ = std::move(n);
676 TVM_FFI_ICHECK(get() !=
nullptr);
681 TVM_FFI_ICHECK(get() !=
nullptr);
686 return PassContext(ffi::UnsafeInit{}, ffi::make_object<PassContextObj>());
692 if (pass_ctx_node->instruments.defined()) {
694 pi->RunAfterPass(ir_module, pass_info);
#define TVM_TIRX_DLL
Definition base.h:55
Managed reference class to IRModuleObj.
Definition module.h:292
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:58
PassContextObj contains the information that a pass can rely on, such as analysis results.
Definition transform.h:607
TConfig GetConfigOrDefault(const std::string &key) const
Get a config object, constructing reflection defaults only when absent.
Definition transform.h:648
ffi::Array< PassInstrument > instruments
A list of pass instrument implementations.
Definition transform.h:620
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("transform.PassContext", PassContextObj, ffi::Object)
ffi::Map< ffi::String, Any > config
Pass specific configurations.
Definition transform.h:617
ffi::Array< ffi::String > required_pass
The list of required passes.
Definition transform.h:613
int opt_level
The default optimization level.
Definition transform.h:610
static void RegisterReflection()
Definition transform.h:656
static constexpr bool _type_mutable
Definition transform.h:667
ffi::Array< ffi::String > disabled_pass
The list of disabled passes.
Definition transform.h:615
ffi::Optional< T > GetConfig(const std::string &key) const
Get a config value, returning an empty optional when the key is absent.
Definition transform.h:631
PassContext that is used to configure the pass behavior.
Definition transform.h:64
static TVM_TIRX_DLL ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::String > > ListConfigs()
Get all supported configuration names and metadata, registered within the PassContext.
static constexpr bool _type_is_nullable
Definition transform.h:66
TVM_TIRX_DLL void InstrumentEnterPassContext()
Call instrument implementations' callbacks when entering PassContext. The callbacks are called in ord...
static TVM_TIRX_DLL PassContext Current()
Get the default pass context in the current scope.
static void RegisterConfigOption(const char *key)
Register a valid configuration option and its ValueType for validation.
Definition transform.h:153
TVM_TIRX_DLL void InstrumentExitPassContext()
Call instrument implementations' callbacks when exiting PassContext. The callbacks are called in orde...
friend class Internal
Definition transform.h:205
static PassContext Create()
Construct a PassContext containing the default configurations.
Definition transform.h:685
const PassContextObj * operator->() const
const accessor.
Definition transform.h:675
TVM_TIRX_DLL bool InstrumentBeforePass(const IRModule &mod, const PassInfo &info) const
Call instrument implementations' callbacks before a pass run. The callbacks are called in order,...
PassContext(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition transform.h:70
void InstrumentAfterPass(const IRModule &mod, const PassInfo &info) const
Call instrument implementations callbacks after a pass run. The callbacks are called in order,...
Definition transform.h:689
TVM_TIRX_DLL bool PassEnabled(const PassInfo &info) const
Check whether a pass is enabled.
IRModule that holds the functions and type definitions.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
RAII wrapper function to enter and exit a context object similar to python's with syntax.