[flang-commits] [flang] [flang] Fix incorrect offset for zero-size COMMON block members. (PR #214174)

Daniel Chen via flang-commits flang-commits at lists.llvm.org
Wed Aug 5 16:55:44 PDT 2026


https://github.com/DanielCChen updated https://github.com/llvm/llvm-project/pull/214174

>From 0f68b553de950a60801cd744507e3a0ef5a96e80 Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Wed, 5 Aug 2026 05:42:47 -0400
Subject: [PATCH 1/2] [flang] Fix incorrect offset for zero-size COMMON block
 members.

---
 flang/lib/Semantics/compute-offsets.cpp |  6 +++++
 flang/test/Lower/common-block-char0.f90 | 36 +++++++++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 flang/test/Lower/common-block-char0.f90

diff --git a/flang/lib/Semantics/compute-offsets.cpp b/flang/lib/Semantics/compute-offsets.cpp
index e427586301dea..3a355f040b865 100644
--- a/flang/lib/Semantics/compute-offsets.cpp
+++ b/flang/lib/Semantics/compute-offsets.cpp
@@ -397,6 +397,12 @@ std::size_t ComputeOffsetsHelper::DoSymbol(
   }
   SizeAndAlignment s{GetSizeAndAlignment(symbol, true)};
   if (s.size == 0) {
+    // Zero-size symbols (e.g. CHARACTER*0) still occupy their sequential
+    // position in a COMMON block or derived-type sequence. Record the current
+    // offset so that LOC() and storage-association checks see the correct
+    // address rather than always returning the block base (offset 0).
+    symbol.set_size(0);
+    symbol.set_offset(offset_);
     return 0;
   }
   std::size_t previousOffset{offset_};
diff --git a/flang/test/Lower/common-block-char0.f90 b/flang/test/Lower/common-block-char0.f90
new file mode 100644
index 0000000000000..964d56f483f7c
--- /dev/null
+++ b/flang/test/Lower/common-block-char0.f90
@@ -0,0 +1,36 @@
+! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
+
+! Test that CHARACTER*0 symbols in a COMMON block are assigned their correct
+! sequential offset rather than always offset 0 (the block base address).
+!
+! Block /blk/ layout:
+!   offset  0 : ii1  (integer(8), 8 bytes)
+!   offset  8 : zc0  (character*0, 0 bytes)  <- zero-size, no storage consumed
+!   offset  8 : ll1  (integer(8), 8 bytes)
+!   Total: 16 bytes
+!
+! Before fix (compute-offsets.cpp): DoSymbol() returned early for zero-size
+! symbols without calling symbol.set_offset(), leaving zc0 stamped with
+! offset 0. This caused storage(%1[0]) for zc0 instead of storage(%1[8]).
+
+subroutine char0_common
+  integer(8) :: ii1
+  character*0 :: zc0
+  integer(8) :: ll1
+  common /blk/ ii1, zc0, ll1
+  call use(ii1, zc0, ll1)
+end subroutine
+
+! CHECK: fir.global common @blk_(dense<0> : vector<16xi8>) {alignment = 8 : i64} : !fir.array<16xi8>
+
+! CHECK-LABEL: func.func @_QPchar0_common
+
+! ii1 at offset 0
+! CHECK: %[[BASE:.*]] = fir.address_of(@blk_) : !fir.ref<!fir.array<16xi8>>
+! CHECK: hlfir.declare {{.*}} storage(%[[BASE]][0]) {uniq_name = "_QFchar0_commonEii1"}
+
+! ll1 at offset 8
+! CHECK: hlfir.declare {{.*}} storage(%[[BASE]][8]) {uniq_name = "_QFchar0_commonEll1"}
+
+! zc0 at offset 8 (not 0) -- key assertion
+! CHECK: hlfir.declare {{.*}} storage(%[[BASE]][8]) {uniq_name = "_QFchar0_commonEzc0"}

>From 00f437fdab973dd97e9d8e9e37134bc38d4c0aa1 Mon Sep 17 00:00:00 2001
From: Daniel Chen <cdchen at ca.ibm.com>
Date: Wed, 5 Aug 2026 19:54:54 -0400
Subject: [PATCH 2/2] To add another test to address review comment.

---
 flang/test/Lower/common-block-char0.f90 | 26 ++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/flang/test/Lower/common-block-char0.f90 b/flang/test/Lower/common-block-char0.f90
index 964d56f483f7c..d0bdc3f0d72c1 100644
--- a/flang/test/Lower/common-block-char0.f90
+++ b/flang/test/Lower/common-block-char0.f90
@@ -1,4 +1,4 @@
-! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
+! RUN: %flang_fc1 -emit-hlfir -o - %s | FileCheck %s
 
 ! Test that CHARACTER*0 symbols in a COMMON block are assigned their correct
 ! sequential offset rather than always offset 0 (the block base address).
@@ -34,3 +34,27 @@ subroutine char0_common
 
 ! zc0 at offset 8 (not 0) -- key assertion
 ! CHECK: hlfir.declare {{.*}} storage(%[[BASE]][8]) {uniq_name = "_QFchar0_commonEzc0"}
+
+! Test that a zero-size CHARACTER in a COMMON block does not trigger a false
+! "cannot backward-extend COMMON block" error when it is referenced in an
+! EQUIVALENCE association.
+!
+! Block /blk/ layout:
+!   offset  0 : i8   (integer(8), 8 bytes)
+!   offset  8 : zc0  (character(0), 0 bytes)
+!
+! equivalence (c8(5:8), zc0) places c8(1) at offset 4 (= 8 - 4 bytes before zc0).
+! Before the fix, zc0 had offset 0, so dep.offset (4) > symbol.offset() (0)
+! triggered the backward-extend error falsely.  After the fix, zc0.offset() == 8
+! so no backward extension occurs.
+!
+! CHECK-LABEL: func.func @_QPp09
+
+subroutine p09
+  integer(8) :: i8
+  character(0) :: zc0
+  character(8) :: c8
+  common /blk/ i8, zc0
+  equivalence (c8(5:8), zc0)
+  call use(c8, i8)
+end subroutine



More information about the flang-commits mailing list