Skip to content

[Flang][OpenMP] fix crash on sematic error in atomic capture clause #140710

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
19 changes: 7 additions & 12 deletions flang/include/flang/Semantics/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -764,19 +764,14 @@ inline bool checkForSingleVariableOnRHS(
return designator != nullptr;
}

/// Checks if the symbol on the LHS of the assignment statement is present in
/// the RHS expression.
inline bool checkForSymbolMatch(
const Fortran::parser::AssignmentStmt &assignmentStmt) {
const auto &var{std::get<Fortran::parser::Variable>(assignmentStmt.t)};
const auto &expr{std::get<Fortran::parser::Expr>(assignmentStmt.t)};
const auto *e{Fortran::semantics::GetExpr(expr)};
const auto *v{Fortran::semantics::GetExpr(var)};
auto varSyms{Fortran::evaluate::GetSymbolVector(*v)};
const Fortran::semantics::Symbol &varSymbol{*varSyms.front()};
/// Checks if the symbol on the LHS is present in the RHS expression.
inline bool checkForSymbolMatch(const Fortran::semantics::SomeExpr *lhs,
const Fortran::semantics::SomeExpr *rhs) {
auto lhsSyms{Fortran::evaluate::GetSymbolVector(*lhs)};
const Fortran::semantics::Symbol &lhsSymbol{*lhsSyms.front()};
for (const Fortran::semantics::Symbol &symbol :
Fortran::evaluate::GetSymbolVector(*e)) {
if (varSymbol == symbol) {
Fortran::evaluate::GetSymbolVector(*rhs)) {
if (lhsSymbol == symbol) {
return true;
}
}
Expand Down
4 changes: 3 additions & 1 deletion flang/lib/Lower/OpenACC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,9 @@ void genAtomicCapture(Fortran::lower::AbstractConverter &converter,
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
if (Fortran::semantics::checkForSingleVariableOnRHS(stmt1)) {
if (Fortran::semantics::checkForSymbolMatch(stmt2)) {
if (Fortran::semantics::checkForSymbolMatch(
Fortran::semantics::GetExpr(stmt2Var),
Fortran::semantics::GetExpr(stmt2Expr))) {
// Atomic capture construct is of the form [capture-stmt, update-stmt]
const Fortran::semantics::SomeExpr &fromExpr =
*Fortran::semantics::GetExpr(stmt1Expr);
Expand Down
3 changes: 2 additions & 1 deletion flang/lib/Lower/OpenMP/OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3198,7 +3198,8 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
if (semantics::checkForSingleVariableOnRHS(stmt1)) {
if (semantics::checkForSymbolMatch(stmt2)) {
if (semantics::checkForSymbolMatch(semantics::GetExpr(stmt2Var),
semantics::GetExpr(stmt2Expr))) {
// Atomic capture construct is of the form [capture-stmt, update-stmt]
const semantics::SomeExpr &fromExpr = *semantics::GetExpr(stmt1Expr);
mlir::Type elementType = converter.genType(fromExpr);
Expand Down
62 changes: 32 additions & 30 deletions flang/lib/Semantics/check-omp-structure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2910,45 +2910,47 @@ void OmpStructureChecker::CheckAtomicCaptureConstruct(
.v.statement;
const auto &stmt1Var{std::get<parser::Variable>(stmt1.t)};
const auto &stmt1Expr{std::get<parser::Expr>(stmt1.t)};
const auto *v1 = GetExpr(context_, stmt1Var);
const auto *e1 = GetExpr(context_, stmt1Expr);

const parser::AssignmentStmt &stmt2 =
std::get<parser::OmpAtomicCapture::Stmt2>(atomicCaptureConstruct.t)
.v.statement;
const auto &stmt2Var{std::get<parser::Variable>(stmt2.t)};
const auto &stmt2Expr{std::get<parser::Expr>(stmt2.t)};

if (semantics::checkForSingleVariableOnRHS(stmt1)) {
CheckAtomicCaptureStmt(stmt1);
if (semantics::checkForSymbolMatch(stmt2)) {
// ATOMIC CAPTURE construct is of the form [capture-stmt, update-stmt]
CheckAtomicUpdateStmt(stmt2);
const auto *v2 = GetExpr(context_, stmt2Var);
const auto *e2 = GetExpr(context_, stmt2Expr);

if (e1 && v1 && e2 && v2) {
if (semantics::checkForSingleVariableOnRHS(stmt1)) {
CheckAtomicCaptureStmt(stmt1);
if (semantics::checkForSymbolMatch(v2, e2)) {
// ATOMIC CAPTURE construct is of the form [capture-stmt, update-stmt]
CheckAtomicUpdateStmt(stmt2);
} else {
// ATOMIC CAPTURE construct is of the form [capture-stmt, write-stmt]
CheckAtomicWriteStmt(stmt2);
}
if (!(*e1 == *v2)) {
context_.Say(stmt1Expr.source,
"Captured variable/array element/derived-type component %s expected to be assigned in the second statement of ATOMIC CAPTURE construct"_err_en_US,
stmt1Expr.source);
}
} else if (semantics::checkForSymbolMatch(v1, e1) &&
semantics::checkForSingleVariableOnRHS(stmt2)) {
// ATOMIC CAPTURE construct is of the form [update-stmt, capture-stmt]
CheckAtomicUpdateStmt(stmt1);
CheckAtomicCaptureStmt(stmt2);
// Variable updated in stmt1 should be captured in stmt2
if (!(*v1 == *e2)) {
context_.Say(stmt1Var.GetSource(),
"Updated variable/array element/derived-type component %s expected to be captured in the second statement of ATOMIC CAPTURE construct"_err_en_US,
stmt1Var.GetSource());
}
} else {
// ATOMIC CAPTURE construct is of the form [capture-stmt, write-stmt]
CheckAtomicWriteStmt(stmt2);
}
auto *v{stmt2Var.typedExpr.get()};
auto *e{stmt1Expr.typedExpr.get()};
if (v && e && !(v->v == e->v)) {
context_.Say(stmt1Expr.source,
"Captured variable/array element/derived-type component %s expected to be assigned in the second statement of ATOMIC CAPTURE construct"_err_en_US,
stmt1Expr.source);
}
} else if (semantics::checkForSymbolMatch(stmt1) &&
semantics::checkForSingleVariableOnRHS(stmt2)) {
// ATOMIC CAPTURE construct is of the form [update-stmt, capture-stmt]
CheckAtomicUpdateStmt(stmt1);
CheckAtomicCaptureStmt(stmt2);
// Variable updated in stmt1 should be captured in stmt2
auto *v{stmt1Var.typedExpr.get()};
auto *e{stmt2Expr.typedExpr.get()};
if (v && e && !(v->v == e->v)) {
context_.Say(stmt1Var.GetSource(),
"Updated variable/array element/derived-type component %s expected to be captured in the second statement of ATOMIC CAPTURE construct"_err_en_US,
stmt1Var.GetSource());
"Invalid ATOMIC CAPTURE construct statements. Expected one of [update-stmt, capture-stmt], [capture-stmt, update-stmt], or [capture-stmt, write-stmt]"_err_en_US);
}
} else {
context_.Say(stmt1Expr.source,
"Invalid ATOMIC CAPTURE construct statements. Expected one of [update-stmt, capture-stmt], [capture-stmt, update-stmt], or [capture-stmt, write-stmt]"_err_en_US);
}
}

Expand Down
22 changes: 22 additions & 0 deletions flang/test/Semantics/OpenMP/atomic-capture-invalid.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
! REQUIRES: openmp_runtime

! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags
! Semantic checks on invalid atomic capture clause

use omp_lib
logical x
complex y
!$omp atomic capture
!ERROR: No intrinsic or user-defined ASSIGNMENT(=) matches operand types LOGICAL(4) and COMPLEX(4)
x = y
!ERROR: Operands of + must be numeric; have COMPLEX(4) and LOGICAL(4)
y = y + x
!$omp end atomic

!$omp atomic capture
!ERROR: Operands of + must be numeric; have COMPLEX(4) and LOGICAL(4)
y = y + x
!ERROR: No intrinsic or user-defined ASSIGNMENT(=) matches operand types LOGICAL(4) and COMPLEX(4)
x = y
!$omp end atomic
end