[flang-commits] [flang] [not for review] Vol actual checks OLD (PR #150040)

Eugene Epshteyn via flang-commits flang-commits at lists.llvm.org
Tue Jul 22 08:18:55 PDT 2025


https://github.com/eugeneepshteyn created https://github.com/llvm/llvm-project/pull/150040

This is not correct implementation, this PR is for easy reference not for review.

>From 50dd216ff0487709d52944006e35e6102015cef0 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Mon, 7 Jul 2025 10:20:38 -0400
Subject: [PATCH 1/4] [flang] Add -pedantic check for passing non-VOLATILE
 array section to VOLATILE dummy arg

Add the check, which partially addresses https://github.com/llvm/llvm-project/issues/137369

Implement HasTriplet().
---
 flang/include/flang/Evaluate/tools.h |  3 +++
 flang/lib/Evaluate/tools.cpp         | 19 +++++++++++++++++++
 flang/lib/Semantics/definable.cpp    |  3 +++
 3 files changed, 25 insertions(+)

diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index cad1b634f8924..0604049fd6a6d 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -1123,6 +1123,9 @@ extern template semantics::UnorderedSymbolSet CollectCudaSymbols(
 // Predicate: does a variable contain a vector-valued subscript (not a triplet)?
 bool HasVectorSubscript(const Expr<SomeType> &);
 
+// Predicate: does a variable contain a triplet?
+bool HasTriplet(const Expr<SomeType> &);
+
 // Predicate: does an expression contain constant?
 bool HasConstant(const Expr<SomeType> &);
 
diff --git a/flang/lib/Evaluate/tools.cpp b/flang/lib/Evaluate/tools.cpp
index fcacdb93d662b..50a201dfcaced 100644
--- a/flang/lib/Evaluate/tools.cpp
+++ b/flang/lib/Evaluate/tools.cpp
@@ -1173,6 +1173,25 @@ bool HasVectorSubscript(const Expr<SomeType> &expr) {
   return HasVectorSubscriptHelper{}(expr);
 }
 
+// HasTriplet()
+struct HasTripletHelper
+    : public AnyTraverse<HasTripletHelper, bool,
+          /*TraverseAssocEntityDetails=*/false> {
+  using Base = AnyTraverse<HasTripletHelper, bool, false>;
+  HasTripletHelper() : Base{*this} {}
+  using Base::operator();
+  bool operator()(const Subscript &ss) const {
+    return std::holds_alternative<Triplet>(ss.u);
+  }
+  bool operator()(const ProcedureRef &) const {
+    return false; // don't descend into function call arguments
+  }
+};
+
+bool HasTriplet(const Expr<SomeType> &expr) {
+  return HasTripletHelper{}(expr);
+}
+
 // HasConstant()
 struct HasConstantHelper : public AnyTraverse<HasConstantHelper, bool,
                                /*TraverseAssocEntityDetails=*/false> {
diff --git a/flang/lib/Semantics/definable.cpp b/flang/lib/Semantics/definable.cpp
index 08cb268b318ae..b2d2330887a53 100644
--- a/flang/lib/Semantics/definable.cpp
+++ b/flang/lib/Semantics/definable.cpp
@@ -371,6 +371,9 @@ std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
         return parser::Message{at,
             "Variable '%s' has a vector subscript"_err_en_US, expr.AsFortran()};
       }
+    } else if (evaluate::HasTriplet(expr)) {
+      return parser::Message{at,
+          "Variable '%s' has array section"_err_en_US, expr.AsFortran()};
     }
     if (FindPureProcedureContaining(scope) &&
         evaluate::ExtractCoarrayRef(expr)) {

>From f5379722f62ea5066261055f3781c8788cf6f929 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Jul 2025 22:24:06 -0400
Subject: [PATCH 2/4] Added DefinabilityFlag::VolatileNotDefinable

---
 flang/lib/Semantics/check-call.cpp | 4 ++--
 flang/lib/Semantics/definable.cpp  | 3 ++-
 flang/lib/Semantics/definable.h    | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/flang/lib/Semantics/check-call.cpp b/flang/lib/Semantics/check-call.cpp
index 6f2503285013d..037d735b6d75f 100644
--- a/flang/lib/Semantics/check-call.cpp
+++ b/flang/lib/Semantics/check-call.cpp
@@ -734,8 +734,8 @@ static void CheckExplicitDataArg(const characteristics::DummyDataObject &dummy,
               characteristics::DummyDataObject::Attr::Asynchronous)) {
         undefinableMessage =
             "Actual argument associated with ASYNCHRONOUS %s is not definable"_warn_en_US;
-      } else if (dummy.attrs.test(
-                     characteristics::DummyDataObject::Attr::Volatile)) {
+      } else if (dummyIsVolatile) {
+        flags.set(DefinabilityFlag::VolatileNotDefinable);
         undefinableMessage =
             "Actual argument associated with VOLATILE %s is not definable"_warn_en_US;
       }
diff --git a/flang/lib/Semantics/definable.cpp b/flang/lib/Semantics/definable.cpp
index b2d2330887a53..4a387303da8a2 100644
--- a/flang/lib/Semantics/definable.cpp
+++ b/flang/lib/Semantics/definable.cpp
@@ -371,7 +371,8 @@ std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
         return parser::Message{at,
             "Variable '%s' has a vector subscript"_err_en_US, expr.AsFortran()};
       }
-    } else if (evaluate::HasTriplet(expr)) {
+    } else if (flags.test(DefinabilityFlag::VolatileNotDefinable) &&
+               evaluate::HasTriplet(expr)) {
       return parser::Message{at,
           "Variable '%s' has array section"_err_en_US, expr.AsFortran()};
     }
diff --git a/flang/lib/Semantics/definable.h b/flang/lib/Semantics/definable.h
index 0d027961417be..879d46a2f526c 100644
--- a/flang/lib/Semantics/definable.h
+++ b/flang/lib/Semantics/definable.h
@@ -33,7 +33,7 @@ ENUM_CLASS(DefinabilityFlag,
     SourcedAllocation, // ALLOCATE(a,SOURCE=)
     PolymorphicOkInPure, // don't check for polymorphic type in pure subprogram
     DoNotNoteDefinition, // context does not imply definition
-    AllowEventLockOrNotifyType, PotentialDeallocation)
+    AllowEventLockOrNotifyType, PotentialDeallocation, VolatileNotDefinable)
 
 using DefinabilityFlags =
     common::EnumSet<DefinabilityFlag, DefinabilityFlag_enumSize>;

>From 411eb310645b04dca904415a74fbef2243c1879c Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Jul 2025 22:24:34 -0400
Subject: [PATCH 3/4] clang-format

---
 flang/lib/Semantics/definable.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/lib/Semantics/definable.cpp b/flang/lib/Semantics/definable.cpp
index 4a387303da8a2..77bb7ceae65a6 100644
--- a/flang/lib/Semantics/definable.cpp
+++ b/flang/lib/Semantics/definable.cpp
@@ -372,7 +372,7 @@ std::optional<parser::Message> WhyNotDefinable(parser::CharBlock at,
             "Variable '%s' has a vector subscript"_err_en_US, expr.AsFortran()};
       }
     } else if (flags.test(DefinabilityFlag::VolatileNotDefinable) &&
-               evaluate::HasTriplet(expr)) {
+        evaluate::HasTriplet(expr)) {
       return parser::Message{at,
           "Variable '%s' has array section"_err_en_US, expr.AsFortran()};
     }

>From fcba7f4c8567aaad6928446a25d3cd66790297d9 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Tue, 8 Jul 2025 23:17:05 -0400
Subject: [PATCH 4/4] Updated flang/test/Semantics/call03.f90 with the new
 warning info

---
 flang/test/Semantics/call03.f90 | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/flang/test/Semantics/call03.f90 b/flang/test/Semantics/call03.f90
index 1721b59986862..0dd4f300c4f50 100644
--- a/flang/test/Semantics/call03.f90
+++ b/flang/test/Semantics/call03.f90
@@ -362,16 +362,28 @@ subroutine test15(assumedrank) ! C1539
     call valueassumedsize(c(::2)) ! ok
     call valueassumedsize(d(::2)) ! ok
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'b(::2_8)' has array section
     call volatileassumedsize(b(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'b(::2_8)' has array section
     call volatilecontiguous(b(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'c(::2_8)' has array section
     call volatileassumedsize(c(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'c(::2_8)' has array section
     call volatilecontiguous(c(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'd(::2_8)' has array section
     call volatileassumedsize(d(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
+    !ERROR: Actual argument associated with VOLATILE dummy argument 'x=' is not definable [-Wundefinable-asynchronous-or-volatile-actual]
+    !BECAUSE: Variable 'd(::2_8)' has array section
     call volatilecontiguous(d(::2))
     !ERROR: ASYNCHRONOUS or VOLATILE actual argument that is not simply contiguous may not be associated with a contiguous ASYNCHRONOUS or VOLATILE dummy argument 'x='
     call volatilecontiguous(assumedrank)



More information about the flang-commits mailing list