llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Issue]: HipKittens Persistent GEMM VGPR Spilling

Open ryanswann-amd opened this issue 11 months ago • 2 comments

Problem Description

Summary

The AMDGPU backend appears to have a register allocation issue with persistent kernel patterns (kernels with outer loops processing multiple work items). A semantically identical kernel that uses a persistent loop pattern shows a 23% increase in VGPR usage (208 → 256 VGPRs) and introduces register spilling.

  • Compiler: hipcc (LLVM-based)
  • Compilation Flags: -O3 -std=c++20 --offload-arch=gfx950 -Rpass-analysis=kernel-resource-usage

Problem Description

We have two functionally identical GEMM kernels:

  1. Non-persistent kernel (micro_tk): Processes one output tile per workgroup
  2. Persistent kernel (micro_tk_persistent): Processes multiple output tiles per workgroup using an outer loop

The persistent kernel shows significantly worse register allocation:

Non-Persistent Kernel Results:

256_256_64_32_with16x32.cpp:40:1: remark: Function Name: _Z8micro_tk13micro_globals [-Rpass-analysis=kernel-resource-usage]
   40 | void micro_tk(const micro_globals g) {
      | ^
256_256_64_32_with16x32.cpp:40:1: remark:     TotalSGPRs: 49 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     VGPRs: 208 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     AGPRs: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     ScratchSize [bytes/lane]: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     Dynamic Stack: False [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     Occupancy [waves/SIMD]: 2 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     SGPRs Spill: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     VGPRs Spill: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32.cpp:40:1: remark:     LDS Size [bytes/block]: 0 [-Rpass-analysis=kernel-resource-usage]

Persistent Kernel Results:

256_256_64_32_with16x32_persistent.cpp:42:1: remark: Function Name: _Z19micro_tk_persistent13micro_globals [-Rpass-analysis=kernel-resource-usage]
   42 |                   2) void micro_tk_persistent(const micro_globals g) {
      | ^
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     TotalSGPRs: 56 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     VGPRs: 256 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     AGPRs: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     ScratchSize [bytes/lane]: 80 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     Dynamic Stack: False [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     Occupancy [waves/SIMD]: 2 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     SGPRs Spill: 0 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     VGPRs Spill: 19 [-Rpass-analysis=kernel-resource-usage]
256_256_64_32_with16x32_persistent.cpp:42:1: remark:     LDS Size [bytes/block]: 0 [-Rpass-analysis=kernel-resource-usage]

Key Differences:

Metric Non-Persistent Persistent Delta
VGPRs 208 256 +48 (+23%)
SGPRs 49 56 +7
VGPR Spills 0 19 +19
Scratch Size 0 80 bytes +80 bytes
Performance (8192x8192) 1590.25 TFLOPS 1238.29 TFLOPS -351.96 TFLOPS (-22%)

Analysis

Expected Behavior:

The persistent kernel should use approximately the same number of VGPRs as all allocations are made outside of the loop.

Operating System

22.04.5 LTS (Jammy Jellyfish)

CPU

AMD EPYC 9575F 64-Core Processor

GPU

AMD Instinct MI350

ROCm Version

ROCM 7.0.0

ROCm Component

No response

Steps to Reproduce

Reproduction Instructions

Repository:

https://github.com/ryanswann-amd/HipKittens/tree/persistent_gemm

git clone -b persistent_gemm [email protected]:ryanswann-amd/HipKittens.git

Files:

  • Non-persistent: kernels/gemm/bf16fp32/mi350x/256_256_64_32_with16x32.cpp
  • Persistent: kernels/gemm/bf16fp32/mi350x/256_256_64_32_with16x32_persistent.cpp

Build Instructions:

Option 1: Using Docker (Recommended)

First, launch the ROCm Docker container using the drun alias (or equivalent docker run command):

# Pull the MI350X/MI355X Docker image
docker pull docker.io/rocm/7.0-preview:rocm7.0_preview_pytorch_training_mi35x_beta

# Launch the container with drun alias (or use the full docker run command below)
drun rocm/7.0-preview:rocm7.0_preview_pytorch_training_mi35x_beta bash

# Alternatively, use the full docker run command:
docker run -it \
    --ipc=host \
    --network=host \
    --privileged \
    --cap-add=CAP_SYS_ADMIN \
    --cap-add=SYS_PTRACE \
    --security-opt seccomp=unconfined \
    --device=/dev/kfd \
    --device=/dev/dri \
    -v $(pwd):/workdir/ \
    -e USE_FASTSAFETENSOR=1 \
    -e SAFETENSORS_FAST_GPU=1 \
    rocm/7.0-preview:rocm7.0_preview_pytorch_training_mi35x_beta \
    bash

Once inside the container:

# Navigate to the HipKittens directory
cd /workdir/HipKittens  # or wherever you mounted the repo

# Set environment variables
source env.src

# Clone and install aiter (required for test scripts)
git clone --recursive https://github.com/ROCm/aiter.git
cd aiter
python3 setup.py develop
cd ..

# Navigate to the kernel directory
cd kernels/gemm/bf16fp32/mi350x

# Build both kernels
make all

Option 2: Direct Build (if already in ROCm environment)

cd kernels/gemm/bf16fp32/mi350x
make all

Run Instructions

After building the kernels, you can run them to verify the behavior and compare performance:

Running in Docker (Recommended)

# Inside the Docker container, after building with 'make all':

# Test the non-persistent kernel
python test_python.py

# Test the persistent kernel
python test_python_persistent.py

Both test scripts will:

  • Execute the respective kernel
  • Display performance metrics
  • Show the register usage differences highlighted in this issue

(Optional for Linux users) Output of /opt/rocm/bin/rocminfo --support

No response

Additional Information

No response

ryanswann-amd avatar Dec 08 '25 22:12 ryanswann-amd

The primary reason behind the additional register requirement is increased live-ins into the start of the outer loop in the persistent kernel. Looking at the debug output of the machine scheduler for the latest development compiler, the live-in VGPR pressure for the start of the outer loop is 79. There are 67 live-out VGPRs on the back-edge feeding this outer loop. The non-persistent kernel does not have this outer loop and is hence able to stay within occupancy=2 without spilling. For example, the setup code for the non-persistent kernel has a VGPR pressure of 7. The inner loop of the persistent kernel has a VGPR pressure of 271 vs 144 for the non-persistent kernel.

I would be curious to know whether you have a persistent version that uses 4 waves (instead of 8). While that would run with occupancy=1, it won't spill though you may lose some of the scheduling advantages of the current version. So, it may or may not improve performance over the current persistent version.

I tested with ROCm6.2.2, ROCm7.0, and the latest compiler. All of them have similar behavior.

dhruvachak avatar Dec 17 '25 00:12 dhruvachak

I would be curious to know whether you have a persistent version that uses 4 waves (instead of 8). While that would run with occupancy=1, it won't spill though you may lose some of the scheduling advantages of the current version. So, it may or may not improve performance over the current persistent version.

This should be fairly easy to create, however, I remember trying this and it also suffered the same problem where adding the persistent loop considerably increased the register pressure.

The explanation makes sense, however my question is what further optimizations are available in the compiler side to better capture this behavior? It seems to be such a common pattern in many of the critical applications. For e.g., even for the GEMM case you can further build out the kernel such that it starts pre-fetching the next tile at the end of the loop while performing the store for the existing accumulators, and since it is persistent, it will simply pull "work" (tiles) until the GEMM has completed. This can be a very effective optimizations for epilog bound GEMMs.

neoblizz avatar Dec 18 '25 04:12 neoblizz