[flang-commits] [flang] [flang] Rewrite an IF body ending in CYCLE as an IF/ELSE (PR #224718)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Sep 18 12:56:00 PDT 2026


================
@@ -0,0 +1,70 @@
+! RUN: %flang_fc1 -mmlir --wrap-unstructured-constructs-in-execute-region -fdebug-dump-pft %s 2>&1 | FileCheck %s
+
+! An IF body whose last statement is a CYCLE becomes an IF/ELSE, with the
+! statements after the construct moved into the ELSE branch. The condition is
+! not negated, the statements ahead of the CYCLE must still run when it holds.
+! A `!` suffix in this dump marks a construct as unstructured.
+
+subroutine trailing_cycle(n, v)
+  integer :: n, i, v(n)
+
+  ! CHECK-LABEL: Subroutine trailing_cycle
+  ! CHECK: <<DoConstruct>>
+  ! CHECK-NOT: DoConstruct!
+  ! CHECK: NonLabelDoStmt{{.*}}: do i = 1, n
+  ! CHECK: <<IfConstruct>>
+  ! CHECK-NOT: [negate]
+  ! CHECK: IfThenStmt{{.*}}: if(i == 1) then
+  ! CHECK: AssignmentStmt: v(i) = 0
+  ! CHECK: ElseStmt
+  ! CHECK: AssignmentStmt: v(i) = 1
+  ! CHECK: EndIfStmt
+  ! CHECK: <<End IfConstruct>>
+  ! CHECK: EndDoStmt
+  ! CHECK: <<End DoConstruct>>
+  ! CHECK-NOT: CycleStmt
+  do i = 1, n
+     if (i == 1) then
+        v(i) = 0
+        cycle
+     end if
+     v(i) = 1
----------------
vzakhari wrote:

Can you please add a test where there is another cycle after the IF with cycle? E.g.:
```
do
  if ... then
    ...
    cycle
  end if
  ...
  if ... then
    cycle
  end if
end do
```

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


More information about the flang-commits mailing list