-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Cleanup the shim code #47865
Cleanup the shim code #47865
Conversation
d658161
to
973881d
Compare
src/librustc_mir/shim.rs
Outdated
cleanup: BasicBlock | ||
) -> Place<'tcx> { | ||
cleanup: BasicBlock, | ||
place: Place<'tcx> |
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 you name this dest
?
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
973881d
to
5bacd19
Compare
src/librustc/mir/mod.rs
Outdated
@@ -1520,6 +1520,9 @@ pub enum AggregateKind<'tcx> { | |||
/// active field number and is present only for union expressions | |||
/// -- e.g. for a union expression `SomeUnion { c: .. }`, the | |||
/// active field index would identity the field `c` | |||
/// | |||
/// For enums, the second field is the index of the variant | |||
/// within AdtDef::fields |
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.
Isn't this also 5 lines above?
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.
The confusion was "is it 0-indexed variant index, 1-indexed variant index, or the discriminant value". Basically, the fact that it says that it's zero for structs and unions suggests that it's perhaps nonzero for enums, and it's better if folks don't need to verify this.
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.
You can just edit that part instead of adding more elsewhere... also you wrote AdtDef::fields
instead of variants
. And this shouldn't be in this PR anyway.
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.
Actually structs and unions have exactly one variant so the rephrasing can be even more enum-focused.
fixed |
5bacd19
to
510b3d2
Compare
src/librustc/mir/mod.rs
Outdated
/// The second field is variant number (discriminant), | ||
/// i.e. the index of the variant within AdtDef::variants. | ||
/// | ||
/// It's equal to 0 for struct and union expressions. The fourth field 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.
The newline seems misplaced. Also, we should avoid using "discriminant", because that's a different thing that may not be equal to the variant index. All the relevant code uses "variant index" (not "variant number").
src/librustc_mir/shim.rs
Outdated
cleanup: BasicBlock | ||
) -> Place<'tcx> { | ||
cleanup: BasicBlock, | ||
dest: Place<'tcx> |
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.
Please put this before rcvr_field
and rename that to src
.
510b3d2
to
5602a2d
Compare
src/librustc_mir/shim.rs
Outdated
@@ -540,7 +536,9 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { | |||
// `let cloned = Clone::clone(rcvr[beg])`; | |||
// Goto #3 if ok, #5 if unwinding happens. | |||
let rcvr_field = rcvr.clone().index(beg); | |||
let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5)); | |||
let cloned = self.make_place(Mutability::Not, ty); |
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.
Please pass in ret_field
from below as the destination (and rename it to dest_elem
- also rcvr_field
should be src_elem
).
07b8813
to
92b533a
Compare
src/librustc_mir/shim.rs
Outdated
/// Gives the index of an upcoming BasicBlock, with an offset. | ||
/// offset=0 will give you the index of the next BasicBlock, | ||
/// offset=1 will give the index of the next-to-next block, | ||
/// offset=-1 will give |
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 comment is unfinished.
src/librustc_mir/shim.rs
Outdated
@@ -571,7 +573,7 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { | |||
// `return ret;` | |||
let ret_statement = self.make_statement( | |||
StatementKind::Assign( | |||
Place::Local(RETURN_PLACE), | |||
dest, | |||
Rvalue::Use(Operand::Move(ret.clone())), |
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.
Please make this function also write in-place.
src/librustc_mir/shim.rs
Outdated
let rcvr_field = rcvr.clone().index(beg); | ||
let cloned = self.make_clone_call(ty, rcvr_field, BasicBlock::new(3), BasicBlock::new(5)); | ||
let src_field = src.clone().index(beg); | ||
let ret_field = ret.clone().index(beg); |
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 you name this dest_field
and put it before src_field
?
src/librustc_mir/shim.rs
Outdated
_ => bug!("only tuples and closures are accepted"), | ||
}; | ||
fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>, | ||
rcvr: Place<'tcx>, tys: I) |
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 should be named src
not rcvr
.
9dbce3f
to
fbb26ee
Compare
done |
src/librustc_mir/shim.rs
Outdated
} | ||
|
||
previous_place = Some((place, cleanup_block)); |
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 should be named previous_field
, I think.
b113fed
to
370315d
Compare
src/librustc_mir/shim.rs
Outdated
|
||
// BB #(2i) | ||
// `returns[i] = Clone::clone(&rcvr.i);` | ||
// `return.i = Clone::clone(&src.i);` |
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.
dest.i
instead of return.i
370315d
to
f48eba7
Compare
cc @arielb1 |
dcb07dd
to
ca17162
Compare
c94151b
to
633e401
Compare
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.
Seems pretty good to me. A few nits/questions.
@@ -294,7 +294,7 @@ fn build_clone_shim<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, | |||
{ | |||
debug!("build_clone_shim(def_id={:?})", def_id); | |||
|
|||
let mut builder = CloneShimBuilder::new(tcx, def_id); | |||
let mut builder = CloneShimBuilder::new(tcx, def_id, self_ty); |
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 test for whatever motivated this commit?
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.
Without this commit everything ICEs. Everything.
Any code instantiating a clone shim of tuples or arrays or closures (libcore does) will break because the type used will be TySelf (not TyArray or TyClosure or whatever) and we'll get errors because we're trying to call .index()
or .field()
on a place that can't be indexed. I can leave a comment to this effect somewhere.
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 mean I can see why this is wrong, but I'm a bit confused -- the code works now, afaik, right? Or is this just patching up some earlier commit of yours?
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 breaks once the change to the array shim is made - anything that depends on the type (e.g. indexing or enum discriminants) starts breaking because it'd see an unsubstituted Self
.
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.
Thsi commit should be earlier in the series I think.
We basically don't hit it because we write to temporary Places and then copy and that works well. I don't recall the exact details, but eddyb can explain.
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.
ok good enough
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.
Oh right yeah this doesn't break the tuple stuff but it does break the array stuff (and the enum stuff from my other PR)
src/librustc_mir/shim.rs
Outdated
@@ -327,8 +327,10 @@ struct CloneShimBuilder<'a, 'tcx: 'a> { | |||
} | |||
|
|||
impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { | |||
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Self { | |||
let sig = tcx.fn_sig(def_id); | |||
fn new(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId, |
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.
Nit: can we use rustfmt-style formatting here?
src/librustc_mir/shim.rs
Outdated
@@ -501,11 +501,11 @@ impl<'a, 'tcx> CloneShimBuilder<'a, 'tcx> { | |||
fn array_shim(&mut self, ty: Ty<'tcx>, len: u64) { | |||
let tcx = self.tcx; | |||
let span = self.span; | |||
let rcvr = Place::Local(Local::new(1+0)).deref(); | |||
let src = Place::Local(Local::new(1+0)).deref(); | |||
let dest = Place::Local(RETURN_PLACE); |
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.
Hmm, I think this is ok, but it's interesting that we can observe the mutations even on unwind etc. Do you know off hand @eddyb if this is already true? Seems like something that we will eventually want to be legal, though. Effectively you're giving in an &mut
to the (uninitialized) RETURN_PLACE
, so we can do what we want there.
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.
But I feel like this has been an interesting question at times in the UCG discussions (when precisely can we reuse places etc).
Otherwise we get random TySelfs there, which means operations on RETURN_PLACE end up breaking down badly.
done |
633e401
to
8a8f91b
Compare
@bors r+ |
📌 Commit 8a8f91b has been approved by |
Cleanup the shim code - We now write directly to `RETURN_PLACE` instead of creating intermediates - `tuple_like_shim` takes an iterator (used by #47867) - `tuple_like_shim` no longer relies on it being the first thing to create blocks, and uses relative block indexing in a cleaner way (necessary for #47867) - All the shim builders take `dest, src` arguments instead of hardcoding RETURN_PLACE r? @eddyb
☀️ Test successful - status-appveyor, status-travis |
RETURN_PLACE
instead of creating intermediatestuple_like_shim
takes an iterator (used by [WIP] Make derive(Clone) memcpy on enum variants containing only Copy types #47867)tuple_like_shim
no longer relies on it being the first thing to create blocks, and uses relative block indexing in a cleaner way (necessary for [WIP] Make derive(Clone) memcpy on enum variants containing only Copy types #47867)dest, src
arguments instead of hardcoding RETURN_PLACEr? @eddyb