Skip to content

Commit f1cecf5

Browse files
committed
review comments
1 parent 8130cad commit f1cecf5

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

flang/include/flang/Lower/ConvertExpr.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ createSomeArrayTempValue(AbstractConverter &converter,
170170
const evaluate::Expr<evaluate::SomeType> &expr,
171171
SymMap &symMap, StatementContext &stmtCtx);
172172

173+
/// Like createSomeArrayTempValue, but the temporary buffer is allocated lazily
174+
/// (inside the loops instead of before the loops). This can be useful if a
175+
/// loop's bounds are functions of other loop indices, for example.
173176
fir::ExtendedValue
174177
createLazyArrayTempValue(AbstractConverter &converter,
175178
const evaluate::Expr<evaluate::SomeType> &expr,

flang/lib/Lower/Bridge.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,12 +2682,9 @@ class FirConverter : public Fortran::lower::AbstractConverter {
26822682
explicitIterSpace.outermostContext().attachCleanup([=]() {
26832683
auto load = builder->create<fir::LoadOp>(loc, var);
26842684
auto cmp = builder->genIsNotNull(loc, load);
2685-
auto ifOp =
2686-
builder->create<fir::IfOp>(loc, cmp, /*withElseRegion=*/false);
2687-
auto insPt = builder->saveInsertionPoint();
2688-
builder->setInsertionPointToStart(&ifOp.thenRegion().front());
2689-
builder->create<fir::FreeMemOp>(loc, load);
2690-
builder->restoreInsertionPoint(insPt);
2685+
builder->genIfThen(loc, cmp)
2686+
.genThen([&]() { builder->create<fir::FreeMemOp>(loc, load); })
2687+
.end();
26912688
});
26922689
}
26932690

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,7 +3356,7 @@ class ArrayExprLowering {
33563356
auto loc = getLoc();
33573357
// Once the loop extents have been computed, which may require being inside
33583358
// some explicit loops, lazily allocate the expression on the heap.
3359-
ccPrelude = [=](llvm::ArrayRef<mlir::Value> shape) -> mlir::Value {
3359+
ccPrelude = [=](llvm::ArrayRef<mlir::Value> shape) {
33603360
auto load = builder.create<fir::LoadOp>(loc, var);
33613361
auto eleTy = fir::unwrapRefType(load.getType());
33623362
auto unknown = fir::SequenceType::getUnknownExtent();
@@ -3365,15 +3365,14 @@ class ArrayExprLowering {
33653365
auto toTy = fir::HeapType::get(seqTy);
33663366
auto castTo = builder.createConvert(loc, toTy, load);
33673367
auto cmp = builder.genIsNull(loc, castTo);
3368-
auto ifOp = builder.create<fir::IfOp>(loc, cmp, /*withElseRegion=*/false);
3369-
auto insPt = builder.saveInsertionPoint();
3370-
builder.setInsertionPointToStart(&ifOp.thenRegion().front());
3371-
auto mem = builder.create<fir::AllocMemOp>(loc, seqTy, ".lazy.mask",
3372-
llvm::None, shape);
3373-
auto uncast = builder.createConvert(loc, load.getType(), mem);
3374-
builder.create<fir::StoreOp>(loc, uncast, var);
3375-
builder.restoreInsertionPoint(insPt);
3376-
return mem;
3368+
builder.genIfThen(loc, cmp)
3369+
.genThen([&]() {
3370+
auto mem = builder.create<fir::AllocMemOp>(loc, seqTy, ".lazy.mask",
3371+
llvm::None, shape);
3372+
auto uncast = builder.createConvert(loc, load.getType(), mem);
3373+
builder.create<fir::StoreOp>(loc, uncast, var);
3374+
})
3375+
.end();
33773376
};
33783377
// Create a dummy array_load before the loop. We're storing to a lazy
33793378
// temporary, so there will be no conflict and no copy-in.
@@ -3603,7 +3602,7 @@ class ArrayExprLowering {
36033602
// Mask expressions are array expressions too.
36043603
for (const auto *e : implicitSpace->getExprs())
36053604
if (e && !implicitSpace->isLowered(e)) {
3606-
if (auto var = implicitSpace->lookupVariable(e)) {
3605+
if (auto var = implicitSpace->lookupMaskVariable(e)) {
36073606
// Allocate the mask buffer lazily.
36083607
auto tmp = Fortran::lower::createLazyArrayTempValue(
36093608
converter, *e, var, symMap, stmtCtx);
@@ -3660,10 +3659,8 @@ class ArrayExprLowering {
36603659
llvm::SmallVector<mlir::Value> ivars;
36613660
if (loopDepth > 0) {
36623661
// Generate the lazy mask allocation, if one was given.
3663-
if (ccPrelude.hasValue()) {
3664-
[[maybe_unused]] auto allocMem = ccPrelude.getValue()(shape);
3665-
assert(allocMem && "mask buffer allocation failure");
3666-
}
3662+
if (ccPrelude.hasValue())
3663+
ccPrelude.getValue()(shape);
36673664

36683665
auto *startBlock = builder.getBlock();
36693666
for (auto i : llvm::enumerate(llvm::reverse(loopUppers))) {
@@ -5540,8 +5537,7 @@ class ArrayExprLowering {
55405537
Fortran::lower::SymMap &symMap;
55415538
/// The continuation to generate code to update the destination.
55425539
llvm::Optional<CC> ccStoreToDest;
5543-
llvm::Optional<std::function<mlir::Value(llvm::ArrayRef<mlir::Value>)>>
5544-
ccPrelude;
5540+
llvm::Optional<std::function<void(llvm::ArrayRef<mlir::Value>)>> ccPrelude;
55455541
llvm::Optional<std::function<fir::ArrayLoadOp(llvm::ArrayRef<mlir::Value>)>>
55465542
ccLoadDest;
55475543
/// The destination is the loaded array into which the results will be

flang/lib/Lower/IterationSpace.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ class ImplicitIterSpace
127127
maskVarMap.try_emplace(exp, var);
128128
}
129129

130-
mlir::Value lookupVariable(FrontEndExpr exp) {
130+
mlir::Value lookupMaskVariable(FrontEndExpr exp) {
131131
return maskVarMap.lookup(exp);
132132
}
133133

0 commit comments

Comments
 (0)