Skip to content

Commit 21cea3f

Browse files
authored
AMDGPU: Stop promoting allocas with addrspacecast users (#104051)
We cannot promote this case unless we know the value is only observed through flat operations. We cannot analyze this through a call. PointerMayBeCaptured was an imprecise check for this. A callee with a nocapture attribute may still cast to private and observe the address space, so really we need a different notion of nocapture. I doubt this was of any use anyway. The promotable cases should have optimized out addrspacecast to begin earlier. Fixes #66669 Fixes #104035
1 parent bdf4b9a commit 21cea3f

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

llvm/lib/Target/AMDGPU/AMDGPUPromoteAlloca.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,14 +1195,10 @@ bool AMDGPUPromoteAllocaImpl::collectUsesWithPtrTypes(
11951195
WorkList.push_back(ICmp);
11961196
}
11971197

1198-
if (UseInst->getOpcode() == Instruction::AddrSpaceCast) {
1199-
// Give up if the pointer may be captured.
1200-
if (PointerMayBeCaptured(UseInst, true, true))
1201-
return false;
1202-
// Don't collect the users of this.
1203-
WorkList.push_back(User);
1204-
continue;
1205-
}
1198+
// TODO: If we know the address is only observed through flat pointers, we
1199+
// could still promote.
1200+
if (UseInst->getOpcode() == Instruction::AddrSpaceCast)
1201+
return false;
12061202

12071203
// Do not promote vector/aggregate type instructions. It is hard to track
12081204
// their users.

llvm/test/CodeGen/AMDGPU/promote-alloca-addrspacecast.ll

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
; The types of the users of the addrspacecast should not be changed.
44

55
; CHECK-LABEL: @invalid_bitcast_addrspace(
6-
; CHECK: [[GEP:%[0-9]+]] = getelementptr inbounds [256 x [1 x i32]], ptr addrspace(3) @invalid_bitcast_addrspace.data, i32 0, i32 %{{[0-9]+}}
7-
; CHECK: [[ASC:%[a-z0-9]+]] = addrspacecast ptr addrspace(3) [[GEP]] to ptr
8-
; CHECK: [[LOAD:%[a-z0-9]+]] = load <2 x i16>, ptr [[ASC]]
9-
; CHECK: bitcast <2 x i16> [[LOAD]] to <2 x half>
6+
; CHECK: alloca
7+
; CHECK: addrspacecast
8+
; CHECK: load
9+
; CHECK: bitcast
1010
define amdgpu_kernel void @invalid_bitcast_addrspace() #0 {
1111
entry:
1212
%data = alloca [1 x i32], addrspace(5)
@@ -16,4 +16,22 @@ entry:
1616
ret void
1717
}
1818

19+
; A callee use is not promotable even if it has a nocapture attribute.
20+
define void @nocapture_callee(ptr nocapture noundef writeonly %flat.observes.addrspace) #0 {
21+
%private.ptr = addrspacecast ptr %flat.observes.addrspace to ptr addrspace(5)
22+
store i32 1, ptr addrspace(5) %private.ptr, align 4
23+
ret void
24+
}
25+
26+
; CHECK-LABEL: @kernel_call_nocapture(
27+
; CHECK: alloca i32
28+
; CHECK-NEXT: addrspacecast
29+
; CHECK-NEXT: call
30+
define amdgpu_kernel void @kernel_call_nocapture() #0 {
31+
%alloca = alloca i32, align 4, addrspace(5)
32+
%flat.alloca = addrspacecast ptr addrspace(5) %alloca to ptr
33+
call void @nocapture_callee(ptr noundef %flat.alloca)
34+
ret void
35+
}
36+
1937
attributes #0 = { nounwind "amdgpu-flat-work-group-size"="1,256" }

0 commit comments

Comments
 (0)