Skip to content

Commit 0edb80c

Browse files
abidhronlieb
authored andcommitted
[flang][OMPIRbuilder] Set debug loc on terminator created by splitBB. (llvm#125897)
Fixes llvm#125088. When splitBB is called with createBranch=true, it creates a branch instruction in the old block. But no debug loc is set on that branch instruction. If that is used as InsertPoint in the restoreIP, it has the potential to set the current debug location to null and subsequent instruction will come out without a debug location. This caused the verification check to fail as shown in the bug report. This PR changes splitBB and spliceBB function to also take a debugLoc parameter which can be used to set the debug location of the branch instruction.
1 parent 6a5af62 commit 0edb80c

File tree

5 files changed

+76
-15
lines changed

5 files changed

+76
-15
lines changed

llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ class OpenMPIRBuilder;
4040
/// not have any PHINodes. If \p CreateBranch is true, a branch instruction to
4141
/// \p New will be added such that there is no semantic change. Otherwise, the
4242
/// \p IP insert block remains degenerate and it is up to the caller to insert a
43-
/// terminator.
44-
void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,
45-
bool CreateBranch);
43+
/// terminator. \p DL is used as the debug location for the branch instruction
44+
/// if one is created.
45+
void spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New, bool CreateBranch,
46+
DebugLoc DL);
4647

4748
/// Splice a BasicBlock at an IRBuilder's current insertion point. Its new
4849
/// insert location will stick to after the instruction before the insertion
@@ -58,9 +59,10 @@ void spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch);
5859
/// is true, a branch to the new successor will new created such that
5960
/// semantically there is no change; otherwise the block of the insertion point
6061
/// remains degenerate and it is the caller's responsibility to insert a
61-
/// terminator. Returns the new successor block.
62+
/// terminator. \p DL is used as the debug location for the branch instruction
63+
/// if one is created. Returns the new successor block.
6264
BasicBlock *splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch,
63-
llvm::Twine Name = {});
65+
DebugLoc DL, llvm::Twine Name = {});
6466

6567
/// Split a BasicBlock at \p Builder's insertion point, even if the block is
6668
/// degenerate (missing the terminator). Its new insert location will stick to

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -313,23 +313,25 @@ static void redirectTo(BasicBlock *Source, BasicBlock *Target, DebugLoc DL) {
313313
}
314314

315315
void llvm::spliceBB(IRBuilderBase::InsertPoint IP, BasicBlock *New,
316-
bool CreateBranch) {
316+
bool CreateBranch, DebugLoc DL) {
317317
assert(New->getFirstInsertionPt() == New->begin() &&
318318
"Target BB must not have PHI nodes");
319319

320320
// Move instructions to new block.
321321
BasicBlock *Old = IP.getBlock();
322322
New->splice(New->begin(), Old, IP.getPoint(), Old->end());
323323

324-
if (CreateBranch)
325-
BranchInst::Create(New, Old);
324+
if (CreateBranch) {
325+
auto *NewBr = BranchInst::Create(New, Old);
326+
NewBr->setDebugLoc(DL);
327+
}
326328
}
327329

328330
void llvm::spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch) {
329331
DebugLoc DebugLoc = Builder.getCurrentDebugLocation();
330332
BasicBlock *Old = Builder.GetInsertBlock();
331333

332-
spliceBB(Builder.saveIP(), New, CreateBranch);
334+
spliceBB(Builder.saveIP(), New, CreateBranch, DebugLoc);
333335
if (CreateBranch)
334336
Builder.SetInsertPoint(Old->getTerminator());
335337
else
@@ -341,20 +343,20 @@ void llvm::spliceBB(IRBuilder<> &Builder, BasicBlock *New, bool CreateBranch) {
341343
}
342344

343345
BasicBlock *llvm::splitBB(IRBuilderBase::InsertPoint IP, bool CreateBranch,
344-
llvm::Twine Name) {
346+
DebugLoc DL, llvm::Twine Name) {
345347
BasicBlock *Old = IP.getBlock();
346348
BasicBlock *New = BasicBlock::Create(
347349
Old->getContext(), Name.isTriviallyEmpty() ? Old->getName() : Name,
348350
Old->getParent(), Old->getNextNode());
349-
spliceBB(IP, New, CreateBranch);
351+
spliceBB(IP, New, CreateBranch, DL);
350352
New->replaceSuccessorsPhiUsesWith(Old, New);
351353
return New;
352354
}
353355

354356
BasicBlock *llvm::splitBB(IRBuilderBase &Builder, bool CreateBranch,
355357
llvm::Twine Name) {
356358
DebugLoc DebugLoc = Builder.getCurrentDebugLocation();
357-
BasicBlock *New = splitBB(Builder.saveIP(), CreateBranch, Name);
359+
BasicBlock *New = splitBB(Builder.saveIP(), CreateBranch, DebugLoc, Name);
358360
if (CreateBranch)
359361
Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
360362
else
@@ -368,7 +370,7 @@ BasicBlock *llvm::splitBB(IRBuilderBase &Builder, bool CreateBranch,
368370
BasicBlock *llvm::splitBB(IRBuilder<> &Builder, bool CreateBranch,
369371
llvm::Twine Name) {
370372
DebugLoc DebugLoc = Builder.getCurrentDebugLocation();
371-
BasicBlock *New = splitBB(Builder.saveIP(), CreateBranch, Name);
373+
BasicBlock *New = splitBB(Builder.saveIP(), CreateBranch, DebugLoc, Name);
372374
if (CreateBranch)
373375
Builder.SetInsertPoint(Builder.GetInsertBlock()->getTerminator());
374376
else
@@ -5413,7 +5415,7 @@ void OpenMPIRBuilder::createIfVersion(CanonicalLoopInfo *CanonicalLoop,
54135415
Builder.CreateCondBr(IfCond, ThenBlock, /*ifFalse*/ ElseBlock);
54145416
InsertPointTy IP{BrInstr->getParent(), ++BrInstr->getIterator()};
54155417
// Then block contains branch to omp loop which needs to be vectorized
5416-
spliceBB(IP, ThenBlock, false);
5418+
spliceBB(IP, ThenBlock, false, Builder.getCurrentDebugLocation());
54175419
ThenBlock->replaceSuccessorsPhiUsesWith(Head, ThenBlock);
54185420

54195421
Builder.SetInsertPoint(ElseBlock);

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7680,4 +7680,20 @@ TEST_F(OpenMPIRBuilderTest, createGPUOffloadEntry) {
76807680
EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
76817681
}
76827682

7683+
TEST_F(OpenMPIRBuilderTest, splitBB) {
7684+
OpenMPIRBuilder OMPBuilder(*M);
7685+
OMPBuilder.Config.IsTargetDevice = false;
7686+
OMPBuilder.initialize();
7687+
F->setName("func");
7688+
IRBuilder<> Builder(BB);
7689+
7690+
Builder.SetCurrentDebugLocation(DL);
7691+
AllocaInst *alloc = Builder.CreateAlloca(Builder.getInt32Ty());
7692+
EXPECT_TRUE(DL == alloc->getStableDebugLoc());
7693+
BasicBlock *AllocaBB = Builder.GetInsertBlock();
7694+
splitBB(Builder, /*CreateBranch=*/true, "test");
7695+
if (AllocaBB->getTerminator())
7696+
EXPECT_TRUE(DL == AllocaBB->getTerminator()->getStableDebugLoc());
7697+
}
7698+
76837699
} // namespace

mlir/lib/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1544,7 +1544,8 @@ allocatePrivateVars(llvm::IRBuilderBase &builder,
15441544
llvm::cast<llvm::BranchInst>(allocaIP.getBlock()->getTerminator());
15451545
splitBB(llvm::OpenMPIRBuilder::InsertPointTy(allocaIP.getBlock(),
15461546
allocaTerminator->getIterator()),
1547-
true, "omp.region.after_alloca");
1547+
true, allocaTerminator->getStableDebugLoc(),
1548+
"omp.region.after_alloca");
15481549

15491550
llvm::IRBuilderBase::InsertPointGuard guard(builder);
15501551
// Update the allocaTerminator in case the alloca block was split above.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: mlir-translate -mlir-to-llvmir %s
2+
3+
module attributes {omp.is_target_device = false} {
4+
llvm.func @main() {
5+
%0 = llvm.mlir.constant(1 : i64) : i64
6+
%1 = llvm.alloca %0 x f32 : (i64) -> !llvm.ptr
7+
%3 = llvm.alloca %0 x i32 : (i64) -> !llvm.ptr
8+
%6 = omp.map.info var_ptr(%1 : !llvm.ptr, f32) map_clauses(tofrom) capture(ByRef) -> !llvm.ptr
9+
%7 = omp.map.info var_ptr(%3 : !llvm.ptr, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !llvm.ptr
10+
omp.target nowait map_entries(%6 -> %arg0, %7 -> %arg1 : !llvm.ptr, !llvm.ptr) {
11+
%8 = llvm.mlir.constant(0 : i64) : i64
12+
%9 = llvm.mlir.constant(100 : i32) : i32
13+
llvm.br ^bb1(%9, %8 : i32, i64)
14+
^bb1(%13: i32, %14: i64): // 2 preds: ^bb0, ^bb2
15+
%15 = llvm.icmp "sgt" %14, %8 : i64
16+
llvm.cond_br %15, ^bb2, ^bb3
17+
^bb2: // pred: ^bb1
18+
llvm.store %13, %arg1 : i32, !llvm.ptr
19+
llvm.br ^bb1(%13, %14 : i32, i64)
20+
^bb3: // pred: ^bb1
21+
llvm.store %13, %arg1 : i32, !llvm.ptr
22+
omp.terminator
23+
}
24+
llvm.return
25+
} loc(#loc2)
26+
}
27+
28+
#file = #llvm.di_file<"test.f90" in "">
29+
#di_null_type = #llvm.di_null_type
30+
#cu = #llvm.di_compile_unit<id = distinct[0]<>,
31+
sourceLanguage = DW_LANG_Fortran95, file = #file, isOptimized = false,
32+
emissionKind = Full>
33+
#sp_ty = #llvm.di_subroutine_type<callingConvention = DW_CC_program,
34+
types = #di_null_type>
35+
#sp = #llvm.di_subprogram<compileUnit = #cu, name = "main", file=#file,
36+
subprogramFlags = "Definition", type = #sp_ty>
37+
38+
#loc1 = loc("test.f90":6:7)
39+
#loc2 = loc(fused<#sp>[#loc1])
40+

0 commit comments

Comments
 (0)