MIR move elimination#3943
Conversation
|
For It seems to be extremely strange to say I need a |
We can drop The purpose of a |
Wouldn't it make more sense to have something similar to |
|
Calling let x = 1;
let y = &x;
drop(move x);
let z = *y; // Fails because x was moved. Removing `move` fixes this.Anyways, |
|
|
|
||
| The proposed behavior of freeing a local variable's allocation on move only applies when the entire variable is moved. This is not the case when only a part of the variable is moved (e.g. only one field of a struct) because a re-initialized field must retain the address it had before, reintroducing the same NB issue. | ||
|
|
||
| Even in the case where all of the fields of a local variable have been moved out one-by-one, the local will not be freed. |
There was a problem hiding this comment.
Even in the case where all of the fields of a local variable have been moved out one-by-one, the local will not be freed.
Why not?
|
|
||
| Even in the case where all of the fields of a local variable have been moved out one-by-one, the local will not be freed. | ||
|
|
||
| With that said, we would like to keep the door open for potentially switching to operational semantics with NB in the future. So although the proposed opsem does not consider accessing a moved field as UB, we would like users to avoid relying on this behavior since it may change in the future. |
There was a problem hiding this comment.
Why can't we say that accessing the moved field is UB? Because we don't have NB, the compiler can't exploit that UB by stashing another allocation with an observable address in the empty space. But that doesn't mean it can't still be UB detected by Miri, if we want users to avoid relying on it. And the compiler could even make use of the UB, to stash an allocation whose address it can prove is never observed.
There was a problem hiding this comment.
There's no reason we can't do it, it's just that I am not doing so in this RFC and instead leaving to future work (I will update the future possibilities section with this). There are 2 main reasons for this:
-
Adding support for partial moves makes the opsem (and by extension Miri) much more complex since we now needs to track which bytes of a local have been moved out and become "inactive" (UB to access). What happens to the padding of a struct if one field is moved out? What happens to the discriminant of an enum like
Option<T>if theSomevalue has been moved out and the layout is optimized (the discriminant occupied the same bytes as the value)? These are all questions that would have to be answered. -
The proposed MIR optimization pass can't easily take advantage of this, and even if it could (while respecting address observation rules) then I expect the benefit over the existing proposed pass would be minimal. It's just not worth the extra complexity of tracking lifetimes separately for every field of a local.
|
|
||
| ## Drawbacks | ||
|
|
||
| ### `Copy` is no longer "free" |
There was a problem hiding this comment.
This makes it sound as though types with Copy would be less efficient than with the status quo, but as far as I understand this is not the case. Copy types would at worst be as efficient as with the status quo.
This sounds more like a limitation (AKA an opportunity for future work) than a drawback.
It's also not clear to me why the mere act of implementing Copy for a type would inhibit the optimization in practice. Surely code that was originally written for a non-Copy type would have an access pattern where every copy could be optimized into a move?
There was a problem hiding this comment.
This makes it sound as though types with
Copywould be less efficient than with the status quo, but as far as I understand this is not the case.Copytypes would at worst be as efficient as with the status quo.
That is correct.
This sounds more like a limitation (AKA an opportunity for future work) than a drawback.
Sure, I can move this to the future work section.
It's also not clear to me why the mere act of implementing
Copyfor a type would inhibit the optimization in practice. Surely code that was originally written for a non-Copytype would have a copy pattern where every copy could be optimized into a move?
It inhibits the optimization if the value has been borrowed. A move invalidates borrows whereas a copy doesn't. I've update the text with an example.
There was a problem hiding this comment.
Finally had the time for a first read over this.
I haven't yet had the chance to look at your MiniRust patch, that may answer some of the questions that came up here. But of course ideally the RFC itself is already crystal clear about the intended MR semantics. :)
|
|
||
| #### Initialization | ||
|
|
||
| `StorageLive` no longer allocates the underlying memory for a local. Instead, any MIR statement or terminator which writes to a place that has no `Deref` projections[^2] will implicitly allocate the storage for that local[^3] before writing to it. This has no effect if the storage for that local is already allocated[^4]. |
There was a problem hiding this comment.
To make it sound to generate LLVM lifetime markers from StorageLive, we will need to do something in the opsem for StorageLive.
Reading on, you clarify this later, but the order in which you explain this is confusing.
|
|
||
| `move` operands only have the effect of de-allocating the storage of a local when used with a bare, unprojected local. If the local has projections then `move` behaves identically to `copy`. | ||
|
|
||
| #### New semantics of `StorageLive` and `StorageDead` |
There was a problem hiding this comment.
This RFC also inevitable deeply changes the semantics of assignments. There should be a section on that.
And this section is where I have the biggest disagreement with the RFC: the RFC proposes to make (de)allocation of locals an implicit side-effect of (some) place/value expressions. I think that's a problem for multiple reasons
- It breaks the property that expression evaluation can be arbitrarily reordered with each other, making it harder to reason about MIR.
- For places it's not even clear how evaluation should work. I can make guesses but the RFC is not very clear about it. (EDIT: This one is answered by the MiniRust patch.)
I would propose that instead the (de)allocation happens as part of the assignment operator. The way I think about it is that the operator performs the following steps:
- evaluate the value expression (RHS)
- deallocate a set of locals
- allocate a set of locals
- evaluate the place expression (LHS)
- store the value into the place
In MiniRust, it's probably easiest to just annotate those two sets of locals as part of the syntax of assignment itself. In MIR, we might want to make that implicit. That would then be able to capture the syntactic ruls you have proposed elsewhere:
- for the locals to deallocate: if the RHS is
Use(Move(local)), thenlocalis deallocated, otherwise nothing gets deallocated. (Or is it really allMove(local)operands on the RHS? I can see it being useful for aggregate initialization but I doubt this makes much sense for binops.) - for the locals to allocate: if the LHS is
local.non-deref-projs, thenlocalgets allocated, otherwise nothing gets allocated
This RFC proposes changes to Rust's operational semantics and MIR representation to enable elimination of unnecessary copies of local variables. Specifically, it makes accessing memory after a move undefined behavior, and redefines the allocation lifetime of local variables to be tied to their initialized state rather than their lexical scope. Finally, it introduces a new MIR optimization pass which exploits these guarantees to eliminate copies between locals when it is safe to do so.
Important
Since RFCs involve many conversations at once that can be difficult to follow, please use review comment threads on the text changes instead of direct comments on the RFC.
If you don't have a particular section of the RFC to comment on, you can click on the "Comment on this file" button on the top-right corner of the diff, to the right of the "Viewed" checkbox. This will create a separate thread even if others have commented on the file too.
Rendered