Skip to content

[BasicBlockUtilsTests] Added test case splitBlockBefore2. NFC#179408

Merged
yrouban merged 1 commit intollvm:mainfrom
yrouban-org:fix-loopinfo-header-in-split-block-before
Feb 5, 2026
Merged

[BasicBlockUtilsTests] Added test case splitBlockBefore2. NFC#179408
yrouban merged 1 commit intollvm:mainfrom
yrouban-org:fix-loopinfo-header-in-split-block-before

Conversation

@yrouban
Copy link
Contributor

@yrouban yrouban commented Feb 3, 2026

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.

@yrouban yrouban requested review from jmorse and nikic February 3, 2026 07:51
@yrouban yrouban self-assigned this Feb 3, 2026
@yrouban yrouban added llvm Umbrella label for LLVM issues llvm:ir llvm:transforms labels Feb 3, 2026
@github-actions
Copy link

github-actions bot commented Feb 3, 2026

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 @ followed by their GitHub username.

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.

@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2026

@llvm/pr-subscribers-llvm-ir

@llvm/pr-subscribers-llvm-transforms

Author: Yevgeny Rouban (yrouban)

Changes

SplitBlock() 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:

  • (modified) llvm/lib/Transforms/Utils/BasicBlockUtils.cpp (+4-1)
  • (modified) llvm/unittests/Transforms/Utils/BasicBlockUtilsTest.cpp (+36)
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(

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

🪟 Windows x64 Test Results

  • 130445 tests passed
  • 2909 tests skipped

✅ The build succeeded and all tests passed.

@github-actions
Copy link

github-actions bot commented Feb 3, 2026

🐧 Linux x64 Test Results

  • 189582 tests passed
  • 5050 tests skipped

✅ The build succeeded and all tests passed.

@nikic
Copy link
Contributor

nikic commented Feb 3, 2026

This conflicts with other recent work on this function (see #179392 for latest version). Does that one fix your issue as well?

@yrouban yrouban force-pushed the fix-loopinfo-header-in-split-block-before branch from 9d378d5 to faaff9a Compare February 3, 2026 09:05
@yrouban
Copy link
Contributor Author

yrouban commented Feb 3, 2026

This conflicts with other recent work on this function (see #179392 for latest version). Does that one fix your issue as well?

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.

@yrouban
Copy link
Contributor Author

yrouban commented Feb 4, 2026

This conflicts with other recent work on this function (see #179392 for latest version). Does that one fix your issue as well?

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.

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.

In that case, can you please rebase this to leave just the new test?

}
#endif

#ifndef NDEBUG
Copy link
Contributor

Choose a reason for hiding this comment

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

Why does this need ifndef NDEBUG?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the crash happened only in assert call.

@yrouban
Copy link
Contributor Author

yrouban commented Feb 4, 2026

In that case, can you please rebase this to leave just the new test?

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.

@nikic
Copy link
Contributor

nikic commented Feb 4, 2026

ok, but when #179392 is landed.

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.
@yrouban yrouban force-pushed the fix-loopinfo-header-in-split-block-before branch from faaff9a to 4d1cf0f Compare February 4, 2026 12:50
@yrouban yrouban changed the title [BasicBlockUtils] Fix loopinfo header in split block before [BasicBlockUtilsTests] Added test case splitBlockBefore2. NFC Feb 4, 2026
@yrouban
Copy link
Contributor Author

yrouban commented Feb 4, 2026

ok, but when #179392 is landed.

Isn't it already landed?

I have updated the PR: removed the fix and rebased the test to the latst main.

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

LoopInfo LI(DT);

EXPECT_TRUE(DT.verify());
LI.verify(DT);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think it makes much sense to verify freshly constructed DT/LI.

@yrouban yrouban merged commit 010f853 into llvm:main Feb 5, 2026
10 checks passed
@github-actions
Copy link

github-actions bot commented Feb 5, 2026

@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-ci
Copy link

llvm-ci commented Feb 5, 2026

LLVM Buildbot has detected a new failure on builder lldb-aarch64-ubuntu running on linaro-lldb-aarch64-ubuntu while building llvm at step 6 "test".

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
Step 6 (test) failure: build (failure)
...
PASS: lldb-api :: commands/frame/var/TestFrameVar.py (193 of 2462)
PASS: lldb-api :: commands/help/TestHelp.py (194 of 2462)
PASS: lldb-api :: commands/log/invalid-args/TestInvalidArgsLog.py (195 of 2462)
PASS: lldb-api :: commands/memory/write/TestMemoryWrite.py (196 of 2462)
PASS: lldb-api :: commands/platform/basic/TestPlatformCommand.py (197 of 2462)
PASS: lldb-api :: commands/platform/basic/TestPlatformPython.py (198 of 2462)
PASS: lldb-api :: commands/platform/file/close/TestPlatformFileClose.py (199 of 2462)
PASS: lldb-api :: commands/platform/file/read/TestPlatformFileRead.py (200 of 2462)
PASS: lldb-api :: commands/memory/read/TestMemoryRead.py (201 of 2462)
UNRESOLVED: lldb-api :: commands/gui/spawn-threads/TestGuiSpawnThreads.py (202 of 2462)
******************** TEST 'lldb-api :: commands/gui/spawn-threads/TestGuiSpawnThreads.py' FAILED ********************
Script:
--
/usr/bin/python3.10 /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/dotest.py -u CXXFLAGS -u CFLAGS --env LLVM_LIBS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --env LLVM_INCLUDE_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/include --env LLVM_TOOLS_DIR=/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --arch aarch64 --build-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex --lldb-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api --clang-module-cache-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-clang/lldb-api --executable /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/lldb --compiler /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/clang --dsymutil /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin/dsymutil --make /usr/bin/gmake --llvm-tools-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./bin --lldb-obj-root /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb --lldb-libs-dir /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/./lib --cmake-build-type Release /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/commands/gui/spawn-threads -p TestGuiSpawnThreads.py
--
Exit Code: 1

Command Output (stdout):
--
lldb version 23.0.0git (https://github.com/llvm/llvm-project.git revision 010f8534084f86ab299f93770e9a83c815774b66)
  clang revision 010f8534084f86ab299f93770e9a83c815774b66
  llvm revision 010f8534084f86ab299f93770e9a83c815774b66
Skipping the following test categories: libc++, msvcstl, dsym, pdb, gmodules, debugserver, objc

--
Command Output (stderr):
--
FAIL: LLDB (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/clang-aarch64) :: test_gui (TestGuiSpawnThreads.TestGuiSpawnThreadsTest)
======================================================================
ERROR: test_gui (TestGuiSpawnThreads.TestGuiSpawnThreadsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/packages/Python/lldbsuite/test/decorators.py", line 160, in wrapper
    return func(*args, **kwargs)
  File "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/llvm-project/lldb/test/API/commands/gui/spawn-threads/TestGuiSpawnThreads.py", line 44, in test_gui
    self.child.expect_exact(f"thread #{i + 2}: tid =")
  File "/usr/local/lib/python3.10/dist-packages/pexpect/spawnbase.py", line 432, in expect_exact
    return exp.expect_loop(timeout)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 179, in expect_loop
    return self.eof(e)
  File "/usr/local/lib/python3.10/dist-packages/pexpect/expect.py", line 122, in eof
    raise exc
pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.
<pexpect.pty_spawn.spawn object at 0xf3c6ae98c790>
command: /home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/lldb
args: ['/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/lldb', '--no-lldbinit', '--no-use-colors', '-O', 'settings clear --all', '-O', 'settings set symbols.enable-external-lookup false', '-O', 'settings set target.inherit-tcc true', '-O', 'settings set target.disable-aslr false', '-O', 'settings set target.detach-on-error false', '-O', 'settings set target.auto-apply-fixits false', '-O', 'settings set plugin.process.gdb-remote.packet-timeout 60', '-O', 'settings set symbols.clang-modules-cache-path "/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/module-cache-lldb/lldb-api"', '-O', 'settings set use-color false', '-O', 'settings set show-statusline false', '--file', '/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/lldb-test-build.noindex/commands/gui/spawn-threads/TestGuiSpawnThreads.test_gui/a.out']
buffer (last 100 chars): b''
before (last 100 chars): b'7 0x0000bdb30a123e70 _start (/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/bin/lldb+0x43e70)\n'
after: <class 'pexpect.exceptions.EOF'>

@llvm-ci
Copy link

llvm-ci commented Feb 5, 2026

LLVM Buildbot has detected a new failure on builder ppc64le-mlir-rhel-clang running on ppc64le-mlir-rhel-test while building llvm at step 3 "clean-build-dir".

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
Step 3 (clean-build-dir) failure: Delete failed. (failure) (timed out)
Step 6 (test-build-check-mlir-build-only-check-mlir) failure: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
...
PASS: MLIR :: Pass/pipeline-options-parsing.mlir (3849 of 3860)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/13/22 (3850 of 3860)
PASS: MLIR-Unit :: Pass/./MLIRPassTests/11/14 (3851 of 3860)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/11/22 (3852 of 3860)
PASS: MLIR-Unit :: Interfaces/./MLIRInterfacesTests/12/22 (3853 of 3860)
PASS: MLIR-Unit :: IR/./MLIRIRTests/0/132 (3854 of 3860)
PASS: MLIR-Unit :: IR/./MLIRIRTests/38/132 (3855 of 3860)
PASS: MLIR-Unit :: IR/./MLIRIRTests/39/132 (3856 of 3860)
PASS: MLIR :: Pass/pass-timing.mlir (3857 of 3860)
PASS: MLIR :: mlir-reduce/dce-test.mlir (3858 of 3860)
command timed out: 1200 seconds without output running [b'ninja', b'check-mlir'], attempting to kill
process killed by signal 9
program finished with exit code -1
elapsedTime=2203.280692

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

Labels

llvm:ir llvm:transforms llvm Umbrella label for LLVM issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants