Skip to content

Commit

Permalink
[OpenMP][OMPIRBuilder] Migrate emitOffloadingArraysArgument from clang
Browse files Browse the repository at this point in the history
This patch moves the emitOffloadingArraysArgument function and
supporting data structures to OpenMPIRBuilder. This will later be used
in flang as well. The TargetDataInfo class was split up into generic
information and clang-specific data, which remain in clang. Further
migration will be done in in the future.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D134662
  • Loading branch information
jsjodin committed Oct 7, 2022
1 parent 14e2592 commit 4627cef
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 200 deletions.
212 changes: 69 additions & 143 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9442,7 +9442,7 @@ static void emitNonContiguousDescriptor(
DimsAddr, CGM.Int8PtrTy, CGM.Int8Ty);
llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.PointersArray, 0, I);
Info.RTArgs.PointersArray, 0, I);
Address PAddr(P, CGM.VoidPtrTy, CGF.getPointerAlign());
CGF.Builder.CreateStore(DAddr.getPointer(), PAddr);
++L;
Expand Down Expand Up @@ -9520,13 +9520,13 @@ static void emitOffloadingArrays(
Ctx.VoidPtrTy, PointerNumAP, nullptr, ArrayType::Normal,
/*IndexTypeQuals=*/0);

Info.BasePointersArray =
Info.RTArgs.BasePointersArray =
CGF.CreateMemTemp(PointerArrayType, ".offload_baseptrs").getPointer();
Info.PointersArray =
Info.RTArgs.PointersArray =
CGF.CreateMemTemp(PointerArrayType, ".offload_ptrs").getPointer();
Address MappersArray =
CGF.CreateMemTemp(PointerArrayType, ".offload_mappers");
Info.MappersArray = MappersArray.getPointer();
Info.RTArgs.MappersArray = MappersArray.getPointer();

// If we don't have any VLA types or other types that require runtime
// evaluation, we can use a constant array for the map sizes, otherwise we
Expand Down Expand Up @@ -9555,7 +9555,7 @@ static void emitOffloadingArrays(
QualType SizeArrayType = Ctx.getConstantArrayType(
Int64Ty, PointerNumAP, nullptr, ArrayType::Normal,
/*IndexTypeQuals=*/0);
Info.SizesArray =
Info.RTArgs.SizesArray =
CGF.CreateMemTemp(SizeArrayType, ".offload_sizes").getPointer();
} else {
auto *SizesArrayInit = llvm::ConstantArray::get(
Expand All @@ -9579,9 +9579,9 @@ static void emitOffloadingArrays(
CGM.getNaturalTypeAlignment(Ctx.getIntTypeForBitwidth(
/*DestWidth=*/64, /*Signed=*/false))),
CGF.getTypeSize(SizeArrayType));
Info.SizesArray = Buffer.getPointer();
Info.RTArgs.SizesArray = Buffer.getPointer();
} else {
Info.SizesArray = SizesArrayGbl;
Info.RTArgs.SizesArray = SizesArrayGbl;
}
}

Expand All @@ -9593,12 +9593,12 @@ static void emitOffloadingArrays(
CGM.getOpenMPRuntime().getName({"offload_maptypes"});
auto *MapTypesArrayGbl =
OMPBuilder.createOffloadMaptypes(Mapping, MaptypesName);
Info.MapTypesArray = MapTypesArrayGbl;
Info.RTArgs.MapTypesArray = MapTypesArrayGbl;

// The information types are only built if there is debug information
// requested.
if (CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo) {
Info.MapNamesArray = llvm::Constant::getNullValue(
Info.RTArgs.MapNamesArray = llvm::Constant::getNullValue(
llvm::Type::getInt8Ty(CGF.Builder.getContext())->getPointerTo());
} else {
auto fillInfoMap = [&](MappableExprsHandler::MappingExprInfo &MapExpr) {
Expand All @@ -9610,7 +9610,7 @@ static void emitOffloadingArrays(
CGM.getOpenMPRuntime().getName({"offload_mapnames"});
auto *MapNamesArrayGbl =
OMPBuilder.createOffloadMapnames(InfoMap, MapnamesName);
Info.MapNamesArray = MapNamesArrayGbl;
Info.RTArgs.MapNamesArray = MapNamesArrayGbl;
}

// If there's a present map type modifier, it must not be applied to the end
Expand All @@ -9626,15 +9626,15 @@ static void emitOffloadingArrays(
if (EndMapTypesDiffer) {
MapTypesArrayGbl =
OMPBuilder.createOffloadMaptypes(Mapping, MaptypesName);
Info.MapTypesArrayEnd = MapTypesArrayGbl;
Info.RTArgs.MapTypesArrayEnd = MapTypesArrayGbl;
}
}

for (unsigned I = 0; I < Info.NumberOfPtrs; ++I) {
llvm::Value *BPVal = *CombinedInfo.BasePointers[I];
llvm::Value *BP = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.BasePointersArray, 0, I);
Info.RTArgs.BasePointersArray, 0, I);
BP = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
BP, BPVal->getType()->getPointerTo(/*AddrSpace=*/0));
Address BPAddr(BP, BPVal->getType(),
Expand All @@ -9649,7 +9649,7 @@ static void emitOffloadingArrays(
llvm::Value *PVal = CombinedInfo.Pointers[I];
llvm::Value *P = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.PointersArray, 0, I);
Info.RTArgs.PointersArray, 0, I);
P = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
P, PVal->getType()->getPointerTo(/*AddrSpace=*/0));
Address PAddr(P, PVal->getType(), Ctx.getTypeAlignInChars(Ctx.VoidPtrTy));
Expand All @@ -9658,7 +9658,7 @@ static void emitOffloadingArrays(
if (RuntimeSizes.test(I)) {
llvm::Value *S = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
Info.SizesArray,
Info.RTArgs.SizesArray,
/*Idx0=*/0,
/*Idx1=*/I);
Address SAddr(S, CGM.Int64Ty, Ctx.getTypeAlignInChars(Int64Ty));
Expand Down Expand Up @@ -9688,76 +9688,6 @@ static void emitOffloadingArrays(
emitNonContiguousDescriptor(CGF, CombinedInfo, Info);
}

namespace {
/// Additional arguments for emitOffloadingArraysArgument function.
struct ArgumentsOptions {
bool ForEndCall = false;
ArgumentsOptions() = default;
ArgumentsOptions(bool ForEndCall) : ForEndCall(ForEndCall) {}
};
} // namespace

/// Emit the arguments to be passed to the runtime library based on the
/// arrays of base pointers, pointers, sizes, map types, and mappers. If
/// ForEndCall, emit map types to be passed for the end of the region instead of
/// the beginning.
static void emitOffloadingArraysArgument(
CodeGenFunction &CGF, llvm::Value *&BasePointersArrayArg,
llvm::Value *&PointersArrayArg, llvm::Value *&SizesArrayArg,
llvm::Value *&MapTypesArrayArg, llvm::Value *&MapNamesArrayArg,
llvm::Value *&MappersArrayArg, CGOpenMPRuntime::TargetDataInfo &Info,
const ArgumentsOptions &Options = ArgumentsOptions()) {
assert((!Options.ForEndCall || Info.separateBeginEndCalls()) &&
"expected region end call to runtime only when end call is separate");
CodeGenModule &CGM = CGF.CGM;
if (Info.NumberOfPtrs) {
BasePointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.BasePointersArray,
/*Idx0=*/0, /*Idx1=*/0);
PointersArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.PointersArray,
/*Idx0=*/0,
/*Idx1=*/0);
SizesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs), Info.SizesArray,
/*Idx0=*/0, /*Idx1=*/0);
MapTypesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.Int64Ty, Info.NumberOfPtrs),
Options.ForEndCall && Info.MapTypesArrayEnd ? Info.MapTypesArrayEnd
: Info.MapTypesArray,
/*Idx0=*/0,
/*Idx1=*/0);

// Only emit the mapper information arrays if debug information is
// requested.
if (CGF.CGM.getCodeGenOpts().getDebugInfo() == codegenoptions::NoDebugInfo)
MapNamesArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
else
MapNamesArrayArg = CGF.Builder.CreateConstInBoundsGEP2_32(
llvm::ArrayType::get(CGM.VoidPtrTy, Info.NumberOfPtrs),
Info.MapNamesArray,
/*Idx0=*/0,
/*Idx1=*/0);
// If there is no user-defined mapper, set the mapper array to nullptr to
// avoid an unnecessary data privatization
if (!Info.HasMapper)
MappersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
else
MappersArrayArg =
CGF.Builder.CreatePointerCast(Info.MappersArray, CGM.VoidPtrPtrTy);
} else {
BasePointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
PointersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
SizesArrayArg = llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
MapTypesArrayArg =
llvm::ConstantPointerNull::get(CGM.Int64Ty->getPointerTo());
MapNamesArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
MappersArrayArg = llvm::ConstantPointerNull::get(CGM.VoidPtrPtrTy);
}
}

/// Check for inner distribute directive.
static const OMPExecutableDirective *
getNestedDistributeDirective(ASTContext &Ctx, const OMPExecutableDirective &D) {
Expand Down Expand Up @@ -10479,25 +10409,26 @@ void CGOpenMPRuntime::emitTargetCall(
// weren't referenced within the construct.
MEHandler.generateAllInfo(CombinedInfo, MappedVarSet);

TargetDataInfo Info;
CGOpenMPRuntime::TargetDataInfo Info;
// Fill up the arrays and create the arguments.
emitOffloadingArrays(CGF, CombinedInfo, Info, OMPBuilder);
emitOffloadingArraysArgument(
CGF, Info.BasePointersArray, Info.PointersArray, Info.SizesArray,
Info.MapTypesArray, Info.MapNamesArray, Info.MappersArray, Info,
{/*ForEndCall=*/false});
bool EmitDebug =
CGF.CGM.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo;
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, Info.RTArgs, Info,
EmitDebug,
/*ForEndCall=*/false);

InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
InputInfo.BasePointersArray =
Address(Info.BasePointersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.PointersArray =
Address(Info.PointersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.BasePointersArray = Address(Info.RTArgs.BasePointersArray,
CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.PointersArray = Address(Info.RTArgs.PointersArray, CGF.VoidPtrTy,
CGM.getPointerAlign());
InputInfo.SizesArray =
Address(Info.SizesArray, CGF.Int64Ty, CGM.getPointerAlign());
Address(Info.RTArgs.SizesArray, CGF.Int64Ty, CGM.getPointerAlign());
InputInfo.MappersArray =
Address(Info.MappersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
MapTypesArray = Info.MapTypesArray;
MapNamesArray = Info.MapNamesArray;
Address(Info.RTArgs.MappersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
MapTypesArray = Info.RTArgs.MapTypesArray;
MapNamesArray = Info.RTArgs.MapNamesArray;
if (RequiresOuterTask)
CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
else
Expand Down Expand Up @@ -11063,7 +10994,8 @@ void CGOpenMPRuntime::emitNumTeamsClause(CodeGenFunction &CGF,

void CGOpenMPRuntime::emitTargetDataCalls(
CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
const Expr *Device, const RegionCodeGenTy &CodeGen,
CGOpenMPRuntime::TargetDataInfo &Info) {
if (!CGF.HaveInsertPoint())
return;

Expand All @@ -11087,15 +11019,11 @@ void CGOpenMPRuntime::emitTargetDataCalls(
emitOffloadingArrays(CGF, CombinedInfo, Info, OMPBuilder,
/*IsNonContiguous=*/true);

llvm::Value *BasePointersArrayArg = nullptr;
llvm::Value *PointersArrayArg = nullptr;
llvm::Value *SizesArrayArg = nullptr;
llvm::Value *MapTypesArrayArg = nullptr;
llvm::Value *MapNamesArrayArg = nullptr;
llvm::Value *MappersArrayArg = nullptr;
emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
SizesArrayArg, MapTypesArrayArg,
MapNamesArrayArg, MappersArrayArg, Info);
llvm::OpenMPIRBuilder::TargetDataRTArgs RTArgs;
bool EmitDebug =
CGF.CGM.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo;
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, RTArgs, Info,
EmitDebug);

// Emit device ID if any.
llvm::Value *DeviceID = nullptr;
Expand All @@ -11115,12 +11043,12 @@ void CGOpenMPRuntime::emitTargetDataCalls(
llvm::Value *OffloadingArgs[] = {RTLoc,
DeviceID,
PointerNum,
BasePointersArrayArg,
PointersArrayArg,
SizesArrayArg,
MapTypesArrayArg,
MapNamesArrayArg,
MappersArrayArg};
RTArgs.BasePointersArray,
RTArgs.PointersArray,
RTArgs.SizesArray,
RTArgs.MapTypesArray,
RTArgs.MapNamesArray,
RTArgs.MappersArray};
CGF.EmitRuntimeCall(
OMPBuilder.getOrCreateRuntimeFunction(
CGM.getModule(), OMPRTL___tgt_target_data_begin_mapper),
Expand All @@ -11137,16 +11065,12 @@ void CGOpenMPRuntime::emitTargetDataCalls(
PrePostActionTy &) {
assert(Info.isValid() && "Invalid data environment closing arguments.");

llvm::Value *BasePointersArrayArg = nullptr;
llvm::Value *PointersArrayArg = nullptr;
llvm::Value *SizesArrayArg = nullptr;
llvm::Value *MapTypesArrayArg = nullptr;
llvm::Value *MapNamesArrayArg = nullptr;
llvm::Value *MappersArrayArg = nullptr;
emitOffloadingArraysArgument(CGF, BasePointersArrayArg, PointersArrayArg,
SizesArrayArg, MapTypesArrayArg,
MapNamesArrayArg, MappersArrayArg, Info,
{/*ForEndCall=*/true});
llvm::OpenMPIRBuilder::TargetDataRTArgs RTArgs;
bool EmitDebug =
CGF.CGM.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo;
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, RTArgs, Info,
EmitDebug,
/*ForEndCall=*/true);

// Emit device ID if any.
llvm::Value *DeviceID = nullptr;
Expand All @@ -11166,12 +11090,12 @@ void CGOpenMPRuntime::emitTargetDataCalls(
llvm::Value *OffloadingArgs[] = {RTLoc,
DeviceID,
PointerNum,
BasePointersArrayArg,
PointersArrayArg,
SizesArrayArg,
MapTypesArrayArg,
MapNamesArrayArg,
MappersArrayArg};
RTArgs.BasePointersArray,
RTArgs.PointersArray,
RTArgs.SizesArray,
RTArgs.MapTypesArray,
RTArgs.MapNamesArray,
RTArgs.MappersArray};
CGF.EmitRuntimeCall(
OMPBuilder.getOrCreateRuntimeFunction(
CGM.getModule(), OMPRTL___tgt_target_data_end_mapper),
Expand Down Expand Up @@ -11360,27 +11284,28 @@ void CGOpenMPRuntime::emitTargetDataStandAloneCall(
MappableExprsHandler MEHandler(D, CGF);
MEHandler.generateAllInfo(CombinedInfo);

TargetDataInfo Info;
CGOpenMPRuntime::TargetDataInfo Info;
// Fill up the arrays and create the arguments.
emitOffloadingArrays(CGF, CombinedInfo, Info, OMPBuilder,
/*IsNonContiguous=*/true);
bool RequiresOuterTask = D.hasClausesOfKind<OMPDependClause>() ||
D.hasClausesOfKind<OMPNowaitClause>();
emitOffloadingArraysArgument(
CGF, Info.BasePointersArray, Info.PointersArray, Info.SizesArray,
Info.MapTypesArray, Info.MapNamesArray, Info.MappersArray, Info,
{/*ForEndCall=*/false});
bool EmitDebug =
CGF.CGM.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo;
OMPBuilder.emitOffloadingArraysArgument(CGF.Builder, Info.RTArgs, Info,
EmitDebug,
/*ForEndCall=*/false);
InputInfo.NumberOfTargetItems = Info.NumberOfPtrs;
InputInfo.BasePointersArray =
Address(Info.BasePointersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.PointersArray =
Address(Info.PointersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.BasePointersArray = Address(Info.RTArgs.BasePointersArray,
CGF.VoidPtrTy, CGM.getPointerAlign());
InputInfo.PointersArray = Address(Info.RTArgs.PointersArray, CGF.VoidPtrTy,
CGM.getPointerAlign());
InputInfo.SizesArray =
Address(Info.SizesArray, CGF.Int64Ty, CGM.getPointerAlign());
Address(Info.RTArgs.SizesArray, CGF.Int64Ty, CGM.getPointerAlign());
InputInfo.MappersArray =
Address(Info.MappersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
MapTypesArray = Info.MapTypesArray;
MapNamesArray = Info.MapNamesArray;
Address(Info.RTArgs.MappersArray, CGF.VoidPtrTy, CGM.getPointerAlign());
MapTypesArray = Info.RTArgs.MapTypesArray;
MapNamesArray = Info.RTArgs.MapNamesArray;
if (RequiresOuterTask)
CGF.EmitOMPTargetTaskBasedDirective(D, ThenGen, InputInfo);
else
Expand Down Expand Up @@ -13075,7 +13000,8 @@ void CGOpenMPSIMDRuntime::emitNumTeamsClause(CodeGenFunction &CGF,

void CGOpenMPSIMDRuntime::emitTargetDataCalls(
CodeGenFunction &CGF, const OMPExecutableDirective &D, const Expr *IfCond,
const Expr *Device, const RegionCodeGenTy &CodeGen, TargetDataInfo &Info) {
const Expr *Device, const RegionCodeGenTy &CodeGen,
CGOpenMPRuntime::TargetDataInfo &Info) {
llvm_unreachable("Not supported in SIMD-only mode");
}

Expand Down
Loading

0 comments on commit 4627cef

Please sign in to comment.