Issue 233990
Summary [flang][OpenMP] ALLOCATABLE SIMD loop variable: wrong final value and spurious "narrowed into 64 bits" warning
Labels flang
Assignees
Reporter adit4443ya
    When the loop variable of `!$omp simd` is an `integer, allocatable` scalar, flang gives a wrong final value for it, a misleading warning, and a 64-bit loop counter.

Reproducer (`alloc_iv.f90`):
```fortran
program alloc_iv
 implicit none
  integer, allocatable :: i
  integer :: k
  allocate(i)
 k = 0
  !$omp simd linear(k)
  do i = 1, 4
    k = k + 1
  end do
 print *, i, k
end program
```

flang (main):
```
$ flang -fopenmp alloc_iv.f90 && ./a.out
warning: loc("alloc_iv.f90":7:9): OpenMP loop iteration variable cannot have more than 64 bits size and will be narrowed into 64 bits.
warning: loc("alloc_iv.f90":7:9): OpenMP loop iteration variable cannot have more than 64 bits size and will be narrowed into 64 bits.
 4 4
```

gfortran 15.2.1 (no warning, with `-Wall` too):
```
$ gfortran -fopenmp -Wall alloc_iv.f90 && ./a.out
           5 4
```

There are two problems.

1. **Wrong final value of `i` (4 instead of 5).** The loop variable of a `simd` construct is implicitly `linear`, so after the loop it should have the value it would have after a sequential loop, which is 5. `genSimdImplicitLinear` (`flang/lib/Lower/OpenMP/OpenMP.cpp`) skips implicit linearization for allocatable or pointer loop variables. A TODO there gives the reason as the type mismatch between `!fir.ref<!fir.box<!fir.heap<i32>>>` and the element type. #228827 added `linear` privatizers that handle ALLOCATABLE list items (explicit `linear(i)` on an allocatable loop variable now gives the right final value), so allocatables no longer need to be skipped. POINTER loop variables are a separate case.

2. **Spurious warning and an `i64` loop counter.** `i` is a default 32-bit integer, but flang warns that it is wider than 64 bits and lowers the loop with an `i64` counter:
   ```
 omp.loop_nest (%arg1) : i64 = (%9) to (%10) inclusive step (%11) {
     %23 = fir.convert %arg1 : (i64) -> i32
   ```
   With a plain `integer :: i` the same loop is `omp.loop_nest (%arg2) : i32 = ...` with no warning. The loop variable's size comes from `Symbol::size()` (`flang/lib/Lower/OpenMP/Utils.cpp`, `loopVarTypeSize = std::max(loopVarTypeSize, ...->GetUltimate().size())`, and the same in `OpenMP.cpp`). For an ALLOCATABLE that is the size of the descriptor, not the size of the integer. It should use the size of the declared type instead.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to