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>
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;
115 return (*it).second.as_or_throw<ffi::Optional<TObjectRef>>();
117 return default_value;
121 template <
typename TObjectRef>
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) {}
245 template <
typename ValueType>
248 if constexpr (std::is_base_of_v<ffi::ObjectRef, ValueType>) {
249 int32_t tindex = ffi::TypeToRuntimeTypeIndex<ValueType>::v();
252 if (
auto opt_map = value.try_cast<ffi::Map<ffi::String, ffi::Any>>()) {
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();
272 <<
"Expect config " << key <<
" to have type " << type_str <<
", but instead get "
273 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
307template <
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");
314 packed_args[0] = ContainerType::RuntimeTypeIndex();
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>()
417class Pass :
public ffi::ObjectRef {
470 namespace refl = tvm::ffi::reflection;
471 refl::ObjectDef<SequentialNode>()
546 int opt_level, ffi::String name, ffi::Array<ffi::String> required,
547 bool traceable =
false);
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::Optional< TObjectRef > GetConfig(const std::string &key, TObjectRef default_value) const
Definition transform.h:122
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
PassContextNode()=default
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
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
static PassContext Current()
Get the default pass context in the current scope.
bool PassEnabled(const PassInfo &info) const
Check whether a pass is enabled.
friend class Internal
Definition transform.h:297
PassContextNode * operator->()
mutable accessor.
Definition transform.h:174
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
const PassContextNode * operator->() const
const accessor.
Definition transform.h:166
static PassContext Create()
Construct a PassContext containing the default configurations.
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...
static ffi::Map< ffi::String, ffi::Map< ffi::String, ffi::String > > ListConfigs()
Get all supported configuration names and metadata, registered within the PassContext.
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.
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.