Skip to content

[NFC][Sink] Change runtime checks to asserts #137354

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

Merged
merged 3 commits into from
Apr 25, 2025
Merged

Conversation

LU-JOHN
Copy link
Contributor

@LU-JOHN LU-JOHN commented Apr 25, 2025

Candidate block for sinking must be dominated by current location. This is true based on how the candidate block was selected. Runtime checks are not necessary and has been changed to an assertion.

@LU-JOHN LU-JOHN changed the title [NFC][Sink] Change dynamic checks to asserts [NFC][Sink] Change runtime checks to asserts Apr 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 25, 2025

@llvm/pr-subscribers-llvm-transforms

Author: None (LU-JOHN)

Changes

Candidate block for sinking must be dominated by current location. This is true based on how the candidate block was selected. A runtime checks are not necessary and has been changed to an assertion.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/Sink.cpp (+9-7)
diff --git a/llvm/lib/Transforms/Scalar/Sink.cpp b/llvm/lib/Transforms/Scalar/Sink.cpp
index 1a48a59c4189e..d6fd485aa5374 100644
--- a/llvm/lib/Transforms/Scalar/Sink.cpp
+++ b/llvm/lib/Transforms/Scalar/Sink.cpp
@@ -82,10 +82,11 @@ static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo,
         !Inst->hasMetadata(LLVMContext::MD_invariant_load))
       return false;
 
-    // We don't want to sink across a critical edge if we don't dominate the
-    // successor. We could be introducing calculations to new code paths.
-    if (!DT.dominates(Inst->getParent(), SuccToSinkTo))
-      return false;
+    // The current location of Inst dominates all uses, thus it must dominate
+    // SuccToSinkTo, which is on the IDom chain between the nearest common
+    // dominator to all uses and the current location.
+    assert(DT.dominates(Inst->getParent(), SuccToSinkTo) &&
+           "SuccToSinkTo must be dominated by current Inst location!");
 
     // Don't sink instructions into a loop.
     Loop *succ = LI.getLoopFor(SuccToSinkTo);
@@ -144,9 +145,10 @@ static bool SinkInstruction(Instruction *Inst,
       SuccToSinkTo = DT.findNearestCommonDominator(SuccToSinkTo, UseBlock);
     else
       SuccToSinkTo = UseBlock;
-    // The current basic block needs to dominate the candidate.
-    if (!DT.dominates(BB, SuccToSinkTo))
-      return false;
+    // The current basic block dominates all uses, thus it must dominate
+    // SuccToSinkTo, the nearest common dominator of all uses.
+    assert(DT.dominates(BB, SuccToSinkTo) &&
+           "SuccToSinkTo must be dominated by current basic block!");
   }
 
   if (SuccToSinkTo) {

Copy link
Contributor

@shiltian shiltian left a comment

Choose a reason for hiding this comment

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

This is not an NFC when assert is disabled.

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.

Nice catch, looks like this is a leftover from an old implementation prior to f2284e3.

// The current basic block dominates all uses, thus it must dominate
// SuccToSinkTo, the nearest common dominator of all uses.
assert(DT.dominates(BB, SuccToSinkTo) &&
"SuccToSinkTo must be dominated by current basic block!");
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it would be better to replace the two asserts with a single one at the end of this function.

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've replaced the two asserts with one assert, but I've put the assert at the beginning of the function since it is already true at that point.

Copy link
Contributor

Choose a reason for hiding this comment

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

The reason why I suggest to put it at the end is that SuccToSinkTo is changed in the function. So we are asserting that the property still holds after these adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved assert to when SuccToSinkTo is used to sink instruction.

@LU-JOHN
Copy link
Contributor Author

LU-JOHN commented Apr 25, 2025

This is not an NFC when assert is disabled.

When assert is disabled I've removed an if-block where the condition can never be true. Isn't this a NFC?

Signed-off-by: John Lu <[email protected]>
Copy link

github-actions bot commented Apr 25, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@LU-JOHN LU-JOHN requested review from nikic and shiltian April 25, 2025 20:15
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.

LGTM

@nikic nikic merged commit f9d4e7e into llvm:main Apr 25, 2025
9 of 10 checks passed
@shiltian
Copy link
Contributor

This is not an NFC when assert is disabled.

When assert is disabled I've removed an if-block where the condition can never be true. Isn't this a NFC?

I was thinking that if there's any chance the condition in the assertion doesn't hold, and the assertion is also removed, then without this PR it would bail out, but now it would simply continue. So it’s not actually an NFC.

jyli0116 pushed a commit to jyli0116/llvm-project that referenced this pull request Apr 28, 2025
Candidate block for sinking must be dominated by current location. This
is true based on how the candidate block was selected. Runtime checks
are not necessary and has been changed to an assertion.

---------

Signed-off-by: John Lu <[email protected]>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Candidate block for sinking must be dominated by current location. This
is true based on how the candidate block was selected. Runtime checks
are not necessary and has been changed to an assertion.

---------

Signed-off-by: John Lu <[email protected]>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Candidate block for sinking must be dominated by current location. This
is true based on how the candidate block was selected. Runtime checks
are not necessary and has been changed to an assertion.

---------

Signed-off-by: John Lu <[email protected]>
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Candidate block for sinking must be dominated by current location. This
is true based on how the candidate block was selected. Runtime checks
are not necessary and has been changed to an assertion.

---------

Signed-off-by: John Lu <[email protected]>
Ankur-0429 pushed a commit to Ankur-0429/llvm-project that referenced this pull request May 9, 2025
Candidate block for sinking must be dominated by current location. This
is true based on how the candidate block was selected. Runtime checks
are not necessary and has been changed to an assertion.

---------

Signed-off-by: John Lu <[email protected]>
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.

4 participants