Skip to content

Commit 92b0c5f

Browse files
committed
[embedded] Support _findStringSwitchCaseWithCache in Embedded Swift
1 parent 5770d59 commit 92b0c5f

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/ModulePasses/MandatoryPerformanceOptimizations.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ fileprivate struct PathFunctionTuple: Hashable {
8080

8181
private func optimize(function: Function, _ context: FunctionPassContext, _ moduleContext: ModulePassContext, _ worklist: inout FunctionWorklist) {
8282
var alreadyInlinedFunctions: Set<PathFunctionTuple> = Set()
83+
84+
// ObjectOutliner replaces calls to findStringSwitchCase with _findStringSwitchCaseWithCache, but this happens as a late SIL optimization,
85+
// which is a problem for Embedded Swift, because _findStringSwitchCaseWithCache will then reference non-specialized code. Solve this by
86+
// eagerly linking and specializing _findStringSwitchCaseWithCache whenever findStringSwitchCase is found in the module.
87+
if context.options.enableEmbeddedSwift {
88+
if function.hasSemanticsAttribute("findStringSwitchCase"),
89+
let f = context.lookupStdlibFunction(name: "_findStringSwitchCaseWithCache") {
90+
worklist.pushIfNotVisited(f)
91+
}
92+
}
8393

8494
var changed = true
8595
while changed {

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,11 @@ OptionalBridgedFunction BridgedPassContext::lookupStdlibFunction(BridgedStringRe
19291929

19301930
SILDeclRef declRef(decl, SILDeclRef::Kind::Func);
19311931
SILOptFunctionBuilder funcBuilder(*invocation->getTransform());
1932-
return {funcBuilder.getOrCreateFunction(SILLocation(decl), declRef, NotForDefinition)};
1932+
SILFunction *function = funcBuilder.getOrCreateFunction(SILLocation(decl), declRef, NotForDefinition);
1933+
if (mod->getOptions().EmbeddedSwift) {
1934+
mod->linkFunction(function, SILModule::LinkingMode::LinkAll);
1935+
}
1936+
return {function};
19331937
}
19341938

19351939
OptionalBridgedFunction BridgedPassContext::lookUpNominalDeinitFunction(BridgedDeclObj nominal) const {

test/embedded/string-switch2.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -wmo -O -Xlinker %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswiftUnicodeDataTables.a) | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: executable_test
5+
// REQUIRES: optimized_stdlib
6+
// REQUIRES: swift_stdlib_no_asserts
7+
// REQUIRES: OS=macosx
8+
// REQUIRES: swift_feature_Embedded
9+
10+
enum MyEnum: String {
11+
case case1
12+
case case2
13+
case case3
14+
case case4
15+
case case5
16+
case case6
17+
case case7
18+
case case8
19+
case case9
20+
case case10
21+
case case11
22+
case case12
23+
case case13
24+
case case14
25+
case case15
26+
case case16
27+
case case17
28+
case case18
29+
case case19
30+
}
31+
32+
var e = MyEnum.case1
33+
print(e.rawValue)
34+
e = MyEnum.case2
35+
print(e.rawValue)
36+
// CHECK: case1
37+
// CHECK: case2

0 commit comments

Comments
 (0)