Correct packing calculation (#485)

Summary:

Correct identification of packing.

Tests were modified to reflect the new behaviour. One test was removed as it was bogus - the flattener pass runs before the alignmentcalc pass and therefore the layout in the test could never happen (i.e. it has a hierarchy).

Reviewed By: JakeHillion

Differential Revision: D53815661
This commit is contained in:
Jonathan Haslam 2024-02-15 10:18:55 -08:00 committed by Facebook Community Bot
parent bfafe2eeae
commit 34a35cd418
3 changed files with 4 additions and 25 deletions

View File

@ -70,7 +70,7 @@ void AlignmentCalc::visit(Class& c) {
c.setAlign(alignment); c.setAlign(alignment);
} }
if (c.size() % c.align() != 0) { if (c.align() == 1 || c.size() % c.align() != 0) {
// Mark as packed if there is no tail padding // Mark as packed if there is no tail padding
c.setPacked(); c.setPacked();
} }

View File

@ -330,6 +330,7 @@ void ClangTypeParser::enumerateClassMembers(const clang::RecordType& ty,
auto& mtype = enumerateType(*qualType); auto& mtype = enumerateType(*qualType);
Member m{mtype, std::move(member_name), offset_in_bits, size_in_bits}; Member m{mtype, std::move(member_name), offset_in_bits, size_in_bits};
m.align = field->getMaxAlignment() / 8;
members.push_back(m); members.push_back(m);
} }

View File

@ -131,7 +131,7 @@ TEST(AlignmentCalcTest, RecurseClassParam) {
Primitive: int64_t Primitive: int64_t
)", )",
R"( R"(
[0] Class: MyClass (size: 0, align: 1) [0] Class: MyClass (size: 0, align: 1, packed)
Param Param
[1] Class: ClassA (size: 16, align: 8) [1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1) Member: a (offset: 0, align: 1)
@ -141,28 +141,6 @@ TEST(AlignmentCalcTest, RecurseClassParam) {
)"); )");
} }
TEST(AlignmentCalcTest, RecurseClassParent) {
test(AlignmentCalc::createPass(),
R"(
[0] Class: MyClass (size: 0)
Parent (offset: 0)
[1] Class: ClassA (size: 16)
Member: a (offset: 0)
Primitive: int8_t
Member: b (offset: 8)
Primitive: int64_t
)",
R"(
[0] Class: MyClass (size: 0, align: 1)
Parent (offset: 0)
[1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1)
Primitive: int8_t
Member: b (offset: 8, align: 8)
Primitive: int64_t
)");
}
TEST(AlignmentCalcTest, RecurseClassMember) { TEST(AlignmentCalcTest, RecurseClassMember) {
test(AlignmentCalc::createPass(), test(AlignmentCalc::createPass(),
R"( R"(
@ -197,7 +175,7 @@ TEST(AlignmentCalcTest, RecurseClassChild) {
Primitive: int64_t Primitive: int64_t
)", )",
R"( R"(
[0] Class: MyClass (size: 0, align: 1) [0] Class: MyClass (size: 0, align: 1, packed)
Child Child
[1] Class: ClassA (size: 16, align: 8) [1] Class: ClassA (size: 16, align: 8)
Member: a (offset: 0, align: 1) Member: a (offset: 0, align: 1)