[llvm] [BasicAA] Gracefully handle large LocationSize (PR #138528)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon May 5 13:29:00 PDT 2025


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/138528

>From 36c3fcf9ceab716bf2e151e44c9954c147c5f9f3 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 5 May 2025 15:15:12 +0200
Subject: [PATCH 1/2] [BasicAA] Gracefully handle large LocationSize

If the LocationSize is larger than the index space of the pointer
type, bail out instead of triggering an APInt assertion.
---
 llvm/lib/Analysis/BasicAliasAnalysis.cpp    |  8 +++++---
 llvm/test/Analysis/BasicAA/size-overflow.ll | 14 ++++++++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Analysis/BasicAA/size-overflow.ll

diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index 2de9bb502baf4..30222b87ea467 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -1237,8 +1237,11 @@ AliasResult BasicAAResult::aliasGEP(
   if (V1Size.isScalable() || V2Size.isScalable())
     return AliasResult::MayAlias;
 
-  // We need to know both acess sizes for all the following heuristics.
-  if (!V1Size.hasValue() || !V2Size.hasValue())
+  // We need to know both access sizes for all the following heuristics. Don't
+  // try to reason about sizes larger than the index space.
+  unsigned BW = DecompGEP1.Offset.getBitWidth();
+  if (!V1Size.hasValue() || !V2Size.hasValue() ||
+      !isUIntN(BW, V1Size.getValue()) || !isUIntN(BW, V2Size.getValue()))
     return AliasResult::MayAlias;
 
   APInt GCD;
@@ -1293,7 +1296,6 @@ AliasResult BasicAAResult::aliasGEP(
 
   // Compute ranges of potentially accessed bytes for both accesses. If the
   // interseciton is empty, there can be no overlap.
-  unsigned BW = OffsetRange.getBitWidth();
   ConstantRange Range1 = OffsetRange.add(
       ConstantRange(APInt(BW, 0), APInt(BW, V1Size.getValue())));
   ConstantRange Range2 =
diff --git a/llvm/test/Analysis/BasicAA/size-overflow.ll b/llvm/test/Analysis/BasicAA/size-overflow.ll
new file mode 100644
index 0000000000000..18791ba20ef5f
--- /dev/null
+++ b/llvm/test/Analysis/BasicAA/size-overflow.ll
@@ -0,0 +1,14 @@
+; RUN: opt -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
+
+target datalayout = "p:32:32"
+
+; Make sure that using a LocationSize larget than the index space does not
+; assert.
+
+; Just Mod:  Ptr: i32* %gep	<->  call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)
+define void @test(ptr %p, i32 %idx) {
+  %gep = getelementptr i8, ptr %p, i32 %idx
+  load i32, ptr %gep
+  call void @llvm.memset.i64(ptr %p, i8 0, i64 u0x100000000, i1 false)
+  ret void
+}

>From caebfd4e09a99f1b0877723d9a97092aae3de10b Mon Sep 17 00:00:00 2001
From: Nikita Popov <nikita.ppv at gmail.com>
Date: Mon, 5 May 2025 22:28:42 +0200
Subject: [PATCH 2/2] fix test

---
 llvm/test/Analysis/BasicAA/size-overflow.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/Analysis/BasicAA/size-overflow.ll b/llvm/test/Analysis/BasicAA/size-overflow.ll
index 18791ba20ef5f..2a390d29e472a 100644
--- a/llvm/test/Analysis/BasicAA/size-overflow.ll
+++ b/llvm/test/Analysis/BasicAA/size-overflow.ll
@@ -1,11 +1,11 @@
-; RUN: opt -passes=aa-eval -print-all-alias-modref-info -disable-output 2>&1 | FileCheck %s
+; RUN: opt -passes=aa-eval -print-all-alias-modref-info -disable-output < %s 2>&1 | FileCheck %s
 
 target datalayout = "p:32:32"
 
 ; Make sure that using a LocationSize larget than the index space does not
 ; assert.
 
-; Just Mod:  Ptr: i32* %gep	<->  call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 68719476736, i1 false)
+; CHECK: Just Mod:  Ptr: i32* %gep	<->  call void @llvm.memset.p0.i64(ptr %p, i8 0, i64 4294967296, i1 false)
 define void @test(ptr %p, i32 %idx) {
   %gep = getelementptr i8, ptr %p, i32 %idx
   load i32, ptr %gep



More information about the llvm-commits mailing list