Skip to content

Commit 5b4e5f8

Browse files
[OpenMPIRBuilder][Clang][NFC] - Combine emitOffloadingArrays and emitOffloadingArraysArgument in OpenMPIRBuilder (#97088)
This patch introduces a new interface in `OpenMPIRBuilder` that combines the creation of the so-called offloading pointer arrays and their subsequent preparation as arguments to the OpenMP runtime library. We then use this in Clang. This is intended to be used in the near future by other frontends such as Flang when lowering MLIR to LLVMIR.
1 parent c9e5af3 commit 5b4e5f8

File tree

4 files changed

+112
-78
lines changed

4 files changed

+112
-78
lines changed

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 71 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8867,36 +8867,21 @@ emitMappingInformation(CodeGenFunction &CGF, llvm::OpenMPIRBuilder &OMPBuilder,
88678867
PLoc.getLine(), PLoc.getColumn(),
88688868
SrcLocStrSize);
88698869
}
8870-
88718870
/// Emit the arrays used to pass the captures and map information to the
88728871
/// offloading runtime library. If there is no map or capture information,
88738872
/// return nullptr by reference.
8874-
static void emitOffloadingArrays(
8873+
static void emitOffloadingArraysAndArgs(
88758874
CodeGenFunction &CGF, MappableExprsHandler::MapCombinedInfoTy &CombinedInfo,
88768875
CGOpenMPRuntime::TargetDataInfo &Info, llvm::OpenMPIRBuilder &OMPBuilder,
8877-
bool IsNonContiguous = false) {
8876+
bool IsNonContiguous = false, bool ForEndCall = false) {
88788877
CodeGenModule &CGM = CGF.CGM;
88798878

8880-
// Reset the array information.
8881-
Info.clearArrayInfo();
8882-
Info.NumberOfPtrs = CombinedInfo.BasePointers.size();
8883-
88848879
using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy;
88858880
InsertPointTy AllocaIP(CGF.AllocaInsertPt->getParent(),
88868881
CGF.AllocaInsertPt->getIterator());
88878882
InsertPointTy CodeGenIP(CGF.Builder.GetInsertBlock(),
88888883
CGF.Builder.GetInsertPoint());
88898884

8890-
auto FillInfoMap = [&](MappableExprsHandler::MappingExprInfo &MapExpr) {
8891-
return emitMappingInformation(CGF, OMPBuilder, MapExpr);
8892-
};
8893-
if (CGM.getCodeGenOpts().getDebugInfo() !=
8894-
llvm::codegenoptions::NoDebugInfo) {
8895-
CombinedInfo.Names.resize(CombinedInfo.Exprs.size());
8896-
llvm::transform(CombinedInfo.Exprs, CombinedInfo.Names.begin(),
8897-
FillInfoMap);
8898-
}
8899-
89008885
auto DeviceAddrCB = [&](unsigned int I, llvm::Value *NewDecl) {
89018886
if (const ValueDecl *DevVD = CombinedInfo.DevicePtrDecls[I]) {
89028887
Info.CaptureDeviceAddrMap.try_emplace(DevVD, NewDecl);
@@ -8907,14 +8892,14 @@ static void emitOffloadingArrays(
89078892
llvm::Value *MFunc = nullptr;
89088893
if (CombinedInfo.Mappers[I]) {
89098894
Info.HasMapper = true;
8910-
MFunc = CGF.CGM.getOpenMPRuntime().getOrCreateUserDefinedMapperFunc(
8895+
MFunc = CGM.getOpenMPRuntime().getOrCreateUserDefinedMapperFunc(
89118896
cast<OMPDeclareMapperDecl>(CombinedInfo.Mappers[I]));
89128897
}
89138898
return MFunc;
89148899
};
8915-
OMPBuilder.emitOffloadingArrays(AllocaIP, CodeGenIP, CombinedInfo, Info,
8916-
/*IsNonContiguous=*/true, DeviceAddrCB,
8917-
CustomMapperCB);
8900+
OMPBuilder.emitOffloadingArraysAndArgs(
8901+
AllocaIP, CodeGenIP, Info, Info.RTArgs, CombinedInfo, IsNonContiguous,
8902+
ForEndCall, DeviceAddrCB, CustomMapperCB);
89188903
}
89198904

89208905
/// Check for inner distribute directive.
@@ -9479,29 +9464,14 @@ llvm::Value *emitDynCGGroupMem(const OMPExecutableDirective &D,
94799464
}
94809465
return DynCGroupMem;
94819466
}
9467+
static void genMapInfoForCaptures(
9468+
MappableExprsHandler &MEHandler, CodeGenFunction &CGF,
9469+
const CapturedStmt &CS, llvm::SmallVectorImpl<llvm::Value *> &CapturedVars,
9470+
llvm::OpenMPIRBuilder &OMPBuilder,
9471+
llvm::DenseSet<CanonicalDeclPtr<const Decl>> &MappedVarSet,
9472+
MappableExprsHandler::MapCombinedInfoTy &CombinedInfo) {
94829473

9483-
static void emitTargetCallKernelLaunch(
9484-
CGOpenMPRuntime *OMPRuntime, llvm::Function *OutlinedFn,
9485-
const OMPExecutableDirective &D,
9486-
llvm::SmallVectorImpl<llvm::Value *> &CapturedVars, bool RequiresOuterTask,
9487-
const CapturedStmt &CS, bool OffloadingMandatory,
9488-
llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
9489-
llvm::Value *OutlinedFnID, CodeGenFunction::OMPTargetDataInfo &InputInfo,
9490-
llvm::Value *&MapTypesArray, llvm::Value *&MapNamesArray,
9491-
llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
9492-
const OMPLoopDirective &D)>
9493-
SizeEmitter,
9494-
CodeGenFunction &CGF, CodeGenModule &CGM) {
9495-
llvm::OpenMPIRBuilder &OMPBuilder = OMPRuntime->getOMPBuilder();
9496-
9497-
// Fill up the arrays with all the captured variables.
9498-
MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
9499-
9500-
// Get mappable expression information.
9501-
MappableExprsHandler MEHandler(D, CGF);
95029474
llvm::DenseMap<llvm::Value *, llvm::Value *> LambdaPointers;
9503-
llvm::DenseSet<CanonicalDeclPtr<const Decl>> MappedVarSet;
9504-
95059475
auto RI = CS.getCapturedRecordDecl()->field_begin();
95069476
auto *CV = CapturedVars.begin();
95079477
for (CapturedStmt::const_capture_iterator CI = CS.capture_begin(),
@@ -9568,18 +9538,64 @@ static void emitTargetCallKernelLaunch(
95689538
MEHandler.adjustMemberOfForLambdaCaptures(
95699539
OMPBuilder, LambdaPointers, CombinedInfo.BasePointers,
95709540
CombinedInfo.Pointers, CombinedInfo.Types);
9541+
}
9542+
static void
9543+
genMapInfo(MappableExprsHandler &MEHandler, CodeGenFunction &CGF,
9544+
MappableExprsHandler::MapCombinedInfoTy &CombinedInfo,
9545+
llvm::OpenMPIRBuilder &OMPBuilder,
9546+
const llvm::DenseSet<CanonicalDeclPtr<const Decl>> &SkippedVarSet =
9547+
llvm::DenseSet<CanonicalDeclPtr<const Decl>>()) {
9548+
9549+
CodeGenModule &CGM = CGF.CGM;
95719550
// Map any list items in a map clause that were not captures because they
95729551
// weren't referenced within the construct.
9573-
MEHandler.generateAllInfo(CombinedInfo, OMPBuilder, MappedVarSet);
9552+
MEHandler.generateAllInfo(CombinedInfo, OMPBuilder, SkippedVarSet);
9553+
9554+
auto FillInfoMap = [&](MappableExprsHandler::MappingExprInfo &MapExpr) {
9555+
return emitMappingInformation(CGF, OMPBuilder, MapExpr);
9556+
};
9557+
if (CGM.getCodeGenOpts().getDebugInfo() !=
9558+
llvm::codegenoptions::NoDebugInfo) {
9559+
CombinedInfo.Names.resize(CombinedInfo.Exprs.size());
9560+
llvm::transform(CombinedInfo.Exprs, CombinedInfo.Names.begin(),
9561+
FillInfoMap);
9562+
}
9563+
}
95749564

9565+
static void genMapInfo(const OMPExecutableDirective &D, CodeGenFunction &CGF,
9566+
const CapturedStmt &CS,
9567+
llvm::SmallVectorImpl<llvm::Value *> &CapturedVars,
9568+
llvm::OpenMPIRBuilder &OMPBuilder,
9569+
MappableExprsHandler::MapCombinedInfoTy &CombinedInfo) {
9570+
// Get mappable expression information.
9571+
MappableExprsHandler MEHandler(D, CGF);
9572+
llvm::DenseSet<CanonicalDeclPtr<const Decl>> MappedVarSet;
9573+
9574+
genMapInfoForCaptures(MEHandler, CGF, CS, CapturedVars, OMPBuilder,
9575+
MappedVarSet, CombinedInfo);
9576+
genMapInfo(MEHandler, CGF, CombinedInfo, OMPBuilder, MappedVarSet);
9577+
}
9578+
static void emitTargetCallKernelLaunch(
9579+
CGOpenMPRuntime *OMPRuntime, llvm::Function *OutlinedFn,
9580+
const OMPExecutableDirective &D,
9581+
llvm::SmallVectorImpl<llvm::Value *> &CapturedVars, bool RequiresOuterTask,
9582+
const CapturedStmt &CS, bool OffloadingMandatory,
9583+
llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device,
9584+
llvm::Value *OutlinedFnID, CodeGenFunction::OMPTargetDataInfo &InputInfo,
9585+
llvm::Value *&MapTypesArray, llvm::Value *&MapNamesArray,
9586+
llvm::function_ref<llvm::Value *(CodeGenFunction &CGF,
9587+
const OMPLoopDirective &D)>
9588+
SizeEmitter,
9589+
CodeGenFunction &CGF, CodeGenModule &CGM) {
9590+
llvm::OpenMPIRBuilder &OMPBuilder = OMPRuntime->getOMPBuilder();
9591+
9592+
// Fill up the arrays with all the captured variables.
9593+
MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
95759594
CGOpenMPRuntime::TargetDataInfo Info;
9576-
// Fill up the arrays and create the arguments.
9577-
emitOffloadingArrays(CGF, CombinedInfo, Info, OMPBuilder);
9578-
bool EmitDebug = CGF.CGM.getCodeGenOpts().getDebugInfo() !=
9579-
llvm::codegenoptions::NoDebugInfo;
9580-
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, Info.RTArgs, Info,
9581-
EmitDebug,
9582-
/*ForEndCall=*/false);
9595+
genMapInfo(D, CGF, CS, CapturedVars, OMPBuilder, CombinedInfo);
9596+
9597+
emitOffloadingArraysAndArgs(CGF, CombinedInfo, Info, OMPBuilder,
9598+
/*IsNonContiguous=*/true, /*ForEndCall=*/false);
95839599

95849600
InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
95859601
InputInfo.BasePointersArray = Address(Info.RTArgs.BasePointersArray,
@@ -10474,22 +10490,15 @@ void CGOpenMPRuntime::emitTargetDataStandAloneCall(
1047410490
PrePostActionTy &) {
1047510491
// Fill up the arrays with all the mapped variables.
1047610492
MappableExprsHandler::MapCombinedInfoTy CombinedInfo;
10477-
10478-
// Get map clause information.
10493+
CGOpenMPRuntime::TargetDataInfo Info;
1047910494
MappableExprsHandler MEHandler(D, CGF);
10480-
MEHandler.generateAllInfo(CombinedInfo, OMPBuilder);
10495+
genMapInfo(MEHandler, CGF, CombinedInfo, OMPBuilder);
10496+
emitOffloadingArraysAndArgs(CGF, CombinedInfo, Info, OMPBuilder,
10497+
/*IsNonContiguous=*/true, /*ForEndCall=*/false);
1048110498

10482-
CGOpenMPRuntime::TargetDataInfo Info;
10483-
// Fill up the arrays and create the arguments.
10484-
emitOffloadingArrays(CGF, CombinedInfo, Info, OMPBuilder,
10485-
/*IsNonContiguous=*/true);
1048610499
bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>() ||
1048710500
D.hasClausesOfKind<OMPNowaitClause>();
10488-
bool EmitDebug = CGF.CGM.getCodeGenOpts().getDebugInfo() !=
10489-
llvm::codegenoptions::NoDebugInfo;
10490-
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, Info.RTArgs, Info,
10491-
EmitDebug,
10492-
/*ForEndCall=*/false);
10501+
1049310502
InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
1049410503
InputInfo.BasePointersArray = Address(Info.RTArgs.BasePointersArray,
1049510504
CGF.VoidPtrTy, CGM.getPointerAlign());

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,6 +2231,8 @@ class OpenMPIRBuilder {
22312231
/// The total number of pointers passed to the runtime library.
22322232
unsigned NumberOfPtrs = 0u;
22332233

2234+
bool EmitDebug = false;
2235+
22342236
explicit TargetDataInfo() {}
22352237
explicit TargetDataInfo(bool RequiresDevicePointerInfo,
22362238
bool SeparateBeginEndCalls)
@@ -2349,7 +2351,6 @@ class OpenMPIRBuilder {
23492351
void emitOffloadingArraysArgument(IRBuilderBase &Builder,
23502352
OpenMPIRBuilder::TargetDataRTArgs &RTArgs,
23512353
OpenMPIRBuilder::TargetDataInfo &Info,
2352-
bool EmitDebug = false,
23532354
bool ForEndCall = false);
23542355

23552356
/// Emit an array of struct descriptors to be assigned to the offload args.
@@ -2360,13 +2361,28 @@ class OpenMPIRBuilder {
23602361

23612362
/// Emit the arrays used to pass the captures and map information to the
23622363
/// offloading runtime library. If there is no map or capture information,
2363-
/// return nullptr by reference.
2364+
/// return nullptr by reference. Accepts a reference to a MapInfosTy object
2365+
/// that contains information generated for mappable clauses,
2366+
/// including base pointers, pointers, sizes, map types, user-defined mappers.
23642367
void emitOffloadingArrays(
23652368
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, MapInfosTy &CombinedInfo,
23662369
TargetDataInfo &Info, bool IsNonContiguous = false,
23672370
function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
23682371
function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
23692372

2373+
/// Allocates memory for and populates the arrays required for offloading
2374+
/// (offload_{baseptrs|ptrs|mappers|sizes|maptypes|mapnames}). Then, it
2375+
/// emits their base addresses as arguments to be passed to the runtime
2376+
/// library. In essence, this function is a combination of
2377+
/// emitOffloadingArrays and emitOffloadingArraysArgument and should arguably
2378+
/// be preferred by clients of OpenMPIRBuilder.
2379+
void emitOffloadingArraysAndArgs(
2380+
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info,
2381+
TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo,
2382+
bool IsNonContiguous = false, bool ForEndCall = false,
2383+
function_ref<void(unsigned int, Value *)> DeviceAddrCB = nullptr,
2384+
function_ref<Value *(unsigned int)> CustomMapperCB = nullptr);
2385+
23702386
/// Creates offloading entry for the provided entry ID \a ID, address \a
23712387
/// Addr, size \a Size, and flags \a Flags.
23722388
void createOffloadEntry(Constant *ID, Constant *Addr, uint64_t Size,

llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6368,8 +6368,7 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData(
63686368
CustomMapperCB);
63696369

63706370
TargetDataRTArgs RTArgs;
6371-
emitOffloadingArraysArgument(Builder, RTArgs, Info,
6372-
!MapInfo->Names.empty());
6371+
emitOffloadingArraysArgument(Builder, RTArgs, Info);
63736372

63746373
// Emit the number of elements in the offloading arrays.
63756374
Value *PointerNum = Builder.getInt32(Info.NumberOfPtrs);
@@ -6422,8 +6421,8 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetData(
64226421
// Generate code for the closing of the data region.
64236422
auto EndThenGen = [&](InsertPointTy AllocaIP, InsertPointTy CodeGenIP) {
64246423
TargetDataRTArgs RTArgs;
6425-
emitOffloadingArraysArgument(Builder, RTArgs, Info, !MapInfo->Names.empty(),
6426-
/*ForEndCall=*/true);
6424+
Info.EmitDebug = !MapInfo->Names.empty();
6425+
emitOffloadingArraysArgument(Builder, RTArgs, Info, /*ForEndCall=*/true);
64276426

64286427
// Emit the number of elements in the offloading arrays.
64296428
Value *PointerNum = Builder.getInt32(Info.NumberOfPtrs);
@@ -7053,6 +7052,16 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::emitTargetTask(
70537052
<< "\n");
70547053
return Builder.saveIP();
70557054
}
7055+
void OpenMPIRBuilder::emitOffloadingArraysAndArgs(
7056+
InsertPointTy AllocaIP, InsertPointTy CodeGenIP, TargetDataInfo &Info,
7057+
TargetDataRTArgs &RTArgs, MapInfosTy &CombinedInfo, bool IsNonContiguous,
7058+
bool ForEndCall, function_ref<void(unsigned int, Value *)> DeviceAddrCB,
7059+
function_ref<Value *(unsigned int)> CustomMapperCB) {
7060+
emitOffloadingArrays(AllocaIP, CodeGenIP, CombinedInfo, Info, IsNonContiguous,
7061+
DeviceAddrCB, CustomMapperCB);
7062+
emitOffloadingArraysArgument(Builder, RTArgs, Info, ForEndCall);
7063+
}
7064+
70567065
static void emitTargetCall(
70577066
OpenMPIRBuilder &OMPBuilder, IRBuilderBase &Builder,
70587067
OpenMPIRBuilder::InsertPointTy AllocaIP, Function *OutlinedFn,
@@ -7066,12 +7075,11 @@ static void emitTargetCall(
70667075
/*SeparateBeginEndCalls=*/true);
70677076

70687077
OpenMPIRBuilder::MapInfosTy &MapInfo = GenMapInfoCB(Builder.saveIP());
7069-
OMPBuilder.emitOffloadingArrays(AllocaIP, Builder.saveIP(), MapInfo, Info,
7070-
/*IsNonContiguous=*/true);
7071-
70727078
OpenMPIRBuilder::TargetDataRTArgs RTArgs;
7073-
OMPBuilder.emitOffloadingArraysArgument(Builder, RTArgs, Info,
7074-
!MapInfo.Names.empty());
7079+
OMPBuilder.emitOffloadingArraysAndArgs(AllocaIP, Builder.saveIP(), Info,
7080+
RTArgs, MapInfo,
7081+
/*IsNonContiguous=*/true,
7082+
/*ForEndCall=*/false);
70757083

70767084
// emitKernelLaunch
70777085
auto &&EmitTargetCallFallbackCB =
@@ -7081,7 +7089,7 @@ static void emitTargetCall(
70817089
return Builder.saveIP();
70827090
};
70837091

7084-
unsigned NumTargetItems = MapInfo.BasePointers.size();
7092+
unsigned NumTargetItems = Info.NumberOfPtrs;
70857093
// TODO: Use correct device ID
70867094
Value *DeviceID = Builder.getInt64(OMP_DEVICEID_UNDEF);
70877095
Value *NumTeamsVal = Builder.getInt32(NumTeams);
@@ -7275,7 +7283,6 @@ void OpenMPIRBuilder::emitMapperCall(const LocationDescription &Loc,
72757283
void OpenMPIRBuilder::emitOffloadingArraysArgument(IRBuilderBase &Builder,
72767284
TargetDataRTArgs &RTArgs,
72777285
TargetDataInfo &Info,
7278-
bool EmitDebug,
72797286
bool ForEndCall) {
72807287
assert((!ForEndCall || Info.separateBeginEndCalls()) &&
72817288
"expected region end call to runtime only when end call is separate");
@@ -7315,7 +7322,7 @@ void OpenMPIRBuilder::emitOffloadingArraysArgument(IRBuilderBase &Builder,
73157322

73167323
// Only emit the mapper information arrays if debug information is
73177324
// requested.
7318-
if (!EmitDebug)
7325+
if (!Info.EmitDebug)
73197326
RTArgs.MapNamesArray = ConstantPointerNull::get(VoidPtrPtrTy);
73207327
else
73217328
RTArgs.MapNamesArray = Builder.CreateConstInBoundsGEP2_32(
@@ -7504,9 +7511,11 @@ void OpenMPIRBuilder::emitOffloadingArrays(
75047511
auto *MapNamesArrayGbl =
75057512
createOffloadMapnames(CombinedInfo.Names, MapnamesName);
75067513
Info.RTArgs.MapNamesArray = MapNamesArrayGbl;
7514+
Info.EmitDebug = true;
75077515
} else {
75087516
Info.RTArgs.MapNamesArray =
75097517
Constant::getNullValue(PointerType::getUnqual(Builder.getContext()));
7518+
Info.EmitDebug = false;
75107519
}
75117520

75127521
// If there's a present map type modifier, it must not be applied to the end

llvm/unittests/Frontend/OpenMPIRBuilderTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6902,8 +6902,8 @@ TEST_F(OpenMPIRBuilderTest, EmitOffloadingArraysArguments) {
69026902
Info.RTArgs.MappersArray =
69036903
ConstantPointerNull::get(Array4VoidPtrTy->getPointerTo());
69046904
Info.NumberOfPtrs = 4;
6905-
6906-
OMPBuilder.emitOffloadingArraysArgument(Builder, RTArgs, Info, false, false);
6905+
Info.EmitDebug = false;
6906+
OMPBuilder.emitOffloadingArraysArgument(Builder, RTArgs, Info, false);
69076907

69086908
EXPECT_NE(RTArgs.BasePointersArray, nullptr);
69096909
EXPECT_NE(RTArgs.PointersArray, nullptr);

0 commit comments

Comments
 (0)