[flang-commits] [flang] ddac11a - [flang] Upstream partial lowering of COMMAND_ARGUMENT_COUNT intrinsic

Josh Mottley via flang-commits flang-commits at lists.llvm.org
Mon Jan 31 09:01:49 PST 2022


Author: Josh Mottley
Date: 2022-01-31T17:01:26Z
New Revision: ddac11aee6494cc91994d5205afd62e2999d3e8c

URL: https://github.com/llvm/llvm-project/commit/ddac11aee6494cc91994d5205afd62e2999d3e8c
DIFF: https://github.com/llvm/llvm-project/commit/ddac11aee6494cc91994d5205afd62e2999d3e8c.diff

LOG: [flang] Upstream partial lowering of COMMAND_ARGUMENT_COUNT intrinsic

This patch adds partial lowering of the "COMMAND_ARGUMENT_COUNT" intrinsic
to the backend runtime hook implemented in patch D109048. Also adds a
"helper" function for retrieving the default integer type from
FIRBuilder, which will be used later when finishing the lowering of
intrinsic.

Differential Revision: https://reviews.llvm.org/D117869

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

Modified: 
    flang/include/flang/Optimizer/Builder/FIRBuilder.h
    flang/lib/Optimizer/Builder/CMakeLists.txt
    flang/unittests/Optimizer/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
index ac2ed279205e8..a4de869ec0a0b 100644
--- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
@@ -57,6 +57,12 @@ class FirOpBuilder : public mlir::OpBuilder {
   /// Get a reference to the kind map.
   const fir::KindMapping &getKindMap() { return kindMap; }
 
+  /// Get the default integer type
+  [[maybe_unused]] mlir::IntegerType getDefaultIntegerType() {
+    return getIntegerType(
+        getKindMap().getIntegerBitsize(getKindMap().defaultIntegerKind()));
+  }
+
   /// The LHS and RHS are not always in agreement in terms of
   /// type. In some cases, the disagreement is between COMPLEX and other scalar
   /// types. In that case, the conversion must insert/extract out of a COMPLEX

diff  --git a/flang/include/flang/Optimizer/Builder/Runtime/Command.h b/flang/include/flang/Optimizer/Builder/Runtime/Command.h
new file mode 100644
index 0000000000000..253a89ebf0790
--- /dev/null
+++ b/flang/include/flang/Optimizer/Builder/Runtime/Command.h
@@ -0,0 +1,27 @@
+//===-- Command.cpp -- generate command line runtime API calls ------------===//
+//
+// 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 FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H
+#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H
+
+namespace mlir {
+class Value;
+class Location;
+} // namespace mlir
+
+namespace fir {
+class FirOpBuilder;
+}
+
+namespace fir::runtime {
+
+/// Generate call to COMMAND_ARGUMENT_COUNT intrinsic runtime routine.
+mlir::Value genCommandArgumentCount(fir::FirOpBuilder &, mlir::Location);
+
+} // namespace fir::runtime
+#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H

diff  --git a/flang/lib/Optimizer/Builder/CMakeLists.txt b/flang/lib/Optimizer/Builder/CMakeLists.txt
index a7dc8de20e46f..c8f8da549d116 100644
--- a/flang/lib/Optimizer/Builder/CMakeLists.txt
+++ b/flang/lib/Optimizer/Builder/CMakeLists.txt
@@ -9,6 +9,7 @@ add_flang_library(FIRBuilder
   MutableBox.cpp
   Runtime/Assign.cpp
   Runtime/Character.cpp
+  Runtime/Command.cpp
   Runtime/Derived.cpp
   Runtime/Numeric.cpp
   Runtime/Ragged.cpp

diff  --git a/flang/lib/Optimizer/Builder/Runtime/Command.cpp b/flang/lib/Optimizer/Builder/Runtime/Command.cpp
new file mode 100644
index 0000000000000..9707c43a5f1e9
--- /dev/null
+++ b/flang/lib/Optimizer/Builder/Runtime/Command.cpp
@@ -0,0 +1,21 @@
+//===-- Command.cpp -- generate command line runtime API calls ------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "flang/Optimizer/Builder/Runtime/Command.h"
+#include "flang/Optimizer/Builder/FIRBuilder.h"
+#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
+#include "flang/Runtime/command.h"
+
+using namespace Fortran::runtime;
+
+mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder,
+                                                  mlir::Location loc) {
+  auto argumentCountFunc =
+      fir::runtime::getRuntimeFunc<mkRTKey(ArgumentCount)>(loc, builder);
+  return builder.create<fir::CallOp>(loc, argumentCountFunc).getResult(0);
+}

diff  --git a/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp b/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
new file mode 100644
index 0000000000000..00f2951e0bd9e
--- /dev/null
+++ b/flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp
@@ -0,0 +1,18 @@
+//===- CommandTest.cpp -- command line runtime builder unit tests ---------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "flang/Optimizer/Builder/Runtime/Command.h"
+#include "RuntimeCallTestBase.h"
+#include "gtest/gtest.h"
+
+TEST_F(RuntimeCallTest, genCommandArgumentCountTest) {
+  mlir::Location loc = firBuilder->getUnknownLoc();
+  mlir::Value result = fir::runtime::genCommandArgumentCount(*firBuilder, loc);
+  checkCallOp(result.getDefiningOp(), "_FortranAArgumentCount", /*nbArgs=*/0,
+      /*addLocArgs=*/false);
+}

diff  --git a/flang/unittests/Optimizer/CMakeLists.txt b/flang/unittests/Optimizer/CMakeLists.txt
index 4edb24ebb93c3..62a130c82e16b 100644
--- a/flang/unittests/Optimizer/CMakeLists.txt
+++ b/flang/unittests/Optimizer/CMakeLists.txt
@@ -14,6 +14,7 @@ add_flang_unittest(FlangOptimizerTests
   Builder/DoLoopHelperTest.cpp
   Builder/FIRBuilderTest.cpp
   Builder/Runtime/AssignTest.cpp
+  Builder/Runtime/CommandTest.cpp
   Builder/Runtime/CharacterTest.cpp
   Builder/Runtime/DerivedTest.cpp
   Builder/Runtime/NumericTest.cpp


        


More information about the flang-commits mailing list