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

instance: polymorphize upvar closures/generators #75255

Conversation

davidtwco
Copy link
Member

This PR modifies how instances are polymorphized so that closures and generators have any closures or generators captured within their upvars also polymorphized.

With the new symbol mangling, a fully polymorphised closure will produce the same symbol regardless of what it was instantiated with. However, when that polymorphised closure captures another closure as an upvar, then the type of that other closure in the upvar substitution wouldn't have been polymorphised. The other closure will still refer to the initial substitutions. Therefore, the polymorphised closure will end up hashing differently but producing the same symbol - triggering assert_symbols_are_distinct in MIR partitioning. The old mangling scheme had a hash at the end that meant this didn't happen (this would still have been an issue, we just didn't have a way to notice).

See this Zulip discussion for further elaboration.

r? @eddyb
cc @lcnr

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 7, 2020
@davidtwco davidtwco force-pushed the polymorphisation-symbol-mangling-v0-upvar-closures branch from a6535d2 to c6f361b Compare August 7, 2020 16:15
@davidtwco davidtwco requested a review from lcnr August 7, 2020 16:15
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

We may want to add a type flag like MAY_POLYMORPHIZE which checks if the ty is or contains a Closure or Generator so we don't have to always looking into the subst.

But apart from that this looks good to me, I don't think @eddyb also needs to look over this 🤔

r=me

src/librustc_middle/ty/instance.rs Outdated Show resolved Hide resolved
src/librustc_middle/ty/instance.rs Show resolved Hide resolved
@davidtwco
Copy link
Member Author

I've addressed those comments, including adding the type flag.

r? @lcnr

@rust-highfive rust-highfive assigned lcnr and unassigned eddyb Aug 7, 2020
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

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

r=me after the nit is fixed

This commit modifies how instances are polymorphized so that closures
and generators have any closures or generators captured within their
upvars also polymorphized - this avoids symbol clashes with the new
symbol mangling scheme.

Signed-off-by: David Wood <david@davidtw.co>
By always polymorphizing substitutions, functions which take closures as
arguments (e.g. `impl Fn()`) can have fewer mono items when some of the
argument closures can be polymorphized.

Signed-off-by: David Wood <david@davidtw.co>
This commit adds a `MAY_POLYMORPHIZE` which checks for closures and
generators so that polymorphization of substs does not need to traverse
every substs.

Signed-off-by: David Wood <david@davidtw.co>
This commit avoids unnecessary calls to `mk_closure` and `mk_generator`
by checking if the polymorphized substs match the original substs.

Signed-off-by: David Wood <david@davidtw.co>
This commit extends previous polymorphization of substs to polymorphize
`FnDef`.

Signed-off-by: David Wood <david@davidtw.co>
@davidtwco davidtwco force-pushed the polymorphisation-symbol-mangling-v0-upvar-closures branch from e9cab0c to ac50d61 Compare August 7, 2020 17:43
@davidtwco
Copy link
Member Author

@bors r=lcnr

@bors
Copy link
Contributor

bors commented Aug 7, 2020

📌 Commit ac50d61 has been approved by lcnr

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

bors commented Aug 7, 2020

⌛ Testing commit ac50d61 with merge 09f4c9f...

@bors
Copy link
Contributor

bors commented Aug 7, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: lcnr
Pushing 09f4c9f to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 7, 2020
@bors bors merged commit 09f4c9f into rust-lang:master Aug 7, 2020
@davidtwco davidtwco deleted the polymorphisation-symbol-mangling-v0-upvar-closures branch August 8, 2020 18:22
davidtwco added a commit to davidtwco/rust that referenced this pull request Aug 9, 2020
This commit restricts the substitution polymorphization added in rust-lang#75255
to only apply to the tupled upvar substitution, rather than all
substitutions, fixing a bunch of regressions when polymorphization is
enabled.

Signed-off-by: David Wood <david@davidtw.co>
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Aug 10, 2020
…xes, r=eddyb

instance: only polymorphize upvar substs

This PR restricts the substitution polymorphization added in rust-lang#75255 to only apply to the tupled upvar substitution, rather than all substitutions, fixing a bunch of regressions when polymorphization is
enabled.

Due to an oversight on my part, when landing rust-lang#75260 and rust-lang#75255, some tests started failing when polymorphization was enabled that I didn't notice until after landing - this PR fixes the regressions from rust-lang#75255. rust-lang#75336 has been filed to make sure that we don't forget to try make this change again in future, as it does enable some optimisations.

r? @lcnr
JohnTitor added a commit to JohnTitor/rust that referenced this pull request Aug 11, 2020
…xes, r=lcnr

polymorphize: constrain unevaluated const handling

This PR constrains the support added for handling unevaluated consts in polymorphization (introduced in rust-lang#75260) by:

- Skipping associated constants as this causes cycle errors.
- Skipping promoted constants when they contain `Self` as this ensures `T` is used in constants of the form `<Self as Foo<T>>`.

Due to an oversight on my part, when landing rust-lang#75260 and rust-lang#75255, some tests started failing when polymorphization was enabled that I didn't notice until after landing - this PR fixes the regressions from rust-lang#75260.

r? @lcnr
@cuviper cuviper added this to the 1.47.0 milestone May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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.

6 participants