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

via flang-commits flang-commits at lists.llvm.org
Wed Aug 5 02:55:57 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Daniel Chen (DanielCChen)

<details>
<summary>Changes</summary>

Fixes #<!-- -->214171 

`ComputeOffsetsHelper::DoSymbol()` in `flang/lib/Semantics/compute-offsets.cpp` returned early without calling `symbol.set_offset()` when a symbol had zero size (e.g. CHARACTER*0). As a result, every zero-size symbol in a COMMON block retained its default offset of 0 — the block base address — instead of its correct sequential position.


---
Full diff: https://github.com/llvm/llvm-project/pull/214174.diff


2 Files Affected:

- (modified) flang/lib/Semantics/compute-offsets.cpp (+6) 
- (added) flang/test/Lower/common-block-char0.f90 (+36) 


``````````diff
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"}

``````````

</details>


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


More information about the flang-commits mailing list