Skip to content

[flang][openacc] Relax constraint on OpenACC declare statement #135238

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 2 commits into from
Apr 14, 2025

Conversation

akuhlens
Copy link
Contributor

@akuhlens akuhlens commented Apr 10, 2025

OpenACC declare statements are restricted from having having clauses that reference assumed size arrays. It should be the case that we can implement deviceptr and present clauses for assumed-size arrays. This is a first step towards relaxing this restriction.

Note running flang on the following example results in an error in lowering.

$ cat t.f90
subroutine vadd (a, b, c, n)
   real(8) :: a(*), b(*), c(*)
!$acc declare deviceptr(a, b, c)
!$acc parallel loop
   do i = 1,n
      c(i) = a(i) + b(i)
   enddo
end subroutine

$ flang -fopenacc -c t.f90
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":3:7): expect declare attribute on variable in declare operation
error: Lowering to LLVM IR failed
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): unsupported OpenACC operation: acc.private.recipe
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): LLVM Translation failed for operation: acc.private.recipe
error: failed to create the LLVM module

I would like to to share this code, because others are currently working on the implementation of deviceptr, but it is obviously not running end-to-end. I think the cleanest approach to this would be to put this exception to the rule behind some feature flag, but I am not certain what the precedence for that is.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:openmp openacc flang:semantics labels Apr 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-flang-semantics
@llvm/pr-subscribers-openacc

@llvm/pr-subscribers-flang-openmp

Author: Andre Kuhlenschmidt (akuhlens)

Changes

OpenACC declare statements are restricted from having having clauses that reference assumed size arrays. It should be the case that we can implement deviceptr and present clauses for assumed-size arrays. This is a first step towards relaxing this restriction.

Note running flang on the following example results in an error in lowering.

$ cat t.f90
subroutine vadd (a, b, c, n)
   real(8) :: a(*), b(*), c(*)
!$acc declare deviceptr(a, b, c)
!$acc parallel loop
   do i = 1,n
      c(i) = a(i) + b(i)
   enddo
end subroutine

$ flang -fopenacc -c t.f90
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":3:7): expect declare attribute on variable in declare operation
error: Lowering to LLVM IR failed
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): unsupported OpenACC operation: acc.private.recipe
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): LLVM Translation failed for operation: acc.private.recipe
error: failed to create the LLVM module

I would like to to share this code, because others are currently working on the implementation of deviceptr, but it is obviously not running end-to-end. I think the cleanest approach to this would be to put this exception to the rule behind some feature flag, but I am not certain what the precedence for that is.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+8-1)
  • (modified) flang/test/Semantics/OpenACC/acc-declare-validity.f90 (+13-1)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index a5b3391859500..4a3994dcffa1b 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -953,7 +953,14 @@ void AccAttributeVisitor::Post(
   const auto &clauseList = std::get<parser::AccClauseList>(x.t);
   for (const auto &clause : clauseList.v) {
     // Restriction - line 2414
-    DoNotAllowAssumedSizedArray(GetAccObjectList(clause));
+    // We assume the restriction is present because clauses that require
+    // moving data would require the size of the data to be present, but
+    // the deviceptr and present clauses do not require moving data and
+    // thus we permit them.
+    if (!std::holds_alternative<parser::AccClause::Deviceptr>(clause.u) &&
+        !std::holds_alternative<parser::AccClause::Present>(clause.u)) {
+      DoNotAllowAssumedSizedArray(GetAccObjectList(clause));
+    }
   }
 }
 
diff --git a/flang/test/Semantics/OpenACC/acc-declare-validity.f90 b/flang/test/Semantics/OpenACC/acc-declare-validity.f90
index 2e7e651815d4a..7cdc69436704b 100644
--- a/flang/test/Semantics/OpenACC/acc-declare-validity.f90
+++ b/flang/test/Semantics/OpenACC/acc-declare-validity.f90
@@ -62,9 +62,21 @@ end function fct1
   subroutine sub2(cc)
     real(8), dimension(*) :: cc
     !ERROR: Assumed-size dummy arrays may not appear on the DECLARE directive
-    !$acc declare present(cc)
+    !$acc declare copyin(cc)
   end subroutine sub2
 
+  subroutine sub2e1(cc)
+    real(8), dimension(*) :: cc
+    !OK
+    !$acc declare present(cc)
+  end subroutine sub2e1
+
+  subroutine sub2e2(cc)
+    real(8), dimension(*) :: cc
+    !OK
+    !$acc declare deviceptr(cc)
+  end subroutine sub2e2
+
   subroutine sub3()
     real :: aa(100)
     !ERROR: The ZERO modifier is not allowed for the COPYOUT clause on the DECLARE directive

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

LGTM

@clementval
Copy link
Contributor

Can you add a line in flang/docs/OpenACC.md. There is a section where we list the intentional deviation from the standard.

@akuhlens akuhlens force-pushed the andre/openacc-relax-declare branch from 03e151f to 5130770 Compare April 10, 2025 21:32
Copy link
Contributor

@razvanlupusoru razvanlupusoru left a comment

Choose a reason for hiding this comment

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

Thank you! Great work!

@akuhlens akuhlens merged commit 2e353a6 into llvm:main Apr 14, 2025
12 checks passed
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
…135238)

OpenACC declare statements are restricted from having having clauses
that reference assumed size arrays. It should be the case that we can
implement `deviceptr` and `present` clauses for assumed-size arrays.
This is a first step towards relaxing this restriction.

Note running flang on the following example results in an error in
lowering.
```
$ cat t.f90
subroutine vadd (a, b, c, n)
   real(8) :: a(*), b(*), c(*)
!$acc declare deviceptr(a, b, c)
!$acc parallel loop
   do i = 1,n
      c(i) = a(i) + b(i)
   enddo
end subroutine

$ flang -fopenacc -c t.f90
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":3:7): expect declare attribute on variable in declare operation
error: Lowering to LLVM IR failed
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): unsupported OpenACC operation: acc.private.recipe
error: loc("/home/akuhlenschmi/work/p4/ta/tests/openacc/src/t.f90":4:7): LLVM Translation failed for operation: acc.private.recipe
error: failed to create the LLVM module
```

I would like to to share this code, because others are currently working
on the implementation of `deviceptr`, but it is obviously not running
end-to-end. I think the cleanest approach to this would be to put this
exception to the rule behind some feature flag, but I am not certain
what the precedence for that is.
@akuhlens akuhlens deleted the andre/openacc-relax-declare branch June 10, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:openmp flang:semantics flang Flang issues not falling into any other category openacc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants