[flang-commits] [flang] [llvm] [flang][OpenMP] Sink intervening code unguarded into collapsed loop body (PR #225159)

Sunil Shrestha via flang-commits flang-commits at lists.llvm.org
Wed Sep 23 15:54:55 PDT 2026


sshrestha-aa wrote:

Now that the intervening code is emitted unguarded into the collapsed body, it runs once per collapsed logical iteration instead of once per enclosing iteration. This changes the result relative to the sequential nest in a few cases.

Cases 1 and 2 are about frequency, where the OpenMP wording likely allows some leniency. Cases 3 and 4 look incorrect, even though they match Clang's behavior. 

Case 1 — print in intervening code 
```
!$omp parallel do collapse(2)
do i = 1, n
   print *, "row", i          ! number of print 
   do j = 1, m
   end do
end do
```


Case 2 — impure function call in intervening code
```
!$omp parallel do collapse(2)
do i = 1, n
   f = func(i)                ! number of calls
   do j = 1, m
      ! use f
   end do
end do
```

Case 3 — reduction updated in intervening code
```
!$omp parallel do collapse(2) reduction(+:r)
do i = 1, n
   r = r + 1                  ! reduction result sequential nest
   do j = 1, m
   end do
end do
```


Case 4 — inner loop variable read after the inner loop 
```
!$omp parallel do collapse(2)
do i = 1, n
   do j = 1, m
   end do
   ksave = j                  ! ksave ends up as m instead of m+1
end do
```

https://github.com/llvm/llvm-project/pull/225159


More information about the flang-commits mailing list