forked from halide/Halide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTuple.cpp
More file actions
79 lines (66 loc) · 2.67 KB
/
Tuple.cpp
File metadata and controls
79 lines (66 loc) · 2.67 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
#include "Tuple.h"
#include "Expr.h"
#include "Func.h"
#include "Var.h"
namespace Halide {
Tuple::operator Expr() const {
std::vector<Expr> callArgs;
std::vector<Expr> definitionArgs;
// Note on reduction vars captured:
// In the call to the anonymous function they're reduction
// vars passed as arguments
// In the definition of the anonymous function on the LHS and
// RHS they're just regular vars with the same name
Var tupleIndex;
Expr body;
int idx = 0;
std::vector<std::string> callArgNames, defArgNames;
// Grab the vars and reduction vars in the tuple args as arguments to the anonymous function
for (const Expr &expr : contents) {
Expr e = expr;
for (size_t i = 0; i < e.vars().size(); i++) {
bool already_exists = false;
for (size_t j = 0; j < callArgNames.size(); j++) {
if (e.vars()[i].name() == callArgNames[j]) already_exists = true;
}
if (!already_exists) {
callArgs.push_back(e.vars()[i]);
callArgNames.push_back(e.vars()[i].name());
}
}
for (int i = 0; i < e.rdom().dimensions(); i++) {
bool already_exists = false;
for (size_t j = 0; j < callArgNames.size(); j++) {
if (e.rdom()[i].name() == callArgNames[j]) already_exists = true;
}
if (!already_exists) {
callArgs.push_back(e.rdom()[i]);
callArgNames.push_back(e.rdom()[i].name());
}
}
e.convertRVarsToVars();
for (size_t i = 0; i < e.vars().size(); i++) {
bool already_exists = false;
for (size_t j = 0; j < defArgNames.size(); j++) {
if (e.vars()[i].name() == defArgNames[j]) already_exists = true;
}
if (!already_exists) {
definitionArgs.push_back(e.vars()[i]);
defArgNames.push_back(e.vars()[i].name());
}
}
if (idx == 0) body = e;
else body = select((tupleIndex % (int)contents.size()) == idx, e, body);
idx++;
}
definitionArgs.push_back(tupleIndex);
Func anon;
anon(definitionArgs) = body;
Expr result = anon(callArgs);
result.shape().push_back((int)contents.size());
return result;
}
Tuple operator,(const Expr &a, const Expr &b) {
return Tuple(a, b);
}
}