[llvm] [SCEV] Use SCEVUse for FoldID. (PR #219813)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 30 08:24:21 PDT 2026
https://github.com/fhahn created https://github.com/llvm/llvm-project/pull/219813
As pointed out in https://github.com/llvm/llvm-project/pull/216761,
FoldID was not properly migrated to use SCEVUse. This caused incorrect
lookups, when an expression was cached with an operand with SCEVUse
flags and queried for the same expression without flags.
>From 1c0b68d1052d519a86a675cddfa101a54cf3d528 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 30 Aug 2026 16:13:59 +0100
Subject: [PATCH 1/2] Add test
---
.../Analysis/ScalarEvolutionTest.cpp | 44 +++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
index 41d792bd171b3..a50566cb64093 100644
--- a/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
+++ b/llvm/unittests/Analysis/ScalarEvolutionTest.cpp
@@ -2330,4 +2330,48 @@ TEST_F(ScalarEvolutionsTest, CastsOfUsesWithNoWrapFlags) {
CheckCast(SE.getAnyExtendExpr(NUWAdd, I64), SE.getAnyExtendExpr(Add, I64));
});
}
+
+TEST_F(ScalarEvolutionsTest, ExtendFoldCacheKeysUseFlags) {
+ LLVMContext C;
+ SMDiagnostic Err;
+ std::unique_ptr<Module> M = parseAssemblyString(
+ R"(define void @f(i32 %x) {
+ entry:
+ ret void
+ })",
+ Err, C);
+
+ if (!M) {
+ Err.print("ScalarEvolutionTest", errs());
+ ASSERT_TRUE(M && "Could not parse module?");
+ }
+ ASSERT_TRUE(!verifyModule(*M, &errs()) && "Must have been well formed!");
+
+ runWithSE(*M, "f", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
+ Type *I32 = Type::getInt32Ty(F.getContext());
+ Type *I64 = Type::getInt64Ty(F.getContext());
+
+ // Build (-2 * smax(smin(%x, 0), -50)), which is known to be in [0, 100],
+ // but cannot be proven to not wrap in an unsigned sense.
+ const SCEV *X = SE.getSCEV(getArgByName(F, "x"));
+ const SCEV *Bounded = SE.getSMaxExpr(SE.getSMinExpr(X, SE.getZero(I32)),
+ SE.getConstant(I32, -50, true));
+ SCEVUse Mul = SE.getMulExpr(SE.getConstant(I32, -2, true), Bounded);
+ ASSERT_TRUE(isa<SCEVMulExpr>(Mul));
+ ASSERT_FALSE(cast<SCEVMulExpr>(Mul.getPointer())->hasNoUnsignedWrap());
+ ASSERT_TRUE(SE.isKnownNonNegative(Mul));
+
+ SCEVUse MulNUW(Mul, SCEV::FlagNUW);
+ ASSERT_TRUE(MulNUW.hasUseFlags());
+
+ // For a known non-negative operand, getSignExtendExpr folds to a zero
+ // extend of the same operand use, and the fold gets cached. Make sure flags
+ // of SCEVUse operands are handled correctly.
+ const SCEV *SExtNUW = SE.getSignExtendExpr(MulNUW, I64);
+ const SCEV *SExtPlain = SE.getSignExtendExpr(Mul, I64);
+ EXPECT_NE(SExtNUW, SExtPlain);
+ EXPECT_EQ(cast<SCEVZeroExtendExpr>(SExtNUW)->getOperand(), MulNUW);
+ EXPECT_EQ(cast<SCEVZeroExtendExpr>(SExtPlain)->getOperand(), Mul);
+ });
+}
} // end namespace llvm
>From fe1655edbb6e3e62289a877de921dc517f87e5d2 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sun, 30 Aug 2026 16:21:22 +0100
Subject: [PATCH 2/2] [SCEV] Use SCEVUse for FoldID.
As pointed out in https://github.com/llvm/llvm-project/pull/216761,
FoldID was not properly migrated to use SCEVUse. This caused incorrect
lookups, when an expression was cached with an operand with SCEVUse
flags and queried for the same expression without flags.
---
llvm/include/llvm/Analysis/ScalarEvolution.h | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 95e545477fa77..385a8e3f0d3bb 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -1625,13 +1625,13 @@ class ScalarEvolution {
SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts);
class FoldID {
- const SCEV *Op = nullptr;
+ SCEVUse Op;
const Type *Ty = nullptr;
unsigned short C;
public:
- FoldID(SCEVTypes C, const SCEV *Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) {
- assert(Op);
+ FoldID(SCEVTypes C, SCEVUse Op, const Type *Ty) : Op(Op), Ty(Ty), C(C) {
+ assert(Op.getPointer());
assert(Ty);
}
@@ -1639,8 +1639,9 @@ class ScalarEvolution {
unsigned computeHash() const {
return detail::combineHashValue(
- C, detail::combineHashValue(reinterpret_cast<uintptr_t>(Op),
- reinterpret_cast<uintptr_t>(Ty)));
+ C, detail::combineHashValue(
+ reinterpret_cast<uintptr_t>(Op.getOpaqueValue()),
+ reinterpret_cast<uintptr_t>(Ty)));
}
bool operator==(const FoldID &RHS) const {
More information about the llvm-commits
mailing list