Skip to content

Commit 4da1ed6

Browse files
committed
SILGen: Forward declare SILFunctions that will be referenced by #_hasSymbol query functions.
1 parent 54e5fa0 commit 4da1ed6

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

lib/SILGen/SILGenDecl.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "swift/SIL/SILArgument.h"
3131
#include "swift/SIL/SILDebuggerClient.h"
3232
#include "swift/SIL/SILInstruction.h"
33+
#include "swift/SIL/SILSymbolVisitor.h"
3334
#include "swift/SIL/SILType.h"
3435
#include "swift/SIL/TypeLowering.h"
3536
#include "llvm/ADT/SmallString.h"
@@ -1617,8 +1618,33 @@ void SILGenFunction::emitStmtCondition(StmtCondition Cond, JumpDest FalseDest,
16171618
booleanTestValue = emitUnwrapIntegerResult(expr, booleanTestValue);
16181619
booleanTestLoc = expr;
16191620

1620-
// FIXME: Add decl to list of decls that need a has symbol query
1621-
// function to be emitted during IRGen.
1621+
// Ensure that function declarations for each function associated with
1622+
// the decl are emitted so that they can be referenced during IRGen.
1623+
class SymbolVisitor : public SILSymbolVisitor {
1624+
SILGenModule &SGM;
1625+
1626+
public:
1627+
SymbolVisitor(SILGenModule &SGM) : SGM{SGM} {};
1628+
1629+
void addFunction(SILDeclRef declRef) override {
1630+
(void)SGM.getFunction(declRef, NotForDefinition);
1631+
}
1632+
1633+
virtual void addFunction(StringRef name, SILDeclRef declRef) override {
1634+
// The kinds of functions which go through this callback (e.g.
1635+
// differentiability witnesses) can't be forward declared with a
1636+
// SILDeclRef alone. For now, just ignore them.
1637+
//
1638+
// Ideally, this callback will be removed entirely in favor of
1639+
// SILDeclRef being able to represent all function variants.
1640+
}
1641+
};
1642+
1643+
SILSymbolVisitorOptions opts;
1644+
opts.VisitMembers = false;
1645+
auto visitorCtx =
1646+
SILSymbolVisitorContext(getModule().getSwiftModule(), opts);
1647+
SymbolVisitor(SGM).visitDecl(decl, visitorCtx);
16221648
break;
16231649
}
16241650
}

test/SILGen/has_symbol.swift

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,23 @@ func testGlobalFunctions() {
2626
if #_hasSymbol(forwardDeclaredFunc) {}
2727
}
2828

29+
// --- function(with:) ---
2930
// CHECK: sil hidden_external @$s17has_symbol_helper8function4withySi_tFTwS : $@convention(thin) () -> Builtin.Int1
31+
// CHECK: sil @$s17has_symbol_helper8function4withySi_tF : $@convention(thin) (Int) -> ()
32+
33+
// --- genericFunc<A>(_:) ---
3034
// CHECK: sil hidden_external @$s17has_symbol_helper11genericFuncyyxAA1PRzlFTwS : $@convention(thin) () -> Builtin.Int1
35+
// CHECK: sil @$s17has_symbol_helper11genericFuncyyxAA1PRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
36+
37+
// --- cdeclFunc() ---
3138
// CHECK: sil hidden_external @$s17has_symbol_helper9cdeclFuncyyFTwS : $@convention(thin) () -> Builtin.Int1
39+
// CHECK: sil @$s17has_symbol_helper9cdeclFuncyyF : $@convention(thin) () -> ()
40+
// CHECK: sil [serialized] @cdecl_func : $@convention(c) () -> ()
41+
42+
// --- forwardDeclaredFunc() ---
43+
// CHECK: sil hidden_external @$s17has_symbol_helper19forwardDeclaredFuncyyFTwS : $@convention(thin) () -> Builtin.Int1
44+
// FIXME: missing forward declared function
45+
3246

3347
// CHECK: sil hidden [ossa] @$s4test0A4VarsyyF : $@convention(thin) () -> ()
3448
func testVars() {
@@ -38,7 +52,10 @@ func testVars() {
3852
if #_hasSymbol(global) {}
3953
}
4054

55+
// --- global ---
4156
// CHECK: sil hidden_external @$s17has_symbol_helper6globalSivpTwS : $@convention(thin) () -> Builtin.Int1
57+
// CHECK: sil @$s17has_symbol_helper6globalSivg : $@convention(thin) () -> Int
58+
4259

4360
// CHECK: sil hidden [ossa] @$s4test0A6Structyy17has_symbol_helper1SVF : $@convention(thin) (@in_guaranteed S) -> ()
4461
func testStruct(_ s: S) {
@@ -68,12 +85,34 @@ func testStruct(_ s: S) {
6885
if #_hasSymbol(S.init(member:)) {}
6986
}
7087

88+
// --- S.member ---
7189
// CHECK: sil hidden_external @$s17has_symbol_helper1SV6memberSivpTwS : $@convention(thin) () -> Builtin.Int1
90+
// CHECK: sil @$s17has_symbol_helper1SV6memberSivg : $@convention(method) (@in_guaranteed S) -> Int
91+
// CHECK: sil @$s17has_symbol_helper1SV6memberSivs : $@convention(method) (Int, @inout S) -> ()
92+
// CHECK: sil @$s17has_symbol_helper1SV6memberSivM : $@yield_once @convention(method) (@inout S) -> @yields @inout Int
93+
94+
// --- S.method(with:) ---
7295
// CHECK: sil hidden_external @$s17has_symbol_helper1SV6method4withySi_tFTwS : $@convention(thin) () -> Builtin.Int1
96+
// CHECK: sil @$s17has_symbol_helper1SV6method4withySi_tF : $@convention(method) (Int, @in_guaranteed S) -> ()
97+
98+
// --- S.genericFunc<A>(_:) ---
7399
// CHECK: sil hidden_external @$s17has_symbol_helper1SV11genericFuncyyxAA1PRzlFTwS : $@convention(thin) () -> Builtin.Int1
100+
// CHECK: sil @$s17has_symbol_helper1SV11genericFuncyyxAA1PRzlF : $@convention(method) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0, @in_guaranteed S) -> ()
101+
102+
// --- S.staticFunc() ---
74103
// CHECK: sil hidden_external @$s17has_symbol_helper1SV10staticFuncyyFZTwS : $@convention(thin) () -> Builtin.Int1
104+
// CHECK: sil @$s17has_symbol_helper1SV10staticFuncyyFZ : $@convention(method) (@thin S.Type) -> ()
105+
106+
// --- S.staticMember ---
75107
// CHECK: sil hidden_external @$s17has_symbol_helper1SV12staticMemberSivpZTwS : $@convention(thin) () -> Builtin.Int1
108+
// CHECK: sil @$s17has_symbol_helper1SV12staticMemberSivgZ : $@convention(method) (@thin S.Type) -> Int
109+
// CHECK: sil @$s17has_symbol_helper1SV12staticMemberSivsZ : $@convention(method) (Int, @thin S.Type) -> ()
110+
// CHECK: sil @$s17has_symbol_helper1SV12staticMemberSivMZ : $@yield_once @convention(method) (@thin S.Type) -> @yields @inout Int
111+
112+
// --- S.init(member:) ---
76113
// CHECK: sil hidden_external @$s17has_symbol_helper1SV6memberACSi_tcfcTwS : $@convention(thin) () -> Builtin.Int1
114+
// sil @$s17has_symbol_helper1SV6memberACSi_tcfC : $@convention(method) (Int, @thin S.Type) -> @out S
115+
77116

78117
// CHECK: sil hidden [ossa] @$s4test0A13GenericStructyy17has_symbol_helper0B1SVyAC1SVGF : $@convention(thin) (@in_guaranteed GenericS<S>) -> ()
79118
func testGenericStruct(_ s: GenericS<S>) {
@@ -87,8 +126,16 @@ func testGenericStruct(_ s: GenericS<S>) {
87126
if #_hasSymbol(s.method(with:)) {}
88127
}
89128

129+
// --- GenericS.member ---
90130
// CHECK: sil hidden_external @$s17has_symbol_helper8GenericSV6memberxvpTwS : $@convention(thin) () -> Builtin.Int1
131+
// CHECK: sil @$s17has_symbol_helper8GenericSV6memberxvg : $@convention(method) <τ_0_0 where τ_0_0 : P> (@in_guaranteed GenericS<τ_0_0>) -> @out τ_0_0
132+
// CHECK: sil @$s17has_symbol_helper8GenericSV6memberxvs : $@convention(method) <τ_0_0 where τ_0_0 : P> (@in τ_0_0, @inout GenericS<τ_0_0>) -> ()
133+
// CHECK: sil @$s17has_symbol_helper8GenericSV6memberxvM : $@yield_once @convention(method) <τ_0_0 where τ_0_0 : P> (@inout GenericS<τ_0_0>) -> @yields @inout τ_0_0
134+
135+
// --- GenericS.method(with:) ---
91136
// CHECK: sil hidden_external @$s17has_symbol_helper8GenericSV6method4withyx_tFTwS : $@convention(thin) () -> Builtin.Int1
137+
// CHECK: sil @$s17has_symbol_helper8GenericSV6method4withyx_tF : $@convention(method) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0, @in_guaranteed GenericS<τ_0_0>) -> ()
138+
92139

93140
// CHECK: sil hidden [ossa] @$s4test0A5Classyy17has_symbol_helper1CCF : $@convention(thin) (@guaranteed C) -> () {
94141
func testClass(_ c: C) {
@@ -114,11 +161,29 @@ func testClass(_ c: C) {
114161
if #_hasSymbol(C.init(member:)) {}
115162
}
116163

164+
// --- C.member ---
117165
// CHECK: sil hidden_external @$s17has_symbol_helper1CC6memberSivpTwS : $@convention(thin) () -> Builtin.Int1
166+
// Method dispatch thunks are generated in IRGen so no SIL decls are expected.
167+
168+
// --- C.method(with:) ---
118169
// CHECK: sil hidden_external @$s17has_symbol_helper1CC6method4withySi_tFTwS : $@convention(thin) () -> Builtin.Int1
170+
// Method dispatch thunks are generated in IRGen so no SIL decls are expected.
171+
172+
// --- C.classFunc() ---
119173
// CHECK: sil hidden_external @$s17has_symbol_helper1CC9classFuncyyFZTwS : $@convention(thin) () -> Builtin.Int1
174+
// Method dispatch thunks are generated in IRGen so no SIL decls are expected.
175+
176+
// --- C.staticMember ---
120177
// CHECK: sil hidden_external @$s17has_symbol_helper1CC12staticMemberSivpZTwS : $@convention(thin) () -> Builtin.Int1
178+
// CHECK: sil @$s17has_symbol_helper1CC12staticMemberSivgZ : $@convention(method) (@thick C.Type) -> Int
179+
// CHECK: sil @$s17has_symbol_helper1CC12staticMemberSivsZ : $@convention(method) (Int, @thick C.Type) -> ()
180+
// CHECK: sil @$s17has_symbol_helper1CC12staticMemberSivMZ : $@yield_once @convention(method) (@thick C.Type) -> @yields @inout Int
181+
182+
// --- C.init(member:) ---
121183
// CHECK: sil hidden_external @$s17has_symbol_helper1CC6memberACSi_tcfcTwS : $@convention(thin) () -> Builtin.Int1
184+
// CHECK: sil @$s17has_symbol_helper1CC6memberACSi_tcfc : $@convention(method) (Int, @owned C) -> @owned C
185+
// CHECK: sil [serialized] @$s17has_symbol_helper1CC6memberACSi_tcfC : $@convention(method) (Int, @thick C.Type) -> @owned C
186+
122187

123188
// CHECK: sil hidden [ossa] @$s4test0A4Enumyy17has_symbol_helper1EOF : $@convention(thin) (@in_guaranteed E) -> ()
124189
func testEnum(_ e: E) {
@@ -136,9 +201,16 @@ func testEnum(_ e: E) {
136201
if #_hasSymbol(e.method) {}
137202
}
138203

204+
// --- E.basicCase(_:) ---
139205
// CHECK: sil hidden_external @$s17has_symbol_helper1EO9basicCaseyA2CmFTwS : $@convention(thin) () -> Builtin.Int1
206+
207+
// --- E.payloadCase(_:) ---
140208
// CHECK: sil hidden_external @$s17has_symbol_helper1EO11payloadCaseyAcA1SVcACmFTwS : $@convention(thin) () -> Builtin.Int1
209+
210+
// --- E.method() ---
141211
// CHECK: sil hidden_external @$s17has_symbol_helper1EO6methodyyFTwS : $@convention(thin) () -> Builtin.Int1
212+
// CHECK: sil @$s17has_symbol_helper1EO6methodyyF : $@convention(method) (@in_guaranteed E) -> ()
213+
142214

143215
// CHECK: sil hidden [ossa] @$s4test0A8Protocolyyx17has_symbol_helper1PRzlF : $@convention(thin) <T where T : P> (@in_guaranteed T) -> ()
144216
func testProtocol<T: P>(_ p: T) {
@@ -148,6 +220,11 @@ func testProtocol<T: P>(_ p: T) {
148220
if #_hasSymbol(p.requirement) {}
149221
}
150222

223+
// --- P.requirement() ---
224+
// CHECK: sil hidden_external @$s17has_symbol_helper1PP11requirementyyFTwS : $@convention(thin) () -> Builtin.Int1
225+
// CHECK: sil @$s17has_symbol_helper1PP11requirementyyF : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
226+
227+
151228
// CHECK: sil hidden [ossa] @$s4test0A9MetatypesyyF : $@convention(thin) () -> ()
152229
func testMetatypes() {
153230
// CHECK: [[QUERY:%[0-9]+]] = function_ref @$s17has_symbol_helper1PPTwS : $@convention(thin) () -> Builtin.Int1
@@ -172,8 +249,17 @@ func testMetatypes() {
172249
if #_hasSymbol(E.self) {}
173250
}
174251

252+
// --- P.self ---
175253
// CHECK: sil hidden_external @$s17has_symbol_helper1PPTwS : $@convention(thin) () -> Builtin.Int1
254+
255+
// --- S.self ---
176256
// CHECK: sil hidden_external @$s17has_symbol_helper1SVTwS : $@convention(thin) () -> Builtin.Int1
257+
258+
// --- GenericS.self ---
177259
// CHECK: sil hidden_external @$s17has_symbol_helper8GenericSVTwS : $@convention(thin) () -> Builtin.Int1
260+
261+
// --- C.self ---
178262
// CHECK: sil hidden_external @$s17has_symbol_helper1CCTwS : $@convention(thin) () -> Builtin.Int1
263+
264+
// --- E.self ---
179265
// CHECK: sil hidden_external @$s17has_symbol_helper1EOTwS : $@convention(thin) () -> Builtin.Int1

0 commit comments

Comments
 (0)