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; @@ -499,11 +500,15 @@ // Constant arrays of empty records count as empty, strip them off. // Constant arrays of zero length always count as empty. + bool WasArray = false; if (AllowArrays) while (const ConstantArrayType *AT = Context.getAsConstantArrayType(FT)) { if (AT->getSize() == 0) return true; FT = AT->getElementType(); + // The [[no_unique_address]] special case below does not apply to + // arrays of C++ empty records, so we need to remember this fact. + WasArray = true; } const RecordType *RT = FT->getAs(); @@ -514,16 +519,25 @@ // // 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. + // In addition, the exception does not apply if the field was + // originally of array type that we stripped off above. + if (isa(RT->getDecl()) && + !(!WasArray && 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 +548,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 +7234,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 +7244,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,37 @@ // 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]]. This does not apply to arrays. +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 %{{.*}}) +struct agg_nofloat_emptyarray { float a; [[no_unique_address]] empty dummy[3]; }; +struct agg_nofloat_emptyarray pass_agg_nofloat_emptyarray(struct agg_nofloat_emptyarray arg) { return arg; } +// CHECK-LABEL: define void @_Z27pass_agg_nofloat_emptyarray22agg_nofloat_emptyarray(%struct.agg_nofloat_emptyarray* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z27pass_agg_nofloat_emptyarray22agg_nofloat_emptyarray(%struct.agg_nofloat_emptyarray* noalias sret align 4 %{{.*}}, i64 %{{.*}}) + +// 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 %{{.*}}) +struct noemptybasearray { [[no_unique_address]] empty dummy[3]; }; +struct agg_nofloat_emptybasearray : noemptybasearray { float a; }; +struct agg_nofloat_emptybasearray pass_agg_nofloat_emptybasearray(struct agg_nofloat_emptybasearray arg) { return arg; } +// CHECK-LABEL: define void @_Z31pass_agg_nofloat_emptybasearray26agg_nofloat_emptybasearray(%struct.agg_nofloat_emptybasearray* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +// SOFT-FLOAT-LABEL: define void @_Z31pass_agg_nofloat_emptybasearray26agg_nofloat_emptybasearray(%struct.agg_nofloat_emptybasearray* noalias sret align 4 %{{.*}}, i64 %{{.*}}) +