Skip to content

Commit 848ae10

Browse files
[Flang][OpenMP] Finalize Statement context of clauses before op creation (#71679)
Currently Statement context used during processing of clauses is finalized after the OpenMP operation creation. This causes the finalization to go inside the OpenMP region and this can lead to multiple finalizations in a parallel region. This fix proposes to finalize them before the OpenMP op creation. Fixes #71342
1 parent cace715 commit 848ae10

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

flang/lib/Lower/OpenMP.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ class ClauseProcessor {
561561
bool processDepend(llvm::SmallVectorImpl<mlir::Attribute> &dependTypeOperands,
562562
llvm::SmallVectorImpl<mlir::Value> &dependOperands) const;
563563
bool
564-
processIf(Fortran::lower::StatementContext &stmtCtx,
565-
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
564+
processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
566565
mlir::Value &result) const;
567566
bool
568567
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
@@ -1116,7 +1115,6 @@ genDependKindAttr(fir::FirOpBuilder &firOpBuilder,
11161115

11171116
static mlir::Value getIfClauseOperand(
11181117
Fortran::lower::AbstractConverter &converter,
1119-
Fortran::lower::StatementContext &stmtCtx,
11201118
const Fortran::parser::OmpClause::If *ifClause,
11211119
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
11221120
mlir::Location clauseLocation) {
@@ -1127,6 +1125,7 @@ static mlir::Value getIfClauseOperand(
11271125
if (directive && directive.value() != directiveName)
11281126
return nullptr;
11291127

1128+
Fortran::lower::StatementContext stmtCtx;
11301129
fir::FirOpBuilder &firOpBuilder = converter.getFirOpBuilder();
11311130
auto &expr = std::get<Fortran::parser::ScalarLogicalExpr>(ifClause->v.t);
11321131
mlir::Value ifVal = fir::getBase(
@@ -1672,15 +1671,14 @@ bool ClauseProcessor::processDepend(
16721671
}
16731672

16741673
bool ClauseProcessor::processIf(
1675-
Fortran::lower::StatementContext &stmtCtx,
16761674
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
16771675
mlir::Value &result) const {
16781676
bool found = false;
16791677
findRepeatableClause<ClauseTy::If>(
16801678
[&](const ClauseTy::If *ifClause,
16811679
const Fortran::parser::CharBlock &source) {
16821680
mlir::Location clauseLocation = converter.genLocation(source);
1683-
mlir::Value operand = getIfClauseOperand(converter, stmtCtx, ifClause,
1681+
mlir::Value operand = getIfClauseOperand(converter, ifClause,
16841682
directiveName, clauseLocation);
16851683
// Assume that, at most, a single 'if' clause will be applicable to the
16861684
// given directive.
@@ -2305,8 +2303,7 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
23052303
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
23062304

23072305
ClauseProcessor cp(converter, clauseList);
2308-
cp.processIf(stmtCtx,
2309-
Fortran::parser::OmpIfClause::DirectiveNameModifier::Parallel,
2306+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Parallel,
23102307
ifClauseOperand);
23112308
cp.processNumThreads(stmtCtx, numThreadsClauseOperand);
23122309
cp.processProcBind(procBindKindAttr);
@@ -2360,8 +2357,7 @@ genTaskOp(Fortran::lower::AbstractConverter &converter,
23602357
dependOperands;
23612358

23622359
ClauseProcessor cp(converter, clauseList);
2363-
cp.processIf(stmtCtx,
2364-
Fortran::parser::OmpIfClause::DirectiveNameModifier::Task,
2360+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Task,
23652361
ifClauseOperand);
23662362
cp.processAllocate(allocatorOperands, allocateOperands);
23672363
cp.processDefault();
@@ -2418,8 +2414,7 @@ genDataOp(Fortran::lower::AbstractConverter &converter,
24182414
llvm::SmallVector<const Fortran::semantics::Symbol *> useDeviceSymbols;
24192415

24202416
ClauseProcessor cp(converter, clauseList);
2421-
cp.processIf(stmtCtx,
2422-
Fortran::parser::OmpIfClause::DirectiveNameModifier::TargetData,
2417+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::TargetData,
24232418
ifClauseOperand);
24242419
cp.processDevice(stmtCtx, deviceOperand);
24252420
cp.processUseDevicePtr(devicePtrOperands, useDeviceTypes, useDeviceLocs,
@@ -2464,7 +2459,7 @@ genEnterExitDataOp(Fortran::lower::AbstractConverter &converter,
24642459
}
24652460

24662461
ClauseProcessor cp(converter, clauseList);
2467-
cp.processIf(stmtCtx, directiveName, ifClauseOperand);
2462+
cp.processIf(directiveName, ifClauseOperand);
24682463
cp.processDevice(stmtCtx, deviceOperand);
24692464
cp.processNowait(nowaitAttr);
24702465
cp.processMap(currentLocation, directive, semanticsContext, stmtCtx,
@@ -2588,8 +2583,7 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
25882583
llvm::SmallVector<const Fortran::semantics::Symbol *> mapSymbols;
25892584

25902585
ClauseProcessor cp(converter, clauseList);
2591-
cp.processIf(stmtCtx,
2592-
Fortran::parser::OmpIfClause::DirectiveNameModifier::Target,
2586+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Target,
25932587
ifClauseOperand);
25942588
cp.processDevice(stmtCtx, deviceOperand);
25952589
cp.processThreadLimit(stmtCtx, threadLimitOperand);
@@ -2743,8 +2737,7 @@ genTeamsOp(Fortran::lower::AbstractConverter &converter,
27432737
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;
27442738

27452739
ClauseProcessor cp(converter, clauseList);
2746-
cp.processIf(stmtCtx,
2747-
Fortran::parser::OmpIfClause::DirectiveNameModifier::Teams,
2740+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Teams,
27482741
ifClauseOperand);
27492742
cp.processAllocate(allocatorOperands, allocateOperands);
27502743
cp.processDefault();
@@ -3019,8 +3012,7 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
30193012
llvm::SmallVector<mlir::Value> alignedVars, nontemporalVars;
30203013
mlir::Value ifClauseOperand;
30213014
mlir::IntegerAttr simdlenClauseOperand, safelenClauseOperand;
3022-
cp.processIf(stmtCtx,
3023-
Fortran::parser::OmpIfClause::DirectiveNameModifier::Simd,
3015+
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Simd,
30243016
ifClauseOperand);
30253017
cp.processSimdlen(simdlenClauseOperand);
30263018
cp.processSafelen(safelenClauseOperand);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s
2+
3+
!CHECK-LABEL: func @_QPtest1
4+
subroutine test1(a)
5+
integer :: a(:,:)
6+
!CHECK: hlfir.destroy
7+
!CHECK: omp.parallel if
8+
!$omp parallel if(any(a .eq. 1))
9+
!CHECK-NOT: hlfir.destroy
10+
print *, "Hello"
11+
!$omp end parallel
12+
end subroutine

0 commit comments

Comments
 (0)