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
54template <typename T>
56 public:
58 ScopeStack() { stack_.emplace_back(); }
59
61 auto rbegin() const { return stack_.rbegin(); }
62 auto rend() const { return stack_.rend(); }
63
65 size_t size() const { return stack_.size(); }
66
68 bool empty() const { return stack_.empty(); }
69
78 T& Current() {
79 TVM_FFI_ICHECK(!stack_.empty());
80 return stack_.back();
81 }
82
84 const T& Current() const {
85 TVM_FFI_ICHECK(!stack_.empty());
86 return stack_.back();
87 }
88
97 template <typename F>
98 auto WithNewScope(F&& body) -> decltype(body()) {
99 stack_.emplace_back();
100 struct Guard {
101 std::deque<T>* stack;
102 ~Guard() noexcept(false) { stack->pop_back(); }
103 } guard{&stack_};
104 if constexpr (std::is_void_v<decltype(body())>) {
105 body();
106 } else {
107 return body();
108 }
109 }
110
111 private:
122 std::deque<T> stack_;
123};
124
125} // namespace tvm
126
127#endif // TVM_IR_SCOPE_STACK_H_
A scope stack for maintaining hierarchical state during IR visiting.
Definition scope_stack.h:55
ScopeStack()
Construct with one initial scope level.
Definition scope_stack.h:58
bool empty() const
Return true if no scopes are active.
Definition scope_stack.h:68
auto WithNewScope(F &&body) -> decltype(body())
Execute body within a new scope.
Definition scope_stack.h:98
size_t size() const
Return the number of active scopes.
Definition scope_stack.h:65
auto rend() const
Definition scope_stack.h:62
T & Current()
Access the current (innermost) scope element.
Definition scope_stack.h:78
auto rbegin() const
Iterate active scopes from innermost to outermost.
Definition scope_stack.h:61
const T & Current() const
Const access to the current (innermost) scope element.
Definition scope_stack.h:84
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23