Skip to content

[Flang][Lower] NFC: Introduce SymMapScope helper class #107866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading