Skip to content

Commit e151b1d

Browse files
authored
[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 ccd92ec commit e151b1d

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
@@ -6180,6 +6180,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
61806180
F->addFnAttr("target-features", "+mmx,+sse");
61816181
IRBuilder<> Builder(BB);
61826182
auto *Int32Ty = Builder.getInt32Ty();
6183+
Builder.SetCurrentDebugLocation(DL);
61836184

61846185
AllocaInst *APtr = Builder.CreateAlloca(Int32Ty, nullptr, "a_ptr");
61856186
AllocaInst *BPtr = Builder.CreateAlloca(Int32Ty, nullptr, "b_ptr");
@@ -6189,6 +6190,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
61896190
Builder.CreateStore(Builder.getInt32(20), BPtr);
61906191
auto BodyGenCB = [&](InsertPointTy AllocaIP,
61916192
InsertPointTy CodeGenIP) -> InsertPointTy {
6193+
IRBuilderBase::InsertPointGuard guard(Builder);
6194+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
61926195
Builder.restoreIP(CodeGenIP);
61936196
LoadInst *AVal = Builder.CreateLoad(Int32Ty, APtr);
61946197
LoadInst *BVal = Builder.CreateLoad(Int32Ty, BPtr);
@@ -6206,6 +6209,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62066209
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
62076210
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
62086211
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6212+
IRBuilderBase::InsertPointGuard guard(Builder);
6213+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
62096214
if (!OMPBuilder.Config.isTargetDevice()) {
62106215
RetVal = cast<llvm::Value>(&Arg);
62116216
return CodeGenIP;
@@ -6252,6 +6257,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegion) {
62526257
Builder.saveIP(), EntryInfo, DefaultAttrs,
62536258
RuntimeAttrs, /*IfCond=*/nullptr, Inputs,
62546259
GenMapInfoCB, BodyGenCB, SimpleArgAccessorCB));
6260+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
62556261
Builder.restoreIP(AfterIP);
62566262

62576263
OMPBuilder.finalize();
@@ -6350,6 +6356,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63506356
F->addFnAttr("target-features", "+gfx9-insts,+wavefrontsize64");
63516357
IRBuilder<> Builder(BB);
63526358
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
6359+
Builder.SetCurrentDebugLocation(DL);
63536360

63546361
LoadInst *Value = nullptr;
63556362
StoreInst *TargetStore = nullptr;
@@ -6361,6 +6368,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63616368
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
63626369
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
63636370
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6371+
IRBuilderBase::InsertPointGuard guard(Builder);
6372+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
63646373
if (!OMPBuilder.Config.isTargetDevice()) {
63656374
RetVal = cast<llvm::Value>(&Arg);
63666375
return CodeGenIP;
@@ -6394,6 +6403,8 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
63946403
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
63956404
OpenMPIRBuilder::InsertPointTy CodeGenIP)
63966405
-> OpenMPIRBuilder::InsertPointTy {
6406+
IRBuilderBase::InsertPointGuard guard(Builder);
6407+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
63976408
Builder.restoreIP(CodeGenIP);
63986409
Value = Builder.CreateLoad(Type::getInt32Ty(Ctx), CapturedArgs[0]);
63996410
TargetStore = Builder.CreateStore(Value, CapturedArgs[1]);
@@ -6415,6 +6426,7 @@ TEST_F(OpenMPIRBuilderTest, TargetRegionDevice) {
64156426
EntryInfo, DefaultAttrs, RuntimeAttrs,
64166427
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
64176428
BodyGenCB, SimpleArgAccessorCB));
6429+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
64186430
Builder.restoreIP(AfterIP);
64196431

64206432
Builder.CreateRetVoid();
@@ -6722,6 +6734,7 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67226734
F->setName("func");
67236735
IRBuilder<> Builder(BB);
67246736
OpenMPIRBuilder::LocationDescription Loc({Builder.saveIP(), DL});
6737+
Builder.SetCurrentDebugLocation(DL);
67256738

67266739
LoadInst *Value = nullptr;
67276740
StoreInst *TargetStore = nullptr;
@@ -6732,6 +6745,8 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67326745
[&](llvm::Argument &Arg, llvm::Value *Input, llvm::Value *&RetVal,
67336746
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP,
67346747
llvm::OpenMPIRBuilder::InsertPointTy CodeGenIP) {
6748+
IRBuilderBase::InsertPointGuard guard(Builder);
6749+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
67356750
if (!OMPBuilder.Config.isTargetDevice()) {
67366751
RetVal = cast<llvm::Value>(&Arg);
67376752
return CodeGenIP;
@@ -6767,6 +6782,8 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67676782
auto BodyGenCB = [&](OpenMPIRBuilder::InsertPointTy AllocaIP,
67686783
OpenMPIRBuilder::InsertPointTy CodeGenIP)
67696784
-> OpenMPIRBuilder::InsertPointTy {
6785+
IRBuilderBase::InsertPointGuard guard(Builder);
6786+
Builder.SetCurrentDebugLocation(llvm::DebugLoc());
67706787
Builder.restoreIP(CodeGenIP);
67716788
RaiseAlloca = Builder.CreateAlloca(Builder.getInt32Ty());
67726789
Value = Builder.CreateLoad(Type::getInt32Ty(Ctx), CapturedArgs[0]);
@@ -6789,6 +6806,7 @@ TEST_F(OpenMPIRBuilderTest, ConstantAllocaRaise) {
67896806
EntryInfo, DefaultAttrs, RuntimeAttrs,
67906807
/*IfCond=*/nullptr, CapturedArgs, GenMapInfoCB,
67916808
BodyGenCB, SimpleArgAccessorCB));
6809+
EXPECT_EQ(DL, Builder.getCurrentDebugLocation());
67926810
Builder.restoreIP(AfterIP);
67936811

67946812
Builder.CreateRetVoid();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
42294229
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
42304230
auto bodyCB = [&](InsertPointTy allocaIP, InsertPointTy codeGenIP)
42314231
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
4232+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
4233+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
42324234
// Forward target-cpu and target-features function attributes from the
42334235
// original function to the new outlined function.
42344236
llvm::Function *llvmParentFn =
@@ -4324,6 +4326,8 @@ convertOmpTarget(Operation &opInst, llvm::IRBuilderBase &builder,
43244326
llvm::Value *&retVal, InsertPointTy allocaIP,
43254327
InsertPointTy codeGenIP)
43264328
-> llvm::OpenMPIRBuilder::InsertPointOrErrorTy {
4329+
llvm::IRBuilderBase::InsertPointGuard guard(builder);
4330+
builder.SetCurrentDebugLocation(llvm::DebugLoc());
43274331
// We just return the unaltered argument for the host function
43284332
// for now, some alterations may be required in the future to
43294333
// keep host fallback functions working identically to the device

0 commit comments

Comments
 (0)