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

MIR: use HirId instead of NodeId to avoid cycles while inlining #71285

Merged
merged 1 commit into from
Apr 21, 2020

Conversation

ljedrz
Copy link
Contributor

@ljedrz ljedrz commented Apr 18, 2020

I wanted to see if I could limit the number of uses of NodeId when HirId is available and I saw that some of the MIR Inliner code could use Span instead of NodeId, not unlike in #71197.

If I'm understanding the reason for not calling optimized_mir in incremental builds here correctly, this change could also allow us to do so.

This change could affect performance, so if this approach makes sense, a perf run is probably a good idea.

@rust-highfive
Copy link
Collaborator

r? @varkor

(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 18, 2020
@ljedrz ljedrz force-pushed the mir_inline_span_for_optimized_mir branch from b716e9e to 81d7283 Compare April 18, 2020 13:44
@ljedrz ljedrz force-pushed the mir_inline_span_for_optimized_mir branch from 81d7283 to a270ba6 Compare April 18, 2020 18:24
@varkor
Copy link
Member

varkor commented Apr 19, 2020

This seems reasonable, but I'm not familiar enough with the subtleties to be sure, so reassigning to the reviewer of the linked PR.

r? @ecstatic-morse

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Apr 19, 2020

#71197 only needed to sort things so that error messages would be produced in source order. In this case, we need to define an arbitrary order for all callable functions to avoid inlining cycles. Why not use the HirId itself for this?

Also, I don't believe perf runs have MIR inlining enabled. If that's true, any slowdown caused by looking up all these spans wouldn't have been visible.

@ljedrz
Copy link
Contributor Author

ljedrz commented Apr 19, 2020

If the order can be arbitrary then it makes sense to just use HirIds - I'll run the tests. Wouldn't this mean it can work in incremental builds too? HirIds are stable after all.

@ecstatic-morse
Copy link
Contributor

ecstatic-morse commented Apr 19, 2020

I don't know. The goal is to have the exact same optimizations applied across incremental runs. If you can prove that is indeed what happens, then have at it. However, I suspect that if it were that simple, HirId would have been used in the first place.

@bjorn3
Copy link
Member

bjorn3 commented Apr 19, 2020

HirIds are stable after all.

A part of a HirId is a DefId, which is basically an interned DefPath. While a DefPath is guaranteed to be stable, DefIds are just numbered sequentially starting from 0 in order of lowering items to hir:

fn allocate(&mut self, key: DefKey, def_path_hash: DefPathHash) -> DefIndex {
let index = {
let index = DefIndex::from(self.index_to_key.len());
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
self.index_to_key.push(key);
index
};
self.def_path_hashes.push(def_path_hash);
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
index
}

This means that if functions are reordered, comparing the order of the HirId will give a different result without the incremental cache system knowing it. This will likely not be a problem at the moment though, as I think the changing span would be enough to cause a recompilation. However if both functions are given DUMMY_SPAN by a proc macro this invalidation won't happen anymore.

@ljedrz ljedrz force-pushed the mir_inline_span_for_optimized_mir branch from a270ba6 to 144ca6b Compare April 19, 2020 19:12
@ljedrz ljedrz force-pushed the mir_inline_span_for_optimized_mir branch from 144ca6b to 3c455fe Compare April 19, 2020 19:14
@ecstatic-morse ecstatic-morse changed the title MIR: use Span instead of NodeId for optimized_mir purposes MIR: use HirId instead of NodeId to avoid cycles while inlining Apr 19, 2020
@ecstatic-morse
Copy link
Contributor

@bors r+

@ljedrz Feel free to explore the interaction between incremental and the current approach to avoiding inlining cycles. Note that more robust approach to MIR inlining in cyclic call graphs would eliminate that issue, along with several others. This is not a simple problem, however. See #43542.

@bors
Copy link
Contributor

bors commented Apr 19, 2020

📌 Commit 3c455fe has been approved by ecstatic-morse

@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 19, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 21, 2020
Rollup of 4 pull requests

Successful merges:

 - rust-lang#69362 (Stabilize most common subset of alloc_layout_extras)
 - rust-lang#71174 (Check that main/start is not async)
 - rust-lang#71285 (MIR: use HirId instead of NodeId to avoid cycles while inlining)
 - rust-lang#71346 (Do not build tools if user do not want them)

Failed merges:

r? @ghost
@bors bors merged commit 42b533d into rust-lang:master Apr 21, 2020
@ljedrz ljedrz deleted the mir_inline_span_for_optimized_mir branch April 21, 2020 06:33
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.

6 participants