Skip to content
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

Merged
merged 12 commits into from Aug 23, 2017
Merged

Conversation

eeckstein
Copy link
Member

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).

@eeckstein
Copy link
Member Author

@swift-ci Please test

@swift-ci
Copy link
Collaborator

Build failed
Swift Test Linux Platform
Git Sha - 217411f061a8a164a42228bcfad7980603809b25

@swift-ci
Copy link
Collaborator

Build failed
Swift Test OS X Platform
Git Sha - 217411f061a8a164a42228bcfad7980603809b25

docs/SIL.rst Outdated

A global can have a static initializer if it's initial value can be
Copy link
Member

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``.
Copy link
Member

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_globals?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting idea

Copy link
Member

@jckarter jckarter left a comment

Choose a reason for hiding this comment

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

Looks great overall!

@eeckstein
Copy link
Member Author

@swift-ci test and merge

1 similar comment
@eeckstein
Copy link
Member Author

@swift-ci test and merge

@eeckstein
Copy link
Member Author

@swift-ci smoke test and merge

1 similar comment
@eeckstein
Copy link
Member Author

@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.
…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.
@eeckstein
Copy link
Member Author

@swift-ci smoke test and merge

1 similar comment
@eeckstein
Copy link
Member Author

@swift-ci smoke test and merge

@swift-ci swift-ci merged commit 993c353 into apple:master Aug 23, 2017
@eeckstein eeckstein deleted the static-initializers branch August 23, 2017 17:14
@shahmishal
Copy link
Member

https://ci.swift.org/job/oss-swift-package-osx/89/

/Users/buildnode/jenkins/workspace/oss-swift-package-osx/swift/test/SILOptimizer/static_arrays.swift:55:15: error: expected string not found in input
// CHECK-DAG: integer_literal $Builtin.Int64, 1
              ^
<stdin>:116:2: note: scanning from here
 %0 = integer_literal $Builtin.Int32, 2 // user: %1
 ^
<stdin>:118:7: note: possible intended match here
 %2 = integer_literal $Builtin.Int32, 1 // user: %3
      ^

--

********************
Testing: 0 .. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 
Testing Time: 986.21s
********************
Failing Tests (1):
    Swift(watchsimulator-i386) :: SILOptimizer/static_arrays.swift


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
Copy link
Contributor

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"

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.

None yet

5 participants