[llvm] [AMDGPU] Skip the iterative atomic scan on native LDS atomics (PR #227222)
Dmitry Sidorov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 1 11:55:56 PDT 2026
MrSidims wrote:
@yxsamliu @perlfu here are results of a toy benchmark on gfx90a:
TL;DR result:
| kernel | Iterative slope | Iterative intercept | patched slope | patched intercept | None slope | None intercept | DPP slope | DPP intercept |
|---|---|---|---|---|---|---|---|---|
| red_f32 | 41.6 | 35.4 | 2.3 | 4.8 | 2.3 | 4.8 | 0.0 | 104.0 |
| scan_f32 | 50.8 | 88.3 | 2.4 | 60.6 | 2.4 | 60.6 | 0.0 | 194.0 |
| red_i32 | 38.0 | 32.1 | 1.1 | 4.9 | 1.1 | 4.9 | 0.0 | 101.0 |
| scan_i32 | 45.2 | 89.9 | 1.2 | 57.3 | 1.2 | 57.3 | 0.0 | 157.0 |
So forms are linear in the number of active lanes, but the iterative scan's slope is 18-35x steeper, plus there is a fixed setup cost of 32-90us.
Since `i > s`, the difference `N*(i - s) + T` is positive for every N and there is no break-even point. The iterative scan already loses at a single active lane and loses progressively more towards a full wave.
On DPP as expected slope = 0, but it carries a bigger fixed cost.
Full:
| red_f32 | main (default) | patched (default) | DPP | None | Iter/None |
|---|---|---|---|---|---|
| 1 lane | 77 | 11 | 104 | 11 | 7.0x |
| 2 lanes | 119 | 11 | 104 | 11 | 10.8x |
| 4 lanes | 202 | 14 | 104 | 14 | 14.4x |
| 8 lanes | 368 | 21 | 104 | 21 | 17.5x |
| 16 lanes | 701 | 38 | 104 | 38 | 18.4x |
| 32 lanes | 1367 | 76 | 104 | 76 | 18.0x |
| 64 lanes | 2699 | 153 | 104 | 153 | 17.6x |
| scan_f32 | main (default) | patched (default) | DPP | None | Iter/None |
|---|---|---|---|---|---|
| 1 lane | 139 | 63 | 194 | 63 | 2.2x |
| 2 lanes | 190 | 65 | 194 | 65 | 2.9x |
| 4 lanes | 292 | 70 | 194 | 70 | 4.2x |
| 8 lanes | 495 | 80 | 194 | 80 | 6.2x |
| 16 lanes | 901 | 99 | 194 | 99 | 9.1x |
| 32 lanes | 1715 | 137 | 194 | 137 | 12.5x |
| 64 lanes | 3341 | 213 | 194 | 213 | 15.7x |
| red_i32 | main (default) | patched (default) | DPP | None | Iter/None |
|---|---|---|---|---|---|
| 1 lane | 70 | 9 | 101 | 9 | 7.8x |
| 2 lanes | 108 | 9 | 101 | 9 | 12.0x |
| 4 lanes | 184 | 9 | 101 | 9 | 20.4x |
| 8 lanes | 337 | 12 | 101 | 12 | 28.1x |
| 16 lanes | 641 | 19 | 101 | 19 | 33.7x |
| 32 lanes | 1250 | 38 | 101 | 38 | 32.9x |
| 64 lanes | 2467 | 76 | 101 | 76 | 32.5x |
| red_i32 | main (default) | patched (default) | DPP | None | Iter/None |
|---|---|---|---|---|---|
| 1 lane | 70 | 9 | 101 | 9 | 7.8x |
| 2 lanes | 108 | 9 | 101 | 9 | 12.0x |
| 4 lanes | 184 | 9 | 101 | 9 | 20.4x |
| 8 lanes | 337 | 12 | 101 | 12 | 28.1x |
| 16 lanes | 641 | 19 | 101 | 19 | 33.7x |
| 32 lanes | 1250 | 38 | 101 | 38 | 32.9x |
| 64 lanes | 2467 | 76 | 101 | 76 | 32.5x |
| scan_i32 | main (default) | patched (default) | DPP | None | Iter/None |
|---|---|---|---|---|---|
| 1 lane | 135 | 59 | 157 | 59 | 2.3x |
| 2 lanes | 180 | 59 | 157 | 59 | 3.1x |
| 4 lanes | 271 | 62 | 157 | 62 | 4.4x |
| 8 lanes | 451 | 67 | 157 | 67 | 6.7x |
| 16 lanes | 813 | 76 | 157 | 76 | 10.7x |
| 32 lanes | 1535 | 95 | 157 | 95 | 16.2x |
| 64 lanes | 2981 | 133 | 157 | 133 | 22.4x |
<details>
<summary>The benchmark (I'm not CUDA/HIP expert, so it's AI generated, but after few iteration it looks legit):</summary>
```
// Lane-count sweep for the AMDGPU atomic optimizer profitability heuristic.
//
// Measures a uniform-address, divergent-value atomicAdd as a function of the
// number of ACTIVE lanes in the wave, under each -amdgpu-atomic-optimizer-strategy.
//
// Cost model being calibrated (N = active lanes):
// cost(None) = L + N*s s = per-lane hardware serialization increment
// cost(Iter) = L + N*i + T i = per-lane scan slope, T = fixed tail
// L cancels, so the break-even is N* = T/(s-i), and if i > s there is none.
//
// ACTIVE is a *runtime* argument, not a template parameter: the exec mask is
// what matters, not compile-time knowledge of the lane count. This keeps the
// ISA byte-identical across all lane counts, so a sweep cannot be contaminated
// by per-ACTIVE codegen differences.
#include <hip/hip_runtime.h>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <string>
#define HIP_CHECK(x) \
do { \
hipError_t e = (x); \
if (e != hipSuccess) { \
fprintf(stderr, "HIP error %s at %s:%d\n", hipGetErrorString(e), \
__FILE__, __LINE__); \
exit(1); \
} \
} while (0)
// LDS == true -> atomic on a __shared__ scalar (addrspace(3))
// RES == true -> the return value is used (NeedResult)
template <typename T, bool LDS, bool RES>
__global__ void sweep(T *__restrict__ g, T *__restrict__ sink, int active,
int reps) {
__shared__ T s;
if (threadIdx.x == 0)
s = (T)0;
__syncthreads();
const int lane = threadIdx.x & 63; // wave64
T acc = (T)0;
for (int r = 0; r < reps; ++r) {
// Divergent value: differs per lane, and depends on r so it is not hoisted.
T v = (T)(threadIdx.x + r);
if (lane < active) { // exactly `active` bits set in exec
T old = LDS ? atomicAdd(&s, v) : atomicAdd(&g[0], v);
if (RES)
acc += old;
}
}
__syncthreads();
if (threadIdx.x == 0)
sink[blockIdx.x] = RES ? acc + s : s;
}
struct Arm {
const char *addrspace;
const char *type;
bool res;
};
// Returns {ms_per_rep, reps_used}. `reps` is auto-calibrated so every arm runs
// for ~targetMs, which keeps the cheap arms (1 block, 1 active lane) far above
// the ~5us launch-overhead floor while stopping the expensive arms (4*CU blocks,
// 64 active lanes) from taking seconds. Reporting per-rep time makes slopes
// directly comparable across arms.
template <typename T, bool LDS, bool RES>
static std::pair<double, int> timeKernel(T *g, T *sink, int blocks, int threads,
int active, double targetMs) {
hipEvent_t a, b;
HIP_CHECK(hipEventCreate(&a));
HIP_CHECK(hipEventCreate(&b));
auto once = [&](int reps) {
HIP_CHECK(hipEventRecord(a));
sweep<T, LDS, RES><<<blocks, threads>>>(g, sink, active, reps);
HIP_CHECK(hipEventRecord(b));
HIP_CHECK(hipEventSynchronize(b));
float ms;
HIP_CHECK(hipEventElapsedTime(&ms, a, b));
return (double)ms;
};
// Calibrate: grow reps until the kernel runs long enough to measure.
int reps = 16;
for (int i = 0; i < 24; ++i) {
double ms = once(reps);
if (ms >= targetMs || reps >= (1 << 22))
break;
double scale = (ms > 1e-3) ? (targetMs / ms) : 8.0;
scale = std::min(scale, 8.0); // grow gradually; avoid overshoot
int next = (int)(reps * scale) + 1;
reps = std::max(next, reps + 1);
}
// Warmup at the final rep count.
for (int i = 0; i < 3; ++i)
once(reps);
// Report the MINIMUM over 20 launches: means are contaminated by DVFS and
// any other tenant on the device.
double best = 1e30;
for (int i = 0; i < 20; ++i)
best = std::min(best, once(reps));
HIP_CHECK(hipGetLastError());
HIP_CHECK(hipEventDestroy(a));
HIP_CHECK(hipEventDestroy(b));
return {best / reps, reps};
}
int main(int argc, char **argv) {
// argv[1] = strategy label (for the CSV only; the actual strategy is baked
// in at compile time via -mllvm -amdgpu-atomic-optimizer-strategy=)
const char *strategy = argc > 1 ? argv[1] : "unknown";
const double targetMs = argc > 2 ? atof(argv[2]) : 2.0;
hipDeviceProp_t prop;
HIP_CHECK(hipGetDeviceProperties(&prop, 0));
const int cu = prop.multiProcessorCount;
float *gf, *sf;
int *gi, *si;
HIP_CHECK(hipMalloc(&gf, sizeof(float) * 4));
HIP_CHECK(hipMalloc(&gi, sizeof(int) * 4));
HIP_CHECK(hipMalloc(&sf, sizeof(float) * 4096));
HIP_CHECK(hipMalloc(&si, sizeof(int) * 4096));
HIP_CHECK(hipMemset(gf, 0, sizeof(float) * 4));
HIP_CHECK(hipMemset(gi, 0, sizeof(int) * 4));
const int actives[] = {1, 2, 4, 6, 8, 12, 16, 24, 32, 48, 64};
const int threadsList[] = {64, 256};
const int blocksList[] = {1, cu, 4 * cu};
printf("strategy,addrspace,type,needresult,blocks,threads,active,reps,ms_per_rep\n");
for (int bi = 0; bi < 3; ++bi) {
for (int ti = 0; ti < 2; ++ti) {
const int blocks = blocksList[bi];
const int threads = threadsList[ti];
for (int ai = 0; ai < (int)(sizeof(actives) / sizeof(actives[0])); ++ai) {
const int N = actives[ai];
#define RUN(TY, PTR, SINK, LDS, RES, ASNAME, TYNAME, RESNAME) \
do { \
auto r = timeKernel<TY, LDS, RES>(PTR, SINK, blocks, threads, N, targetMs);\
printf("%s,%s,%s,%s,%d,%d,%d,%d,%.9f\n", strategy, ASNAME, TYNAME, \
RESNAME, blocks, threads, N, r.second, r.first); \
fflush(stdout); \
} while (0);
RUN(float, gf, sf, true, false, "lds", "f32", "nores")
RUN(float, gf, sf, true, true, "lds", "f32", "res")
RUN(float, gf, sf, false, false, "global", "f32", "nores")
RUN(float, gf, sf, false, true, "global", "f32", "res")
RUN(int, gi, si, true, false, "lds", "i32", "nores")
RUN(int, gi, si, true, true, "lds", "i32", "res")
RUN(int, gi, si, false, false, "global", "i32", "nores")
RUN(int, gi, si, false, true, "global", "i32", "res")
#undef RUN
}
}
}
HIP_CHECK(hipFree(gf));
HIP_CHECK(hipFree(gi));
HIP_CHECK(hipFree(sf));
HIP_CHECK(hipFree(si));
return 0;
}
```
</details>
https://github.com/llvm/llvm-project/pull/227222
More information about the llvm-commits
mailing list