llvm / llvm/llvm-project

[Flang] Zero-sized FORALL is executed when the absolute values of both bounds are extremely large

Open
#222,869 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This was discovered by @claude.

Consider the following code:

```fortran
program test_kind4all_1_2_v9
implicit none
integer(8), parameter :: sentinel = 99_8
integer(8), parameter :: lo = -HUGE(0_8)
integer(8), parameter :: hi = HUGE(0_8)
integer(8), dimension(2) :: A
A = sentinel
! stride=-1: zero-trip since lo < hi; (hi-lo-1)=-1 wraps on naive path to large positive
forall (integer(8) :: i = lo:hi:-1) A(1) = 0_8
if (A(1) /= sentinel) stop 1
if (A(2) /= sentinel) stop 2
print *, "pass"
end program
```

Since `lo` is less than `hi` and the step is negative, the set of combinations of *index-name* values should be empty.
However, Flang and GFortran seem to execute the *forall-assignment-stmt*: https://godbolt.org/z/sadrrGY7n

If the FORALL construct is replaced with a standard DO loop, GFortran behaves correctly, but Flang still fails.

Contributor guide

Open the contributing guide

Research direction

Start with the provided Fortran reproducer and compare Flang's FORALL behavior with the standard DO-loop behavior described in the issue. Use the Godbolt example to reproduce the failure and inspect the Flang compiler path handling FORALL bounds and trip counts. Done means the zero-trip FORALL leaves both array elements unchanged and the reproducer prints "pass".

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.