[flang-commits] [flang] 51e2c8c - [fir] Add assignment runtime API builder

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Mon Nov 29 01:23:27 PST 2021


Author: Valentin Clement
Date: 2021-11-29T10:23:06+01:00
New Revision: 51e2c8c9bf96f19a3160a7da36be26943318a2cb

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

LOG: [fir] Add assignment runtime API builder

This patch adds the builder that generate assignment runtime API calls.

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

Reviewed By: rovka, awarzynski

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

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

Added: 
    flang/include/flang/Optimizer/Builder/Runtime/Assign.h
    flang/lib/Optimizer/Builder/Runtime/Assign.cpp
    flang/unittests/Optimizer/Builder/Runtime/AssignTest.cpp

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

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Optimizer/Builder/Runtime/Assign.h b/flang/include/flang/Optimizer/Builder/Runtime/Assign.h
new file mode 100644
index 0000000000000..5c02d4e9b71df
--- /dev/null
+++ b/flang/include/flang/Optimizer/Builder/Runtime/Assign.h
@@ -0,0 +1,32 @@
+//===-- Assign.h - generate assignment 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_ASSIGN_H
+#define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_ASSIGN_H
+
+namespace mlir {
+class Value;
+class Location;
+} // namespace mlir
+
+namespace fir {
+class FirOpBuilder;
+}
+
+namespace fir::runtime {
+
+/// Generate runtime call to assign \p sourceBox to \p destBox.
+/// \p destBox must be a fir.ref<fir.box<T>> and \p sourceBox a fir.box<T>.
+/// \p destBox Fortran descriptor may be modified if destBox is an allocatable
+/// according to Fortran allocatable assignment rules, otherwise it is not
+/// modified.
+void genAssign(fir::FirOpBuilder &builder, mlir::Location loc,
+               mlir::Value destBox, mlir::Value sourceBox);
+
+} // namespace fir::runtime
+#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_ASSIGN_H

diff  --git a/flang/lib/Optimizer/Builder/CMakeLists.txt b/flang/lib/Optimizer/Builder/CMakeLists.txt
index 228423315d703..58f0e2877df44 100644
--- a/flang/lib/Optimizer/Builder/CMakeLists.txt
+++ b/flang/lib/Optimizer/Builder/CMakeLists.txt
@@ -7,6 +7,7 @@ add_flang_library(FIRBuilder
   DoLoopHelper.cpp
   FIRBuilder.cpp
   MutableBox.cpp
+  Runtime/Assign.cpp
   Runtime/Transformational.cpp
 
   DEPENDS

diff  --git a/flang/lib/Optimizer/Builder/Runtime/Assign.cpp b/flang/lib/Optimizer/Builder/Runtime/Assign.cpp
new file mode 100644
index 0000000000000..71345e5924b78
--- /dev/null
+++ b/flang/lib/Optimizer/Builder/Runtime/Assign.cpp
@@ -0,0 +1,26 @@
+//===-- Assign.cpp -- generate assignment 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/Assign.h"
+#include "flang/Optimizer/Builder/FIRBuilder.h"
+#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
+#include "flang/Runtime/assign.h"
+
+using namespace Fortran::runtime;
+
+void fir::runtime::genAssign(fir::FirOpBuilder &builder, mlir::Location loc,
+                             mlir::Value destBox, mlir::Value sourceBox) {
+  auto func = fir::runtime::getRuntimeFunc<mkRTKey(Assign)>(loc, builder);
+  auto fTy = func.getType();
+  auto sourceFile = fir::factory::locationToFilename(builder, loc);
+  auto sourceLine =
+      fir::factory::locationToLineNo(builder, loc, fTy.getInput(3));
+  auto args = fir::runtime::createArguments(builder, loc, fTy, destBox,
+                                            sourceBox, sourceFile, sourceLine);
+  builder.create<fir::CallOp>(loc, func, args);
+}

diff  --git a/flang/unittests/Optimizer/Builder/Runtime/AssignTest.cpp b/flang/unittests/Optimizer/Builder/Runtime/AssignTest.cpp
new file mode 100644
index 0000000000000..8bf7b1d266444
--- /dev/null
+++ b/flang/unittests/Optimizer/Builder/Runtime/AssignTest.cpp
@@ -0,0 +1,21 @@
+//===- AssignTest.cpp -- assignment 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/Assign.h"
+#include "RuntimeCallTestBase.h"
+#include "gtest/gtest.h"
+
+TEST_F(RuntimeCallTest, genDerivedTypeAssign) {
+  auto loc = firBuilder->getUnknownLoc();
+  mlir::Type seqTy =
+      fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
+  mlir::Value source = firBuilder->create<fir::UndefOp>(loc, seqTy);
+  mlir::Value dest = firBuilder->create<fir::UndefOp>(loc, seqTy);
+  fir::runtime::genAssign(*firBuilder, loc, dest, source);
+  checkCallOpFromResultBox(dest, "_FortranAAssign", 2);
+}

diff  --git a/flang/unittests/Optimizer/CMakeLists.txt b/flang/unittests/Optimizer/CMakeLists.txt
index d36dda71b9140..81983c27347a1 100644
--- a/flang/unittests/Optimizer/CMakeLists.txt
+++ b/flang/unittests/Optimizer/CMakeLists.txt
@@ -13,6 +13,7 @@ add_flang_unittest(FlangOptimizerTests
   Builder/ComplexTest.cpp
   Builder/DoLoopHelperTest.cpp
   Builder/FIRBuilderTest.cpp
+  Builder/Runtime/AssignTest.cpp
   Builder/Runtime/TransformationalTest.cpp
   FIRContextTest.cpp
   InternalNamesTest.cpp


        


More information about the flang-commits mailing list