Skip to content

Commit 0fcc8dc

Browse files
kzhuravlDavid Salinas
authored andcommitted
Revert "[clang][CGRecordLayout] Remove dependency on isZeroSize (llvm#96422)"
This reverts commit 4497ec2. Change-Id: I8ea53f9a56befb3072515df0f955734e8b12511f
1 parent 5693200 commit 0fcc8dc

28 files changed

+109
-242
lines changed

clang/lib/CodeGen/ABIInfoImpl.cpp

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -310,41 +310,6 @@ bool CodeGen::isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays,
310310
return true;
311311
}
312312

313-
bool CodeGen::isEmptyFieldForLayout(const ASTContext &Context,
314-
const FieldDecl *FD) {
315-
if (FD->isZeroLengthBitField(Context))
316-
return true;
317-
318-
if (FD->isUnnamedBitField())
319-
return false;
320-
321-
return isEmptyRecordForLayout(Context, FD->getType());
322-
}
323-
324-
bool CodeGen::isEmptyRecordForLayout(const ASTContext &Context, QualType T) {
325-
const RecordType *RT = T->getAs<RecordType>();
326-
if (!RT)
327-
return false;
328-
329-
const RecordDecl *RD = RT->getDecl();
330-
331-
// If this is a C++ record, check the bases first.
332-
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
333-
if (CXXRD->isDynamicClass())
334-
return false;
335-
336-
for (const auto &I : CXXRD->bases())
337-
if (!isEmptyRecordForLayout(Context, I.getType()))
338-
return false;
339-
}
340-
341-
for (const auto *I : RD->fields())
342-
if (!isEmptyFieldForLayout(Context, I))
343-
return false;
344-
345-
return true;
346-
}
347-
348313
const Type *CodeGen::isSingleElementStruct(QualType T, ASTContext &Context) {
349314
const RecordType *RT = T->getAs<RecordType>();
350315
if (!RT)

clang/lib/CodeGen/ABIInfoImpl.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,6 @@ bool isEmptyField(ASTContext &Context, const FieldDecl *FD, bool AllowArrays,
137137
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays,
138138
bool AsIfNoUniqueAddr = false);
139139

140-
/// isEmptyFieldForLayout - Return true iff the field is "empty", that is,
141-
/// either a zero-width bit-field or an \ref isEmptyRecordForLayout.
142-
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD);
143-
144-
/// isEmptyRecordForLayout - Return true iff a structure contains only empty
145-
/// base classes (per \ref isEmptyRecordForLayout) and fields (per
146-
/// \ref isEmptyFieldForLayout). Note, C++ record fields are considered empty
147-
/// if the [[no_unique_address]] attribute would have made them empty.
148-
bool isEmptyRecordForLayout(const ASTContext &Context, QualType T);
149-
150140
/// isSingleElementStruct - Determine if a structure is a "single
151141
/// element struct", i.e. it has exactly one non-empty field or
152142
/// exactly one field which is itself a single element

clang/lib/CodeGen/CGClass.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ABIInfoImpl.h"
1413
#include "CGBlocks.h"
1514
#include "CGCXXABI.h"
1615
#include "CGDebugInfo.h"
@@ -934,7 +933,7 @@ namespace {
934933
}
935934

936935
void addMemcpyableField(FieldDecl *F) {
937-
if (isEmptyFieldForLayout(CGF.getContext(), F))
936+
if (F->isZeroSize(CGF.getContext()))
938937
return;
939938
if (!FirstField)
940939
addInitialField(F);
@@ -1816,7 +1815,7 @@ namespace {
18161815
const CXXDestructorDecl *DD)
18171816
: Context(Context), EHStack(EHStack), DD(DD), StartIndex(std::nullopt) {}
18181817
void PushCleanupForField(const FieldDecl *Field) {
1819-
if (isEmptyFieldForLayout(Context, Field))
1818+
if (Field->isZeroSize(Context))
18201819
return;
18211820
unsigned FieldIndex = Field->getFieldIndex();
18221821
if (FieldHasTrivialDestructorBody(Context, Field)) {

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ABIInfoImpl.h"
1413
#include "CGCUDARuntime.h"
1514
#include "CGCXXABI.h"
1615
#include "CGCall.h"
@@ -4758,7 +4757,7 @@ static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
47584757
/// The resulting address doesn't necessarily have the right type.
47594758
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
47604759
const FieldDecl *field) {
4761-
if (isEmptyFieldForLayout(CGF.getContext(), field))
4760+
if (field->isZeroSize(CGF.getContext()))
47624761
return emitAddrOfZeroSizeField(CGF, base, field);
47634762

47644763
const RecordDecl *rec = field->getParent();

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ABIInfoImpl.h"
1413
#include "CGCXXABI.h"
1514
#include "CGObjCRuntime.h"
1615
#include "CGRecordLayout.h"
@@ -737,7 +736,7 @@ bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
737736

738737
// Zero-sized fields are not emitted, but their initializers may still
739738
// prevent emission of this struct as a constant.
740-
if (isEmptyFieldForLayout(CGM.getContext(), Field)) {
739+
if (Field->isZeroSize(CGM.getContext())) {
741740
if (Init->HasSideEffects(CGM.getContext()))
742741
return false;
743742
continue;
@@ -858,8 +857,7 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
858857
continue;
859858

860859
// Don't emit anonymous bitfields or zero-sized fields.
861-
if (Field->isUnnamedBitField() ||
862-
isEmptyFieldForLayout(CGM.getContext(), *Field))
860+
if (Field->isUnnamedBitField() || Field->isZeroSize(CGM.getContext()))
863861
continue;
864862

865863
// Emit the value of the initializer.
@@ -2527,10 +2525,8 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
25272525
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
25282526

25292527
// Ignore empty bases.
2530-
if (isEmptyRecordForLayout(CGM.getContext(), I.getType()) ||
2531-
CGM.getContext()
2532-
.getASTRecordLayout(base)
2533-
.getNonVirtualSize()
2528+
if (base->isEmpty() ||
2529+
CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
25342530
.isZero())
25352531
continue;
25362532

@@ -2544,8 +2540,7 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
25442540
for (const auto *Field : record->fields()) {
25452541
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
25462542
// will fill in later.)
2547-
if (!Field->isBitField() &&
2548-
!isEmptyFieldForLayout(CGM.getContext(), Field)) {
2543+
if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())) {
25492544
unsigned fieldIndex = layout.getLLVMFieldNo(Field);
25502545
elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
25512546
}
@@ -2567,7 +2562,7 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
25672562
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
25682563

25692564
// Ignore empty bases.
2570-
if (isEmptyRecordForLayout(CGM.getContext(), I.getType()))
2565+
if (base->isEmpty())
25712566
continue;
25722567

25732568
unsigned fieldIndex = layout.getVirtualBaseIndex(base);

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "CGOpenMPRuntime.h"
14-
#include "ABIInfoImpl.h"
1514
#include "CGCXXABI.h"
1615
#include "CGCleanup.h"
1716
#include "CGRecordLayout.h"
@@ -7755,28 +7754,23 @@ class MappableExprsHandler {
77557754
for (const auto &I : RD->bases()) {
77567755
if (I.isVirtual())
77577756
continue;
7758-
7759-
QualType BaseTy = I.getType();
7760-
const auto *Base = BaseTy->getAsCXXRecordDecl();
7757+
const auto *Base = I.getType()->getAsCXXRecordDecl();
77617758
// Ignore empty bases.
7762-
if (isEmptyRecordForLayout(CGF.getContext(), BaseTy) ||
7763-
CGF.getContext()
7764-
.getASTRecordLayout(Base)
7765-
.getNonVirtualSize()
7766-
.isZero())
7759+
if (Base->isEmpty() || CGF.getContext()
7760+
.getASTRecordLayout(Base)
7761+
.getNonVirtualSize()
7762+
.isZero())
77677763
continue;
77687764

77697765
unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base);
77707766
RecordLayout[FieldIndex] = Base;
77717767
}
77727768
// Fill in virtual bases.
77737769
for (const auto &I : RD->vbases()) {
7774-
QualType BaseTy = I.getType();
7770+
const auto *Base = I.getType()->getAsCXXRecordDecl();
77757771
// Ignore empty bases.
7776-
if (isEmptyRecordForLayout(CGF.getContext(), BaseTy))
7772+
if (Base->isEmpty())
77777773
continue;
7778-
7779-
const auto *Base = BaseTy->getAsCXXRecordDecl();
77807774
unsigned FieldIndex = RL.getVirtualBaseIndex(Base);
77817775
if (RecordLayout[FieldIndex])
77827776
continue;
@@ -7787,8 +7781,7 @@ class MappableExprsHandler {
77877781
for (const auto *Field : RD->fields()) {
77887782
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
77897783
// will fill in later.)
7790-
if (!Field->isBitField() &&
7791-
!isEmptyFieldForLayout(CGF.getContext(), Field)) {
7784+
if (!Field->isBitField() && !Field->isZeroSize(CGF.getContext())) {
77927785
unsigned FieldIndex = RL.getLLVMFieldNo(Field);
77937786
RecordLayout[FieldIndex] = Field;
77947787
}

clang/lib/CodeGen/CGRecordLayoutBuilder.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "ABIInfoImpl.h"
14-
#include "CGCXXABI.h"
1513
#include "CGRecordLayout.h"
14+
#include "CGCXXABI.h"
1615
#include "CodeGenTypes.h"
1716
#include "clang/AST/ASTContext.h"
1817
#include "clang/AST/Attr.h"
@@ -385,7 +384,7 @@ void CGRecordLowering::accumulateFields(bool isNonVirtualBaseType) {
385384
Field = accumulateBitFields(isNonVirtualBaseType, Field, FieldEnd);
386385
assert((Field == FieldEnd || !Field->isBitField()) &&
387386
"Failed to accumulate all the bitfields");
388-
} else if (isEmptyFieldForLayout(Context, *Field)) {
387+
} else if (Field->isZeroSize(Context)) {
389388
// Empty fields have no storage.
390389
++Field;
391390
} else {
@@ -634,7 +633,7 @@ CGRecordLowering::accumulateBitFields(bool isNonVirtualBaseType,
634633
// non-reusable tail padding.
635634
CharUnits LimitOffset;
636635
for (auto Probe = Field; Probe != FieldEnd; ++Probe)
637-
if (!isEmptyFieldForLayout(Context, *Probe)) {
636+
if (!Probe->isZeroSize(Context)) {
638637
// A member with storage sets the limit.
639638
assert((getFieldBitOffset(*Probe) % CharBits) == 0 &&
640639
"Next storage is not byte-aligned");
@@ -732,7 +731,7 @@ void CGRecordLowering::accumulateBases() {
732731
// Bases can be zero-sized even if not technically empty if they
733732
// contain only a trailing array member.
734733
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
735-
if (!isEmptyRecordForLayout(Context, Base.getType()) &&
734+
if (!BaseDecl->isEmpty() &&
736735
!Context.getASTRecordLayout(BaseDecl).getNonVirtualSize().isZero())
737736
Members.push_back(MemberInfo(Layout.getBaseClassOffset(BaseDecl),
738737
MemberInfo::Base, getStorageType(BaseDecl), BaseDecl));
@@ -880,7 +879,7 @@ CGRecordLowering::calculateTailClippingOffset(bool isNonVirtualBaseType) const {
880879
if (!isNonVirtualBaseType && isOverlappingVBaseABI())
881880
for (const auto &Base : RD->vbases()) {
882881
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
883-
if (isEmptyRecordForLayout(Context, Base.getType()))
882+
if (BaseDecl->isEmpty())
884883
continue;
885884
// If the vbase is a primary virtual base of some base, then it doesn't
886885
// get its own storage location but instead lives inside of that base.
@@ -896,7 +895,7 @@ CGRecordLowering::calculateTailClippingOffset(bool isNonVirtualBaseType) const {
896895
void CGRecordLowering::accumulateVBases() {
897896
for (const auto &Base : RD->vbases()) {
898897
const CXXRecordDecl *BaseDecl = Base.getType()->getAsCXXRecordDecl();
899-
if (isEmptyRecordForLayout(Context, Base.getType()))
898+
if (BaseDecl->isEmpty())
900899
continue;
901900
CharUnits Offset = Layout.getVBaseClassOffset(BaseDecl);
902901
// If the vbase is a primary virtual base of some base, then it doesn't
@@ -1162,7 +1161,7 @@ CodeGenTypes::ComputeRecordLayout(const RecordDecl *D, llvm::StructType *Ty) {
11621161
const FieldDecl *FD = *it;
11631162

11641163
// Ignore zero-sized fields.
1165-
if (isEmptyFieldForLayout(getContext(), FD))
1164+
if (FD->isZeroSize(getContext()))
11661165
continue;
11671166

11681167
// For non-bit-fields, just check that the LLVM struct offset matches the

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "CodeGenTBAA.h"
18-
#include "ABIInfoImpl.h"
1918
#include "CGRecordLayout.h"
2019
#include "CodeGenTypes.h"
2120
#include "clang/AST/ASTContext.h"
@@ -356,7 +355,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
356355
unsigned idx = 0;
357356
for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
358357
i != e; ++i, ++idx) {
359-
if (isEmptyFieldForLayout(Context, *i))
358+
if ((*i)->isZeroSize(Context))
360359
continue;
361360

362361
uint64_t Offset =
Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
1-
// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-linux-gnu -o - | FileCheck %s --check-prefixes=CHECK,EMPTY
2-
// RUN: %clang_cc1 %s -emit-llvm -triple x86_64-windows-msvc -o - | FileCheck %s --check-prefixes=CHECK,EMPTY-MSVC
1+
// RUN: %clang_cc1 -emit-llvm < %s | grep "zeroinitializer, i16 16877"
32
// PR4390
43
struct sysfs_dirent {
5-
union { struct sysfs_elem_dir { int x; } s_dir; };
4+
union { struct sysfs_elem_dir {} s_dir; };
65
unsigned short s_mode;
76
};
87
struct sysfs_dirent sysfs_root = { {}, 16877 };
9-
10-
// CHECK: @sysfs_root = {{.*}}global %struct.sysfs_dirent { %union.anon zeroinitializer, i16 16877 }
11-
12-
struct Foo {
13-
union { struct empty {} x; };
14-
unsigned short s_mode;
15-
};
16-
struct Foo foo = { {}, 16877 };
17-
18-
// EMPTY: @foo = {{.*}}global %struct.Foo { i16 16877 }
19-
// EMPTY-MSVC: @foo = {{.*}}global %struct.Foo { [4 x i8] undef, i16 16877 }

clang/test/CodeGen/X86/x86_64-vaarg.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ typedef struct {
5656
// CHECK: vaarg.end:
5757
// CHECK-NEXT: [[VAARG_ADDR:%.*]] = phi ptr [ [[TMP1]], [[VAARG_IN_REG]] ], [ [[OVERFLOW_ARG_AREA]], [[VAARG_IN_MEM]] ]
5858
// CHECK-NEXT: call void @llvm.memcpy.p0.p0.i64(ptr align 8 [[RETVAL]], ptr align 8 [[VAARG_ADDR]], i64 8, i1 false)
59-
// CHECK-NEXT: [[COERCE:%.*]] = getelementptr inbounds [[STRUCT_S1]], ptr [[RETVAL]], i32 0, i32 0
60-
// CHECK-NEXT: [[TMP3:%.*]] = load double, ptr [[COERCE]], align 8
59+
// CHECK-NEXT: [[TMP3:%.*]] = load double, ptr [[RETVAL]], align 8
6160
// CHECK-NEXT: ret double [[TMP3]]
6261
//
6362
s1 f(int z, ...) {

0 commit comments

Comments
 (0)