[llvm-branch-commits] [flang] [flang] Lower loops whose branching is confined to their body structurally (PR #225758)

Kareem Ergawy via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Sep 25 02:43:13 PDT 2026


ergawy wrote:

> Codex found that DCE can remove side-effect free non-terminating loop bodies, leaving just the finite fir.do_loop.
> 
> ```
> subroutine self_loop(n)
>   integer :: n, i
>   do i = 1, n
> 10   goto 10
>   end do
> 
>   call never_executed()
> end subroutine
> ```
> 
> `flang -fc1 -emit-hlfir ...` produces
> 
> ```
> ...
> fir.do_loop ... {
>   fir.store ...
> }
> ...
> fir.call ...
> return
> ```
> 
> I think the problem is the recursive memory effects on the scf.execute_region turning up nothing. If this instead used a fully unstructured lowering, the unstructured branches would all be terminators, which are never removed by DCE.

Thanks for the comment. I had a detection for infinite loops but that wasn't sufficient for such cases. Added detection support in a23f20250b87fb48dcef33310a8257f00e39f33f (https://github.com/llvm/llvm-project/pull/225757) and lowering testing in 245f71a9e62f62559208fa7c9a659de6df2a2217 (this PR).

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


More information about the llvm-branch-commits mailing list