AMDGPU workgroup size as metadata

From LLVM IR, how can you get the 'workgroup size' value?
It seems to be set by the AMDGPU backend as metadata since in AMDGPUMetadata.h there are things defined like

constexpr char ReqdWorkGroupSize[] = "ReqdWorkGroupSize";


struct Metadata final {
/// 'reqd_work_group_size' attribute. Optional.
std::vector<uint32_t> mReqdWorkGroupSize = std::vector<uint32_t>();

Is this metadata set to the kernel function or to the module?

What IR instructions would give access to the value of, say, the workgroup size in dimension x?


The code object metadata is only for statically known workgroup size information The metadata you found here corresponds to !reqd_work_group_size, corresponding to the OpenCL attribute of the same name. We have a variety of other static attributes useful related to workgroup sizes, as documented here: The "uniform-work-group-size” (corresponding to the OpenCL flag -cl-uniform-work-group-size) may also be of interest.

Dynamically, there isn’t a single instruction to get the group size and it depends on the runtime/driver how to implement it. You need to get a pointer to somewhere, and load from it. For HSA/ROCm, these are loaded from an ABI struct pointed to by a special kernel input SGPR. Recently the core implementation was moved into clang builtin so we can annotate the load with !range metadata: You can see how these are used here: