Skip to content

Commit 9b2d508

Browse files
authored
Introduce a macro for marking multiple functions as device-only. (#1117)
Use it to mark the indexing functions device-only.
1 parent fb7440f commit 9b2d508

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/device/intrinsics/indexing.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ for dim in (:x, :y, :z)
6161
@eval @inline $fn() = Int(_index($(Val(intr)), $(Val(1:max_grid_size[dim]))))
6262
end
6363

64+
@device_functions begin
65+
6466
"""
6567
gridDim()::CuDim3
6668
@@ -111,4 +113,6 @@ executing thread.
111113
"""
112114
@inline active_mask() = @asmcall("activemask.b32 \$0;", "=r", false, UInt32, Tuple{})
113115

116+
end
117+
114118
const FULL_MASK = 0xffffffff

src/device/utils.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ macro device_function(ex)
3636
@device_override $ex
3737
end)
3838
end
39+
40+
macro device_functions(ex)
41+
ex = macroexpand(__module__, ex)
42+
43+
# recursively prepend `@device_function` to all function definitions
44+
function rewrite(block)
45+
out = Expr(:block)
46+
for arg in block.args
47+
if Meta.isexpr(arg, :block)
48+
# descend in blocks
49+
push!(out.args, rewrite(arg))
50+
elseif Meta.isexpr(arg, [:function, :(=)])
51+
# rewrite function definitions
52+
push!(out.args, :(@device_function $arg))
53+
else
54+
# preserve all the rest
55+
push!(out.args, arg)
56+
end
57+
end
58+
out
59+
end
60+
61+
esc(rewrite(ex))
62+
end

0 commit comments

Comments
 (0)