Skip to content

Commit 9dbf3e2

Browse files
authored
[Flang][OpenMP] NFC: Simplify handling of insertion points (#89221)
This patch replaces some `saveInsertionPoint`, `restoreInsertionPoint` call pairs for an `InsertionGuard` instance where it makes sense within Flang OpenMP lowering to make further modifications less error-prone.
1 parent f76475c commit 9dbf3e2

File tree

3 files changed

+6
-12
lines changed

3 files changed

+6
-12
lines changed

flang/lib/Lower/OpenMP/DataSharingProcessor.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void DataSharingProcessor::insertBarrier() {
136136

137137
void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
138138
bool cmpCreated = false;
139-
mlir::OpBuilder::InsertPoint localInsPt = firOpBuilder.saveInsertionPoint();
139+
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
140140
for (const omp::Clause &clause : clauses) {
141141
if (clause.id != llvm::omp::OMPC_lastprivate)
142142
continue;
@@ -203,12 +203,11 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
203203
// Lastprivate operation is inserted at the end
204204
// of the lexically last section in the sections
205205
// construct
206-
mlir::OpBuilder::InsertPoint unstructuredSectionsIP =
207-
firOpBuilder.saveInsertionPoint();
206+
mlir::OpBuilder::InsertionGuard unstructuredSectionsGuard(
207+
firOpBuilder);
208208
mlir::Operation *lastOper = op->getRegion(0).back().getTerminator();
209209
firOpBuilder.setInsertionPoint(lastOper);
210210
lastPrivIP = firOpBuilder.saveInsertionPoint();
211-
firOpBuilder.restoreInsertionPoint(unstructuredSectionsIP);
212211
}
213212
}
214213
} else if (mlir::isa<mlir::omp::WsloopOp>(op)) {
@@ -268,7 +267,6 @@ void DataSharingProcessor::insertLastPrivateCompare(mlir::Operation *op) {
268267
"simd/worksharing-loop");
269268
}
270269
}
271-
firOpBuilder.restoreInsertionPoint(localInsPt);
272270
}
273271

274272
void DataSharingProcessor::collectSymbols(
@@ -372,7 +370,7 @@ void DataSharingProcessor::doPrivatize(
372370
uniquePrivatizerName))
373371
return existingPrivatizer;
374372

375-
auto ip = firOpBuilder.saveInsertionPoint();
373+
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
376374
firOpBuilder.setInsertionPoint(&moduleOp.getBodyRegion().front(),
377375
moduleOp.getBodyRegion().front().begin());
378376
auto result = firOpBuilder.create<mlir::omp::PrivateClauseOp>(
@@ -424,7 +422,6 @@ void DataSharingProcessor::doPrivatize(
424422
}
425423

426424
symTable->popScope();
427-
firOpBuilder.restoreInsertionPoint(ip);
428425
return result;
429426
}();
430427

flang/lib/Lower/OpenMP/OpenMP.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static void threadPrivatizeVars(Fortran::lower::AbstractConverter &converter,
134134
Fortran::lower::pft::Evaluation &eval) {
135135
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
136136
mlir::Location currentLocation = converter.getCurrentLocation();
137-
mlir::OpBuilder::InsertPoint insPt = firOpBuilder.saveInsertionPoint();
137+
mlir::OpBuilder::InsertionGuard guard(firOpBuilder);
138138
firOpBuilder.setInsertionPointToStart(firOpBuilder.getAllocaBlock());
139139

140140
// If the symbol corresponds to the original ThreadprivateOp, use the symbol
@@ -197,8 +197,6 @@ static void threadPrivatizeVars(Fortran::lower::AbstractConverter &converter,
197197
getExtendedValue(sexv, symThreadprivateValue);
198198
converter.bindSymbol(*sym, symThreadprivateExv);
199199
}
200-
201-
firOpBuilder.restoreInsertionPoint(insPt);
202200
}
203201

204202
static mlir::Operation *

flang/lib/Lower/OpenMP/ReductionProcessor.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,8 @@ createReductionInitRegion(fir::FirOpBuilder &builder, mlir::Location loc,
486486
assert(cstNeedsDealloc.has_value() &&
487487
"createTempFromMold decides this statically");
488488
if (cstNeedsDealloc.has_value() && *cstNeedsDealloc != false) {
489-
auto insPt = builder.saveInsertionPoint();
489+
mlir::OpBuilder::InsertionGuard guard(builder);
490490
createReductionCleanupRegion(builder, loc, reductionDecl);
491-
builder.restoreInsertionPoint(insPt);
492491
}
493492

494493
// Put the temporary inside of a box:

0 commit comments

Comments
 (0)