diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp --- a/clang/lib/AST/RecordLayoutBuilder.cpp +++ b/clang/lib/AST/RecordLayoutBuilder.cpp @@ -1968,6 +1968,12 @@ } } + const Type *Ty = D->getType()->getBaseElementTypeUnsafe(); + // When used as part of a typedef, or together with a 'packed' attribute, + // the 'aligned' attribute can be used to decrease alignment. + bool AlignAttrCanDecreaseAlignment = + AlignIsRequired && (Ty->getAs() != nullptr || FieldPacked); + // The AIX `power` alignment rules apply the natural alignment of the // "first member" if it is of a floating-point data type (or is an aggregate // whose recursively "first" member or element is such a type). The alignment @@ -1978,7 +1984,7 @@ // and zero-width bit-fields count as prior members; members of empty class // types marked `no_unique_address` are not considered to be prior members. CharUnits PreferredAlign = FieldAlign; - if (DefaultsToAIXPowerAlignment && !AlignIsRequired && + if (DefaultsToAIXPowerAlignment && !AlignAttrCanDecreaseAlignment && (FoundFirstNonOverlappingEmptyFieldForAIX || IsNaturalAlign)) { auto performBuiltinTypeAlignmentUpgrade = [&](const BuiltinType *BTy) { if (BTy->getKind() == BuiltinType::Double || @@ -1989,7 +1995,6 @@ } }; - const Type *Ty = D->getType()->getBaseElementTypeUnsafe(); if (const ComplexType *CTy = Ty->getAs()) { performBuiltinTypeAlignmentUpgrade(CTy->getElementType()->castAs()); } else if (const BuiltinType *BTy = Ty->getAs()) { diff --git a/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp b/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp --- a/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp +++ b/clang/test/Layout/aix-alignof-align-and-pack-attr.cpp @@ -27,3 +27,23 @@ // CHECK: @{{.*}}test3{{.*}}c{{.*}} = global %"struct.test3::C" zeroinitializer, align 16 } // namespace test3 + +namespace test4 { +struct C { + struct CC { + long double ld; + } __attribute__((aligned(2))) cc; +} c; + +// CHECK: @{{.*}}test4{{.*}}c{{.*}} = global %"struct.test4::C" zeroinitializer, align 8 +} // namespace test4 + +namespace test5 { +struct C { + struct CC { + long double ld; + } __attribute__((aligned(2), packed)) cc; +} c; + +// CHECK: @{{.*}}test5{{.*}}c{{.*}} = global %"struct.test5::C" zeroinitializer, align 2 +} // namespace test5