Skip to content

Conversation

@llvmbot
Copy link
Member

@llvmbot llvmbot commented Sep 16, 2025

Backport 7af659d

Requested by: @nikic

@llvmbot
Copy link
Member Author

llvmbot commented Sep 16, 2025

@fhahn What do you think about merging this PR to the release branch?

@llvmbot llvmbot requested a review from fhahn September 16, 2025 07:30
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Sep 16, 2025
@llvmbot
Copy link
Member Author

llvmbot commented Sep 16, 2025

@llvm/pr-subscribers-llvm-analysis

Author: None (llvmbot)

Changes

Backport 7af659d

Requested by: @nikic


Full diff: https://github.com/llvm/llvm-project/pull/158798.diff

1 Files Affected:

  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+7-2)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 24adfa346c642..221468a2d1a84 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15093,15 +15093,20 @@ void SCEVUnionPredicate::add(const SCEVPredicate *N, ScalarEvolution &SE) {
     return;
   }
 
+  // Implication checks are quadratic in the number of predicates. Stop doing
+  // them if there are many predicates, as they should be too expensive to use
+  // anyway at that point.
+  bool CheckImplies = Preds.size() < 16;
+
   // Only add predicate if it is not already implied by this union predicate.
-  if (implies(N, SE))
+  if (CheckImplies && implies(N, SE))
     return;
 
   // Build a new vector containing the current predicates, except the ones that
   // are implied by the new predicate N.
   SmallVector<const SCEVPredicate *> PrunedPreds;
   for (auto *P : Preds) {
-    if (N->implies(P, SE))
+    if (CheckImplies && N->implies(P, SE))
       continue;
     PrunedPreds.push_back(P);
   }

@c-rhodes c-rhodes moved this from Needs Triage to Needs Review in LLVM Release Status Sep 17, 2025
@tstellar
Copy link
Collaborator

ping @fhahn

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

LGTM, thanks

@github-project-automation github-project-automation bot moved this from Needs Review to Needs Merge in LLVM Release Status Sep 23, 2025
…8652)

When adding a new predicate to a union, we currently do a bidirectional
implication for all the contained predicates. This means that the number
of implication checks is quadratic in the number of total predicates (if
they don't end up being eliminated).

Fix this by not checking for implication if the number of predicates
grows too large. The expectation is that if there is a large number of
predicates, we should be discarding them later anyway, as expanding them
would be too expensive.

Fixes llvm#156114.

(cherry picked from commit 7af659d)
@tstellar tstellar merged commit b708aea into llvm:release/21.x Sep 23, 2025
1 of 2 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Sep 23, 2025
@github-actions
Copy link

@nikic (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:analysis Includes value tracking, cost tables and constant folding

Projects

Development

Successfully merging this pull request may close these issues.

4 participants