Skip to content

Commit 445bb50

Browse files
committed
[CIR][NFC] Add helpers to improve emitNullConstant
This makes it on par with OG but does not add anything just yet (same NYI asserts should fail).
1 parent e97724e commit 445bb50

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,6 +1670,44 @@ ConstantEmitter::tryEmitAbstractForInitializer(const VarDecl &D) {
16701670
return validateAndPopAbstract(C, state);
16711671
}
16721672

1673+
// FIXME(cir): both these helpers belong to ABInfoImpl.cpp in OG, but this
1674+
// file in CIR sits in TargetLowering instead, which won't make sense here since
1675+
// they are all AST queries, should belong in a shared location with OG helpers.
1676+
bool isEmptyRecordForLayout(const ASTContext &Context, QualType T);
1677+
bool isEmptyFieldForLayout(const ASTContext &ctx, const FieldDecl *fd) {
1678+
if (fd->isZeroLengthBitField())
1679+
return true;
1680+
1681+
if (fd->isUnnamedBitField())
1682+
return false;
1683+
1684+
return isEmptyRecordForLayout(ctx, fd->getType());
1685+
}
1686+
1687+
bool isEmptyRecordForLayout(const ASTContext &ctx, QualType t) {
1688+
const RecordType *rt = t->getAs<RecordType>();
1689+
if (!rt)
1690+
return false;
1691+
1692+
const RecordDecl *rd = rt->getDecl();
1693+
1694+
// If this is a C++ record, check the bases first.
1695+
if (const CXXRecordDecl *CXXRD = dyn_cast<CXXRecordDecl>(rd)) {
1696+
if (CXXRD->isDynamicClass())
1697+
return false;
1698+
1699+
for (const auto &I : CXXRD->bases())
1700+
if (!isEmptyRecordForLayout(ctx, I.getType()))
1701+
return false;
1702+
}
1703+
1704+
for (const auto *I : rd->fields())
1705+
if (!isEmptyFieldForLayout(ctx, I))
1706+
return false;
1707+
1708+
return true;
1709+
}
1710+
16731711
static mlir::TypedAttr emitNullConstant(CIRGenModule &CGM, const RecordDecl *rd,
16741712
bool asCompleteObject) {
16751713
const CIRGenRecordLayout &layout = CGM.getTypes().getCIRGenRecordLayout(rd);
@@ -1698,8 +1736,8 @@ static mlir::TypedAttr emitNullConstant(CIRGenModule &CGM, const RecordDecl *rd,
16981736
for (const auto *Field : rd->fields()) {
16991737
// Fill in non-bitfields. (Bitfields always use a zero pattern, which we
17001738
// will fill in later.)
1701-
if (!Field->isBitField()) {
1702-
// TODO(cir) check for !isEmptyFieldForLayout(CGM.getContext(), Field))
1739+
if (!Field->isBitField() &&
1740+
!isEmptyFieldForLayout(CGM.getASTContext(), Field)) {
17031741
llvm_unreachable("NYI");
17041742
}
17051743

0 commit comments

Comments
 (0)