[flang] Upstream partial lowering of GET_COMMAND_ARGUMENT intrinsic
authorJosh Mottley <Josh.Mottley@arm.com>
Thu, 6 Jan 2022 14:55:33 +0000 (14:55 +0000)
committerJosh Mottley <Josh.Mottley@arm.com>
Fri, 4 Feb 2022 10:25:01 +0000 (10:25 +0000)
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

flang/include/flang/Optimizer/Builder/Runtime/Command.h
flang/lib/Optimizer/Builder/Runtime/Command.cpp
flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp

index 253a89e..fd6332b 100644 (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
index 9707c43..e3fc363 100644 (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);
+  }
+}
index 00f2951..1b19789 100644 (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);
+}