[flang-commits] [flang] [flang][OpenMP] Fix crash when arrays used in LINEAR clause (PR #175383)
Krish Gupta via flang-commits
flang-commits at lists.llvm.org
Sat Jan 10 12:43:41 PST 2026
https://github.com/KrxGu updated https://github.com/llvm/llvm-project/pull/175383
>From b987d708bb6c6b9ffd5c463b387baa3c0f5d9987 Mon Sep 17 00:00:00 2001
From: KrxGu <krishom70 at gmail.com>
Date: Sun, 11 Jan 2026 00:56:43 +0530
Subject: [PATCH 1/4] [flang][OpenMP] Fix crash when arrays used in LINEAR
clause
Closes #171007
Previously the compiler would crash with "unknown LLVM dialect type"
when you put an array in a LINEAR clause on SIMD directives. This
happened because arrays aren't allowed there - LINEAR only works with
scalar variables (rank 0).
Now we properly check for this during semantic analysis and show a
clear error message instead of crashing later in MLIR translation.
The fix allows arrays only for declare simd with the REF modifier,
which is the one case where the OpenMP spec permits it.
---
flang/lib/Semantics/check-omp-loop.cpp | 10 +++
.../test/Semantics/OpenMP/linear-clause01.f90 | 2 +
.../Semantics/OpenMP/simd-linear-array.f90 | 71 +++++++++++++++++++
3 files changed, 83 insertions(+)
create mode 100644 flang/test/Semantics/OpenMP/simd-linear-array.f90
diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index bff00a222ba92..24cf674f8c86c 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -808,6 +808,16 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
// OpenMP 5.2: Linear clause Restrictions
for (auto &[symbol, source] : symbols) {
+ // Check that the list item is a scalar variable (rank 0)
+ // For declare simd with REF modifier, arrays are allowed
+ bool isArrayAllowed = dir == llvm::omp::Directive::OMPD_declare_simd &&
+ linearMod &&
+ linearMod->v == parser::OmpLinearModifier::Value::Ref;
+ if (symbol->Rank() != 0 && !isArrayAllowed) {
+ context_.Say(source,
+ "List item '%s' in LINEAR clause must be a scalar variable"_err_en_US,
+ symbol->name());
+ }
if (!linearMod) {
// Already checked this with the modifier present.
CheckIntegerNoRef(symbol, source);
diff --git a/flang/test/Semantics/OpenMP/linear-clause01.f90 b/flang/test/Semantics/OpenMP/linear-clause01.f90
index 2f499ac892a48..63b09c07875e5 100644
--- a/flang/test/Semantics/OpenMP/linear-clause01.f90
+++ b/flang/test/Semantics/OpenMP/linear-clause01.f90
@@ -8,6 +8,7 @@
subroutine linear_clause_01(arg)
integer, intent(in) :: arg(:)
!ERROR: A modifier may not be specified in a LINEAR clause on the DO directive
+ !ERROR: List item 'arg' in LINEAR clause must be a scalar variable
!$omp do linear(uval(arg))
do i = 1, 5
print *, arg(i)
@@ -17,6 +18,7 @@ end subroutine linear_clause_01
! Case 2
subroutine linear_clause_02(arg_01, arg_02)
!ERROR: The list item 'arg_01' specified without the REF 'linear-modifier' must be of INTEGER type
+ !ERROR: List item 'arg_01' in LINEAR clause must be a scalar variable
!$omp declare simd linear(val(arg_01))
real, intent(in) :: arg_01(:)
diff --git a/flang/test/Semantics/OpenMP/simd-linear-array.f90 b/flang/test/Semantics/OpenMP/simd-linear-array.f90
new file mode 100644
index 0000000000000..c9a43223307c9
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/simd-linear-array.f90
@@ -0,0 +1,71 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 %openmp_flags
+! OpenMP Version 5.2
+! Test that arrays in LINEAR clause are rejected on SIMD directive
+! This test addresses issue #171007 - crash with array in LINEAR clause
+
+subroutine test_1d_array_in_linear()
+ implicit none
+ integer :: j, arr(2)
+
+ !ERROR: List item 'arr' in LINEAR clause must be a scalar variable
+ !$omp simd linear(arr)
+ do j=1,10
+ end do
+end subroutine
+
+subroutine test_multidim_array()
+ implicit none
+ integer :: j, matrix(3,3)
+
+ !ERROR: List item 'matrix' in LINEAR clause must be a scalar variable
+ !$omp simd linear(matrix)
+ do j=1,10
+ end do
+end subroutine
+
+subroutine test_assumed_shape_array(arr)
+ implicit none
+ integer :: j
+ integer, intent(in) :: arr(:)
+
+ !ERROR: List item 'arr' in LINEAR clause must be a scalar variable
+ !$omp simd linear(arr)
+ do j=1,10
+ end do
+end subroutine
+
+subroutine test_multiple_vars_with_array()
+ implicit none
+ integer :: j, scalar1, arr(5), scalar2
+
+ !ERROR: List item 'arr' in LINEAR clause must be a scalar variable
+ !$omp simd linear(scalar1, arr, scalar2)
+ do j=1,10
+ scalar1 = j
+ scalar2 = j
+ end do
+end subroutine
+
+! Valid case - scalar should work fine
+subroutine test_scalar_valid()
+ implicit none
+ integer :: j, scalar
+
+ !$omp simd linear(scalar)
+ do j=1,10
+ scalar = j
+ end do
+end subroutine
+
+! Valid case - multiple scalars should work
+subroutine test_multiple_scalars_valid()
+ implicit none
+ integer :: j, scalar1, scalar2, scalar3
+
+ !$omp simd linear(scalar1, scalar2, scalar3)
+ do j=1,10
+ scalar1 = j
+ scalar2 = j
+ scalar3 = j
+ end do
+end subroutine
>From 2308c24b1ff16142c6ad6bc037da8f777628ce57 Mon Sep 17 00:00:00 2001
From: KrxGu <krishom70 at gmail.com>
Date: Sun, 11 Jan 2026 01:29:26 +0530
Subject: [PATCH 2/4] Fix formatting and update tests for LINEAR clause check
- Applied clang-format to check-omp-loop.cpp
- Added test for valid case: declare simd with REF modifier allows arrays
- Updated linear-iter.f90 to expect new scalar check error for arrays
- Updated cray-pointer-usage.f90 to expect scalar check error
The new semantic check catches arrays earlier, so tests that have
arrays in LINEAR clauses now show the scalar error message along
with their other expected errors.
---
flang/lib/Semantics/check-omp-loop.cpp | 3 +--
flang/test/Semantics/OpenMP/cray-pointer-usage.f90 | 1 +
flang/test/Semantics/OpenMP/linear-iter.f90 | 2 ++
flang/test/Semantics/OpenMP/simd-linear-array.f90 | 9 +++++++++
4 files changed, 13 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index 24cf674f8c86c..a59e4f66625ff 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -811,8 +811,7 @@ void OmpStructureChecker::Enter(const parser::OmpClause::Linear &x) {
// Check that the list item is a scalar variable (rank 0)
// For declare simd with REF modifier, arrays are allowed
bool isArrayAllowed = dir == llvm::omp::Directive::OMPD_declare_simd &&
- linearMod &&
- linearMod->v == parser::OmpLinearModifier::Value::Ref;
+ linearMod && linearMod->v == parser::OmpLinearModifier::Value::Ref;
if (symbol->Rank() != 0 && !isArrayAllowed) {
context_.Say(source,
"List item '%s' in LINEAR clause must be a scalar variable"_err_en_US,
diff --git a/flang/test/Semantics/OpenMP/cray-pointer-usage.f90 b/flang/test/Semantics/OpenMP/cray-pointer-usage.f90
index 6c74462dd2789..3f13338a19f7f 100644
--- a/flang/test/Semantics/OpenMP/cray-pointer-usage.f90
+++ b/flang/test/Semantics/OpenMP/cray-pointer-usage.f90
@@ -5,6 +5,7 @@ subroutine test_cray_pointer_usage
real(8) :: var(*), pointee(2)
pointer(ivar, var)
! ERROR: Cray Pointee 'var' may not appear in LINEAR clause
+ ! ERROR: List item 'var' in LINEAR clause must be a scalar variable
! ERROR: The list item 'var' specified without the REF 'linear-modifier' must be of INTEGER type
! ERROR: The list item `var` must be a dummy argument
!$omp declare simd linear(var)
diff --git a/flang/test/Semantics/OpenMP/linear-iter.f90 b/flang/test/Semantics/OpenMP/linear-iter.f90
index 9f6d37422fc0c..2a5af3fea3207 100644
--- a/flang/test/Semantics/OpenMP/linear-iter.f90
+++ b/flang/test/Semantics/OpenMP/linear-iter.f90
@@ -32,6 +32,7 @@ SUBROUTINE LINEAR_BAD(N)
!$omp target
!$omp teams
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
+ !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!ERROR: Variable 'b' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
!$omp distribute parallel do simd linear(j) linear(b)
do i = 1, N
@@ -44,6 +45,7 @@ SUBROUTINE LINEAR_BAD(N)
!$omp target
!$omp teams
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
+ !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!ERROR: Variable 'b' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
!$omp distribute parallel do simd linear(j, b)
do i = 1, N
diff --git a/flang/test/Semantics/OpenMP/simd-linear-array.f90 b/flang/test/Semantics/OpenMP/simd-linear-array.f90
index c9a43223307c9..e904f348ae47a 100644
--- a/flang/test/Semantics/OpenMP/simd-linear-array.f90
+++ b/flang/test/Semantics/OpenMP/simd-linear-array.f90
@@ -69,3 +69,12 @@ subroutine test_multiple_scalars_valid()
scalar3 = j
end do
end subroutine
+
+! Valid case - declare simd with REF modifier allows arrays
+subroutine test_declare_simd_ref_array_valid(arr)
+ implicit none
+ integer, intent(in) :: arr(:)
+
+ !$omp declare simd linear(ref(arr))
+ ! No error expected - REF modifier allows assumed-shape arrays
+end subroutine
>From 529e7eb6a7c89886695f37d54c75d1c6c77f5fc0 Mon Sep 17 00:00:00 2001
From: KrxGu <krishom70 at gmail.com>
Date: Sun, 11 Jan 2026 01:49:31 +0530
Subject: [PATCH 3/4] Fix error message order in linear-iter.f90 test
When LINEAR clause has comma-separated items like linear(j, b), the
errors are reported in a specific order: first the iterator check for
each variable, then the scalar check. Updated test expectations to
match the actual error output order.
---
flang/test/Semantics/OpenMP/linear-iter.f90 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/test/Semantics/OpenMP/linear-iter.f90 b/flang/test/Semantics/OpenMP/linear-iter.f90
index 2a5af3fea3207..fe9c1ec952bb3 100644
--- a/flang/test/Semantics/OpenMP/linear-iter.f90
+++ b/flang/test/Semantics/OpenMP/linear-iter.f90
@@ -45,8 +45,8 @@ SUBROUTINE LINEAR_BAD(N)
!$omp target
!$omp teams
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
- !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!ERROR: Variable 'b' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
+ !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!$omp distribute parallel do simd linear(j, b)
do i = 1, N
a = 3.14
>From 7564c865fa2d42d80eda292ee220db9941db4afe Mon Sep 17 00:00:00 2001
From: KrxGu <krishom70 at gmail.com>
Date: Sun, 11 Jan 2026 02:13:30 +0530
Subject: [PATCH 4/4] Fix error order at line 37 in linear-iter.f90
Both line 37 and line 50 had the same issue - when array 'b' appears
in LINEAR clause, the compiler reports errors in this order:
1. Variable not allowed (iterator check)
2. List item must be scalar
Updated line 37 to match this order, just like we did for line 50.
---
flang/test/Semantics/OpenMP/linear-iter.f90 | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/flang/test/Semantics/OpenMP/linear-iter.f90 b/flang/test/Semantics/OpenMP/linear-iter.f90
index fe9c1ec952bb3..a46a3411fab34 100644
--- a/flang/test/Semantics/OpenMP/linear-iter.f90
+++ b/flang/test/Semantics/OpenMP/linear-iter.f90
@@ -32,8 +32,8 @@ SUBROUTINE LINEAR_BAD(N)
!$omp target
!$omp teams
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
- !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!ERROR: Variable 'b' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
+ !ERROR: List item 'b' in LINEAR clause must be a scalar variable
!$omp distribute parallel do simd linear(j) linear(b)
do i = 1, N
a = 3.14
More information about the flang-commits
mailing list