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

Take impl Into<DefId> for query methods on TyCtxt #70961

Merged
merged 8 commits into from
Apr 13, 2020

Conversation

ecstatic-morse
Copy link
Contributor

Alternative implementation of #70956. cc #70853.

@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 9, 2020
@ecstatic-morse
Copy link
Contributor Author

r? @Zoxc

@rust-highfive rust-highfive assigned Zoxc and unassigned petrochenkov Apr 9, 2020
@ecstatic-morse
Copy link
Contributor Author

cc @marmeladema @eddyb

Comment on lines 250 to 252
fn inner(this: TyCtxtAt<$tcx>, key: DefId) -> $V {
get_query::<queries::$name<'_>, _>(this.tcx, this.span, key)
}
Copy link
Member

Choose a reason for hiding this comment

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

Is get_query inlined? This does nothing otherwise.

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Apr 9, 2020

Choose a reason for hiding this comment

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

This was an attempt to address @Zoxc's comment. I don't understand why it's beneficial for the caller of get_query to be monomorphic, since it is #[inline(never)]. Perhaps I misunderstood? ensure_query is not #[inline(never)] at the moment, however.

Copy link
Contributor

Choose a reason for hiding this comment

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

IFAIK, the LLVM codegen of the {get,force,ensure}_query functions is responsible for most of the compile time for librustc_middle. It happens once for each of the 170 queries. By making the caller generic, part of this codegen may be replicated in caller crates. This would have a serious impact on rustc's compile time. Please correct me if #[inline(never)] is enough to prevent this.

Copy link
Member

Choose a reason for hiding this comment

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

The wrapper functions are already #[inline(always)] so they would be codegen'd cross-crate even when not generic.

Comment on lines 246 to 249
(TyCtxtAt<$tcx:tt>, $(#[$attr:meta])* $name:ident(DefId) -> $V:ty) => {
$(#[$attr])*
#[inline(always)]
pub fn $name(self, key: impl Into<DefId>) -> $V {
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 make a type macro and use that to pattern-match on just the key type?
I think we can just use .into() in all methods, as it will compile to a noop when the types are the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea. It won't work if we do want a monomorphic helper function, but maybe we don't.

Copy link
Member

Choose a reason for hiding this comment

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

get_query is #[inline(never)] (I just checked) so you don't need this wrapper.

@nikomatsakis
Copy link
Contributor

Per rust-lang/team#316, r? @eddyb -- let me know @eddyb if you'd prefer someone else.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Apr 10, 2020

@eddyb The last two commits implement your suggestion for simplifying the macro and replace Into with a newly defined IntoQueryParam respectively. You can't seal traits with blanket impls, although I suppose since this is only used for DefId we could just have manual impls.

Comment on lines 197 to 199
pub trait IntoQueryParam<P> {
fn into_query_param(self) -> P;
}
Copy link
Member

Choose a reason for hiding this comment

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

In case there was any confusion, "sealing" works like this:

mod sealed {
    pub trait IntoQueryParam<P> {
        fn into_query_param(self) -> P;
    }
}

Then you can refer to it as sealed::IntoQueryParam, and as long as sealed remains private, and you don't reexport IntoQueryParam somewhere, it should be impossible to implement or import the trait, outside of the parent module of the mod sealed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I thought you meant the super-trait patten that's in the API guidelines.

Copy link
Member

Choose a reason for hiding this comment

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

@ecstatic-morse ah I see, that pattern uses a sealed trait for a specific purpose.
Or maybe I'm the only person referring to "pub but in a private mod" as "sealed".

src/librustc_span/def_id.rs Outdated Show resolved Hide resolved
@eddyb
Copy link
Member

eddyb commented Apr 10, 2020

LGTM, but before I approve:

@bors try @rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Apr 10, 2020

⌛ Trying commit 3ac350d8349c9fbb921c9b9ac7f01cf5c8869759 with merge bca1f3800d78009dd283fff15b216fa1edf32996...

Comment on lines -383 to +389
pub fn $name(self, key: $K) {
ensure_query::<queries::$name<'_>, _>(self.tcx, key)
pub fn $name(self, key: query_helper_param_ty!($($K)*)) {
ensure_query::<queries::$name<'_>, _>(self.tcx, key.into_query_param())
Copy link
Member

Choose a reason for hiding this comment

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

Actually, are these changes needed? "ensuring" should be rather rare AFAIK?

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 10, 2020
…e-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 10, 2020
…dle-local-def-id, r=eddyb

rustc_middle: return `LocalDefId` where possible in hir::map module

This changes the return type of the following functions to return a `LocalDefId` instead of a `DefId`:
* opt_local_def_id_from_node_id
* opt_local_def_id
* body_owner_def_id
* local_def_id_from_node_id
* get_parent_id

This is another step in the right direction for rust-lang#70853

This pull request will be followed by another (substantial one) which changes the return type of `local_def_id` function but this change being more invasive, we might want to wait for rust-lang#70956 or rust-lang#70961 (or some other form it) to land first.
@bors
Copy link
Contributor

bors commented Apr 10, 2020

☀️ Try build successful - checks-azure
Build commit: bca1f3800d78009dd283fff15b216fa1edf32996 (bca1f3800d78009dd283fff15b216fa1edf32996)

@rust-timer
Copy link
Collaborator

Queued bca1f3800d78009dd283fff15b216fa1edf32996 with parent 9682f0e, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit bca1f3800d78009dd283fff15b216fa1edf32996, comparison URL.

@marmeladema
Copy link
Contributor

@ecstatic-morse @eddyb is the performance good enough for this to land?

@ecstatic-morse
Copy link
Contributor Author

@marmeladema Too slow IMO. We'll need to micro-optimize or abandon this idea.

}

impl IntoQueryParam<DefId> for LocalDefId {
fn into_query_param(self) -> DefId {
Copy link
Member

Choose a reason for hiding this comment

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

Try adding #[inline(always)] to this (and to_def_id if it doesn't have it).

Although, wait, are we using this at all now? Is the slowdown just from going through the noop impl?

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Apr 11, 2020

Choose a reason for hiding this comment

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

It's used exactly once as a test 6e5a210. I'm skeptical that this will help, since no into_query_param call appears in the optimized binary, and the DefId queries I looked at inline down to get_query. There's no predicting the optimizer though.

@ecstatic-morse
Copy link
Contributor Author

@bors try
@rust-timer queue

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors
Copy link
Contributor

bors commented Apr 11, 2020

⌛ Trying commit 04c91a0 with merge 4db70335bb09ba1a20ddee9e96cc14d6bb67b630...

@bors
Copy link
Contributor

bors commented Apr 12, 2020

☀️ Try build successful - checks-azure
Build commit: 4db70335bb09ba1a20ddee9e96cc14d6bb67b630 (4db70335bb09ba1a20ddee9e96cc14d6bb67b630)

@rust-timer
Copy link
Collaborator

Queued 4db70335bb09ba1a20ddee9e96cc14d6bb67b630 with parent e82734e, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit 4db70335bb09ba1a20ddee9e96cc14d6bb67b630, comparison URL.

@eddyb
Copy link
Member

eddyb commented Apr 12, 2020

Heh that's what it was! r=me if there is no disagreement about doing this.

@eddyb
Copy link
Member

eddyb commented Apr 13, 2020

Eh let's just do it.

@bors r+

@bors
Copy link
Contributor

bors commented Apr 13, 2020

📌 Commit 04c91a0 has been approved by eddyb

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

bors commented Apr 13, 2020

⌛ Testing commit 04c91a0 with merge 5179ebe...

@bors
Copy link
Contributor

bors commented Apr 13, 2020

☀️ Test successful - checks-azure
Approved by: eddyb
Pushing 5179ebe to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Apr 13, 2020
@bors bors merged commit 5179ebe into rust-lang:master Apr 13, 2020
@ecstatic-morse ecstatic-morse deleted the into-def-id branch October 6, 2020 01:42
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.

10 participants