Skip to content

[SCEV] Return nullopt from CompareValueComplexity() if depth limit reached #101022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,14 @@ void SCEVUnknown::allUsesReplacedWith(Value *New) {
///
/// Since we do not continue running this routine on expression trees once we
/// have seen unequal values, there is no need to track them in the cache.
static int
static std::optional<int>
CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
const LoopInfo *const LI, Value *LV, Value *RV,
unsigned Depth) {
if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV))
if (Depth > MaxValueCompareDepth)
return std::nullopt;

if (EqCacheValue.isEquivalent(LV, RV))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the bug with inconsistent results, but did you check if the cache actually does anything useful in the average case? In the (admittedly limited) examples I looked at, it was almost pure overhead (i.e., almost no hits, but lots of writes and checks).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that we should also drop the cache, see #100721 (comment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, missed that. SGTM.

return 0;

// Order pointer values after integer values. This helps SCEVExpander form
Expand Down Expand Up @@ -660,7 +663,7 @@ CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
return (int)LNumOps - (int)RNumOps;

for (unsigned Idx : seq(LNumOps)) {
int Result =
std::optional<int> Result =
CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
RInst->getOperand(Idx), Depth + 1);
if (Result != 0)
Expand Down Expand Up @@ -705,8 +708,8 @@ CompareSCEVComplexity(EquivalenceClasses<const SCEV *> &EqCacheSCEV,
const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);

int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(),
RU->getValue(), Depth + 1);
std::optional<int> X = CompareValueComplexity(
EqCacheValue, LI, LU->getValue(), RU->getValue(), Depth + 1);
if (X == 0)
EqCacheSCEV.unionSets(LHS, RHS);
return X;
Expand Down
36 changes: 36 additions & 0 deletions llvm/unittests/Analysis/ScalarEvolutionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1625,4 +1625,40 @@ TEST_F(ScalarEvolutionsTest, ForgetValueWithOverflowInst) {
});
}

TEST_F(ScalarEvolutionsTest, ComplexityComparatorIsStrictWeakOrdering) {
// Regression test for a case where caching of equivalent values caused the
// comparator to get inconsistent.
LLVMContext C;
SMDiagnostic Err;
std::unique_ptr<Module> M = parseAssemblyString(R"(
define i32 @foo(i32 %arg0) {
%1 = add i32 %arg0, 1
%2 = add i32 %arg0, 1
%3 = xor i32 %2, %1
%4 = add i32 %3, %2
%5 = add i32 %arg0, 1
%6 = xor i32 %5, %arg0
%7 = add i32 %arg0, %6
%8 = add i32 %5, %7
%9 = xor i32 %8, %7
%10 = add i32 %9, %8
%11 = xor i32 %10, %9
%12 = add i32 %11, %10
%13 = xor i32 %12, %11
%14 = add i32 %12, %13
%15 = add i32 %14, %4
ret i32 %15
})",
Err, C);

ASSERT_TRUE(M && "Could not parse module?");
ASSERT_TRUE(!verifyModule(*M) && "Must have been well formed!");

runWithSE(*M, "foo", [](Function &F, LoopInfo &LI, ScalarEvolution &SE) {
// When _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG, this will
// crash if the comparator has the specific caching bug.
SE.getSCEV(F.getEntryBlock().getTerminator()->getOperand(0));
});
}

} // end namespace llvm
4 changes: 2 additions & 2 deletions polly/test/ScopInfo/invariant_load_zext_parameter-2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
; CHECK: p0: ((sext i32 %tmp6 to i64) * %p1)
; CHECK: p1: ((sext i32 %tmp3 to i64) * (sext i32 %tmp8 to i64) * (%p0 + %p1) * %p3)
; CHECK: p2: ((sext i32 %tmp3 to i64) * (%p0 + %p1))
; CHECK: p3: ((sext i32 %tmp5 to i64) * %p0)
; CHECK: p4: ((sext i32 %tmp7 to i64) * %p2)
; CHECK: p3: ((sext i32 %tmp7 to i64) * %p2)
; CHECK: p4: ((sext i32 %tmp5 to i64) * %p0)
;
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

Expand Down
Loading