|
1 | 1 | # Work-Item Functions |
2 | | - |
3 | | -export get_work_dim, |
4 | | - get_global_size, get_global_id, |
5 | | - get_local_size, get_enqueued_local_size, get_local_id, |
6 | | - get_num_groups, get_group_id, |
7 | | - get_global_offset, |
8 | | - get_global_linear_id, get_local_linear_id |
| 2 | +# |
| 3 | +# https://registry.khronos.org/OpenCL/specs/3.0-unified/html/OpenCL_Env.html#_built_in_variables |
9 | 4 |
|
10 | 5 | # NOTE: these functions now unsafely truncate to Int to avoid top bit checks. |
11 | 6 | # we should probably use range metadata instead. |
12 | 7 |
|
13 | | -@device_function get_work_dim() = @builtin_ccall("get_work_dim", UInt32, ()) % Int |
14 | | - |
15 | | -@device_function get_global_size(dimindx::Integer = 1u32) = @builtin_ccall("get_global_size", UInt, (UInt32,), dimindx - 1u32) % Int |
16 | | -@device_function get_global_id(dimindx::Integer = 1u32) = @builtin_ccall("get_global_id", UInt, (UInt32,), dimindx - 1u32) % Int + 1 |
17 | | - |
18 | | -@device_function get_local_size(dimindx::Integer = 1u32) = @builtin_ccall("get_local_size", UInt, (UInt32,), dimindx - 1u32) % Int |
19 | | -@device_function get_enqueued_local_size(dimindx::Integer = 1u32) = @builtin_ccall("get_enqueued_local_size", UInt, (UInt32,), dimindx - 1u32) % Int |
20 | | -@device_function get_local_id(dimindx::Integer = 1u32) = @builtin_ccall("get_local_id", UInt, (UInt32,), dimindx - 1u32) % Int + 1 |
21 | | - |
22 | | -@device_function get_num_groups(dimindx::Integer = 1u32) = @builtin_ccall("get_num_groups", UInt, (UInt32,), dimindx - 1u32) % Int |
23 | | -@device_function get_group_id(dimindx::Integer = 1u32) = @builtin_ccall("get_group_id", UInt, (UInt32,), dimindx - 1u32) % Int + 1 |
24 | | - |
25 | | -@device_function get_global_offset(dimindx::Integer = 1u32) = @builtin_ccall("get_global_offset", UInt, (UInt32,), dimindx - 1u32) % Int + 1 |
| 8 | +# 1D values |
| 9 | +for (julia_name, (spirv_name, julia_type, offset)) in [ |
| 10 | + # indices |
| 11 | + :get_global_linear_id => (:BuiltInGlobalLinearId, Csize_t, 1), |
| 12 | + :get_local_linear_id => (:BuiltInLocalInvocationIndex, Csize_t, 1), |
| 13 | + :get_sub_group_id => (:BuiltInSubgroupId, UInt32, 1), |
| 14 | + :get_sub_group_local_id => (:BuiltInSubgroupLocalInvocationId, UInt32, 1), |
| 15 | + # sizes |
| 16 | + :get_work_dim => (:BuiltInWorkDim, UInt32, 0), |
| 17 | + :get_sub_group_size => (:BuiltInSubgroupSize, UInt32, 0), |
| 18 | + :get_max_sub_group_size => (:BuiltInSubgroupMaxSize, UInt32, 0), |
| 19 | + :get_num_sub_groups => (:BuiltInNumSubgroups, UInt32, 0), |
| 20 | + :get_enqueued_num_sub_groups => (:BuiltInNumEnqueuedSubgroups, UInt32, 0)] |
| 21 | + gvar_name = Symbol("@__spirv_$(spirv_name)") |
| 22 | + width = sizeof(julia_type) * 8 |
| 23 | + @eval begin |
| 24 | + export $julia_name |
| 25 | + @device_function $julia_name() = |
| 26 | + Base.llvmcall( |
| 27 | + $("""$gvar_name = external addrspace($(AS.Input)) global i$(width) |
| 28 | + define i$(width) @entry() #0 { |
| 29 | + %val = load i$(width), i$(width) addrspace($(AS.Input))* $gvar_name |
| 30 | + ret i$(width) %val |
| 31 | + } |
| 32 | + attributes #0 = { alwaysinline } |
| 33 | + """, "entry"), $julia_type, Tuple{}) % Int + $offset |
| 34 | + end |
| 35 | +end |
26 | 36 |
|
27 | | -@device_function get_global_linear_id() = @builtin_ccall("get_global_linear_id", UInt, ()) % Int + 1 |
28 | | -@device_function get_local_linear_id() = @builtin_ccall("get_local_linear_id", UInt, ()) % Int + 1 |
| 37 | +# 3D values |
| 38 | +for (julia_name, (spirv_name, offset)) in [ |
| 39 | + # indices |
| 40 | + :get_global_id => (:BuiltInGlobalInvocationId, 1), |
| 41 | + :get_global_offset => (:BuiltInGlobalOffset, 1), |
| 42 | + :get_local_id => (:BuiltInLocalInvocationId, 1), |
| 43 | + :get_group_id => (:BuiltInWorkgroupId, 1), |
| 44 | + # sizes |
| 45 | + :get_global_size => (:BuiltInGlobalSize, 0), |
| 46 | + :get_local_size => (:BuiltInWorkgroupSize, 0), |
| 47 | + :get_enqueued_local_size => (:BuiltInEnqueuedWorkgroupSize, 0), |
| 48 | + :get_num_groups => (:BuiltInNumWorkgroups, 0)] |
| 49 | + gvar_name = Symbol("@__spirv_$(spirv_name)") |
| 50 | + width = Int === Int64 ? 64 : 32 |
| 51 | + @eval begin |
| 52 | + export $julia_name |
| 53 | + @device_function $julia_name(dimindx::Integer=1u32) = |
| 54 | + Base.llvmcall( |
| 55 | + $("""$gvar_name = external addrspace($(AS.Input)) global <3 x i$(width)> |
| 56 | + define i$(width) @entry(i$(width) %idx) #0 { |
| 57 | + %val = load <3 x i$(width)>, <3 x i$(width)> addrspace($(AS.Input))* $gvar_name |
| 58 | + %element = extractelement <3 x i$(width)> %val, i$(width) %idx |
| 59 | + ret i$(width) %element |
| 60 | + } |
| 61 | + attributes #0 = { alwaysinline } |
| 62 | + """, "entry"), UInt, Tuple{UInt}, UInt(dimindx - 1u32)) % Int + $offset |
| 63 | + end |
| 64 | +end |
0 commit comments