Repository navigation
Description
Context
gpu.subgroup_reduce is the MLIR operation that implements, in relevant part, subgroup reductions - that is, each lane provides some value in, and all the lanes end up with out = op(in[lane0], in[rane1], ... op[laneN-1], identity(op)).
The documentation for gpu.subgroup_reduce is at https://mlir.llvm.org/docs/Dialects/GPU/#gpusubgroup_mma_store_matrix-gpusubgroupmmastorematrixop
This operation is important for attention kernels and operations that rhyme with matrix-vector products.
Currently, the default lowering of this operation is to use gpu.shuffle xor in order to transport values between lanes, which in turn lowers to the ds.permute or ds.bpermute intrinsics on AMD GPUs, which are slow because they use LDS.
However, thanks to the DPP modifier on instructions, we can do better than using these general-purpose permutation operators. For our purposes, DPP is an operation that performs one of a set of specified permutations on some input value across all lanes.
Prior work
The device libraries implement a suite of reduction functions as generated by macros at https://github.com/ROCm/llvm-project/blob/amd-staging/amd/device-libs/ockl/src/wfredscan.cl#L295 . These only implement reduction across the entire subgroup/wave.
LLPC - the graphics compiler, implements the SPIR-V subgroup reduction operator, which gpu.subgroup_reduce seems to be based on, at https://github.com/GPUOpen-Drivers/llpc/blob/b36b68fff3bc4bfb171ffa1e23597309d5d52cd6/lgc/builder/SubgroupBuilder.cpp#L564 . Note that all the stuff about "set inactive" and "WQM" and "whole wave mode" isn't relevant unless there are plans to have the reduction inside an if statement where some lanes might not be executing ... and even then, I think the DPP update intrinsic might do the right thing anyway.
The https://mlir.llvm.org/docs/Dialects/AMDGPU/#amdgpudpp-amdgpudppop is the MLIR abstraction for DPP intrinsics, which can be used to get a user-friendly API over them (including doing the right thing when it comes to typecasts)
What's wanted
- Implement a pattern upstream that rewrites
gpu.subgroup_reduceto DPP operations - Ensure that said pattern is being invoked by IREE
Activity
krzysz00 commentedon Feb 17, 2025
Note that the
expandSubgroupReductionargument to thecreateConvertVectorReductionToGPUPasspass will need to be false - and can probably be removedkuhar commentedon Feb 18, 2025
Also perf investigation from @knwng: https://github.com/nod-ai/shark-ai/blob/main/docs/amdgpu_kernel_optimization_guide.md#data-parallel-primitives-and-warp-level-reduction
Muzammiluddin-Syed-ECE commentedon Feb 18, 2025
@Muzammiluddin-Syed-ECE
[mlir][AMDGPU] Implement gpu.subgroup_reduce with DPP intrinsics on A…
Automerge: [mlir][AMDGPU] Implement gpu.subgroup_reduce with DPP intr…
[Codegen][GPU] Lower gpu.subgroup_reduce to DPP intrinsics on AMD GPUs (
krzysz00 commentedon Jun 9, 2025
I think this is closable, +- the SPIR-V changes
[Codegen][GPU] Lower gpu.subgroup_reduce to DPP intrinsics on AMD GPUs (
[mlir][AMDGPU] Implement gpu.subgroup_reduce with DPP intrinsics on A…