Skip to content

[IRCE] Fix '"Instruction does not dominate all uses!" after IRCE pass #63984' #136505

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 3 commits into
base: main
Choose a base branch
from

Conversation

Zentrik
Copy link
Contributor

@Zentrik Zentrik commented Apr 20, 2025

Fixes #63984. Not sure if this is the correct fix or if there's a deeper bug somewhere else that needs to be fixed.

The issue is that running irce with IR that has a range check against undef seems to be able to generate IR where instructions do not dominate all their uses. I believe this fixes the issue by preventing IRCE from optimizing over range checks against undefs.

@llvmbot
Copy link
Member

llvmbot commented Apr 20, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (Zentrik)

Changes

upstream/main


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp (+9-4)
diff --git a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
index e706a6f83b1e7..4da816bea5870 100644
--- a/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/InductiveRangeCheckElimination.cpp
@@ -265,8 +265,13 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
                                               ScalarEvolution &SE,
                                               const SCEVAddRecExpr *&Index,
                                               const SCEV *&End) {
-  auto IsLoopInvariant = [&SE, L](Value *V) {
-    return SE.isLoopInvariant(SE.getSCEV(V), L);
+  auto IsLoopInvariantAndNotUndef = [&SE, L](Value *V) {
+    const SCEV *S = SE.getSCEV(V);
+
+    if (isa<SCEVCouldNotCompute>(S))
+      return false;
+
+    return SE.isLoopInvariant(SE.getSCEV(V), L) && !SE.containsUndefs(S);
   };
 
   ICmpInst::Predicate Pred = ICI->getPredicate();
@@ -277,10 +282,10 @@ bool InductiveRangeCheck::parseRangeCheckICmp(Loop *L, ICmpInst *ICI,
     return false;
 
   // Canonicalize to the `Index Pred Invariant` comparison
-  if (IsLoopInvariant(LHS)) {
+  if (IsLoopInvariantAndNotUndef(LHS)) {
     std::swap(LHS, RHS);
     Pred = CmpInst::getSwappedPredicate(Pred);
-  } else if (!IsLoopInvariant(RHS))
+  } else if (!IsLoopInvariantAndNotUndef(RHS))
     // Both LHS and RHS are loop variant
     return false;
 

@Zentrik Zentrik changed the title IRCE: Fix '"Instruction does not dominate all uses!" after IRCE pass #63984' [IRCE] Fix '"Instruction does not dominate all uses!" after IRCE pass #63984' Apr 20, 2025
@dtcxzyw dtcxzyw requested a review from nikic April 21, 2025 05:17
@nikic nikic requested a review from aleks-tmb April 21, 2025 08:26
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Can you please add a test case and some explanation to the PR description about what the problem is / why this fixes it?

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

Successfully merging this pull request may close these issues.

"Instruction does not dominate all uses!" after IRCE pass
3 participants