[llvm] [flang] Don't associate pointers with zero sized storage targets (PR #155867)

Eugene Epshteyn via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 28 13:05:55 PDT 2025


https://github.com/eugeneepshteyn updated https://github.com/llvm/llvm-project/pull/155867

>From 96e80af46f4cba147b3c3297cdcc138ffcafb775 Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 28 Aug 2025 11:54:13 -0400
Subject: [PATCH 1/4] [flang] Don't associate pointers with zero sized storage
 targets

Fixes #155481
---
 flang-rt/lib/runtime/pointer.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/flang-rt/lib/runtime/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
index 68db2594acdd4..4b1b921b1ce07 100644
--- a/flang-rt/lib/runtime/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -67,8 +67,12 @@ void RTDEF(PointerAssociateScalar)(Descriptor &pointer, void *target) {
 }
 
 void RTDEF(PointerAssociate)(Descriptor &pointer, const Descriptor &target) {
-  pointer = target;
-  pointer.raw().attribute = CFI_attribute_pointer;
+  if (target.ElementBytes() > 0) {
+    // F2023, 16.9.20, p5, case (v)-(vi): don't associate pointers with
+    // targets that have zero sized storage sequence.
+    pointer = target;
+    pointer.raw().attribute = CFI_attribute_pointer;
+  }
 }
 
 void RTDEF(PointerAssociateLowerBounds)(Descriptor &pointer,

>From 2b671c302fec74efdb52537797dc67519715fc8b Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 28 Aug 2025 13:51:11 -0400
Subject: [PATCH 2/4] Moved the check to PointerIsAssociatedWith()

---
 flang-rt/lib/runtime/pointer.cpp | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/flang-rt/lib/runtime/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
index 4b1b921b1ce07..f6c338f464d82 100644
--- a/flang-rt/lib/runtime/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -67,12 +67,8 @@ void RTDEF(PointerAssociateScalar)(Descriptor &pointer, void *target) {
 }
 
 void RTDEF(PointerAssociate)(Descriptor &pointer, const Descriptor &target) {
-  if (target.ElementBytes() > 0) {
-    // F2023, 16.9.20, p5, case (v)-(vi): don't associate pointers with
-    // targets that have zero sized storage sequence.
-    pointer = target;
-    pointer.raw().attribute = CFI_attribute_pointer;
-  }
+  pointer = target;
+  pointer.raw().attribute = CFI_attribute_pointer;
 }
 
 void RTDEF(PointerAssociateLowerBounds)(Descriptor &pointer,
@@ -271,6 +267,11 @@ bool RTDEF(PointerIsAssociatedWith)(
   if (!target) {
     return pointer.raw().base_addr != nullptr;
   }
+  if (target->ElementBytes() == 0) {
+    // F2023, 16.9.20, p5, case (v)-(vi): don't associate pointers with
+    // targets that have zero sized storage sequence.
+    return false;
+  }
   if (!target->raw().base_addr ||
       (target->raw().type != CFI_type_struct && target->ElementBytes() == 0)) {
     return false;

>From 60addf833cdb77d31ead68e334b8cab584b7f5da Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 28 Aug 2025 14:52:44 -0400
Subject: [PATCH 3/4] Simplify

---
 flang-rt/lib/runtime/pointer.cpp | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/flang-rt/lib/runtime/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
index f6c338f464d82..caa8c2429ebeb 100644
--- a/flang-rt/lib/runtime/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -267,15 +267,11 @@ bool RTDEF(PointerIsAssociatedWith)(
   if (!target) {
     return pointer.raw().base_addr != nullptr;
   }
-  if (target->ElementBytes() == 0) {
+  if (!target->raw().base_addr || target->ElementBytes() == 0) {
     // F2023, 16.9.20, p5, case (v)-(vi): don't associate pointers with
     // targets that have zero sized storage sequence.
     return false;
   }
-  if (!target->raw().base_addr ||
-      (target->raw().type != CFI_type_struct && target->ElementBytes() == 0)) {
-    return false;
-  }
   int rank{pointer.rank()};
   if (pointer.raw().base_addr != target->raw().base_addr ||
       pointer.ElementBytes() != target->ElementBytes() ||

>From eff8a3325cd3c66753915f7f7f5385fe98bca9ac Mon Sep 17 00:00:00 2001
From: Eugene Epshteyn <eepshteyn at nvidia.com>
Date: Thu, 28 Aug 2025 15:57:16 -0400
Subject: [PATCH 4/4] Also check for array with no elements

---
 flang-rt/lib/runtime/pointer.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/flang-rt/lib/runtime/pointer.cpp b/flang-rt/lib/runtime/pointer.cpp
index caa8c2429ebeb..94702a24c13f9 100644
--- a/flang-rt/lib/runtime/pointer.cpp
+++ b/flang-rt/lib/runtime/pointer.cpp
@@ -267,7 +267,8 @@ bool RTDEF(PointerIsAssociatedWith)(
   if (!target) {
     return pointer.raw().base_addr != nullptr;
   }
-  if (!target->raw().base_addr || target->ElementBytes() == 0) {
+  if (!target->raw().base_addr || target->ElementBytes() == 0
+      || target->Elements() == 0) {
     // F2023, 16.9.20, p5, case (v)-(vi): don't associate pointers with
     // targets that have zero sized storage sequence.
     return false;



More information about the llvm-commits mailing list