Skip to content

[Flang][OpenMP]add semantic check for linear clause with statement function variables#199743

Open
blazie2004 wants to merge 1 commit into
llvm:mainfrom
blazie2004:linear-stmt-func-check-clean
Open

[Flang][OpenMP]add semantic check for linear clause with statement function variables#199743
blazie2004 wants to merge 1 commit into
llvm:mainfrom
blazie2004:linear-stmt-func-check-clean

Conversation

@blazie2004
Copy link
Copy Markdown
Contributor

Description

  1. This patch adds a missing semantic check for the LINEAR clause.
  2. OpenMP treats LINEAR variables similarly to PRIVATE variables. Variables used inside statement function expressions are not allowed to be privatized, but Flang was not checking this for LINEAR.
  3. The existing privatization check already handled PRIVATE, FIRSTPRIVATE, and LASTPRIVATE. This patch extends the same check to LINEAR.

Fixes : 199660

Reproducer

subroutine test()
  integer :: pi, r, f, x
  f(r) = pi * r + x
  !$omp parallel do linear(x)
  do r = 1, 10
    pi = f(r)
  end do
  !$omp end parallel do
end subroutine

when compiled with : -fopenmp -fopenmp-version=45 -c

Observed Behavior

No diagnostic emitted

Expected Behavior

error: Variable 'x' in statement function expression cannot be in a LINEAR clause
    !$omp parallel do linear(x)

@llvmorg-github-actions llvmorg-github-actions Bot added flang Flang issues not falling into any other category flang:openmp flang:semantics labels May 26, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-flang-openmp

Author: jay0x (blazie2004)

Changes

Description

  1. This patch adds a missing semantic check for the LINEAR clause.
  2. OpenMP treats LINEAR variables similarly to PRIVATE variables. Variables used inside statement function expressions are not allowed to be privatized, but Flang was not checking this for LINEAR.
  3. The existing privatization check already handled PRIVATE, FIRSTPRIVATE, and LASTPRIVATE. This patch extends the same check to LINEAR.

Fixes : 199660

Reproducer

subroutine test()
  integer :: pi, r, f, x
  f(r) = pi * r + x
  !$omp parallel do linear(x)
  do r = 1, 10
    pi = f(r)
  end do
  !$omp end parallel do
end subroutine

when compiled with : -fopenmp -fopenmp-version=45 -c

Observed Behavior

No diagnostic emitted

Expected Behavior

error: Variable 'x' in statement function expression cannot be in a LINEAR clause
    !$omp parallel do linear(x)

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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+4-1)
  • (modified) flang/test/Semantics/OpenMP/private03.f90 (+9-1)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index b97f7ce58a1c0..fa84c35424108 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1064,7 +1064,8 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
       Symbol::Flag::OmpIsDevicePtr, Symbol::Flag::OmpHasDeviceAddr};
 
   Symbol::Flags privateDataSharingAttributeFlags{Symbol::Flag::OmpPrivate,
-      Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate};
+      Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
+      Symbol::Flag::OmpLinear};
 
   Symbol::Flags ompFlagsRequireNewSymbol{Symbol::Flag::OmpPrivate,
       Symbol::Flag::OmpLinear, Symbol::Flag::OmpFirstPrivate,
@@ -3263,6 +3264,8 @@ void OmpAttributeVisitor::CheckObjectIsPrivatizable(
     clauseName = "FIRSTPRIVATE";
   } else if (ompFlag == Symbol::Flag::OmpLastPrivate) {
     clauseName = "LASTPRIVATE";
+  } else if (ompFlag == Symbol::Flag::OmpLinear) {
+    clauseName = "LINEAR";
   }
 
   if (SymbolOrEquivalentIsInNamelist(symbol)) {
diff --git a/flang/test/Semantics/OpenMP/private03.f90 b/flang/test/Semantics/OpenMP/private03.f90
index 61f790fbb38b2..e921d8a58fdba 100644
--- a/flang/test/Semantics/OpenMP/private03.f90
+++ b/flang/test/Semantics/OpenMP/private03.f90
@@ -1,7 +1,7 @@
 ! 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.
+! may not appear in private, firstprivate, lastprivate or linear clauses.
 
 subroutine stmt_function(temp)
 
@@ -34,6 +34,14 @@ subroutine stmt_function(temp)
   end do
   !$omp end parallel do
 
+  !ERROR: Variable 'p' in statement function expression cannot be in a LINEAR clause
+  !$omp parallel do simd linear(p)
+  do i = 1, 10
+  t(i) = v(temp) + i
+  p = p + 1
+  end do
+  !$omp end parallel do simd
+
   print *, t
 
 end subroutine stmt_function

@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-flang-semantics

Author: jay0x (blazie2004)

Changes

Description

  1. This patch adds a missing semantic check for the LINEAR clause.
  2. OpenMP treats LINEAR variables similarly to PRIVATE variables. Variables used inside statement function expressions are not allowed to be privatized, but Flang was not checking this for LINEAR.
  3. The existing privatization check already handled PRIVATE, FIRSTPRIVATE, and LASTPRIVATE. This patch extends the same check to LINEAR.

Fixes : 199660

Reproducer

subroutine test()
  integer :: pi, r, f, x
  f(r) = pi * r + x
  !$omp parallel do linear(x)
  do r = 1, 10
    pi = f(r)
  end do
  !$omp end parallel do
end subroutine

when compiled with : -fopenmp -fopenmp-version=45 -c

Observed Behavior

No diagnostic emitted

Expected Behavior

error: Variable 'x' in statement function expression cannot be in a LINEAR clause
    !$omp parallel do linear(x)

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

2 Files Affected:

  • (modified) flang/lib/Semantics/resolve-directives.cpp (+4-1)
  • (modified) flang/test/Semantics/OpenMP/private03.f90 (+9-1)
diff --git a/flang/lib/Semantics/resolve-directives.cpp b/flang/lib/Semantics/resolve-directives.cpp
index b97f7ce58a1c0..fa84c35424108 100644
--- a/flang/lib/Semantics/resolve-directives.cpp
+++ b/flang/lib/Semantics/resolve-directives.cpp
@@ -1064,7 +1064,8 @@ class OmpAttributeVisitor : DirectiveAttributeVisitor<llvm::omp::Directive> {
       Symbol::Flag::OmpIsDevicePtr, Symbol::Flag::OmpHasDeviceAddr};
 
   Symbol::Flags privateDataSharingAttributeFlags{Symbol::Flag::OmpPrivate,
-      Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate};
+      Symbol::Flag::OmpFirstPrivate, Symbol::Flag::OmpLastPrivate,
+      Symbol::Flag::OmpLinear};
 
   Symbol::Flags ompFlagsRequireNewSymbol{Symbol::Flag::OmpPrivate,
       Symbol::Flag::OmpLinear, Symbol::Flag::OmpFirstPrivate,
@@ -3263,6 +3264,8 @@ void OmpAttributeVisitor::CheckObjectIsPrivatizable(
     clauseName = "FIRSTPRIVATE";
   } else if (ompFlag == Symbol::Flag::OmpLastPrivate) {
     clauseName = "LASTPRIVATE";
+  } else if (ompFlag == Symbol::Flag::OmpLinear) {
+    clauseName = "LINEAR";
   }
 
   if (SymbolOrEquivalentIsInNamelist(symbol)) {
diff --git a/flang/test/Semantics/OpenMP/private03.f90 b/flang/test/Semantics/OpenMP/private03.f90
index 61f790fbb38b2..e921d8a58fdba 100644
--- a/flang/test/Semantics/OpenMP/private03.f90
+++ b/flang/test/Semantics/OpenMP/private03.f90
@@ -1,7 +1,7 @@
 ! 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.
+! may not appear in private, firstprivate, lastprivate or linear clauses.
 
 subroutine stmt_function(temp)
 
@@ -34,6 +34,14 @@ subroutine stmt_function(temp)
   end do
   !$omp end parallel do
 
+  !ERROR: Variable 'p' in statement function expression cannot be in a LINEAR clause
+  !$omp parallel do simd linear(p)
+  do i = 1, 10
+  t(i) = v(temp) + i
+  p = p + 1
+  end do
+  !$omp end parallel do simd
+
   print *, t
 
 end subroutine stmt_function

@eugeneepshteyn eugeneepshteyn requested review from kparzysz and luporl May 26, 2026 21:24
Copy link
Copy Markdown
Contributor

@luporl luporl left a comment

Choose a reason for hiding this comment

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

LGTM

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Flang][OpenMP] Missing semantic check: LINEAR clause variable in statement function expression

2 participants