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

Extract queries for the trait system operations that are performed in trans #44891

Closed
nikomatsakis opened this issue Sep 27, 2017 · 13 comments
Closed
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-incr-comp Working group: Incremental compilation WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804

Comments

@nikomatsakis
Copy link
Contributor

Currently, there are a number of trait system operations that are used by the code in trans. Currently, they are invoked directly, and behind the scenes they use a DepTrackingMap to memoize across multiple calls. It would be better for incremental if they were converted into named queries. Effectively the goal is to remove the trans_trait_caches field of the tcx. For these particular operations, since they do not work on types that include inference variables, this should be fairly straight-forward:

The idea would to take the following steps:

  • Define a query for trans_fulfill_obligation.
  • With respect to the normalization that is taking place here, I think a wee bit of refactoring is in order before we introduce the query. The actual setup here is a bit confused and probably could use even more refactoring, but for the purposes of this issue, we can do something fairly tailored.
    • Step 1: Rename the method normalize_associated_type() to normalize_associated_type_in(). This method is defined here. It only has a handful of callers (as you can see with a quick rg \.normalize_associated_type\().
      • This is a generic function that will normalize associated types in all kinds of things.
    • Step 2: Define a query normalize_ty(Ty<'tcx>) -> Ty<'tcx> that simply invokes normalize_associated_types_in. This is basically that function in query form, but specialized to inputs of type Ty<'tcx>.
    • Step 3. Replace these lines that invoke memoize()) with code that just invokes the query self.tcx.normalize_ty(ty).

For bonus points, we might consider converting the following functions into queries. It seems like they could benefit from caching:

  • traits::get_vtable_methods (definition)
    • would have to be converted to return a vector
  • traits::normalize_and_test_predicates
  • trans_normalize
  • trans_apply_param_substs

It's unclear though if this is a good idea. I'd keep those for a later PR so we can do some experiments with performance and memory use.

@nikomatsakis nikomatsakis added E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. WG-incr-comp Working group: Incremental compilation WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804 T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 27, 2017
@wesleywiser
Copy link
Member

I'd like to take a shot at this if that's ok.

@nikomatsakis
Copy link
Contributor Author

cc @michaelwoerister

@nikomatsakis
Copy link
Contributor Author

@wesleywiser definitely! Feel free to reach out on gitter if you have any questions along the way. You can also leave comments here, but I have a hard time keeping up with GH notifications, so that can sometimes lead to a long lag in getting a reply. =)

@nikomatsakis
Copy link
Contributor Author

nikomatsakis commented Sep 27, 2017

Oh one other thing. There is no reason to do this all at once. In particular, it would be fine to open a PR that just tackles trans_fulfill_obligation, and then tackle the second one later.

@wesleywiser
Copy link
Member

Thanks! I'll definitely work on trans_fulfill_obligation first.

@aidanhs aidanhs added the C-cleanup Category: PRs that clean code up or issues documenting cleanup. label Sep 28, 2017
@wesleywiser
Copy link
Member

I'm working on the first part of creating a query for trans_fulfill_obligation. I've added a query which returns a Vtable because that's what trans_fulfill_obligation returns but now I'm getting this error:

error[E0277]: the trait bound traits::Vtable<'_, ()>: rustc_data_structures::stable_hasher::HashStable<ich::hcx::StableHashingContext<'_>> is not satisfied

(Full error here)
I understand that this means that Vtable doesn't implement HashStable and I'll need to implement that. I just wanted to check in and make sure that's the right approach. Is there some other value I should be returning instead of Vtable that's stable hash friendly?

@michaelwoerister
Copy link
Member

Copying from gitter to make sure you see it:

You're on the right path. Returning a Vtable is the right way to go and implementing HashStable is also the correct thing to do.
The implementation will probably end up looking something like https://github.com/rust-lang/rust/blob/master/src/librustc/ich/impls_ty.rs#L206

@Maaarcocr
Copy link
Contributor

I would like to work on turning normalize_associated_type() to a query!

@theotherjimmy
Copy link
Contributor

I'm working on traits::get_vtable_methods

wesleywiser added a commit to wesleywiser/rust that referenced this issue Oct 8, 2017
@theotherjimmy
Copy link
Contributor

Oh, I #45137 does the traits::get_vtable_methods => VtableMethods query conversion.

bors added a commit that referenced this issue Oct 12, 2017
…tsakis

Turn `trans_fulfill_obligation` into a query

Part of #44891
bors added a commit that referenced this issue Oct 24, 2017
Create NormalizeTy query

As part of the effort to solve #44891,  I've created the normalize_ty query.

As outlined in the issue this meant:

- renamed `normalize_associated_type()` to `normalize_associated_type_in()`
- created the `normalize_ty` query
- substituted the use of memoize with the query

This PR is not ready. While running tests, one of the incremental ones failed. [This](https://pastebin.com/vGhH6bv6) is the error I got.
@BurntPizza
Copy link
Contributor

I've query-ifed normalize_and_test_predicates, which you can see here: https://github.com/BurntPizza/rust/tree/query-snatp
I'm not noticing any timing difference on some quick by-hand benchmarks, but I'm not looking at memory usage. or anything incremental. Should I make a PR?
Also, @nikomatsakis , I want to thank you for such clear and actionable instructions. I've had "contribute to rustc" on the "I'd like to do that" list for a while, but this got me over the edge.

@michaelwoerister
Copy link
Member

Should I make a PR?

Sure, go ahead! Thanks, @BurntPizza!

bors added a commit that referenced this issue Jan 8, 2018
Make normalize_and_test_predicates into a query

From #44891.

I'm not real solid on how `dep_graph` stuff works, but if a node is going to have a key (again, not sure how important that is), then the key needs to be `Copy`. So since `normalize_and_test_predicates` only had one out-of-module use, I changed that call site to use a new function, `substitute_normalize_and_test_predicates` which is the query and enables having the arguments be `Copy`. Hopefully this makes sense.

r? @nikomatsakis
and/or @michaelwoerister
@nikomatsakis
Copy link
Contributor Author

I feel like this "got done" enough to close this general issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-incr-comp Working group: Incremental compilation WG-traits Working group: Traits, https://internals.rust-lang.org/t/announcing-traits-working-group/6804
Projects
None yet
Development

No branches or pull requests

8 participants