Skip to content

SILGen: Implement if #_hasSymbol(...) conditions #61563

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
merged 3 commits into from
Oct 14, 2022

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Oct 13, 2022

Emit a call to a helper function that determines whether the symbols associated with the declaration referenced in the #_hasSymbol(...) condition are non-null at runtime. An upcoming change will emit a definition for this function during IRGen.

The current implementation strategy is not compatible with if #_hasSymbol() appearing in inlinable code since the ValueDecl * referenced by the condition would need to be recoverable when deserializing SIL. For now, diagnose the syntax when it appears in fragile functions.

Resolves rdar://100130015

@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli marked this pull request as ready for review October 13, 2022 14:58

func->getAttrs().add(new (ctx) SILGenNameAttr(mangledName, IsImplicit));
return func;
}
Copy link
Contributor

@xymus xymus Oct 13, 2022

Choose a reason for hiding this comment

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

Would it help the inlinable case to write that synthesized function in the swiftinterface (and swiftmodule) as usable from inline?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't help directly. The function body cannot be spelled in Swift (or even SIL) today so we can't emit the implementation of the helper function. Emitting just the function declaration with some kind of indication that it needs special treatment is doable but it also doesn't help with the crux of the problem, which is reconstituting the VarDecl * that we need to pass down to IRGen.

These are my ideas for lifting the restriction in the future:

  1. Invent a new SIL instruction for the #_hasSymbol() condition that, when serialized, serializes the VarDecl * so that we can re-derive the helper function.
  2. Make SIL capable of representing the logic of the helper function, in which case we generate the helper function in SILGen instead of IRGen.

@tshortli
Copy link
Contributor Author

@swift-ci please smoke test and merge

Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

Very cool!

Emit a call to a helper function that determines whether the symbols associated with the declaration referenced in the `#_hasSymbol(...)` condition are non-null at runtime. An upcoming change will emit a definition for this function during IRGen.

Resolves rdar://100130015
@tshortli
Copy link
Contributor Author

swiftlang/swift-syntax#946

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 649f09f into swiftlang:main Oct 14, 2022
@tshortli tshortli deleted the silgen-has-symbol branch October 14, 2022 16:09
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.

4 participants