llvm / llvm/llvm-project

[Flang] IS_CONTIGUOUS returns incorrect result for possibly non-contiguous vector subscripts of constant arrays

Open
#199,878 5 comments 0 reactions 1 assignee Claimed by @eugeneepshteyn View on GitHub
documentation flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

## Summary

When IS_CONTIGUOUS is called with a non-contiguous array section of a constant array, it returns an incorrect result. I could not find any documented restrictions regarding constant arrays.

* 8.5.7 CONTIGUOUS attribute

> An object is contiguous if it is
> ...
> (7) a nonzero-sized array section (9.5.3) provided that
> (a) its base object is contiguous,
> (b) it does not have a vector subscript,
> (c) the array element ordering of the elements of the section is the same as the array element ordering of those elements of the base object,
> (d) in the array element ordering of the base object, every element of the base object that is not an element of the section either precedes every element of the section or follows every element of the section,
> (e) if the array is of type character and a substring-range appears, the substring-range specifies all of the characters of the parent-string (9.4.1),
> (f) only its final part-ref has nonzero rank, and
> (g) it is not the real or imaginary part (9.4.4) of an array of type complex.
>
> An object is not contiguous if it is an array subobject, and
> * the object has two or more elements,
> * the elements of the object in array element order are not consecutive in the elements of the base object,
> * the object is not of type character with length zero, and
> * the object is not of a derived type that has no ultimate components other than zero-sized arrays and
characters with length zero.
>
> It is processor dependent whether any other object is contiguous.

### Reproducer

* test.f90

```fortran
integer,parameter::a(5)=[1,2,3,4,5]
integer::b(5)
logical,parameter::n1=is_contiguous(a(::2))
logical,parameter::n2=is_contiguous(b(::2))
if (n1 ) print *,101
if (n2 ) print *,102
if (is_contiguous(a(::2))) print *,103
if (is_contiguous(b(::2))) print *,104
print *,'pass'
end
```

* commands

```console
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git f95ccbae8bd70e56e31fc04d6307d317212b7fbc)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/llvm/build/bin
Build config: +assertions
$ flang test.f90 && ./a.out
101
103
pass
```

## Non-problematic Conditions

The issue does not occur under the following conditions:
* Removing PARAMETER attribute from the array `a`.

## Other Compilers

* GFortran
```console
$ gfortran --version
GNU Fortran (GCC) 15.2.0
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ gfortran test.f90 && ./a.out
pass
```

* Intel Fortran
```console
$ ifx --version
ifx (IFX) 2025.3.2 20260112
Copyright (C) 1985-2026 Intel Corporation. All rights reserved.

$ ifx -O0 test.f90 && ./a.out
pass
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.