[flang-commits] [flang] [flang] fix a dangling else in allOtherUsesAreSafeForAssociate (PR #189748)
via flang-commits
flang-commits at lists.llvm.org
Wed Apr 1 07:38:14 PDT 2026
https://github.com/yebinchon updated https://github.com/llvm/llvm-project/pull/189748
>From 752e82056992a8e0a4ed994c118fdb6bb015cbb4 Mon Sep 17 00:00:00 2001
From: Yebin Chon <ychon at nvidia.com>
Date: Tue, 31 Mar 2026 13:50:26 -0700
Subject: [PATCH 1/3] fix a dangling else that caused isBeforeInBlock to be
called when instructions were in different blocks
---
flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index 53ff13f5a9d28..35cbdd59cf5d8 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -382,11 +382,12 @@ static bool allOtherUsesAreSafeForAssociate(mlir::Value value,
if (!endAssociate)
continue;
// If useOp dominates the endAssociate, then it is definitely safe.
- if (useOp->getBlock() != endAssociate->getBlock())
+ if (useOp->getBlock() != endAssociate->getBlock()) {
if (mlir::DominanceInfo{}.dominates(useOp, endAssociate))
continue;
- if (useOp->isBeforeInBlock(endAssociate))
+ } else if (useOp->isBeforeInBlock(endAssociate)) {
continue;
+ }
}
return false;
}
>From 52b708ac016139ba0f43d070aa6d537a051e8f86 Mon Sep 17 00:00:00 2001
From: Yebin Chon <ychon at nvidia.com>
Date: Tue, 31 Mar 2026 14:26:59 -0700
Subject: [PATCH 2/3] added test
---
flang/test/HLFIR/bufferize-associate-block-ordering.fir | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 flang/test/HLFIR/bufferize-associate-block-ordering.fir
diff --git a/flang/test/HLFIR/bufferize-associate-block-ordering.fir b/flang/test/HLFIR/bufferize-associate-block-ordering.fir
new file mode 100644
index 0000000000000..e69de29bb2d1d
>From aaec9469a6bbecf75f317b69fa55249196884f22 Mon Sep 17 00:00:00 2001
From: Yebin Chon <ychon at nvidia.com>
Date: Tue, 31 Mar 2026 14:28:37 -0700
Subject: [PATCH 3/3] added test body
---
.../bufferize-associate-block-ordering.fir | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/flang/test/HLFIR/bufferize-associate-block-ordering.fir b/flang/test/HLFIR/bufferize-associate-block-ordering.fir
index e69de29bb2d1d..57c8ec5102cc8 100644
--- a/flang/test/HLFIR/bufferize-associate-block-ordering.fir
+++ b/flang/test/HLFIR/bufferize-associate-block-ordering.fir
@@ -0,0 +1,41 @@
+// RUN: fir-opt --bufferize-hlfir %s | FileCheck %s
+
+func.func @shape_of_non_dominating_block(%cond: i1, %n: index) {
+ %shape = fir.shape %n : (index) -> !fir.shape<1>
+ %expr = hlfir.elemental %shape : (!fir.shape<1>) -> !hlfir.expr<?xi32> {
+ ^bb0(%i: index):
+ %c0 = arith.constant 0 : i32
+ hlfir.yield_element %c0 : i32
+ }
+ %assoc:3 = hlfir.associate %expr(%shape) {uniq_name = "test"} : (!hlfir.expr<?xi32>, !fir.shape<1>) -> (!fir.box<!fir.array<?xi32>>, !fir.ref<!fir.array<?xi32>>, i1)
+ cf.cond_br %cond, ^bb1, ^bb2
+^bb1:
+ %s = hlfir.shape_of %expr : (!hlfir.expr<?xi32>) -> !fir.shape<1>
+ cf.br ^bb3
+^bb2:
+ cf.br ^bb3
+^bb3:
+ hlfir.end_associate %assoc#1, %assoc#2 : !fir.ref<!fir.array<?xi32>>, i1
+ hlfir.destroy %expr : !hlfir.expr<?xi32>
+ return
+}
+
+// The shape_of in ^bb1 does not dominate the end_associate in ^bb3
+// (^bb3 is reachable from ^bb2 as well), so allOtherUsesAreSafeForAssociate
+// returns false and the associate takes the copy path.
+// CHECK-LABEL: func.func @shape_of_non_dominating_block(
+// CHECK-SAME: %[[COND:.*]]: i1, %[[N:.*]]: index)
+// CHECK: %[[SHAPE:.*]] = fir.shape %[[N]]
+// CHECK: %[[ELEM_BUF:.*]] = fir.allocmem !fir.array<?xi32>, %[[N]] {{.*}}".tmp.array"
+// CHECK: fir.do_loop
+// CHECK: %[[COPY_BUF:.*]] = fir.allocmem !fir.array<?xi32>, %[[N]] {{.*}}".tmp"
+// CHECK: hlfir.assign %{{.*}} to %{{.*}} temporary_lhs
+// CHECK: cf.cond_br %[[COND]], ^bb1, ^bb2
+// CHECK: ^bb1:
+// CHECK: cf.br ^bb3
+// CHECK: ^bb2:
+// CHECK: cf.br ^bb3
+// CHECK: ^bb3:
+// CHECK: fir.freemem
+// CHECK: fir.freemem
+// CHECK: return
More information about the flang-commits
mailing list