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

Cleanup the shim code #47865

Merged
merged 8 commits into from
Feb 5, 2018
Merged

Cleanup the shim code #47865

merged 8 commits into from
Feb 5, 2018

Conversation

Manishearth
Copy link
Member

@Manishearth Manishearth commented Jan 30, 2018

r? @eddyb

@Manishearth Manishearth force-pushed the cleanup-shim branch 3 times, most recently from d658161 to 973881d Compare January 30, 2018 06:26
@Manishearth
Copy link
Member Author

r? @nikomatsakis

This is a precursor to my patches for #47796 (which may need an rfc). @eddyb noticed that the tuple shim code was suboptimal and since I'm fixing that anyway might as well get these cleanups through.

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Jan 30, 2018
cleanup: BasicBlock
) -> Place<'tcx> {
cleanup: BasicBlock,
place: Place<'tcx>
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@@ -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
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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.

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 30, 2018
@Manishearth
Copy link
Member Author

fixed

/// 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
Copy link
Member

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

cleanup: BasicBlock
) -> Place<'tcx> {
cleanup: BasicBlock,
dest: Place<'tcx>
Copy link
Member

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.

@@ -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);
Copy link
Member

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

/// 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
Copy link
Member

Choose a reason for hiding this comment

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

This comment is unfinished.

@@ -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())),
Copy link
Member

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.

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);
Copy link
Member

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?

_ => bug!("only tuples and closures are accepted"),
};
fn tuple_like_shim<I>(&mut self, dest: Place<'tcx>,
rcvr: Place<'tcx>, tys: I)
Copy link
Member

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.

@Manishearth Manishearth force-pushed the cleanup-shim branch 2 times, most recently from 9dbce3f to fbb26ee Compare January 30, 2018 13:42
@Manishearth Manishearth changed the title Cleanup the shim code to write directly to RETURN_PLACE Cleanup the shim code Jan 30, 2018
@Manishearth
Copy link
Member Author

done

}

previous_place = Some((place, cleanup_block));
Copy link
Member

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.

@Manishearth Manishearth force-pushed the cleanup-shim branch 2 times, most recently from b113fed to 370315d Compare January 30, 2018 14:20

// BB #(2i)
// `returns[i] = Clone::clone(&rcvr.i);`
// `return.i = Clone::clone(&src.i);`
Copy link
Member

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

@eddyb
Copy link
Member

eddyb commented Jan 30, 2018

cc @arielb1

@Manishearth Manishearth force-pushed the cleanup-shim branch 3 times, most recently from dcb07dd to ca17162 Compare January 30, 2018 15:29
@Manishearth Manishearth force-pushed the cleanup-shim branch 2 times, most recently from c94151b to 633e401 Compare January 30, 2018 16:00
Copy link
Contributor

@nikomatsakis nikomatsakis left a 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);
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok good enough

Copy link
Member Author

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)

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

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?

@@ -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);
Copy link
Contributor

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.

Copy link
Contributor

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

@Manishearth
Copy link
Member Author

done

@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Feb 1, 2018

📌 Commit 8a8f91b has been approved by nikomatsakis

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 1, 2018
@bors
Copy link
Contributor

bors commented Feb 5, 2018

⌛ Testing commit 8a8f91b with merge dd29d3d...

bors added a commit that referenced this pull request Feb 5, 2018
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
@bors
Copy link
Contributor

bors commented Feb 5, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing dd29d3d to master...

@bors bors merged commit 8a8f91b into rust-lang:master Feb 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants