Skip to content

Commit d87964d

Browse files
authored
[OpenMP][OMPIRBuilder] Error propagation across callbacks (#112533)
This patch implements an approach to communicate errors between the OMPIRBuilder and its users. It introduces `llvm::Error` and `llvm::Expected` objects to replace the values returned by callbacks passed to `OMPIRBuilder` codegen functions. These functions then check the result for errors when callbacks are called and forward them back to the caller, which has the flexibility to recover, exit cleanly or dump a stack trace. This prevents a failed callback to leave the IR in an invalid state and still continue the codegen process, triggering unrelated assertions or segmentation faults. In the case of MLIR to LLVM IR translation of the 'omp' dialect, this change results in the compiler emitting errors and exiting early instead of triggering a crash for not-yet-implemented errors. The behavior in Clang and openmp-opt stays unchanged, since callbacks will continue always returning 'success'.
1 parent 86d65ae commit d87964d

File tree

8 files changed

+1467
-858
lines changed

8 files changed

+1467
-858
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,6 +1192,7 @@ struct PushAndPopStackRAII {
11921192
CodeGenFunction::JumpDest Dest =
11931193
CGF.getOMPCancelDestination(OMPD_parallel);
11941194
CGF.EmitBranchThroughCleanup(Dest);
1195+
return llvm::Error::success();
11951196
};
11961197

11971198
// TODO: Remove this once we emit parallel regions through the
@@ -2331,8 +2332,11 @@ void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
23312332
auto *OMPRegionInfo =
23322333
dyn_cast_or_null<CGOpenMPRegionInfo>(CGF.CapturedStmtInfo);
23332334
if (CGF.CGM.getLangOpts().OpenMPIRBuilder) {
2334-
CGF.Builder.restoreIP(OMPBuilder.createBarrier(
2335-
CGF.Builder, Kind, ForceSimpleCall, EmitChecks));
2335+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
2336+
OMPBuilder.createBarrier(CGF.Builder, Kind, ForceSimpleCall,
2337+
EmitChecks);
2338+
assert(AfterIP && "unexpected error creating barrier");
2339+
CGF.Builder.restoreIP(*AfterIP);
23362340
return;
23372341
}
23382342

@@ -5928,8 +5932,10 @@ void CGOpenMPRuntime::emitTargetOutlinedFunctionHelper(
59285932
return CGF.GenerateOpenMPCapturedStmtFunction(CS, D.getBeginLoc());
59295933
};
59305934

5931-
OMPBuilder.emitTargetRegionFunction(EntryInfo, GenerateOutlinedFunction,
5932-
IsOffloadEntry, OutlinedFn, OutlinedFnID);
5935+
llvm::Error Err = OMPBuilder.emitTargetRegionFunction(
5936+
EntryInfo, GenerateOutlinedFunction, IsOffloadEntry, OutlinedFn,
5937+
OutlinedFnID);
5938+
assert(!Err && "unexpected error creating target region");
59335939

59345940
if (!OutlinedFn)
59355941
return;
@@ -9670,9 +9676,12 @@ static void emitTargetCallKernelLaunch(
96709676
NumTargetItems, RTArgs, NumIterations, NumTeams, NumThreads,
96719677
DynCGGroupMem, HasNoWait);
96729678

9673-
CGF.Builder.restoreIP(OMPRuntime->getOMPBuilder().emitKernelLaunch(
9674-
CGF.Builder, OutlinedFnID, EmitTargetCallFallbackCB, Args, DeviceID,
9675-
RTLoc, AllocaIP));
9679+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
9680+
OMPRuntime->getOMPBuilder().emitKernelLaunch(
9681+
CGF.Builder, OutlinedFnID, EmitTargetCallFallbackCB, Args, DeviceID,
9682+
RTLoc, AllocaIP);
9683+
assert(AfterIP && "unexpected error creating kernel launch");
9684+
CGF.Builder.restoreIP(*AfterIP);
96769685
};
96779686

96789687
if (RequiresOuterTask)
@@ -10349,9 +10358,12 @@ void CGOpenMPRuntime::emitTargetDataCalls(
1034910358
InsertPointTy CodeGenIP(CGF.Builder.GetInsertBlock(),
1035010359
CGF.Builder.GetInsertPoint());
1035110360
llvm::OpenMPIRBuilder::LocationDescription OmpLoc(CodeGenIP);
10352-
CGF.Builder.restoreIP(OMPBuilder.createTargetData(
10353-
OmpLoc, AllocaIP, CodeGenIP, DeviceID, IfCondVal, Info, GenMapInfoCB,
10354-
/*MapperFunc=*/nullptr, BodyCB, DeviceAddrCB, CustomMapperCB, RTLoc));
10361+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
10362+
OMPBuilder.createTargetData(
10363+
OmpLoc, AllocaIP, CodeGenIP, DeviceID, IfCondVal, Info, GenMapInfoCB,
10364+
/*MapperFunc=*/nullptr, BodyCB, DeviceAddrCB, CustomMapperCB, RTLoc);
10365+
assert(AfterIP && "unexpected error creating target data");
10366+
CGF.Builder.restoreIP(*AfterIP);
1035510367
}
1035610368

1035710369
void CGOpenMPRuntime::emitTargetDataStandAloneCall(

clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,11 +1753,14 @@ void CGOpenMPRuntimeGPU::emitReduction(
17531753
Idx++;
17541754
}
17551755

1756-
CGF.Builder.restoreIP(OMPBuilder.createReductionsGPU(
1757-
OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
1758-
DistributeReduction, llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
1759-
CGF.getTarget().getGridValue(), C.getLangOpts().OpenMPCUDAReductionBufNum,
1760-
RTLoc));
1756+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
1757+
OMPBuilder.createReductionsGPU(
1758+
OmpLoc, AllocaIP, CodeGenIP, ReductionInfos, false, TeamsReduction,
1759+
DistributeReduction, llvm::OpenMPIRBuilder::ReductionGenCBKind::Clang,
1760+
CGF.getTarget().getGridValue(),
1761+
C.getLangOpts().OpenMPCUDAReductionBufNum, RTLoc);
1762+
assert(AfterIP && "unexpected error creating GPU reductions");
1763+
CGF.Builder.restoreIP(*AfterIP);
17611764
return;
17621765
}
17631766

clang/lib/CodeGen/CGStmtOpenMP.cpp

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,7 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
18091809
// thus calls destructors etc.
18101810
auto FiniCB = [this](InsertPointTy IP) {
18111811
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
1812+
return llvm::Error::success();
18121813
};
18131814

18141815
// Privatization callback that performs appropriate action for
@@ -1831,15 +1832,18 @@ void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) {
18311832
InsertPointTy CodeGenIP) {
18321833
OMPBuilderCBHelpers::EmitOMPOutlinedRegionBody(
18331834
*this, ParallelRegionBodyStmt, AllocaIP, CodeGenIP, "parallel");
1835+
return llvm::Error::success();
18341836
};
18351837

18361838
CGCapturedStmtInfo CGSI(*CS, CR_OpenMP);
18371839
CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI);
18381840
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP(
18391841
AllocaInsertPt->getParent(), AllocaInsertPt->getIterator());
1840-
Builder.restoreIP(
1842+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
18411843
OMPBuilder.createParallel(Builder, AllocaIP, BodyGenCB, PrivCB, FiniCB,
1842-
IfCond, NumThreads, ProcBind, S.hasCancel()));
1844+
IfCond, NumThreads, ProcBind, S.hasCancel());
1845+
assert(AfterIP && "unexpected error creating parallel");
1846+
Builder.restoreIP(*AfterIP);
18431847
return;
18441848
}
18451849

@@ -2128,9 +2132,13 @@ void CodeGenFunction::EmitOMPCanonicalLoop(const OMPCanonicalLoop *S) {
21282132

21292133
RunCleanupsScope BodyScope(*this);
21302134
EmitStmt(BodyStmt);
2135+
return llvm::Error::success();
21312136
};
2132-
llvm::CanonicalLoopInfo *CL =
2137+
2138+
llvm::Expected<llvm::CanonicalLoopInfo *> Result =
21332139
OMPBuilder.createCanonicalLoop(Builder, BodyGen, DistVal);
2140+
assert(Result && "unexpected error creating canonical loop");
2141+
llvm::CanonicalLoopInfo *CL = *Result;
21342142

21352143
// Finish up the loop.
21362144
Builder.restoreIP(CL->getAfterIP());
@@ -4016,11 +4024,13 @@ static void emitOMPForDirective(const OMPLoopDirective &S, CodeGenFunction &CGF,
40164024
CGM.getOpenMPRuntime().getOMPBuilder();
40174025
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP(
40184026
CGF.AllocaInsertPt->getParent(), CGF.AllocaInsertPt->getIterator());
4019-
OMPBuilder.applyWorkshareLoop(
4020-
CGF.Builder.getCurrentDebugLocation(), CLI, AllocaIP, NeedsBarrier,
4021-
SchedKind, ChunkSize, /*HasSimdModifier=*/false,
4022-
/*HasMonotonicModifier=*/false, /*HasNonmonotonicModifier=*/false,
4023-
/*HasOrderedClause=*/false);
4027+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4028+
OMPBuilder.applyWorkshareLoop(
4029+
CGF.Builder.getCurrentDebugLocation(), CLI, AllocaIP,
4030+
NeedsBarrier, SchedKind, ChunkSize, /*HasSimdModifier=*/false,
4031+
/*HasMonotonicModifier=*/false, /*HasNonmonotonicModifier=*/false,
4032+
/*HasOrderedClause=*/false);
4033+
assert(AfterIP && "unexpected error creating workshare loop");
40244034
return;
40254035
}
40264036

@@ -4257,6 +4267,7 @@ void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
42574267

42584268
auto FiniCB = [this](InsertPointTy IP) {
42594269
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
4270+
return llvm::Error::success();
42604271
};
42614272

42624273
const CapturedStmt *ICS = S.getInnermostCapturedStmt();
@@ -4269,6 +4280,7 @@ void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
42694280
InsertPointTy CodeGenIP) {
42704281
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
42714282
*this, SubStmt, AllocaIP, CodeGenIP, "section");
4283+
return llvm::Error::success();
42724284
};
42734285
SectionCBVector.push_back(SectionCB);
42744286
}
@@ -4277,6 +4289,7 @@ void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
42774289
InsertPointTy CodeGenIP) {
42784290
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
42794291
*this, CapturedStmt, AllocaIP, CodeGenIP, "section");
4292+
return llvm::Error::success();
42804293
};
42814294
SectionCBVector.push_back(SectionCB);
42824295
}
@@ -4298,9 +4311,12 @@ void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) {
42984311
CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI);
42994312
llvm::OpenMPIRBuilder::InsertPointTy AllocaIP(
43004313
AllocaInsertPt->getParent(), AllocaInsertPt->getIterator());
4301-
Builder.restoreIP(OMPBuilder.createSections(
4302-
Builder, AllocaIP, SectionCBVector, PrivCB, FiniCB, S.hasCancel(),
4303-
S.getSingleClause<OMPNowaitClause>()));
4314+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4315+
OMPBuilder.createSections(Builder, AllocaIP, SectionCBVector, PrivCB,
4316+
FiniCB, S.hasCancel(),
4317+
S.getSingleClause<OMPNowaitClause>());
4318+
assert(AfterIP && "unexpected error creating sections");
4319+
Builder.restoreIP(*AfterIP);
43044320
return;
43054321
}
43064322
{
@@ -4326,17 +4342,22 @@ void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) {
43264342
const Stmt *SectionRegionBodyStmt = S.getAssociatedStmt();
43274343
auto FiniCB = [this](InsertPointTy IP) {
43284344
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
4345+
return llvm::Error::success();
43294346
};
43304347

43314348
auto BodyGenCB = [SectionRegionBodyStmt, this](InsertPointTy AllocaIP,
43324349
InsertPointTy CodeGenIP) {
43334350
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
43344351
*this, SectionRegionBodyStmt, AllocaIP, CodeGenIP, "section");
4352+
return llvm::Error::success();
43354353
};
43364354

43374355
LexicalScope Scope(*this, S.getSourceRange());
43384356
EmitStopPoint(&S);
4339-
Builder.restoreIP(OMPBuilder.createSection(Builder, BodyGenCB, FiniCB));
4357+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4358+
OMPBuilder.createSection(Builder, BodyGenCB, FiniCB);
4359+
assert(AfterIP && "unexpected error creating section");
4360+
Builder.restoreIP(*AfterIP);
43404361

43414362
return;
43424363
}
@@ -4407,17 +4428,22 @@ void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) {
44074428

44084429
auto FiniCB = [this](InsertPointTy IP) {
44094430
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
4431+
return llvm::Error::success();
44104432
};
44114433

44124434
auto BodyGenCB = [MasterRegionBodyStmt, this](InsertPointTy AllocaIP,
44134435
InsertPointTy CodeGenIP) {
44144436
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
44154437
*this, MasterRegionBodyStmt, AllocaIP, CodeGenIP, "master");
4438+
return llvm::Error::success();
44164439
};
44174440

44184441
LexicalScope Scope(*this, S.getSourceRange());
44194442
EmitStopPoint(&S);
4420-
Builder.restoreIP(OMPBuilder.createMaster(Builder, BodyGenCB, FiniCB));
4443+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4444+
OMPBuilder.createMaster(Builder, BodyGenCB, FiniCB);
4445+
assert(AfterIP && "unexpected error creating master");
4446+
Builder.restoreIP(*AfterIP);
44214447

44224448
return;
44234449
}
@@ -4453,18 +4479,22 @@ void CodeGenFunction::EmitOMPMaskedDirective(const OMPMaskedDirective &S) {
44534479

44544480
auto FiniCB = [this](InsertPointTy IP) {
44554481
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
4482+
return llvm::Error::success();
44564483
};
44574484

44584485
auto BodyGenCB = [MaskedRegionBodyStmt, this](InsertPointTy AllocaIP,
44594486
InsertPointTy CodeGenIP) {
44604487
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
44614488
*this, MaskedRegionBodyStmt, AllocaIP, CodeGenIP, "masked");
4489+
return llvm::Error::success();
44624490
};
44634491

44644492
LexicalScope Scope(*this, S.getSourceRange());
44654493
EmitStopPoint(&S);
4466-
Builder.restoreIP(
4467-
OMPBuilder.createMasked(Builder, BodyGenCB, FiniCB, FilterVal));
4494+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4495+
OMPBuilder.createMasked(Builder, BodyGenCB, FiniCB, FilterVal);
4496+
assert(AfterIP && "unexpected error creating masked");
4497+
Builder.restoreIP(*AfterIP);
44684498

44694499
return;
44704500
}
@@ -4493,19 +4523,23 @@ void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) {
44934523

44944524
auto FiniCB = [this](InsertPointTy IP) {
44954525
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
4526+
return llvm::Error::success();
44964527
};
44974528

44984529
auto BodyGenCB = [CriticalRegionBodyStmt, this](InsertPointTy AllocaIP,
44994530
InsertPointTy CodeGenIP) {
45004531
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
45014532
*this, CriticalRegionBodyStmt, AllocaIP, CodeGenIP, "critical");
4533+
return llvm::Error::success();
45024534
};
45034535

45044536
LexicalScope Scope(*this, S.getSourceRange());
45054537
EmitStopPoint(&S);
4506-
Builder.restoreIP(OMPBuilder.createCritical(
4507-
Builder, BodyGenCB, FiniCB, S.getDirectiveName().getAsString(),
4508-
HintInst));
4538+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
4539+
OMPBuilder.createCritical(Builder, BodyGenCB, FiniCB,
4540+
S.getDirectiveName().getAsString(), HintInst);
4541+
assert(AfterIP && "unexpected error creating critical");
4542+
Builder.restoreIP(*AfterIP);
45094543

45104544
return;
45114545
}
@@ -5464,11 +5498,15 @@ void CodeGenFunction::EmitOMPTaskgroupDirective(
54645498
InsertPointTy CodeGenIP) {
54655499
Builder.restoreIP(CodeGenIP);
54665500
EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt());
5501+
return llvm::Error::success();
54675502
};
54685503
CodeGenFunction::CGCapturedStmtInfo CapStmtInfo;
54695504
if (!CapturedStmtInfo)
54705505
CapturedStmtInfo = &CapStmtInfo;
5471-
Builder.restoreIP(OMPBuilder.createTaskgroup(Builder, AllocaIP, BodyGenCB));
5506+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
5507+
OMPBuilder.createTaskgroup(Builder, AllocaIP, BodyGenCB);
5508+
assert(AfterIP && "unexpected error creating taskgroup");
5509+
Builder.restoreIP(*AfterIP);
54725510
return;
54735511
}
54745512
auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) {
@@ -6041,6 +6079,7 @@ void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
60416079

60426080
auto FiniCB = [this](InsertPointTy IP) {
60436081
OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP);
6082+
return llvm::Error::success();
60446083
};
60456084

60466085
auto BodyGenCB = [&S, C, this](InsertPointTy AllocaIP,
@@ -6064,11 +6103,14 @@ void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) {
60646103
OMPBuilderCBHelpers::EmitOMPInlinedRegionBody(
60656104
*this, CS->getCapturedStmt(), AllocaIP, CodeGenIP, "ordered");
60666105
}
6106+
return llvm::Error::success();
60676107
};
60686108

60696109
OMPLexicalScope Scope(*this, S, OMPD_unknown);
6070-
Builder.restoreIP(
6071-
OMPBuilder.createOrderedThreadsSimd(Builder, BodyGenCB, FiniCB, !C));
6110+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
6111+
OMPBuilder.createOrderedThreadsSimd(Builder, BodyGenCB, FiniCB, !C);
6112+
assert(AfterIP && "unexpected error creating ordered");
6113+
Builder.restoreIP(*AfterIP);
60726114
}
60736115
return;
60746116
}
@@ -7344,8 +7386,10 @@ void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) {
73447386
if (IfCond)
73457387
IfCondition = EmitScalarExpr(IfCond,
73467388
/*IgnoreResultAssign=*/true);
7347-
return Builder.restoreIP(
7348-
OMPBuilder.createCancel(Builder, IfCondition, S.getCancelRegion()));
7389+
llvm::OpenMPIRBuilder::InsertPointOrErrorTy AfterIP =
7390+
OMPBuilder.createCancel(Builder, IfCondition, S.getCancelRegion());
7391+
assert(AfterIP && "unexpected error creating cancel");
7392+
return Builder.restoreIP(*AfterIP);
73497393
}
73507394
}
73517395

0 commit comments

Comments
 (0)