tvm
Loading...
Searching...
No Matches
with_context.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
25#ifndef TVM_IR_WITH_CONTEXT_H_
26#define TVM_IR_WITH_CONTEXT_H_
27
28#include <exception>
29#include <memory>
30#include <utility>
31#include <vector>
32
33namespace tvm {
34
57template <typename ContextType>
58class With {
59 public:
64 template <typename... Args>
65 explicit With(Args&&... args) : ctx_(std::forward<Args>(args)...) {
66 ctx_.EnterWithScope();
67 }
69 ~With() noexcept(false) { ctx_.ExitWithScope(); }
70
71 // Disable copy and move construction. `With` is intended only for
72 // use in nested contexts that are exited in the reverse order of
73 // entry. Allowing context to be copied or moved would break this
74 // expectation.
75 With(const With& other) = delete;
76 With& operator=(const With& other) = delete;
77 With(With&& other) = delete;
78 With& operator=(With&& other) = delete;
79
80 ContextType* get() { return &ctx_; }
81 const ContextType* get() const { return &ctx_; }
82
83 ContextType* operator->() { return get(); }
84 const ContextType* operator->() const { return get(); }
85 ContextType& operator*() { return *get(); }
86 const ContextType& operator*() const { return *get(); }
87
88 ContextType operator()() { return ctx_; }
89
90 private:
92 ContextType ctx_;
93};
94
112template <typename ContextType>
114 public:
115 WithGroup() = default;
116 WithGroup(WithGroup&&) = default;
118 WithGroup(const WithGroup&) = delete;
119 WithGroup& operator=(const WithGroup&) = delete;
120
125 template <typename... Args>
126 void Emplace(Args&&... args) {
127 entries_.push_back(std::make_unique<With<ContextType>>(std::forward<Args>(args)...));
128 }
129
131 size_t size() const { return entries_.size(); }
132
143 ~WithGroup() noexcept(false) {
144 bool unwinding = std::uncaught_exceptions() > 0;
145 std::exception_ptr first_exc;
146 while (!entries_.empty()) {
147 // Move the last entry out of the vector first, then destroy it.
148 // This ensures entries_ shrinks even if ~With() throws.
149 auto entry = std::move(entries_.back());
150 entries_.pop_back();
151 try {
152 entry.reset(); // calls ~With<ContextType>() -> ExitWithScope()
153 } catch (...) {
154 if (!unwinding && !first_exc) {
155 first_exc = std::current_exception();
156 }
157 }
158 }
159 if (first_exc) std::rethrow_exception(first_exc);
160 }
161
162 private:
163 std::vector<std::unique_ptr<With<ContextType>>> entries_;
164};
165
166} // namespace tvm
167#endif // TVM_IR_WITH_CONTEXT_H_
A group of RAII contexts managed together.
Definition with_context.h:113
~WithGroup() noexcept(false)
Destructor — exits all contexts in reverse order.
Definition with_context.h:143
WithGroup(const WithGroup &)=delete
WithGroup & operator=(const WithGroup &)=delete
WithGroup()=default
void Emplace(Args &&... args)
Construct a context and enter its scope.
Definition with_context.h:126
WithGroup & operator=(WithGroup &&)=default
WithGroup(WithGroup &&)=default
size_t size() const
Number of active contexts in this group.
Definition with_context.h:131
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:58
const ContextType & operator*() const
Definition with_context.h:86
With(Args &&... args)
constructor. Enter the scope of the context.
Definition with_context.h:65
With(With &&other)=delete
ContextType * operator->()
Definition with_context.h:83
ContextType operator()()
Definition with_context.h:88
With(const With &other)=delete
const ContextType * get() const
Definition with_context.h:81
~With() noexcept(false)
destructor, leaves the scope of the context.
Definition with_context.h:69
ContextType & operator*()
Definition with_context.h:85
With & operator=(With &&other)=delete
const ContextType * operator->() const
Definition with_context.h:84
ContextType * get()
Definition with_context.h:80
With & operator=(const With &other)=delete
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41