Skip to content

[embedded] Support _findStringSwitchCaseWithCache in Embedded Swift #78915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

kubamracek
Copy link
Contributor

@kubamracek kubamracek commented Jan 25, 2025

ObjectOutliner has an optimization that replaces calls to findStringSwitchCase with _findStringSwitchCaseWithCache. This currently causes a "missing symbol" linker error in Embedded Swift, because this replacement happens without loading the body of the function from the stdlib. See the attached testcase that shows the problem (only happens under -O).

This PR fixes that with two changes:

(1) Calling lookupStdlibFunction under Embedded Swift now triggers loading the body (deserialization) + its dependencies.
(2) The above alone is not a full fix, because ObjectOutliner is a late SIL optimization, and so deserializing stdlib code causes un-specialized code to get added to the module, causing later problems (we don't expect any unspecialized code to be used in Embedded Swift after MandatoryPerformanceOptimizations are run). To fix that, we eagerly link in _findStringSwitchCaseWithCache whenever findStringSwitchCase is present as part of MandatoryPerformanceOptimizations.

Suggestions for a better approach are welcome :)

rdar://143587257

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@@ -1929,7 +1929,11 @@ OptionalBridgedFunction BridgedPassContext::lookupStdlibFunction(BridgedStringRe

SILDeclRef declRef(decl, SILDeclRef::Kind::Func);
SILOptFunctionBuilder funcBuilder(*invocation->getTransform());
return {funcBuilder.getOrCreateFunction(SILLocation(decl), declRef, NotForDefinition)};
SILFunction *function = funcBuilder.getOrCreateFunction(SILLocation(decl), declRef, NotForDefinition);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not good to hide such kind of logic in a bridging function.
Instead it's better to explicitly call Context.loadFunction at the call site (in the optimize function in this case).

let f = context.lookupStdlibFunction(name: "_findStringSwitchCaseWithCache") {
worklist.pushIfNotVisited(f)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a reasonable approach

@kubamracek kubamracek requested a review from a team as a code owner January 28, 2025 17:02
@kubamracek kubamracek force-pushed the embedded-string-switch-with-cache branch from c0b1526 to 500f8ad Compare January 28, 2025 18:08
@kubamracek
Copy link
Contributor Author

@swift-ci please test

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@kubamracek kubamracek enabled auto-merge January 28, 2025 18:50
@kubamracek kubamracek merged commit 7c685ba into swiftlang:main Jan 28, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants