-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEarlyFree.cpp
201 lines (162 loc) · 4.94 KB
/
EarlyFree.cpp
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
#include <map>
#include "EarlyFree.h"
#include "IRVisitor.h"
#include "IRMutator.h"
namespace Halide {
namespace Internal {
using std::map;
using std::string;
using std::vector;
class FindLastUse : public IRVisitor {
public:
string func;
Stmt last_use;
FindLastUse(string s) : func(s), in_loop(false) {}
private:
bool in_loop;
Stmt containing_stmt;
using IRVisitor::visit;
void visit(const For *loop) {
loop->min.accept(this);
loop->extent.accept(this);
bool old_in_loop = in_loop;
in_loop = true;
loop->body.accept(this);
in_loop = old_in_loop;
}
void visit(const Load *load) {
if (func == load->name) {
last_use = containing_stmt;
}
IRVisitor::visit(load);
}
void visit(const Call *call) {
if (call->name == func) {
last_use = containing_stmt;
}
IRVisitor::visit(call);
}
void visit(const Store *store) {
if (func == store->name) {
last_use = containing_stmt;
}
IRVisitor::visit(store);
}
void visit(const Variable *var) {
if (starts_with(var->name, func + ".") &&
(ends_with(var->name, ".buffer") || ends_with(var->name, ".host"))) {
last_use = containing_stmt;
}
}
void visit(const IfThenElse *op) {
// It's a bad idea to inject it in either side of an
// ifthenelse, so we treat this as being in a loop.
op->condition.accept(this);
bool old_in_loop = in_loop;
in_loop = true;
op->then_case.accept(this);
if (op->else_case.defined()) {
op->else_case.accept(this);
}
in_loop = old_in_loop;
}
void visit(const Pipeline *pipe) {
if (in_loop) {
IRVisitor::visit(pipe);
} else {
Stmt old_containing_stmt = containing_stmt;
containing_stmt = pipe->produce;
pipe->produce.accept(this);
if (pipe->update.defined()) {
containing_stmt = pipe->update;
pipe->update.accept(this);
}
containing_stmt = old_containing_stmt;
pipe->consume.accept(this);
}
}
void visit(const Block *block) {
if (in_loop) {
IRVisitor::visit(block);
} else {
Stmt old_containing_stmt = containing_stmt;
containing_stmt = block->first;
block->first.accept(this);
if (block->rest.defined()) {
containing_stmt = block->rest;
block->rest.accept(this);
}
containing_stmt = old_containing_stmt;
}
}
};
class InjectMarker : public IRMutator {
public:
string func;
Stmt last_use;
InjectMarker() : injected(false) {}
private:
bool injected;
using IRMutator::visit;
Stmt inject_marker(Stmt s) {
if (injected) return s;
if (s.same_as(last_use)) {
injected = true;
return Block::make(s, Free::make(func));
} else {
return mutate(s);
}
}
void visit(const Pipeline *pipe) {
// Do it in reverse order, so the injection occurs in the last instance of the stmt.
Stmt new_consume = inject_marker(pipe->consume);
Stmt new_update;
if (pipe->update.defined()) {
new_update = inject_marker(pipe->update);
}
Stmt new_produce = inject_marker(pipe->produce);
if (new_produce.same_as(pipe->produce) &&
new_update.same_as(pipe->update) &&
new_consume.same_as(pipe->consume)) {
stmt = pipe;
} else {
stmt = Pipeline::make(pipe->name, new_produce, new_update, new_consume);
}
}
void visit(const Block *block) {
Stmt new_rest = inject_marker(block->rest);
Stmt new_first = inject_marker(block->first);
if (new_first.same_as(block->first) &&
new_rest.same_as(block->rest)) {
stmt = block;
} else {
stmt = Block::make(new_first, new_rest);
}
}
};
class InjectEarlyFrees : public IRMutator {
using IRMutator::visit;
void visit(const Allocate *alloc) {
IRMutator::visit(alloc);
alloc = stmt.as<Allocate>();
internal_assert(alloc);
FindLastUse last_use(alloc->name);
stmt.accept(&last_use);
if (last_use.last_use.defined()) {
InjectMarker inject_marker;
inject_marker.func = alloc->name;
inject_marker.last_use = last_use.last_use;
stmt = inject_marker.mutate(stmt);
} else {
stmt = Allocate::make(alloc->name, alloc->type, alloc->extents, alloc->condition,
Block::make(alloc->body, Free::make(alloc->name)));
}
}
};
Stmt inject_early_frees(Stmt s) {
InjectEarlyFrees early_frees;
return early_frees.mutate(s);
}
// TODO: test
}
}