Skip to content

Commit 1804748

Browse files
committed
[flang][hlfir] fix issue 118922
1 parent 788d5a5 commit 1804748

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -806,8 +806,13 @@ struct ElementalOpConversion
806806
// elemental is a "view" over a variable (e.g parentheses or transpose).
807807
if (auto asExpr = elementValue.getDefiningOp<hlfir::AsExprOp>()) {
808808
if (asExpr->hasOneUse() && !asExpr.isMove()) {
809-
elementValue = hlfir::Entity{asExpr.getVar()};
810-
rewriter.eraseOp(asExpr);
809+
// Check that the asExpr is the final operation before the yield,
810+
// otherwise, clean-ups could impact the memory being re-used.
811+
mlir::Operation *nextOp = asExpr->getNextNode();
812+
if (nextOp && nextOp == yield.getOperation()) {
813+
elementValue = hlfir::Entity{asExpr.getVar()};
814+
rewriter.eraseOp(asExpr);
815+
}
811816
}
812817
}
813818
rewriter.eraseOp(yield);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Test issue 113843 and 118922 fix: do not elide hlfir.elemental final as_expr
2+
// copy if this is not the last operation.
3+
// RUN: fir-opt %s --bufferize-hlfir | FileCheck %s
4+
5+
func.func @_QMmPbug(%val: !fir.char<1>, %var: !fir.ref<!fir.array<10x!fir.char<1>>>) {
6+
%c1 = arith.constant 1 : index
7+
%c10 = arith.constant 10 : index
8+
%1 = fir.shape %c10 : (index) -> !fir.shape<1>
9+
%expr = hlfir.elemental %1 typeparams %c1 unordered : (!fir.shape<1>, index) -> !hlfir.expr<10x!fir.char<1>> {
10+
^bb0(%arg2: index):
11+
%alloc = fir.allocmem !fir.char<1>
12+
fir.store %val to %alloc : !fir.heap<!fir.char<1>>
13+
%addr = fir.convert %alloc: (!fir.heap<!fir.char<1>>) -> !fir.ref<!fir.char<1>>
14+
%9 = hlfir.as_expr %addr : (!fir.ref<!fir.char<1>>) -> !hlfir.expr<!fir.char<1>>
15+
fir.freemem %alloc : !fir.heap<!fir.char<1>>
16+
hlfir.yield_element %9 : !hlfir.expr<!fir.char<1>>
17+
}
18+
hlfir.assign %expr to %var : !hlfir.expr<10x!fir.char<1>>, !fir.ref<!fir.array<10x!fir.char<1>>>
19+
hlfir.destroy %expr : !hlfir.expr<10x!fir.char<1>>
20+
return
21+
}
22+
23+
// CHECK-LABEL: func.func @_QMmPbug(
24+
// CHECK-SAME: %[[VAL_0:.*]]: !fir.char<1>,
25+
// CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.array<10x!fir.char<1>>>) {
26+
// CHECK: %[[VAL_2:.*]] = fir.alloca !fir.char<1> {bindc_name = ".tmp"}
27+
// CHECK: %[[VAL_3:.*]] = arith.constant 1 : index
28+
// CHECK: %[[VAL_4:.*]] = arith.constant 10 : index
29+
// CHECK: %[[VAL_5:.*]] = fir.shape %[[VAL_4]] : (index) -> !fir.shape<1>
30+
// CHECK: %[[VAL_6:.*]] = fir.allocmem !fir.array<10x!fir.char<1>> {bindc_name = ".tmp.array", uniq_name = ""}
31+
// CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_6]](%[[VAL_5]]) typeparams %[[VAL_3]] {uniq_name = ".tmp.array"} : (!fir.heap<!fir.array<10x!fir.char<1>>>, !fir.shape<1>, index) -> (!fir.heap<!fir.array<10x!fir.char<1>>>, !fir.heap<!fir.array<10x!fir.char<1>>>)
32+
// CHECK: %[[VAL_8:.*]] = arith.constant true
33+
// CHECK: %[[VAL_9:.*]] = arith.constant 1 : index
34+
// CHECK: fir.do_loop %[[VAL_10:.*]] = %[[VAL_9]] to %[[VAL_4]] step %[[VAL_9]] unordered {
35+
// CHECK: %[[VAL_11:.*]] = fir.allocmem !fir.char<1>
36+
// CHECK: fir.store %[[VAL_0]] to %[[VAL_11]] : !fir.heap<!fir.char<1>>
37+
// CHECK: %[[VAL_12:.*]] = fir.convert %[[VAL_11]] : (!fir.heap<!fir.char<1>>) -> !fir.ref<!fir.char<1>>
38+
// CHECK: %[[VAL_13:.*]] = arith.constant 1 : index
39+
// CHECK: %[[VAL_14:.*]] = arith.constant false
40+
// CHECK: %[[VAL_15:.*]]:2 = hlfir.declare %[[VAL_2]] typeparams %[[VAL_13]] {uniq_name = ".tmp"} : (!fir.ref<!fir.char<1>>, index) -> (!fir.ref<!fir.char<1>>, !fir.ref<!fir.char<1>>)
41+
// CHECK: hlfir.assign %[[VAL_12]] to %[[VAL_15]]#0 temporary_lhs : !fir.ref<!fir.char<1>>, !fir.ref<!fir.char<1>>
42+
// CHECK: %[[VAL_16:.*]] = fir.undefined tuple<!fir.ref<!fir.char<1>>, i1>
43+
// CHECK: %[[VAL_17:.*]] = fir.insert_value %[[VAL_16]], %[[VAL_14]], [1 : index] : (tuple<!fir.ref<!fir.char<1>>, i1>, i1) -> tuple<!fir.ref<!fir.char<1>>, i1>
44+
// CHECK: %[[VAL_18:.*]] = fir.insert_value %[[VAL_17]], %[[VAL_15]]#0, [0 : index] : (tuple<!fir.ref<!fir.char<1>>, i1>, !fir.ref<!fir.char<1>>) -> tuple<!fir.ref<!fir.char<1>>, i1>
45+
// CHECK: fir.freemem %[[VAL_11]] : !fir.heap<!fir.char<1>>
46+
// CHECK: %[[VAL_19:.*]] = hlfir.designate %[[VAL_7]]#0 (%[[VAL_10]]) typeparams %[[VAL_3]] : (!fir.heap<!fir.array<10x!fir.char<1>>>, index, index) -> !fir.ref<!fir.char<1>>
47+
// CHECK: hlfir.assign %[[VAL_15]]#0 to %[[VAL_19]] temporary_lhs : !fir.ref<!fir.char<1>>, !fir.ref<!fir.char<1>>
48+
// CHECK: }

0 commit comments

Comments
 (0)