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/string.h>
68 explicit PassContext(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
72 explicit PassContext(ffi::UnsafeInit tag, ffi::ObjectPtr<PassContextObj> n);
150 template <
typename ValueType>
152 if constexpr (std::is_base_of_v<ffi::ObjectRef, ValueType>) {
154 int32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
155 auto type_key = ffi::TypeIndexToTypeKey(tindex);
156 auto legalization = [=](ffi::Any value) -> ffi::Any {
157 if (
auto opt_map = value.try_cast<ffi::Map<ffi::String, ffi::Any>>()) {
158 return ffi::reflection::ObjectCreator(type_key)(opt_map.value());
160 auto opt_val = value.try_cast<ValueType>();
161 if (!opt_val.has_value()) {
162 TVM_FFI_THROW(AttributeError)
163 <<
"Expect config " << key <<
" to have type " << type_key <<
", but instead get "
164 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
172 std::string type_str = ffi::TypeTraits<ValueType>::TypeStr();
173 auto legalization = [=](ffi::Any value) -> ffi::Any {
174 auto opt_val = value.try_cast<ValueType>();
175 if (!opt_val.has_value()) {
176 TVM_FFI_THROW(AttributeError)
177 <<
"Expect config " << key <<
" to have type " << type_str <<
", but instead get "
178 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
198 std::function<ffi::Any(ffi::Any)> legalization);
211template <
typename TConfig>
213 static_assert(std::is_base_of_v<ffi::ObjectRef, TConfig>,
214 "Can only create ObjectRef-derived types");
215 using ContainerType =
typename TConfig::ContainerType;
216 static auto finit_object = ffi::Function::GetGlobalRequired(
"ffi.MakeObjectFromPackedArgs");
217 ffi::AnyView packed_args[1];
218 packed_args[0] = ContainerType::RuntimeTypeIndex();
220 finit_object.CallPacked(ffi::PackedArgs(packed_args, 1), &rv);
221 return rv.cast<TConfig>();
239 namespace refl = tvm::ffi::reflection;
240 refl::ObjectDef<PassInfoObj>()
298class Pass :
public ffi::ObjectRef {
353 namespace refl = tvm::ffi::reflection;
354 refl::ObjectDef<SequentialObj>()
415 int opt_level, ffi::String name);
446 const ffi::Error& err,
const IRModule& mod, ffi::String pass_name,
447 ffi::Optional<GlobalVar> func = ffi::Optional<GlobalVar>(std::nullopt));
543 namespace refl = tvm::ffi::reflection;
586 template <
typename T>
587 ffi::Optional<T>
GetConfig(
const std::string& key)
const {
589 auto it =
config.find(key);
591 return (*it).second.as_or_throw<ffi::Optional<T>>();
603 template <
typename TConfig>
605 auto config = GetConfig<TConfig>(key);
609 return PassConfigWithDefaults<TConfig>();
613 namespace refl = tvm::ffi::reflection;
614 refl::ObjectDef<PassContextObj>()
625 : ffi::ObjectRef(tag) {
626 data_ = std::move(n);
630 TVM_FFI_ICHECK(get() !=
nullptr);
635 TVM_FFI_ICHECK(get() !=
nullptr);
Managed reference class to IRModuleNode.
Definition module.h:255
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
PassContextObj contains the information that a pass can rely on, such as analysis results.
Definition transform.h:563
TConfig GetConfigOrDefault(const std::string &key) const
Get a config object, constructing reflection defaults only when absent.
Definition transform.h:604
ffi::Array< PassInstrument > instruments
A list of pass instrument implementations.
Definition transform.h:576
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("transform.PassContext", PassContextObj, ffi::Object)
ffi::Map< ffi::String, Any > config
Pass specific configurations.
Definition transform.h:573
ffi::Array< ffi::String > required_pass
The list of required passes.
Definition transform.h:569
int opt_level
The default optimization level.
Definition transform.h:566
static void RegisterReflection()
Definition transform.h:612
ffi::Array< ffi::String > disabled_pass
The list of disabled passes.
Definition transform.h:571
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:587
PassContext that is used to configure the pass behavior.
Definition transform.h:62
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:64
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:151
TVM_TIRX_DLL void InstrumentExitPassContext()
Call instrument implementations' callbacks when exiting PassContext. The callbacks are called in orde...
friend class Internal
Definition transform.h:201
const PassContextObj * operator->() const
const accessor.
Definition transform.h:629
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,...
TVM_TIRX_DLL void InstrumentAfterPass(const IRModule &mod, const PassInfo &info) const
Call instrument implementations callbacks after a pass run. The callbacks are called in order,...
static TVM_TIRX_DLL PassContext Create()
Construct a PassContext containing the default configurations.
PassContext(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition transform.h:68
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 attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83
RAII wrapper function to enter and exit a context object similar to python's with syntax.