Skip to content

[flang] Test SYNC IMAGES, increase checking #132279

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 1 commit into from
Mar 26, 2025
Merged

Conversation

klausler
Copy link
Contributor

Add a test for the SYNC IMAGES statement, and add a check for invalid image numbers.

Add a test for the SYNC IMAGES statement, and add a check for
invalid image numbers.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 20, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

Add a test for the SYNC IMAGES statement, and add a check for invalid image numbers.


Full diff: https://github.com/llvm/llvm-project/pull/132279.diff

2 Files Affected:

  • (modified) flang/lib/Semantics/check-coarray.cpp (+20-1)
  • (added) flang/test/Semantics/sync-images.f90 (+12)
diff --git a/flang/lib/Semantics/check-coarray.cpp b/flang/lib/Semantics/check-coarray.cpp
index 9dd287e9d2495..b21e3cd757d6b 100644
--- a/flang/lib/Semantics/check-coarray.cpp
+++ b/flang/lib/Semantics/check-coarray.cpp
@@ -194,7 +194,6 @@ void CoarrayChecker::Leave(const parser::SyncAllStmt &x) {
 
 void CoarrayChecker::Leave(const parser::SyncImagesStmt &x) {
   CheckSyncStatList(context_, std::get<std::list<parser::StatOrErrmsg>>(x.t));
-
   const auto &imageSet{std::get<parser::SyncImagesStmt::ImageSet>(x.t)};
   if (const auto *intExpr{std::get_if<parser::IntExpr>(&imageSet.u)}) {
     if (const auto *expr{GetExpr(context_, *intExpr)}) {
@@ -202,6 +201,26 @@ void CoarrayChecker::Leave(const parser::SyncImagesStmt &x) {
         context_.Say(parser::FindSourceLocation(imageSet), // C1174
             "An image-set that is an int-expr must be a scalar or a rank-one array"_err_en_US);
       }
+      if (const auto *someInt{
+              std::get_if<evaluate::Expr<evaluate::SomeInteger>>(&expr->u)};
+          someInt && evaluate::IsActuallyConstant(*someInt)) {
+        auto converted{evaluate::Fold(context_.foldingContext(),
+            evaluate::ConvertToType<evaluate::SubscriptInteger>(
+                common::Clone(*someInt)))};
+        if (const auto *cst{
+                evaluate::UnwrapConstantValue<evaluate::SubscriptInteger>(
+                    converted)}) {
+          for (auto elt : cst->values()) {
+            auto n{elt.ToInt64()};
+            if (n < 1) {
+              context_.Say(parser::FindSourceLocation(imageSet),
+                  "Image number %jd in the image-set is not valid"_err_en_US,
+                  std::intmax_t{n});
+              break;
+            }
+          }
+        }
+      }
     }
   }
 }
diff --git a/flang/test/Semantics/sync-images.f90 b/flang/test/Semantics/sync-images.f90
new file mode 100644
index 0000000000000..637fa263354d0
--- /dev/null
+++ b/flang/test/Semantics/sync-images.f90
@@ -0,0 +1,12 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1
+integer twod(1,1)
+sync images (*) ! ok
+!ERROR: An image-set that is an int-expr must be a scalar or a rank-one array
+sync images (twod)
+!ERROR: Must have INTEGER type, but is REAL(4)
+sync images (3.14159)
+!ERROR: Image number -1 in the image-set is not valid
+sync images (-1)
+!ERROR: Image number -1 in the image-set is not valid
+sync images ([2, -1, 3])
+end

@JDPailleux
Copy link
Contributor

Maybe cand you add a check on values that shouldn't be repeated in the image set ?
Otherwise LGTM.

@klausler klausler merged commit 38207a5 into llvm:main Mar 26, 2025
14 checks passed
@klausler klausler deleted the fix0322 branch March 26, 2025 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants