-
Notifications
You must be signed in to change notification settings - Fork 10.8k
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
[BasicBlockUtils] Remove redundant llvm.dbg instructions after blocks to reduce compile time #89069
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write If you have received no comments on your PR for a week, you can request a review If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-llvm-transforms Author: CL (coderchenlin) Changesmerged. Full diff: https://github.com/llvm/llvm-project/pull/89069.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index 5aa59acfa6df99..ca9d7649ea9826 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -333,6 +333,9 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
// Finally, erase the old block and update dominator info.
DeleteDeadBlock(BB, DTU);
+ // Remove redundant "llvm.dbg" instrunctions after blocks merged.
+ RemoveRedundantDbgInstrs(PredBB);
+
return true;
}
|
this patch helps to remove redundant llvm.dbgs after blocks merged. |
the issue is there : #89073 |
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.
Please add a test showing the impact of this change
@@ -333,6 +333,9 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, | |||
// Finally, erase the old block and update dominator info. | |||
DeleteDeadBlock(BB, DTU); | |||
|
|||
// Remove redundant "llvm.dbg" instrunctions after blocks merged. | |||
RemoveRedundantDbgInstrs(PredBB); |
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 needs to do a forward and backward scan over the BB, even if there are no debug intrinsics in the block. Can it only be called when we cloned blocks with debug intrinsics?
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.
Thanks a lot, @fhahn , I will check it
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.
@fhahn, I added a condition to control it (only execute with the "-g" flag ). Could you please review it again?
5bd126f
to
c4e39bc
Compare
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.
Thanks for the update! The change isn't really specific to LoopUnroll, but any code that uses MergeBlockIntoPredecessor
; could you update the title and also update the PR descriptions .
Please also include Fixes https://github.com/llvm/llvm-project/issues/89073
at the end of the PR description, so the issue gets auto-closed when the PR is merged.
Please also add a loop-unroll test based on the issue.
c4e39bc
to
6ade1a9
Compare
@fhahn , thanks for the review, the title and the PR description has been updated. |
|
Thanks for the patch @coderchenlin ! I'm on holiday for a long while, but Stephen or Orlando may be able to review the patch. |
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.
Could you please also update the description of the PR and include Fixes https://github.com/llvm/llvm-project/issues/89073
?
@@ -333,6 +333,10 @@ bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU, | |||
// Finally, erase the old block and update dominator info. | |||
DeleteDeadBlock(BB, DTU); | |||
|
|||
// Remove redundant "llvm.dbg" instrunctions after blocks merged. |
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.
nit: after blocks have been merged
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.
Thanks, the advice is acceptable, and the comment has been modified.
71da336
to
64f2c33
Compare
The compile time impact of this change looks to be more-or-less neutral on the compile time tracker - at ReleaseLTO-g, the geomean compile time change is +0.02%. Since it looks like this solves some particularly pathological cases though it's probably worth having - so SGTM as long as @fhahn has no more comments and is happy with the current state of the patch. |
@coderchenlin I might have missed it somewhere, but is it possible add a loop-unroll test based on the reproducer from #89073? |
@fhahn, Thanks! I can add the IR case based on the reproducer from #89073, but I need some time to reduce the llvm-ir of the case. As @SLTozer said, the case is a little pathological, and the IR is also strange, I will try to reduce it and make it clearer Before that, if you want to check the effect of this patch, please test this case |
64f2c33
to
0a111ec
Compare
@@ -0,0 +1,51 @@ | |||
; RUN: opt < %s -S -mtriple=riscv64 -passes=loop-unroll | FileCheck %s |
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.
Thanks for the patch! Does the test require -mtriple=riscv64
/ target triple = "riscv64"
? If not, please remove the references to RISCV. Otherwise it needs to be moved to llvm/test/Transforms/LoopUnroll/RISCV/
, as it will fail on systems that don't build the RISCV backend.
declare void @llvm.dbg.value(metadata, metadata, metadata) #1 | ||
|
||
attributes #0 = { "target-cpu"="generic-rv64" } | ||
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } |
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.
not needed
; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) | ||
declare void @llvm.dbg.value(metadata, metadata, metadata) #1 | ||
|
||
attributes #0 = { "target-cpu"="generic-rv64" } |
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.
not needed?
0a111ec
to
f5ea905
Compare
f5ea905
to
7b08d27
Compare
|
||
for.body: ; preds = %for.body, %entry | ||
; There should be only one "llvm.dbg.vale" after loop unrolling | ||
; CHECK: tail call void @llvm.dbg.value(metadata i32 0, metadata !13, metadata !DIExpression()), !dbg !17 |
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.
Forgot to mention this earlier, but this checks that there is at least one call to llvm.dbg.value
, not exactly one.
Multiple ways to go about this, .e.g
CHECK: call void @llvm.dbg.value
CHECK-NOT: call void @llvm.dbg.value
or matching the full generated IR using llvm/utils/update_test_checks.py
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.
Better to just check for call void @llvm.dbg.value
, as otherwise the test would be unnecessary brittle, e.g. needing updating if the metadata numbering slightly changes
f8369ee
to
5d394a7
Compare
have been merged. When compiling with the "-g" and enabling loop-unroll optimization, the redundant "llvm.dbg" instructions sometimes will be generated. It has a significant impact on compilation time in some cases. Fixes llvm#89073.
5d394a7
to
bea3e85
Compare
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, thanks!
@fhahn, Thanks for the review, All tests passed, can this patch merge? |
@coderchenlin Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested Please check whether problems have been caused by your change specifically, as How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
We've bisected a compilation time increase to this patch. It seems to introduce quadratic behavior in SimplifyCFG. The source code of a possible reproducer looks like this:
Unfortunately, reproducing the problem from this source code requires a profile I can't share, so the source code is useful for illustrative purposes only. However, I was able to dump the IR using I'm also attaching two |
seems like we're doing a linear scan of dbg instructions every time we call the call to can we revert this and then land a more targeted solution? |
Reverted in 2cde0e2 |
Thanks for the new case, I will try to find another way to fix the original case. @slackito @aeubanks |
this patch is to fix the compile time for some cases, before this change, some targets (riscv-64, ve) will spend much more compile time on this case (https://godbolt.org/z/rrov17cTo). With this change, the compile time was reduced a lot.
Fixes #89073