Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[flang][volatile] Get volatility of designators from base instead of component symbol #138611

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

Conversation

akuhlens
Copy link
Contributor

@akuhlens akuhlens commented May 5, 2025

The standard says in [8.5.20 VOLATILE attribute]:
If an object has the VOLATILE attribute, then all of its sub-objects also have the VOLATILE attribute.

This code takes this into account and uses the volatility of the base of the designator instead of that of the component. In fact, fields in a structure are not allowed to have the volatile attribute. So given the code, A%B => t, symbol B could never directly have the volatile attribute, and the volatility of A indicates the volatility of B.

This PR should address the comments on this PR #132486

@akuhlens akuhlens requested a review from ashermancinelli May 5, 2025 23:21
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels May 5, 2025
@llvmbot
Copy link
Member

llvmbot commented May 5, 2025

@llvm/pr-subscribers-flang-semantics

Author: Andre Kuhlenschmidt (akuhlens)

Changes

The standard says in [8.5.20 VOLATILE attribute]:
If an object has the VOLATILE attribute, then all of its sub-objects also have the VOLATILE attribute.

This code takes this into account and uses the volatility of the base of the designator instead of that of the component. In fact, fields in a structure are not allowed to have the volatile attribute. So given the code, A%B => t, symbol B could never directly have the volatile attribute, and the volatility of A indicates the volatility of B.

This PR should address the comments on this PR #132486


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

2 Files Affected:

  • (modified) flang/lib/Semantics/pointer-assignment.cpp (+6-1)
  • (modified) flang/test/Semantics/assign02.f90 (+10)
diff --git a/flang/lib/Semantics/pointer-assignment.cpp b/flang/lib/Semantics/pointer-assignment.cpp
index 36c9c5b845706..c17eb0aa941ec 100644
--- a/flang/lib/Semantics/pointer-assignment.cpp
+++ b/flang/lib/Semantics/pointer-assignment.cpp
@@ -329,7 +329,6 @@ bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
             " shape"_err_en_US;
     } else if (rhsType->corank() > 0 &&
         (isVolatile_ != last->attrs().test(Attr::VOLATILE))) { // C1020
-      // TODO: what if A is VOLATILE in A%B%C?  need a better test here
       if (isVolatile_) {
         msg = "Pointer may not be VOLATILE when target is a"
               " non-VOLATILE coarray"_err_en_US;
@@ -569,6 +568,12 @@ bool CheckPointerAssignment(SemanticsContext &context, const SomeExpr &lhs,
     return false; // error was reported
   }
   PointerAssignmentChecker checker{context, scope, *pointer};
+  const Symbol *base{GetFirstSymbol(lhs)};
+  if (base) {
+    // 8.5.20(4) If an object has the VOLATILE attribute, then all of its
+    // subobjects also have the VOLATILE attribute.
+    checker.set_isVolatile(base->attrs().test(Attr::VOLATILE));
+  }
   checker.set_isBoundsRemapping(isBoundsRemapping);
   checker.set_isAssumedRank(isAssumedRank);
   bool lhsOk{checker.CheckLeftHandSide(lhs)};
diff --git a/flang/test/Semantics/assign02.f90 b/flang/test/Semantics/assign02.f90
index d83d126e2734c..9fa672025bfe7 100644
--- a/flang/test/Semantics/assign02.f90
+++ b/flang/test/Semantics/assign02.f90
@@ -8,9 +8,11 @@ module m1
   type t2
     sequence
     real :: t2Field
+    real, pointer :: t2FieldPtr
   end type
   type t3
     type(t2) :: t3Field
+    type(t2), pointer :: t3FieldPtr
   end type
 contains
 
@@ -198,6 +200,14 @@ subroutine s13
     q2 => y%t3Field
     !OK:
     q3 => y
+    !ERROR: VOLATILE target associated with non-VOLATILE pointer
+    p3%t3FieldPtr => y%t3Field
+    !ERROR: VOLATILE target associated with non-VOLATILE pointer
+    p3%t3FieldPtr%t2FieldPtr => y%t3Field%t2Field
+    !OK
+    q3%t3FieldPtr => y%t3Field
+    !OK
+    q3%t3FieldPtr%t2FieldPtr => y%t3Field%t2Field
   end
 end
 

Copy link
Contributor

@DanielCChen DanielCChen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch fixed the incorrect warning. Thanks!

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.

6 participants