[flang] [llvm] [flang-rt] Add APIs to retrive base_addr and DataSizeInBytes from Descriptor. (PR #152756)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 8 09:35:44 PDT 2025


https://github.com/skc7 created https://github.com/llvm/llvm-project/pull/152756

This PR adds below APIs to flang-rt:
DescriptorGetBaseAddress to retrive base_addr from Descriptor
DescriptorGetDataSizeInBytes to retrive the total Size in bytes of data from Descriptor.

>From e5a205afbcc4c5127b2b0907b565f0d7f45327a6 Mon Sep 17 00:00:00 2001
From: skc7 <Krishna.Sankisa at amd.com>
Date: Fri, 8 Aug 2025 16:48:13 +0530
Subject: [PATCH] [flang-rt] Add APIs to retrive base_addr and DataSizeInBytes
 from Descriptor.

---
 flang-rt/lib/runtime/support.cpp       | 24 ++++++++++++++++++++++++
 flang-rt/unittests/Runtime/Support.cpp | 23 +++++++++++++++++++++++
 flang/include/flang/Runtime/support.h  |  8 ++++++++
 3 files changed, 55 insertions(+)

diff --git a/flang-rt/lib/runtime/support.cpp b/flang-rt/lib/runtime/support.cpp
index 9beb46e48a11e..ffeaafaa162ea 100644
--- a/flang-rt/lib/runtime/support.cpp
+++ b/flang-rt/lib/runtime/support.cpp
@@ -48,6 +48,30 @@ void RTDEF(CopyAndUpdateDescriptor)(Descriptor &to, const Descriptor &from,
   }
 }
 
+void *RTDEF(DescriptorGetBaseAddress)(
+    const Descriptor &desc, const char *sourceFile, int sourceLine) {
+  Terminator terminator{sourceFile, sourceLine};
+  void *baseAddr = desc.raw().base_addr;
+  if (!baseAddr) {
+    terminator.Crash("Could not retrieve Descriptor's base address");
+  }
+  return baseAddr;
+}
+
+std::size_t RTDEF(DescriptorGetDataSizeInBytes)(
+    const Descriptor &desc, const char *sourceFile, int sourceLine) {
+  Terminator terminator{sourceFile, sourceLine};
+  std::size_t descElements{desc.Elements()};
+  if (!descElements) {
+    terminator.Crash("Could not retrieve Descriptor's Elements");
+  }
+  std::size_t descElementBytes{desc.ElementBytes()};
+  if (!descElementBytes) {
+    terminator.Crash("Could not retrieve Descriptor's ElementBytes");
+  }
+  return descElements * descElementBytes;
+}
+
 RT_EXT_API_GROUP_END
 } // extern "C"
 } // namespace Fortran::runtime
diff --git a/flang-rt/unittests/Runtime/Support.cpp b/flang-rt/unittests/Runtime/Support.cpp
index 46c6805d5d238..264dde872c242 100644
--- a/flang-rt/unittests/Runtime/Support.cpp
+++ b/flang-rt/unittests/Runtime/Support.cpp
@@ -98,3 +98,26 @@ TEST(IsContiguous, Basic) {
   EXPECT_TRUE(RTNAME(IsContiguousUpTo)(section, 1));
   EXPECT_FALSE(RTNAME(IsContiguousUpTo)(section, 2));
 }
+
+TEST(DescriptorGetBaseAddress, Basic) {
+  auto array{MakeArray<TypeCategory::Integer, 4>(
+      std::vector<int>{2, 3}, std::vector<std::int32_t>{0, 1, 2, 3, 4, 5})};
+  void *baseAddr = RTNAME(DescriptorGetBaseAddress)(*array);
+  EXPECT_NE(baseAddr, nullptr);
+  EXPECT_EQ(baseAddr, array->raw().base_addr);
+}
+
+TEST(DescriptorGetDataSizeInBytes, Basic) {
+  // Test with a 2x3 integer*4 array
+  auto int4Array{MakeArray<TypeCategory::Integer, 4>({2, 3})};
+  EXPECT_EQ(RTNAME(DescriptorGetDataSizeInBytes)(*int4Array),
+      6 * sizeof(std::int32_t));
+  // Test with a 1D, 5-element real*8 array
+  auto real8Array{MakeArray<TypeCategory::Real, 8>({5})};
+  EXPECT_EQ(
+      RTNAME(DescriptorGetDataSizeInBytes)(*real8Array), 5 * sizeof(double));
+  // Test with a scalar logical*1
+  auto logical1Scalar{MakeArray<TypeCategory::Logical, 1>({})};
+  EXPECT_EQ(
+      RTNAME(DescriptorGetDataSizeInBytes)(*logical1Scalar), 1 * sizeof(bool));
+}
diff --git a/flang/include/flang/Runtime/support.h b/flang/include/flang/Runtime/support.h
index 8a345bee7f867..5ebe6c6406a01 100644
--- a/flang/include/flang/Runtime/support.h
+++ b/flang/include/flang/Runtime/support.h
@@ -49,6 +49,14 @@ void RTDECL(CopyAndUpdateDescriptor)(Descriptor &to, const Descriptor &from,
     const typeInfo::DerivedType *newDynamicType,
     ISO::CFI_attribute_t newAttribute, enum LowerBoundModifier newLowerBounds);
 
+// Retrieve the base_addr from Descriptor
+void *RTDECL(DescriptorGetBaseAddress)(const Descriptor &desc,
+    const char *sourceFile = nullptr, int sourceLine = 0);
+
+// Retrieve the totalSizeInBytes of data from Descriptor
+std::size_t RTDECL(DescriptorGetDataSizeInBytes)(const Descriptor &desc,
+    const char *sourceFile = nullptr, int sourceLine = 0);
+
 } // extern "C"
 } // namespace Fortran::runtime
 #endif // FORTRAN_RUNTIME_SUPPORT_H_



More information about the llvm-commits mailing list