|
57 | 57 | # The easy case, transform the function for GPU execution
|
58 | 58 | # - mark constant arguments by applying `constify`.
|
59 | 59 | function transform_gpu!(def, constargs)
|
60 |
| - new_stmts = Expr[] |
| 60 | + let_constargs = Expr[] |
61 | 61 | for (i, arg) in enumerate(def[:args])
|
62 | 62 | if constargs[i]
|
63 |
| - push!(new_stmts, :($arg = $constify($arg))) |
| 63 | + push!(let_constargs, :($arg = $constify($arg))) |
64 | 64 | end
|
65 | 65 | end
|
66 | 66 |
|
67 |
| - def[:body] = quote |
| 67 | + body = quote |
68 | 68 | if $__validindex()
|
69 |
| - $(new_stmts...) |
70 | 69 | $(def[:body])
|
71 | 70 | end
|
72 | 71 | return nothing
|
73 | 72 | end
|
| 73 | + def[:body] = Expr(:let, |
| 74 | + Expr(:block, let_constargs...), |
| 75 | + body, |
| 76 | + ) |
74 | 77 | end
|
75 | 78 |
|
76 | 79 | # The hard case, transform the function for CPU execution
|
|
81 | 84 | # - hoist workgroup definitions
|
82 | 85 | # - hoist uniform variables
|
83 | 86 | function transform_cpu!(def, constargs)
|
84 |
| - new_stmts = Expr[] |
| 87 | + let_constargs = Expr[] |
85 | 88 | for (i, arg) in enumerate(def[:args])
|
86 | 89 | if constargs[i]
|
87 |
| - push!(new_stmts, :($arg = $constify($arg))) |
| 90 | + push!(let_constargs, :($arg = $constify($arg))) |
88 | 91 | end
|
89 | 92 | end
|
90 |
| - |
| 93 | + new_stmts = Expr[] |
91 | 94 | body = MacroTools.flatten(def[:body])
|
92 | 95 | push!(new_stmts, Expr(:aliasscope))
|
93 | 96 | append!(new_stmts, split(body.args))
|
94 | 97 | push!(new_stmts, Expr(:popaliasscope))
|
95 | 98 | push!(new_stmts, :(return nothing))
|
96 |
| - def[:body] = Expr(:block, new_stmts...) |
| 99 | + def[:body] = Expr(:let, |
| 100 | + Expr(:block, let_constargs...), |
| 101 | + Expr(:block, new_stmts...) |
| 102 | + ) |
97 | 103 | end
|
98 | 104 |
|
99 | 105 | struct WorkgroupLoop
|
|
0 commit comments