[flang-commits] [flang] 38073b0 - [flang] Upstream partial lowering of GET_COMMAND_ARGUMENT intrinsic
Josh Mottley via flang-commits
flang-commits at lists.llvm.org
Fri Feb 4 02:25:09 PST 2022
Author: Josh Mottley
Date: 2022-02-04T10:25:01Z
New Revision: 38073b066973e42c4ba833aaeb04389ba33bf5d7
URL: https://github.com/llvm/llvm-project/commit/38073b066973e42c4ba833aaeb04389ba33bf5d7
DIFF: https://github.com/llvm/llvm-project/commit/38073b066973e42c4ba833aaeb04389ba33bf5d7.diff
LOG: [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
Added:
Modified:
flang/include/flang/Optimizer/Builder/Runtime/Command.h
flang/lib/Optimizer/Builder/Runtime/Command.cpp
flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Builder/Runtime/Command.h b/flang/include/flang/Optimizer/Builder/Runtime/Command.h
index 253a89ebf0790..fd6332b785eeb 100644
--- a/flang/include/flang/Optimizer/Builder/Runtime/Command.h
+++ b/flang/include/flang/Optimizer/Builder/Runtime/Command.h
@@ -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
diff --git a/flang/lib/Optimizer/Builder/Runtime/Command.cpp b/flang/lib/Optimizer/Builder/Runtime/Command.cpp
index 9707c43a5f1e9..e3fc363be9b04 100644
--- a/flang/lib/Optimizer/Builder/Runtime/Command.cpp
+++ b/flang/lib/Optimizer/Builder/Runtime/Command.cpp
@@ -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);
+ }
+}
diff --git a/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp b/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
index 00f2951e0bd9e..1b1978950e19b 100644
--- a/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
+++ b/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
@@ -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);
+}
More information about the flang-commits
mailing list