Skip to content

Commit

Permalink
IRGen: fix creating statically initialized objects which contain empt…
Browse files Browse the repository at this point in the history
…y fields
  • Loading branch information
eeckstein committed Aug 30, 2017
1 parent ae33dd0 commit f1321ee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/IRGen/GenConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,12 @@ llvm::Constant *irgen::emitConstantObject(IRGenModule &IGM, ObjectInst *OI,
// Construct the object init value including tail allocated elements.
for (unsigned i = 0; i != NumElems; i++) {
SILValue Val = OI->getAllElements()[i];
unsigned EltIdx = ClassLayout->getElements()[i].getStructIndex();
assert(EltIdx != 0 && "the first element is the object header");
elts[EltIdx] = emitConstantValue(IGM, Val);
const ElementLayout &EL = ClassLayout->getElements()[i];
if (!EL.isEmpty()) {
unsigned EltIdx = EL.getStructIndex();
assert(EltIdx != 0 && "the first element is the object header");
elts[EltIdx] = emitConstantValue(IGM, Val);
}
}
// Construct the object header.
llvm::Type *ObjectHeaderTy = sTy->getElementType(0);
Expand Down
16 changes: 16 additions & 0 deletions test/IRGen/static_initializer.sil
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ sil_global @static_array : $TestArrayStorage = {
}
// CHECK: @static_array = {{(protected )?}}global %T18static_initializer16TestArrayStorageC_tailelems0c { i64 0, %T18static_initializer16TestArrayStorageC_tailelems0 <{ %swift.refcounted zeroinitializer, %Ts5Int32V <{ i32 2 }>, [4 x i8] undef, %Ts5Int64V <{ i64 10 }>, %Ts5Int64V <{ i64 20 }> }> }, align 8

final class ClassWithEmptyField {
@sil_stored var x: Int32
@sil_stored var y: ()
init()
}

sil_vtable ClassWithEmptyField { }

sil_global @static_class_with_empty_field : $ClassWithEmptyField = {
%0 = integer_literal $Builtin.Int32, 2
%3 = struct $Int32 (%0 : $Builtin.Int32)
%5 = tuple ()
%initval = object $ClassWithEmptyField (%3 : $Int32, %5 : $())
}
// CHECK: @static_class_with_empty_field = {{(protected )?}}global %T18static_initializer19ClassWithEmptyFieldC_tailelems1c { i64 0, %T18static_initializer19ClassWithEmptyFieldC_tailelems1 <{ %swift.refcounted zeroinitializer, %Ts5Int32V <{ i32 2 }> }> }, align 8

// CHECK-LABEL: define{{( protected)?}} swiftcc i8* @_TF2cha1xSi() {{.*}} {
// CHECK-NEXT: entry:
// CHECK-NEXT: ret i8* bitcast (%Ts5Int32V* @_T02ch1xSiv to i8*)
Expand Down

0 comments on commit f1321ee

Please sign in to comment.