Skip to content

Commit

Permalink
Implement N-D bounds and tidy up, still small bug with N-D parent sub…
Browse files Browse the repository at this point in the history
…scripts to work through
  • Loading branch information
agozillon committed Sep 21, 2024
1 parent 5d62d62 commit 79c444a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 128 deletions.
9 changes: 6 additions & 3 deletions flang/lib/Lower/OpenMP/ClauseProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,12 @@ void ClauseProcessor::processMapObjects(
assert(!objectList.empty() &&
"could not find parent objects of derived type member");
parentObj = objectList[0];
auto insert = parentMemberIndices.emplace(parentObj.value(),
OmpMapParentAndMemberData{});
insert.first->second.parentObjList.push_back(parentObj.value());
auto found = parentMemberIndices.find(parentObj.value());
if (found != parentMemberIndices.end())
found->second.parentObjList.push_back(parentObj.value());
else
parentMemberIndices.emplace(parentObj.value(),
OmpMapParentAndMemberData{});

if (isMemberOrParentAllocatableOrPointer(object, semaCtx)) {
llvm::SmallVector<int64_t> indices;
Expand Down
174 changes: 49 additions & 125 deletions flang/lib/Lower/OpenMP/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <flang/Parser/tools.h>
#include <flang/Semantics/tools.h>
#include <llvm/Support/CommandLine.h>
#include <mlir/Analysis/TopologicalSortUtils.h>
#include <mlir/Dialect/Arith/IR/Arith.h>

#include <numeric>
Expand Down Expand Up @@ -171,12 +172,12 @@ bool isDuplicateMemberMapInfo(OmpMapParentAndMemberData &parentMembers,
return false;
}

void generateArrayIndices(lower::AbstractConverter &converter,
static void generateArrayIndices(lower::AbstractConverter &converter,
fir::FirOpBuilder &firOpBuilder,
lower::StatementContext &stmtCtx,
mlir::Location clauseLocation,
llvm::SmallVectorImpl<mlir::Value> &indices,
omp::Object object) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
if (auto maybeRef = evaluate::ExtractDataRef(*object.ref())) {
evaluate::DataRef ref = *maybeRef;
if (auto *arr = std::get_if<evaluate::ArrayRef>(&ref.u)) {
Expand All @@ -200,12 +201,11 @@ void generateArrayIndices(lower::AbstractConverter &converter,
}
}

mlir::Value generateBoundsComparisonBranch(lower::AbstractConverter &converter,
static mlir::Value generateBoundsComparisonBranch(fir::FirOpBuilder &firOpBuilder,
mlir::Location clauseLocation,
mlir::arith::CmpIPredicate pred,
mlir::Value &index,
mlir::Value index,
mlir::Value bound) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
auto cmp = firOpBuilder.create<mlir::arith::CmpIOp>(clauseLocation, pred,
index, bound);
return firOpBuilder
Expand All @@ -220,6 +220,34 @@ mlir::Value generateBoundsComparisonBranch(lower::AbstractConverter &converter,
.getResults()[0];
}

static void extendBoundsFromMultipleSubscripts(
lower::AbstractConverter &converter, lower::StatementContext &stmtCtx,
mlir::omp::MapInfoOp mapOp, omp::ObjectList objList) {
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
if (!mapOp.getBounds().empty()) {
for (omp::Object v : objList) {
llvm::SmallVector<mlir::Value> indices;
generateArrayIndices(converter, firOpBuilder, stmtCtx, mapOp.getLoc(),
indices, v);

for (size_t i = 0; i < mapOp.getBounds().size(); ++i) {
if (auto boundOp = mlir::dyn_cast_if_present<mlir::omp::MapBoundsOp>(
mapOp.getBounds()[i].getDefiningOp())) {
boundOp.getUpperBoundMutable().assign(generateBoundsComparisonBranch(
firOpBuilder, mapOp.getLoc(), mlir::arith::CmpIPredicate::ugt,
indices[i], boundOp.getUpperBoundMutable().begin()->get()));
boundOp.getLowerBoundMutable().assign(generateBoundsComparisonBranch(
firOpBuilder, mapOp.getLoc(), mlir::arith::CmpIPredicate::ult,
indices[i], boundOp.getLowerBoundMutable().begin()->get()));
}
}
}
}

// reorder all SSA's we may have generated to make sure we maintain ordering.
sortTopologically(mapOp->getBlock());
}

// When mapping members of derived types, there is a chance that one of the
// members along the way to a mapped member is an descriptor. In which case
// we have to make sure we generate a map for those along the way otherwise
Expand Down Expand Up @@ -261,8 +289,8 @@ mlir::Value createParentSymAndGenIntermediateMaps(
fir::unwrapPassByRefType(curValue.getType()))) {
if (arrayExprWithSubscript(objectList[i])) {
llvm::SmallVector<mlir::Value> indices;
generateArrayIndices(converter, stmtCtx, clauseLocation, indices,
objectList[i]);
generateArrayIndices(converter, firOpBuilder, stmtCtx, clauseLocation,
indices, objectList[i]);
assert(!indices.empty() && "missing expected indices for map clause");
curValue = firOpBuilder.create<fir::CoordinateOp>(
clauseLocation, firOpBuilder.getRefType(arrType.getEleTy()),
Expand Down Expand Up @@ -332,37 +360,9 @@ mlir::Value createParentSymAndGenIntermediateMaps(
interimIndices);
auto v = std::distance(
parentMemberIndices.memberPlacementIndices.begin(), it);

auto savedIP = firOpBuilder.getInsertionPoint();
firOpBuilder.setInsertionPoint(
parentMemberIndices.memberMap[v].getBounds()[0].getDefiningOp());

llvm::SmallVector<mlir::Value> indices;
generateArrayIndices(converter, stmtCtx, clauseLocation, indices,
objectList[i]);

for (size_t i = 0;
i < parentMemberIndices.memberMap[v].getBounds().size(); i++) {
if (auto boundOp = mlir::dyn_cast_if_present<mlir::omp::MapBoundsOp>(
parentMemberIndices.memberMap[v]
.getBounds()[i]
.getDefiningOp())) {
boundOp.getUpperBoundMutable().assign(
generateBoundsComparisonBranch(
converter, clauseLocation, mlir::arith::CmpIPredicate::ugt,
indices[i], boundOp.getUpperBoundMutable().begin()->get()));
boundOp.getLowerBoundMutable().assign(
generateBoundsComparisonBranch(
converter, clauseLocation, mlir::arith::CmpIPredicate::ult,
indices[i], boundOp.getLowerBoundMutable().begin()->get()));
}
}

firOpBuilder.setInsertionPoint(parentMemberIndices.memberMap[v]
.getBounds()[0]
.getDefiningOp()
->getBlock(),
savedIP);
extendBoundsFromMultipleSubscripts(converter, stmtCtx,
parentMemberIndices.memberMap[v],
{objectList[i]});
}

curValue = firOpBuilder.create<fir::LoadOp>(clauseLocation, curValue);
Expand Down Expand Up @@ -473,50 +473,22 @@ void insertChildMapInfoIntoParent(
if (parentExists) {
auto mapOp = llvm::cast<mlir::omp::MapInfoOp>(
mapOperands[parentIdx].getDefiningOp());
// NOTE: To maintain appropriate SSA ordering, we move the parent map
// which will now have references to its children after the last
// of its members to be generated. This is necessary when a user
// has defined a series of parent and children maps where the parent
// precedes the children. An alternative, may be to do
// delayed generation of map info operations from the clauses and
// organize them first before generation.
mapOp->moveAfter(indices.second.memberMap.back());

for (mlir::omp::MapInfoOp memberMap : indices.second.memberMap)
mapOp.getMembersMutable().append(memberMap.getResult());

mapOp.setMembersIndexAttr(firOpBuilder.create2DIntegerArrayAttr(
indices.second.memberPlacementIndices));

if (!mapOp.getBounds().empty()) {
auto savedIP = firOpBuilder.getInsertionPoint();
firOpBuilder.setInsertionPoint(mapOp.getBounds()[0].getDefiningOp());

for (auto v : indices.second.parentObjList) {
llvm::SmallVector<mlir::Value> indexes;
generateArrayIndices(converter, stmtCtx, mapOp.getLoc(), indexes, v);

for (size_t i = 0; i < mapOp.getBounds().size(); i++) {
if (auto boundOp =
mlir::dyn_cast_if_present<mlir::omp::MapBoundsOp>(
mapOp.getBounds()[i].getDefiningOp())) {
boundOp.getUpperBoundMutable().assign(
generateBoundsComparisonBranch(
converter, mapOp.getLoc(),
mlir::arith::CmpIPredicate::ugt, indexes[i],
boundOp.getUpperBoundMutable().begin()->get()));
boundOp.getLowerBoundMutable().assign(
generateBoundsComparisonBranch(
converter, mapOp.getLoc(),
mlir::arith::CmpIPredicate::ult, indexes[i],
boundOp.getLowerBoundMutable().begin()->get()));
}
}
}

firOpBuilder.setInsertionPoint(
mapOp.getBounds()[0].getDefiningOp()->getBlock(), savedIP);
}
// Not only does this extend bounds if multiple subscripts are
// defined for a map parent, but it also performs a topological
// sort, re-ordering SSA values so everything maintains correct
// ordering, this by extension shuffles the parent map, into the
// correct position after it's member definitions, as when we fall
// into this segment of the if statement, the parent map information
// has been generated prior to it's members in most cases.
extendBoundsFromMultipleSubscripts(converter, stmtCtx, mapOp,
indices.second.parentObjList);
} else {
// NOTE: We take the map type of the first child, this may not
// be the correct thing to do, however, we shall see. For the moment
Expand All @@ -525,7 +497,6 @@ void insertChildMapInfoIntoParent(
// the "main" map type clause from the directive being used.
uint64_t mapType = indices.second.memberMap[0].getMapType().value_or(0);

llvm::errs() << "Not parent exists \n";
llvm::SmallVector<mlir::Value> members;
for (mlir::omp::MapInfoOp memberMap : indices.second.memberMap)
members.push_back(memberMap.getResult());
Expand All @@ -550,55 +521,8 @@ void insertChildMapInfoIntoParent(
info.rawInput.getType(),
/*partialMap=*/true);

// TODO:
// > Fix all check-all tests that are broken
// > Fix parent mapping,
// probably needs to be elsewhere rather than below, i.e. whenever we
// find parent sym more than once which may be in the regular
// proccessMap
// as i think above is just checking if we have alreadty generated it to
// insert. so it's likely in the processmapObject where we first insert
// the parent symbol
// + map info, and in these cases we'll have to adjust the member indices
// > check N-D indexing works, it likely will need some minor
// adjustments, we can hold off on it until everything
// else is done as an extra addition

// for (auto v : indices.second.parentObjList) {
// v.ref()->dump();
// v.sym()->dump();
// }

if (!mapOp.getBounds().empty()) {
auto savedIP = firOpBuilder.getInsertionPoint();
firOpBuilder.setInsertionPoint(mapOp.getBounds()[0].getDefiningOp());

for (auto v : indices.second.parentObjList) {
llvm::SmallVector<mlir::Value> indexes;
generateArrayIndices(converter, stmtCtx, mapOp.getLoc(), indexes, v);

for (size_t i = 0; i < mapOp.getBounds().size(); i++) {
if (auto boundOp =
mlir::dyn_cast_if_present<mlir::omp::MapBoundsOp>(
mapOp.getBounds()[i].getDefiningOp())) {
boundOp.getUpperBoundMutable().assign(
generateBoundsComparisonBranch(
converter, mapOp.getLoc(),
mlir::arith::CmpIPredicate::ugt, indexes[i],
boundOp.getUpperBoundMutable().begin()->get()));
boundOp.getLowerBoundMutable().assign(
generateBoundsComparisonBranch(
converter, mapOp.getLoc(),
mlir::arith::CmpIPredicate::ult, indexes[i],
boundOp.getLowerBoundMutable().begin()->get()));
}
}
}

firOpBuilder.setInsertionPoint(
mapOp.getBounds()[0].getDefiningOp()->getBlock(), savedIP);
}

extendBoundsFromMultipleSubscripts(converter, stmtCtx, mapOp,
indices.second.parentObjList);
mapOperands.push_back(mapOp);
if (mapSymTypes)
mapSymTypes->push_back(mapOp.getType());
Expand Down

0 comments on commit 79c444a

Please sign in to comment.