[flang-commits] [flang] [flang][semantics][OpenMP] no privatisation of stmt functions (PR #106550)

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Wed Oct 2 03:02:25 PDT 2024


https://github.com/tblah updated https://github.com/llvm/llvm-project/pull/106550

>From 2c6b70ff1523501c39a3ec56a7a0ac9dacc32112 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Thu, 29 Aug 2024 10:19:56 +0000
Subject: [PATCH 1/3] [flang][semantics][OpenMP] no privatisation of stmt
 functions

OpenMP prohibits privatisation of variables that appear in expressions
for statement functions.

This is a re-working of an old patch https://reviews.llvm.org/D93213 by
@praveen-g-ctt.

The old patch couldn't be landed because of ordering concerns. Statement
functions are rewritten during parse tree rewriting, but this was done
after resolve-directives and so some array expressions were incorrectly
identified as statement functions. For this reason **I have opted to
re-order the semantics driver so that resolve-directives is run after
parse tree rewriting**.

Closes #54677

Co-authored-by: Praveen <praveen at compilertree.com>
---
 flang/lib/Semantics/resolve-directives.cpp |  8 +++++
 flang/lib/Semantics/semantics.cpp          |  2 +-
 flang/test/Semantics/OpenMP/private03.f90  | 39 ++++++++++++++++++++++
 3 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Semantics/OpenMP/private03.f90

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 23a1ecbd2842d5..98cf54a3b71297 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2750,6 +2750,14 @@ void OmpAttributeVisitor::CheckObjectInNamelistOrAssociate(
         "Variable '%s' in ASSOCIATE cannot be in a %s clause"_err_en_US,
         name.ToString(), clauseName.str());
   }
+
+  if (stmtFunctionExprSymbols_.find(ultimateSymbol) !=
+      stmtFunctionExprSymbols_.end()) {
+    context_.Say(name.source,
+        "Variable '%s' in STATEMENT FUNCTION expression cannot be in a "
+        "%s clause"_err_en_US,
+        name.ToString(), clauseName.str());
+  }
 }
 
 void OmpAttributeVisitor::CheckSourceLabel(const parser::Label &label) {
diff --git a/flang/lib/Semantics/semantics.cpp b/flang/lib/Semantics/semantics.cpp
index 1f2980b07b3e0e..dcce1fd3b1c53a 100644
--- a/flang/lib/Semantics/semantics.cpp
+++ b/flang/lib/Semantics/semantics.cpp
@@ -642,8 +642,8 @@ bool Semantics::Perform() {
       CanonicalizeAcc(context_.messages(), program_) &&
       CanonicalizeOmp(context_.messages(), program_) &&
       CanonicalizeCUDA(program_) &&
-      CanonicalizeDirectives(context_.messages(), program_) &&
       PerformStatementSemantics(context_, program_) &&
+      CanonicalizeDirectives(context_.messages(), program_) &&
       ModFileWriter{context_}
           .set_hermeticModuleFileOutput(hermeticModuleFileOutput_)
           .WriteAll();
diff --git a/flang/test/Semantics/OpenMP/private03.f90 b/flang/test/Semantics/OpenMP/private03.f90
new file mode 100644
index 00000000000000..3ce9cf81bb73a5
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/private03.f90
@@ -0,0 +1,39 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp
+! OpenMP Version 4.5
+! Variables that appear in expressions for statement function definitions
+! may not appear in private, firstprivate or lastprivate clauses.
+
+subroutine stmt_function(temp)
+
+  integer :: i, p, q, r
+  real :: c, f, s, v, t(10)
+  real, intent(in) :: temp
+
+  c(temp) = p * (temp - q) / r
+  f(temp) = q + (temp * r/p)
+  v(temp) = c(temp) + f(temp)/2 - s
+
+  p = 5
+  q = 32
+  r = 9
+
+  !ERROR: Variable 'p' in STATEMENT FUNCTION expression cannot be in a PRIVATE clause
+  !$omp parallel private(p)
+  s = c(temp)
+  !$omp end parallel
+
+  !ERROR: Variable 's' in STATEMENT FUNCTION expression cannot be in a FIRSTPRIVATE clause
+  !$omp parallel firstprivate(s)
+  s = s + f(temp)
+  !$omp end parallel
+
+  !ERROR: Variable 's' in STATEMENT FUNCTION expression cannot be in a LASTPRIVATE clause
+  !$omp parallel do lastprivate(s, t)
+  do i = 1, 10
+  t(i) = v(temp) + i - s
+  end do
+  !$omp end parallel do
+
+  print *, t
+
+end subroutine stmt_function

>From 9a43a1dd239fdc88822b10a7ad4871894d4412d3 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Thu, 29 Aug 2024 14:00:24 +0000
Subject: [PATCH 2/3] Don't capitalise 'statement function'

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

diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index 98cf54a3b71297..3ab5fde12558be 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -2754,7 +2754,7 @@ void OmpAttributeVisitor::CheckObjectInNamelistOrAssociate(
   if (stmtFunctionExprSymbols_.find(ultimateSymbol) !=
       stmtFunctionExprSymbols_.end()) {
     context_.Say(name.source,
-        "Variable '%s' in STATEMENT FUNCTION expression cannot be in a "
+        "Variable '%s' in statement function expression cannot be in a "
         "%s clause"_err_en_US,
         name.ToString(), clauseName.str());
   }

>From d3aa6da834477f16e8d2edf959520bb4c24b78b4 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 2 Sep 2024 10:19:35 +0000
Subject: [PATCH 3/3] Fix test

---
 flang/test/Semantics/OpenMP/private03.f90 | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang/test/Semantics/OpenMP/private03.f90 b/flang/test/Semantics/OpenMP/private03.f90
index 3ce9cf81bb73a5..61f790fbb38b28 100644
--- a/flang/test/Semantics/OpenMP/private03.f90
+++ b/flang/test/Semantics/OpenMP/private03.f90
@@ -17,17 +17,17 @@ subroutine stmt_function(temp)
   q = 32
   r = 9
 
-  !ERROR: Variable 'p' in STATEMENT FUNCTION expression cannot be in a PRIVATE clause
+  !ERROR: Variable 'p' in statement function expression cannot be in a PRIVATE clause
   !$omp parallel private(p)
   s = c(temp)
   !$omp end parallel
 
-  !ERROR: Variable 's' in STATEMENT FUNCTION expression cannot be in a FIRSTPRIVATE clause
+  !ERROR: Variable 's' in statement function expression cannot be in a FIRSTPRIVATE clause
   !$omp parallel firstprivate(s)
   s = s + f(temp)
   !$omp end parallel
 
-  !ERROR: Variable 's' in STATEMENT FUNCTION expression cannot be in a LASTPRIVATE clause
+  !ERROR: Variable 's' in statement function expression cannot be in a LASTPRIVATE clause
   !$omp parallel do lastprivate(s, t)
   do i = 1, 10
   t(i) = v(temp) + i - s



More information about the flang-commits mailing list