-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEarlyFree.cpp
159 lines (131 loc) · 3.99 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
#include <map>
#include "EarlyFree.h"
#include "ExprUsesVar.h"
#include "IREquality.h"
#include "IRMutator.h"
#include "InjectHostDevBufferCopies.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) {}
private:
bool in_loop = false;
Stmt containing_stmt;
using IRVisitor::visit;
void visit(const For *loop) {
loop->min.accept(this);
loop->extent.accept(this);
ScopedValue<bool> old_in_loop(in_loop, true);
loop->body.accept(this);
}
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 (var->name == func || var->name == func + ".buffer") {
// Don't free the allocation while a buffer that may refer
// to it is still in use.
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);
ScopedValue<bool> old_in_loop(in_loop, true);
op->then_case.accept(this);
if (op->else_case.defined()) {
op->else_case.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 IRMutator2 {
public:
string func;
Stmt last_use;
private:
bool injected = false;
using IRMutator2::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);
}
}
Stmt visit(const Block *block) override {
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)) {
return block;
} else {
return Block::make(new_first, new_rest);
}
}
};
class InjectEarlyFrees : public IRMutator2 {
using IRMutator2::visit;
Stmt visit(const Allocate *alloc) override {
Stmt stmt = IRMutator2::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->memory_type,
alloc->extents, alloc->condition,
Block::make(alloc->body, Free::make(alloc->name)),
alloc->new_expr, alloc->free_function);
}
return stmt;
}
};
Stmt inject_early_frees(Stmt s) {
InjectEarlyFrees early_frees;
return early_frees.mutate(s);
}
} // namespace Internal
} // namespace Halide