Index: clang/lib/CodeGen/TargetInfo.cpp =================================================================== --- clang/lib/CodeGen/TargetInfo.cpp +++ clang/lib/CodeGen/TargetInfo.cpp @@ -486,12 +486,13 @@ return Ctx.getOrInsertSyncScopeID(""); /* default sync scope */ } -static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays); +static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, + bool AllowNoUniqueAddr = false); /// isEmptyField - Return true iff a the field is "empty", that is it /// is an unnamed bit-field or an (array of) empty record(s). static bool isEmptyField(ASTContext &Context, const FieldDecl *FD, - bool AllowArrays) { + bool AllowArrays, bool AllowNoUniqueAddr = false) { if (FD->isUnnamedBitfield()) return true; @@ -514,16 +515,23 @@ // // FIXME: We should use a predicate for whether this behavior is true in the // current ABI. - if (isa(RT->getDecl())) + // + // The exception to the above rule are fields marked with the + // [[no_unique_address]] attribute (since C++20). Those do count + // as empty according to the Itanium ABI. This property is currently + // only respected if the AllowNoUniqueAddr parameter is true. + if (isa(RT->getDecl()) && + !(AllowNoUniqueAddr && FD->hasAttr())) return false; - return isEmptyRecord(Context, FT, AllowArrays); + return isEmptyRecord(Context, FT, AllowArrays, AllowNoUniqueAddr); } /// isEmptyRecord - Return true iff a structure contains only empty /// fields. Note that a structure with a flexible array member is not /// considered empty. -static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays) { +static bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, + bool AllowNoUniqueAddr) { const RecordType *RT = T->getAs(); if (!RT) return false; @@ -534,11 +542,11 @@ // If this is a C++ record, check the bases first. if (const CXXRecordDecl *CXXRD = dyn_cast(RD)) for (const auto &I : CXXRD->bases()) - if (!isEmptyRecord(Context, I.getType(), true)) + if (!isEmptyRecord(Context, I.getType(), true, AllowNoUniqueAddr)) return false; for (const auto *I : RD->fields()) - if (!isEmptyField(Context, I, AllowArrays)) + if (!isEmptyField(Context, I, AllowArrays, AllowNoUniqueAddr)) return false; return true; } @@ -7220,7 +7228,7 @@ QualType Base = I.getType(); // Empty bases don't affect things either way. - if (isEmptyRecord(getContext(), Base, true)) + if (isEmptyRecord(getContext(), Base, true, true)) continue; if (!Found.isNull()) @@ -7230,12 +7238,17 @@ // Check the fields. for (const auto *FD : RD->fields()) { - // For compatibility with GCC, ignore empty bitfields in C++ mode. + // For compatibility with GCC, ignore empty bitfields in C++ mode, + // and empty C++ classes marked with the no_unique_address attribute. // Unlike isSingleElementStruct(), empty structure and array fields // do count. So do anonymous bitfields that aren't zero-sized. - if (getContext().getLangOpts().CPlusPlus && - FD->isZeroLengthBitField(getContext())) - continue; + if (getContext().getLangOpts().CPlusPlus) { + if (FD->isZeroLengthBitField(getContext())) + continue; + if (FD->hasAttr() && + isEmptyRecord(getContext(), FD->getType(), true, true)) + continue; + } // Unlike isSingleElementStruct(), arrays do not count. // Nested structures still do though. Index: clang/test/CodeGen/systemz-abi.cpp =================================================================== --- clang/test/CodeGen/systemz-abi.cpp +++ clang/test/CodeGen/systemz-abi.cpp @@ -23,3 +23,28 @@ // CHECK-LABEL: define void @_Z18pass_agg_float_cpp13agg_float_cpp(%struct.agg_float_cpp* noalias sret align 4 %{{.*}}, float %{{.*}}) // SOFT-FLOAT-LABEL: define void @_Z18pass_agg_float_cpp13agg_float_cpp(%struct.agg_float_cpp* noalias sret align 4 %{{.*}}, i32 %{{.*}}) + +// A field member of empty class type in C++ makes the record nonhomogeneous, +// unless it is marked as [[no_unique_address]]; +struct empty { }; +struct agg_nofloat_empty { float a; empty dummy; }; +struct agg_nofloat_empty pass_agg_nofloat_empty(struct agg_nofloat_empty arg) { return arg; } +// CHECK-LABEL: define void @_Z22pass_agg_nofloat_empty17agg_nofloat_empty(%struct.agg_nofloat_empty* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z22pass_agg_nofloat_empty17agg_nofloat_empty(%struct.agg_nofloat_empty* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +struct agg_float_empty { float a; [[no_unique_address]] empty dummy; }; +struct agg_float_empty pass_agg_float_empty(struct agg_float_empty arg) { return arg; } +// CHECK-LABEL: define void @_Z20pass_agg_float_empty15agg_float_empty(%struct.agg_float_empty* noalias sret align 4 %{{.*}}, float %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z20pass_agg_float_empty15agg_float_empty(%struct.agg_float_empty* noalias sret align 4 %{{.*}}, i32 %{{.*}}) + +// And likewise for members of base classes. +struct noemptybase { empty dummy; }; +struct agg_nofloat_emptybase : noemptybase { float a; }; +struct agg_nofloat_emptybase pass_agg_nofloat_emptybase(struct agg_nofloat_emptybase arg) { return arg; } +// CHECK-LABEL: define void @_Z26pass_agg_nofloat_emptybase21agg_nofloat_emptybase(%struct.agg_nofloat_emptybase* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z26pass_agg_nofloat_emptybase21agg_nofloat_emptybase(%struct.agg_nofloat_emptybase* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +struct emptybase { [[no_unique_address]] empty dummy; }; +struct agg_float_emptybase : emptybase { float a; }; +struct agg_float_emptybase pass_agg_float_emptybase(struct agg_float_emptybase arg) { return arg; } +// CHECK-LABEL: define void @_Z24pass_agg_float_emptybase19agg_float_emptybase(%struct.agg_float_emptybase* noalias sret align 4 %{{.*}}, float %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z24pass_agg_float_emptybase19agg_float_emptybase(%struct.agg_float_emptybase* noalias sret align 4 %{{.*}}, i32 %{{.*}}) +