[BasicBlockUtilsTests] Added test case splitBlockBefore2. NFC#179408
[BasicBlockUtilsTests] Added test case splitBlockBefore2. NFC#179408
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 notified. 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 permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. 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-ir @llvm/pr-subscribers-llvm-transforms Author: Yevgeny Rouban (yrouban) ChangesSplitBlock() breaks LoopInfo. Splitting a basic block BB into a pair of blocks NewBB->BB the call SplitBlock(... DT, &LI,... /* Before */ true) does not change NewBB to be the new head of the loop if BB was the loop head before the split. This PR fixes the loop header in splitBlockBefore(). If BB was the loop header before NewBB = splitBlockBefore(BB, ...) then the NewBB is set to be the new header of the loop. Full diff: https://github.com/llvm/llvm-project/pull/179408.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
index b0c04086f5e84..381cb7170c0f6 100644
--- a/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
+++ b/llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
@@ -1080,8 +1080,11 @@ BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, BasicBlock::iterator SplitPt
// The new block lives in whichever loop the old one did. This preserves
// LCSSA as well, because we force the split point to be after any PHI nodes.
if (LI)
- if (Loop *L = LI->getLoopFor(Old))
+ if (Loop *L = LI->getLoopFor(Old)) {
L->addBasicBlockToLoop(New, *LI);
+ if (L->getHeader() == Old)
+ L->moveToHeader(New);
+ }
if (DTU) {
SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
diff --git a/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp b/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp
index 00d9e9ff81e05..198518e130551 100644
--- a/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp
+++ b/llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp
@@ -26,6 +26,8 @@
#include "llvm/Support/SourceMgr.h"
#include "gtest/gtest.h"
+#define DEBUG_TYPE "basic-block-utils-tests"
+
using namespace llvm;
static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
@@ -354,6 +356,40 @@ define void @foo() {
}
#endif
+#ifndef NDEBUG
+TEST(BasicBlockUtils, SplitBlockBefore) {
+ LLVMContext C;
+ std::unique_ptr<Module> M = parseIR(C, R"IR(
+define void @split-block-before-test(i1 %flag) {
+entry:
+ br label %loop
+
+loop:
+ br i1 %flag, label %loop, label %exit
+
+exit:
+ ret void
+}
+)IR");
+ Function *F = M->getFunction("split-block-before-test");
+ DominatorTree DT(*F);
+ LoopInfo LI(DT);
+
+ EXPECT_TRUE(DT.verify());
+ LI.verify(DT);
+ auto *LoopBB = getBasicBlockByName(*F, "loop");
+ auto *New =
+ SplitBlock(LoopBB, LoopBB->getFirstInsertionPt(), &DT, &LI,
+ /* MemorySSAUpdater */ nullptr, LoopBB->getName() + ".split",
+ /* Before */ true);
+
+ EXPECT_TRUE(DT.verify());
+ LLVM_DEBUG(F->dump(); LI.print(dbgs()));
+ LI.verify(DT);
+ EXPECT_EQ(LI.getLoopFor(New)->getHeader(), New);
+}
+#endif
+
TEST(BasicBlockUtils, NoUnreachableBlocksToEliminate) {
LLVMContext C;
std::unique_ptr<Module> M = parseIR(C, R"IR(
|
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
|
This conflicts with other recent work on this function (see #179392 for latest version). Does that one fix your issue as well? |
9d378d5 to
faaff9a
Compare
At first glance it fixes as it does LI update in UpdateAnalysisInformation(). I will check my case and let you know. At least it would be good to add my case to BasicBlockUtilsTest.cpp. |
I have rebased my test over the PR #179392 locally and found the test passed. So you were right, my fix is not needed any more, except the unit test. |
nikic
left a comment
There was a problem hiding this comment.
In that case, can you please rebase this to leave just the new test?
| } | ||
| #endif | ||
|
|
||
| #ifndef NDEBUG |
There was a problem hiding this comment.
Why does this need ifndef NDEBUG?
There was a problem hiding this comment.
Because the crash happened only in assert call.
ok, but when #179392 is landed. Or it could be 2 patches: before and after #179392. The first one is crashing in DEBUG mode, the second is paasing due to #179392. Another choice is to incorporate the testcase into #179392 as I suggested there. |
Isn't it already landed? |
Splitting a basic block BB into a pair of blocks NewBB->BB used to make LoopInfo invalid. Commit 28a0cfa fixed this issue. So this commit just adds the test case the issue was found with.
faaff9a to
4d1cf0f
Compare
I have updated the PR: removed the fix and rebased the test to the latst main. |
| LoopInfo LI(DT); | ||
|
|
||
| EXPECT_TRUE(DT.verify()); | ||
| LI.verify(DT); |
There was a problem hiding this comment.
I don't think it makes much sense to verify freshly constructed DT/LI.
|
@yrouban 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 by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. 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. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/59/builds/31279 Here is the relevant piece of the build log for the reference |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/129/builds/38200 Here is the relevant piece of the build log for the reference |
Splitting a basic block BB into a pair of blocks NewBB->BB used to make LoopInfo invalid. Commit 28a0cfa (PR #179392) fixed this issue. So this commit just adds the test case the issue was found with.