Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions clang/lib/CIR/CodeGen/CIRGenExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,7 @@ bool ConstRecordBuilder::applyZeroInitPadding(const ASTRecordLayout &layout,
}

bool ConstRecordBuilder::build(InitListExpr *ile, bool allowOverwrite) {
RecordDecl *rd = ile->getType()
->castAs<clang::RecordType>()
->getDecl()
->getDefinitionOrSelf();
RecordDecl *rd = ile->getType()->castAsRecordDecl();
const ASTRecordLayout &layout = cgm.getASTContext().getASTRecordLayout(rd);

// Bail out if we have base classes. We could support these, but they only
Expand Down Expand Up @@ -686,17 +683,14 @@ bool ConstRecordBuilder::build(InitListExpr *ile, bool allowOverwrite) {
return false;
}

mlir::TypedAttr eltInit;
if (init)
eltInit = mlir::cast<mlir::TypedAttr>(
emitter.tryEmitPrivateForMemory(init, field->getType()));
else
eltInit = mlir::cast<mlir::TypedAttr>(emitter.emitNullForMemory(
cgm.getLoc(ile->getSourceRange()), field->getType()));

if (!eltInit)
mlir::Attribute eltInitAttr =
init ? emitter.tryEmitPrivateForMemory(init, field->getType())
: emitter.emitNullForMemory(cgm.getLoc(ile->getSourceRange()),
field->getType());
if (!eltInitAttr)
return false;

mlir::TypedAttr eltInit = mlir::cast<mlir::TypedAttr>(eltInitAttr);
if (!field->isBitField()) {
// Handle non-bitfield members.
if (!appendField(field, layout.getFieldOffset(index), eltInit,
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CIR/CodeGen/struct-init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ StructWithDefaultInit swdi = {};
// LLVM: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4
// OGCG: @swdi = global %struct.StructWithDefaultInit { i32 2 }, align 4

struct StructWithFieldInitFromConst {
int a : 10;
int b = a;
};

StructWithFieldInitFromConst swfifc = {};

// CIR: cir.global external @swfifc = #cir.zero : !rec_anon_struct
// LLVM: @swfifc = global { i8, i8, i32 } zeroinitializer, align 4
// OGCG: @swfifc = global { i8, i8, i32 } zeroinitializer, align 4

StructWithFieldInitFromConst swfifc2 = { 2 };

// CIR: cir.global external @swfifc2 = #cir.const_record<{#cir.int<2> : !u8i, #cir.int<0> : !u8i, #cir.int<2> : !s32i}> : !rec_anon_struct
// LLVM: @swfifc2 = global { i8, i8, i32 } { i8 2, i8 0, i32 2 }, align 4
// OGCG: @swfifc2 = global { i8, i8, i32 } { i8 2, i8 0, i32 2 }, align 4

void init() {
S s1 = {1, 2, 3};
S s2 = {4, 5};
Expand Down
Loading