[flang-commits] [flang] [flang][OpenMP] Skip implicit typing for DeclareSimdConstruct (PR #142415)

Kajetan Puchalski via flang-commits flang-commits at lists.llvm.org
Tue Jun 3 07:08:32 PDT 2025


https://github.com/mrkajetanp updated https://github.com/llvm/llvm-project/pull/142415

>From 0cf05e5d85f316718864f5c957e41f7a321cced8 Mon Sep 17 00:00:00 2001
From: Kajetan Puchalski <kajetan.puchalski at arm.com>
Date: Mon, 2 Jun 2025 15:28:51 +0000
Subject: [PATCH 1/3] [flang][OpenMP] Skip implicit typing for
 DeclareSimdConstruct

DeclareSimdConstruct currently can implicitly declare variables
regardless of whether the source code contains "implicit none" or not.
This causes semantic analysis issues if the implicit type does not match
the declared type. To solve it, skip implicit typing for declare simd.
Fixes issue #140754.

Signed-off-by: Kajetan Puchalski <kajetan.puchalski at arm.com>
---
 flang/lib/Semantics/resolve-names.cpp          |  1 +
 .../Semantics/OpenMP/declare-simd-linear.f90   | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+)
 create mode 100644 flang/test/Semantics/OpenMP/declare-simd-linear.f90

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 7bea6fdb00e55..93096442329e9 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1513,6 +1513,7 @@ class OmpVisitor : public virtual DeclarationVisitor {
 
   bool Pre(const parser::OpenMPDeclareSimdConstruct &x) {
     AddOmpSourceRange(x.source);
+    SkipImplicitTyping(true);
     return true;
   }
 
diff --git a/flang/test/Semantics/OpenMP/declare-simd-linear.f90 b/flang/test/Semantics/OpenMP/declare-simd-linear.f90
new file mode 100644
index 0000000000000..681cac7f02e0f
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/declare-simd-linear.f90
@@ -0,0 +1,18 @@
+! RUN: %python %S/../test_errors.py %s %flang -fopenmp
+! Test declare simd with linear clause
+
+module mod
+contains
+subroutine sub(m,i)
+!$omp declare simd linear(i:1)
+  implicit none
+  integer*8 i,n
+  value i
+  parameter(n=10000)
+  real*4 a,b,m
+  common/com1/a(n)
+  common/com2/b(n)
+  a(i) = b(i) + m
+  i=i+2
+end subroutine
+end module

>From a50ca71ed15fde11b411a08d861a7d53611e2723 Mon Sep 17 00:00:00 2001
From: Kajetan Puchalski <kajetan.puchalski at arm.com>
Date: Mon, 2 Jun 2025 16:30:42 +0000
Subject: [PATCH 2/3] Fix tests, re-enable implicit typing in Post, handle
 DeclarativeConstructs

---
 flang/lib/Semantics/resolve-names.cpp               |  5 +++++
 flang/test/Semantics/OpenMP/declare-simd-linear.f90 | 10 ++--------
 flang/test/Semantics/OpenMP/linear-clause01.f90     |  2 --
 3 files changed, 7 insertions(+), 10 deletions(-)

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 93096442329e9..82b389f4f6271 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1516,6 +1516,9 @@ class OmpVisitor : public virtual DeclarationVisitor {
     SkipImplicitTyping(true);
     return true;
   }
+  void Post(const parser::OpenMPDeclareSimdConstruct &x) {
+    SkipImplicitTyping(false);
+  }
 
   bool Pre(const parser::OmpInitializerProc &x) {
     auto &procDes = std::get<parser::ProcedureDesignator>(x.t);
@@ -1662,9 +1665,11 @@ class OmpVisitor : public virtual DeclarationVisitor {
   }
   bool Pre(const parser::OpenMPDeclarativeConstruct &x) {
     AddOmpSourceRange(x.source);
+    SkipImplicitTyping(true);
     return true;
   }
   void Post(const parser::OpenMPDeclarativeConstruct &) {
+    SkipImplicitTyping(false);
     messageHandler().set_currStmtSource(std::nullopt);
   }
   bool Pre(const parser::OpenMPDepobjConstruct &x) {
diff --git a/flang/test/Semantics/OpenMP/declare-simd-linear.f90 b/flang/test/Semantics/OpenMP/declare-simd-linear.f90
index 681cac7f02e0f..c534f23fcb3da 100644
--- a/flang/test/Semantics/OpenMP/declare-simd-linear.f90
+++ b/flang/test/Semantics/OpenMP/declare-simd-linear.f90
@@ -3,16 +3,10 @@
 
 module mod
 contains
-subroutine sub(m,i)
+subroutine test(i)
 !$omp declare simd linear(i:1)
   implicit none
-  integer*8 i,n
-  value i
-  parameter(n=10000)
-  real*4 a,b,m
-  common/com1/a(n)
-  common/com2/b(n)
-  a(i) = b(i) + m
+  integer*8 i
   i=i+2
 end subroutine
 end module
diff --git a/flang/test/Semantics/OpenMP/linear-clause01.f90 b/flang/test/Semantics/OpenMP/linear-clause01.f90
index 286def2dba119..2f499ac892a48 100644
--- a/flang/test/Semantics/OpenMP/linear-clause01.f90
+++ b/flang/test/Semantics/OpenMP/linear-clause01.f90
@@ -24,12 +24,10 @@ subroutine linear_clause_02(arg_01, arg_02)
     !$omp declare simd linear(uval(arg_02))
     integer, value, intent(in) :: arg_02
 
-    !ERROR: The list item 'var' specified without the REF 'linear-modifier' must be of INTEGER type
     !ERROR: If the `linear-modifier` is REF or UVAL, the list item 'var' must be a dummy argument without the VALUE attribute
     !ERROR: The list item `var` must be a dummy argument
     !ERROR: The list item `var` in a LINEAR clause must not be Cray Pointer or a variable with POINTER attribute
     !$omp declare simd linear(uval(var))
-    !ERROR: The type of 'var' has already been implicitly declared
     integer, pointer :: var
 end subroutine linear_clause_02
 

>From 8a2666c1f7b92cd5c550dd6a52a5058a5b56b310 Mon Sep 17 00:00:00 2001
From: Kajetan Puchalski <kajetan.puchalski at arm.com>
Date: Tue, 3 Jun 2025 14:07:53 +0000
Subject: [PATCH 3/3] Drop unnecessary handling for DeclareSimd

---
 flang/lib/Semantics/resolve-names.cpp | 4 ----
 1 file changed, 4 deletions(-)

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 82b389f4f6271..cc1f2131d00d7 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -1513,12 +1513,8 @@ class OmpVisitor : public virtual DeclarationVisitor {
 
   bool Pre(const parser::OpenMPDeclareSimdConstruct &x) {
     AddOmpSourceRange(x.source);
-    SkipImplicitTyping(true);
     return true;
   }
-  void Post(const parser::OpenMPDeclareSimdConstruct &x) {
-    SkipImplicitTyping(false);
-  }
 
   bool Pre(const parser::OmpInitializerProc &x) {
     auto &procDes = std::get<parser::ProcedureDesignator>(x.t);



More information about the flang-commits mailing list