tvm
Public Member Functions | Static Public Attributes | List of all members
tvm::relax::BlockBuilderNode Class Referenceabstract

A builder to build Relax binding blocks. More...

#include <block_builder.h>

Inheritance diagram for tvm::relax::BlockBuilderNode:
Collaboration diagram for tvm::relax::BlockBuilderNode:

Public Member Functions

virtual NameSupply name_supply ()=0
 Get the name supply for generating unique names. More...
 
virtual IRModule GetContextIRModule () const =0
 Get the context IRModule in this builder. More...
 
virtual IRModule Finalize ()=0
 Finalize the building process and return the result IRModule. Possibly rename GlobalVars in the IRModule to ensure name uniqueness and the invariant: every public function has the same name as its "global_symbol" attribute. More...
 
virtual GlobalVar AddFunction (const BaseFunc &func, String func_name_hint)=0
 Add a Relax function or a TIR PrimFunc to internal context module. More...
 
virtual void UpdateFunction (const GlobalVar &gv, BaseFunc function)=0
 Update a Relax function or a TIR PrimFunc in the internal context module. More...
 
virtual void ReportFatal (const Diagnostic &diagnostic)=0
 Report an error during transformation construction. More...
 
virtual Optional< ExprLookupBinding (const Var &var)=0
 Lookup the binding value that var binds to in the current emitted sequences. More...
 
virtual void BeginScope (Optional< Array< Var >> params)=0
 Begin a new scope, with optional parameters that are visible within the scope. More...
 
virtual void EndScope ()=0
 End the previously defined scope. More...
 
virtual void BeginDataflowBlock ()=0
 Begin to build a DataflowBlock. More...
 
virtual void BeginBindingBlock ()=0
 Begin to build a BindingBlock. More...
 
virtual BindingBlock EndBlock ()=0
 End building a BindingBlock. More...
 
virtual bool CurrentBlockIsDataFlow ()=0
 Check if the block being built is DataflowBlock or not. More...
 
virtual Var Emit (Expr expr, String name_hint="")=0
 Emits an Expr, and returns the variable it is bound to. More...
 
virtual Var EmitMatchCast (Expr value, StructInfo struct_info, String name_hint="")=0
 Emit a MatchCast. More...
 
virtual Var EmitOutput (Expr output, String name_hint="")=0
 Generate an output for the current dataflow block. More...
 
virtual void EmitNormalized (Binding normalized_binding)=0
 Emit a binding that is already normalized. More...
 
virtual Expr Normalize (const Expr &expr)=0
 Convert an expression to normal form, and try to eagerly infer types and shapes. More...
 
virtual Expr NormalizeArgument (const Expr &expr)=0
 Normalize argument to a call or another IRNode. More...
 
virtual arith::AnalyzerGetAnalyzer ()=0
 Get the analyzer of the BlockBuilder. More...
 
 TVM_DECLARE_BASE_OBJECT_INFO (BlockBuilderNode, Object)
 
- Public Member Functions inherited from tvm::runtime::Object
uint32_t type_index () const
 
std::string GetTypeKey () const
 
size_t GetTypeKeyHash () const
 
template<typename TargetType >
bool IsInstance () const
 
bool unique () const
 
 Object ()
 
 Object (const Object &other)
 
 Object (Object &&other)
 
Objectoperator= (const Object &other)
 
Objectoperator= (Object &&other)
 

Static Public Attributes

static constexpr const uint32_t _type_index = TypeIndex::kDynamic
 
static constexpr const char * _type_key = "relax.BlockBuilder"
 
- Static Public Attributes inherited from tvm::runtime::Object
static constexpr const char * _type_key = "runtime.Object"
 
static constexpr bool _type_final = false
 
static constexpr uint32_t _type_child_slots = 0
 
static constexpr bool _type_child_slots_can_overflow = true
 
static constexpr bool _type_has_method_visit_attrs = true
 
static constexpr bool _type_has_method_sequal_reduce = false
 
static constexpr bool _type_has_method_shash_reduce = false
 
static constexpr uint32_t _type_index = TypeIndex::kDynamic
 

Additional Inherited Members

- Public Types inherited from tvm::runtime::Object
typedef void(* FDeleter) (Object *self)
 Object deleter. More...
 
using RefCounterType = std::atomic< int32_t >
 
- Static Public Member Functions inherited from tvm::runtime::Object
static std::string TypeIndex2Key (uint32_t tindex)
 Get the type key of the corresponding index from runtime. More...
 
static size_t TypeIndex2KeyHash (uint32_t tindex)
 Get the type key hash of the corresponding index from runtime. More...
 
static uint32_t TypeKey2Index (const std::string &key)
 Get the type index of the corresponding key from runtime. More...
 
static uint32_t _GetOrAllocRuntimeTypeIndex ()
 
static uint32_t RuntimeTypeIndex ()
 
- Protected Member Functions inherited from tvm::runtime::Object
void IncRef ()
 developer function, increases reference counter. More...
 
void DecRef ()
 developer function, decrease reference counter. More...
 
- Static Protected Member Functions inherited from tvm::runtime::Object
static uint32_t GetOrAllocRuntimeTypeIndex (const std::string &key, uint32_t static_tindex, uint32_t parent_tindex, uint32_t type_child_slots, bool type_child_slots_can_overflow)
 Get the type index using type key. More...
 
- Protected Attributes inherited from tvm::runtime::Object
uint32_t type_index_ {0}
 Type index(tag) that indicates the type of the object. More...
 
RefCounterType ref_counter_ {0}
 The internal reference counter. More...
 
FDeleter deleter_ = nullptr
 deleter of this object to enable customized allocation. If the deleter is nullptr, no deletion will be performed. The creator of the object must always set the deleter field properly. More...
 

Detailed Description

A builder to build Relax binding blocks.

BlockBuilder provides the following three categories of main functionalities for IR building and transformations:

Importantly, these three categories of features can be dependent on each other. For example, when we emit into scope we will call normalize to ensure the code is in normal form. Similarly, when we normalize we could choose to emit into the current context.

We would encourage the developers to keep these three category in mind when using and developing BlockBuilder, we can group the code in a logically clean way.

BlockBuilderNode is implemented as a virtual interface to allow logically grouped implementation and internal data structures that are hidden from the users.

Member Function Documentation

◆ AddFunction()

virtual GlobalVar tvm::relax::BlockBuilderNode::AddFunction ( const BaseFunc func,
String  func_name_hint 
)
pure virtual

Add a Relax function or a TIR PrimFunc to internal context module.

Parameters
funcThe function to be added.
func_name_hintThe name hint of the function to be added.
Note
If the function to be added already exists, return its GlobalVar directly.
Returns
The global var bound to the added function.

◆ BeginBindingBlock()

virtual void tvm::relax::BlockBuilderNode::BeginBindingBlock ( )
pure virtual

Begin to build a BindingBlock.

◆ BeginDataflowBlock()

virtual void tvm::relax::BlockBuilderNode::BeginDataflowBlock ( )
pure virtual

Begin to build a DataflowBlock.

◆ BeginScope()

virtual void tvm::relax::BlockBuilderNode::BeginScope ( Optional< Array< Var >>  params)
pure virtual

Begin a new scope, with optional parameters that are visible within the scope.

Parameters
paramsParameters that are visible within the scope.
Note
This function should be called when new scope is introduced (function, seq) to properly track the variable availability and help the best effort deduction.
See also
EndScope

◆ CurrentBlockIsDataFlow()

virtual bool tvm::relax::BlockBuilderNode::CurrentBlockIsDataFlow ( )
pure virtual

Check if the block being built is DataflowBlock or not.

Returns
A boolean that indicates if the block being built is DataflowBlock or not.

◆ Emit()

virtual Var tvm::relax::BlockBuilderNode::Emit ( Expr  expr,
String  name_hint = "" 
)
pure virtual

Emits an Expr, and returns the variable it is bound to.

Parameters
exprThe Expr to be emitted.
name_hintName hint for the bound variable.
Returns
The new variable that expr is bound to.
Note
This Emit function normalizes the expr, and performs shape and type deductions by calling Normalize.

◆ EmitMatchCast()

virtual Var tvm::relax::BlockBuilderNode::EmitMatchCast ( Expr  value,
StructInfo  struct_info,
String  name_hint = "" 
)
pure virtual

Emit a MatchCast.

Parameters
valueThe input value.
struct_infoThe struct info to be matched.
name_hintName hint for the bound variable.
Returns
The variable bound to the MatchCast.

◆ EmitNormalized()

virtual void tvm::relax::BlockBuilderNode::EmitNormalized ( Binding  normalized_binding)
pure virtual

Emit a binding that is already normalized.

Parameters
normalized_bindingA binding whose value is already normalized.
Note
This function requires binding to be pre-normalized.

◆ EmitOutput()

virtual Var tvm::relax::BlockBuilderNode::EmitOutput ( Expr  output,
String  name_hint = "" 
)
pure virtual

Generate an output for the current dataflow block.

Parameters
outputThe output variable of the block.
name_hintName hint for the bound variable.
Returns
The variable bound to output.

◆ EndBlock()

virtual BindingBlock tvm::relax::BlockBuilderNode::EndBlock ( )
pure virtual

End building a BindingBlock.

Returns
The BindingBlock being built.

◆ EndScope()

virtual void tvm::relax::BlockBuilderNode::EndScope ( )
pure virtual

End the previously defined scope.

◆ Finalize()

virtual IRModule tvm::relax::BlockBuilderNode::Finalize ( )
pure virtual

Finalize the building process and return the result IRModule. Possibly rename GlobalVars in the IRModule to ensure name uniqueness and the invariant: every public function has the same name as its "global_symbol" attribute.

Note
this method should be called only once at the end of the building process, since it may invalidate global vars previously returned by this builder. See also transform::NormalizeGlobalVar.
Returns
The result IRModule.

◆ GetAnalyzer()

virtual arith::Analyzer* tvm::relax::BlockBuilderNode::GetAnalyzer ( )
pure virtual

Get the analyzer of the BlockBuilder.

Returns
The BlockBuilder's arithmetic analyzer.

◆ GetContextIRModule()

virtual IRModule tvm::relax::BlockBuilderNode::GetContextIRModule ( ) const
pure virtual

Get the context IRModule in this builder.

Note
The context
Returns
The IRModule in this BlockBuilder.

◆ LookupBinding()

virtual Optional<Expr> tvm::relax::BlockBuilderNode::LookupBinding ( const Var var)
pure virtual

Lookup the binding value that var binds to in the current emitted sequences.

Parameters
varThe input var.
Returns
The Expr bound to the input var.
Note
For function parameters, this function returns NullOpt.

◆ name_supply()

virtual NameSupply tvm::relax::BlockBuilderNode::name_supply ( )
pure virtual

Get the name supply for generating unique names.

Returns
The name supply.

◆ Normalize()

virtual Expr tvm::relax::BlockBuilderNode::Normalize ( const Expr expr)
pure virtual

Convert an expression to normal form, and try to eagerly infer types and shapes.

Parameters
exprThe input expression.
Returns
The normalized expression.
Note
Invariant: If any of the sub expr have struct_info field. they must have already been normalized.

◆ NormalizeArgument()

virtual Expr tvm::relax::BlockBuilderNode::NormalizeArgument ( const Expr expr)
pure virtual

Normalize argument to a call or another IRNode.

Parameters
exprThe input expression.
Returns
The normalized expression.
Note
This function will create a binding var for non-leaf expressions such as Call.

◆ ReportFatal()

virtual void tvm::relax::BlockBuilderNode::ReportFatal ( const Diagnostic diagnostic)
pure virtual

Report an error during transformation construction.

Parameters
diagnosticThe diagnostic information.

◆ TVM_DECLARE_BASE_OBJECT_INFO()

tvm::relax::BlockBuilderNode::TVM_DECLARE_BASE_OBJECT_INFO ( BlockBuilderNode  ,
Object   
)

◆ UpdateFunction()

virtual void tvm::relax::BlockBuilderNode::UpdateFunction ( const GlobalVar gv,
BaseFunc  function 
)
pure virtual

Update a Relax function or a TIR PrimFunc in the internal context module.

Parameters
gvThe global var referring the function to be updated.
functionThe updated function.

Member Data Documentation

◆ _type_index

constexpr const uint32_t tvm::relax::BlockBuilderNode::_type_index = TypeIndex::kDynamic
staticconstexpr

◆ _type_key

constexpr const char* tvm::relax::BlockBuilderNode::_type_key = "relax.BlockBuilder"
staticconstexpr

The documentation for this class was generated from the following file: