tvm
Loading...
Searching...
No Matches
Public Member Functions | List of all members
tvm::ScopeStack< T > Class Template Reference

A scope stack for maintaining hierarchical state during IR visiting. More...

#include <scope_stack.h>

Collaboration diagram for tvm::ScopeStack< T >:

Public Member Functions

 ScopeStack ()
 Construct with one initial scope level.
 
auto rbegin () const
 Iterate active scopes from innermost to outermost.
 
auto rend () const
 
size_t size () const
 Return the number of active scopes.
 
bool empty () const
 Return true if no scopes are active.
 
T & Current ()
 Access the current (innermost) scope element.
 
const T & Current () const
 Const access to the current (innermost) scope element.
 
template<typename F >
auto WithNewScope (F &&body) -> decltype(body())
 Execute body within a new scope.
 

Detailed Description

template<typename T>
class tvm::ScopeStack< T >

A scope stack for maintaining hierarchical state during IR visiting.

During IR tree traversal, visitors often need to track scope-local state (e.g., active constraints, variable bindings) that should be automatically cleaned up when leaving a scope. ScopeStack provides this via WithNewScope, which pushes a new element on entry and pops it on exit.

// In VisitStmt_(ForNode):
return constraints.WithNewScope([&]() -> Stmt {
constraints.Current().Emplace(analyzer, condition);
return StmtExprMutator::VisitStmt_(op);
});
A scope stack for maintaining hierarchical state during IR visiting.
Definition scope_stack.h:55
auto WithNewScope(F &&body) -> decltype(body())
Execute body within a new scope.
Definition scope_stack.h:98
T & Current()
Access the current (innermost) scope element.
Definition scope_stack.h:78
Container of all statements.
Definition stmt.h:62
Template Parameters
TThe element type stored on the stack. Must be default-constructible.

Constructor & Destructor Documentation

◆ ScopeStack()

template<typename T >
tvm::ScopeStack< T >::ScopeStack ( )
inline

Construct with one initial scope level.

Member Function Documentation

◆ Current() [1/2]

template<typename T >
T & tvm::ScopeStack< T >::Current ( )
inline

Access the current (innermost) scope element.

The returned reference is stable across push_back/pop_back because std::deque guarantees pointer stability for these operations.

Returns
Mutable reference to the top element.

◆ Current() [2/2]

template<typename T >
const T & tvm::ScopeStack< T >::Current ( ) const
inline

Const access to the current (innermost) scope element.

◆ empty()

template<typename T >
bool tvm::ScopeStack< T >::empty ( ) const
inline

Return true if no scopes are active.

◆ rbegin()

template<typename T >
auto tvm::ScopeStack< T >::rbegin ( ) const
inline

Iterate active scopes from innermost to outermost.

◆ rend()

template<typename T >
auto tvm::ScopeStack< T >::rend ( ) const
inline

◆ size()

template<typename T >
size_t tvm::ScopeStack< T >::size ( ) const
inline

Return the number of active scopes.

◆ WithNewScope()

template<typename T >
template<typename F >
auto tvm::ScopeStack< T >::WithNewScope ( F &&  body) -> decltype(body())
inline

Execute body within a new scope.

Pushes a new T onto the stack, executes the body, then pops it.

Parameters
bodyA callable to execute within the scope.
Returns
The return value of body(), if non-void.

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