56 #ifndef TVM_IR_TRANSFORM_H_
57 #define TVM_IR_TRANSFORM_H_
59 #include <tvm/ffi/container/array.h>
60 #include <tvm/ffi/reflection/creator.h>
61 #include <tvm/ffi/reflection/registry.h>
62 #include <tvm/ffi/string.h>
89 mutable ffi::Optional<DiagnosticContext>
diag_ctx;
109 template <
typename TObjectRef>
111 const std::string& key,
112 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt))
const {
113 if (!
config.defined())
return default_value;
114 auto it =
config.find(key);
116 return Downcast<ffi::Optional<TObjectRef>>((*it).second);
118 return default_value;
122 template <
typename TObjectRef>
123 ffi::Optional<TObjectRef>
GetConfig(
const std::string& key, TObjectRef default_value)
const {
124 return GetConfig<TObjectRef>(key, ffi::Optional<TObjectRef>(default_value));
129 refl::ObjectDef<PassContextNode>()
163 explicit PassContext(ObjectPtr<PassContextNode> n) : ObjectRef(n) {}
169 ICHECK(get() !=
nullptr);
177 ICHECK(get() !=
nullptr);
196 TVM_DLL
static ffi::Map<ffi::String, ffi::Map<ffi::String, ffi::String>>
ListConfigs();
247 template <
typename ValueType>
250 if constexpr (std::is_base_of_v<ObjectRef, ValueType>) {
251 int32_t tindex = ffi::TypeToRuntimeTypeIndex<ValueType>::v();
252 auto type_key = ffi::TypeIndexToTypeKey(tindex);
253 auto legalization = [=](ffi::Any value) -> ffi::Any {
254 if (
auto opt_map = value.try_cast<ffi::Map<ffi::String, ffi::Any>>()) {
255 return ffi::reflection::ObjectCreator(type_key)(opt_map.value());
257 auto opt_val = value.try_cast<ValueType>();
258 if (!opt_val.has_value()) {
259 TVM_FFI_THROW(AttributeError)
260 <<
"Expect config " << key <<
" to have type " << type_key <<
", but instead get "
261 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
269 std::string type_str = ffi::TypeTraits<ValueType>::TypeStr();
270 auto legalization = [=](ffi::Any value) -> ffi::Any {
271 auto opt_val = value.try_cast<ValueType>();
272 if (!opt_val.has_value()) {
273 TVM_FFI_THROW(AttributeError)
274 <<
"Expect config " << key <<
" to have type " << type_str <<
", but instead get "
275 << ffi::details::AnyUnsafe::GetMismatchTypeInfo<ValueType>(value);
291 TVM_DLL
void EnterWithScope();
293 TVM_DLL
void ExitWithScope();
296 std::function<ffi::Any(ffi::Any)> legalization);
303 #define TVM_PASS_CTX_CONFIG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_PassContext_tid
311 #define TVM_REGISTER_PASS_CONFIG_OPTION(Key, ValueType) \
312 TVM_STR_CONCAT(TVM_PASS_CTX_CONFIG_VAR_DEF, __COUNTER__) = \
313 ::tvm::transform::PassContext::RegisterConfigOption<ValueType>(Key)
337 refl::ObjectDef<PassInfoNode>()
359 TVM_DLL
PassInfo(
int opt_level, ffi::String name, ffi::Array<ffi::String> required,
400 class Pass :
public ObjectRef {
454 refl::ObjectDef<SequentialNode>()
509 TVM_DLL
Sequential(ffi::Array<Pass> passes, ffi::String name =
"sequential");
529 int opt_level, ffi::String name, ffi::Array<ffi::String> required,
530 bool traceable =
false);
555 bool error_if_no_function_matches_regex =
false);
563 TVM_DLL
Pass PrintIR(ffi::String header =
"",
bool show_meta_data =
false);
Managed reference class to IRModuleNode.
Definition: module.h:256
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with.h:58
PassContextNode contains the information that a pass can rely on, such as analysis results.
Definition: transform.h:79
ffi::Map< ffi::String, Any > config
Pass specific configurations.
Definition: transform.h:91
ffi::Optional< DiagnosticContext > diag_ctx
The diagnostic context.
Definition: transform.h:89
ffi::Array< ffi::String > disabled_pass
The list of disabled passes.
Definition: transform.h:87
ffi::Array< instrument::PassInstrument > instruments
A list of pass instrument implementations.
Definition: transform.h:94
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("transform.PassContext", PassContextNode, Object)
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:110
PassContextNode()=default
ffi::Optional< TObjectRef > GetConfig(const std::string &key, TObjectRef default_value) const
Definition: transform.h:123
static void RegisterReflection()
Definition: transform.h:127
int opt_level
The default optimization level.
Definition: transform.h:82
ffi::Array< ffi::String > required_pass
The list of required passes.
Definition: transform.h:85
PassContext that is used to configure the pass behavior.
Definition: transform.h:153
const PassContextNode * operator->() const
const accessor.
Definition: transform.h:168
static PassContext Current()
Get the default pass context in the current scope.
bool PassEnabled(const PassInfo &info) const
Check whether a pass is enabled.
PassContext(ObjectPtr< PassContextNode > n)
constructor with ObjectPtr
Definition: transform.h:163
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:299
PassContext()
Definition: transform.h:155
static int32_t RegisterConfigOption(const char *key)
Register a valid configuration option and its ValueType for validation.
Definition: transform.h:248
static PassContext Create()
Construct a PassContext containing the default configurations.
PassContextNode * operator->()
mutable accessor.
Definition: transform.h:176
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::UnsafeInit tag)
constructor with UnsafeInit
Definition: transform.h:159
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,...
A new diagnostic interface for TVM error reporting.
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
IRModuleFrame IRModule()
The IRModule declaration statement.
Definition: module.h:249
tvm::PrimExpr mod(const tvm::PrimExpr &a, const tvm::PrimExpr &b)
Definition: broadcast.h:308
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
RAII wrapper function to enter and exit a context object similar to python's with syntax.