Skip to content

[Flang][OpenMP] Finalize Statement context of clauses before op creation #71679

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 2 commits into from
Nov 9, 2023
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
28 changes: 10 additions & 18 deletions flang/lib/Lower/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,7 @@ class ClauseProcessor {
bool processDepend(llvm::SmallVectorImpl<mlir::Attribute> &dependTypeOperands,
llvm::SmallVectorImpl<mlir::Value> &dependOperands) const;
bool
processIf(Fortran::lower::StatementContext &stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
mlir::Value &result) const;
bool
processLink(llvm::SmallVectorImpl<DeclareTargetCapturePair> &result) const;
Expand Down Expand Up @@ -1116,7 +1115,6 @@ genDependKindAttr(fir::FirOpBuilder &firOpBuilder,

static mlir::Value getIfClauseOperand(
Fortran::lower::AbstractConverter &converter,
Fortran::lower::StatementContext &stmtCtx,
const Fortran::parser::OmpClause::If *ifClause,
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
mlir::Location clauseLocation) {
Expand All @@ -1127,6 +1125,7 @@ static mlir::Value getIfClauseOperand(
if (directive && directive.value() != directiveName)
return nullptr;

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

bool ClauseProcessor::processIf(
Fortran::lower::StatementContext &stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier directiveName,
mlir::Value &result) const {
bool found = false;
findRepeatableClause<ClauseTy::If>(
[&](const ClauseTy::If *ifClause,
const Fortran::parser::CharBlock &source) {
mlir::Location clauseLocation = converter.genLocation(source);
mlir::Value operand = getIfClauseOperand(converter, stmtCtx, ifClause,
mlir::Value operand = getIfClauseOperand(converter, ifClause,
directiveName, clauseLocation);
// Assume that, at most, a single 'if' clause will be applicable to the
// given directive.
Expand Down Expand Up @@ -2305,8 +2303,7 @@ genParallelOp(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::Parallel,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Parallel,
ifClauseOperand);
cp.processNumThreads(stmtCtx, numThreadsClauseOperand);
cp.processProcBind(procBindKindAttr);
Expand Down Expand Up @@ -2359,8 +2356,7 @@ genTaskOp(Fortran::lower::AbstractConverter &converter,
dependOperands;

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::Task,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Task,
ifClauseOperand);
cp.processAllocate(allocatorOperands, allocateOperands);
cp.processDefault();
Expand Down Expand Up @@ -2417,8 +2413,7 @@ genDataOp(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<const Fortran::semantics::Symbol *> useDeviceSymbols;

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::TargetData,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::TargetData,
ifClauseOperand);
cp.processDevice(stmtCtx, deviceOperand);
cp.processUseDevicePtr(devicePtrOperands, useDeviceTypes, useDeviceLocs,
Expand Down Expand Up @@ -2463,7 +2458,7 @@ genEnterExitDataOp(Fortran::lower::AbstractConverter &converter,
}

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx, directiveName, ifClauseOperand);
cp.processIf(directiveName, ifClauseOperand);
cp.processDevice(stmtCtx, deviceOperand);
cp.processNowait(nowaitAttr);
cp.processMap(currentLocation, directive, semanticsContext, stmtCtx,
Expand Down Expand Up @@ -2587,8 +2582,7 @@ genTargetOp(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<const Fortran::semantics::Symbol *> mapSymbols;

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::Target,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Target,
ifClauseOperand);
cp.processDevice(stmtCtx, deviceOperand);
cp.processThreadLimit(stmtCtx, threadLimitOperand);
Expand Down Expand Up @@ -2742,8 +2736,7 @@ genTeamsOp(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<mlir::Attribute> reductionDeclSymbols;

ClauseProcessor cp(converter, clauseList);
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::Teams,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Teams,
ifClauseOperand);
cp.processAllocate(allocatorOperands, allocateOperands);
cp.processDefault();
Expand Down Expand Up @@ -3018,8 +3011,7 @@ static void genOMP(Fortran::lower::AbstractConverter &converter,
llvm::SmallVector<mlir::Value> alignedVars, nontemporalVars;
mlir::Value ifClauseOperand;
mlir::IntegerAttr simdlenClauseOperand, safelenClauseOperand;
cp.processIf(stmtCtx,
Fortran::parser::OmpIfClause::DirectiveNameModifier::Simd,
cp.processIf(Fortran::parser::OmpIfClause::DirectiveNameModifier::Simd,
ifClauseOperand);
cp.processSimdlen(simdlenClauseOperand);
cp.processSafelen(safelenClauseOperand);
Expand Down
12 changes: 12 additions & 0 deletions flang/test/Lower/OpenMP/parallel-if.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
!RUN: %flang_fc1 -emit-hlfir -fopenmp %s -o - | FileCheck %s

!CHECK-LABEL: func @_QPtest1
subroutine test1(a)
integer :: a(:,:)
!CHECK: hlfir.destroy
!CHECK: omp.parallel if
!$omp parallel if(any(a .eq. 1))
!CHECK-NOT: hlfir.destroy
print *, "Hello"
!$omp end parallel
end subroutine