llvm-project/llvm/test/CodeGen/AMDGPU/attributor-loop-issue-58639.ll
Shilei Tian 578741b5e8
[AMDGPU][Attributor] Rework update of AAAMDWavesPerEU (#123995)
Currently, we use `AAAMDWavesPerEU` to iteratively update values based
on attributes from the associated function, potentially propagating
user-annotated values, along with `AAAMDFlatWorkGroupSize`. Similarly,
we have `AAAMDFlatWorkGroupSize`. However, since the value calculated
through the flat workgroup size always dominates the user annotation
(i.e., the attribute), running `AAAMDWavesPerEU` iteratively is
unnecessary if no user-annotated value exists.

This PR completely rewrites how the `amdgpu-waves-per-eu` attribute is
handled in `AMDGPUAttributor`. The key changes are as follows:

- `AAAMDFlatWorkGroupSize` remains unchanged.
- `AAAMDWavesPerEU` now only propagates user-annotated values.
- A new function is added to check and update `amdgpu-waves-per-eu`
based on the following rules:
- No waves per eu, no flat workgroup size: Assume a flat workgroup size
of `1,1024` and compute waves per eu based on this.
- No waves per eu, flat workgroup size exists: Use the provided flat
workgroup size to compute waves-per-eu.
- Waves per eu exists, no flat workgroup size: This is a tricky case. In
this PR, we assume a flat workgroup size of `1,1024`, but this can be
adjusted if a different approach is preferred. Alternatively, we could
directly use the user-annotated value.
- Both waves per eu and flat workgroup size exist: If there’s a
conflict, the value derived from the flat workgroup size takes
precedence over waves per eu.

This PR also updates the logic for merging two waves per eu pairs. The
current implementation, which uses `clampStateAndIndicateChange` to
compute a union, might not be ideal. If we think from ensure proper
resource allocation perspective, for instance, if one pair specifies a
minimum of 2 waves per eu, and another specifies a minimum of 4, we
should guarantee that 4 waves per eu can be supported, as failing to do
so could result in excessive resource allocation per wave. A similar
principle applies to the upper bound. Thus, the PR uses the following
approach for merging two pairs, `lo_a,up_a` and `lo_b,up_b`: `max(lo_a,
lo_b), max(up_a, up_b)`. This ensures that resource allocation adheres
to the stricter constraints from both inputs.

Fix #123092.
2025-05-17 01:01:09 -04:00

68 lines
2.4 KiB
LLVM

; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature --check-globals
; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor %s | FileCheck %s
%0 = type { ptr, ptr }
define internal fastcc i1 @widget(ptr %arg) {
; CHECK-LABEL: define {{[^@]+}}@widget
; CHECK-SAME: (ptr [[ARG:%.*]]) #[[ATTR0:[0-9]+]] {
; CHECK-NEXT: bb:
; CHECK-NEXT: [[TMP:%.*]] = getelementptr inbounds [[TMP0:%.*]], ptr [[ARG]], i64 0, i32 1
; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[TMP]], align 8
; CHECK-NEXT: [[TMP2:%.*]] = call fastcc double @baz(ptr [[TMP1]])
; CHECK-NEXT: ret i1 false
;
bb:
%tmp = getelementptr inbounds %0, ptr %arg, i64 0, i32 1
%tmp1 = load ptr, ptr %tmp, align 8
%tmp2 = call fastcc double @baz(ptr %tmp1)
ret i1 false
}
define internal fastcc double @baz(ptr %arg) {
; CHECK-LABEL: define {{[^@]+}}@baz
; CHECK-SAME: (ptr [[ARG:%.*]]) #[[ATTR0]] {
; CHECK-NEXT: bb:
; CHECK-NEXT: [[TMP1:%.*]] = load ptr, ptr [[ARG]], align 8
; CHECK-NEXT: [[TMP2:%.*]] = tail call double [[TMP1]]()
; CHECK-NEXT: br label [[BB3:%.*]]
; CHECK: bb3:
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds [[TMP0:%.*]], ptr [[ARG]], i64 0, i32 1
; CHECK-NEXT: br label [[BB5:%.*]]
; CHECK: bb5:
; CHECK-NEXT: [[TMP6:%.*]] = load ptr, ptr [[TMP4]], align 8
; CHECK-NEXT: [[TMP7:%.*]] = call fastcc i1 @widget(ptr [[TMP6]])
; CHECK-NEXT: br label [[BB5]]
;
bb:
%tmp1 = load ptr, ptr %arg, align 8
%tmp2 = tail call double %tmp1()
br label %bb3
bb3: ; preds = %bb
%tmp4 = getelementptr inbounds %0, ptr %arg, i64 0, i32 1
br label %bb5
bb5: ; preds = %bb5, %bb3
%tmp6 = load ptr, ptr %tmp4, align 8
%tmp7 = call fastcc i1 @widget(ptr %tmp6)
br label %bb5
}
define amdgpu_kernel void @entry() {
; CHECK-LABEL: define {{[^@]+}}@entry
; CHECK-SAME: () #[[ATTR0]] {
; CHECK-NEXT: [[ALLOCA:%.*]] = alloca [[TMP0:%.*]], align 8, addrspace(5)
; CHECK-NEXT: [[CAST:%.*]] = addrspacecast ptr addrspace(5) [[ALLOCA]] to ptr
; CHECK-NEXT: [[ARST:%.*]] = call double @baz(ptr [[CAST]])
; CHECK-NEXT: ret void
;
%alloca = alloca %0, align 8, addrspace(5)
%cast = addrspacecast ptr addrspace(5) %alloca to ptr
%arst = call double @baz(ptr %cast)
ret void
}
;.
; CHECK: attributes #[[ATTR0]] = { "amdgpu-waves-per-eu"="4,10" "uniform-work-group-size"="false" }
;.