-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Refactor trans some more to pave way for incremental compilation #41355
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
Refactor trans some more to pave way for incremental compilation #41355
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM modulo nits
src/librustc/traits/trans/mod.rs
Outdated
use ty::{self, Ty}; | ||
|
||
/// Specializes caches used in trans -- in particular, they assume all | ||
/// types are fully monomorphized and that free regions can be erased. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The monomorphic condition was fixed a while back I think, at least in terms of specialization - and I assume the regions wouldn't matter after we get lazy normalization? Still, can't these caches be chosen dynamically, if all conditions are met?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, probably it's true that these caches aren't so important, though I'm not sure I'm inclined to change anything here in this particular PR. (I did however intend to move a lot of the routines that manipulate said caches out of trans and into this file, and I forgot to do so.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I more or less want to kill all "trans" terminology and just have backends that use Reveal::All
and do the lifetime erasing themselves, in the long term.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I more or less want to kill all "trans" terminology and just have backends that use Reveal::All and do the lifetime erasing themselves, in the long term.
I basically agree, but I'm not sure I want to do that now. In particular, I want to remove these caches eventually and just sort of roll this into a more general caching scheme. Therefore, I tried to just shuffle things around for now rather than worry too much about the details, so that we can address them in a more uniform fashion.
@@ -111,6 +111,7 @@ provide! { <'tcx> tcx, def_id, cdata | |||
closure_kind => { cdata.closure_kind(def_id.index) } | |||
closure_type => { cdata.closure_ty(def_id.index, tcx) } | |||
inherent_impls => { Rc::new(cdata.get_inherent_implementations_for_type(def_id.index)) } | |||
is_foreign_item => { cdata.is_foreign_item(def_id.index) } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no similar method to remove on the CrateStore
trait?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, there is such a method, it gets called quite a few times. I can work on refactoring those away.
|
||
translation_items: RefCell<FxHashSet<TransItem<'tcx>>>, | ||
trait_cache: RefCell<DepTrackingMap<TraitSelectionCache<'tcx>>>, | ||
project_cache: RefCell<DepTrackingMap<ProjectionCache<'tcx>>>, | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This reminds me, the design of SharedCrateContext
was a mistake IMO - its fields should be public and CrateContext
should Deref
to it so we get e.g. ccx.tcx
and also no duplication of methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm basically aiming to kill it.
src/librustc_trans/symbol_cache.rs
Outdated
|
||
pub struct SymbolCache<'a, 'tcx: 'a> { | ||
tcx: TyCtxt<'a, 'tcx, 'tcx>, | ||
index: RefCell<FxHashMap<TransItem<'tcx>, Rc<String>>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this use the interned Symbol
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i.e., I should do Symbol::intern()
for each string? Seems ok, sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works out to be pretty annoying in practice. Every caller of get()
winds up calling as_str()
on the result almost immediately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just add a convenience method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the point of interning this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it reduces memory usage when multiple CGUs have the same serialization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just that we already have something akin to Rc<String>
and it's Symbol
.
☔ The latest upstream changes (presumably #41357) made this pull request unmergeable. Please resolve the merge conflicts. |
This may seem like overkill, but it's exactly what we want/need for incremental compilation I think. In particular, while generating code for some codegen unit X, we can wind up querying about any number of external items, and we only want to be forced to rebuild X is some of those changed from a foreign item to otherwise. Factoring this into a query means we would re-run only if some `false` became `true` (or vice versa).
If we are going to hash `SharedCrateContext`, we don't want a list of things that pertain to **every CGU** in there.
The symbol map is not good for incremental: it has inputs from every fn in existence, and it will change if anything changes. One could imagine cheating with the symbol-map and exempting it from the usual dependency tracking, since the results are fully deterministic. Instead, I opted to just add a per-CGU cache, on the premise that recomputing some symbol names is not going to be so very expensive.
The more we can things dependent on just tcx, the easier it will be to make queries etc later on.
Once it is computed, no need to deep clone the set.
Arguably these could become custom queries, but I chose not to do that because the relationship of queries and trait system is not yet fleshed out enough. For now it seems fine to have them be `DepTrackingMap` using the memoize pattern.
This makes these routines more readily available for other bits of code. It also will help when refactoring.
e3c3b80
to
6d86f81
Compare
@bors r=eddyb |
📌 Commit 6d86f81 has been approved by |
…ns-2, r=eddyb Refactor trans some more to pave way for incremental compilation Various refactorings paving the way for the newer approach to incremental compilation (And, in particular, to "query-ifying" trans). My partial goal is to remove `SharedCrateContext`; this PR gets about as far as I can easily get before starting to really want the red/green algorithm. r? @eddyb cc @michaelwoerister
@bors r- retry |
Various refactorings paving the way for the newer approach to incremental compilation (And, in particular, to "query-ifying" trans). My partial goal is to remove
SharedCrateContext
; this PR gets about as far as I can easily get before starting to really want the red/green algorithm.r? @eddyb
cc @michaelwoerister