[flang] Upstream partial lowering of GET_COMMAND_ARGUMENT intrinsic

This patch adds partial lowering of the "GET_COMMAND_ARGUMENT"
intrinsic to the backend runtime hook implemented in patches D109227,
D109813, D109814.

Differential Revision: https://reviews.llvm.org/D118801
This commit is contained in:
Josh Mottley 2022-01-06 14:55:33 +00:00
parent 31fa3a4d44
commit 38073b0669
3 changed files with 79 additions and 1 deletions

View File

@ -16,12 +16,21 @@ class Location;
namespace fir {
class FirOpBuilder;
}
} // namespace fir
namespace fir::runtime {
/// Generate call to COMMAND_ARGUMENT_COUNT intrinsic runtime routine.
mlir::Value genCommandArgumentCount(fir::FirOpBuilder &, mlir::Location);
/// Generate call to GET_COMMAND_ARGUMENT intrinsic runtime routine.
/// Note that GET_COMMAND_ARGUMENT intrinsic is split between 2 functions in
/// implementation; ArgumentValue and ArgumentLength. So we handle each
/// seperately.
void genGetCommandArgument(fir::FirOpBuilder &, mlir::Location,
mlir::Value number, mlir::Value value,
mlir::Value length, mlir::Value status,
mlir::Value errmsg);
} // namespace fir::runtime
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H

View File

@ -19,3 +19,52 @@ mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder,
fir::runtime::getRuntimeFunc<mkRTKey(ArgumentCount)>(loc, builder);
return builder.create<fir::CallOp>(loc, argumentCountFunc).getResult(0);
}
void fir::runtime::genGetCommandArgument(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Value number,
mlir::Value value, mlir::Value length,
mlir::Value status,
mlir::Value errmsg) {
auto argumentValueFunc =
fir::runtime::getRuntimeFunc<mkRTKey(ArgumentValue)>(loc, builder);
auto argumentLengthFunc =
fir::runtime::getRuntimeFunc<mkRTKey(ArgumentLength)>(loc, builder);
auto isPresent = [&](mlir::Value val) -> bool {
return !mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
};
mlir::Value valueResult;
// Run `ArgumentValue` intrisc only if we have either "value", "status" or
// "errmsg" `ArgumentValue` "requires" existing values for its arguments
// "value" and "errmsg". So in the case they aren't given, but the user has
// requested "status", we have to assign "absent" values to them before
// calling `ArgumentValue`. This happens in IntrinsicCall.cpp. For this reason
// we need extra indirection with `isPresent` for testing whether "value" or
// "errmsg" is present.
if (isPresent(value) || status || isPresent(errmsg)) {
llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
builder, loc, argumentValueFunc.getType(), number, value, errmsg);
valueResult =
builder.create<fir::CallOp>(loc, argumentValueFunc, args).getResult(0);
}
// Only save result of ArgumentValue if "status" parameter has been given
if (status) {
const mlir::Value statusLoaded = builder.create<fir::LoadOp>(loc, status);
mlir::Value resultCast =
builder.createConvert(loc, statusLoaded.getType(), valueResult);
builder.create<fir::StoreOp>(loc, resultCast, status);
}
if (length) {
llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
builder, loc, argumentLengthFunc.getType(), number);
mlir::Value result =
builder.create<fir::CallOp>(loc, argumentLengthFunc, args).getResult(0);
const mlir::Value valueLoaded = builder.create<fir::LoadOp>(loc, length);
mlir::Value resultCast =
builder.createConvert(loc, valueLoaded.getType(), result);
builder.create<fir::StoreOp>(loc, resultCast, length);
}
}

View File

@ -16,3 +16,23 @@ TEST_F(RuntimeCallTest, genCommandArgumentCountTest) {
checkCallOp(result.getDefiningOp(), "_FortranAArgumentCount", /*nbArgs=*/0,
/*addLocArgs=*/false);
}
TEST_F(RuntimeCallTest, genGetCommandArgument) {
mlir::Location loc = firBuilder->getUnknownLoc();
mlir::Type intTy = firBuilder->getDefaultIntegerType();
mlir::Type charTy = fir::BoxType::get(firBuilder->getNoneType());
mlir::Value number = firBuilder->create<fir::UndefOp>(loc, intTy);
mlir::Value value = firBuilder->create<fir::UndefOp>(loc, charTy);
mlir::Value errmsg = firBuilder->create<fir::UndefOp>(loc, charTy);
// genGetCommandArgument expects `length` and `status` to be memory references
mlir::Value length = firBuilder->create<fir::AllocaOp>(loc, intTy);
mlir::Value status = firBuilder->create<fir::AllocaOp>(loc, intTy);
fir::runtime::genGetCommandArgument(
*firBuilder, loc, number, value, length, status, errmsg);
checkCallOpFromResultBox(
value, "_FortranAArgumentValue", /*nbArgs=*/3, /*addLocArgs=*/false);
mlir::Block *block = firBuilder->getBlock();
EXPECT_TRUE(block) << "Failed to retrieve the block!";
checkBlockForCallOp(block, "_FortranAArgumentLength", /*nbArgs=*/1);
}