Class IRModuleObj#

Inheritance Relationships#

Base Type#

  • public ffi::Object

Class Documentation#

class IRModuleObj : public ffi::Object#

IRModule that holds functions and type definitions.

IRModule is the basic unit for all IR transformations across the stack.

Many operations require access to the global IRModule. We pass the IRModule by value in a functional style as an explicit argument, but we mutate the Module while optimizing programs.

See also

IRModule

Public Functions

template<typename TObjectRef>
inline ffi::Optional<TObjectRef> GetAttr(const std::string &attr_key, ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const#

Get a module attribute.

void GetAttrExample(const IRModule& mod) {
auto value = f->GetAttr<int64_t>("AttrKey", 0);
}

Parameters:
  • attr_key – The attribute key.

  • default_value – The default value if the key does not exist, defaults to nullptr.

Template Parameters:

TObjectRef – the expected object type.

Throws:

Error – if the key exists but the value does not match TObjectRef

Returns:

The result

template<typename T>
inline ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key, ffi::Optional<typename ffi::reflection::AttrKey<T>::ValueType> default_value = std::nullopt) const#

Get an attribute with its value type deduced only from the key.

Parameters:
  • key – The typed attribute key.

  • default_value – Value returned only when the key is absent.

Returns:

The attribute or default, with the same conversions as the name-based getter.

template<typename T>
inline ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key, typename ffi::reflection::AttrKey<T>::ValueType default_value) const#

Accept a default convertible to the key’s value type.

template<typename TObjectRef>
inline ffi::Optional<TObjectRef> GetAttr(const std::string &attr_key, TObjectRef default_value) const#
inline DictAttrs GetAttrs() const#

Get the metadata attributes.

Returns:

The additional meta-data attributes

inline bool HasNonzeroAttr(const std::string &attr_key) const#

Check whether the module has an non-zero integer attr.

This function can be used to check whether an optional attribute mark(e.g. inline) exists.

void HasNonzeroAttrExample(const IRModule& mod) {
if (mod->HasNonzeroAttr(attr::kInline)) {
// inline the function.
  }
}

Parameters:

attr_key – The key to the attribute.

Returns:

The check result.

IRModuleObj() = default#
bool SEqual(const IRModuleObj *other, ffi::TypedFunction<bool(AnyView, AnyView, bool, AnyView)> equal) const#
int64_t SHash(int64_t init_hash, ffi::TypedFunction<int64_t(AnyView, int64_t, bool)> hash) const#
inline void Add(const GlobalVar &var, const BaseFunc &func, bool update = false)#

Add a function to the global environment.

Parameters:
  • var – The var of the global function.

  • func – The function.

  • update – Controls whether you can replace a definition in the environment.

void AddUnchecked(const GlobalVar &var, const BaseFunc &func)#

Add a function to the global environment.

It does not do type inference as Add does.

Parameters:
  • var – The name of the global function.

  • func – The function.

inline void Update(const GlobalVar &var, const BaseFunc &func)#

Update a function in the global environment.

Parameters:
  • var – The name of the global function to update.

  • func – The new function.

inline void UpdateGlobalInfo(const ffi::String &name, const ffi::Array<GlobalInfo> &info)#

Update an array of global infos in the global environment.

Parameters:
  • name – The name of the global info.

  • info – The new array of global infos.

inline void Remove(const GlobalVar &var)#

Remove a function from the global environment.

Parameters:

var – The name of the global function to update.

inline bool ContainGlobalVar(const ffi::String &name) const#

Check if the global_var_map_ contains a global variable.

Parameters:

name – The variable name.

Returns:

true if contains, otherise false.

GlobalVar GetGlobalVar(const ffi::String &str) const#

Lookup a global function by its variable.

Parameters:

str – The unique string specifying the global variable.

Returns:

The global variable.

ffi::Array<GlobalVar> GetGlobalVars() const#

Collect all global vars defined in this module, ordered by the global variable name.

Returns:

An array of global vars

inline BaseFunc Lookup(const GlobalVar &var) const#

Look up a global function by its variable.

Parameters:

var – The global var to lookup.

Returns:

The function named by the variable argument.

inline BaseFunc Lookup(const ffi::String &name) const#

Look up a global function by its string name.

Parameters:

name – The name of the function.

Returns:

The function named by the argument.

inline void Update(const IRModule &other)#

Update the functions inside this environment by functions in another environment.

Parameters:

other – The other environment.

inline IRModule ShallowCopy()#

Create a shallow copy of this IRModule.

Returns:

The shallow copy of the IRModule.

Public Members

ffi::Map<GlobalVar, BaseFunc> functions#

A map from ids to all global functions.

DictAttrs attrs#
ffi::Map<ffi::String, ffi::Array<GlobalInfo>> global_infos#

Globally static object that are referred by the IR itself.

ffi::Map<ffi::String, GlobalVar> global_var_map_#

A map from string names to global variables that ensures global uniqueness.

Public Static Functions

static inline void RegisterReflection()#
static inline int32_t _GetOrAllocRuntimeTypeIndex()#
static inline int32_t RuntimeTypeIndex() noexcept#