Bug 17277 – Member and aggregate alignment semantics

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-03-26T13:00:00Z
Last change time
2017-08-07T13:16:14Z
Assigned to
nobody
Creator
kinke

Comments

Comment #0 by kinke — 2017-03-26T13:00:21Z
Code: ***** struct S { byte[5] bytes; struct { byte byte1; align(1) int int1; } } pragma(msg, "S.int1.offsetof: ", S.int1.offsetof); pragma(msg, "S.alignof: ", S.alignof); pragma(msg, "S.sizeof: ", S.sizeof); ***** Result: S.int1.offsetof: 9LU S.alignof: 4LU S.sizeof: 16LU I expected { 6, 1, 10 }. `byte1` has an implicit alignment of 1 as it's a byte, `int1` has an explicit alignment of 1. I'd expect the anonymous struct's implicit alignment to be max(1, 1) = 1 (maximum of all member alignments). As the `bytes` array has an implicit alignment of 1 too, I'd expect S to have an implicit alignment of max(1, 1) = 1 too. And so both the anonymous struct and S aren't padded. Just for clarifiction, in this modified version: struct S { byte[5] bytes; struct { byte byte1; align(2) int int1; } } I'd expect { 8, 2, 12 } with 2 padding bytes at offsets 5 and 7. ----- When adding an explicit align(1) for both anonymous struct and S: align(1) struct S { byte[5] bytes; align(1) struct { byte byte1; align(1) int int1; } } you still don't come up with { 6, 1, 10 }. It's { 6, 4, 10 }. I.e., S's alignment is 4, but it's size is 10. Which means that every odd element in an array of S structs is **guaranteed** NOT to be aligned on a 4-bytes-boundary. Note that LDC uses these alignments as optimization hints for LLVM codegen, and platforms such as ARM don't forgive unaligned memory access.
Comment #1 by github-bugzilla — 2017-05-28T12:02:52Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/9180e57bdd4a6330217be5fcefc30f965b0d365b Fix alignment/size of packed aggregates (issue #17277) Change the definition of an aggregate's overall alignment from maximum of each field's `max(natural type alignment, optional alignment attribute)` to maximum of each field's actual alignment (which may be less than its type's natural alignment). https://github.com/dlang/dmd/commit/6fc815992b66938e093fff537724614fddc06ad2 Merge pull request #6754 from kinke/fix17277 Fix alignment/size of packed aggregates (issue #17277) merged-on-behalf-of: Daniel Murphy <[email protected]>
Comment #2 by dlang-bugzilla — 2017-07-26T10:31:27Z
*** Issue 17685 has been marked as a duplicate of this issue. ***
Comment #3 by github-bugzilla — 2017-08-07T13:16:14Z
Commits pushed to newCTFE at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/9180e57bdd4a6330217be5fcefc30f965b0d365b Fix alignment/size of packed aggregates (issue #17277) https://github.com/dlang/dmd/commit/6fc815992b66938e093fff537724614fddc06ad2 Merge pull request #6754 from kinke/fix17277