Skip to content

Commit 8f0c362

Browse files
committed
[Flang][OpenMP] Derived type explicit allocatable member mapping
This PR is one of 3 in a PR stack, this is the primary change set which seeks to extend the current derived type explicit member mapping support to handle descriptor member mapping at arbitrary levels of nesting. The PR stack seems to do this reasonably (from testing so far) but as you can create quite complex mappings with derived types (in particular when adding allocatable derived types or arrays of allocatable derived types) I imagine there will be hiccups, which I am more than happy to address. There will also be further extensions to this work to handle the implicit auto-magical mapping of descriptor members in derived types and a few other changes planned for the future (with some ideas on optimizing things). The changes in this PR primarily occur in the OpenMP lowering and the OMPMapInfoFinalization pass. In the OpenMP lowering several utility functions were added or extended to support the generation of appropriate intermediate member mappings which are currently required when the parent (or multiple parents) of a mapped member are descriptor types. We need to map the entirety of these types or do a "deep copy" for lack of a better term, where we map both the base address and the descriptor as without the copying of both of these we lack the information in the case of the descriptor to access the member or attach the pointers data to the pointer and in the latter case we require the base address to map the chunk of data. Currently we do not segment descriptor based derived types as we do with regular non-descriptor derived types, we effectively map their entirety in all cases at the moment, I hope to address this at some point in the future as it adds a fair bit of a performance penalty to having nestings of allocatable derived types as an example. The process of mapping all intermediate descriptor members in a members path only occurs if a member has an allocatable or object parent in its symbol path or the member itself is a member or allocatable. This occurs in the createParentSymAndGenIntermediateMaps function, which will also generate the appropriate address for the allocatable member within the derived type to use as a the varPtr field of the map (for intermediate allocatable maps and final allocatable mappings). In this case it's necessary as we can't utilise the usual Fortran::lower functionality such as gatherDataOperandAddrAndBounds without causing issues later in the lowering due to extra allocas being spawned which seem to affect the pointer attachment (at least this is my current assumption, it results in memory access errors on the device due to incorrect map information generation). This is similar to why we do not use the MLIR value generated for this and utilise the original symbol provided when mapping descriptor types external to derived types. Hopefully this can be rectified in the future so this function can be simplified and more closely aligned to the other type mappings. We also make use of fir::CoordinateOp as opposed to the HLFIR version as the HLFIR version doesn't support the appropriate lowering to FIR necessary at the moment, we also cannot use a single CoordinateOp (similarly to a single GEP) as when we index through a descriptor operation (BoxType) we encounter issues later in the lowering, however in either case we need access to intermediate descriptors so individual CoordinateOp's aid this (although, being able to compress them into a smaller amount of CoordinateOp's may simplify the IR and perhaps result in a better end product, something to consider for the future). The other large change area was in the OMPMapInfoFinalization pass, where the pass had to be extended to support the expansion of box types (or multiple nestings of box types) within derived types, or box type derived types. This requires expanding each BoxType mapping from one into two maps and then modifying all of the existing member indices of the overarching parent mapping to account for the addition of these new members alongside adjusting the existing member indices to support the addition of these new maps which extend the original member indices (as a base address of a box type is currently considered a member of the box type at a position of 0 as when lowered to LLVM-IR it's a pointer contained at this position in the descriptor type, however, this means extending mapped children of this expanded descriptor type to additionally incorporate the new member index in the correct location in its own index list). I believe there is a reasonable amount of comments that should aid in understanding this better, alongside the test alterations for the pass. A subset of the changes were also aimed at making some of the utilities for packing and unpacking the DenseIntElementsAttr containing the member indices shareable across the lowering and OMPMapInfoFinalization, this required moving some functions to the Lower/Support/Utils.h header, and transforming the lowering structure containing the member index data into something more similar to the version used in OMPMapInfoFinalization. There we also some other attempts at tidying things up in relation to the member index data generation in the lowering, some of which required creating a logical operator for the OpenMP ID class so it can be utilised as a map key (it simply utilises the symbol address for the moment as ordering isn't particularly important). Otherwise I have added a set of new tests encompassing some of the mappings currently supported by this PR (unfortunately as you can have arbitrary nestings of all shapes and types it's not very feasible to cover them all).
1 parent 76edf72 commit 8f0c362

20 files changed

+1885
-397
lines changed

flang/include/flang/Optimizer/Builder/FIRBuilder.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
215215
llvm::ArrayRef<mlir::Value> lenParams,
216216
bool asTarget = false);
217217

218+
/// Create a two dimensional ArrayAttr containing integer data as
219+
/// IntegerAttrs, effectively: ArrayAttr<ArrayAttr<IntegerAttr>>>.
220+
mlir::ArrayAttr create2DI64ArrayAttr(
221+
llvm::SmallVectorImpl<llvm::SmallVector<int64_t>> &intData);
222+
218223
/// Create a temporary using `fir.alloca`. This function does not hoist.
219224
/// It is the callers responsibility to set the insertion point if
220225
/// hoisting is required.

flang/lib/Lower/OpenMP/ClauseProcessor.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -881,14 +881,15 @@ void ClauseProcessor::processMapObjects(
881881
lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
882882
const omp::ObjectList &objects,
883883
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits,
884-
std::map<const semantics::Symbol *,
885-
llvm::SmallVector<OmpMapMemberIndicesData>> &parentMemberIndices,
884+
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
886885
llvm::SmallVectorImpl<mlir::Value> &mapVars,
887886
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) const {
888887
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
888+
889889
for (const omp::Object &object : objects) {
890890
llvm::SmallVector<mlir::Value> bounds;
891891
std::stringstream asFortran;
892+
std::optional<omp::Object> parentObj;
892893

893894
lower::AddrAndBoundsInfo info =
894895
lower::gatherDataOperandAddrAndBounds<mlir::omp::MapBoundsOp,
@@ -897,24 +898,43 @@ void ClauseProcessor::processMapObjects(
897898
object.ref(), clauseLocation, asFortran, bounds,
898899
treatIndexAsSection);
899900

901+
mlir::Value baseOp = info.rawInput;
902+
if (object.sym()->owner().IsDerivedType()) {
903+
omp::ObjectList objectList = gatherObjectsOf(object, semaCtx);
904+
assert(!objectList.empty() &&
905+
"could not find parent objects of derived type member");
906+
parentObj = objectList[0];
907+
parentMemberIndices.emplace(parentObj.value(),
908+
OmpMapParentAndMemberData{});
909+
910+
if (isMemberOrParentAllocatableOrPointer(object, semaCtx)) {
911+
llvm::SmallVector<int64_t> indices;
912+
generateMemberPlacementIndices(object, indices, semaCtx);
913+
baseOp = createParentSymAndGenIntermediateMaps(
914+
clauseLocation, converter, semaCtx, stmtCtx, objectList, indices,
915+
parentMemberIndices[parentObj.value()], asFortran.str(),
916+
mapTypeBits);
917+
}
918+
}
919+
900920
// Explicit map captures are captured ByRef by default,
901921
// optimisation passes may alter this to ByCopy or other capture
902922
// types to optimise
903-
mlir::Value baseOp = info.rawInput;
904923
auto location = mlir::NameLoc::get(
905924
mlir::StringAttr::get(firOpBuilder.getContext(), asFortran.str()),
906925
baseOp.getLoc());
907926
mlir::omp::MapInfoOp mapOp = createMapInfoOp(
908927
firOpBuilder, location, baseOp,
909928
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds,
910-
/*members=*/{}, /*membersIndex=*/mlir::DenseIntElementsAttr{},
929+
/*members=*/{}, /*membersIndex=*/mlir::ArrayAttr{},
911930
static_cast<
912931
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
913932
mapTypeBits),
914933
mlir::omp::VariableCaptureKind::ByRef, baseOp.getType());
915934

916-
if (object.sym()->owner().IsDerivedType()) {
917-
addChildIndexAndMapToParent(object, parentMemberIndices, mapOp, semaCtx);
935+
if (parentObj.has_value()) {
936+
parentMemberIndices[parentObj.value()].addChildIndexAndMapToParent(
937+
object, mapOp, semaCtx);
918938
} else {
919939
mapVars.push_back(mapOp);
920940
mapSyms.push_back(object.sym());
@@ -932,9 +952,7 @@ bool ClauseProcessor::processMap(
932952
llvm::SmallVector<const semantics::Symbol *> localMapSyms;
933953
llvm::SmallVectorImpl<const semantics::Symbol *> *ptrMapSyms =
934954
mapSyms ? mapSyms : &localMapSyms;
935-
std::map<const semantics::Symbol *,
936-
llvm::SmallVector<OmpMapMemberIndicesData>>
937-
parentMemberIndices;
955+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
938956

939957
auto process = [&](const omp::clause::Map &clause,
940958
const parser::CharBlock &source) {
@@ -994,17 +1012,15 @@ bool ClauseProcessor::processMap(
9941012
};
9951013

9961014
bool clauseFound = findRepeatableClause<omp::clause::Map>(process);
997-
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars,
998-
*ptrMapSyms);
1015+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
1016+
result.mapVars, *ptrMapSyms);
9991017

10001018
return clauseFound;
10011019
}
10021020

10031021
bool ClauseProcessor::processMotionClauses(lower::StatementContext &stmtCtx,
10041022
mlir::omp::MapClauseOps &result) {
1005-
std::map<const semantics::Symbol *,
1006-
llvm::SmallVector<OmpMapMemberIndicesData>>
1007-
parentMemberIndices;
1023+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
10081024
llvm::SmallVector<const semantics::Symbol *> mapSymbols;
10091025

10101026
auto callbackFn = [&](const auto &clause, const parser::CharBlock &source) {
@@ -1025,8 +1041,9 @@ bool ClauseProcessor::processMotionClauses(lower::StatementContext &stmtCtx,
10251041
clauseFound =
10261042
findRepeatableClause<omp::clause::From>(callbackFn) || clauseFound;
10271043

1028-
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars,
1029-
mapSymbols);
1044+
insertChildMapInfoIntoParent(
1045+
converter, semaCtx, stmtCtx, parentMemberIndices, result.mapVars, mapSymbols);
1046+
10301047
return clauseFound;
10311048
}
10321049

@@ -1088,9 +1105,7 @@ bool ClauseProcessor::processEnter(
10881105
bool ClauseProcessor::processUseDeviceAddr(
10891106
lower::StatementContext &stmtCtx, mlir::omp::UseDeviceAddrClauseOps &result,
10901107
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const {
1091-
std::map<const semantics::Symbol *,
1092-
llvm::SmallVector<OmpMapMemberIndicesData>>
1093-
parentMemberIndices;
1108+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
10941109
bool clauseFound = findRepeatableClause<omp::clause::UseDeviceAddr>(
10951110
[&](const omp::clause::UseDeviceAddr &clause,
10961111
const parser::CharBlock &source) {
@@ -1103,17 +1118,16 @@ bool ClauseProcessor::processUseDeviceAddr(
11031118
useDeviceSyms);
11041119
});
11051120

1106-
insertChildMapInfoIntoParent(converter, parentMemberIndices,
1121+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
11071122
result.useDeviceAddrVars, useDeviceSyms);
11081123
return clauseFound;
11091124
}
11101125

11111126
bool ClauseProcessor::processUseDevicePtr(
11121127
lower::StatementContext &stmtCtx, mlir::omp::UseDevicePtrClauseOps &result,
11131128
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const {
1114-
std::map<const semantics::Symbol *,
1115-
llvm::SmallVector<OmpMapMemberIndicesData>>
1116-
parentMemberIndices;
1129+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
1130+
11171131
bool clauseFound = findRepeatableClause<omp::clause::UseDevicePtr>(
11181132
[&](const omp::clause::UseDevicePtr &clause,
11191133
const parser::CharBlock &source) {
@@ -1126,7 +1140,7 @@ bool ClauseProcessor::processUseDevicePtr(
11261140
useDeviceSyms);
11271141
});
11281142

1129-
insertChildMapInfoIntoParent(converter, parentMemberIndices,
1143+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
11301144
result.useDevicePtrVars, useDeviceSyms);
11311145
return clauseFound;
11321146
}

flang/lib/Lower/OpenMP/ClauseProcessor.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,7 @@ class ClauseProcessor {
166166
lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
167167
const omp::ObjectList &objects,
168168
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits,
169-
std::map<const semantics::Symbol *,
170-
llvm::SmallVector<OmpMapMemberIndicesData>> &parentMemberIndices,
169+
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
171170
llvm::SmallVectorImpl<mlir::Value> &mapVars,
172171
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) const;
173172

flang/lib/Lower/OpenMP/Clauses.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ struct IdTyTemplate {
5151
return designator == other.designator;
5252
}
5353

54+
// Defining an "ordering" which allows types derived from this to be
55+
// utilised in maps and other containers that require comparison
56+
// operators for ordering
57+
bool operator<(const IdTyTemplate &other) const {
58+
return symbol < other.symbol;
59+
}
60+
5461
operator bool() const { return symbol != nullptr; }
5562
};
5663

@@ -72,6 +79,10 @@ struct ObjectT<Fortran::lower::omp::IdTyTemplate<Fortran::lower::omp::ExprTy>,
7279
Fortran::semantics::Symbol *sym() const { return identity.symbol; }
7380
const std::optional<ExprTy> &ref() const { return identity.designator; }
7481

82+
bool operator<(const ObjectT<IdTy, ExprTy> &other) const {
83+
return identity < other.identity;
84+
}
85+
7586
IdTy identity;
7687
};
7788
} // namespace tomp::type

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ static void genBodyOfTargetOp(
999999
firOpBuilder, copyVal.getLoc(), copyVal,
10001000
/*varPtrPtr=*/mlir::Value{}, name.str(), bounds,
10011001
/*members=*/llvm::SmallVector<mlir::Value>{},
1002-
/*membersIndex=*/mlir::DenseIntElementsAttr{},
1002+
/*membersIndex=*/mlir::ArrayAttr{},
10031003
static_cast<
10041004
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
10051005
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT),
@@ -1781,7 +1781,7 @@ genTargetOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
17811781
mlir::Value mapOp = createMapInfoOp(
17821782
firOpBuilder, location, baseOp, /*varPtrPtr=*/mlir::Value{},
17831783
name.str(), bounds, /*members=*/{},
1784-
/*membersIndex=*/mlir::DenseIntElementsAttr{},
1784+
/*membersIndex=*/mlir::ArrayAttr{},
17851785
static_cast<
17861786
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
17871787
mapFlag),

0 commit comments

Comments
 (0)