From d057782c04219c4c4d0c3df1dfc39b066e382d22 Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Thu, 9 Jan 2025 16:24:24 +0000 Subject: [PATCH 1/4] [AMDGPU] Fix spurious NoAlias results After a30e50fc, AMDGPUAAResult is being called in more situations where BasicAA isn't sure. This exposed some regressions where NoAlias is being incorrectly returned for two identical pointers. The fix is to check the underlying objects for equality before returning NoAlias. --- llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp | 7 +++++-- .../CodeGen/AMDGPU/amdgpu-alias-analysis.ll | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp index 8d3eac6868318..c2177c18f3cc3 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp @@ -80,10 +80,13 @@ AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, } else if (const Argument *Arg = dyn_cast(ObjA)) { const Function *F = Arg->getParent(); switch (F->getCallingConv()) { - case CallingConv::AMDGPU_KERNEL: + case CallingConv::AMDGPU_KERNEL: { // In the kernel function, kernel arguments won't alias to (local) // variables in shared or private address space. - return AliasResult::NoAlias; + const auto *ObjB = + getUnderlyingObject(B.Ptr->stripPointerCastsForAliasAnalysis()); + return (ObjA == ObjB) ? AliasResult::MustAlias : AliasResult::NoAlias; + } default: // TODO: In the regular function, if that local variable in the // location B is not captured, that argument pointer won't alias to it diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll index a13eb5c6d085f..851c18dfccf5a 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll @@ -318,3 +318,20 @@ define void @test_9_9(ptr addrspace(9) %p, ptr addrspace(9) %p1) { load i8, ptr addrspace(9) %p1 ret void } + +; CHECK-LABEL: Function: test_kernel_arg_local_ptr +; CHECK: MayAlias: i32 addrspace(3)* %arg, i32 addrspace(3)* %arg1 +; CHECK: MustAlias: i32 addrspace(3)* %arg, i32* %arg2 +; CHECK: MustAlias: i32 addrspace(3)* %arg1, i32* %arg2 +define amdgpu_kernel void @test_kernel_arg_local_ptr(ptr addrspace(3) noundef align 4 %arg) { +entry: + %load1 = load i32, ptr addrspace(3) %arg, align 4 + %arg.plus.1 = getelementptr inbounds nuw i8, ptr addrspace(3) %arg, i64 1 + %arg1 = getelementptr inbounds nuw i8, ptr addrspace(3) %arg.plus.1, i64 -1 + %load2 = load i32, ptr addrspace(3) %arg1, align 4 + %arg.plus.4 = getelementptr inbounds nuw i8, ptr addrspace(3) %arg, i64 4 + %acast = addrspacecast ptr addrspace(3) %arg.plus.4 to ptr + %arg2 = getelementptr inbounds i8, ptr %acast, i64 -4 + %load3 = load i32, ptr %arg2, align 4 + ret void +} From a6e37b2b53b0f23e06bf910ea534cedeffcf2afb Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Thu, 9 Jan 2025 16:33:41 +0000 Subject: [PATCH 2/4] remove attributes --- llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll index 851c18dfccf5a..0c9fcbd7ac929 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll @@ -323,7 +323,7 @@ define void @test_9_9(ptr addrspace(9) %p, ptr addrspace(9) %p1) { ; CHECK: MayAlias: i32 addrspace(3)* %arg, i32 addrspace(3)* %arg1 ; CHECK: MustAlias: i32 addrspace(3)* %arg, i32* %arg2 ; CHECK: MustAlias: i32 addrspace(3)* %arg1, i32* %arg2 -define amdgpu_kernel void @test_kernel_arg_local_ptr(ptr addrspace(3) noundef align 4 %arg) { +define amdgpu_kernel void @test_kernel_arg_local_ptr(ptr addrspace(3) %arg) { entry: %load1 = load i32, ptr addrspace(3) %arg, align 4 %arg.plus.1 = getelementptr inbounds nuw i8, ptr addrspace(3) %arg, i64 1 From 57b244824e5e19d66acdd0b26826a7f662e9e1fc Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Mon, 13 Jan 2025 10:25:56 +0000 Subject: [PATCH 3/4] used identifiedobject --- llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp | 3 ++- llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp index c2177c18f3cc3..01e230e7d079c 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp @@ -85,7 +85,8 @@ AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, // variables in shared or private address space. const auto *ObjB = getUnderlyingObject(B.Ptr->stripPointerCastsForAliasAnalysis()); - return (ObjA == ObjB) ? AliasResult::MustAlias : AliasResult::NoAlias; + return isIdentifiedObject(ObjB) && ObjA != ObjB ? AliasResult::NoAlias + : AliasResult::MayAlias; } default: // TODO: In the regular function, if that local variable in the diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll index 0c9fcbd7ac929..6b935a8768d3d 100644 --- a/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll +++ b/llvm/test/CodeGen/AMDGPU/amdgpu-alias-analysis.ll @@ -320,9 +320,9 @@ define void @test_9_9(ptr addrspace(9) %p, ptr addrspace(9) %p1) { } ; CHECK-LABEL: Function: test_kernel_arg_local_ptr -; CHECK: MayAlias: i32 addrspace(3)* %arg, i32 addrspace(3)* %arg1 -; CHECK: MustAlias: i32 addrspace(3)* %arg, i32* %arg2 -; CHECK: MustAlias: i32 addrspace(3)* %arg1, i32* %arg2 +; CHECK: MayAlias: i32 addrspace(3)* %arg, i32 addrspace(3)* %arg1 +; CHECK: MayAlias: i32 addrspace(3)* %arg, i32* %arg2 +; CHECK: MayAlias: i32 addrspace(3)* %arg1, i32* %arg2 define amdgpu_kernel void @test_kernel_arg_local_ptr(ptr addrspace(3) %arg) { entry: %load1 = load i32, ptr addrspace(3) %arg, align 4 From e18b9033d08ce30997f7a1049ed263f94428049b Mon Sep 17 00:00:00 2001 From: Fraser Cormack Date: Mon, 13 Jan 2025 11:43:48 +0000 Subject: [PATCH 4/4] swap checks --- llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp index 01e230e7d079c..5a6868f96d970 100644 --- a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp +++ b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp @@ -85,7 +85,7 @@ AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA, // variables in shared or private address space. const auto *ObjB = getUnderlyingObject(B.Ptr->stripPointerCastsForAliasAnalysis()); - return isIdentifiedObject(ObjB) && ObjA != ObjB ? AliasResult::NoAlias + return ObjA != ObjB && isIdentifiedObject(ObjB) ? AliasResult::NoAlias : AliasResult::MayAlias; } default: