Skip to content

Commit e508bac

Browse files
authored
[Flang][OpenMP] Derived type explicit allocatable member mapping (#113557)
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 b5db75b commit e508bac

22 files changed

+1915
-435
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
@@ -894,14 +894,15 @@ void ClauseProcessor::processMapObjects(
894894
lower::StatementContext &stmtCtx, mlir::Location clauseLocation,
895895
const omp::ObjectList &objects,
896896
llvm::omp::OpenMPOffloadMappingFlags mapTypeBits,
897-
std::map<const semantics::Symbol *,
898-
llvm::SmallVector<OmpMapMemberIndicesData>> &parentMemberIndices,
897+
std::map<Object, OmpMapParentAndMemberData> &parentMemberIndices,
899898
llvm::SmallVectorImpl<mlir::Value> &mapVars,
900899
llvm::SmallVectorImpl<const semantics::Symbol *> &mapSyms) const {
901900
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
901+
902902
for (const omp::Object &object : objects) {
903903
llvm::SmallVector<mlir::Value> bounds;
904904
std::stringstream asFortran;
905+
std::optional<omp::Object> parentObj;
905906

906907
lower::AddrAndBoundsInfo info =
907908
lower::gatherDataOperandAddrAndBounds<mlir::omp::MapBoundsOp,
@@ -910,24 +911,43 @@ void ClauseProcessor::processMapObjects(
910911
object.ref(), clauseLocation, asFortran, bounds,
911912
treatIndexAsSection);
912913

914+
mlir::Value baseOp = info.rawInput;
915+
if (object.sym()->owner().IsDerivedType()) {
916+
omp::ObjectList objectList = gatherObjectsOf(object, semaCtx);
917+
assert(!objectList.empty() &&
918+
"could not find parent objects of derived type member");
919+
parentObj = objectList[0];
920+
parentMemberIndices.emplace(parentObj.value(),
921+
OmpMapParentAndMemberData{});
922+
923+
if (isMemberOrParentAllocatableOrPointer(object, semaCtx)) {
924+
llvm::SmallVector<int64_t> indices;
925+
generateMemberPlacementIndices(object, indices, semaCtx);
926+
baseOp = createParentSymAndGenIntermediateMaps(
927+
clauseLocation, converter, semaCtx, stmtCtx, objectList, indices,
928+
parentMemberIndices[parentObj.value()], asFortran.str(),
929+
mapTypeBits);
930+
}
931+
}
932+
913933
// Explicit map captures are captured ByRef by default,
914934
// optimisation passes may alter this to ByCopy or other capture
915935
// types to optimise
916-
mlir::Value baseOp = info.rawInput;
917936
auto location = mlir::NameLoc::get(
918937
mlir::StringAttr::get(firOpBuilder.getContext(), asFortran.str()),
919938
baseOp.getLoc());
920939
mlir::omp::MapInfoOp mapOp = createMapInfoOp(
921940
firOpBuilder, location, baseOp,
922941
/*varPtrPtr=*/mlir::Value{}, asFortran.str(), bounds,
923-
/*members=*/{}, /*membersIndex=*/mlir::DenseIntElementsAttr{},
942+
/*members=*/{}, /*membersIndex=*/mlir::ArrayAttr{},
924943
static_cast<
925944
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
926945
mapTypeBits),
927946
mlir::omp::VariableCaptureKind::ByRef, baseOp.getType());
928947

929-
if (object.sym()->owner().IsDerivedType()) {
930-
addChildIndexAndMapToParent(object, parentMemberIndices, mapOp, semaCtx);
948+
if (parentObj.has_value()) {
949+
parentMemberIndices[parentObj.value()].addChildIndexAndMapToParent(
950+
object, mapOp, semaCtx);
931951
} else {
932952
mapVars.push_back(mapOp);
933953
mapSyms.push_back(object.sym());
@@ -945,9 +965,7 @@ bool ClauseProcessor::processMap(
945965
llvm::SmallVector<const semantics::Symbol *> localMapSyms;
946966
llvm::SmallVectorImpl<const semantics::Symbol *> *ptrMapSyms =
947967
mapSyms ? mapSyms : &localMapSyms;
948-
std::map<const semantics::Symbol *,
949-
llvm::SmallVector<OmpMapMemberIndicesData>>
950-
parentMemberIndices;
968+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
951969

952970
auto process = [&](const omp::clause::Map &clause,
953971
const parser::CharBlock &source) {
@@ -1007,17 +1025,15 @@ bool ClauseProcessor::processMap(
10071025
};
10081026

10091027
bool clauseFound = findRepeatableClause<omp::clause::Map>(process);
1010-
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars,
1011-
*ptrMapSyms);
1028+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
1029+
result.mapVars, *ptrMapSyms);
10121030

10131031
return clauseFound;
10141032
}
10151033

10161034
bool ClauseProcessor::processMotionClauses(lower::StatementContext &stmtCtx,
10171035
mlir::omp::MapClauseOps &result) {
1018-
std::map<const semantics::Symbol *,
1019-
llvm::SmallVector<OmpMapMemberIndicesData>>
1020-
parentMemberIndices;
1036+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
10211037
llvm::SmallVector<const semantics::Symbol *> mapSymbols;
10221038

10231039
auto callbackFn = [&](const auto &clause, const parser::CharBlock &source) {
@@ -1044,8 +1060,9 @@ bool ClauseProcessor::processMotionClauses(lower::StatementContext &stmtCtx,
10441060
clauseFound =
10451061
findRepeatableClause<omp::clause::From>(callbackFn) || clauseFound;
10461062

1047-
insertChildMapInfoIntoParent(converter, parentMemberIndices, result.mapVars,
1048-
mapSymbols);
1063+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
1064+
result.mapVars, mapSymbols);
1065+
10491066
return clauseFound;
10501067
}
10511068

@@ -1107,9 +1124,7 @@ bool ClauseProcessor::processEnter(
11071124
bool ClauseProcessor::processUseDeviceAddr(
11081125
lower::StatementContext &stmtCtx, mlir::omp::UseDeviceAddrClauseOps &result,
11091126
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const {
1110-
std::map<const semantics::Symbol *,
1111-
llvm::SmallVector<OmpMapMemberIndicesData>>
1112-
parentMemberIndices;
1127+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
11131128
bool clauseFound = findRepeatableClause<omp::clause::UseDeviceAddr>(
11141129
[&](const omp::clause::UseDeviceAddr &clause,
11151130
const parser::CharBlock &source) {
@@ -1122,17 +1137,16 @@ bool ClauseProcessor::processUseDeviceAddr(
11221137
useDeviceSyms);
11231138
});
11241139

1125-
insertChildMapInfoIntoParent(converter, parentMemberIndices,
1140+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
11261141
result.useDeviceAddrVars, useDeviceSyms);
11271142
return clauseFound;
11281143
}
11291144

11301145
bool ClauseProcessor::processUseDevicePtr(
11311146
lower::StatementContext &stmtCtx, mlir::omp::UseDevicePtrClauseOps &result,
11321147
llvm::SmallVectorImpl<const semantics::Symbol *> &useDeviceSyms) const {
1133-
std::map<const semantics::Symbol *,
1134-
llvm::SmallVector<OmpMapMemberIndicesData>>
1135-
parentMemberIndices;
1148+
std::map<Object, OmpMapParentAndMemberData> parentMemberIndices;
1149+
11361150
bool clauseFound = findRepeatableClause<omp::clause::UseDevicePtr>(
11371151
[&](const omp::clause::UseDevicePtr &clause,
11381152
const parser::CharBlock &source) {
@@ -1145,7 +1159,7 @@ bool ClauseProcessor::processUseDevicePtr(
11451159
useDeviceSyms);
11461160
});
11471161

1148-
insertChildMapInfoIntoParent(converter, parentMemberIndices,
1162+
insertChildMapInfoIntoParent(converter, semaCtx, stmtCtx, parentMemberIndices,
11491163
result.useDevicePtrVars, useDeviceSyms);
11501164
return clauseFound;
11511165
}

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
@@ -1028,7 +1028,7 @@ static void genBodyOfTargetOp(
10281028
firOpBuilder, copyVal.getLoc(), copyVal,
10291029
/*varPtrPtr=*/mlir::Value{}, name.str(), bounds,
10301030
/*members=*/llvm::SmallVector<mlir::Value>{},
1031-
/*membersIndex=*/mlir::DenseIntElementsAttr{},
1031+
/*membersIndex=*/mlir::ArrayAttr{},
10321032
static_cast<
10331033
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
10341034
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT),
@@ -1818,7 +1818,7 @@ genTargetOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
18181818
mlir::Value mapOp = createMapInfoOp(
18191819
firOpBuilder, location, baseOp, /*varPtrPtr=*/mlir::Value{},
18201820
name.str(), bounds, /*members=*/{},
1821-
/*membersIndex=*/mlir::DenseIntElementsAttr{},
1821+
/*membersIndex=*/mlir::ArrayAttr{},
18221822
static_cast<
18231823
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
18241824
mapFlag),

0 commit comments

Comments
 (0)