forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_unpacked_api.cc
More file actions
201 lines (170 loc) · 6.09 KB
/
make_unpacked_api.cc
File metadata and controls
201 lines (170 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file make_unpacked_api.cc Lower PrimFunc to a standard C function API.
*/
#include <tvm/runtime/device_api.h>
#include <tvm/runtime/registry.h>
#include <tvm/target/target.h>
#include <tvm/tir/analysis.h>
#include <tvm/tir/buffer.h>
#include <tvm/tir/builtin.h>
#include <tvm/tir/expr.h>
#include <tvm/tir/stmt_functor.h>
#include <tvm/tir/transform.h>
#include <unordered_set>
#include <utility>
#include <vector>
#include "arg_binder.h"
#include "ir_utils.h"
namespace tvm {
namespace tir {
namespace {
class SubroutineCallRewriter : public StmtExprMutator {
public:
static Optional<Stmt> Apply(const std::unordered_set<const GlobalVarNode*>& external_methods,
Stmt stmt) {
SubroutineCallRewriter rewriter(external_methods);
stmt = rewriter.VisitStmt(std::move(stmt));
if (rewriter.made_change_) {
return stmt;
} else {
return NullOpt;
}
}
private:
explicit SubroutineCallRewriter(const std::unordered_set<const GlobalVarNode*>& external_methods)
: external_methods_(external_methods) {}
PrimExpr VisitExpr_(const CallNode* op) override {
auto node = Downcast<Call>(StmtExprMutator::VisitExpr_(op));
if (auto gvar = node->op.as<GlobalVarNode>()) {
if (external_methods_.count(gvar)) {
Array<PrimExpr> args = node->args.Map([this](const PrimExpr& arg) -> PrimExpr {
if (auto* as_call = arg.as<CallNode>()) {
if (as_call->op.same_as(builtin::tvm_stack_make_array())) {
PrimExpr data_ptr = as_call->args[0];
made_change_ = true;
return data_ptr;
}
}
return arg;
});
if (!args.same_as(node->args)) {
node.CopyOnWrite()->args = args;
}
}
}
return std::move(node);
}
const std::unordered_set<const GlobalVarNode*>& external_methods_;
bool made_change_{false};
};
} // namespace
PrimFunc MakeUnpackedAPI(PrimFunc func) {
// A function with an explicit calling convention has already been
// lowered, and should not be modified.
if (auto opt = func->GetAttr<Integer>(tvm::attr::kCallingConv)) {
if (CallingConv(opt.value()->value) != CallingConv::kDefault) {
return func;
}
}
// Internal function calls do not need API updates
auto global_symbol = func->GetAttr<String>(tvm::attr::kGlobalSymbol);
if (!global_symbol.defined()) {
return func;
}
Target target = [&]() {
auto opt = func->GetAttr<Target>(tvm::attr::kTarget);
ICHECK(opt) << "MakeUnpackedAPI required the function to be annotated with tvm::attr::kTarget ("
<< tvm::attr::kTarget << "), but the function only has attributes " << func->attrs;
return opt.value();
}();
int target_device_type = target->GetTargetDeviceType();
// A function without a host target has already been lowered.
Target target_host;
if (auto opt = target->GetHost()) {
target_host = opt.value();
} else {
return func;
}
auto* func_ptr = func.CopyOnWrite();
// Setup device context
Integer device_type(target_device_type);
Integer device_id(0);
ObjectRef node = String("default");
const Stmt nop = Evaluate(0);
std::vector<Stmt> device_init;
// Collect variables and buffers to map between
Array<Var> args;
for (const Var& param : func->params) {
// Ideally all func params should have Buffers defined in the buffer_map
// We should look to insert buffer_maps for all PrimFuncs that are returned
// to the core compiler.
if (func->buffer_map.find(param) != func->buffer_map.end()) {
args.push_back(func->buffer_map[param]->data);
} else {
args.push_back(param);
}
}
if (func->buffer_map.size()) {
device_init.push_back(AttrStmt(node, attr::device_id, device_id, nop));
device_init.push_back(AttrStmt(node, attr::device_type, device_type, nop));
}
func_ptr->body = MergeNest(device_init, func_ptr->body);
func_ptr->params = args;
func_ptr->ret_type = PrimType(DataType::Int(32));
func_ptr->buffer_map = Map<Var, Buffer>();
// return the function.
return WithAttrs(std::move(func), {{tvm::attr::kTarget, target_host}});
}
namespace transform {
Pass MakeUnpackedAPI() {
auto pass_func = [](IRModule mod, PassContext ctx) {
std::unordered_set<const GlobalVarNode*> external_methods;
for (const auto& [gvar, base_func] : mod->functions) {
if (auto* prim_func = base_func.as<PrimFuncNode>()) {
if (prim_func->GetAttr<String>(tvm::attr::kGlobalSymbol)) {
external_methods.insert(gvar.get());
}
}
}
IRModule updates;
for (const auto& [gvar, base_func] : mod->functions) {
if (auto opt = base_func.as<PrimFunc>()) {
auto func = opt.value();
if (auto body = SubroutineCallRewriter::Apply(external_methods, func->body)) {
func.CopyOnWrite()->body = body.value();
}
func = MakeUnpackedAPI(std::move(func));
if (!func.same_as(base_func)) {
updates->Add(gvar, func);
}
}
}
if (updates->functions.size()) {
mod.CopyOnWrite()->Update(updates);
}
return mod;
};
return tvm::transform::CreateModulePass(pass_func, 0, "tir.MakeUnpackedAPI", {});
}
TVM_REGISTER_GLOBAL("tir.transform.MakeUnpackedAPI").set_body_typed(MakeUnpackedAPI);
} // namespace transform
} // namespace tir
} // namespace tvm