[llvm] [BasicAA] Fix assertion failure in alias() caused by non-pointer base in DecomposeGEPExpression (PR #191180)
Kacper Doga via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 9 05:55:45 PDT 2026
https://github.com/varev-dev updated https://github.com/llvm/llvm-project/pull/191180
>From 8112ce8ba17b7e5822a603f793b23151b530f052 Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Thu, 9 Apr 2026 13:59:30 +0200
Subject: [PATCH 1/2] [BasicAA] Fix assertion failure in alias() caused by
non-pointer base in DecomposeGEPExpression
When stripping a bitcast in DecomposeGEPExpression, the resulting operand may
have a non-scalar-pointer type (e.g. <1 x ptr>). Proceeding with such a type
as the decomposition base violates the AA assumption that all pointers are
scalar pointer types, triggering an assertion failure on alias() call.
Add a type check in the bitcast/addrspacecast handling path to return not
stripped V as base when the stripped operand is not a scalar pointer type.
Add a lit test verifying no crash on valid IR containing such a bitcast, and
checking that the alias query conservatively returns MayAlias.
Fixes #191157
---
llvm/lib/Analysis/BasicAliasAnalysis.cpp | 6 ++++++
.../gep-decompose-bitcast-vector-type-mismatch.ll | 11 +++++++++++
2 files changed, 17 insertions(+)
create mode 100644 llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
diff --git a/llvm/lib/Analysis/BasicAliasAnalysis.cpp b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
index dee305a6f40d2..3ba7c26f66d01 100644
--- a/llvm/lib/Analysis/BasicAliasAnalysis.cpp
+++ b/llvm/lib/Analysis/BasicAliasAnalysis.cpp
@@ -624,6 +624,12 @@ BasicAAResult::DecomposeGEPExpression(const Value *V, const DataLayout &DL,
if (Op->getOpcode() == Instruction::BitCast ||
Op->getOpcode() == Instruction::AddrSpaceCast) {
Value *NewV = Op->getOperand(0);
+ // Bitcast may produce a non-scalar-pointer operand (e.g. <1 x ptr>);
+ // AA requires scalar pointer types as base.
+ if (!NewV->getType()->isPointerTy()) {
+ Decomposed.Base = V;
+ return Decomposed;
+ }
// Don't look through casts between address spaces with differing index
// widths.
if (DL.getIndexTypeSizeInBits(NewV->getType()) != IndexSize) {
diff --git a/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll b/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
new file mode 100644
index 0000000000000..4ae72597d2cae
--- /dev/null
+++ b/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
@@ -0,0 +1,11 @@
+; RUN: opt -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output < %s 2>&1 | FileCheck %s
+
+; GEPExpression decomposition strips bitcast, resulting in type change for bitcast from ptr to operand type <1 x ptr>.
+define void @test(ptr addrspace(1) %C, i64 %0) {
+; CHECK-LABEL: test
+; CHECK: MayAlias: i32 addrspace(1)* %2, <2 x i16>* bitcast (<1 x ptr> <ptr inttoptr (i64 -1 to ptr)> to ptr)
+ %2 = getelementptr i8, ptr addrspace(1) %C, i64 %0
+ store i32 0, ptr addrspace(1) %2, align 4
+ store <2 x i16> zeroinitializer, ptr bitcast (<1 x ptr> <ptr inttoptr (i64 -1 to ptr)> to ptr), align 4
+ ret void
+}
>From 1ab4d8ac181f23b5e44cbe40d8aad9142c74d36b Mon Sep 17 00:00:00 2001
From: "Doga, Kacper" <kacper.doga at intel.com>
Date: Thu, 9 Apr 2026 14:51:28 +0200
Subject: [PATCH 2/2] clarify lit scenario
---
.../BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll b/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
index 4ae72597d2cae..cc2749e82b2e5 100644
--- a/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
+++ b/llvm/test/Analysis/BasicAA/gep-decompose-bitcast-vector-type-mismatch.ll
@@ -1,6 +1,9 @@
; RUN: opt -aa-pipeline=basic-aa -passes=aa-eval -print-all-alias-modref-info -disable-output < %s 2>&1 | FileCheck %s
-; GEPExpression decomposition strips bitcast, resulting in type change for bitcast from ptr to operand type <1 x ptr>.
+; Verify that BasicAA does not crash when DecomposeGEPExpression encounters
+; a bitcast whose stripped operand has a non-pointer type (e.g. <1 x ptr>).
+; The alias query should conservatively return MayAlias instead of hitting
+; an assertion in alias() which requires scalar pointer types.
define void @test(ptr addrspace(1) %C, i64 %0) {
; CHECK-LABEL: test
; CHECK: MayAlias: i32 addrspace(1)* %2, <2 x i16>* bitcast (<1 x ptr> <ptr inttoptr (i64 -1 to ptr)> to ptr)
More information about the llvm-commits
mailing list