Krish Gupta b76c84e70f
[flang][OpenMP] Fix crash when arrays used in LINEAR clause (#175383)
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

Tested with the original reproducer and it now fails cleanly with a
proper error message instead of crashing.
2026-01-12 14:25:55 -06:00

88 lines
2.8 KiB
Fortran

! RUN: %python %S/../test_errors.py %s %flang -fopenmp
! OpenMP Version 4.5
! Various checks with the ordered construct
SUBROUTINE LINEAR_GOOD(N)
INTEGER N, i, j, a, b(10)
!$omp target
!$omp teams
!$omp distribute parallel do simd linear(i)
do i = 1, N
a = 3.14
enddo
!$omp end distribute parallel do simd
!$omp end teams
!$omp end target
END SUBROUTINE LINEAR_GOOD
SUBROUTINE LINEAR_BAD(N)
INTEGER N, i, j, a, b(10)
!$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
!$omp distribute parallel do simd linear(j)
do i = 1, N
a = 3.14
enddo
!$omp end distribute parallel do simd
!$omp end teams
!$omp end target
!$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: 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
enddo
!$omp end distribute parallel do simd
!$omp end teams
!$omp end target
!$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: 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
enddo
!$omp end distribute parallel do simd
!$omp end teams
!$omp end target
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
!$omp distribute simd linear(i,j)
do i = 1, N
do j = 1, N
a = 3.14
enddo
enddo
!$omp end distribute simd
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
!ERROR: Variable 'j' not allowed in LINEAR clause, only loop iterator can be specified in LINEAR clause of a construct combined with DISTRIBUTE
!$omp distribute simd linear(i,j) collapse(1)
do i = 1, N
do j = 1, N
a = 3.14
enddo
enddo
!$omp end distribute simd
!WARNING: `DISTRIBUTE` must be dynamically enclosed in a `TEAMS` region.
!$omp distribute simd linear(i,j) collapse(2)
do i = 1, N
do j = 1, N
a = 3.14
enddo
enddo
!$omp end distribute simd
END SUBROUTINE LINEAR_BAD