[llvm-branch-commits] [flang] [flang][OpenACC] Fix lowering crash on DO WHILE loop constructs (PR #243885)
Kareem Ergawy via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Nov 19 04:05:42 PST 2026
https://github.com/ergawy updated https://github.com/llvm/llvm-project/pull/243885
>From 74b31b2ccde00c897d041757db6b55c27cfc1a10 Mon Sep 17 00:00:00 2001
From: ergawy <kareem.ergawy at gmail.com>
Date: Wed, 18 Nov 2026 04:28:54 -0800
Subject: [PATCH] [flang][OpenACC] Fix lowering crash on DO WHILE loop
constructs
A loop construct associated with a DO WHILE crashed lowering: reading the
DO WHILE's loop control as bounds aborted, both when the DO WHILE is lowered
unstructured and, with -lower-do-while-to-scf-while, when it is lowered
structurally. A DO WHILE has no bounds and no induction variable, so lower
the construct to an acc.loop without bounds that holds the whole DO WHILE,
as for an unstructured loop, with nothing to privatize.
---
flang/lib/Lower/Bridge.cpp | 12 +-
flang/lib/Lower/OpenACC.cpp | 19 +-
.../Lower/OpenACC/acc-loop-seq-do-while.f90 | 227 ++++++++++++++++++
3 files changed, 250 insertions(+), 8 deletions(-)
create mode 100644 flang/test/Lower/OpenACC/acc-loop-seq-do-while.f90
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 235e13de17348f..bff1546dacd152 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -4060,8 +4060,16 @@ class FirConverter : public Fortran::lower::AbstractConverter {
}
// The descent lands on the loop the directive takes over, and every
// level it steps through is one. A construct whose first evaluation is
- // not a loop takes over none.
- if (outerDo)
+ // not a loop takes over none. Neither does a DO WHILE lowered
+ // structurally: it has no bounds, so its acc.loop does not iterate, and
+ // the whole loop, condition included, is lowered inside it. The code
+ // below lowers the evaluations nested in curEval; for a loop the
+ // directive takes over, curEval is that loop, so only its body is
+ // lowered. Resetting curEval to the construct's own evaluation lowers
+ // the DO WHILE evaluation itself instead, through genFIR(DoConstruct).
+ if (outerDo && outerDo->IsDoWhile())
+ curEval = &getEval();
+ else if (outerDo)
absorbedLoop = curEval;
}
diff --git a/flang/lib/Lower/OpenACC.cpp b/flang/lib/Lower/OpenACC.cpp
index fdf474f27b97d8..386d144fb202e9 100644
--- a/flang/lib/Lower/OpenACC.cpp
+++ b/flang/lib/Lower/OpenACC.cpp
@@ -2394,7 +2394,11 @@ buildACCLoopOp(Fortran::lower::AbstractConverter &converter,
// Look at the do/do concurrent loops to extract bounds information unless
// this loop is lowered in an unstructured fashion, in which case bounds are
// not represented on acc.loop and explicit control flow is used inside body.
- if (!eval.lowerAsUnstructured()) {
+ // A DO WHILE has no bounds either, even when it is lowered structurally (to
+ // scf.while): the whole loop is generated inside the body.
+ const bool boundsFree =
+ eval.lowerAsUnstructured() || outerDoConstruct.IsDoWhile();
+ if (!boundsFree) {
processDoLoopBounds(converter, semanticsContext, currentLocation, stmtCtx,
builder, outerDoConstruct, eval, lowerbounds,
upperbounds, steps, privateOperands, ivPrivate, ivTypes,
@@ -2404,10 +2408,13 @@ buildACCLoopOp(Fortran::lower::AbstractConverter &converter,
} else {
// When the loop contains early exits, privatize induction variables, but do
// not create acc.loop bounds. The control flow of the loop will be
- // generated explicitly in the acc.loop body that is just a container.
- privatizeInductionVariables(converter, currentLocation, outerDoConstruct,
- eval, privateOperands, ivPrivate, locs,
- loopsToProcess);
+ // generated explicitly in the acc.loop body that is just a container. Only
+ // a counted DO has a loop variable here: a DO WHILE has none, and a DO
+ // CONCURRENT cannot contain early exits.
+ if (outerDoConstruct.IsDoNormal())
+ privatizeInductionVariables(converter, currentLocation, outerDoConstruct,
+ eval, privateOperands, ivPrivate, locs,
+ loopsToProcess);
}
llvm::SmallVector<mlir::Value> operands;
llvm::SmallVector<int32_t> operandSegments;
@@ -2458,7 +2465,7 @@ buildACCLoopOp(Fortran::lower::AbstractConverter &converter,
for (auto &[hostAssocSym, ultimateSym] : localSymPairs)
converter.copySymbolBinding(ultimateSym, hostAssocSym);
- if (!eval.lowerAsUnstructured()) {
+ if (!boundsFree) {
for (auto [arg, iv] :
llvm::zip(loopOp.getLoopRegions().front()->front().getArguments(),
ivPrivate)) {
diff --git a/flang/test/Lower/OpenACC/acc-loop-seq-do-while.f90 b/flang/test/Lower/OpenACC/acc-loop-seq-do-while.f90
new file mode 100644
index 00000000000000..9feff36e73b366
--- /dev/null
+++ b/flang/test/Lower/OpenACC/acc-loop-seq-do-while.f90
@@ -0,0 +1,227 @@
+! A DO WHILE after an `acc loop seq` directive. A DO WHILE is always lowered
+! with explicit control flow, so the seq loop and its `acc loop` construct are
+! unstructured (!): the acc.loop carries no bounds and holds the loop's blocks.
+! A DO WHILE has no induction variable, so nothing is privatized for it.
+!
+! With -lower-do-while-to-scf-while, a DO WHILE without branches out of it stays
+! structured: the acc.loop, still without bounds, holds an scf.while.
+
+! RUN: bbc -fopenacc -pft-test -o /dev/null %s | FileCheck %s --check-prefix=PFT
+! RUN: bbc -fopenacc -emit-hlfir %s -o - | FileCheck %s
+! RUN: bbc -fopenacc -lower-do-while-to-scf-while -pft-test -o /dev/null %s | FileCheck %s --check-prefix=PFT-SCF
+! RUN: bbc -fopenacc -lower-do-while-to-scf-while -emit-hlfir %s -o - | FileCheck %s --check-prefix=SCF
+
+subroutine do_while_in_kernels(n, a, k)
+ integer :: n, k
+ real :: a(n)
+ !$acc kernels
+ k = 0
+ !$acc loop seq
+ do while (k < n)
+ k = k + 1
+ a(k) = 1.0
+ end do
+ !$acc end kernels
+end subroutine
+
+! PFT-LABEL: Subroutine do_while_in_kernels
+! PFT: <<OpenACCConstruct!>>
+! PFT: <<OpenACCConstruct!>>
+! PFT-NEXT: <<DoConstruct!>>
+! PFT-NEXT: NonLabelDoStmt! {{.*}}: do while(k < n)
+! PFT: <<End DoConstruct!>>
+! PFT: <<End OpenACCConstruct!>>
+! PFT: <<End OpenACCConstruct!>>
+
+! The PFT nodes above lower to:
+! <<OpenACCConstruct!>> kernels -> acc.kernels
+! <<OpenACCConstruct!>> loop seq -> acc.loop without bounds or private
+! operands (seq unstructured)
+! <<DoConstruct!>> do while -> CFG blocks inside that acc.loop
+! CHECK-LABEL: func.func @_QPdo_while_in_kernels
+! CHECK: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_kernelsEk")
+! CHECK: acc.kernels {
+! CHECK: acc.loop {
+! CHECK: ^bb1:
+! CHECK: fir.load %[[K_DECL]]#0
+! CHECK: cf.cond_br %{{.*}}, ^bb2, ^bb3
+! CHECK: ^bb3:
+! CHECK: acc.yield
+! CHECK: } seq unstructured
+! CHECK: acc.terminator
+! CHECK: }
+
+! PFT-SCF-LABEL: Subroutine do_while_in_kernels
+! PFT-SCF: <<OpenACCConstruct>>
+! PFT-SCF: <<OpenACCConstruct>>
+! PFT-SCF-NEXT: <<DoConstruct>>
+! PFT-SCF-NEXT: NonLabelDoStmt {{.*}}: do while(k < n)
+! PFT-SCF: <<End DoConstruct>>
+! PFT-SCF: <<End OpenACCConstruct>>
+! PFT-SCF: <<End OpenACCConstruct>>
+
+! SCF-LABEL: func.func @_QPdo_while_in_kernels
+! SCF: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_kernelsEk")
+! SCF: acc.kernels {
+! SCF: acc.loop {
+! SCF: scf.while : () -> () {
+! SCF: fir.load %[[K_DECL]]#0
+! SCF: scf.condition(%{{.*}})
+! SCF: } do {
+! SCF: scf.yield
+! SCF: }
+! SCF: acc.yield
+! SCF: } seq unstructured
+! SCF: acc.terminator
+! SCF: }
+
+
+! The same DO WHILE after a combined `parallel loop seq` directive.
+subroutine do_while_parallel_loop(n, a, k)
+ integer :: n, k
+ real :: a(n)
+ k = 0
+ !$acc parallel loop seq
+ do while (k < n)
+ k = k + 1
+ a(k) = 1.0
+ end do
+end subroutine
+
+! PFT-LABEL: Subroutine do_while_parallel_loop
+! PFT: <<OpenACCConstruct!>>
+! PFT-NEXT: <<DoConstruct!>>
+! PFT-NEXT: NonLabelDoStmt! {{.*}}: do while(k < n)
+! PFT: <<End DoConstruct!>>
+! PFT: <<End OpenACCConstruct!>>
+
+! The PFT nodes above lower to:
+! <<OpenACCConstruct!>> parallel loop seq -> acc.parallel holding an acc.loop
+! without bounds (seq unstructured)
+! <<DoConstruct!>> do while -> CFG blocks inside that acc.loop
+! CHECK-LABEL: func.func @_QPdo_while_parallel_loop
+! CHECK: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_parallel_loopEk")
+! CHECK: acc.parallel combined(loop) {
+! CHECK: acc.loop combined(parallel) {
+! CHECK: ^bb1:
+! CHECK: fir.load %[[K_DECL]]#0
+! CHECK: cf.cond_br %{{.*}}, ^bb2, ^bb3
+! CHECK: ^bb3:
+! CHECK: acc.yield
+! CHECK: } seq unstructured
+! CHECK: acc.yield
+! CHECK: }
+
+! PFT-SCF-LABEL: Subroutine do_while_parallel_loop
+! PFT-SCF: <<OpenACCConstruct>>
+! PFT-SCF-NEXT: <<DoConstruct>>
+! PFT-SCF-NEXT: NonLabelDoStmt {{.*}}: do while(k < n)
+! PFT-SCF: <<End DoConstruct>>
+! PFT-SCF: <<End OpenACCConstruct>>
+
+! SCF-LABEL: func.func @_QPdo_while_parallel_loop
+! SCF: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_parallel_loopEk")
+! SCF: acc.parallel combined(loop) {
+! SCF: acc.loop combined(parallel) {
+! SCF: scf.while : () -> () {
+! SCF: fir.load %[[K_DECL]]#0
+! SCF: scf.condition(%{{.*}})
+! SCF: } do {
+! SCF: scf.yield
+! SCF: }
+! SCF: acc.yield
+! SCF: } seq unstructured
+! SCF: acc.yield
+! SCF: }
+
+
+! A DO WHILE seq loop nested in a loop in kernels: the enclosing loop stays a
+! structured acc.loop with bounds.
+subroutine do_while_in_loops(n, m, b, kref)
+ integer :: n, m
+ real :: b(m, n)
+ integer :: kref(n)
+ integer :: i, k
+ !$acc kernels
+ do i = 1, n
+ k = 1
+ !$acc loop seq
+ do while (k <= m .and. b(min(k, m), i) <= 0.0)
+ k = k + 1
+ end do
+ kref(i) = k
+ end do
+ !$acc end kernels
+end subroutine
+
+! PFT-LABEL: Subroutine do_while_in_loops
+! PFT: <<OpenACCConstruct~>>
+! PFT-NEXT: <<DoConstruct~>>
+! PFT-NEXT: NonLabelDoStmt {{.*}}: do i = 1, n
+! PFT: <<OpenACCConstruct!>>
+! PFT-NEXT: <<DoConstruct!>>
+! PFT-NEXT: NonLabelDoStmt! {{.*}}: do while(k <= m .and. b(min(k, m), i) <= 0.0)
+! PFT: <<End DoConstruct!>>
+! PFT: <<End OpenACCConstruct!>>
+! PFT: <<End DoConstruct~>>
+! PFT: <<End OpenACCConstruct~>>
+
+! The PFT nodes above lower to:
+! <<OpenACCConstruct~>> kernels -> acc.kernels
+! <<DoConstruct~>> do i -> acc.loop with bounds (auto_), its body in
+! an scf.execute_region
+! <<OpenACCConstruct!>> loop seq -> acc.loop without bounds or private
+! operands (seq unstructured)
+! <<DoConstruct!>> do while -> CFG blocks inside that acc.loop
+! CHECK-LABEL: func.func @_QPdo_while_in_loops
+! CHECK: %[[I_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_loopsEi")
+! CHECK: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_loopsEk")
+! CHECK: acc.kernels {
+! 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: scf.execute_region no_inline {
+! CHECK: acc.loop {
+! CHECK: ^bb1:
+! CHECK: fir.load %[[K_DECL]]#0
+! CHECK: cf.cond_br %{{.*}}, ^bb2, ^bb3
+! CHECK: ^bb3:
+! CHECK: acc.yield
+! CHECK: } seq unstructured
+! CHECK: scf.yield
+! CHECK: }
+! CHECK: acc.yield
+! CHECK: } inclusiveUpperbound(array<i1: true>) auto_
+! CHECK: acc.terminator
+! CHECK: }
+
+! PFT-SCF-LABEL: Subroutine do_while_in_loops
+! PFT-SCF: <<OpenACCConstruct>>
+! PFT-SCF-NEXT: <<DoConstruct>>
+! PFT-SCF-NEXT: NonLabelDoStmt {{.*}}: do i = 1, n
+! PFT-SCF: <<OpenACCConstruct>>
+! PFT-SCF-NEXT: <<DoConstruct>>
+! PFT-SCF-NEXT: NonLabelDoStmt {{.*}}: do while(k <= m .and. b(min(k, m), i) <= 0.0)
+! PFT-SCF: <<End DoConstruct>>
+! PFT-SCF: <<End OpenACCConstruct>>
+! PFT-SCF: <<End DoConstruct>>
+! PFT-SCF: <<End OpenACCConstruct>>
+
+! SCF-LABEL: func.func @_QPdo_while_in_loops
+! SCF: %[[I_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_loopsEi")
+! SCF: %[[K_DECL:.*]]:2 = hlfir.declare %{{.*}} uniq_name("_QFdo_while_in_loopsEk")
+! SCF: acc.kernels {
+! SCF: %[[I_PRIV:.*]] = acc.private varPtr(%[[I_DECL]]#0 : !fir.ref<i32>) recipe(@privatization_ref_i32) implicit(true) name("i")
+! SCF: acc.loop private(%[[I_PRIV]] : !fir.ref<i32>) control(%{{.*}} : i32) = (%{{.*}} : i32) to (%{{.*}} : i32) step (%{{.*}} : i32) {
+! SCF: acc.loop {
+! SCF: scf.while : () -> () {
+! SCF: fir.load %[[K_DECL]]#0
+! SCF: scf.condition(%{{.*}})
+! SCF: } do {
+! SCF: scf.yield
+! SCF: }
+! SCF: acc.yield
+! SCF: } seq unstructured
+! SCF: acc.yield
+! SCF: } inclusiveUpperbound(array<i1: true>) auto_
+! SCF: acc.terminator
+! SCF: }
More information about the llvm-branch-commits
mailing list