Skip to content

Commit 46f46a3

Browse files
clementvalschweitzpgijeanPerier
committed
[flang] Lower basic IO file statements
This patches adds lowering for couple of basic io statements such as `flush`, `endfile`, `backspace` and `rewind` This patch is part of the upstreaming effort from fir-dev branch. Depends on D120821 Reviewed By: schweitz Differential Revision: https://reviews.llvm.org/D120822 Co-authored-by: Eric Schweitz <[email protected]> Co-authored-by: Jean Perier <[email protected]>
1 parent db31da2 commit 46f46a3

File tree

4 files changed

+103
-5
lines changed

4 files changed

+103
-5
lines changed

flang/include/flang/Lower/IO.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ class Value;
1919

2020
namespace Fortran {
2121
namespace parser {
22+
struct BackspaceStmt;
2223
struct CloseStmt;
24+
struct EndfileStmt;
25+
struct FlushStmt;
2326
struct OpenStmt;
2427
struct ReadStmt;
28+
struct RewindStmt;
2529
struct PrintStmt;
30+
struct WaitStmt;
2631
struct WriteStmt;
2732
} // namespace parser
2833

2934
namespace lower {
3035

3136
class AbstractConverter;
3237

38+
/// Generate IO call(s) for BACKSPACE; return the IOSTAT code
39+
mlir::Value genBackspaceStatement(AbstractConverter &,
40+
const parser::BackspaceStmt &);
41+
3342
/// Generate IO call(s) for CLOSE; return the IOSTAT code
3443
mlir::Value genCloseStatement(AbstractConverter &, const parser::CloseStmt &);
3544

45+
/// Generate IO call(s) for ENDFILE; return the IOSTAT code
46+
mlir::Value genEndfileStatement(AbstractConverter &,
47+
const parser::EndfileStmt &);
48+
49+
/// Generate IO call(s) for FLUSH; return the IOSTAT code
50+
mlir::Value genFlushStatement(AbstractConverter &, const parser::FlushStmt &);
51+
3652
/// Generate IO call(s) for READ; return the IOSTAT code
3753
mlir::Value genReadStatement(AbstractConverter &converter,
3854
const parser::ReadStmt &stmt);
@@ -44,6 +60,12 @@ mlir::Value genOpenStatement(AbstractConverter &, const parser::OpenStmt &);
4460
void genPrintStatement(AbstractConverter &converter,
4561
const parser::PrintStmt &stmt);
4662

63+
/// Generate IO call(s) for REWIND; return the IOSTAT code
64+
mlir::Value genRewindStatement(AbstractConverter &, const parser::RewindStmt &);
65+
66+
/// Generate IO call(s) for WAIT; return the IOSTAT code
67+
mlir::Value genWaitStatement(AbstractConverter &, const parser::WaitStmt &);
68+
4769
/// Generate IO call(s) for WRITE; return the IOSTAT code
4870
mlir::Value genWriteStatement(AbstractConverter &converter,
4971
const parser::WriteStmt &stmt);

flang/lib/Lower/Bridge.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,8 @@ class FirConverter : public Fortran::lower::AbstractConverter {
811811
//===--------------------------------------------------------------------===//
812812

813813
void genFIR(const Fortran::parser::BackspaceStmt &stmt) {
814-
TODO(toLocation(), "BackspaceStmt lowering");
814+
mlir::Value iostat = genBackspaceStatement(*this, stmt);
815+
genIoConditionBranches(getEval(), stmt.v, iostat);
815816
}
816817

817818
void genFIR(const Fortran::parser::CloseStmt &stmt) {
@@ -820,11 +821,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
820821
}
821822

822823
void genFIR(const Fortran::parser::EndfileStmt &stmt) {
823-
TODO(toLocation(), "EndfileStmt lowering");
824+
mlir::Value iostat = genEndfileStatement(*this, stmt);
825+
genIoConditionBranches(getEval(), stmt.v, iostat);
824826
}
825827

826828
void genFIR(const Fortran::parser::FlushStmt &stmt) {
827-
TODO(toLocation(), "FlushStmt lowering");
829+
mlir::Value iostat = genFlushStatement(*this, stmt);
830+
genIoConditionBranches(getEval(), stmt.v, iostat);
828831
}
829832

830833
void genFIR(const Fortran::parser::InquireStmt &stmt) {
@@ -846,11 +849,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
846849
}
847850

848851
void genFIR(const Fortran::parser::RewindStmt &stmt) {
849-
TODO(toLocation(), "RewindStmt lowering");
852+
mlir::Value iostat = genRewindStatement(*this, stmt);
853+
genIoConditionBranches(getEval(), stmt.v, iostat);
850854
}
851855

852856
void genFIR(const Fortran::parser::WaitStmt &stmt) {
853-
TODO(toLocation(), "WaitStmt lowering");
857+
mlir::Value iostat = genWaitStatement(*this, stmt);
858+
genIoConditionBranches(getEval(), stmt.v, iostat);
854859
}
855860

856861
void genFIR(const Fortran::parser::WriteStmt &stmt) {

flang/lib/Lower/IO.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,30 @@ static mlir::Value genBasicIOStmt(Fortran::lower::AbstractConverter &converter,
15131513
stmtCtx);
15141514
}
15151515

1516+
mlir::Value Fortran::lower::genBackspaceStatement(
1517+
Fortran::lower::AbstractConverter &converter,
1518+
const Fortran::parser::BackspaceStmt &stmt) {
1519+
return genBasicIOStmt<mkIOKey(BeginBackspace)>(converter, stmt);
1520+
}
1521+
1522+
mlir::Value Fortran::lower::genEndfileStatement(
1523+
Fortran::lower::AbstractConverter &converter,
1524+
const Fortran::parser::EndfileStmt &stmt) {
1525+
return genBasicIOStmt<mkIOKey(BeginEndfile)>(converter, stmt);
1526+
}
1527+
1528+
mlir::Value
1529+
Fortran::lower::genFlushStatement(Fortran::lower::AbstractConverter &converter,
1530+
const Fortran::parser::FlushStmt &stmt) {
1531+
return genBasicIOStmt<mkIOKey(BeginFlush)>(converter, stmt);
1532+
}
1533+
1534+
mlir::Value
1535+
Fortran::lower::genRewindStatement(Fortran::lower::AbstractConverter &converter,
1536+
const Fortran::parser::RewindStmt &stmt) {
1537+
return genBasicIOStmt<mkIOKey(BeginRewind)>(converter, stmt);
1538+
}
1539+
15161540
mlir::Value
15171541
Fortran::lower::genOpenStatement(Fortran::lower::AbstractConverter &converter,
15181542
const Fortran::parser::OpenStmt &stmt) {
@@ -1554,6 +1578,33 @@ Fortran::lower::genCloseStatement(Fortran::lower::AbstractConverter &converter,
15541578
return genBasicIOStmt<mkIOKey(BeginClose)>(converter, stmt);
15551579
}
15561580

1581+
mlir::Value
1582+
Fortran::lower::genWaitStatement(Fortran::lower::AbstractConverter &converter,
1583+
const Fortran::parser::WaitStmt &stmt) {
1584+
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
1585+
Fortran::lower::StatementContext stmtCtx;
1586+
mlir::Location loc = converter.getCurrentLocation();
1587+
bool hasId = hasMem<Fortran::parser::IdExpr>(stmt);
1588+
mlir::FuncOp beginFunc =
1589+
hasId ? getIORuntimeFunc<mkIOKey(BeginWait)>(loc, builder)
1590+
: getIORuntimeFunc<mkIOKey(BeginWaitAll)>(loc, builder);
1591+
mlir::FunctionType beginFuncTy = beginFunc.getType();
1592+
mlir::Value unit = fir::getBase(converter.genExprValue(
1593+
getExpr<Fortran::parser::FileUnitNumber>(stmt), stmtCtx, loc));
1594+
mlir::Value un = builder.createConvert(loc, beginFuncTy.getInput(0), unit);
1595+
llvm::SmallVector<mlir::Value> args{un};
1596+
if (hasId) {
1597+
mlir::Value id = fir::getBase(converter.genExprValue(
1598+
getExpr<Fortran::parser::IdExpr>(stmt), stmtCtx, loc));
1599+
args.push_back(builder.createConvert(loc, beginFuncTy.getInput(1), id));
1600+
}
1601+
auto cookie = builder.create<fir::CallOp>(loc, beginFunc, args).getResult(0);
1602+
ConditionSpecInfo csi;
1603+
genConditionHandlerCall(converter, loc, cookie, stmt.v, csi);
1604+
return genEndIO(converter, converter.getCurrentLocation(), cookie, csi,
1605+
stmtCtx);
1606+
}
1607+
15571608
//===----------------------------------------------------------------------===//
15581609
// Data transfer statements.
15591610
//

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@
1212
! CHECK: call {{.*}}EndIoStatement
1313
open(8, file="foo", access="sequential")
1414

15+
! CHECK: call {{.*}}BeginBackspace
16+
! CHECK: call {{.*}}EndIoStatement
17+
backspace(8)
18+
19+
! CHECK: call {{.*}}BeginFlush
20+
! CHECK: call {{.*}}EndIoStatement
21+
flush(8)
22+
23+
! CHECK: call {{.*}}BeginRewind
24+
! CHECK: call {{.*}}EndIoStatement
25+
rewind(8)
26+
27+
! CHECK: call {{.*}}BeginEndfile
28+
! CHECK: call {{.*}}EndIoStatement
29+
endfile(8)
30+
31+
! CHECK: call {{.*}}BeginWaitAll
32+
! CHECK: call {{.*}}EndIoStatement
33+
wait(unit=8)
34+
1535
! CHECK: call {{.*}}BeginExternalListInput
1636
! CHECK: call {{.*}}InputInteger
1737
! CHECK: call {{.*}}InputReal32

0 commit comments

Comments
 (0)