Skip to content

Consider merging values and targets fields on MIR switchInt? #65693

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

Closed
wesleywiser opened this issue Oct 22, 2019 · 5 comments · Fixed by #77796
Closed

Consider merging values and targets fields on MIR switchInt? #65693

wesleywiser opened this issue Oct 22, 2019 · 5 comments · Fixed by #77796
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@wesleywiser
Copy link
Member

Writing some kinds of MIR passes that care about values and targets on switchInt can be a bit messy (for example, passes that remove dead branches). @oli-obk has the idea:

oli: I like it. The actual elimination is a bit convoluted, but I see no way around it. Maybe the values and targets fields of the SwitchInt variant could be merged into a single list, then one could use retain on the list

oli: the otherwise branch would then need a custom field, but that would be fine I think

oli: not sure how much other could would be affected by that (I think there's some code that just wants to know about all targets, but that could be helped by creating an accessor function returning an iterator)

@wesleywiser wesleywiser added the A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html label Oct 22, 2019
@Centril Centril added C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 22, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Jul 14, 2020

The mentioned values and targets lists are https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/mir/terminator/enum.TerminatorKind.html#variant.SwitchInt.field.values

My sketch of an idea would keep the targets list, but make it a Vec<Branch> where struct Branch { value: u128, target: BasicBlock }. There'd be an additional otherwise: BasicBlock field in the SwitchInt variant. This will affect a lot of optimizations, but I expect that all changes will be very mechanical changes.

@eddyb
Copy link
Member

eddyb commented Jul 14, 2020

@oli-obk isn't it the way it is today to optimize the space usage? (not saying that's a good reason, just that I thought it was intentional)

EDIT: also, there are some things that expect a Cow<[BasicBlock]> to exist for targets, so we'd have to allocate to get that.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 15, 2020

Hmm... true, allocating a bunch of (u128, u32) after each other is more expensive than having two vectors. Also a bunch of the values fields get initialized with a slice from global memory.
Maybe just creating a wrapper type around these two fields and giving it an API that doesn't expose the internal representation would make more sense? We can also try making target a SmallVec since a lot of things have just two branches.

EDIT: also, there are some things that expect a Cow<[BasicBlock]> to exist for targets, so we'd have to allocate to get that.

these could probably also take an impl Iterator<item = BasicBlock> or similar

@eddyb
Copy link
Member

eddyb commented Jul 20, 2020

@oli-obk The problem is that they're returned, so you'd need something like a many-cased enum of different iterators. It could work, it was just cleaner to use Cow<[_]> I think.

@oli-obk
Copy link
Contributor

oli-obk commented Jul 20, 2020

Maybe just creating a wrapper type around these two fields and giving it an API that doesn't expose the internal representation would make more sense? We can also try making target a SmallVec since a lot of things have just two branches.

Ok, so I think this is the only actionable item from this issue then.

@bors bors closed this as completed in afb4514 Oct 13, 2020
ebroto pushed a commit to ebroto/rust-clippy that referenced this issue Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-cleanup Category: PRs that clean code up or issues documenting cleanup. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants