[flang] [llvm] [flang-rt] Add Assign_omp RT call. (PR #145465)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 26 02:56:59 PDT 2025


https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/145465

>From b02fee6bffe8639be23892ae620d390a6a72bc9c Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Mon, 23 Jun 2025 15:15:01 +0530
Subject: [PATCH 1/3] [flang-rt] Add Assign_omp RT call.

---
 flang-rt/lib/runtime/CMakeLists.txt |  2 +
 flang-rt/lib/runtime/assign_omp.cpp | 76 +++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)
 create mode 100644 flang-rt/lib/runtime/assign_omp.cpp

diff --git a/flang-rt/lib/runtime/CMakeLists.txt b/flang-rt/lib/runtime/CMakeLists.txt
index 332c0872e065f..5200b2b710a5e 100644
--- a/flang-rt/lib/runtime/CMakeLists.txt
+++ b/flang-rt/lib/runtime/CMakeLists.txt
@@ -21,6 +21,7 @@ set(supported_sources
   allocatable.cpp
   array-constructor.cpp
   assign.cpp
+  assign_omp.cpp
   buffer.cpp
   character.cpp
   connection.cpp
@@ -99,6 +100,7 @@ set(gpu_sources
   allocatable.cpp
   array-constructor.cpp
   assign.cpp
+  assign_omp.cpp
   buffer.cpp
   character.cpp
   connection.cpp
diff --git a/flang-rt/lib/runtime/assign_omp.cpp b/flang-rt/lib/runtime/assign_omp.cpp
new file mode 100644
index 0000000000000..a214afea11380
--- /dev/null
+++ b/flang-rt/lib/runtime/assign_omp.cpp
@@ -0,0 +1,76 @@
+//===-- lib/runtime/assign_omp.cpp ----------------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#include "flang/Runtime/assign.h"
+#include "flang-rt/runtime/assign-impl.h"
+#include "flang-rt/runtime/derived.h"
+#include "flang-rt/runtime/descriptor.h"
+#include "flang-rt/runtime/stat.h"
+#include "flang-rt/runtime/terminator.h"
+#include "flang-rt/runtime/tools.h"
+#include "flang-rt/runtime/type-info.h"
+
+#include <omp.h>
+
+namespace Fortran::runtime {
+namespace omp {
+
+typedef int32_t OMPDeviceTy;
+
+template <typename T> static T *getDevicePtr(T *anyPtr, OMPDeviceTy ompDevice) {
+  auto voidAnyPtr = reinterpret_cast<void *>(anyPtr);
+  // If not present on the device it should already be a device ptr
+  if (!omp_target_is_present(voidAnyPtr, ompDevice))
+    return anyPtr;
+  T *device_ptr = omp_get_mapped_ptr(anyPtr, ompDevice);
+  return device_ptr;
+}
+
+RT_API_ATTRS static void Assign(Descriptor &to, const Descriptor &from,
+    Terminator &terminator, int flags, OMPDeviceTy omp_device) {
+  std::size_t toElementBytes{to.ElementBytes()};
+  std::size_t fromElementBytes{from.ElementBytes()};
+  std::size_t toElements{to.Elements()};
+  std::size_t fromElements{from.Elements()};
+
+  if (toElementBytes != fromElementBytes)
+    terminator.Crash("Assign: toElementBytes != fromElementBytes");
+  if (toElements != fromElements)
+    terminator.Crash("Assign: toElements != fromElements");
+
+  // Get base addresses and calculate length
+  void *to_base = to.raw().base_addr;
+  void *from_base = from.raw().base_addr;
+  size_t length = toElements * toElementBytes;
+
+  // Get device pointers after ensuring data is on device
+  void *to_ptr = getDevicePtr(to_base, omp_device);
+  void *from_ptr = getDevicePtr(from_base, omp_device);
+
+  // Perform copy between device pointers
+  int result = omp_target_memcpy(to_ptr, from_ptr, length,
+      /*dst_offset*/ 0, /*src_offset*/ 0, omp_device, omp_device);
+
+  if (result != 0)
+    terminator.Crash("Assign: omp_target_memcpy failed");
+  return;
+}
+
+extern "C" {
+RT_EXT_API_GROUP_BEGIN
+void RTDEF(Assign_omp)(Descriptor &to, const Descriptor &from,
+    const char *sourceFile, int sourceLine, omp::OMPDeviceTy omp_device) {
+  Terminator terminator{sourceFile, sourceLine};
+  Fortran::runtime::omp::Assign(to, from, terminator,
+      MaybeReallocate | NeedFinalization | ComponentCanBeDefinedAssignment,
+      omp_device);
+}
+
+} // extern "C"
+} // namespace omp
+} // namespace Fortran::runtime

>From 2964b15537fef8355cdd5c38cca8e7a8cc97b229 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Thu, 26 Jun 2025 14:10:54 +0530
Subject: [PATCH 2/3] [flang-rt] clang format

---
 flang-rt/lib/runtime/assign_omp.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/flang-rt/lib/runtime/assign_omp.cpp b/flang-rt/lib/runtime/assign_omp.cpp
index a214afea11380..10eb22a2650e1 100644
--- a/flang-rt/lib/runtime/assign_omp.cpp
+++ b/flang-rt/lib/runtime/assign_omp.cpp
@@ -1,4 +1,5 @@
-//===-- lib/runtime/assign_omp.cpp ----------------------------------*- C++ -*-===//
+//===-- lib/runtime/assign_omp.cpp ----------------------------------*- C++
+//-*-===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -6,7 +7,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "flang/Runtime/assign.h"
 #include "flang-rt/runtime/assign-impl.h"
 #include "flang-rt/runtime/derived.h"
 #include "flang-rt/runtime/descriptor.h"
@@ -14,6 +14,7 @@
 #include "flang-rt/runtime/terminator.h"
 #include "flang-rt/runtime/tools.h"
 #include "flang-rt/runtime/type-info.h"
+#include "flang/Runtime/assign.h"
 
 #include <omp.h>
 

>From d89abc2613835a634c46c70876f1148fd175fdc5 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Thu, 26 Jun 2025 15:25:38 +0530
Subject: [PATCH 3/3] [flang] Add Assign_omp declaratioon

---
 flang/include/flang/Runtime/assign.h | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang/include/flang/Runtime/assign.h b/flang/include/flang/Runtime/assign.h
index 7d198bdcc9e89..0be52413e4814 100644
--- a/flang/include/flang/Runtime/assign.h
+++ b/flang/include/flang/Runtime/assign.h
@@ -56,6 +56,8 @@ extern "C" {
 // API for lowering assignment
 void RTDECL(Assign)(Descriptor &to, const Descriptor &from,
     const char *sourceFile = nullptr, int sourceLine = 0);
+void RTDECL(Assign_omp)(Descriptor &to, const Descriptor &from,
+    const char *sourceFile = nullptr, int sourceLine = 0, int32_t omp_device = 0);
 // This variant has no finalization, defined assignment, or allocatable
 // reallocation.
 void RTDECL(AssignTemporary)(Descriptor &to, const Descriptor &from,



More information about the llvm-commits mailing list