forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCalls.cpp
More file actions
85 lines (70 loc) · 2.06 KB
/
FindCalls.cpp
File metadata and controls
85 lines (70 loc) · 2.06 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
#include "FindCalls.h"
#include "IRVisitor.h"
namespace Halide{
namespace Internal {
using std::map;
using std::vector;
using std::string;
using std::pair;
/* Find all the internal halide calls in an expr */
class FindCalls : public IRVisitor {
public:
map<string, Function> calls;
using IRVisitor::visit;
void include_function(Function f) {
map<string, Function>::iterator iter = calls.find(f.name());
if (iter == calls.end()) {
calls[f.name()] = f;
} else {
user_assert(iter->second.same_as(f))
<< "Can't compile a pipeline using multiple functions with same name: "
<< f.name() << "\n";
}
}
void visit(const Call *call) {
IRVisitor::visit(call);
if (call->call_type == Call::Halide) {
Function f = call->func;
include_function(f);
}
}
};
void populate_environment(Function f, map<string, Function> &env, bool recursive = true) {
map<string, Function>::const_iterator iter = env.find(f.name());
if (iter != env.end()) {
user_assert(iter->second.same_as(f))
<< "Can't compile a pipeline using multiple functions with same name: "
<< f.name() << "\n";
return;
}
FindCalls calls;
f.accept(&calls);
if (f.has_extern_definition()) {
for (ExternFuncArgument arg : f.extern_arguments()) {
if (arg.is_func()) {
Function g(arg.func);
calls.calls[g.name()] = g;
}
}
}
if (!recursive) {
env.insert(calls.calls.begin(), calls.calls.end());
} else {
env[f.name()] = f;
for (pair<string, Function> i : calls.calls) {
populate_environment(i.second, env);
}
}
}
map<string, Function> find_transitive_calls(Function f) {
map<string, Function> res;
populate_environment(f, res, true);
return res;
}
map<string, Function> find_direct_calls(Function f) {
map<string, Function> res;
populate_environment(f, res, false);
return res;
}
}
}