[flang-commits] [flang] c32421c - [fir] Add fir derived type runtime builder

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Fri Dec 3 05:52:20 PST 2021


Author: Valentin Clement
Date: 2021-12-03T14:51:59+01:00
New Revision: c32421c925131da39f028d3ea39a6cc5e9e1b365

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

LOG: [fir] Add fir derived type runtime builder

This patch adds the builder to generate derived type runtime API calls.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: rovka

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

Co-authored-by: Peter Klausler <pklausler at nvidia.com>
Co-authored-by: Jean Perier <jperier at nvidia.com>

Added: 
    flang/include/flang/Optimizer/Builder/Runtime/Derived.h
    flang/lib/Optimizer/Builder/Runtime/Derived.cpp
    flang/unittests/Optimizer/Builder/Runtime/DerivedTest.cpp

Modified: 
    flang/lib/Optimizer/Builder/CMakeLists.txt
    flang/unittests/Optimizer/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Builder/Runtime/Derived.h b/flang/include/flang/Optimizer/Builder/Runtime/Derived.h
new file mode 100644
index 0000000000000..a5e1083be3acc
--- /dev/null
+++ b/flang/include/flang/Optimizer/Builder/Runtime/Derived.h
@@ -0,0 +1,34 @@
+//===-- Derived.h - generate derived type runtime API calls -*- 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 FORTRAN_OPTIMIZER_BUILDER_RUNTIME_DERIVED_H
+#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_DERIVED_H
+
+namespace mlir {
+class Value;
+class Location;
+} // namespace mlir
+
+namespace fir {
+class FirOpBuilder;
+}
+
+namespace fir::runtime {
+
+/// Generate call to derived type initialization runtime routine to
+/// default initialize \p box.
+void genDerivedTypeInitialize(fir::FirOpBuilder &builder, mlir::Location loc,
+                              mlir::Value box);
+
+/// Generate call to derived type destruction runtime routine to
+/// destroy \p box.
+void genDerivedTypeDestroy(fir::FirOpBuilder &builder, mlir::Location loc,
+                           mlir::Value box);
+
+} // namespace fir::runtime
+#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_DERIVED_H

diff  --git a/flang/lib/Optimizer/Builder/CMakeLists.txt b/flang/lib/Optimizer/Builder/CMakeLists.txt
index 4076c77b299bc..8855cffc47481 100644
--- a/flang/lib/Optimizer/Builder/CMakeLists.txt
+++ b/flang/lib/Optimizer/Builder/CMakeLists.txt
@@ -8,6 +8,7 @@ add_flang_library(FIRBuilder
   FIRBuilder.cpp
   MutableBox.cpp
   Runtime/Assign.cpp
+  Runtime/Derived.cpp
   Runtime/Numeric.cpp
   Runtime/Reduction.cpp
   Runtime/Transformational.cpp

diff  --git a/flang/lib/Optimizer/Builder/Runtime/Derived.cpp b/flang/lib/Optimizer/Builder/Runtime/Derived.cpp
new file mode 100644
index 0000000000000..c5c67984db221
--- /dev/null
+++ b/flang/lib/Optimizer/Builder/Runtime/Derived.cpp
@@ -0,0 +1,35 @@
+//===-- Derived.cpp -- derived type runtime API ---------------------------===//
+//
+// 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/Derived.h"
+#include "flang/Optimizer/Builder/FIRBuilder.h"
+#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
+#include "flang/Runtime/derived-api.h"
+
+using namespace Fortran::runtime;
+
+void fir::runtime::genDerivedTypeInitialize(fir::FirOpBuilder &builder,
+                                            mlir::Location loc,
+                                            mlir::Value box) {
+  auto func = fir::runtime::getRuntimeFunc<mkRTKey(Initialize)>(loc, builder);
+  auto fTy = func.getType();
+  auto sourceFile = fir::factory::locationToFilename(builder, loc);
+  auto sourceLine =
+      fir::factory::locationToLineNo(builder, loc, fTy.getInput(2));
+  auto args = fir::runtime::createArguments(builder, loc, fTy, box, sourceFile,
+                                            sourceLine);
+  builder.create<fir::CallOp>(loc, func, args);
+}
+
+void fir::runtime::genDerivedTypeDestroy(fir::FirOpBuilder &builder,
+                                         mlir::Location loc, mlir::Value box) {
+  auto func = fir::runtime::getRuntimeFunc<mkRTKey(Destroy)>(loc, builder);
+  auto fTy = func.getType();
+  auto args = fir::runtime::createArguments(builder, loc, fTy, box);
+  builder.create<fir::CallOp>(loc, func, args);
+}

diff  --git a/flang/unittests/Optimizer/Builder/Runtime/DerivedTest.cpp b/flang/unittests/Optimizer/Builder/Runtime/DerivedTest.cpp
new file mode 100644
index 0000000000000..d5654523c9e29
--- /dev/null
+++ b/flang/unittests/Optimizer/Builder/Runtime/DerivedTest.cpp
@@ -0,0 +1,29 @@
+//===- DerivedTest.cpp -- Derived type 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/Derived.h"
+#include "RuntimeCallTestBase.h"
+#include "gtest/gtest.h"
+
+TEST_F(RuntimeCallTest, genDerivedTypeInitialize) {
+  auto loc = firBuilder->getUnknownLoc();
+  mlir::Type seqTy =
+      fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
+  mlir::Value box = firBuilder->create<fir::UndefOp>(loc, seqTy);
+  fir::runtime::genDerivedTypeInitialize(*firBuilder, loc, box);
+  checkCallOpFromResultBox(box, "_FortranAInitialize", 1);
+}
+
+TEST_F(RuntimeCallTest, genDerivedTypeDestroy) {
+  auto loc = firBuilder->getUnknownLoc();
+  mlir::Type seqTy =
+      fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
+  mlir::Value box = firBuilder->create<fir::UndefOp>(loc, seqTy);
+  fir::runtime::genDerivedTypeDestroy(*firBuilder, loc, box);
+  checkCallOpFromResultBox(box, "_FortranADestroy", 1, /*addLocArg=*/false);
+}

diff  --git a/flang/unittests/Optimizer/CMakeLists.txt b/flang/unittests/Optimizer/CMakeLists.txt
index 2f0f9c99fe3dc..3b44fe763ca02 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/DerivedTest.cpp
   Builder/Runtime/NumericTest.cpp
   Builder/Runtime/ReductionTest.cpp
   Builder/Runtime/TransformationalTest.cpp


        


More information about the flang-commits mailing list