Skip to content

Commit ddde5bd

Browse files
authored
Amd/dev/rlieberm/handle mapping (llvm#2607)
2 parents cc42135 + 0edb80c commit ddde5bd

File tree

5 files changed

+98
-15
lines changed

5 files changed

+98
-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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6197,6 +6197,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
61976197
F->addFnAttr("target-features", "+mmx,+sse");
61986198
IRBuilder<> Builder(BB);
61996199
auto *Int32Ty = Builder.getInt32Ty();
6200+
Builder.SetCurrentDebugLocation(DL);
62006201

62016202
AllocaInst *APtr = Builder.CreateAlloca(Int32Ty, nullptr, "a_ptr");
62026203
AllocaInst *BPtr = Builder.CreateAlloca(Int32Ty, nullptr, "b_ptr");
@@ -6206,6 +6207,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62066207
Builder.CreateStore(Builder.getInt32(20), BPtr);
62076208
auto BodyGenCB = [&](InsertPointTy AllocaIP,
62086209
InsertPointTy CodeGenIP) -> InsertPointTy {
6210+
IRBuilderBase::InsertPointGuard guard(Builder);
6211+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
62096212
Builder.restoreIP(CodeGenIP);
62106213
LoadInst *AVal = Builder.CreateLoad(Int32Ty, APtr);
62116214
LoadInst *BVal = Builder.CreateLoad(Int32Ty, BPtr);
@@ -6223,6 +6226,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62236226
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
62246227
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
62256228
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6229+
IRBuilderBase::InsertPointGuard guard(Builder);
6230+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
62266231
if (!OMPBuilder.Config.isTargetDevice()) {
62276232
RetVal = cast<llvm::Value>(&Arg);
62286233
return CodeGenIP;
@@ -6269,6 +6274,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62696274
Builder.saveIP(), EntryInfo, DefaultAttrs,
62706275
RuntimeAttrs, /*IfCond=*/nullptr, Inputs,
62716276
GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB));
6277+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
62726278
Builder.restoreIP(AfterIP);
62736279

62746280
OMPBuilder.finalize();
@@ -6368,6 +6374,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63686374
F->addFnAttr("target-features", "+gfx9-insts,+wavefrontsize64");
63696375
IRBuilder<> Builder(BB);
63706376
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
6377+
Builder.SetCurrentDebugLocation(DL);
63716378

63726379
LoadInst *Value = nullptr;
63736380
StoreInst *TargetStore = nullptr;
@@ -6379,6 +6386,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63796386
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
63806387
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
63816388
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6389+
IRBuilderBase::InsertPointGuard guard(Builder);
6390+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
63826391
if (!OMPBuilder.Config.isTargetDevice()) {
63836392
RetVal = cast<llvm::Value>(&Arg);
63846393
return CodeGenIP;
@@ -6412,6 +6421,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
64126421
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
64136422
OpenMPIRBuilder::InsertPointTy CodeGenIP)
64146423
-> OpenMPIRBuilder::InsertPointTy {
6424+
IRBuilderBase::InsertPointGuard guard(Builder);
6425+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
64156426
Builder.restoreIP(CodeGenIP);
64166427
Value = Builder.CreateLoad(Type::getInt32Ty(Ctx), CapturedArgs[0]);
64176428
TargetStore = Builder.CreateStore(Value, CapturedArgs[1]);
@@ -6433,6 +6444,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
64336444
EntryInfo, DefaultAttrs, RuntimeAttrs,
64346445
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
64356446
BodyGenCB, SimpleArgAccessorCB));
6447+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
64366448
Builder.restoreIP(AfterIP);
64376449

64386450
Builder.CreateRetVoid();
@@ -6742,6 +6754,7 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67426754
F->setName("func");
67436755
IRBuilder<> Builder(BB);
67446756
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
6757+
Builder.SetCurrentDebugLocation(DL);
67456758

67466759
LoadInst *Value = nullptr;
67476760
StoreInst *TargetStore = nullptr;
@@ -6752,6 +6765,8 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67526765
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
67536766
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
67546767
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6768+
IRBuilderBase::InsertPointGuard guard(Builder);
6769+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
67556770
if (!OMPBuilder.Config.isTargetDevice()) {
67566771
RetVal = cast<llvm::Value>(&Arg);
67576772
return CodeGenIP;
@@ -6787,6 +6802,8 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67876802
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
67886803
OpenMPIRBuilder::InsertPointTy CodeGenIP)
67896804
-> OpenMPIRBuilder::InsertPointTy {
6805+
IRBuilderBase::InsertPointGuard guard(Builder);
6806+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
67906807
Builder.restoreIP(CodeGenIP);
67916808
RaiseAlloca = Builder.CreateAlloca(Builder.getInt32Ty());
67926809
Value = Builder.CreateLoad(Type::getInt32Ty(Ctx), CapturedArgs[0]);
@@ -6809,6 +6826,7 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
68096826
EntryInfo, DefaultAttrs, RuntimeAttrs,
68106827
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
68116828
BodyGenCB, SimpleArgAccessorCB));
6829+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
68126830
Builder.restoreIP(AfterIP);
68136831

68146832
Builder.CreateRetVoid();
@@ -7662,4 +7680,20 @@ TEST_F(OpenMPIRBuilderTest, createGPUOffloadEntry) {
76627680
EXPECT_TRUE(Fn->hasFnAttribute(Attribute::MustProgress));
76637681
}
76647682

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+
76657699
} // namespace

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

Lines changed: 6 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.
@@ -4936,6 +4937,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
49364937
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
49374938
auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP)
49384939
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
4940+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
4941+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
49394942
// Forward target-cpu and target-features function attributes from the
49404943
// original function to the new outlined function.
49414944
llvm::Function *llvmParentFn =
@@ -5031,6 +5034,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
50315034
llvm::Value *&retVal, InsertPointTy allocaIP,
50325035
InsertPointTy codeGenIP)
50335036
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
5037+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
5038+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
50345039
// We just return the unaltered argument for the host function
50355040
// for now, some alterations may be required in the future to
50365041
// keep host fallback functions working identically to the device
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)