Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,15 @@ void llvm::spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,

// Move instructions to new block.
BasicBlock *Old = IP.getBlock();
New->splice(New->begin(), Old, IP.getPoint(), Old->end());
// If the Old block is empty then there are no instructions to move. But in
// the new debug scheme, it could have trailing debug records which will be
// moved to New in spliceDebugInfoEmptyBlock. We dont want that for 2 reasons:
// 1. If New is also empty, it could cause a crash.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// 1. If New is also empty, it could cause a crash.
// 1. If New is also empty, `BasicBlock::splice` crashes

[nit] to be specific. If it doesn't crash anymore, we know it has been fixed.

// 2. Even if New is not empty, we want to keep those debug records in the
// Old as that was the behavior with the old scheme (debug intrinsics).
Copy link
Member

@Meinersbur Meinersbur Aug 29, 2025

Choose a reason for hiding this comment

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

Suggested change
// 2. Even if New is not empty, we want to keep those debug records in the
// Old as that was the behavior with the old scheme (debug intrinsics).
// 2. Even if New is not empty, we want to keep those debug records in the
// Old as that was the behavior with the old scheme (debug intrinsics).

[nit] Just being the legacy scheme doesn't mean it is more correct. Consider something that mentions the rationale from BasicBlock::spliceDebugInfoEmptyBlock:

  // If the source block is completely empty, including no terminator, then
  // transfer any trailing DbgRecords that are still hanging around. This can
  // occur when a block is optimised away and the terminator has been moved
  // somewhere else.

which does not apply here since we still want to use Old BB (like adding a BranchInst with CreateBranch). And/or that those trailing DbgRecords should only exist if Old is empty since there is no other place to attach them.

// So we call BasicBlock::splice only when Old is not empty.
if (!Old->empty())
New->splice(New->begin(), Old, IP.getPoint(), Old->end());

if (CreateBranch) {
auto *NewBr = BranchInst::Create(New, Old);
Expand Down
24 changes: 24 additions & 0 deletions llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7852,4 +7852,28 @@ TEST_F(OpenMPIRBuilderTest, splitBB) {
EXPECT_TRUE(DL == AllocaBB->getTerminator()->getStableDebugLoc());
}

TEST_F(OpenMPIRBuilderTest, spliceBBWithEmptyBB) {
OpenMPIRBuilder OMPBuilder(*M);
OMPBuilder.Config.IsTargetDevice = false;
OMPBuilder.initialize();
F->setName("func");
IRBuilder<> Builder(BB);

// Test calling spliceBB with an empty Block (but having trailing debug
// records).
DIBuilder DIB(*M);
DISubprogram *SP = F->getSubprogram();
DIType *VoidPtrTy =
DIB.createQualifiedType(dwarf::DW_TAG_pointer_type, nullptr);
DILocalVariable *Var = DIB.createParameterVariable(
SP, "test", /*ArgNo*/ 1, SP->getFile(), /*LineNo=*/0, VoidPtrTy);
DIB.insertDeclare(F->getArg(0), Var, DIB.createExpression(), DL,
Builder.GetInsertPoint());
BasicBlock *New = BasicBlock::Create(Ctx, "", F);
spliceBB(Builder.saveIP(), New, true, DL);
Instruction *Terminator = BB->getTerminator();
EXPECT_NE(Terminator, nullptr);
EXPECT_FALSE(Terminator->getDbgRecordRange().empty());
}

} // namespace