Skip to content

Commit 4950ee0

Browse files
authored
Revert "Reland "Revert "[clang][CGRecordLayout] Remove dependency on isZeroSize (llvm#96422)" (#156)"" (llvm#2065)
This reverts commit c515ad9. This was relanded in llvm#1633, but we still need it reverted to maintain the the device function ABI.
1 parent 33fbd5a commit 4950ee0

29 files changed

+164
-259
lines changed

clang/lib/CodeGen/ABIInfoImpl.cpp

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

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

clang/lib/CodeGen/ABIInfoImpl.h

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

123-
/// isEmptyFieldForLayout - Return true iff the field is "empty", that is,
124-
/// either a zero-width bit-field or an \ref isEmptyRecordForLayout.
125-
bool isEmptyFieldForLayout(const ASTContext &Context, const FieldDecl *FD);
126-
127-
/// isEmptyRecordForLayout - Return true iff a structure contains only empty
128-
/// base classes (per \ref isEmptyRecordForLayout) and fields (per
129-
/// \ref isEmptyFieldForLayout). Note, C++ record fields are considered empty
130-
/// if the [[no_unique_address]] attribute would have made them empty.
131-
bool isEmptyRecordForLayout(const ASTContext &Context, QualType T);
132-
133123
/// isSingleElementStruct - Determine if a structure is a "single
134124
/// element struct", i.e. it has exactly one non-empty field or
135125
/// 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"
@@ -932,7 +931,7 @@ namespace {
932931
}
933932

934933
void addMemcpyableField(FieldDecl *F) {
935-
if (isEmptyFieldForLayout(CGF.getContext(), F))
934+
if (F->isZeroSize(CGF.getContext()))
936935
return;
937936
if (!FirstField)
938937
addInitialField(F);
@@ -1814,7 +1813,7 @@ namespace {
18141813
const CXXDestructorDecl *DD)
18151814
: Context(Context), EHStack(EHStack), DD(DD), StartIndex(std::nullopt) {}
18161815
void PushCleanupForField(const FieldDecl *Field) {
1817-
if (isEmptyFieldForLayout(Context, Field))
1816+
if (Field->isZeroSize(Context))
18181817
return;
18191818
unsigned FieldIndex = Field->getFieldIndex();
18201819
if (FieldHasTrivialDestructorBody(Context, Field)) {

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4965,7 +4965,7 @@ static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
49654965
/// The resulting address doesn't necessarily have the right type.
49664966
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
49674967
const FieldDecl *field, bool IsInBounds) {
4968-
if (isEmptyFieldForLayout(CGF.getContext(), field))
4968+
if (field->isZeroSize(CGF.getContext()))
49694969
return emitAddrOfZeroSizeField(CGF, base, field, IsInBounds);
49704970

49714971
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"
@@ -758,7 +757,7 @@ bool ConstStructBuilder::Build(const InitListExpr *ILE, bool AllowOverwrite) {
758757

759758
// Zero-sized fields are not emitted, but their initializers may still
760759
// prevent emission of this struct as a constant.
761-
if (isEmptyFieldForLayout(CGM.getContext(), Field)) {
760+
if (Field->isZeroSize(CGM.getContext())) {
762761
if (Init && Init->HasSideEffects(CGM.getContext()))
763762
return false;
764763
continue;
@@ -894,8 +893,7 @@ bool ConstStructBuilder::Build(const APValue &Val, const RecordDecl *RD,
894893
continue;
895894

896895
// Don't emit anonymous bitfields or zero-sized fields.
897-
if (Field->isUnnamedBitField() ||
898-
isEmptyFieldForLayout(CGM.getContext(), *Field))
896+
if (Field->isUnnamedBitField() || Field->isZeroSize(CGM.getContext()))
899897
continue;
900898

901899
// Emit the value of the initializer.
@@ -2637,10 +2635,8 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
26372635
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
26382636

26392637
// Ignore empty bases.
2640-
if (isEmptyRecordForLayout(CGM.getContext(), I.getType()) ||
2641-
CGM.getContext()
2642-
.getASTRecordLayout(base)
2643-
.getNonVirtualSize()
2638+
if (base->isEmpty() ||
2639+
CGM.getContext().getASTRecordLayout(base).getNonVirtualSize()
26442640
.isZero())
26452641
continue;
26462642

@@ -2654,8 +2650,7 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
26542650
for (const auto *Field : record->fields()) {
26552651
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
26562652
// will fill in later.)
2657-
if (!Field->isBitField() &&
2658-
!isEmptyFieldForLayout(CGM.getContext(), Field)) {
2653+
if (!Field->isBitField() && !Field->isZeroSize(CGM.getContext())) {
26592654
unsigned fieldIndex = layout.getLLVMFieldNo(Field);
26602655
elements[fieldIndex] = CGM.EmitNullConstant(Field->getType());
26612656
}
@@ -2677,7 +2672,7 @@ static llvm::Constant *EmitNullConstant(CodeGenModule &CGM,
26772672
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
26782673

26792674
// Ignore empty bases.
2680-
if (isEmptyRecordForLayout(CGM.getContext(), I.getType()))
2675+
if (base->isEmpty())
26812676
continue;
26822677

26832678
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 "CGDebugInfo.h"
@@ -7762,28 +7761,23 @@ class MappableExprsHandler {
77627761
for (const auto &I : RD->bases()) {
77637762
if (I.isVirtual())
77647763
continue;
7765-
7766-
QualType BaseTy = I.getType();
7767-
const auto *Base = BaseTy->getAsCXXRecordDecl();
7764+
const auto *Base = I.getType()->getAsCXXRecordDecl();
77687765
// Ignore empty bases.
7769-
if (isEmptyRecordForLayout(CGF.getContext(), BaseTy) ||
7770-
CGF.getContext()
7771-
.getASTRecordLayout(Base)
7772-
.getNonVirtualSize()
7773-
.isZero())
7766+
if (Base->isEmpty() || CGF.getContext()
7767+
.getASTRecordLayout(Base)
7768+
.getNonVirtualSize()
7769+
.isZero())
77747770
continue;
77757771

77767772
unsigned FieldIndex = RL.getNonVirtualBaseLLVMFieldNo(Base);
77777773
RecordLayout[FieldIndex] = Base;
77787774
}
77797775
// Fill in virtual bases.
77807776
for (const auto &I : RD->vbases()) {
7781-
QualType BaseTy = I.getType();
7777+
const auto *Base = I.getType()->getAsCXXRecordDecl();
77827778
// Ignore empty bases.
7783-
if (isEmptyRecordForLayout(CGF.getContext(), BaseTy))
7779+
if (Base->isEmpty())
77847780
continue;
7785-
7786-
const auto *Base = BaseTy->getAsCXXRecordDecl();
77877781
unsigned FieldIndex = RL.getVirtualBaseIndex(Base);
77887782
if (RecordLayout[FieldIndex])
77897783
continue;
@@ -7794,8 +7788,7 @@ class MappableExprsHandler {
77947788
for (const auto *Field : RD->fields()) {
77957789
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
77967790
// will fill in later.)
7797-
if (!Field->isBitField() &&
7798-
!isEmptyFieldForLayout(CGF.getContext(), Field)) {
7791+
if (!Field->isBitField() && !Field->isZeroSize(CGF.getContext())) {
77997792
unsigned FieldIndex = RL.getLLVMFieldNo(Field);
78007793
RecordLayout[FieldIndex] = Field;
78017794
}

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 "CGCXXABI.h"
2019
#include "CGRecordLayout.h"
2120
#include "CodeGenTypes.h"
@@ -441,7 +440,7 @@ CodeGenTBAA::CollectFields(uint64_t BaseOffset,
441440
unsigned idx = 0;
442441
for (RecordDecl::field_iterator i = RD->field_begin(), e = RD->field_end();
443442
i != e; ++i, ++idx) {
444-
if (isEmptyFieldForLayout(Context, *i))
443+
if ((*i)->isZeroSize(Context))
445444
continue;
446445

447446
uint64_t Offset =
Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
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 };
98

10-
// CHECK: @sysfs_root = {{.*}}global { %union.anon, i16, [2 x i8] } { %union.anon zeroinitializer, i16 16877, [2 x i8] zeroinitializer }
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] zeroinitializer, 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_DIVE:%.*]] = getelementptr inbounds nuw [[STRUCT_S1]], ptr [[RETVAL]], i32 0, i32 0
60-
// CHECK-NEXT: [[TMP3:%.*]] = load double, ptr [[COERCE_DIVE]], 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)