[llvm] [AggressiveInstCombine] Require a definitive initializer for table folds (PR #222826)
Chris Kennelly via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 19:08:40 PDT 2026
https://github.com/ckennelly created https://github.com/llvm/llvm-project/pull/222826
tryToRecognizeTableBasedCttzOrLog2 accepted any global that isConstant() and hasInitializer(), but isConstant() only means the value does not change at run time -- it does not mean the initializer that is read is the one that gets linked. An interposable (weak/linkonce) constant, or an externally_initialized constant, can be replaced with a different constant table at link or load time, so folding tbl[i] against the observed initializer is unsound for those.
Assisted-by: Claude Code
>From ddab2cd4290ee88587d3783484673fa218ed0f8c Mon Sep 17 00:00:00 2001
From: Chris Kennelly <ckennelly at ckennelly.com>
Date: Thu, 10 Sep 2026 23:29:43 +0000
Subject: [PATCH] [AggressiveInstCombine] Require a definitive initializer for
table folds
tryToRecognizeTableBasedCttzOrLog2 accepted any global that isConstant()
and hasInitializer(), but isConstant() only means the value does not
change at run time -- it does not mean the initializer that is read is
the one that gets linked. An interposable (weak/linkonce) constant, or
an externally_initialized constant, can be replaced with a different
constant table at link or load time, so folding tbl[i] against the
observed initializer is unsound for those.
Assisted-by: Claude Code
---
.../AggressiveInstCombine.cpp | 3 ++-
.../negative-lower-table-based-cttz.ll | 17 +++++++++++++++++
2 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
index 9b0ed2bd0c54d..9646b2785b9f1 100644
--- a/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
+++ b/llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
@@ -1250,7 +1250,8 @@ static bool tryToRecognizeTableBasedCttzOrLog2(Instruction &I,
return false;
GlobalVariable *GVTable = dyn_cast<GlobalVariable>(GEP->getPointerOperand());
- if (!GVTable || !GVTable->hasInitializer() || !GVTable->isConstant())
+ if (!GVTable || !GVTable->isConstant() ||
+ !GVTable->hasDefinitiveInitializer())
return false;
unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
diff --git a/llvm/test/Transforms/AggressiveInstCombine/negative-lower-table-based-cttz.ll b/llvm/test/Transforms/AggressiveInstCombine/negative-lower-table-based-cttz.ll
index 90836db80603b..4a417d1288348 100644
--- a/llvm/test/Transforms/AggressiveInstCombine/negative-lower-table-based-cttz.ll
+++ b/llvm/test/Transforms/AggressiveInstCombine/negative-lower-table-based-cttz.ll
@@ -136,3 +136,20 @@ entry:
%0 = load i32, ptr %arrayidx, align 4
ret i32 %0
}
+
+;; Negative: an interposable (weak) constant table may be replaced at link time
+;; with a different constant, so its observed initializer cannot be trusted.
+
+ at ctz_weak.table = weak unnamed_addr constant [32 x i32] [i32 0, i32 1, i32 2, i32 24, i32 3, i32 19, i32 6, i32 25, i32 22, i32 4, i32 20, i32 10, i32 16, i32 7, i32 12, i32 26, i32 31, i32 23, i32 18, i32 5, i32 21, i32 9, i32 15, i32 11, i32 30, i32 17, i32 8, i32 14, i32 29, i32 13, i32 28, i32 27], align 4
+
+define i32 @ctz_weak(i32 %x) {
+entry:
+ %sub = sub i32 0, %x
+ %and = and i32 %sub, %x
+ %mul = mul i32 %and, 81224991
+ %shr = lshr i32 %mul, 27
+ %idxprom = zext i32 %shr to i64
+ %arrayidx = getelementptr inbounds [32 x i32], ptr @ctz_weak.table, i64 0, i64 %idxprom
+ %0 = load i32, ptr %arrayidx, align 4
+ ret i32 %0
+}
More information about the llvm-commits
mailing list