Skip to content

Commit ed8d3fb

Browse files
SC llvm teamSC llvm team
SC llvm team
authored and
SC llvm team
committed
Merged main:cf1bde33423d into amd-gfx:af24aa3aae84
Local branch amd-gfx af24aa3 Merged main:076ec9f5f5bf into amd-gfx:f74e82fc358f Remote branch main cf1bde3 [clang] Fix sorting module headers (llvm#73146)
2 parents af24aa3 + cf1bde3 commit ed8d3fb

File tree

21 files changed

+241
-259
lines changed

21 files changed

+241
-259
lines changed

clang/lib/Lex/ModuleMap.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2509,9 +2509,9 @@ void ModuleMapParser::parseHeaderDecl(MMToken::TokenKind LeadingToken,
25092509
<< FixItHint::CreateReplacement(CurrModuleDeclLoc, "framework module");
25102510
}
25112511

2512-
static int compareModuleHeaders(const Module::Header *A,
2513-
const Module::Header *B) {
2514-
return A->NameAsWritten.compare(B->NameAsWritten);
2512+
static bool compareModuleHeaders(const Module::Header &A,
2513+
const Module::Header &B) {
2514+
return A.NameAsWritten < B.NameAsWritten;
25152515
}
25162516

25172517
/// Parse an umbrella directory declaration.
@@ -2574,7 +2574,7 @@ void ModuleMapParser::parseUmbrellaDirDecl(SourceLocation UmbrellaLoc) {
25742574
}
25752575

25762576
// Sort header paths so that the pcm doesn't depend on iteration order.
2577-
llvm::array_pod_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
2577+
std::stable_sort(Headers.begin(), Headers.end(), compareModuleHeaders);
25782578

25792579
for (auto &Header : Headers)
25802580
Map.addHeader(ActiveModule, std::move(Header), ModuleMap::TextualHeader);

flang/lib/Lower/OpenMP.cpp

Lines changed: 89 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "flang/Semantics/tools.h"
2929
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
3030
#include "mlir/Dialect/SCF/IR/SCF.h"
31+
#include "mlir/Transforms/RegionUtils.h"
3132
#include "llvm/Frontend/OpenMP/OMPConstants.h"
3233
#include "llvm/Support/CommandLine.h"
3334

@@ -1711,26 +1712,22 @@ static mlir::omp::MapInfoOp
17111712
createMapInfoOp(fir::FirOpBuilder &builder, mlir::Location loc,
17121713
mlir::Value baseAddr, std::stringstream &name,
17131714
mlir::SmallVector<mlir::Value> bounds, uint64_t mapType,
1714-
mlir::omp::VariableCaptureKind mapCaptureType, mlir::Type retTy,
1715-
bool isVal = false) {
1716-
mlir::Value val, varPtr, varPtrPtr;
1715+
mlir::omp::VariableCaptureKind mapCaptureType,
1716+
mlir::Type retTy) {
1717+
mlir::Value varPtr, varPtrPtr;
17171718
mlir::TypeAttr varType;
17181719

17191720
if (auto boxTy = baseAddr.getType().dyn_cast<fir::BaseBoxType>()) {
17201721
baseAddr = builder.create<fir::BoxAddrOp>(loc, baseAddr);
17211722
retTy = baseAddr.getType();
17221723
}
17231724

1724-
if (isVal)
1725-
val = baseAddr;
1726-
else
1727-
varPtr = baseAddr;
1728-
1729-
if (auto ptrType = llvm::dyn_cast<mlir::omp::PointerLikeType>(retTy))
1730-
varType = mlir::TypeAttr::get(ptrType.getElementType());
1725+
varPtr = baseAddr;
1726+
varType = mlir::TypeAttr::get(
1727+
llvm::cast<mlir::omp::PointerLikeType>(retTy).getElementType());
17311728

17321729
mlir::omp::MapInfoOp op = builder.create<mlir::omp::MapInfoOp>(
1733-
loc, retTy, val, varPtr, varType, varPtrPtr, bounds,
1730+
loc, retTy, varPtr, varType, varPtrPtr, bounds,
17341731
builder.getIntegerAttr(builder.getIntegerType(64, false), mapType),
17351732
builder.getAttr<mlir::omp::VariableCaptureKindAttr>(mapCaptureType),
17361733
builder.getStringAttr(name.str()));
@@ -2503,21 +2500,27 @@ static void genBodyOfTargetOp(
25032500
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
25042501
mlir::Region &region = targetOp.getRegion();
25052502

2506-
firOpBuilder.createBlock(&region, {}, mapSymTypes, mapSymLocs);
2503+
auto *regionBlock =
2504+
firOpBuilder.createBlock(&region, {}, mapSymTypes, mapSymLocs);
25072505

25082506
unsigned argIndex = 0;
2509-
unsigned blockArgsIndex = mapSymbols.size();
2510-
2511-
// The block arguments contain the map_operands followed by the bounds in
2512-
// order. This returns a vector containing the next 'n' block arguments for
2513-
// the bounds.
2514-
auto extractBoundArgs = [&](auto n) {
2515-
llvm::SmallVector<mlir::Value> argExtents;
2516-
while (n--) {
2517-
argExtents.push_back(fir::getBase(region.getArgument(blockArgsIndex)));
2518-
blockArgsIndex++;
2507+
2508+
// Clones the `bounds` placing them inside the target region and returns them.
2509+
auto cloneBound = [&](mlir::Value bound) {
2510+
if (mlir::isMemoryEffectFree(bound.getDefiningOp())) {
2511+
mlir::Operation *clonedOp = bound.getDefiningOp()->clone();
2512+
regionBlock->push_back(clonedOp);
2513+
return clonedOp->getResult(0);
25192514
}
2520-
return argExtents;
2515+
TODO(converter.getCurrentLocation(),
2516+
"target map clause operand unsupported bound type");
2517+
};
2518+
2519+
auto cloneBounds = [cloneBound](llvm::ArrayRef<mlir::Value> bounds) {
2520+
llvm::SmallVector<mlir::Value> clonedBounds;
2521+
for (mlir::Value bound : bounds)
2522+
clonedBounds.emplace_back(cloneBound(bound));
2523+
return clonedBounds;
25212524
};
25222525

25232526
// Bind the symbols to their corresponding block arguments.
@@ -2526,34 +2529,31 @@ static void genBodyOfTargetOp(
25262529
fir::ExtendedValue extVal = converter.getSymbolExtendedValue(*sym);
25272530
extVal.match(
25282531
[&](const fir::BoxValue &v) {
2529-
converter.bindSymbol(
2530-
*sym,
2531-
fir::BoxValue(arg, extractBoundArgs(v.getLBounds().size()),
2532-
v.getExplicitParameters(), v.getExplicitExtents()));
2532+
converter.bindSymbol(*sym,
2533+
fir::BoxValue(arg, cloneBounds(v.getLBounds()),
2534+
v.getExplicitParameters(),
2535+
v.getExplicitExtents()));
25332536
},
25342537
[&](const fir::MutableBoxValue &v) {
25352538
converter.bindSymbol(
2536-
*sym,
2537-
fir::MutableBoxValue(arg, extractBoundArgs(v.getLBounds().size()),
2538-
v.getMutableProperties()));
2539+
*sym, fir::MutableBoxValue(arg, cloneBounds(v.getLBounds()),
2540+
v.getMutableProperties()));
25392541
},
25402542
[&](const fir::ArrayBoxValue &v) {
25412543
converter.bindSymbol(
2542-
*sym,
2543-
fir::ArrayBoxValue(arg, extractBoundArgs(v.getExtents().size()),
2544-
extractBoundArgs(v.getLBounds().size()),
2545-
v.getSourceBox()));
2544+
*sym, fir::ArrayBoxValue(arg, cloneBounds(v.getExtents()),
2545+
cloneBounds(v.getLBounds()),
2546+
v.getSourceBox()));
25462547
},
25472548
[&](const fir::CharArrayBoxValue &v) {
25482549
converter.bindSymbol(
2549-
*sym,
2550-
fir::CharArrayBoxValue(arg, extractBoundArgs(1).front(),
2551-
extractBoundArgs(v.getExtents().size()),
2552-
extractBoundArgs(v.getLBounds().size())));
2550+
*sym, fir::CharArrayBoxValue(arg, cloneBound(v.getLen()),
2551+
cloneBounds(v.getExtents()),
2552+
cloneBounds(v.getLBounds())));
25532553
},
25542554
[&](const fir::CharBoxValue &v) {
2555-
converter.bindSymbol(
2556-
*sym, fir::CharBoxValue(arg, extractBoundArgs(1).front()));
2555+
converter.bindSymbol(*sym,
2556+
fir::CharBoxValue(arg, cloneBound(v.getLen())));
25572557
},
25582558
[&](const fir::UnboxedValue &v) { converter.bindSymbol(*sym, arg); },
25592559
[&](const auto &) {
@@ -2563,6 +2563,55 @@ static void genBodyOfTargetOp(
25632563
argIndex++;
25642564
}
25652565

2566+
// Check if cloning the bounds introduced any dependency on the outer region.
2567+
// If so, then either clone them as well if they are MemoryEffectFree, or else
2568+
// copy them to a new temporary and add them to the map and block_argument
2569+
// lists and replace their uses with the new temporary.
2570+
llvm::SetVector<mlir::Value> valuesDefinedAbove;
2571+
mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove);
2572+
while (!valuesDefinedAbove.empty()) {
2573+
for (mlir::Value val : valuesDefinedAbove) {
2574+
mlir::Operation *valOp = val.getDefiningOp();
2575+
if (mlir::isMemoryEffectFree(valOp)) {
2576+
mlir::Operation *clonedOp = valOp->clone();
2577+
regionBlock->push_front(clonedOp);
2578+
val.replaceUsesWithIf(
2579+
clonedOp->getResult(0), [regionBlock](mlir::OpOperand &use) {
2580+
return use.getOwner()->getBlock() == regionBlock;
2581+
});
2582+
} else {
2583+
auto savedIP = firOpBuilder.getInsertionPoint();
2584+
firOpBuilder.setInsertionPointAfter(valOp);
2585+
auto copyVal =
2586+
firOpBuilder.createTemporary(val.getLoc(), val.getType());
2587+
firOpBuilder.createStoreWithConvert(copyVal.getLoc(), val, copyVal);
2588+
2589+
llvm::SmallVector<mlir::Value> bounds;
2590+
std::stringstream name;
2591+
firOpBuilder.setInsertionPoint(targetOp);
2592+
mlir::Value mapOp = createMapInfoOp(
2593+
firOpBuilder, copyVal.getLoc(), copyVal, name, bounds,
2594+
static_cast<
2595+
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
2596+
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT),
2597+
mlir::omp::VariableCaptureKind::ByCopy, copyVal.getType());
2598+
targetOp.getMapOperandsMutable().append(mapOp);
2599+
mlir::Value clonedValArg =
2600+
region.addArgument(copyVal.getType(), copyVal.getLoc());
2601+
firOpBuilder.setInsertionPointToStart(regionBlock);
2602+
auto loadOp = firOpBuilder.create<fir::LoadOp>(clonedValArg.getLoc(),
2603+
clonedValArg);
2604+
val.replaceUsesWithIf(
2605+
loadOp->getResult(0), [regionBlock](mlir::OpOperand &use) {
2606+
return use.getOwner()->getBlock() == regionBlock;
2607+
});
2608+
firOpBuilder.setInsertionPoint(regionBlock, savedIP);
2609+
}
2610+
}
2611+
valuesDefinedAbove.clear();
2612+
mlir::getUsedValuesDefinedAbove(region, valuesDefinedAbove);
2613+
}
2614+
25662615
// Insert dummy instruction to remember the insertion position. The
25672616
// marker will be deleted since there are not uses.
25682617
// In the HLFIR flow there are hlfir.declares inserted above while
@@ -2685,53 +2734,6 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
26852734
};
26862735
Fortran::lower::pft::visitAllSymbols(eval, captureImplicitMap);
26872736

2688-
// Add the bounds and extents for box values to mapOperands
2689-
auto addMapInfoForBounds = [&](const auto &bounds) {
2690-
for (auto &val : bounds) {
2691-
mapSymLocs.push_back(val.getLoc());
2692-
mapSymTypes.push_back(val.getType());
2693-
2694-
llvm::SmallVector<mlir::Value> bounds;
2695-
std::stringstream name;
2696-
2697-
mlir::Value mapOp = createMapInfoOp(
2698-
converter.getFirOpBuilder(), val.getLoc(), val, name, bounds,
2699-
static_cast<
2700-
std::underlying_type_t<llvm::omp::OpenMPOffloadMappingFlags>>(
2701-
llvm::omp::OpenMPOffloadMappingFlags::OMP_MAP_IMPLICIT),
2702-
mlir::omp::VariableCaptureKind::ByCopy, val.getType(), true);
2703-
mapOperands.push_back(mapOp);
2704-
}
2705-
};
2706-
2707-
for (const Fortran::semantics::Symbol *sym : mapSymbols) {
2708-
fir::ExtendedValue extVal = converter.getSymbolExtendedValue(*sym);
2709-
extVal.match(
2710-
[&](const fir::BoxValue &v) { addMapInfoForBounds(v.getLBounds()); },
2711-
[&](const fir::MutableBoxValue &v) {
2712-
addMapInfoForBounds(v.getLBounds());
2713-
},
2714-
[&](const fir::ArrayBoxValue &v) {
2715-
addMapInfoForBounds(v.getExtents());
2716-
addMapInfoForBounds(v.getLBounds());
2717-
},
2718-
[&](const fir::CharArrayBoxValue &v) {
2719-
llvm::SmallVector<mlir::Value> len;
2720-
len.push_back(v.getLen());
2721-
addMapInfoForBounds(len);
2722-
addMapInfoForBounds(v.getExtents());
2723-
addMapInfoForBounds(v.getLBounds());
2724-
},
2725-
[&](const fir::CharBoxValue &v) {
2726-
llvm::SmallVector<mlir::Value> len;
2727-
len.push_back(v.getLen());
2728-
addMapInfoForBounds(len);
2729-
},
2730-
[&](const auto &) {
2731-
// Nothing to do for non-box values.
2732-
});
2733-
}
2734-
27352737
auto targetOp = converter.getFirOpBuilder().create<mlir::omp::TargetOp>(
27362738
currentLocation, ifClauseOperand, deviceOperand, threadLimitOperand,
27372739
nowaitAttr, mapOperands);

flang/test/Lower/OpenMP/FIR/array-bounds.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
!ALL: %[[BOUNDS1:.*]] = omp.bounds lower_bound(%[[C5]] : index) upper_bound(%[[C6]] : index) stride(%[[C4]] : index) start_idx(%[[C4]] : index)
1717
!ALL: %[[MAP1:.*]] = omp.map_info var_ptr(%[[WRITE]] : !fir.ref<!fir.array<10xi32>>, !fir.array<10xi32>) map_clauses(tofrom) capture(ByRef) bounds(%[[BOUNDS1]]) -> !fir.ref<!fir.array<10xi32>> {name = "sp_write(2:5)"}
1818
!ALL: %[[MAP2:.*]] = omp.map_info var_ptr(%[[ITER]] : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = "i"}
19-
!ALL: omp.target map_entries(%[[MAP0]] -> %{{.*}}, %[[MAP1]] -> %{{.*}}, %[[MAP2]] -> %{{.*}}, %{{.*}} -> %{{.*}}, %{{.*}} -> %{{.*}} : !fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>, !fir.ref<i32>, index, index) {
19+
!ALL: omp.target map_entries(%[[MAP0]] -> %{{.*}}, %[[MAP1]] -> %{{.*}}, %[[MAP2]] -> %{{.*}} : !fir.ref<!fir.array<10xi32>>, !fir.ref<!fir.array<10xi32>>, !fir.ref<i32>) {
2020

2121
subroutine read_write_section()
2222
integer :: sp_read(10) = (/1,2,3,4,5,6,7,8,9,10/)
@@ -64,7 +64,7 @@ end subroutine assumed_shape_array
6464
!ALL: %[[BOUNDS:.*]] = omp.bounds lower_bound(%[[C1]] : index) upper_bound(%[[C2]] : index) stride(%[[C0]] : index) start_idx(%[[C0]] : index)
6565
!ALL: %[[MAP:.*]] = omp.map_info var_ptr(%[[ARG0]] : !fir.ref<!fir.array<?xi32>>, !fir.array<?xi32>) map_clauses(tofrom) capture(ByRef) bounds(%[[BOUNDS]]) -> !fir.ref<!fir.array<?xi32>> {name = "arr_read_write(2:5)"}
6666
!ALL: %[[MAP2:.*]] = omp.map_info var_ptr(%[[ALLOCA]] : !fir.ref<i32>, i32) map_clauses(implicit, exit_release_or_enter_alloc) capture(ByCopy) -> !fir.ref<i32> {name = "i"}
67-
!ALL: omp.target map_entries(%[[MAP]] -> %{{.*}}, %[[MAP2]] -> %{{.*}}, %{{.*}} -> %{{.*}} : !fir.ref<!fir.array<?xi32>>, !fir.ref<i32>, index) {
67+
!ALL: omp.target map_entries(%[[MAP]] -> %{{.*}}, %[[MAP2]] -> %{{.*}} : !fir.ref<!fir.array<?xi32>>, !fir.ref<i32>) {
6868

6969
subroutine assumed_size_array(arr_read_write)
7070
integer, intent(inout) :: arr_read_write(*)

0 commit comments

Comments
 (0)