-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathmacros.jl
More file actions
300 lines (275 loc) · 9.18 KB
/
macros.jl
File metadata and controls
300 lines (275 loc) · 9.18 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import MacroTools: splitdef, combinedef, isexpr, postwalk
function find_return(stmt)
result = false
postwalk(stmt) do expr
result |= @capture(expr, return x_)
expr
end
result
end
# XXX: Proper errors
function __kernel(N, expr, generate_cpu = true, force_inbounds = false)
def = splitdef(expr)
name = def[:name]
args = def[:args]
generate_cpu && find_return(expr) && error(
"Return statement not permitted in a kernel function $name",
)
constargs = Array{Bool}(undef, length(args))
for (i, arg) in enumerate(args)
if isexpr(arg, :macrocall)
if arg.args[1] === Symbol("@Const")
# arg.args[2] is a LineInfo node
args[i] = arg.args[3] # strip @Const
constargs[i] = true
continue
end
end
constargs[i] = false
end
# create two functions
# 1. GPU function
# 2. CPU function with work-group loops inserted
#
# Without the deepcopy we might accidentially modify expr shared between CPU and GPU
cpu_name = Symbol(:cpu_, name)
if generate_cpu
def_cpu = deepcopy(def)
def_cpu[:name] = cpu_name
transform_cpu!(def_cpu, constargs, force_inbounds)
cpu_function = combinedef(def_cpu)
end
def_gpu = deepcopy(def)
def_gpu[:name] = gpu_name = Symbol(:gpu_, name)
transform_gpu!(def_gpu, constargs, force_inbounds)
gpu_function = combinedef(def_gpu)
# create constructor functions
constructors = quote
if $(name isa Symbol ? :(!@isdefined($name)) : true)
Core.@__doc__ $name(dev) = $name(dev, $DynamicSize(), $DynamicSize())
$name(dev, size) = $name(dev, $StaticSize(size), $DynamicSize())
$name(dev, size, range) = $name(dev, $StaticSize(size), $StaticSize(range))
function $name(dev::Dev, sz::S, range::NDRange) where {Dev, S <: $_Size, NDRange <: $_Size}
if $isgpu(dev)
return $construct(dev, $(N), sz, range, $gpu_name)
else
if $generate_cpu
return $construct(dev, $(N), sz, range, $cpu_name)
else
error("This kernel is unavailable for backend CPU")
end
end
end
end
end
if generate_cpu
return Expr(:block, esc(cpu_function), esc(gpu_function), esc(constructors))
else
return Expr(:block, esc(gpu_function), esc(constructors))
end
end
# The easy case, transform the function for GPU execution
# - mark constant arguments by applying `constify`.
function transform_gpu!(def, constargs, force_inbounds)
let_constargs = Expr[]
for (i, arg) in enumerate(def[:args])
if constargs[i]
push!(let_constargs, :($arg = $constify($arg)))
end
end
pushfirst!(def[:args], :__ctx__)
body = def[:body]
if force_inbounds
body = quote
@inbounds $(body)
end
end
body = quote
if $__validindex(__ctx__)
$(body)
end
return nothing
end
def[:body] = Expr(
:let,
Expr(:block, let_constargs...),
body,
)
end
# The hard case, transform the function for CPU execution
# - mark constant arguments by applying `constify`.
# - insert aliasscope markers
# - insert implied loop bodys
# - handle indicies
# - hoist workgroup definitions
# - hoist uniform variables
function transform_cpu!(def, constargs, force_inbounds)
let_constargs = Expr[]
for (i, arg) in enumerate(def[:args])
if constargs[i]
push!(let_constargs, :($arg = $constify($arg)))
end
end
pushfirst!(def[:args], :__ctx__)
new_stmts = Expr[]
body = MacroTools.flatten(def[:body])
push!(new_stmts, Expr(:aliasscope))
if force_inbounds
push!(new_stmts, Expr(:inbounds, true))
end
append!(new_stmts, split(body.args))
if force_inbounds
push!(new_stmts, Expr(:inbounds, :pop))
end
push!(new_stmts, Expr(:popaliasscope))
push!(new_stmts, :(return nothing))
def[:body] = Expr(
:let,
Expr(:block, let_constargs...),
Expr(:block, new_stmts...),
)
end
struct WorkgroupLoop
indicies::Vector{Any}
stmts::Vector{Any}
allocations::Vector{Any}
private_allocations::Vector{Any}
private::Set{Symbol}
end
is_sync(expr) = @capture(expr, @synchronize() | @synchronize(a_))
function is_scope_construct(expr::Expr)
expr.head === :block # ||
# expr.head === :let
end
function find_sync(stmt)
result = false
postwalk(stmt) do expr
result |= is_sync(expr)
expr
end
result
end
# TODO proper handling of LineInfo
function split(
stmts,
indicies = Any[], private = Set{Symbol}(),
)
# 1. Split the code into blocks separated by `@synchronize`
# 2. Aggregate `@index` expressions
# 3. Hoist allocations
# 4. Hoist uniforms
current = Any[]
allocations = Any[]
private_allocations = Any[]
new_stmts = Any[]
for stmt in stmts
has_sync = find_sync(stmt)
if has_sync
loop = WorkgroupLoop(deepcopy(indicies), current, allocations, private_allocations, deepcopy(private))
push!(new_stmts, emit(loop))
allocations = Any[]
private_allocations = Any[]
current = Any[]
is_sync(stmt) && continue
# Recurse into scope constructs
# TODO: This currently implements hard scoping
# probably need to implemet soft scoping
# by not deepcopying the environment.
recurse(x) = x
function recurse(expr::Expr)
expr = unblock(expr)
if is_scope_construct(expr) && any(find_sync, expr.args)
new_args = unblock(split(expr.args, deepcopy(indicies), deepcopy(private)))
return Expr(expr.head, new_args...)
else
return Expr(expr.head, map(recurse, expr.args)...)
end
end
push!(new_stmts, recurse(stmt))
continue
end
if @capture(stmt, @uniform x_)
push!(allocations, stmt)
continue
elseif @capture(stmt, @private lhs_ = rhs_)
push!(private, lhs)
push!(private_allocations, :($lhs = $rhs))
continue
elseif @capture(stmt, lhs_ = rhs_ | (vs__, lhs_ = rhs_))
if @capture(rhs, @index(args__))
push!(indicies, stmt)
continue
elseif @capture(rhs, @localmem(args__) | @uniform(args__))
push!(allocations, stmt)
continue
elseif @capture(rhs, @private(T_, dims_))
# Implement the legacy `mem = @private T dims` as
# mem = Scratchpad(T, Val(dims))
if dims isa Integer
dims = (dims,)
end
alloc = :($Scratchpad(__ctx__, $T, Val($dims)))
push!(allocations, :($lhs = $alloc))
push!(private, lhs)
continue
end
end
push!(current, stmt)
end
# everything since the last `@synchronize`
if !isempty(current)
loop = WorkgroupLoop(deepcopy(indicies), current, allocations, private_allocations, deepcopy(private))
push!(new_stmts, emit(loop))
end
return new_stmts
end
function emit(loop)
idx = gensym(:I)
for stmt in loop.indicies
# splice index into the i = @index(Cartesian, $idx)
@assert stmt.head === :(=)
rhs = stmt.args[2]
push!(rhs.args, idx)
end
stmts = Any[]
append!(stmts, loop.allocations)
# private_allocations turn into lhs = ntuple(i->rhs, length(__workitems_iterspace()))
N = gensym(:N)
push!(stmts, :($N = length($__workitems_iterspace(__ctx__))))
for stmt in loop.private_allocations
if @capture(stmt, lhs_ = rhs_)
push!(stmts, :($lhs = ntuple(_ -> $rhs, $N)))
else
error("@private $stmt not an assignment")
end
end
# don't emit empty loops
if !(isempty(loop.stmts) || all(s -> s isa LineNumberNode, loop.stmts))
body = Expr(:block, loop.stmts...)
body = postwalk(body) do expr
if @capture(expr, lhs_ = rhs_)
if lhs in loop.private
error("Can't assign to variables marked private")
end
elseif @capture(expr, A_[i__])
if A in loop.private
return :($A[$__index_Local_Linear(__ctx__, $(idx))][$(i...)])
end
elseif expr isa Symbol
if expr in loop.private
return :($expr[$__index_Local_Linear(__ctx__, $(idx))])
end
end
return expr
end
loopexpr = quote
for $idx in $__workitems_iterspace(__ctx__)
$__validindex(__ctx__, $idx) || continue
$(loop.indicies...)
$(unblock(body))
end
end
push!(stmts, loopexpr)
end
return unblock(Expr(:block, stmts...))
end