[llvm-branch-commits] [flang] [flang][OpenACC] Lower unstructured seq loops directly in kernels (PR #240801)

Kareem Ergawy via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 10 03:59:55 PST 2026


https://github.com/ergawy created https://github.com/llvm/llvm-project/pull/240801

An `acc loop seq` with an early exit directly in a kernels region still hit the "unstructured do loop in acc kernels" TODO, because the kernels construct around it stays unstructured. Its control flow stays in the acc.kernels region, its acc.loop privatizes its induction variables, and no parallel loop runs it on more than one thread, so lower it like a structured seq loop in kernels.

Stacked on https://github.com/llvm/llvm-project/pull/240800.

🤖 Generated with [Claude Code](https://claude.com/claude-code)


>From b33e266b44637936f3f78b3d92821d4d746c7dc0 Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at gmail.com>
Date: Tue, 10 Nov 2026 03:39:15 -0800
Subject: [PATCH] [flang][OpenACC] Lower unstructured seq loops directly in
 kernels

An `acc loop seq` with an early exit directly in a kernels region still
hit the "unstructured do loop in acc kernels" TODO, because the kernels
construct around it stays unstructured. Its control flow stays in the
acc.kernels region, its acc.loop privatizes its induction variables,
and no parallel loop runs it on more than one thread, so lower it like
a structured seq loop in kernels.
---
 flang/lib/Lower/OpenACC.cpp                   |  39 ++++--
 .../Todo/do-loops-to-acc-loops-todo.f90       |  39 ------
 .../acc-kernels-unstructured-seq-loop.f90     | 119 ++++++++++++++++--
 3 files changed, 144 insertions(+), 53 deletions(-)

diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index 1edf31cec2502..e1a1df6c20cfc 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -5758,8 +5758,8 @@ std::pair<uint64_t, bool> Fortran::lower::getCollapseSizeAndForce(
 /// Return true if \p doConstruct, which \p eval holds and which is lowered as
 /// unstructured, is a DO of an `acc loop seq` construct whose control flow is
 /// contained: the construct's acc.loop privatizes the DO's induction variable,
-/// and the construct sits in an enclosing construct that is lowered
-/// structurally.
+/// and the construct sits either in an enclosing construct that is lowered
+/// structurally or directly in a kernels construct.
 ///
 /// Running example, with each construct's PFT classification and what it
 /// lowers to:
@@ -5785,6 +5785,13 @@ std::pair<uint64_t, bool> Fortran::lower::getCollapseSizeAndForce(
 /// its body, so the seq loop's CFG is contained there, and the DOs over j and
 /// i stay parallelizable loops.
 ///
+/// Were the `acc loop seq` directly in the kernels construct, with no DOs over
+/// j and i, nothing would enclose it but the kernels region, which stays
+/// unstructured. The seq loop's CFG is then contained in the acc.kernels
+/// region, and no parallel loop runs it on more than one thread. As for a
+/// structured seq loop there, the seq loop runs as a sequential part of the
+/// region, and code following it in the region runs after it.
+///
 /// genOpenACCLoopFromDoConstruct calls this once for the DO over k and once for
 /// the DO over l. Both times the builder inserts into currentLoop's region: an
 /// unstructured acc.loop carries no bounds, so the DOs it owns are emitted as
@@ -5834,13 +5841,31 @@ static bool isDoOfContainedUnstructuredSeqLoop(
   // lowered structurally. In the example that is the DO over i, which the PFT
   // reclassified from unstructured to structured with unstructured internals
   // (~); the DO over j and the kernels construct above it never become
-  // unstructured. Two variations do not qualify: with `exit kloop` changed to
-  // an EXIT of the DO over i, that DO stays unstructured; and were `acc loop
-  // seq` directly in the kernels construct, the kernels construct would stay
-  // unstructured.
+  // unstructured. With `exit kloop` changed to an EXIT of the DO over i, that
+  // DO stays unstructured and the seq loop does not qualify: the DO over i
+  // could run in parallel, and its own branching would leave its body.
   const Fortran::lower::pft::Evaluation *container =
       accEval ? accEval->parentConstruct : nullptr;
-  return container && !container->lowerAsUnstructured();
+  if (!container)
+    return false;
+  if (!container->lowerAsUnstructured())
+    return true;
+
+  // The seq loop is also contained when the `acc loop seq` is directly in the
+  // kernels construct: the kernels construct stays unstructured, but the seq
+  // loop's control flow stays in the acc.kernels region.
+  const auto *accConstruct =
+      container->getIf<Fortran::parser::OpenACCConstruct>();
+  const auto *blockConstruct =
+      accConstruct ? std::get_if<Fortran::parser::OpenACCBlockConstruct>(
+                         &accConstruct->u)
+                   : nullptr;
+  return blockConstruct &&
+         std::get<Fortran::parser::AccBlockDirective>(
+             std::get<Fortran::parser::AccBeginBlockDirective>(
+                 blockConstruct->t)
+                 .t)
+                 .v == llvm::acc::ACCD_kernels;
 }
 
 /// Create an ACC loop operation for a DO construct when inside ACC compute
diff --git a/flang/test/Lower/OpenACC/Todo/do-loops-to-acc-loops-todo.f90 b/flang/test/Lower/OpenACC/Todo/do-loops-to-acc-loops-todo.f90
index e9d1177b02177..9bba7aa067ffa 100644
--- a/flang/test/Lower/OpenACC/Todo/do-loops-to-acc-loops-todo.f90
+++ b/flang/test/Lower/OpenACC/Todo/do-loops-to-acc-loops-todo.f90
@@ -4,8 +4,6 @@
 ! RUN: %not_todo_cmd bbc -fopenacc -emit-hlfir %t/collapse_lt.f90 -o - 2>&1 | FileCheck %s --check-prefix=CHECK7
 ! RUN: %not_todo_cmd bbc -fopenacc -emit-hlfir %t/collapse_gt.f90 -o - 2>&1 | FileCheck %s --check-prefix=CHECK8
 ! RUN: %not_todo_cmd bbc -fopenacc -emit-hlfir %t/collapse_nested.f90 -o - 2>&1 | FileCheck %s --check-prefix=CHECK6
-! RUN: bbc -fopenacc -pft-test -o /dev/null %t/seq_exit_in_kernels.f90 | FileCheck %s --check-prefix=PFT9
-! RUN: %not_todo_cmd bbc -fopenacc -emit-hlfir %t/seq_exit_in_kernels.f90 -o - 2>&1 | FileCheck %s --check-prefix=CHECK9
 
 //--- do_loop_with_cycle_goto.f90
 
@@ -97,40 +95,3 @@ subroutine combined(i, j, k)
   ! CHECK6: not yet implemented: OpenACC LOOP with nested DO CONCURRENT
 
 end subroutine
-
-//--- seq_exit_in_kernels.f90
-
-! A seq loop with an early EXIT directly in the `acc kernels` region: no
-! enclosing construct contains its control flow, so the kernels construct
-! itself stays unstructured.
-subroutine seq_exit_in_kernels(m, b, kfirst)
-  integer :: m
-  real :: b(m)
-  integer :: kfirst
-  integer :: k
-  !$acc kernels
-  kfirst = 0
-  !$acc loop seq
-  do k = 1, m
-    if (b(k) > 0.0) then
-      kfirst = k
-      exit
-    end if
-  end do
-  !$acc end kernels
-
-! PFT9-LABEL: Subroutine seq_exit_in_kernels
-! PFT9:         <<OpenACCConstruct!>>
-! PFT9:           <<OpenACCConstruct!>>
-! PFT9-NEXT:        <<DoConstruct!>>
-! PFT9-NEXT:          NonLabelDoStmt {{.*}}: do k = 1, m
-! PFT9:               ExitStmt! {{.*}}: exit
-! PFT9:             <<End DoConstruct!>>
-! PFT9:           <<End OpenACCConstruct!>>
-! PFT9:         <<End OpenACCConstruct!>>
-
-! Lowering stops at the `!` DO over k (line 13): its `acc loop` construct sits
-! directly in the `!` kernels construct, so nothing contains its control flow.
-! CHECK9: {{.*}}seq_exit_in_kernels.f90":13:3){{.*}}not yet implemented: unstructured do loop in acc kernels
-
-end subroutine
diff --git a/flang/test/Lower/OpenACC/acc-kernels-unstructured-seq-loop.f90 b/flang/test/Lower/OpenACC/acc-kernels-unstructured-seq-loop.f90
index a9b6f2346e62a..5951afc66d0ae 100644
--- a/flang/test/Lower/OpenACC/acc-kernels-unstructured-seq-loop.f90
+++ b/flang/test/Lower/OpenACC/acc-kernels-unstructured-seq-loop.f90
@@ -1,13 +1,14 @@
 ! An `acc loop seq` with an early EXIT inside an `acc kernels` region. The EXIT
-! makes the seq loop unstructured, but its enclosing loop keeps its branching
-! inside its body, so the seq loop's control flow is contained there: the
-! enclosing loop stays a structured acc.loop with bounds, and the seq acc.loop
-! privatizes the induction variables of the DOs it owns and of the
+! makes the seq loop and its `acc loop` construct unstructured (!). The seq
+! acc.loop privatizes the induction variables of the DOs it owns and of the
 ! unstructured DOs nested in its body.
 !
-! In the pre-FIR tree, the seq loop and its `acc loop` construct are
-! unstructured (!), while the enclosing loop and the kernels construct are
-! structured with unstructured internals (~).
+! In the first cases, the seq loop is nested in a loop that keeps the branching
+! inside its body: that loop and the kernels construct are structured with
+! unstructured internals (~), and the loop stays a structured acc.loop with
+! bounds. In the last cases, the seq loop is directly in the kernels region:
+! the kernels construct stays unstructured, and the seq loop's control flow
+! stays in the acc.kernels region.
 
 ! RUN: bbc -fopenacc -pft-test -o /dev/null %s | FileCheck %s --check-prefix=PFT
 ! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s
@@ -297,3 +298,107 @@ subroutine collapse_force_sibling_do(n, m, p, b, kref)
 ! CHECK:           } inclusiveUpperbound(array<i1: true>) auto_
 ! CHECK:           acc.terminator
 ! CHECK:         }
+
+! A seq loop with an early EXIT directly in the kernels region. Nothing
+! encloses it but the kernels construct, which stays unstructured; the seq
+! loop's control flow stays in the acc.kernels region.
+subroutine seq_exit_in_kernels(m, b, kfirst)
+  integer :: m
+  real :: b(m)
+  integer :: kfirst
+  integer :: k
+  !$acc kernels
+  kfirst = 0
+  !$acc loop seq
+  do k = 1, m
+    if (b(k) > 0.0) then
+      kfirst = k
+      exit
+    end if
+  end do
+  !$acc end kernels
+end subroutine
+
+! PFT-LABEL: Subroutine seq_exit_in_kernels
+! PFT:         <<OpenACCConstruct!>>
+! PFT:           <<OpenACCConstruct!>>
+! PFT-NEXT:        <<DoConstruct!>>
+! PFT-NEXT:          NonLabelDoStmt {{.*}}: do k = 1, m
+! PFT:               ExitStmt! {{.*}}: exit
+! PFT:             <<End DoConstruct!>>
+! PFT:           <<End OpenACCConstruct!>>
+! PFT:         <<End OpenACCConstruct!>>
+
+! The PFT nodes above lower to:
+!   <<OpenACCConstruct!>> kernels  -> acc.kernels
+!   <<OpenACCConstruct!>> loop seq -> acc.loop private(k) without bounds
+!                                     (seq unstructured)
+!   <<DoConstruct!>> do k          -> CFG blocks inside that acc.loop
+! CHECK-LABEL: func.func @_QPseq_exit_in_kernels
+! CHECK:         %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFseq_exit_in_kernelsEk")
+! CHECK:         acc.kernels {
+! CHECK:           %[[K_PRIV:.*]] = acc.private varPtr(%[[K_DECL]]#0 : !fir.ref<i32>) recipe(@privatization_ref_i32) implicit(true) name("k")
+! CHECK:           acc.loop private(%[[K_PRIV]] : !fir.ref<i32>) {
+! CHECK:             hlfir.declare %[[K_PRIV]] uniq_name("_QFseq_exit_in_kernelsEk")
+! CHECK:             acc.yield
+! CHECK:           } seq  unstructured
+! CHECK:           acc.terminator
+! CHECK:         }
+
+! The same seq loop followed by a parallelizable loop in the kernels region.
+! The following loop stays a structured acc.loop with bounds.
+subroutine seq_exit_then_loop(n, m, a, b, kfirst)
+  integer :: n, m
+  real :: a(n), b(m)
+  integer :: kfirst
+  integer :: i, k
+  !$acc kernels
+  kfirst = 0
+  !$acc loop seq
+  do k = 1, m
+    if (b(k) > 0.0) then
+      kfirst = k
+      exit
+    end if
+  end do
+  do i = 1, n
+    a(i) = a(i) + kfirst
+  end do
+  !$acc end kernels
+end subroutine
+
+! PFT-LABEL: Subroutine seq_exit_then_loop
+! PFT:         <<OpenACCConstruct!>>
+! PFT:           <<OpenACCConstruct!>>
+! PFT-NEXT:        <<DoConstruct!>>
+! PFT-NEXT:          NonLabelDoStmt {{.*}}: do k = 1, m
+! PFT:               ExitStmt! {{.*}}: exit
+! PFT:             <<End DoConstruct!>>
+! PFT:           <<End OpenACCConstruct!>>
+! PFT-NEXT:      <<DoConstruct>>
+! PFT-NEXT:        NonLabelDoStmt {{.*}}: do i = 1, n
+! PFT:           <<End DoConstruct>>
+! PFT:         <<End OpenACCConstruct!>>
+
+! The PFT nodes above lower to:
+!   <<OpenACCConstruct!>> kernels  -> acc.kernels
+!   <<OpenACCConstruct!>> loop seq -> acc.loop private(k) without bounds
+!                                     (seq unstructured)
+!   <<DoConstruct!>> do k          -> CFG blocks inside that acc.loop
+!   <<DoConstruct>> do i           -> acc.loop with bounds (auto_)
+! CHECK-LABEL: func.func @_QPseq_exit_then_loop
+! CHECK:         %[[I_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFseq_exit_then_loopEi")
+! CHECK:         %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFseq_exit_then_loopEk")
+! CHECK:         acc.kernels {
+! CHECK:           %[[K_PRIV:.*]] = acc.private varPtr(%[[K_DECL]]#0 : !fir.ref<i32>) recipe(@privatization_ref_i32) implicit(true) name("k")
+! CHECK:           acc.loop private(%[[K_PRIV]] : !fir.ref<i32>) {
+! CHECK:             hlfir.declare %[[K_PRIV]] uniq_name("_QFseq_exit_then_loopEk")
+! CHECK:             acc.yield
+! CHECK:           } seq  unstructured
+! CHECK:           %[[I_PRIV:.*]] = acc.private varPtr(%[[I_DECL]]#0 : !fir.ref<i32>) recipe(@privatization_ref_i32) implicit(true) name("i")
+! CHECK:           acc.loop private(%[[I_PRIV]] : !fir.ref<i32>) control(%{{.*}} : i32) = (%{{.*}} : i32) to (%{{.*}} : i32)  step (%{{.*}} : i32) {
+! CHECK:             hlfir.declare %[[I_PRIV]] uniq_name("_QFseq_exit_then_loopEi")
+! CHECK:             acc.yield
+! CHECK:           } inclusiveUpperbound(array<i1: true>) auto_
+! CHECK:           acc.terminator
+! CHECK:         }



More information about the llvm-branch-commits mailing list