diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp index 14642146df015..505a2d7486288 100644 --- a/flang/lib/Lower/IO.cpp +++ b/flang/lib/Lower/IO.cpp @@ -1989,7 +1989,6 @@ genDataTransferStmt(Fortran::lower::AbstractConverter &converter, csi.hasTransferConditionSpec(), ok, /*inLoop=*/false); } - stmtCtx.finalize(); builder.restoreInsertionPoint(insertPt); if constexpr (hasIOCtrl) { @@ -1997,7 +1996,9 @@ genDataTransferStmt(Fortran::lower::AbstractConverter &converter, csi.hasErrorConditionSpec()); } // Generate end statement call/s. - return genEndIO(converter, loc, cookie, csi, stmtCtx); + mlir::Value result = genEndIO(converter, loc, cookie, csi, stmtCtx); + stmtCtx.finalize(); + return result; } void Fortran::lower::genPrintStatement( diff --git a/flang/test/Lower/io-write.f90 b/flang/test/Lower/io-write.f90 new file mode 100644 index 0000000000000..fa1424e710c51 --- /dev/null +++ b/flang/test/Lower/io-write.f90 @@ -0,0 +1,42 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! Test that IO item calls stackrestore in the right place + +! CHECK-LABEL: func.func @_QQmain() { + character(3) string + write(string,getstring(6)) +! CHECK: %[[Val_0:.*]] = fir.alloca i32 {adapt.valuebyref} +! CHECK: %[[Val_1:.*]] = fir.address_of(@_QFEstring) : !fir.ref> +! CHECK: %[[Const_3:.*]] = arith.constant 3 : index +! CHECK: %[[Val_2:.*]] = fir.convert %[[Val_1]] : (!fir.ref>) -> !fir.ref +! CHECK: %[[Val_3:.*]] = fir.convert %[[Const_3]] : (index) -> i64 +! CHECK: %[[Const_6:.*]] = arith.constant 6 : i32 +! CHECK: fir.store %[[Const_6]] to %[[Val_0]] : !fir.ref +! CHECK: %[[Val_4:.*]] = fir.load %[[Val_0]] : !fir.ref +! CHECK: %[[Val_5:.*]] = fir.convert %[[Val_4]] : (i32) -> i64 +! CHECK: %[[Val_6:.*]] = fir.convert %[[Val_5]] : (i64) -> index +! CHECK: %[[Const_0:.*]] = arith.constant 0 : index +! CHECK: %[[Val_7:.*]] = arith.cmpi sgt, %[[Val_6]], %[[Const_0]] : index +! CHECK: %[[Val_8:.*]] = arith.select %[[Val_7]], %[[Val_6]], %[[Const_0]] : index +! CHECK: %[[Val_9:.*]] = fir.call @llvm.stacksave() : () -> !fir.ref +! CHECK: %[[Val_10:.*]] = fir.alloca !fir.char<1,?>(%[[Val_8]] : index) {bindc_name = ".result"} +! CHECK: %[[Val_11:.*]] = fir.call @_QFPgetstring(%[[Val_10]], %[[Val_8]], %[[Val_0]]) : (!fir.ref>, index, !fir.ref) -> !fir.boxchar<1> +! CHECK: %[[Val_12:.*]] = fir.convert %[[Val_10]] : (!fir.ref>) -> !fir.ref +! CHECK: %[[Val_13:.*]] = fir.convert %[[Val_8]] : (index) -> i64 +! CHECK: %[[Val_14:.*]] = fir.zero_bits !fir.box +! CHECK: %[[Const_0_i64:.*]] = arith.constant 0 : i64 +! CHECK: %[[Val_15:.*]] = fir.convert %[[Const_0_i64]] : (i64) -> !fir.ref> +! CHECK: %[[Const_0_i64_0:.*]] = arith.constant 0 : i64 +! CHECK: %[[Val_16:.*]] = fir.address_of(@_QQcl.{{.*}}) : !fir.ref> +! CHECK: %[[Val_17:.*]] = fir.convert %[[Val_16]] : (!fir.ref>) -> !fir.ref +! CHECK: %[[Val_18:.*]] = fir.call @_FortranAioBeginInternalFormattedOutput(%[[Val_2]], %[[Val_3]], %[[Val_12]], %[[Val_13]], +! %[[Val_14]], %[[Val_15]], %[[Const_0_i64_0]], %17, %{{.*}}) : (!fir.ref, i64, !fir.ref, i64, !fir.box, !fir.ref>, i64, !fir.ref, i32) -> !fir.ref +! CHECK: %[[Val_19:.*]] = fir.call @_FortranAioEndIoStatement(%18) : (!fir.ref) -> i32 +! CHECK: fir.call @llvm.stackrestore(%[[Val_9]]) : (!fir.ref) -> () + if (string/="hi") stop 'FAIL' +contains + function getstring(n) result(r) + character(n) r + r = '("hi")' + end function +end