Skip to content

Commit 837d45b

Browse files
committed
Keep the copy-out to deallocate the temporary
Signed-off-by: Kajetan Puchalski <[email protected]>
1 parent 63f66ae commit 837d45b

File tree

2 files changed

+8
-18
lines changed

2 files changed

+8
-18
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,15 @@ InlineCopyInConversion::matchAndRewrite(hlfir::CopyInOp copyIn,
139139
})
140140
.getResults();
141141

142-
mlir::OpResult addr = results[0];
142+
mlir::OpResult resultBox = results[0];
143143
mlir::OpResult needsCleanup = results[1];
144144

145-
builder.setInsertionPoint(copyOut);
146-
builder.genIfOp(loc, {}, needsCleanup, /*withElseRegion=*/false).genThen([&] {
147-
auto boxAddr = builder.create<fir::BoxAddrOp>(loc, addr);
148-
fir::HeapType heapType =
149-
fir::HeapType::get(fir::BoxValue(addr).getBaseTy());
150-
mlir::Value heapVal =
151-
builder.createConvert(loc, heapType, boxAddr.getResult());
152-
builder.create<fir::FreeMemOp>(loc, heapVal);
153-
});
154-
rewriter.eraseOp(copyOut);
155-
156-
rewriter.replaceOp(copyIn, {addr, builder.genNot(loc, isContiguous)});
145+
auto alloca = builder.create<fir::AllocaOp>(loc, resultBox.getType());
146+
auto store = builder.create<fir::StoreOp>(loc, resultBox, alloca);
147+
copyOut->setOperand(0, store.getMemref());
148+
copyOut->setOperand(1, needsCleanup);
149+
150+
rewriter.replaceOp(copyIn, {resultBox, builder.genNot(loc, isContiguous)});
157151
return mlir::success();
158152
}
159153

flang/test/HLFIR/inline-hlfir-copy-in.fir

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,7 @@ func.func private @_test_inline_copy_in(%arg0: !fir.box<!fir.array<?x?x?xf64>> {
7373
// CHECK: %[[VAL_22:.*]] = fir.box_addr %[[VAL_21:.*]]#0 : (!fir.box<!fir.array<?xf64>>) -> !fir.ref<!fir.array<?xf64>>
7474
// CHECK: %[[VAL_23:.*]]:3 = hlfir.associate %[[VAL_5:.*]] {adapt.valuebyref} : (i32) -> (!fir.ref<i32>, !fir.ref<i32>, i1)
7575
// CHECK: fir.call @_QFPsb(%[[VAL_22:.*]], %[[VAL_23:.*]]#0) fastmath<contract> : (!fir.ref<!fir.array<?xf64>>, !fir.ref<i32>) -> ()
76-
// CHECK: fir.if %[[VAL_21:.*]]#1 {
77-
// CHECK: %[[VAL_24:.*]] = fir.box_addr %[[VAL_21:.*]]#0 : (!fir.box<!fir.array<?xf64>>) -> !fir.ref<!fir.array<?xf64>>
78-
// CHECK: %[[VAL_25:.*]] = fir.convert %[[VAL_24:.*]] : (!fir.ref<!fir.array<?xf64>>) -> !fir.heap<!fir.array<?xf64>>
79-
// CHECK: fir.freemem %[[VAL_25:.*]] : !fir.heap<!fir.array<?xf64>>
80-
// CHECK: }
76+
// CHECK: hlfir.copy_out %16, %15#1 : (!fir.ref<!fir.box<!fir.array<?xf64>>>, i1) -> ()
8177
// CHECK: hlfir.end_associate %[[VAL_23:.*]]#1, %[[VAL_23:.*]]#2 : !fir.ref<i32>, i1
8278
// CHECK: return
8379
// CHECK: }

0 commit comments

Comments
 (0)