[flang-commits] [flang] [flang] Round derived type byte sizes up to alignment multiple (PR #67571)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Wed Sep 27 09:08:49 PDT 2023


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/67571

When calculating sizes and offsets of types and components, be sure to round the size of a derived type up to a multiple of its alignment.

>From f725816a53b383fb4c392520c3042ae6e73ae338 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Wed, 27 Sep 2023 09:06:48 -0700
Subject: [PATCH] [flang] Round derived type byte sizes up to alignment
 multiple

When calculating sizes and offsets of types and components, be sure
to round the size of a derived type up to a multiple of its alignment.
---
 flang/lib/Semantics/compute-offsets.cpp |  2 ++
 flang/test/Semantics/offsets02.f90      | 10 ++++++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Semantics/compute-offsets.cpp b/flang/lib/Semantics/compute-offsets.cpp
index 139a8eb7c8c3771..375d2e3f7df376a 100644
--- a/flang/lib/Semantics/compute-offsets.cpp
+++ b/flang/lib/Semantics/compute-offsets.cpp
@@ -116,6 +116,8 @@ void ComputeOffsetsHelper::Compute(Scope &scope) {
       DoSymbol(*symbol);
     }
   }
+  // Ensure that the size is a multiple of the alignment
+  offset_ = Align(offset_, alignment_);
   scope.set_size(offset_);
   scope.SetAlignment(alignment_);
   // Assign offsets in COMMON blocks, unless this scope is a BLOCK construct,
diff --git a/flang/test/Semantics/offsets02.f90 b/flang/test/Semantics/offsets02.f90
index 387bbac5ff6d438..11e086cf68bee3d 100644
--- a/flang/test/Semantics/offsets02.f90
+++ b/flang/test/Semantics/offsets02.f90
@@ -8,11 +8,17 @@ subroutine s1
     real(8) :: a
     real(4) :: b
   end type
-  !CHECK: x1 size=12 offset=0:
-  !CHECK: y1 size=12 offset=16:
+  type t2
+    type(t1) c
+    real(4) d
+  end type
+  !CHECK: x1 size=16 offset=0:
+  !CHECK: y1 size=16 offset=16:
   type(t1) :: x1, y1
   !CHECK: z1 size=160 offset=32:
   type(t1) :: z1(10)
+  !CHECK: z2 size=24 offset=192
+  type(t2) z2
 end
 
 ! Like t1 but t2 does not need to be aligned on 64-bit boundary



More information about the flang-commits mailing list