Skip to content

Commit ab8253c

Browse files
committed
[Import as memory] SILGen for delegation to a C function imported as an initializer.
Always statically dispatch to C functions imported as members, and call to the foreign entry point. This gets us through SILGen, but DI is still deeply unhappy with the resulting SIL.
1 parent a02573e commit ab8253c

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

lib/SIL/SILDeclRef.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ swift::getMethodDispatch(AbstractFunctionDecl *method) {
3535
if (method->hasForcedStaticDispatch())
3636
return MethodDispatch::Static;
3737

38+
// Import-as-member declarations are always statically referenced.
39+
if (method->isImportAsMember())
40+
return MethodDispatch::Static;
41+
3842
// If this declaration is in a class but not marked final, then it is
3943
// always dynamically dispatched.
4044
auto dc = method->getDeclContext();

lib/SILGen/SILGenApply.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14401440
// that's the only thing that's witnessed. For classes,
14411441
// this is the initializing constructor, to which we will dynamically
14421442
// dispatch.
1443-
if (SelfParam.getSubstRValueType()->getRValueInstanceType()->is<ArchetypeType>()
1443+
if (SelfParam.getSubstRValueType()->getRValueInstanceType()
1444+
->is<ArchetypeType>()
14441445
&& isa<ProtocolDecl>(ctorRef->getDecl()->getDeclContext())) {
14451446
// Look up the witness for the constructor.
14461447
auto constant = SILDeclRef(ctorRef->getDecl(),
@@ -1470,13 +1471,17 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
14701471
getSubstFnType(), fn));
14711472
} else {
14721473
// Directly call the peer constructor.
1473-
setCallee(Callee::forDirect(SGF,
1474-
SILDeclRef(ctorRef->getDecl(),
1475-
useAllocatingCtor
1476-
? SILDeclRef::Kind::Allocator
1477-
: SILDeclRef::Kind::Initializer,
1478-
SILDeclRef::ConstructAtBestResilienceExpansion),
1479-
getSubstFnType(useAllocatingCtor), fn));
1474+
setCallee(
1475+
Callee::forDirect(
1476+
SGF,
1477+
SILDeclRef(ctorRef->getDecl(),
1478+
useAllocatingCtor
1479+
? SILDeclRef::Kind::Allocator
1480+
: SILDeclRef::Kind::Initializer,
1481+
SILDeclRef::ConstructAtBestResilienceExpansion,
1482+
SILDeclRef::ConstructAtNaturalUncurryLevel,
1483+
requiresForeignEntryPoint(ctorRef->getDecl())),
1484+
getSubstFnType(useAllocatingCtor), fn));
14801485
}
14811486

14821487
// Set up the substitutions, if we have any.

test/SILGen/import_as_member.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// RUN: %target-swift-frontend -emit-sil -I %S/../IDE/Inputs/custom-modules %s 2>&1 | FileCheck --check-prefix=SIL %s
1+
// RUN: %target-swift-frontend -emit-silgen -I %S/../IDE/Inputs/custom-modules %s 2>&1 | FileCheck --check-prefix=SIL %s
22
// REQUIRES: objc_interop
33
import ImportAsMember.A
44
import ImportAsMember.Proto
5+
import ImportAsMember.Class
56

67
public func returnGlobalVar() -> Double {
78
return Struct1.globalVar
@@ -22,3 +23,25 @@ public func useProto(p: IAMProto) {
2223

2324
// SIL-LABEL: sil {{.*}}anchor{{.*}} () -> () {
2425
func anchor() {}
26+
27+
// SIL-LABEL: sil {{.*}}useClass{{.*}}
28+
// SIL: bb0([[D:%[0-9]+]] : $Double, [[OPTS:%[0-9]+]] : $SomeClass.Options):
29+
public func useClass(d: Double, opts: SomeClass.Options) {
30+
// SIL: [[CTOR:%[0-9]+]] = function_ref @MakeIAMSomeClass : $@convention(c) (Double) -> @autoreleased SomeClass
31+
// SIL: [[OBJ:%[0-9]+]] = apply [[CTOR]]([[D]])
32+
let o = SomeClass(value: d)
33+
34+
// SIL: [[APPLY_FN:%[0-9]+]] = function_ref @IAMSomeClassApplyOptions : $@convention(c) (SomeClass, SomeClass.Options) -> ()
35+
// SIL: apply [[APPLY_FN]]([[OBJ]], [[OPTS]])
36+
o.applyOptions(opts)
37+
}
38+
39+
extension SomeClass {
40+
// SIL-LABEL: sil hidden @_TFE16import_as_memberCSo9SomeClasscfT6doubleSd_S0_
41+
// SIL: bb0([[DOUBLE:%[0-9]+]] : $Double
42+
// SIL: [[FNREF:%[0-9]+]] = function_ref @MakeIAMSomeClass
43+
// SIL: apply [[FNREF]]([[DOUBLE]])
44+
convenience init(double: Double) {
45+
self.init(value: double)
46+
}
47+
}

0 commit comments

Comments
 (0)