Skip to content

Commit ab7128e

Browse files
Merge pull request #7306 from aschwaighofer/fix_irgen_alloc_ref_objc_lowering
IRGen: Cast an ObjC allocation to the instantiated class type
2 parents 069fefb + 2307145 commit ab7128e

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/IRGen/GenClass.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,12 @@ llvm::Value *irgen::emitClassAllocation(IRGenFunction &IGF, SILType selfType,
677677
emitClassHeapMetadataRef(IGF, classType, MetadataValueType::ObjCClass,
678678
/*allow uninitialized*/ true);
679679
StackAllocSize = -1;
680-
return emitObjCAllocObjectCall(IGF, metadata, selfType.getSwiftRValueType());
680+
auto &ti = IGF.getTypeInfo(selfType);
681+
assert(ti.getSchema().size() == 1);
682+
assert(!ti.getSchema().containsAggregate());
683+
auto destType = ti.getSchema()[0].getScalarType();
684+
auto *val = emitObjCAllocObjectCall(IGF, metadata, selfType.getSwiftRValueType());
685+
return IGF.Builder.CreateBitCast(val, destType);
681686
}
682687

683688
llvm::Value *metadata =

test/IRGen/objc_alloc.sil

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: rm -rf %t && mkdir -p %t
2+
// RUN: %build-irgen-test-overlays
3+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) -Xllvm -new-mangling-for-tests -assume-parsing-unqualified-ownership-sil %s -emit-ir | %FileCheck %s
4+
5+
// REQUIRES: CPU=i386_or_x86_64
6+
// REQUIRES: objc_interop
7+
8+
sil_stage canonical
9+
10+
import Builtin
11+
import Swift
12+
import gizmo
13+
14+
// CHECK-LABEL: define {{.*}} @test
15+
// CHECK: [[ALLOC1:%.*]] = call %objc_object* @objc_allocWithZone
16+
// CHECK: [[CAST1:%.*]] = bitcast %objc_object* [[ALLOC1]] to [[KLASS:%.*]]*
17+
// CHECK: br
18+
19+
// CHECK: [[ALLOC2:%.*]] = call %objc_object* @objc_allocWithZone
20+
// CHECK: [[CAST2:%.*]] = bitcast %objc_object* [[ALLOC2]] to [[KLASS]]*
21+
// CHECK: br
22+
23+
// CHECK: phi [[KLASS]]* [ [[CAST2]], %{{.*}} ], [ [[CAST1]], %{{.*}} ]
24+
// CHECK: ret
25+
26+
sil @test : $@convention(thin) (@owned Optional<Int>) -> () {
27+
bb0(%0 : $Optional<Int>):
28+
switch_enum %0 : $Optional<Int>, case #Optional.none!enumelt: bb1, case #Optional.some!enumelt.1: bb2
29+
30+
bb1:
31+
%1 = alloc_ref [objc] $Gizmo
32+
br bb3(%1 : $Gizmo)
33+
34+
bb2:
35+
%2 = alloc_ref [objc] $Gizmo
36+
br bb3(%2 : $Gizmo)
37+
38+
bb3(%3 : $Gizmo):
39+
%4 = class_method [volatile] %3 : $Gizmo, #Gizmo.frob!1.foreign : (Gizmo) -> () -> (), $@convention(objc_method) (Gizmo) -> ()
40+
%5 = apply %4(%3) : $@convention(objc_method) (Gizmo) -> ()
41+
%6 = tuple ()
42+
return %6 : $()
43+
}
44+

0 commit comments

Comments
 (0)