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
Statically initialized objects #11537
Conversation
@swift-ci Please test |
Build failed |
Build failed |
docs/SIL.rst
Outdated
|
||
A global can have a static initializer if it's initial value can be |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's
-> its
the global variable is a statically initialized object. In this case the | ||
variable cannot be used as l-value, i.e. the reference to the object cannot be | ||
modified. As a consequence the variable cannot be accessed with ``global_addr`` | ||
but only with ``global_value``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we model this more generally, by having a sil_global
track whether it's immutable or not, and only allowing object
literals and global_value
accesses on immutable sil_global
s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll leave this open for a follow-up work. I think this needs some more discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. Another thing that crossed my mind—is the constraint on object
strictly necessary? It seems like something we could support generally by lowering to alloc_ref + initialization of the class fields during IRGen, and it might make some analyses easier to be able to represent object instantiations that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great overall!
217411f
to
49fd192
Compare
@swift-ci test and merge |
1 similar comment
@swift-ci test and merge |
a6cbdb3
to
49ab322
Compare
@swift-ci smoke test and merge |
1 similar comment
@swift-ci smoke test and merge |
…without a function context just refactoring, NFC
Plus a small refactoring to simplify the code: remove the inner SILPrintFunctionContext struct
Don't pass the basic block to parseSILInstruction. NFC
Static initializers are now represented by a list of literal and aggregate instructions in a SILGlobalVariable. For details see SIL.rst. This representation is cleaner than what we did so far (point to the initializer function and do some pattern matching). One implication of that change is that now (a subset of) instructions not necessarily have a parent function. Regarding the generated code it's a NFC. Also the swift module format didn't change because so far we don't serializer global variables.
…lly allocated object
…ail allocated elements
…tatically initialized objects. This commit contains: -) adding the new instructions + infrastructure, like parsing, printing, etc. -) support in IRGen to generate global object-variables (i.e. "heap" objects) which are statically initialized in the data section. -) IRGen for global_value which lazily initializes the object header and returns a reference to the object. For details see the documentation of the new instructions in SIL.rst.
Mainly this is done for array literals. This new optimization creates a statically initialized global variable which is the allocated object. The alloc_ref instruction is replaced by a global_value instruction. This optimization can give significant performance improvements for large array literals.
49ab322
to
375526a
Compare
@swift-ci smoke test and merge |
1 similar comment
@swift-ci smoke test and merge |
https://ci.swift.org/job/oss-swift-package-osx/89/
|
|
||
Returns the value of a global variable which has been previously initialized | ||
by ``alloc_global``. It is undefined behavior to perform this operation on a | ||
global variable which has not been initialized, except the global variable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing "if": "except if the global variable"
Outline heap objects to global variables.
Mainly this is done for array literals.
This new optimization creates a statically initialized global variable which is the allocated object.
The alloc_ref instruction is replaced by a global_value instruction.
This optimization gives significant performance improvements for large array literals (e.g. for the ArrayLiteral benchmark).