-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
Signed-off-by: John Lu <[email protected]>
@llvm/pr-subscribers-llvm-transforms Author: None (LU-JOHN) ChangesCandidate 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:
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) {
|
There was a problem hiding this 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.
There was a problem hiding this 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.
llvm/lib/Transforms/Scalar/Sink.cpp
Outdated
// 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!"); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
Signed-off-by: John Lu <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
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. |
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]>
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]>
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]>
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]>
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]>
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.