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#include <tvm/ir/module.h>
20#include <tvm/runtime/registry.h>
21#include <tvm/script/ir_builder/base.h>
22
23namespace tvm {
24namespace script {
25namespace ir_builder {
26
27void IRBuilderFrameNode::EnterWithScope() {
28 IRBuilder::Current()->frames.push_back(GetRef<IRBuilderFrame>(this));
29}
30
31void IRBuilderFrameNode::ExitWithScope() {
32 for (auto it = callbacks.rbegin(); it != callbacks.rend(); ++it) {
33 (*it)();
34 }
35 this->callbacks.clear();
36 IRBuilder::Current()->frames.pop_back();
37}
38
39void IRBuilderFrameNode::AddCallback(runtime::TypedPackedFunc<void()> callback) {
40 if (IRBuilder::Current()->frames.empty()) {
41 LOG(FATAL) << "ValueError: No frames in Builder to add callback";
42 }
43 IRBuilder::Current()->frames.back()->callbacks.push_back(callback);
44}
45
46IRBuilder::IRBuilder() {
47 ObjectPtr<IRBuilderNode> n = make_object<IRBuilderNode>();
48 n->frames.clear();
49 n->result = NullOpt;
50 data_ = n;
51}
52
53std::vector<IRBuilder>* ThreadLocalBuilderStack() {
54 thread_local std::vector<IRBuilder> stack;
55 return &stack;
56}
57
58void IRBuilder::EnterWithScope() {
59 IRBuilderNode* n = this->get();
60 CHECK(n->frames.empty()) << "ValueError: There are frame(s) left in the builder: "
61 << n->frames.size()
62 << ". Please use a fresh new builder every time building IRs";
63 n->result = NullOpt;
64 std::vector<IRBuilder>* stack = ThreadLocalBuilderStack();
65 stack->push_back(*this);
66}
67
68void IRBuilder::ExitWithScope() {
69 std::vector<IRBuilder>* stack = ThreadLocalBuilderStack();
70 ICHECK(!stack->empty());
71 stack->pop_back();
72}
73
74IRBuilder IRBuilder::Current() {
75 std::vector<IRBuilder>* stack = ThreadLocalBuilderStack();
76 CHECK(!stack->empty()) << "ValueError: No builder in current scope";
77 return stack->back();
78}
79
80namespace details {
81
82Namer::FType& Namer::vtable() {
83 static FType inst;
84 return inst;
85}
86
87void Namer::Name(ObjectRef node, String name) {
88 static const FType& f = vtable();
89 CHECK(node.defined()) << "ValueError: Cannot name nullptr with: " << name;
90 CHECK(f.can_dispatch(node)) << "ValueError: Do not know how to name type \""
91 << node->GetTypeKey();
92 f(node, name);
93}
94
95} // namespace details
96
97TVM_REGISTER_NODE_TYPE(IRBuilderFrameNode);
98TVM_REGISTER_NODE_TYPE(IRBuilderNode);
99TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderFrameEnter")
100 .set_body_method<IRBuilderFrame>(&IRBuilderFrameNode::EnterWithScope);
101TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderFrameExit")
102 .set_body_method<IRBuilderFrame>(&IRBuilderFrameNode::ExitWithScope);
103TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderFrameAddCallback")
104 .set_body_method<IRBuilderFrame>(&IRBuilderFrameNode::AddCallback);
105TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilder").set_body_typed([]() { return IRBuilder(); });
106TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderEnter").set_body_method(&IRBuilder::EnterWithScope);
107TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderExit").set_body_method(&IRBuilder::ExitWithScope);
108TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderCurrent").set_body_typed(IRBuilder::Current);
109TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderGet")
110 .set_body_method<IRBuilder>(&IRBuilderNode::Get<ObjectRef>);
111TVM_REGISTER_GLOBAL("script.ir_builder.IRBuilderName").set_body_typed(IRBuilder::Name<ObjectRef>);
112
113} // namespace ir_builder
114} // namespace script
115} // namespace tvm
116