Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang] Round derived type byte sizes up to alignment multiple #67571

Merged
merged 1 commit into from
Oct 17, 2023

Conversation

klausler
Copy link
Contributor

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.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Sep 27, 2023
@llvmbot
Copy link

llvmbot commented Sep 27, 2023

@llvm/pr-subscribers-flang-semantics

Changes

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.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/compute-offsets.cpp (+2)
  • (modified) flang/test/Semantics/offsets02.f90 (+8-2)
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

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@klausler klausler force-pushed the bug1392 branch 2 times, most recently from 58d7329 to 8c85f18 Compare September 29, 2023 21:18
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.
@klausler
Copy link
Contributor Author

Note to self: do not merge this patch until Jean has merged a similar fix to lowering.

@klausler
Copy link
Contributor Author

@jeanPerier can this fix be merged now?

@jeanPerier
Copy link
Contributor

@jeanPerier can this fix be merged now?

Yes, you can merge. My fix is not merged yet, but I made sure it is not an issue anymore for you to merge before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants