[flang-commits] [flang] [flang][OpenMP] Fix crash when arrays used in LINEAR clause (PR #175383)
via flang-commits
flang-commits at lists.llvm.org
Sat Jan 10 11:39:15 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
@llvm/pr-subscribers-flang-openmp
Author: Krish Gupta (KrxGu)
<details>
<summary>Changes</summary>
Closes #<!-- -->171007
Found this crash while testing OpenMP code - turns out putting an array in a LINEAR clause on SIMD directives causes the compiler to blow up with "unknown LLVM dialect type" and hit an UNREACHABLE.
The issue is that LINEAR clauses only accept scalar variables (rank 0), but we weren't checking for that early enough. The invalid array type would slip through semantic analysis and then crash during MLIR to LLVM IR translation when it tried to convert the type.
**What changed:**
- Added a rank check in the LINEAR clause validation to catch arrays up front
- Made an exception for `declare simd` with the REF modifier since that's actually allowed per spec
- Added tests covering different array types (1D, multi-dim, assumed-shape)
- Updated existing tests that now catch the new error
**Before:**
```fortran
!$omp simd linear(arr) ! arr is an array
```
→ Compiler crashes
**After:**
```fortran
!$omp simd linear(arr) ! arr is an array
```
→ `error: List item 'arr' in LINEAR clause must be a scalar variable`
<img width="700" height="102" alt="image" src="https://github.com/user-attachments/assets/3726207a-cc6d-4cc6-889c-053814df008f" />
Tested with the original reproducer and it now fails cleanly with a proper error message instead of crashing.
---
Full diff: https://github.com/llvm/llvm-project/pull/175383.diff
3 Files Affected:
- (modified) flang/lib/Semantics/check-omp-loop.cpp (+10)
- (modified) flang/test/Semantics/OpenMP/linear-clause01.f90 (+2)
- (added) flang/test/Semantics/OpenMP/simd-linear-array.f90 (+71)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/175383
More information about the flang-commits
mailing list