[flang-commits] [flang] [flang][NFC] Clean up code in two new functions (PR #142037)
Peter Klausler via flang-commits
flang-commits at lists.llvm.org
Thu May 29 14:14:25 PDT 2025
https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/142037
>From 6a6edec1e09e9123a8b9d1e1e059b0beee6e4aaf Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 29 May 2025 14:13:41 -0700
Subject: [PATCH] [flang][NFC] Clean up code in two new functions
Two recently-added functions in Semantics/tools.h need some cleaning
up to conform to the coding style of the project. One of them should
actually be in Parser/tools.{h,cpp}, the other doesn't need to be
defined in the header.
---
flang/include/flang/Parser/tools.h | 3 +++
flang/include/flang/Semantics/tools.h | 26 ++-------------------
flang/lib/Lower/OpenACC.cpp | 4 ++--
flang/lib/Lower/OpenMP/OpenMP.cpp | 4 ++--
flang/lib/Parser/tools.cpp | 6 +++++
flang/lib/Semantics/check-omp-structure.cpp | 8 +++----
flang/lib/Semantics/tools.cpp | 12 ++++++++++
7 files changed, 31 insertions(+), 32 deletions(-)
diff --git a/flang/include/flang/Parser/tools.h b/flang/include/flang/Parser/tools.h
index f1ead11734fa0..447bccd5d35a6 100644
--- a/flang/include/flang/Parser/tools.h
+++ b/flang/include/flang/Parser/tools.h
@@ -250,5 +250,8 @@ template <typename A> std::optional<CharBlock> GetLastSource(A &x) {
return GetSourceHelper<false>::GetSource(const_cast<const A &>(x));
}
+// Checks whether the assignment statement has a single variable on the RHS.
+bool CheckForSingleVariableOnRHS(const AssignmentStmt &);
+
} // namespace Fortran::parser
#endif // FORTRAN_PARSER_TOOLS_H_
diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index 3839bc1d2a215..4b2bb4fa167f8 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -753,29 +753,7 @@ std::string GetCommonBlockObjectName(const Symbol &, bool underscoring);
// Check for ambiguous USE associations
bool HadUseError(SemanticsContext &, SourceName at, const Symbol *);
-/// Checks if the assignment statement has a single variable on the RHS.
-inline bool checkForSingleVariableOnRHS(
- const Fortran::parser::AssignmentStmt &assignmentStmt) {
- const Fortran::parser::Expr &expr{
- std::get<Fortran::parser::Expr>(assignmentStmt.t)};
- const Fortran::common::Indirection<Fortran::parser::Designator> *designator =
- std::get_if<Fortran::common::Indirection<Fortran::parser::Designator>>(
- &expr.u);
- return designator != nullptr;
-}
-
-/// 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(*rhs)) {
- if (lhsSymbol == symbol) {
- return true;
- }
- }
- return false;
-}
+// Checks whether the symbol on the LHS is present in the RHS expression.
+bool CheckForSymbolMatch(const SomeExpr *lhs, const SomeExpr *rhs);
} // namespace Fortran::semantics
#endif // FORTRAN_SEMANTICS_TOOLS_H_
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 02dba22c29c7f..c10e1777614cd 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -653,8 +653,8 @@ void genAtomicCapture(Fortran::lower::AbstractConverter &converter,
firOpBuilder.createBlock(&(atomicCaptureOp->getRegion(0)));
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
- if (Fortran::semantics::checkForSingleVariableOnRHS(stmt1)) {
- if (Fortran::semantics::checkForSymbolMatch(
+ if (Fortran::parser::CheckForSingleVariableOnRHS(stmt1)) {
+ if (Fortran::semantics::CheckForSymbolMatch(
Fortran::semantics::GetExpr(stmt2Var),
Fortran::semantics::GetExpr(stmt2Expr))) {
// Atomic capture construct is of the form [capture-stmt, update-stmt]
diff --git a/flang/lib/Lower/OpenMP/OpenMP.cpp b/flang/lib/Lower/OpenMP/OpenMP.cpp
index ddb08f74b3841..e07f33671e728 100644
--- a/flang/lib/Lower/OpenMP/OpenMP.cpp
+++ b/flang/lib/Lower/OpenMP/OpenMP.cpp
@@ -3199,8 +3199,8 @@ static void genAtomicCapture(lower::AbstractConverter &converter,
firOpBuilder.createBlock(&(atomicCaptureOp->getRegion(0)));
mlir::Block &block = atomicCaptureOp->getRegion(0).back();
firOpBuilder.setInsertionPointToStart(&block);
- if (semantics::checkForSingleVariableOnRHS(stmt1)) {
- if (semantics::checkForSymbolMatch(semantics::GetExpr(stmt2Var),
+ if (parser::CheckForSingleVariableOnRHS(stmt1)) {
+ 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);
diff --git a/flang/lib/Parser/tools.cpp b/flang/lib/Parser/tools.cpp
index 6e5f1ed2fc66f..85f0858a8f147 100644
--- a/flang/lib/Parser/tools.cpp
+++ b/flang/lib/Parser/tools.cpp
@@ -174,4 +174,10 @@ const CoindexedNamedObject *GetCoindexedNamedObject(
},
allocateObject.u);
}
+
+bool CheckForSingleVariableOnRHS(const AssignmentStmt &assignmentStmt) {
+ const Expr &expr{std::get<Expr>(assignmentStmt.t)};
+ return std::holds_alternative<common::Indirection<Designator>>(expr.u);
+}
+
} // namespace Fortran::parser
diff --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index bda0d62829506..ae5dca1b95f6e 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -2922,9 +2922,9 @@ void OmpStructureChecker::CheckAtomicCaptureConstruct(
const auto *e2 = GetExpr(context_, stmt2Expr);
if (e1 && v1 && e2 && v2) {
- if (semantics::checkForSingleVariableOnRHS(stmt1)) {
+ if (parser::CheckForSingleVariableOnRHS(stmt1)) {
CheckAtomicCaptureStmt(stmt1);
- if (semantics::checkForSymbolMatch(v2, e2)) {
+ if (CheckForSymbolMatch(v2, e2)) {
// ATOMIC CAPTURE construct is of the form [capture-stmt, update-stmt]
CheckAtomicUpdateStmt(stmt2);
} else {
@@ -2936,8 +2936,8 @@ void OmpStructureChecker::CheckAtomicCaptureConstruct(
"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)) {
+ } else if (CheckForSymbolMatch(v1, e1) &&
+ parser::CheckForSingleVariableOnRHS(stmt2)) {
// ATOMIC CAPTURE construct is of the form [update-stmt, capture-stmt]
CheckAtomicUpdateStmt(stmt1);
CheckAtomicCaptureStmt(stmt2);
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index 1d1e3ac044166..d8e9385f6973c 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -1756,4 +1756,16 @@ bool HadUseError(
}
}
+bool CheckForSymbolMatch(const SomeExpr *lhs, const SomeExpr *rhs) {
+ if (lhs && rhs) {
+ if (const Symbol *first{evaluate::GetFirstSymbol(*lhs)}) {
+ for (const Symbol &symbol : evaluate::GetSymbolVector(*rhs)) {
+ if (first == &symbol) {
+ return true;
+ }
+ }
+ }
+ }
+ return false;
+}
} // namespace Fortran::semantics
More information about the flang-commits
mailing list