forked from ldc-developers/ldc
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix packed struct layout regression (issue ldc-developers#2346)
- Loading branch information
Showing
2 changed files
with
63 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// RUN: %ldc -output-ll -of=%t.ll %s && FileCheck %s < %t.ll | ||
|
||
// Make sure the LL struct is packed due to an unnatural overall alignment of 1. | ||
// CHECK-DAG: %gh2346.UnalignedUInt = type <{ i32 }> | ||
struct UnalignedUInt { | ||
align(1) uint a; | ||
} | ||
static assert(UnalignedUInt.alignof == 1); | ||
static assert(UnalignedUInt.sizeof == 4); | ||
|
||
// Then there's no need to pack naturally aligned containers. | ||
// CHECK-DAG: %gh2346.Container = type { i8, %gh2346.UnalignedUInt } | ||
struct Container { | ||
ubyte one; | ||
UnalignedUInt two; | ||
} | ||
static assert(Container.alignof == 1); | ||
static assert(Container.sizeof == 5); | ||
static assert(Container.two.offsetof == 1); | ||
|
||
// CHECK-DAG: %gh2346.UnalignedUInt2 = type <{ i32 }> | ||
struct UnalignedUInt2 { | ||
align(2) uint a; | ||
} | ||
static assert(UnalignedUInt2.alignof == 2); | ||
static assert(UnalignedUInt2.sizeof == 4); | ||
|
||
// CHECK-DAG: %gh2346.Container2 = type { i8, [1 x i8], %gh2346.UnalignedUInt2 } | ||
struct Container2 { | ||
ubyte one; | ||
UnalignedUInt2 two; | ||
} | ||
static assert(Container2.alignof == 2); | ||
static assert(Container2.sizeof == 6); | ||
static assert(Container2.two.offsetof == 2); | ||
|
||
// CHECK-DAG: %gh2346.PackedContainer2 = type <{ i8, %gh2346.UnalignedUInt2 }> | ||
struct PackedContainer2 { | ||
ubyte one; | ||
align(1) UnalignedUInt2 two; | ||
} | ||
static assert(PackedContainer2.alignof == 1); | ||
static assert(PackedContainer2.sizeof == 5); | ||
static assert(PackedContainer2.two.offsetof == 1); | ||
|
||
// CHECK-DAG: %gh2346.WeirdContainer = type { i8, [1 x i8], %gh2346.UnalignedUInt, [2 x i8] } | ||
align(4) struct WeirdContainer { | ||
ubyte one; | ||
align(2) UnalignedUInt two; | ||
} | ||
static assert(WeirdContainer.alignof == 2); | ||
static assert(WeirdContainer.sizeof == 8); | ||
static assert(WeirdContainer.two.offsetof == 2); |