Skip to content

Implement gpu.subgroup_reduce with DPP intrinsics on AMD GPUs #20007

Closed

Description

@krzysz00

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_reduce to DPP operations
  • Ensure that said pattern is being invoked by IREE

Activity

  1. krzysz00 commented on Feb 17, 2025

    @krzysz00
    ContributorAuthor

    Note that the expandSubgroupReduction argument to the createConvertVectorReductionToGPUPass pass will need to be false - and can probably be removed

  2. Muzammiluddin-Syed-ECE commented on Feb 18, 2025

    @Muzammiluddin-Syed-ECE
    Contributor
  3. krzysz00 commented on Jun 9, 2025

    @krzysz00
    ContributorAuthor

    I think this is closable, +- the SPIR-V changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions