tvm
Loading...
Searching...
No Matches
scope_stack.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
24#ifndef TVM_IR_SCOPE_STACK_H_
25#define TVM_IR_SCOPE_STACK_H_
26
27#include <tvm/ffi/error.h>
28
29#include <deque>
30#include <type_traits>
31
32namespace tvm {
33
53template <typename T>
55 public:
57 ScopeStack() { stack_.emplace_back(); }
58
60 auto rbegin() const { return stack_.rbegin(); }
61 auto rend() const { return stack_.rend(); }
62
64 size_t size() const { return stack_.size(); }
65
67 bool empty() const { return stack_.empty(); }
68
77 T& Current() {
78 TVM_FFI_ICHECK(!stack_.empty());
79 return stack_.back();
80 }
81
83 const T& Current() const {
84 TVM_FFI_ICHECK(!stack_.empty());
85 return stack_.back();
86 }
87
96 template <typename F>
97 auto WithNewScope(F&& body) -> decltype(body()) {
98 stack_.emplace_back();
99 struct Guard {
100 std::deque<T>* stack;
101 ~Guard() noexcept(false) { stack->pop_back(); }
102 } guard{&stack_};
103 if constexpr (std::is_void_v<decltype(body())>) {
104 body();
105 } else {
106 return body();
107 }
108 }
109
110 private:
121 std::deque<T> stack_;
122};
123
124} // namespace tvm
125
126#endif // TVM_IR_SCOPE_STACK_H_
A scope stack for maintaining hierarchical state during IR visiting.
Definition scope_stack.h:54
ScopeStack()
Construct with one initial scope level.
Definition scope_stack.h:57
bool empty() const
Return true if no scopes are active.
Definition scope_stack.h:67
auto WithNewScope(F &&body) -> decltype(body())
Execute body within a new scope.
Definition scope_stack.h:97
size_t size() const
Return the number of active scopes.
Definition scope_stack.h:64
auto rend() const
Definition scope_stack.h:61
T & Current()
Access the current (innermost) scope element.
Definition scope_stack.h:77
auto rbegin() const
Iterate active scopes from innermost to outermost.
Definition scope_stack.h:60
const T & Current() const
Const access to the current (innermost) scope element.
Definition scope_stack.h:83
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41