Skip to content

Commit

Permalink
[Flang][Lower] Introduce SymMapScope helper class (NFC) (#107866)
Browse files Browse the repository at this point in the history
This patch creates a simple RAII wrapper class for `SymMap` to make it
easier to use and prevent a missing matching `popScope()` for a
`pushScope()` call on simple use cases.

Some push-pop pairs are replaced with instances of the new class by this
patch.
  • Loading branch information
skatrak authored Sep 10, 2024
1 parent 516f08b commit 433ca3e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
10 changes: 10 additions & 0 deletions flang/include/flang/Lower/SymbolMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,16 @@ class SymMap {
llvm::SmallVector<std::pair<AcDoVar, mlir::Value>> impliedDoStack;
};

/// RAII wrapper for SymMap.
class SymMapScope {
public:
explicit SymMapScope(SymMap &map) : map(map) { map.pushScope(); }
~SymMapScope() { map.popScope(); }

private:
SymMap &map;
};

} // namespace Fortran::lower

#endif // FORTRAN_LOWER_SYMBOLMAP_H
9 changes: 3 additions & 6 deletions flang/lib/Lower/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2597,14 +2597,12 @@ class FirConverter : public Fortran::lower::AbstractConverter {
stmt.t)
.value();
if (lowerToHighLevelFIR()) {
mlir::OpBuilder::InsertPoint insertPt = builder->saveInsertionPoint();
localSymbols.pushScope();
mlir::OpBuilder::InsertionGuard guard(*builder);
Fortran::lower::SymMapScope scope(localSymbols);
genForallNest(concurrentHeader);
genFIR(std::get<Fortran::parser::UnlabeledStatement<
Fortran::parser::ForallAssignmentStmt>>(stmt.t)
.statement);
localSymbols.popScope();
builder->restoreInsertionPoint(insertPt);
return;
}
prepareExplicitSpace(stmt);
Expand Down Expand Up @@ -2824,7 +2822,7 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}

void genFIR(const Fortran::parser::CUFKernelDoConstruct &kernel) {
localSymbols.pushScope();
Fortran::lower::SymMapScope scope(localSymbols);
const Fortran::parser::CUFKernelDoConstruct::Directive &dir =
std::get<Fortran::parser::CUFKernelDoConstruct::Directive>(kernel.t);

Expand Down Expand Up @@ -3015,7 +3013,6 @@ class FirConverter : public Fortran::lower::AbstractConverter {

builder->create<fir::FirEndOp>(loc);
builder->setInsertionPointAfter(op);
localSymbols.popScope();
}

void genFIR(const Fortran::parser::OpenMPConstruct &omp) {
Expand Down
15 changes: 4 additions & 11 deletions flang/lib/Lower/OpenMP/DataSharingProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ void DataSharingProcessor::insertDeallocs() {
fir::ExtendedValue symExV = converter.getSymbolExtendedValue(*sym);
mlir::omp::PrivateClauseOp privatizer = symToPrivatizer.at(sym);

symTable->pushScope();

lower::SymMapScope scope(*symTable);
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);

mlir::Region &deallocRegion = privatizer.getDeallocRegion();
Expand All @@ -108,8 +107,6 @@ void DataSharingProcessor::insertDeallocs() {

converter.createHostAssociateVarCloneDealloc(*sym);
firOpBuilder.create<mlir::omp::YieldOp>(hsb.getAddr().getLoc());

symTable->popScope();
}
}

Expand Down Expand Up @@ -488,8 +485,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
isFirstPrivate ? mlir::omp::DataSharingClauseType::FirstPrivate
: mlir::omp::DataSharingClauseType::Private);
fir::ExtendedValue symExV = converter.getSymbolExtendedValue(*sym);

symTable->pushScope();
lower::SymMapScope outerScope(*symTable);

// Populate the `alloc` region.
{
Expand All @@ -507,7 +503,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
.first;

symTable->addSymbol(*sym, localExV);
symTable->pushScope();
lower::SymMapScope innerScope(*symTable);
cloneSymbol(sym);
mlir::Value cloneAddr = symTable->shallowLookupSymbol(*sym).getAddr();
mlir::Type cloneType = cloneAddr.getType();
Expand All @@ -523,7 +519,6 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,

firOpBuilder.create<mlir::omp::YieldOp>(hsb.getAddr().getLoc(),
yieldedValue);
symTable->popScope();
}

// Populate the `copy` region if this is a `firstprivate`.
Expand All @@ -548,7 +543,7 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
};

addSymbol(0, true);
symTable->pushScope();
lower::SymMapScope innerScope(*symTable);
addSymbol(1);

auto ip = firOpBuilder.saveInsertionPoint();
Expand All @@ -557,10 +552,8 @@ void DataSharingProcessor::doPrivatize(const semantics::Symbol *sym,
firOpBuilder.create<mlir::omp::YieldOp>(
hsb.getAddr().getLoc(),
symTable->shallowLookupSymbol(*sym).getAddr());
symTable->popScope();
}

symTable->popScope();
return result;
}();

Expand Down
6 changes: 2 additions & 4 deletions flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,7 +1546,7 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
auto &builder = converter.getFirOpBuilder();

// Insert privatizations before SECTIONS
symTable.pushScope();
lower::SymMapScope scope(symTable);
DataSharingProcessor dsp(converter, semaCtx, item->clauses, eval,
lower::omp::isLastItemInQueue(item, queue));
dsp.processStep1();
Expand Down Expand Up @@ -1643,7 +1643,6 @@ genSectionsOp(lower::AbstractConverter &converter, lower::SymMap &symTable,
if (clauseOps.nowait && !lastprivates.empty())
builder.create<mlir::omp::BarrierOp>(loc);

symTable.popScope();
return sectionsOp;
}

Expand Down Expand Up @@ -2900,9 +2899,8 @@ void Fortran::lower::genOpenMPConstruct(lower::AbstractConverter &converter,
semantics::SemanticsContext &semaCtx,
lower::pft::Evaluation &eval,
const parser::OpenMPConstruct &omp) {
symTable.pushScope();
lower::SymMapScope scope(symTable);
genOMP(converter, symTable, semaCtx, eval, omp);
symTable.popScope();
}

void Fortran::lower::genOpenMPDeclarativeConstruct(
Expand Down

0 comments on commit 433ca3e

Please sign in to comment.