-
Couldn't load subscription status.
- Fork 2.5k
Proposal to add a move(x) function for ending local variable lifetimes #1693
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
Conversation
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.
Two small nits.
proposals/nnnn-move-function.md
Outdated
| ``` | ||
|
|
||
| Notice how the compiler gives us all of the information that we need to resolve | ||
| this: it tells us where the move was and gives tells us the later uses that |
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.
gives tells looks like a thought stream being crossed.
proposals/nnnn-move-function.md
Outdated
| } | ||
| ``` | ||
|
|
||
| we get separable nice diagnostics: |
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 am not sure if nice is needed here. But I am fine with it staying ; ).
|
Hi @gottesmm and @atrick, I'll be managing the review for this proposal! I just finished going through the pitch thread and reading the updated proposal. Here are my notes on reviewers' feedback that I think you should consider addressing in the proposal:
Let me know if you have any questions! I'll schedule this for review once you've had a chance to consider my notes. |
proposals/nnnn-move-function.md
Outdated
| Notice how in the above, we are able to use `x` both in the true block AND the | ||
| continuation block since over all paths, x now has a valid value. | ||
|
|
||
| The value based analysis uses Ownership SSA to determine if values are used |
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.
Is there a resource that can be linking out to here for folks who are not familiar with Ownership SSA?
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 don't think we should be referring to Ownership SSA in a language-level document. We should focus on what the compiler does, not how it does it.
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.
On the whole, the examples are good, but there's far too much discussion of how your particular implementation of the proposed feature works.
Here's a way to think about it: Pretend that some other group had implemented a from-scratch implementation of a Swift compiler. That compiler might not use SIL nor OSSA nor any other particular internal technology, but everything in this document should still be relevant to that other implementation.
proposals/nnnn-move-function.md
Outdated
| useY(y) // do some stuff with local variable y | ||
| useX(x) // error, x's lifetime was ended at [1] | ||
|
|
||
| // Ends lifetime of y. Since _ is no-op, we perform an actual release here. |
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.
Whether there is a "release" depends on the type of x. Focus on the language implications:
Ends the lifetime of the variable `y`. Assigning to `_` avoids creation of a new lifetime.
I would also through here be careful to distinguish the lifetime of a "variable" from the lifetime of the "value" that may be temporarily stored in that value.
proposals/nnnn-move-function.md
Outdated
|
|
||
| In this document, we propose adding a new function called `move` to the swift | ||
| standard library, which ends the lifetime of a specific local `let`, | ||
| local `var`, or `consuming` parameter. In order to enforce this, the compiler will |
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.
Overall suggestion for making markup documents easier to review: Put a line break after every full stop.
I suggest you add a sentence here after "parameter."
This gives developers a way to control retain/release operations and avoid
unnecessary copy-on-write behaviors in performance-critical code.
Then this paragraph will provide a good summary: A sentence saying what you propose, a sentence about why, and a sentence summarizing how the compiler will support this operation.
proposals/nnnn-move-function.md
Outdated
| useY(y) // error, y's lifetime was ended at [2] | ||
| ``` | ||
|
|
||
| This allows the user to influence the uniqueness of COW data structures |
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 would start the "Motivation" section here, and reword things some to focus
on the problems that you're trying to solve:
Performance-critical code can suffer from unexpected copy-on-write operations
and unnecessary retain/release calls.
Consider the following array/uniqueness example:
proposals/nnnn-move-function.md
Outdated
| } | ||
| ``` | ||
|
|
||
| in the example above, without `move`, `y`'s formal lifetime extends to the end of |
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.
Capitalize "In"
proposals/nnnn-move-function.md
Outdated
| ``` | ||
|
|
||
|
|
||
| In the future, we may add support for globals and stored properties, although |
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.
Again, you're talking about the implementation. You need to explain why you do not think the feature requires this support today.
Hmmm.... This paragraph might be better off moved to "alternatives considered".
proposals/nnnn-move-function.md
Outdated
| } | ||
| ``` | ||
|
|
||
| Builtin.move is a hook in the compiler to force emission of special SIL "move" |
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 think you should delete the rest of this section from this point.
At most, I would like to see a brief paragraph that says in essence that "Builtin.move() causes the compiler to perform additional data flow analysis to verify that the argument is not in fact used after this point." The details of how that is implemented in this particular compiler is not relevant; a different compiler may implement it in very different ways.
proposals/nnnn-move-function.md
Outdated
| an SSA based analysis that determines if any uses of an address are reachable | ||
| from a move. All of these are already in tree and can be used today by invoking | ||
| the stdlib non-API function `_move` on a local let or move. *NOTE* This function | ||
| is always emit into client and transparent so there isn't an ABI impact so it is |
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.
This NOTE is useful to keep, since it explains the library implementation shown at the top of this section.
proposals/nnnn-move-function.md
Outdated
|
|
||
| None, this is additive. | ||
|
|
||
| ## Alternatives considered |
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.
This is a reasonable place to explain why you don't think global support is necessary and could be considered separately.
proposals/nnnn-move-function.md
Outdated
| term that has already been used in other Swift standard library APIs such as | ||
| the `UnsafeMutablePointer.move*` family of methods that move a value out of | ||
| memory referenced by a pointer. Declaring it as a function also minimizes the | ||
| potential impact to the language syntax. We are however open to discussing |
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.
Strike the sentence beginning "We are however open ..."
proposals/nnnn-move-function.md
Outdated
| In this case, we get the following output from the compiler as expected: | ||
|
|
||
| ```swift | ||
| test.swift:10:7: error: 'y' used after being moved |
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 would drop this entire example and just put the error message directly into the example above:
useYAgain(y) // Error: 'y' used after being moved
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.
Done.
|
Definitely improved. Still some work to do, but it's improving nicely. |
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.
Thank you!
proposals/0366-move-function.md
Outdated
|
|
||
| If the binding is a `var`, the analysis additionally allows for code to | ||
| conditionally reinitialize the var and thus be able to use it in positions | ||
| that are dominated by the reinitialization. continuation path. Consider the |
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.
Extra words?
No description provided.