[llvm] [AMDGPUAA] Check Type before Taking AddressSpace of a Value (PR #128116)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 20 19:14:38 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-amdgpu

Author: Chengjun (Chengjunp)

<details>
<summary>Changes</summary>

In current `AMDGPUAAResult::alias`, we directly take the AddressSpace of `LocA.Ptr->getType()`. It's unsafe as `LocA.Ptr->getType()` may be a non-pointer type. 

This patch adds a check to ensure we only take AddressSpace on a pointer value. If either of `LocA.Ptr` or `LocB.Ptr` is non-pointer, we return `AliasResult::MayAlias` which is the most conservative result.

---
Full diff: https://github.com/llvm/llvm-project/pull/128116.diff


1 Files Affected:

- (modified) llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp (+6-2) 


``````````diff
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp
index 5a6868f96d970..d60dc22f1d837 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUAliasAnalysis.cpp
@@ -49,8 +49,12 @@ void AMDGPUAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
 AliasResult AMDGPUAAResult::alias(const MemoryLocation &LocA,
                                   const MemoryLocation &LocB, AAQueryInfo &AAQI,
                                   const Instruction *) {
-  unsigned asA = LocA.Ptr->getType()->getPointerAddressSpace();
-  unsigned asB = LocB.Ptr->getType()->getPointerAddressSpace();
+  Type *TypeA = LocA.Ptr->getType();
+  Type *TypeB = LocB.Ptr->getType();
+  if (!TypeA->isPointerTy() || !TypeB->isPointerTy())
+    return AliasResult::MayAlias;
+  unsigned asA = TypeA->getPointerAddressSpace();
+  unsigned asB = TypeB->getPointerAddressSpace();
 
   if (!AMDGPU::addrspacesMayAlias(asA, asB))
     return AliasResult::NoAlias;

``````````

</details>


https://github.com/llvm/llvm-project/pull/128116


More information about the llvm-commits mailing list