Skip to content

Commit 6a5af62

Browse files
abidhronlieb
authored andcommitted
[MLIR][OpenMP] Use correct DebugLoc in target construct callbacks. (llvm#125856)
This is same as PR llvm#125106 which somehow is stuck in a "Processing Update" loop for many hours now. I am going to close that one and push this one instead. While working on llvm#125088, I noticed a problem with the TargetBodyGenCallbackTy and TargetGenArgAccessorsCallbackTy. The OMPIRBuilder and MLIR side Both maintain their own IRBuilder and when control goes from one to other, we have to take care to not use a stale debug location. The code currently rely on restoreIP to set the insertion point and the debug location. But if the passes InsertPointTy has an empty block, then the debug location will not be updated (see SetInsertPoint). This can cause invalid debug location to be attached to instruction and the verifier will complain. Similarly when we exit the callback, the debug location of the Builder is not set to what it was before the callback. This again can cause verification failures. This PR resets the debug location at the start and also uses an InsertPointGuard to restore the debug location at exit. Both of these problems would have been caught by the unit tests but they were not setting the debug location of the builder before calling the createTarget so the problem was hidden. I have updated the tests accordingly.
1 parent 1a2e2ae commit 6a5af62

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 18 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();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4936,6 +4936,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
49364936
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
49374937
auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP)
49384938
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
4939+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
4940+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
49394941
// Forward target-cpu and target-features function attributes from the
49404942
// original function to the new outlined function.
49414943
llvm::Function *llvmParentFn =
@@ -5031,6 +5033,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
50315033
llvm::Value *&retVal, InsertPointTy allocaIP,
50325034
InsertPointTy codeGenIP)
50335035
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
5036+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
5037+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
50345038
// We just return the unaltered argument for the host function
50355039
// for now, some alterations may be required in the future to
50365040
// keep host fallback functions working identically to the device

0 commit comments

Comments
 (0)