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.
 
size_t size () const
 Return the number of active scopes.
 
bool empty () const
 Return true if no scopes are active.
 
TCurrent ()
 Access the current (innermost) scope element.
 
const TCurrent () 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);
});
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
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.

◆ 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: