Skip to content

Commit 10552f2

Browse files
authored
Check the results of tightly nested IO loops (#1123)
Such as: write(*,*,iostat=m) ((aa(j,k),j=1,3),k=1,3)
1 parent e749021 commit 10552f2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

flang/lib/Lower/IO.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,9 +657,14 @@ static void genIoLoop(Fortran::lower::AbstractConverter &converter,
657657
auto ifOp = dyn_cast<fir::IfOp>(op);
658658
auto *lastOp = &ifOp.thenRegion().front().back();
659659
builder.setInsertionPointAfter(lastOp);
660-
builder.create<fir::ResultOp>(loc, lastOp->getResult(0)); // runtime result
660+
// The primary ifOp result is the result of an IO call or loop.
661+
if (mlir::isa<fir::CallOp, fir::IfOp>(*lastOp))
662+
builder.create<fir::ResultOp>(loc, lastOp->getResult(0));
663+
else
664+
builder.create<fir::ResultOp>(loc, ok); // loop result
665+
// The else branch propagates an early exit false result.
661666
builder.setInsertionPointToStart(&ifOp.elseRegion().front());
662-
builder.create<fir::ResultOp>(loc, falseValue); // known false result
667+
builder.create<fir::ResultOp>(loc, falseValue);
663668
}
664669
builder.setInsertionPointToEnd(iterWhileOp.getBody());
665670
auto iterateResult = builder.getBlock()->back().getResult(0);

flang/test/Lower/io-statement-2.f90

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,33 @@ subroutine control2() ! I/O condition specifier control flow (use index result)
106106
write(*,'(8F4.1,I5)',iostat=m) (c,d,j=11,14), j
107107
end
108108

109+
! CHECK-LABEL: func @_QPloopnest
110+
subroutine loopnest
111+
integer :: aa(3,3)
112+
aa = 10
113+
! CHECK: BeginExternalListOutput
114+
! CHECK: EnableHandlers
115+
! CHECK: {{.*}}:2 = fir.iterate_while ({{.*}} = {{.*}} to {{.*}} step {{.*}}) and ({{.*}} = {{.*}}) -> (index, i1) {
116+
! CHECK: fir.if {{.*}} -> (i1) {
117+
! CHECK: {{.*}}:2 = fir.iterate_while ({{.*}} = {{.*}} to {{.*}} step {{.*}}) and ({{.*}} = {{.*}}) -> (index, i1) {
118+
! CHECK: fir.if {{.*}} -> (i1) {
119+
! CHECK: OutputInteger64
120+
! CHECK: fir.result {{.*}} : i1
121+
! CHECK: } else {
122+
! CHECK: fir.result {{.*}} : i1
123+
! CHECK: }
124+
! CHECK: fir.result {{.*}}, {{.*}} : index, i1
125+
! CHECK: }
126+
! CHECK: fir.result {{.*}}#1 : i1
127+
! CHECK: } else {
128+
! CHECK: fir.result {{.*}} : i1
129+
! CHECK: }
130+
! CHECK: fir.result {{.*}}, {{.*}} : index, i1
131+
! CHECK: }
132+
! CHECK: EndIoStatement
133+
write(*,*,err=66) ((aa(j,k)+j+k,j=1,3),k=1,3)
134+
66 continue
135+
end
109136

110137
! CHECK-LABEL: func @_QPimpliedformat
111138
subroutine impliedformat

0 commit comments

Comments
 (0)