56 #ifndef TVM_IR_TRANSFORM_H_
57 #define TVM_IR_TRANSFORM_H_
59 #include <tvm/ffi/container/array.h>
60 #include <tvm/ffi/function.h>
61 #include <tvm/ffi/reflection/creator.h>
62 #include <tvm/ffi/reflection/registry.h>
63 #include <tvm/ffi/string.h>
69 #include <type_traits>
108 template <
typename TObjectRef>
110 const std::string& key,
111 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt))
const {
112 if (!
config.defined())
return default_value;
113 auto it =
config.find(key);
115 return (*it).second.as_or_throw<ffi::Optional<TObjectRef>>();
117 return default_value;
121 template <
typename TObjectRef>
122 ffi::Optional<TObjectRef>
GetConfig(
const std::string& key, TObjectRef default_value)
const {
123 return GetConfig<TObjectRef>(key, ffi::Optional<TObjectRef>(default_value));
127 namespace refl = tvm::ffi::reflection;
128 refl::ObjectDef<PassContextNode>()
157 explicit PassContext(ffi::UnsafeInit tag) : ffi::ObjectRef(tag) {}
161 explicit PassContext(ffi::ObjectPtr<PassContextNode> n) : ffi::ObjectRef(n) {}
167 TVM_FFI_ICHECK(get() !=
nullptr);
175 TVM_FFI_ICHECK(get() !=
nullptr);
194 TVM_DLL
static ffi::Map<ffi::String, ffi::Map<ffi::String, ffi::String>>
ListConfigs();
245 template <
typename ValueType>
248 if constexpr (std::is_base_of_v<ffi::ObjectRef, ValueType>) {
249 int32_t tindex = ffi::TypeToRuntimeTypeIndex<ValueType>::v();
250 auto type_key = ffi::TypeIndexToTypeKey(tindex);
251 auto legalization = [=](ffi::Any value) -> ffi::Any {
252 if (
auto opt_map = value.try_cast<ffi::Map<ffi::String, ffi::Any>>()) {
253 return ffi::reflection::ObjectCreator(type_key)(opt_map.value());
255 auto opt_val = value.try_cast<ValueType>();
256 if (!opt_val.has_value()) {
257 TVM_FFI_THROW(AttributeError)
258 <<
"Expect config " << key <<
" to have type " << type_key <<
", but instead get "
259 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
267 std::string type_str = ffi::TypeTraits<ValueType>::TypeStr();
268 auto legalization = [=](ffi::Any value) -> ffi::Any {
269 auto opt_val = value.try_cast<ValueType>();
270 if (!opt_val.has_value()) {
271 TVM_FFI_THROW(AttributeError)
272 <<
"Expect config " << key <<
" to have type " << type_str <<
", but instead get "
273 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
289 TVM_DLL
void EnterWithScope();
291 TVM_DLL
void ExitWithScope();
294 std::function<ffi::Any(ffi::Any)> legalization);
307 template <
typename TConfig>
309 static_assert(std::is_base_of_v<ffi::ObjectRef, TConfig>,
310 "Can only create ObjectRef-derived types");
311 using ContainerType =
typename TConfig::ContainerType;
312 static auto finit_object = ffi::Function::GetGlobalRequired(
"ffi.MakeObjectFromPackedArgs");
313 ffi::AnyView packed_args[1];
314 packed_args[0] = ContainerType::RuntimeTypeIndex();
316 finit_object.CallPacked(ffi::PackedArgs(packed_args, 1), &rv);
317 return rv.cast<TConfig>();
320 #define TVM_PASS_CTX_CONFIG_VAR_DEF [[maybe_unused]] static uint32_t __make_PassContext_tid
328 #define TVM_REGISTER_PASS_CONFIG_OPTION(Key, ValueType) \
329 TVM_FFI_STR_CONCAT(TVM_PASS_CTX_CONFIG_VAR_DEF, __COUNTER__) = \
330 ::tvm::transform::PassContext::RegisterConfigOption<ValueType>(Key)
353 namespace refl = tvm::ffi::reflection;
354 refl::ObjectDef<PassInfoNode>()
376 TVM_DLL
PassInfo(
int opt_level, ffi::String name, ffi::Array<ffi::String> required,
417 class Pass :
public ffi::ObjectRef {
470 namespace refl = tvm::ffi::reflection;
471 refl::ObjectDef<SequentialNode>()
526 TVM_DLL
Sequential(ffi::Array<Pass> passes, ffi::String name =
"sequential");
546 int opt_level, ffi::String name, ffi::Array<ffi::String> required,
547 bool traceable =
false);
578 const ffi::Error& err,
const IRModule&
mod, ffi::String pass_name,
579 ffi::Optional<GlobalVar> func = ffi::Optional<GlobalVar>(std::nullopt));
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
PassContextNode contains the information that a pass can rely on, such as analysis results.
Definition: transform.h:80
ffi::Map< ffi::String, Any > config
Pass specific configurations.
Definition: transform.h:90
ffi::Array< ffi::String > disabled_pass
The list of disabled passes.
Definition: transform.h:88
ffi::Array< instrument::PassInstrument > instruments
A list of pass instrument implementations.
Definition: transform.h:93
ffi::Optional< TObjectRef > GetConfig(const std::string &key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get a config value from the pass context.
Definition: transform.h:109
PassContextNode()=default
ffi::Optional< TObjectRef > GetConfig(const std::string &key, TObjectRef default_value) const
Definition: transform.h:122
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("transform.PassContext", PassContextNode, ffi::Object)
static void RegisterReflection()
Definition: transform.h:126
int opt_level
The default optimization level.
Definition: transform.h:83
ffi::Array< ffi::String > required_pass
The list of required passes.
Definition: transform.h:86
PassContext that is used to configure the pass behavior.
Definition: transform.h:151
const PassContextNode * operator->() const
const accessor.
Definition: transform.h:166
static PassContext Current()
Get the default pass context in the current scope.
bool PassEnabled(const PassInfo &info) const
Check whether a pass is enabled.
static ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::String > > ListConfigs()
Get all supported configuration names and metadata, registered within the PassContext.
friend class Internal
Definition: transform.h:297
PassContext()
Definition: transform.h:153
static int32_t RegisterConfigOption(const char *key)
Register a valid configuration option and its ValueType for validation.
Definition: transform.h:246
static PassContext Create()
Construct a PassContext containing the default configurations.
PassContextNode * operator->()
mutable accessor.
Definition: transform.h:174
void InstrumentEnterPassContext()
Call instrument implementations' callbacks when entering PassContext. The callbacks are called in ord...
void InstrumentExitPassContext()
Call instrument implementations' callbacks when exiting PassContext. The callbacks are called in orde...
PassContext(ffi::ObjectPtr< PassContextNode > n)
constructor with ffi::ObjectPtr
Definition: transform.h:161
PassContext(ffi::UnsafeInit tag)
constructor with UnsafeInit
Definition: transform.h:157
bool InstrumentBeforePass(const IRModule &mod, const PassInfo &info) const
Call instrument implementations' callbacks before a pass run. The callbacks are called in order,...
void InstrumentAfterPass(const IRModule &mod, const PassInfo &info) const
Call instrument implementations callbacks after a pass run. The callbacks are called in order,...
IRModule that holds the functions and type definitions.
IRModuleFrame IRModule()
The IRModule declaration statement.
Definition: module.h:248
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:310
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
RAII wrapper function to enter and exit a context object similar to python's with syntax.