Skip to content

Commit f6a535e

Browse files
committed
emit let statement for constify
1 parent 0f31c52 commit f6a535e

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/macros.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ end
5757
# The easy case, transform the function for GPU execution
5858
# - mark constant arguments by applying `constify`.
5959
function transform_gpu!(def, constargs)
60-
new_stmts = Expr[]
60+
let_constargs = Expr[]
6161
for (i, arg) in enumerate(def[:args])
6262
if constargs[i]
63-
push!(new_stmts, :($arg = $constify($arg)))
63+
push!(let_constargs, :($arg = $constify($arg)))
6464
end
6565
end
6666

67-
def[:body] = quote
67+
body = quote
6868
if $__validindex()
69-
$(new_stmts...)
7069
$(def[:body])
7170
end
7271
return nothing
7372
end
73+
def[:body] = Expr(:let,
74+
Expr(:block, let_constargs...),
75+
body,
76+
)
7477
end
7578

7679
# The hard case, transform the function for CPU execution
@@ -81,19 +84,22 @@ end
8184
# - hoist workgroup definitions
8285
# - hoist uniform variables
8386
function transform_cpu!(def, constargs)
84-
new_stmts = Expr[]
87+
let_constargs = Expr[]
8588
for (i, arg) in enumerate(def[:args])
8689
if constargs[i]
87-
push!(new_stmts, :($arg = $constify($arg)))
90+
push!(let_constargs, :($arg = $constify($arg)))
8891
end
8992
end
90-
93+
new_stmts = Expr[]
9194
body = MacroTools.flatten(def[:body])
9295
push!(new_stmts, Expr(:aliasscope))
9396
append!(new_stmts, split(body.args))
9497
push!(new_stmts, Expr(:popaliasscope))
9598
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+
)
97103
end
98104

99105
struct WorkgroupLoop

0 commit comments

Comments
 (0)