Reland "[flang] Set LLVM specific attributes to fir.call's of Fortran runtime. (#128093)"

This change is inspired by a case in facerec benchmark, where
performance
of scalar code may improve by about 6%@aarch64 due to getting rid of
redundant
loads from Fortran descriptors. These descriptors are corresponding
to subroutine local ALLOCATABLE, SAVE variables. The scalar loop nest
in LocalMove subroutine contains call to Fortran runtime IO functions,
and LLVM globals-aa analysis cannot prove that these calls do not modify
the globalized descriptors with internal linkage.

This patch sets and propagates llvm.memory_effects attribute for
fir.call
operations calling Fortran runtime functions. In particular, it tries
to set the Other memory effect to NoModRef. The Other memory effect
includes accesses to globals and captured pointers, so we cannot set
it for functions taking Fortran descriptors with one exception
for calls where the Fortran descriptor arguments are all null.

As long as different calls to the same Fortran runtime function may have
different attributes, I decided to attach the attributes to the calls
rather than functions. Moreover, attaching the attributes to func.func
will require propagating these attributes to llvm.func, which is not
happening right now.

In addition to llvm.memory_effects, the new pass sets llvm.nosync
and llvm.nocallback attributes that may also help LLVM alias analysis
(e.g. see #127707). These attributes are ignored currently.
I will support them in LLVM IR dialect in a separate patch.

I also added another pass for developers to be able to print
declarations/calls of all Fortran runtime functions that are recognized
by the attributes setting pass. It should help with maintenance
of the LIT tests.
This commit is contained in:
Slava Zakharin 2025-02-24 09:27:48 -08:00
parent d6ec32c8f2
commit 0caa8f42be
19 changed files with 1977 additions and 162 deletions

View File

@ -385,6 +385,15 @@ public:
mlir::FunctionType ty,
mlir::SymbolTable *);
/// Returns a named function for a Fortran runtime API, creating
/// it, if it does not exist in the module yet.
/// If \p isIO is set to true, then the function corresponds
/// to one of Fortran runtime IO APIs.
mlir::func::FuncOp createRuntimeFunction(mlir::Location loc,
llvm::StringRef name,
mlir::FunctionType ty,
bool isIO = false);
/// Cast the input value to IndexType.
mlir::Value convertToIndexType(mlir::Location loc, mlir::Value val) {
return createConvert(loc, getIndexType(), val);

View File

@ -21,6 +21,7 @@
#include "flang/Optimizer/Builder/FIRBuilder.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Runtime/io-api-consts.h"
#include "flang/Runtime/reduce.h"
#include "flang/Support/Fortran.h"
#include "mlir/IR/BuiltinTypes.h"
@ -586,6 +587,33 @@ constexpr TypeBuilderFunc getModel<void>() {
};
}
// Define additional runtime type models specific to IO.
template <>
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
return getModel<char *>();
}
template <>
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context,
8 * sizeof(Fortran::runtime::io::Iostat));
};
}
template <>
constexpr TypeBuilderFunc
getModel<const Fortran::runtime::io::NamelistGroup &>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return fir::ReferenceType::get(mlir::TupleType::get(context));
};
}
template <>
constexpr TypeBuilderFunc
getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return fir::ReferenceType::get(mlir::TupleType::get(context));
};
}
REDUCTION_REF_OPERATION_MODEL(std::int8_t)
REDUCTION_VALUE_OPERATION_MODEL(std::int8_t)
REDUCTION_REF_OPERATION_MODEL(std::int16_t)
@ -778,16 +806,22 @@ struct RuntimeTableEntry<RuntimeTableKey<KT>, RuntimeIdentifier<Cs...>> {
/// argument is intended to be of the form: <mkRTKey(runtime function name)>.
template <typename RuntimeEntry>
static mlir::func::FuncOp getRuntimeFunc(mlir::Location loc,
fir::FirOpBuilder &builder) {
fir::FirOpBuilder &builder,
bool isIO = false) {
using namespace Fortran::runtime;
auto name = RuntimeEntry::name;
auto func = builder.getNamedFunction(name);
if (func)
return func;
auto funTy = RuntimeEntry::getTypeModel()(builder.getContext());
func = builder.createFunction(loc, name, funTy);
func->setAttr(FIROpsDialect::getFirRuntimeAttrName(), builder.getUnitAttr());
return func;
return builder.createRuntimeFunction(loc, name, funTy, isIO);
}
/// Get (or generate) the MLIR FuncOp for a given IO runtime function.
template <typename E>
static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
fir::FirOpBuilder &builder) {
return getRuntimeFunc<E>(loc, builder, /*isIO=*/true);
}
namespace helper {

View File

@ -56,6 +56,18 @@ def FIROpsDialect : Dialect {
static constexpr llvm::StringRef getFirRuntimeAttrName() {
return "fir.runtime";
}
// Return string name of fir.memory attributes.
// It is attached to fir.call operations to convey
// llvm.memory attributes to LLVM IR.
// Its value is intended to be mlir::LLVM::MemoryEffectsAttr.
// TODO: we should probably make it an inherent attribute
// of fir.call, though, it is supposed to be a short-lived
// attribute that appears right before CodeGen and only
// meaningful for LLVM, so it is unclear if embedding
// it into fir.call makes sense.
static constexpr llvm::StringRef getFirCallMemoryAttrName() {
return "fir.llvm_memory";
}
}];
}

View File

@ -60,6 +60,8 @@ namespace fir {
#define GEN_PASS_DECL_FUNCTIONATTR
#define GEN_PASS_DECL_CONSTANTARGUMENTGLOBALISATIONOPT
#define GEN_PASS_DECL_COMPILERGENERATEDNAMESCONVERSION
#define GEN_PASS_DECL_SETRUNTIMECALLATTRIBUTES
#define GEN_PASS_DECL_GENRUNTIMECALLSFORTEST
#include "flang/Optimizer/Transforms/Passes.h.inc"

View File

@ -453,4 +453,37 @@ def CUFGPUToLLVMConversion : Pass<"cuf-gpu-convert-to-llvm", "mlir::ModuleOp"> {
];
}
def SetRuntimeCallAttributes
: Pass<"set-runtime-call-attrs", "mlir::func::FuncOp"> {
let summary = "Set Fortran runtime fir.call attributes targeting LLVM IR";
let description = [{
This pass sets different attributes for Fortran runtime calls
that enable more optimizations in LLVM backend.
For the time being, the meaning of these attributes is not
strictly defined for HLFIR/FIR.
}];
let dependentDialects = ["fir::FIROpsDialect", "mlir::LLVM::LLVMDialect"];
}
def GenRuntimeCallsForTest
: Pass<"gen-runtime-calls-for-test", "mlir::ModuleOp"> {
let summary =
"Print FIR containing declarations/calls of Fortran runtime functions";
let description = [{
This pass is only for developers to be able to print FIR
that declares and calls Fortran runtime functions.
It helps producing/updating tests for passes that modify
the func/call operations based on some knowledge of
Fortran runtime.
}];
let options =
[Option<"doGenerateCalls", "do-generate-calls", "bool",
/*default=*/"false",
"Generate thin wrapper functions that call Fortran runtime "
"functions. If it is set to false, then only the declarations "
"are generated.">,
];
let dependentDialects = ["fir::FIROpsDialect", "mlir::func::FuncDialect"];
}
#endif // FLANG_OPTIMIZER_TRANSFORMS_PASSES

View File

@ -0,0 +1,111 @@
//===-- Optimizer/Transforms/RuntimeFunctions.inc ---------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef KNOWN_IO_FUNC
#error "Define KNOWN_IO_FUNC before including this file"
#endif
#ifndef KNOWN_RUNTIME_FUNC
#error "Define KNOWN_RUNTIME_FUNC before including this file"
#endif
// Fortran runtime functions that SetRuntimeCallAttributesPass recognizes.
// WARNING: if you add a function entry here, you must make sure
// that the attribute computation callbacks that end up being
// used are correct for this function. If needed, add
// specializations for the types that provide attribute
// computation callbacks in SetRuntimeCallAttributesPass.
// clang-format off
KNOWN_IO_FUNC(BeginBackspace),
KNOWN_IO_FUNC(BeginClose),
KNOWN_IO_FUNC(BeginEndfile),
KNOWN_IO_FUNC(BeginExternalFormattedInput),
KNOWN_IO_FUNC(BeginExternalFormattedOutput),
KNOWN_IO_FUNC(BeginExternalListInput),
KNOWN_IO_FUNC(BeginExternalListOutput),
KNOWN_IO_FUNC(BeginFlush),
KNOWN_IO_FUNC(BeginInquireFile),
KNOWN_IO_FUNC(BeginInquireIoLength),
KNOWN_IO_FUNC(BeginInquireUnit),
KNOWN_IO_FUNC(BeginInternalArrayFormattedInput),
KNOWN_IO_FUNC(BeginInternalArrayFormattedOutput),
KNOWN_IO_FUNC(BeginInternalArrayListInput),
KNOWN_IO_FUNC(BeginInternalArrayListOutput),
KNOWN_IO_FUNC(BeginInternalFormattedInput),
KNOWN_IO_FUNC(BeginInternalFormattedOutput),
KNOWN_IO_FUNC(BeginInternalListInput),
KNOWN_IO_FUNC(BeginInternalListOutput),
KNOWN_IO_FUNC(BeginOpenNewUnit),
KNOWN_IO_FUNC(BeginOpenUnit),
KNOWN_IO_FUNC(BeginRewind),
KNOWN_IO_FUNC(BeginUnformattedInput),
KNOWN_IO_FUNC(BeginUnformattedOutput),
KNOWN_IO_FUNC(BeginWait),
KNOWN_IO_FUNC(BeginWaitAll),
KNOWN_IO_FUNC(CheckUnitNumberInRange128),
KNOWN_IO_FUNC(CheckUnitNumberInRange64),
KNOWN_IO_FUNC(EnableHandlers),
KNOWN_IO_FUNC(EndIoStatement),
KNOWN_IO_FUNC(GetAsynchronousId),
KNOWN_IO_FUNC(GetIoLength),
KNOWN_IO_FUNC(GetIoMsg),
KNOWN_IO_FUNC(GetNewUnit),
KNOWN_IO_FUNC(GetSize),
KNOWN_IO_FUNC(InputAscii),
KNOWN_IO_FUNC(InputComplex32),
KNOWN_IO_FUNC(InputComplex64),
KNOWN_IO_FUNC(InputDerivedType),
KNOWN_IO_FUNC(InputDescriptor),
KNOWN_IO_FUNC(InputInteger),
KNOWN_IO_FUNC(InputLogical),
KNOWN_IO_FUNC(InputNamelist),
KNOWN_IO_FUNC(InputReal32),
KNOWN_IO_FUNC(InputReal64),
KNOWN_IO_FUNC(InquireCharacter),
KNOWN_IO_FUNC(InquireInteger64),
KNOWN_IO_FUNC(InquireLogical),
KNOWN_IO_FUNC(InquirePendingId),
KNOWN_IO_FUNC(OutputAscii),
KNOWN_IO_FUNC(OutputComplex32),
KNOWN_IO_FUNC(OutputComplex64),
KNOWN_IO_FUNC(OutputDerivedType),
KNOWN_IO_FUNC(OutputDescriptor),
KNOWN_IO_FUNC(OutputInteger128),
KNOWN_IO_FUNC(OutputInteger16),
KNOWN_IO_FUNC(OutputInteger32),
KNOWN_IO_FUNC(OutputInteger64),
KNOWN_IO_FUNC(OutputInteger8),
KNOWN_IO_FUNC(OutputLogical),
KNOWN_IO_FUNC(OutputNamelist),
KNOWN_IO_FUNC(OutputReal32),
KNOWN_IO_FUNC(OutputReal64),
KNOWN_IO_FUNC(SetAccess),
KNOWN_IO_FUNC(SetAction),
KNOWN_IO_FUNC(SetAdvance),
KNOWN_IO_FUNC(SetAsynchronous),
KNOWN_IO_FUNC(SetBlank),
KNOWN_IO_FUNC(SetCarriagecontrol),
KNOWN_IO_FUNC(SetConvert),
KNOWN_IO_FUNC(SetDecimal),
KNOWN_IO_FUNC(SetDelim),
KNOWN_IO_FUNC(SetEncoding),
KNOWN_IO_FUNC(SetFile),
KNOWN_IO_FUNC(SetForm),
KNOWN_IO_FUNC(SetPad),
KNOWN_IO_FUNC(SetPos),
KNOWN_IO_FUNC(SetPosition),
KNOWN_IO_FUNC(SetRec),
KNOWN_IO_FUNC(SetRecl),
KNOWN_IO_FUNC(SetRound),
KNOWN_IO_FUNC(SetSign),
KNOWN_IO_FUNC(SetStatus)
// clang-format on
#undef KNOWN_IO_FUNC
#undef KNOWN_RUNTIME_FUNC

View File

@ -43,35 +43,6 @@
#define DEBUG_TYPE "flang-lower-io"
// Define additional runtime type models specific to IO.
namespace fir::runtime {
template <>
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::IoStatementState *>() {
return getModel<char *>();
}
template <>
constexpr TypeBuilderFunc getModel<Fortran::runtime::io::Iostat>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return mlir::IntegerType::get(context,
8 * sizeof(Fortran::runtime::io::Iostat));
};
}
template <>
constexpr TypeBuilderFunc
getModel<const Fortran::runtime::io::NamelistGroup &>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return fir::ReferenceType::get(mlir::TupleType::get(context));
};
}
template <>
constexpr TypeBuilderFunc
getModel<const Fortran::runtime::io::NonTbpDefinedIoTable *>() {
return [](mlir::MLIRContext *context) -> mlir::Type {
return fir::ReferenceType::get(mlir::TupleType::get(context));
};
}
} // namespace fir::runtime
using namespace Fortran::runtime::io;
#define mkIOKey(X) FirmkKey(IONAME(X))
@ -172,22 +143,6 @@ inline int64_t getLength(mlir::Type argTy) {
return mlir::cast<fir::SequenceType>(argTy).getShape()[0];
}
/// Get (or generate) the MLIR FuncOp for a given IO runtime function.
template <typename E>
static mlir::func::FuncOp getIORuntimeFunc(mlir::Location loc,
fir::FirOpBuilder &builder) {
llvm::StringRef name = getName<E>();
mlir::func::FuncOp func = builder.getNamedFunction(name);
if (func)
return func;
auto funTy = getTypeModel<E>()(builder.getContext());
func = builder.createFunction(loc, name, funTy);
func->setAttr(fir::FIROpsDialect::getFirRuntimeAttrName(),
builder.getUnitAttr());
func->setAttr("fir.io", builder.getUnitAttr());
return func;
}
/// Generate calls to end an IO statement. Return the IOSTAT value, if any.
/// It is the caller's responsibility to generate branches on that value.
static mlir::Value genEndIO(Fortran::lower::AbstractConverter &converter,
@ -197,7 +152,7 @@ static mlir::Value genEndIO(Fortran::lower::AbstractConverter &converter,
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
if (csi.ioMsg) {
mlir::func::FuncOp getIoMsg =
getIORuntimeFunc<mkIOKey(GetIoMsg)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(GetIoMsg)>(loc, builder);
builder.create<fir::CallOp>(
loc, getIoMsg,
mlir::ValueRange{
@ -208,7 +163,7 @@ static mlir::Value genEndIO(Fortran::lower::AbstractConverter &converter,
fir::getLen(*csi.ioMsg))});
}
mlir::func::FuncOp endIoStatement =
getIORuntimeFunc<mkIOKey(EndIoStatement)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(EndIoStatement)>(loc, builder);
auto call = builder.create<fir::CallOp>(loc, endIoStatement,
mlir::ValueRange{cookie});
mlir::Value iostat = call.getResult(0);
@ -659,45 +614,57 @@ static mlir::func::FuncOp getOutputFunc(mlir::Location loc,
fir::FirOpBuilder &builder,
mlir::Type type, bool isFormatted) {
if (mlir::isa<fir::RecordType>(fir::unwrapPassByRefType(type)))
return getIORuntimeFunc<mkIOKey(OutputDerivedType)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputDerivedType)>(loc,
builder);
if (!isFormatted)
return getIORuntimeFunc<mkIOKey(OutputDescriptor)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputDescriptor)>(loc,
builder);
if (auto ty = mlir::dyn_cast<mlir::IntegerType>(type)) {
if (!ty.isUnsigned()) {
switch (ty.getWidth()) {
case 1:
return getIORuntimeFunc<mkIOKey(OutputLogical)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputLogical)>(loc,
builder);
case 8:
return getIORuntimeFunc<mkIOKey(OutputInteger8)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputInteger8)>(loc,
builder);
case 16:
return getIORuntimeFunc<mkIOKey(OutputInteger16)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputInteger16)>(
loc, builder);
case 32:
return getIORuntimeFunc<mkIOKey(OutputInteger32)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputInteger32)>(
loc, builder);
case 64:
return getIORuntimeFunc<mkIOKey(OutputInteger64)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputInteger64)>(
loc, builder);
case 128:
return getIORuntimeFunc<mkIOKey(OutputInteger128)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputInteger128)>(
loc, builder);
}
llvm_unreachable("unknown OutputInteger kind");
}
}
if (auto ty = mlir::dyn_cast<mlir::FloatType>(type)) {
if (auto width = ty.getWidth(); width == 32)
return getIORuntimeFunc<mkIOKey(OutputReal32)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputReal32)>(loc,
builder);
else if (width == 64)
return getIORuntimeFunc<mkIOKey(OutputReal64)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputReal64)>(loc,
builder);
}
auto kindMap = fir::getKindMapping(builder.getModule());
if (auto ty = mlir::dyn_cast<mlir::ComplexType>(type)) {
// COMPLEX(KIND=k) corresponds to a pair of REAL(KIND=k).
auto width = mlir::cast<mlir::FloatType>(ty.getElementType()).getWidth();
if (width == 32)
return getIORuntimeFunc<mkIOKey(OutputComplex32)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputComplex32)>(loc,
builder);
else if (width == 64)
return getIORuntimeFunc<mkIOKey(OutputComplex64)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputComplex64)>(loc,
builder);
}
if (mlir::isa<fir::LogicalType>(type))
return getIORuntimeFunc<mkIOKey(OutputLogical)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputLogical)>(loc, builder);
if (fir::factory::CharacterExprHelper::isCharacterScalar(type)) {
// TODO: What would it mean if the default CHARACTER KIND is set to a wide
// character encoding scheme? How do we handle UTF-8? Is it a distinct KIND
@ -706,9 +673,10 @@ static mlir::func::FuncOp getOutputFunc(mlir::Location loc,
auto asciiKind = kindMap.defaultCharacterKind();
if (kindMap.getCharacterBitsize(asciiKind) == 8 &&
fir::factory::CharacterExprHelper::getCharacterKind(type) == asciiKind)
return getIORuntimeFunc<mkIOKey(OutputAscii)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputAscii)>(loc, builder);
}
return getIORuntimeFunc<mkIOKey(OutputDescriptor)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(OutputDescriptor)>(loc,
builder);
}
/// Generate a sequence of output data transfer calls.
@ -778,39 +746,46 @@ static mlir::func::FuncOp getInputFunc(mlir::Location loc,
fir::FirOpBuilder &builder,
mlir::Type type, bool isFormatted) {
if (mlir::isa<fir::RecordType>(fir::unwrapPassByRefType(type)))
return getIORuntimeFunc<mkIOKey(InputDerivedType)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputDerivedType)>(loc,
builder);
if (!isFormatted)
return getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc,
builder);
if (auto ty = mlir::dyn_cast<mlir::IntegerType>(type)) {
if (type.isUnsignedInteger())
return getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc,
builder);
return ty.getWidth() == 1
? getIORuntimeFunc<mkIOKey(InputLogical)>(loc, builder)
: getIORuntimeFunc<mkIOKey(InputInteger)>(loc, builder);
? fir::runtime::getIORuntimeFunc<mkIOKey(InputLogical)>(loc,
builder)
: fir::runtime::getIORuntimeFunc<mkIOKey(InputInteger)>(loc,
builder);
}
if (auto ty = mlir::dyn_cast<mlir::FloatType>(type)) {
if (auto width = ty.getWidth(); width == 32)
return getIORuntimeFunc<mkIOKey(InputReal32)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputReal32)>(loc, builder);
else if (width == 64)
return getIORuntimeFunc<mkIOKey(InputReal64)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputReal64)>(loc, builder);
}
auto kindMap = fir::getKindMapping(builder.getModule());
if (auto ty = mlir::dyn_cast<mlir::ComplexType>(type)) {
auto width = mlir::cast<mlir::FloatType>(ty.getElementType()).getWidth();
if (width == 32)
return getIORuntimeFunc<mkIOKey(InputComplex32)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputComplex32)>(loc,
builder);
else if (width == 64)
return getIORuntimeFunc<mkIOKey(InputComplex64)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputComplex64)>(loc,
builder);
}
if (mlir::isa<fir::LogicalType>(type))
return getIORuntimeFunc<mkIOKey(InputLogical)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputLogical)>(loc, builder);
if (fir::factory::CharacterExprHelper::isCharacterScalar(type)) {
auto asciiKind = kindMap.defaultCharacterKind();
if (kindMap.getCharacterBitsize(asciiKind) == 8 &&
fir::factory::CharacterExprHelper::getCharacterKind(type) == asciiKind)
return getIORuntimeFunc<mkIOKey(InputAscii)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputAscii)>(loc, builder);
}
return getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(InputDescriptor)>(loc, builder);
}
/// Interpret the lowest byte of a LOGICAL and store that value into the full
@ -1145,7 +1120,7 @@ mlir::Value genIntIOOption(Fortran::lower::AbstractConverter &converter,
const B &spec) {
Fortran::lower::StatementContext localStatementCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp ioFunc = getIORuntimeFunc<A>(loc, builder);
mlir::func::FuncOp ioFunc = fir::runtime::getIORuntimeFunc<A>(loc, builder);
mlir::FunctionType ioFuncTy = ioFunc.getFunctionType();
mlir::Value expr = fir::getBase(converter.genExprValue(
loc, Fortran::semantics::GetExpr(spec.v), localStatementCtx));
@ -1162,7 +1137,7 @@ mlir::Value genCharIOOption(Fortran::lower::AbstractConverter &converter,
const B &spec) {
Fortran::lower::StatementContext localStatementCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp ioFunc = getIORuntimeFunc<A>(loc, builder);
mlir::func::FuncOp ioFunc = fir::runtime::getIORuntimeFunc<A>(loc, builder);
mlir::FunctionType ioFuncTy = ioFunc.getFunctionType();
std::tuple<mlir::Value, mlir::Value, mlir::Value> tup =
lowerStringLit(converter, loc, localStatementCtx, spec,
@ -1194,7 +1169,8 @@ mlir::Value genIOOption<Fortran::parser::FileNameExpr>(
Fortran::lower::StatementContext localStatementCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
// has an extra KIND argument
mlir::func::FuncOp ioFunc = getIORuntimeFunc<mkIOKey(SetFile)>(loc, builder);
mlir::func::FuncOp ioFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(SetFile)>(loc, builder);
mlir::FunctionType ioFuncTy = ioFunc.getFunctionType();
std::tuple<mlir::Value, mlir::Value, mlir::Value> tup =
lowerStringLit(converter, loc, localStatementCtx, spec,
@ -1212,46 +1188,48 @@ mlir::Value genIOOption<Fortran::parser::ConnectSpec::CharExpr>(
mlir::func::FuncOp ioFunc;
switch (std::get<Fortran::parser::ConnectSpec::CharExpr::Kind>(spec.t)) {
case Fortran::parser::ConnectSpec::CharExpr::Kind::Access:
ioFunc = getIORuntimeFunc<mkIOKey(SetAccess)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetAccess)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Action:
ioFunc = getIORuntimeFunc<mkIOKey(SetAction)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetAction)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Asynchronous:
ioFunc = getIORuntimeFunc<mkIOKey(SetAsynchronous)>(loc, builder);
ioFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(SetAsynchronous)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Blank:
ioFunc = getIORuntimeFunc<mkIOKey(SetBlank)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetBlank)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Decimal:
ioFunc = getIORuntimeFunc<mkIOKey(SetDecimal)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetDecimal)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Delim:
ioFunc = getIORuntimeFunc<mkIOKey(SetDelim)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetDelim)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Encoding:
ioFunc = getIORuntimeFunc<mkIOKey(SetEncoding)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetEncoding)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Form:
ioFunc = getIORuntimeFunc<mkIOKey(SetForm)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetForm)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Pad:
ioFunc = getIORuntimeFunc<mkIOKey(SetPad)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetPad)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Position:
ioFunc = getIORuntimeFunc<mkIOKey(SetPosition)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetPosition)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Round:
ioFunc = getIORuntimeFunc<mkIOKey(SetRound)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetRound)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Sign:
ioFunc = getIORuntimeFunc<mkIOKey(SetSign)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetSign)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Carriagecontrol:
ioFunc = getIORuntimeFunc<mkIOKey(SetCarriagecontrol)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetCarriagecontrol)>(
loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Convert:
ioFunc = getIORuntimeFunc<mkIOKey(SetConvert)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetConvert)>(loc, builder);
break;
case Fortran::parser::ConnectSpec::CharExpr::Kind::Dispose:
TODO(loc, "DISPOSE not part of the runtime::io interface");
@ -1289,25 +1267,25 @@ mlir::Value genIOOption<Fortran::parser::IoControlSpec::CharExpr>(
mlir::func::FuncOp ioFunc;
switch (std::get<Fortran::parser::IoControlSpec::CharExpr::Kind>(spec.t)) {
case Fortran::parser::IoControlSpec::CharExpr::Kind::Advance:
ioFunc = getIORuntimeFunc<mkIOKey(SetAdvance)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetAdvance)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Blank:
ioFunc = getIORuntimeFunc<mkIOKey(SetBlank)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetBlank)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Decimal:
ioFunc = getIORuntimeFunc<mkIOKey(SetDecimal)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetDecimal)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Delim:
ioFunc = getIORuntimeFunc<mkIOKey(SetDelim)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetDelim)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Pad:
ioFunc = getIORuntimeFunc<mkIOKey(SetPad)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetPad)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Round:
ioFunc = getIORuntimeFunc<mkIOKey(SetRound)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetRound)>(loc, builder);
break;
case Fortran::parser::IoControlSpec::CharExpr::Kind::Sign:
ioFunc = getIORuntimeFunc<mkIOKey(SetSign)>(loc, builder);
ioFunc = fir::runtime::getIORuntimeFunc<mkIOKey(SetSign)>(loc, builder);
break;
}
Fortran::lower::StatementContext localStatementCtx;
@ -1351,7 +1329,8 @@ static void genIOGetVar(Fortran::lower::AbstractConverter &converter,
mlir::Location loc, mlir::Value cookie,
const VAR &parserVar) {
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp ioFunc = getIORuntimeFunc<IoRuntimeKey>(loc, builder);
mlir::func::FuncOp ioFunc =
fir::runtime::getIORuntimeFunc<IoRuntimeKey>(loc, builder);
mlir::Value value =
builder.create<fir::CallOp>(loc, ioFunc, mlir::ValueRange{cookie})
.getResult(0);
@ -1478,7 +1457,7 @@ genConditionHandlerCall(Fortran::lower::AbstractConverter &converter,
return;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp enableHandlers =
getIORuntimeFunc<mkIOKey(EnableHandlers)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(EnableHandlers)>(loc, builder);
mlir::Type boolType = enableHandlers.getFunctionType().getInput(1);
auto boolValue = [&](bool specifierIsPresent) {
return builder.create<mlir::arith::ConstantOp>(
@ -1793,9 +1772,10 @@ static mlir::Value genIOUnitNumber(Fortran::lower::AbstractConverter &converter,
if (rawUnitWidth > runtimeArgWidth) {
mlir::func::FuncOp check =
rawUnitWidth <= 64
? getIORuntimeFunc<mkIOKey(CheckUnitNumberInRange64)>(loc, builder)
: getIORuntimeFunc<mkIOKey(CheckUnitNumberInRange128)>(loc,
builder);
? fir::runtime::getIORuntimeFunc<mkIOKey(CheckUnitNumberInRange64)>(
loc, builder)
: fir::runtime::getIORuntimeFunc<mkIOKey(
CheckUnitNumberInRange128)>(loc, builder);
mlir::FunctionType funcTy = check.getFunctionType();
llvm::SmallVector<mlir::Value> args;
args.push_back(builder.createConvert(loc, funcTy.getInput(0), rawUnit));
@ -1870,7 +1850,8 @@ static mlir::Value genBasicIOStmt(Fortran::lower::AbstractConverter &converter,
Fortran::lower::StatementContext stmtCtx;
mlir::Location loc = converter.getCurrentLocation();
ConditionSpecInfo csi = lowerErrorSpec(converter, loc, stmt.v);
mlir::func::FuncOp beginFunc = getIORuntimeFunc<K>(loc, builder);
mlir::func::FuncOp beginFunc =
fir::runtime::getIORuntimeFunc<K>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
mlir::Value unit = genIOUnitNumber(
converter, loc, getExpr<Fortran::parser::FileUnitNumber>(stmt),
@ -1924,7 +1905,7 @@ genNewunitSpec(Fortran::lower::AbstractConverter &converter, mlir::Location loc,
Fortran::lower::StatementContext stmtCtx;
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp ioFunc =
getIORuntimeFunc<mkIOKey(GetNewUnit)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(GetNewUnit)>(loc, builder);
mlir::FunctionType ioFuncTy = ioFunc.getFunctionType();
const auto *var = Fortran::semantics::GetExpr(newunit->v);
mlir::Value addr = builder.createConvert(
@ -1949,7 +1930,8 @@ Fortran::lower::genOpenStatement(Fortran::lower::AbstractConverter &converter,
ConditionSpecInfo csi = lowerErrorSpec(converter, loc, stmt.v);
bool hasNewunitSpec = false;
if (hasSpec<Fortran::parser::FileUnitNumber>(stmt)) {
beginFunc = getIORuntimeFunc<mkIOKey(BeginOpenUnit)>(loc, builder);
beginFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(BeginOpenUnit)>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
mlir::Value unit = genIOUnitNumber(
converter, loc, getExpr<Fortran::parser::FileUnitNumber>(stmt),
@ -1960,7 +1942,8 @@ Fortran::lower::genOpenStatement(Fortran::lower::AbstractConverter &converter,
} else {
hasNewunitSpec = hasSpec<Fortran::parser::ConnectSpec::Newunit>(stmt);
assert(hasNewunitSpec && "missing unit specifier");
beginFunc = getIORuntimeFunc<mkIOKey(BeginOpenNewUnit)>(loc, builder);
beginFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(BeginOpenNewUnit)>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
beginArgs.push_back(locToFilename(converter, loc, beginFuncTy.getInput(0)));
beginArgs.push_back(locToLineNo(converter, loc, beginFuncTy.getInput(1)));
@ -1992,8 +1975,9 @@ Fortran::lower::genWaitStatement(Fortran::lower::AbstractConverter &converter,
ConditionSpecInfo csi = lowerErrorSpec(converter, loc, stmt.v);
bool hasId = hasSpec<Fortran::parser::IdExpr>(stmt);
mlir::func::FuncOp beginFunc =
hasId ? getIORuntimeFunc<mkIOKey(BeginWait)>(loc, builder)
: getIORuntimeFunc<mkIOKey(BeginWaitAll)>(loc, builder);
hasId
? fir::runtime::getIORuntimeFunc<mkIOKey(BeginWait)>(loc, builder)
: fir::runtime::getIORuntimeFunc<mkIOKey(BeginWaitAll)>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
mlir::Value unit = genIOUnitNumber(
converter, loc, getExpr<Fortran::parser::FileUnitNumber>(stmt),
@ -2038,45 +2022,49 @@ getBeginDataTransferFunc(mlir::Location loc, fir::FirOpBuilder &builder,
if (isInternal) {
if (isInternalWithDesc) {
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginInternalArrayListInput)>(
loc, builder);
return getIORuntimeFunc<mkIOKey(BeginInternalArrayFormattedInput)>(
loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalArrayListInput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalArrayFormattedInput)>(loc, builder);
}
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginInternalListInput)>(loc,
builder);
return getIORuntimeFunc<mkIOKey(BeginInternalFormattedInput)>(loc,
builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalListInput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalFormattedInput)>(loc, builder);
}
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginExternalListInput)>(loc, builder);
return getIORuntimeFunc<mkIOKey(BeginExternalFormattedInput)>(loc,
builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(BeginExternalListInput)>(
loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginExternalFormattedInput)>(loc, builder);
}
return getIORuntimeFunc<mkIOKey(BeginUnformattedInput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(BeginUnformattedInput)>(
loc, builder);
} else {
if (isFormatted || isListOrNml) {
if (isInternal) {
if (isInternalWithDesc) {
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginInternalArrayListOutput)>(
loc, builder);
return getIORuntimeFunc<mkIOKey(BeginInternalArrayFormattedOutput)>(
loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalArrayListOutput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalArrayFormattedOutput)>(loc, builder);
}
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginInternalListOutput)>(loc,
builder);
return getIORuntimeFunc<mkIOKey(BeginInternalFormattedOutput)>(loc,
builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalListOutput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginInternalFormattedOutput)>(loc, builder);
}
if (isListOrNml)
return getIORuntimeFunc<mkIOKey(BeginExternalListOutput)>(loc, builder);
return getIORuntimeFunc<mkIOKey(BeginExternalFormattedOutput)>(loc,
builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(BeginExternalListOutput)>(
loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(
BeginExternalFormattedOutput)>(loc, builder);
}
return getIORuntimeFunc<mkIOKey(BeginUnformattedOutput)>(loc, builder);
return fir::runtime::getIORuntimeFunc<mkIOKey(BeginUnformattedOutput)>(
loc, builder);
}
}
@ -2203,19 +2191,21 @@ genDataTransferStmt(Fortran::lower::AbstractConverter &converter,
// Generate data transfer list calls.
if constexpr (isInput) { // READ
if (isNml)
genNamelistIO(converter, cookie,
getIORuntimeFunc<mkIOKey(InputNamelist)>(loc, builder),
*getIOControl<Fortran::parser::Name>(stmt)->symbol,
csi.hasTransferConditionSpec(), ok, stmtCtx);
genNamelistIO(
converter, cookie,
fir::runtime::getIORuntimeFunc<mkIOKey(InputNamelist)>(loc, builder),
*getIOControl<Fortran::parser::Name>(stmt)->symbol,
csi.hasTransferConditionSpec(), ok, stmtCtx);
else
genInputItemList(converter, cookie, stmt.items, isFormatted,
csi.hasTransferConditionSpec(), ok, /*inLoop=*/false);
} else if constexpr (std::is_same_v<A, Fortran::parser::WriteStmt>) {
if (isNml)
genNamelistIO(converter, cookie,
getIORuntimeFunc<mkIOKey(OutputNamelist)>(loc, builder),
*getIOControl<Fortran::parser::Name>(stmt)->symbol,
csi.hasTransferConditionSpec(), ok, stmtCtx);
genNamelistIO(
converter, cookie,
fir::runtime::getIORuntimeFunc<mkIOKey(OutputNamelist)>(loc, builder),
*getIOControl<Fortran::parser::Name>(stmt)->symbol,
csi.hasTransferConditionSpec(), ok, stmtCtx);
else
genOutputItemList(converter, cookie, stmt.items, isFormatted,
csi.hasTransferConditionSpec(), ok,
@ -2307,7 +2297,7 @@ mlir::Value genInquireSpec<Fortran::parser::InquireSpec::CharVar>(
return {};
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp specFunc =
getIORuntimeFunc<mkIOKey(InquireCharacter)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(InquireCharacter)>(loc, builder);
mlir::FunctionType specFuncTy = specFunc.getFunctionType();
const auto *varExpr = Fortran::semantics::GetExpr(
std::get<Fortran::parser::ScalarDefaultCharVariable>(var.t));
@ -2337,7 +2327,7 @@ mlir::Value genInquireSpec<Fortran::parser::InquireSpec::IntVar>(
return {};
fir::FirOpBuilder &builder = converter.getFirOpBuilder();
mlir::func::FuncOp specFunc =
getIORuntimeFunc<mkIOKey(InquireInteger64)>(loc, builder);
fir::runtime::getIORuntimeFunc<mkIOKey(InquireInteger64)>(loc, builder);
mlir::FunctionType specFuncTy = specFunc.getFunctionType();
const auto *varExpr = Fortran::semantics::GetExpr(
std::get<Fortran::parser::ScalarIntVariable>(var.t));
@ -2374,8 +2364,10 @@ mlir::Value genInquireSpec<Fortran::parser::InquireSpec::LogVar>(
idExpr &&
logVarKind == Fortran::parser::InquireSpec::LogVar::Kind::Pending;
mlir::func::FuncOp specFunc =
pendId ? getIORuntimeFunc<mkIOKey(InquirePendingId)>(loc, builder)
: getIORuntimeFunc<mkIOKey(InquireLogical)>(loc, builder);
pendId ? fir::runtime::getIORuntimeFunc<mkIOKey(InquirePendingId)>(
loc, builder)
: fir::runtime::getIORuntimeFunc<mkIOKey(InquireLogical)>(loc,
builder);
mlir::FunctionType specFuncTy = specFunc.getFunctionType();
mlir::Value addr = fir::getBase(converter.genExprAddr(
loc,
@ -2460,7 +2452,8 @@ mlir::Value Fortran::lower::genInquireStatement(
// Make one of three BeginInquire calls.
if (inquireFileUnit()) {
// Inquire by unit -- [UNIT=]file-unit-number.
beginFunc = getIORuntimeFunc<mkIOKey(BeginInquireUnit)>(loc, builder);
beginFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(BeginInquireUnit)>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
mlir::Value unit = genIOUnitNumber(converter, loc, exprPair.first,
beginFuncTy.getInput(0), csi, stmtCtx);
@ -2468,7 +2461,8 @@ mlir::Value Fortran::lower::genInquireStatement(
locToLineNo(converter, loc, beginFuncTy.getInput(2))};
} else if (inquireFileName()) {
// Inquire by file -- FILE=file-name-expr.
beginFunc = getIORuntimeFunc<mkIOKey(BeginInquireFile)>(loc, builder);
beginFunc =
fir::runtime::getIORuntimeFunc<mkIOKey(BeginInquireFile)>(loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
fir::ExtendedValue file =
converter.genExprAddr(loc, exprPair.first, stmtCtx);
@ -2482,7 +2476,8 @@ mlir::Value Fortran::lower::genInquireStatement(
const auto *ioLength =
std::get_if<Fortran::parser::InquireStmt::Iolength>(&stmt.u);
assert(ioLength && "must have an IOLENGTH specifier");
beginFunc = getIORuntimeFunc<mkIOKey(BeginInquireIoLength)>(loc, builder);
beginFunc = fir::runtime::getIORuntimeFunc<mkIOKey(BeginInquireIoLength)>(
loc, builder);
mlir::FunctionType beginFuncTy = beginFunc.getFunctionType();
beginArgs = {locToFilename(converter, loc, beginFuncTy.getInput(0)),
locToLineNo(converter, loc, beginFuncTy.getInput(1))};
@ -2501,7 +2496,10 @@ mlir::Value Fortran::lower::genInquireStatement(
mlir::Value length =
builder
.create<fir::CallOp>(
loc, getIORuntimeFunc<mkIOKey(GetIoLength)>(loc, builder), args)
loc,
fir::runtime::getIORuntimeFunc<mkIOKey(GetIoLength)>(loc,
builder),
args)
.getResult(0);
mlir::Value length1 =
builder.createConvert(loc, converter.genType(*ioLengthVar), length);

View File

@ -16,6 +16,7 @@
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Support/DataLayout.h"
@ -46,6 +47,17 @@ fir::FirOpBuilder::createFunction(mlir::Location loc, mlir::ModuleOp module,
return fir::createFuncOp(loc, module, name, ty, /*attrs*/ {}, symbolTable);
}
mlir::func::FuncOp
fir::FirOpBuilder::createRuntimeFunction(mlir::Location loc,
llvm::StringRef name,
mlir::FunctionType ty, bool isIO) {
mlir::func::FuncOp func = createFunction(loc, name, ty);
func->setAttr(fir::FIROpsDialect::getFirRuntimeAttrName(), getUnitAttr());
if (isIO)
func->setAttr("fir.io", getUnitAttr());
return func;
}
mlir::func::FuncOp
fir::FirOpBuilder::getNamedFunction(mlir::ModuleOp modOp,
const mlir::SymbolTable *symbolTable,

View File

@ -17,6 +17,7 @@
#include "flang/Optimizer/CodeGen/FIROpPatterns.h"
#include "flang/Optimizer/CodeGen/TypeConverter.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Support/DataLayout.h"
@ -585,6 +586,11 @@ struct CallOpConversion : public fir::FIROpConversion<fir::CallOp> {
matchAndRewrite(fir::CallOp call, OpAdaptor adaptor,
mlir::ConversionPatternRewriter &rewriter) const override {
llvm::SmallVector<mlir::Type> resultTys;
mlir::Attribute memAttr =
call->getAttr(fir::FIROpsDialect::getFirCallMemoryAttrName());
if (memAttr)
call->removeAttr(fir::FIROpsDialect::getFirCallMemoryAttrName());
for (auto r : call.getResults())
resultTys.push_back(convertType(r.getType()));
// Convert arith::FastMathFlagsAttr to LLVM::FastMathFlagsAttr.
@ -626,6 +632,10 @@ struct CallOpConversion : public fir::FIROpConversion<fir::CallOp> {
}
if (mlir::ArrayAttr resAttrs = call.getResAttrsAttr())
llvmCall.setResAttrsAttr(resAttrs);
if (memAttr)
llvmCall.setMemoryEffectsAttr(
mlir::cast<mlir::LLVM::MemoryEffectsAttr>(memAttr));
return mlir::success();
}
};

View File

@ -211,6 +211,9 @@ void createDefaultFIROptimizerPassPipeline(mlir::PassManager &pm,
pm.addPass(fir::createSimplifyRegionLite());
pm.addPass(mlir::createCSEPass());
if (pc.OptLevel.isOptimizingForSpeed())
pm.addPass(fir::createSetRuntimeCallAttributes());
// Last Optimizer EP Callback
pc.invokeFIROptLastEPCallbacks(pm, pc.OptLevel);
}

View File

@ -29,6 +29,8 @@ add_flang_library(FIRTransforms
VScaleAttr.cpp
FunctionAttr.cpp
DebugTypeGenerator.cpp
SetRuntimeCallAttributes.cpp
GenRuntimeCallsForTest.cpp
DEPENDS
CUFAttrs

View File

@ -0,0 +1,106 @@
//===- GenRuntimeCallsForTest.cpp -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
/// \file
/// This pass is only for developers to generate declarations/calls
/// of Fortran runtime function recognized in
/// flang/Optimizer/Transforms/RuntimeFunctions.inc table.
/// Sample of the generated FIR:
/// func.func private
/// @_FortranAioSetStatus(!fir.ref<i8>, !fir.ref<i8>, i64) ->
/// i1 attributes {fir.io, fir.runtime}
///
/// func.func @test__FortranAioSetStatus(
/// %arg0: !fir.ref<i8>, %arg1: !fir.ref<i8>, %arg2: i64) -> i1 {
/// %0 = fir.call @_FortranAioSetStatus(%arg0, %arg1, %arg2) :
/// (!fir.ref<i8>, !fir.ref<i8>, i64) -> i1
/// return %0 : i1
/// }
//===----------------------------------------------------------------------===//
#include "flang/Common/static-multimap-view.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "flang/Runtime/io-api-consts.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
namespace fir {
#define GEN_PASS_DEF_GENRUNTIMECALLSFORTEST
#include "flang/Optimizer/Transforms/Passes.h.inc"
} // namespace fir
#define DEBUG_TYPE "gen-runtime-calls-for-test"
using namespace Fortran::runtime;
using namespace Fortran::runtime::io;
#define mkIOKey(X) FirmkKey(IONAME(X))
#define mkRTKey(X) FirmkKey(RTNAME(X))
namespace {
class GenRuntimeCallsForTestPass
: public fir::impl::GenRuntimeCallsForTestBase<GenRuntimeCallsForTestPass> {
using GenRuntimeCallsForTestBase<
GenRuntimeCallsForTestPass>::GenRuntimeCallsForTestBase;
public:
void runOnOperation() override;
};
} // end anonymous namespace
static constexpr llvm::StringRef testPrefix = "test_";
void GenRuntimeCallsForTestPass::runOnOperation() {
mlir::ModuleOp moduleOp = getOperation();
mlir::OpBuilder mlirBuilder(moduleOp.getRegion());
fir::FirOpBuilder builder(mlirBuilder, moduleOp);
mlir::Location loc = mlir::UnknownLoc::get(builder.getContext());
#define KNOWN_IO_FUNC(X) \
fir::runtime::getIORuntimeFunc<mkIOKey(X)>(loc, builder)
#define KNOWN_RUNTIME_FUNC(X) \
fir::runtime::getRuntimeFunc<mkRTKey(X)>(loc, builder)
mlir::func::FuncOp runtimeFuncsTable[] = {
#include "flang/Optimizer/Transforms/RuntimeFunctions.inc"
};
if (!doGenerateCalls)
return;
// Generate thin wrapper functions calling the known Fortran
// runtime functions.
llvm::SmallVector<mlir::Operation *> newFuncs;
for (unsigned i = 0;
i < sizeof(runtimeFuncsTable) / sizeof(runtimeFuncsTable[0]); ++i) {
mlir::func::FuncOp funcOp = runtimeFuncsTable[i];
mlir::FunctionType funcTy = funcOp.getFunctionType();
std::string name = (llvm::Twine(testPrefix) + funcOp.getName()).str();
mlir::func::FuncOp callerFunc = builder.createFunction(loc, name, funcTy);
callerFunc.setVisibility(mlir::SymbolTable::Visibility::Public);
mlir::OpBuilder::InsertPoint insertPt = builder.saveInsertionPoint();
// Generate the wrapper function body that consists of a call and return.
builder.setInsertionPointToStart(callerFunc.addEntryBlock());
mlir::Block::BlockArgListType args = callerFunc.front().getArguments();
auto callOp = builder.create<fir::CallOp>(loc, funcOp, args);
builder.create<mlir::func::ReturnOp>(loc, callOp.getResults());
newFuncs.push_back(callerFunc.getOperation());
builder.restoreInsertionPoint(insertPt);
}
// Make sure all wrapper functions are at the beginning
// of the module.
auto moduleBegin = moduleOp.getBody()->begin();
for (auto func : newFuncs)
func->moveBefore(moduleOp.getBody(), moduleBegin);
}

View File

@ -0,0 +1,258 @@
//===- SetRuntimeCallAttributes.cpp ---------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
/// \file
/// SetRuntimeCallAttributesPass looks for fir.call operations
/// that are calling into Fortran runtime, and tries to set different
/// attributes on them to enable more optimizations in LLVM backend
/// (granted that they are preserved all the way to LLVM IR).
/// This pass is currently only attaching fir.call wide atttributes,
/// such as ones corresponding to llvm.memory, nosync, nocallbac, etc.
/// It is not designed to attach attributes to the arguments and the results
/// of a call.
//===----------------------------------------------------------------------===//
#include "flang/Common/static-multimap-view.h"
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "flang/Runtime/io-api-consts.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
namespace fir {
#define GEN_PASS_DEF_SETRUNTIMECALLATTRIBUTES
#include "flang/Optimizer/Transforms/Passes.h.inc"
} // namespace fir
#define DEBUG_TYPE "set-runtime-call-attrs"
using namespace Fortran::runtime;
using namespace Fortran::runtime::io;
#define mkIOKey(X) FirmkKey(IONAME(X))
#define mkRTKey(X) FirmkKey(RTNAME(X))
// Return LLVM dialect MemoryEffectsAttr for the given Fortran runtime call.
// This function is computing a generic value of this attribute
// by analyzing the arguments and their types.
// It tries to figure out if an "indirect" memory access is possible
// during this call. If it is not possible, then the memory effects
// are:
// * other = NoModRef
// * argMem = ModRef
// * inaccessibleMem = ModRef
//
// Otherwise, it returns an empty attribute meaning ModRef for all kinds
// of memory.
//
// The attribute deduction is conservative in a sense that it applies
// to most of the runtime calls, but it may still be incorrect for some
// runtime calls.
static mlir::LLVM::MemoryEffectsAttr getGenericMemoryAttr(fir::CallOp callOp) {
bool maybeIndirectAccess = false;
for (auto arg : callOp.getArgOperands()) {
mlir::Type argType = arg.getType();
if (mlir::isa<fir::BaseBoxType>(argType)) {
// If it is a null/absent box, then this particular call
// cannot access memory indirectly through the box's
// base_addr.
auto def = arg.getDefiningOp();
if (!mlir::isa_and_nonnull<fir::ZeroOp, fir::AbsentOp>(def)) {
maybeIndirectAccess = true;
break;
}
}
if (auto refType = mlir::dyn_cast<fir::ReferenceType>(argType)) {
if (!fir::isa_trivial(refType.getElementType())) {
maybeIndirectAccess = true;
break;
}
}
if (auto ptrType = mlir::dyn_cast<mlir::LLVM::LLVMPointerType>(argType)) {
maybeIndirectAccess = true;
break;
}
}
if (!maybeIndirectAccess) {
return mlir::LLVM::MemoryEffectsAttr::get(
callOp->getContext(),
{/*other=*/mlir::LLVM::ModRefInfo::NoModRef,
/*argMem=*/mlir::LLVM::ModRefInfo::ModRef,
/*inaccessibleMem=*/mlir::LLVM::ModRefInfo::ModRef});
}
return {};
}
namespace {
class SetRuntimeCallAttributesPass
: public fir::impl::SetRuntimeCallAttributesBase<
SetRuntimeCallAttributesPass> {
public:
void runOnOperation() override;
};
// A helper to match a type against a list of types.
template <typename T, typename... Ts>
constexpr bool IsAny = std::disjunction_v<std::is_same<T, Ts>...>;
} // end anonymous namespace
// MemoryAttrDesc type provides get() method for computing
// mlir::LLVM::MemoryEffectsAttr for the given Fortran runtime call.
// If needed, add specializations for particular runtime calls.
namespace {
// Default implementation just uses getGenericMemoryAttr().
// Note that it may be incorrect for some runtime calls.
template <typename KEY, typename Enable = void>
struct MemoryAttrDesc {
static mlir::LLVM::MemoryEffectsAttr get(fir::CallOp callOp) {
return getGenericMemoryAttr(callOp);
}
};
} // end anonymous namespace
// NosyncAttrDesc type provides get() method for computing
// LLVM nosync attribute for the given call.
namespace {
// Default implementation always returns LLVM nosync.
// This should be true for the majority of the Fortran runtime calls.
template <typename KEY, typename Enable = void>
struct NosyncAttrDesc {
static std::optional<mlir::NamedAttribute> get(fir::CallOp callOp) {
// TODO: replace llvm.nosync with an LLVM dialect callback.
return mlir::NamedAttribute("llvm.nosync",
mlir::UnitAttr::get(callOp->getContext()));
}
};
} // end anonymous namespace
// NocallbackAttrDesc type provides get() method for computing
// LLVM nocallback attribute for the given call.
namespace {
// Default implementation always returns LLVM nocallback.
// It must be specialized for Fortran runtime functions that may call
// user functions during their execution (e.g. defined IO, assignment).
template <typename KEY, typename Enable = void>
struct NocallbackAttrDesc {
static std::optional<mlir::NamedAttribute> get(fir::CallOp callOp) {
// TODO: replace llvm.nocallback with an LLVM dialect callback.
return mlir::NamedAttribute("llvm.nocallback",
mlir::UnitAttr::get(callOp->getContext()));
}
};
// Derived types IO may call back into a Fortran module.
// This specialization is conservative for Input/OutputDerivedType,
// and it might be improved by checking if the NonTbpDefinedIoTable
// pointer argument is null.
template <typename KEY>
struct NocallbackAttrDesc<
KEY, std::enable_if_t<
IsAny<KEY, mkIOKey(OutputDerivedType), mkIOKey(InputDerivedType),
mkIOKey(OutputNamelist), mkIOKey(InputNamelist)>>> {
static std::optional<mlir::NamedAttribute> get(fir::CallOp) {
return std::nullopt;
}
};
} // end anonymous namespace
namespace {
// RuntimeFunction provides different callbacks that compute values
// of fir.call attributes for a Fortran runtime function.
struct RuntimeFunction {
using MemoryAttrGeneratorTy = mlir::LLVM::MemoryEffectsAttr (*)(fir::CallOp);
using NamedAttrGeneratorTy =
std::optional<mlir::NamedAttribute> (*)(fir::CallOp);
using Key = std::string_view;
constexpr operator Key() const { return key; }
Key key;
MemoryAttrGeneratorTy memoryAttrGenerator;
NamedAttrGeneratorTy nosyncAttrGenerator;
NamedAttrGeneratorTy nocallbackAttrGenerator;
};
// Helper type to create a RuntimeFunction descriptor given
// the KEY and a function name.
template <typename KEY>
struct RuntimeFactory {
static constexpr RuntimeFunction create(const char name[]) {
// GCC 7 does not recognize this as a constant expression:
// ((const char *)RuntimeFunction<>::name) == nullptr
// This comparison comes from the basic_string_view(const char *)
// constructor. We have to use the other constructor
// that takes explicit length parameter.
return RuntimeFunction{
std::string_view{name, std::char_traits<char>::length(name)},
MemoryAttrDesc<KEY>::get, NosyncAttrDesc<KEY>::get,
NocallbackAttrDesc<KEY>::get};
}
};
} // end anonymous namespace
#define KNOWN_IO_FUNC(X) RuntimeFactory<mkIOKey(X)>::create(mkIOKey(X)::name)
#define KNOWN_RUNTIME_FUNC(X) \
RuntimeFactory<mkRTKey(X)>::create(mkRTKey(X)::name)
// A table of RuntimeFunction descriptors for all recognized
// Fortran runtime functions.
static constexpr RuntimeFunction runtimeFuncsTable[] = {
#include "flang/Optimizer/Transforms/RuntimeFunctions.inc"
};
static constexpr Fortran::common::StaticMultimapView<RuntimeFunction>
runtimeFuncs(runtimeFuncsTable);
static_assert(runtimeFuncs.Verify() && "map must be sorted");
// Set attributes for the given Fortran runtime call.
// The symbolTable is used to cache the name lookups in the module.
static void setRuntimeCallAttributes(fir::CallOp callOp,
mlir::SymbolTableCollection &symbolTable) {
auto iface = mlir::cast<mlir::CallOpInterface>(callOp.getOperation());
auto funcOp = mlir::dyn_cast_or_null<mlir::func::FuncOp>(
iface.resolveCallableInTable(&symbolTable));
if (!funcOp || !funcOp->hasAttrOfType<mlir::UnitAttr>(
fir::FIROpsDialect::getFirRuntimeAttrName()))
return;
llvm::StringRef name = funcOp.getName();
if (auto range = runtimeFuncs.equal_range(name);
range.first != range.second) {
// There should not be duplicate entries.
assert(range.first + 1 == range.second);
const RuntimeFunction &desc = *range.first;
LLVM_DEBUG(llvm::dbgs()
<< "Identified runtime function call: " << desc.key << '\n');
if (mlir::LLVM::MemoryEffectsAttr memoryAttr =
desc.memoryAttrGenerator(callOp))
callOp->setAttr(fir::FIROpsDialect::getFirCallMemoryAttrName(),
memoryAttr);
if (auto attr = desc.nosyncAttrGenerator(callOp))
callOp->setAttr(attr->getName(), attr->getValue());
if (auto attr = desc.nocallbackAttrGenerator(callOp))
callOp->setAttr(attr->getName(), attr->getValue());
LLVM_DEBUG(llvm::dbgs() << "Operation with attrs: " << callOp << '\n');
}
}
void SetRuntimeCallAttributesPass::runOnOperation() {
mlir::func::FuncOp funcOp = getOperation();
// Exit early for declarations to skip the debug output for them.
if (funcOp.isDeclaration())
return;
LLVM_DEBUG(llvm::dbgs() << "=== Begin " DEBUG_TYPE " ===\n");
LLVM_DEBUG(llvm::dbgs() << "Func-name:" << funcOp.getSymName() << "\n");
mlir::SymbolTableCollection symbolTable;
funcOp.walk([&](fir::CallOp callOp) {
setRuntimeCallAttributes(callOp, symbolTable);
});
LLVM_DEBUG(llvm::dbgs() << "=== End " DEBUG_TYPE " ===\n");
}

View File

@ -123,6 +123,8 @@ end program
! ALL-NEXT: CSE
! ALL-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
! ALL-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
! O2-NEXT: 'func.func' Pipeline
! O2-NEXT: SetRuntimeCallAttributes
! ALL-NEXT: BoxedProcedurePass
! ALL-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']

View File

@ -121,6 +121,8 @@ func.func @_QQmain() {
// PASSES-NEXT: CSE
// PASSES-NEXT: (S) 0 num-cse'd - Number of operations CSE'd
// PASSES-NEXT: (S) 0 num-dce'd - Number of operations DCE'd
// PASSES-NEXT: 'func.func' Pipeline
// PASSES-NEXT: SetRuntimeCallAttributes
// PASSES-NEXT: BoxedProcedurePass
// PASSES-NEXT: Pipeline Collection : ['fir.global', 'func.func', 'gpu.module', 'omp.declare_reduction', 'omp.private']

View File

@ -122,7 +122,7 @@ end
! CHECK: %[[V_50:[0-9]+]] = fir.slice %[[C_1]], %[[C_2]], %[[C_1]] : (index, index, index) -> !fir.slice<1>
! CHECK: %[[V_51:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_50]]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<2xf32>>
! CHECK: %[[V_52:[0-9]+]] = fir.convert %[[V_51:[0-9]+]] : (!fir.box<!fir.array<2xf32>>) -> !fir.box<none>
! CHECK: %[[V_53:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_49]], %[[V_52]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_53:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_49]], %[[V_52]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_54:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
! CHECK: %[[V_55:[0-9]+]] = arith.subi %[[V_54]], %[[C_1_i32]] : i32
! CHECK: %[[V_56:[0-9]+]] = fir.convert %[[V_55:[0-9]+]] : (i32) -> index
@ -130,8 +130,8 @@ end
! CHECK: %[[V_58:[0-9]+]] = fir.slice %[[V_56]], %[[V_57]], %[[C_1]] : (index, index, index) -> !fir.slice<1>
! CHECK: %[[V_59:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_58]]] : (!fir.ref<!fir.array<?xf32>>, !fir.shape<1>, !fir.slice<1>) -> !fir.box<!fir.array<?xf32>>
! CHECK: %[[V_60:[0-9]+]] = fir.convert %[[V_59:[0-9]+]] : (!fir.box<!fir.array<?xf32>>) -> !fir.box<none>
! CHECK: %[[V_61:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_49]], %[[V_60]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_62:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_49]]) fastmath<contract> : (!fir.ref<i8>) -> i32
! CHECK: %[[V_61:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_49]], %[[V_60]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_62:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_49]]) fastmath<contract> {{.*}}: (!fir.ref<i8>) -> i32
! CHECK: return
! CHECK: }
@ -248,7 +248,7 @@ end
! CHECK: %[[V_74:[0-9]+]] = fir.slice %[[C_1]], %[[C_2]], %[[C_1]], %[[C_1]], %[[C_2]], %[[C_1]] : (index, index, index, index, index, index) -> !fir.slice<2>
! CHECK: %[[V_75:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_74]]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x2xf32>>
! CHECK: %[[V_76:[0-9]+]] = fir.convert %[[V_75:[0-9]+]] : (!fir.box<!fir.array<?x2xf32>>) -> !fir.box<none>
! CHECK: %[[V_77:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_76]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_77:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_76]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_78:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
! CHECK: %[[V_79:[0-9]+]] = arith.subi %[[V_78]], %[[C_1_i32]] : i32
! CHECK: %[[V_80:[0-9]+]] = fir.convert %[[V_79:[0-9]+]] : (i32) -> index
@ -256,8 +256,8 @@ end
! CHECK: %[[V_82:[0-9]+]] = fir.slice %[[C_1]], %[[C_2]], %[[C_1]], %[[V_80]], %[[V_81]], %[[C_1]] : (index, index, index, index, index, index) -> !fir.slice<2>
! CHECK: %[[V_83:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_82]]] : (!fir.ref<!fir.array<2x?xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
! CHECK: %[[V_84:[0-9]+]] = fir.convert %[[V_83:[0-9]+]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
! CHECK: %[[V_85:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_84]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_86:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_73]]) fastmath<contract> : (!fir.ref<i8>) -> i32
! CHECK: %[[V_85:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_84]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_86:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_73]]) fastmath<contract> {{.*}}: (!fir.ref<i8>) -> i32
! CHECK: return
! CHECK: }
@ -374,7 +374,7 @@ end
! CHECK: %[[V_74:[0-9]+]] = fir.slice %[[C_1]], %[[C_2]], %[[C_1]], %[[C_1]], %[[C_2]], %[[C_1]] : (index, index, index, index, index, index) -> !fir.slice<2>
! CHECK: %[[V_75:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_74]]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<2x?xf32>>
! CHECK: %[[V_76:[0-9]+]] = fir.convert %[[V_75:[0-9]+]] : (!fir.box<!fir.array<2x?xf32>>) -> !fir.box<none>
! CHECK: %[[V_77:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_76]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_77:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_76]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_78:[0-9]+]] = fir.load %arg0 : !fir.ref<i32>
! CHECK: %[[V_79:[0-9]+]] = arith.subi %[[V_78]], %[[C_1_i32]] : i32
! CHECK: %[[V_80:[0-9]+]] = fir.convert %[[V_79:[0-9]+]] : (i32) -> index
@ -382,8 +382,8 @@ end
! CHECK: %[[V_82:[0-9]+]] = fir.slice %[[V_80]], %[[V_81]], %[[C_1]], %[[C_1]], %[[C_2]], %[[C_1]] : (index, index, index, index, index, index) -> !fir.slice<2>
! CHECK: %[[V_83:[0-9]+]] = fir.embox %[[V_4]](%[[V_5]]) [%[[V_82]]] : (!fir.ref<!fir.array<?x2xf32>>, !fir.shape<2>, !fir.slice<2>) -> !fir.box<!fir.array<?x?xf32>>
! CHECK: %[[V_84:[0-9]+]] = fir.convert %[[V_83:[0-9]+]] : (!fir.box<!fir.array<?x?xf32>>) -> !fir.box<none>
! CHECK: %[[V_85:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_84]]) fastmath<contract> : (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_86:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_73]]) fastmath<contract> : (!fir.ref<i8>) -> i32
! CHECK: %[[V_85:[0-9]+]] = fir.call @_FortranAioOutputDescriptor(%[[V_73]], %[[V_84]]) fastmath<contract> {{.*}}: (!fir.ref<i8>, !fir.box<none>) -> i1
! CHECK: %[[V_86:[0-9]+]] = fir.call @_FortranAioEndIoStatement(%[[V_73]]) fastmath<contract> {{.*}}: (!fir.ref<i8>) -> i32
! CHECK: return
! CHECK: }

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,101 @@
// RUN: echo "module {}" | fir-opt --gen-runtime-calls-for-test | FileCheck %s
// NOTE: Assertions have been autogenerated by flang/test/Utils/generate-checks-for-runtime-funcs.py
// The script allows updating Flang LIT test
// flang/test/Transforms/verify-known-runtime-functions.fir,
// which is intended to verify signatures of Fortran runtime
// functions recognized in flang/Optimizer/Transforms/RuntimeFunctions.inc
// table. If new function is added into the table or
// an existing function changes its signature,
// the SetRuntimeCallAttributesPass may need to be updated
// to properly handle it. Once the pass is verified to work,
// one can update this test using the following output:
// echo "module {}" | fir-opt --gen-runtime-calls-for-test | \
// generate-checks-for-runtime-funcs.py
// CHECK-NOT: func.func
// CHECK: func.func private @_FortranAioBeginBackspace(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginClose(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginEndfile(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginExternalFormattedInput(!fir.ref<i8>, i64, !fir.box<none>, i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginExternalFormattedOutput(!fir.ref<i8>, i64, !fir.box<none>, i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginExternalListInput(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginExternalListOutput(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginFlush(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInquireFile(!fir.ref<i8>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInquireIoLength(!fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInquireUnit(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalArrayFormattedInput(!fir.box<none>, !fir.ref<i8>, i64, !fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalArrayFormattedOutput(!fir.box<none>, !fir.ref<i8>, i64, !fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalArrayListInput(!fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalArrayListOutput(!fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalFormattedInput(!fir.ref<i8>, i64, !fir.ref<i8>, i64, !fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalFormattedOutput(!fir.ref<i8>, i64, !fir.ref<i8>, i64, !fir.box<none>, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalListInput(!fir.ref<i8>, i64, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginInternalListOutput(!fir.ref<i8>, i64, !fir.ref<!fir.llvm_ptr<i8>>, i64, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginOpenNewUnit(!fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginOpenUnit(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginRewind(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginUnformattedInput(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginUnformattedOutput(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginWait(i32, i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioBeginWaitAll(i32, !fir.ref<i8>, i32) -> !fir.ref<i8> attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioCheckUnitNumberInRange128(i128, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioCheckUnitNumberInRange64(i64, i1, !fir.ref<i8>, i64, !fir.ref<i8>, i32) -> i32 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioEnableHandlers(!fir.ref<i8>, i1, i1, i1, i1, i1) attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioEndIoStatement(!fir.ref<i8>) -> i32 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioGetAsynchronousId(!fir.ref<i8>) -> i32 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioGetIoLength(!fir.ref<i8>) -> i64 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioGetIoMsg(!fir.ref<i8>, !fir.ref<i8>, i64) attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioGetNewUnit(!fir.ref<i8>, !fir.ref<i32>, i32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioGetSize(!fir.ref<i8>) -> i64 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputAscii(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputComplex32(!fir.ref<i8>, !fir.ref<f32>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputComplex64(!fir.ref<i8>, !fir.ref<f64>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputDerivedType(!fir.ref<i8>, !fir.box<none>, !fir.ref<tuple<>>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputDescriptor(!fir.ref<i8>, !fir.box<none>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputInteger(!fir.ref<i8>, !fir.ref<i64>, i32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputLogical(!fir.ref<i8>, !fir.ref<i1>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputNamelist(!fir.ref<i8>, !fir.ref<tuple<>>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputReal32(!fir.ref<i8>, !fir.ref<f32>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInputReal64(!fir.ref<i8>, !fir.ref<f64>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInquireCharacter(!fir.ref<i8>, i64, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInquireInteger64(!fir.ref<i8>, i64, !fir.ref<i64>, i32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInquireLogical(!fir.ref<i8>, i64, !fir.ref<i1>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioInquirePendingId(!fir.ref<i8>, i32, !fir.ref<i1>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputAscii(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputComplex32(!fir.ref<i8>, f32, f32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputComplex64(!fir.ref<i8>, f64, f64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputDerivedType(!fir.ref<i8>, !fir.box<none>, !fir.ref<tuple<>>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputDescriptor(!fir.ref<i8>, !fir.box<none>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputInteger128(!fir.ref<i8>, i128) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputInteger16(!fir.ref<i8>, i16) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputInteger32(!fir.ref<i8>, i32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputInteger64(!fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputInteger8(!fir.ref<i8>, i8) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputLogical(!fir.ref<i8>, i1) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputNamelist(!fir.ref<i8>, !fir.ref<tuple<>>) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputReal32(!fir.ref<i8>, f32) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioOutputReal64(!fir.ref<i8>, f64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetAccess(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetAction(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetAdvance(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetAsynchronous(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetBlank(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetCarriagecontrol(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetConvert(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetDecimal(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetDelim(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetEncoding(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetFile(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetForm(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetPad(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetPos(!fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetPosition(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetRec(!fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetRecl(!fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetRound(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetSign(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NEXT: func.func private @_FortranAioSetStatus(!fir.ref<i8>, !fir.ref<i8>, i64) -> i1 attributes {fir.io, fir.runtime}
// CHECK-NOT: func.func

View File

@ -0,0 +1,81 @@
#!/usr/bin/env python3
"""A script to generate FileCheck statements for Fortran runtime funcs.
This script can be used to update
flang/test/Transforms/verify-known-runtime-functions.fir
whenever new recognized Fortran runtime functions are added
into flang/Optimizer/Transforms/RuntimeFunctions.inc
or any of the recognized functions changes its signature.
"""
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import argparse
import os
import re
import sys
ADVERT_BEGIN = "// NOTE: Assertions have been autogenerated by "
ADVERT_END = """
// The script allows updating Flang LIT test
// flang/test/Transforms/verify-known-runtime-functions.fir,
// which is intended to verify signatures of Fortran runtime
// functions recognized in flang/Optimizer/Transforms/RuntimeFunctions.inc
// table. If new function is added into the table or
// an existing function changes its signature,
// the SetRuntimeCallAttributesPass may need to be updated
// to properly handle it. Once the pass is verified to work,
// one can update this test using the following output:
// echo "module {}" | fir-opt --gen-runtime-calls-for-test | \\
// generate-checks-for-runtime-funcs.py
"""
CHECK_RE_STR = "func.func.*@_Fortran.*"
CHECK_RE = re.compile(CHECK_RE_STR)
CHECK_NOT_STR = "// CHECK-NOT: func.func"
CHECK_STR = "// CHECK:"
CHECK_NEXT_STR = "// CHECK-NEXT:"
def main():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"input", nargs="?", type=argparse.FileType("r"), default=sys.stdin
)
args = parser.parse_args()
input_lines = [l.rstrip() for l in args.input]
args.input.close()
repo_path = os.path.join(os.path.dirname(__file__), "..", "..", "..")
script_name = os.path.relpath(__file__, repo_path)
autogenerated_note = ADVERT_BEGIN + script_name + "\n" + ADVERT_END
output = sys.stdout
output.write(autogenerated_note + "\n")
output_lines = []
output_lines.append(CHECK_NOT_STR)
check_prefix = CHECK_STR
for input_line in input_lines:
if not input_line:
continue
m = CHECK_RE.match(input_line.lstrip())
if m:
output_lines.append(check_prefix + " " + input_line.lstrip())
check_prefix = CHECK_NEXT_STR
output_lines.append(CHECK_NOT_STR)
for line in output_lines:
output.write(line + "\n")
output.close()
if __name__ == "__main__":
main()