Skip to content

Commit 3bde6af

Browse files
committed
GPU: make the kernel entry-point signature work on Metal
Two things MSL does differently from every other backend. It has no work-item builtins: the grid dimensions arrive as kernel attributes. Without a branch of its own Metal fell through to the host definitions of get_group_id() and friends, which name iBlock and nBlocks, variables that only exist in the host-side loop. And it requires every kernel parameter to carry an attribute, so the sector index cannot be passed by value. GPUCA_KRNLGPU_DEF therefore gets two hooks, GPUCA_KRNL_SECTOR_ARG and GPUCA_KRNL_GRID_ARGS, which the Metal source fills in with a buffer and the four grid attributes. Both default to what the signature had, so CUDA, HIP and OpenCL generate exactly the same entry point as before. Kernel list diagnostics: 408 to 96, and the translation unit 1108 to 998. The 96 left are the 48 kernels that take arguments, which still need an answer for how Metal passes them.
1 parent 8d0a553 commit 3bde6af

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎GPU/Common/GPUCommonDefAPI.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@
277277
#define get_group_id(dim) (blockIdx.x)
278278
#elif defined(__OPENCL__)
279279
// Using OpenCL defaults
280+
#elif defined(__METAL__)
281+
// MSL has no work-item builtins; these come in as kernel attributes, declared
282+
// by GPUCA_KRNL_GRID_ARGS on every entry point.
283+
#define get_global_id(dim) (_metalTgIg * _metalTPerTg + _metalTiTg)
284+
#define get_global_size(dim) (_metalTPerTg * _metalTgPerG)
285+
#define get_num_groups(dim) (_metalTgPerG)
286+
#define get_local_id(dim) (_metalTiTg)
287+
#define get_local_size(dim) (_metalTPerTg)
288+
#define get_group_id(dim) (_metalTgIg)
280289
#else
281290
#define get_global_id(dim) iBlock
282291
#define get_global_size(dim) nBlocks

‎GPU/GPUTracking/Base/GPUReconstructionKernelMacros.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,17 @@
6363
#define GPUCA_ATTRRES(...) GPUCA_M_EXPAND(GPUCA_M_CAT(GPUCA_ATTRRES_, GPUCA_M_FIRST(__VA_ARGS__)))(__VA_ARGS__)
6464

6565
// GPU Kernel entry point
66+
// MSL requires every kernel parameter to carry an attribute, and supplies the
67+
// grid dimensions the same way, so the backend gets to shape both ends of the
68+
// parameter list.
69+
#ifndef GPUCA_KRNL_SECTOR_ARG
70+
#define GPUCA_KRNL_SECTOR_ARG int32_t _iSector_internal
71+
#endif
72+
#ifndef GPUCA_KRNL_GRID_ARGS
73+
#define GPUCA_KRNL_GRID_ARGS
74+
#endif
6675
#define GPUCA_KRNLGPU_DEF(x_class, x_attributes, x_arguments, ...) \
67-
GPUg() void GPUCA_ATTRRES(GPUCA_M_STRIP(x_attributes)) GPUCA_M_CAT(krnl_, GPUCA_M_KRNL_NAME(x_class))(GPUCA_CONSMEM_PTR int32_t _iSector_internal GPUCA_M_STRIP(x_arguments))
76+
GPUg() void GPUCA_ATTRRES(GPUCA_M_STRIP(x_attributes)) GPUCA_M_CAT(krnl_, GPUCA_M_KRNL_NAME(x_class))(GPUCA_CONSMEM_PTR GPUCA_KRNL_SECTOR_ARG GPUCA_M_STRIP(x_arguments) GPUCA_KRNL_GRID_ARGS)
6877

6978
#ifdef GPUCA_KRNL_DEFONLY
7079
#define GPUCA_KRNLGPU(...) GPUCA_KRNLGPU_DEF(__VA_ARGS__);

‎GPU/GPUTracking/Base/metal/GPUReconstructionMETAL.metal‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ using namespace metal;
7070
device char* pConstantRaw [[buffer(1)]],
7171
#define GPUCA_CONSMEM (*(device GPUConstantMem*)pConstantRaw)
7272

73+
// Every kernel parameter needs an attribute, so the sector index arrives as a
74+
// buffer rather than by value, and the grid dimensions come in at the end, where
75+
// GPUCommonDefAPI.h's get_group_id() and friends pick them up.
76+
#define GPUCA_KRNL_SECTOR_ARG constant int32_t& _iSector_internal [[buffer(2)]]
77+
#define GPUCA_KRNL_GRID_ARGS \
78+
, uint _metalTgIg [[threadgroup_position_in_grid]] \
79+
, uint _metalTiTg [[thread_position_in_threadgroup]] \
80+
, uint _metalTPerTg [[threads_per_threadgroup]] \
81+
, uint _metalTgPerG [[threadgroups_per_grid]]
82+
7383
#include "GPUReconstructionKernelList.h"
7484

7585
// clang-format on

0 commit comments

Comments
 (0)