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

Refactor translation unit partitioning/collection as a query #44529

Merged
merged 11 commits into from
Sep 18, 2017

Conversation

alexcrichton
Copy link
Member

@alexcrichton alexcrichton commented Sep 12, 2017

This commit is targeted at #44486 with the ultimate goal of making the collect_and_partition_translation_items function a query. This mostly just involved query-ifying a few other systems along with plumbing the tcx instead of SharedCrateContext in a few locations.

Currently this only tackles the first bullet of #44486 and doesn't add a dedicated query for a particular codegen unit. I wasn't quite sure how to do that yet but figured this was good to put up.

Closes #44486

@alexcrichton
Copy link
Member Author

r? @michaelwoerister

@rust-highfive
Copy link
Collaborator

r? @pnkfelix

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

@bors
Copy link
Contributor

bors commented Sep 13, 2017

☔ The latest upstream changes (presumably #44420) made this pull request unmergeable. Please resolve the merge conflicts.

@michaelwoerister
Copy link
Member

michaelwoerister commented Sep 13, 2017

Thanks, Alex! At first I wasn't sure if I liked the CodegenUnitExt/TransItemExt construction but now I think it's a pretty good solution.

My only nit would be that the fields of CodegenUnit start with an underscore now. I interpret this as "these are actually private". I'd prefer if we did not need that (which, I guess, could be done by using a local builder type in the partitioning module). I don't think this should block this PR though.

r=me after conflicts have been resolved.

@alexcrichton
Copy link
Member Author

Oh sure that's fine, I'll add some method accessors in rustc and use those throughout librustc_trans?

@michaelwoerister
Copy link
Member

Yes, that should work.

@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 13, 2017

📌 Commit f744cc2 has been approved by michaelwoerister

@alexcrichton
Copy link
Member Author

Ok I've pushed another commit which changes again how exported symbols are dealt with. Everything should now be behind queries and moving us one step closer to 'codegen as a query' now that CrateContext no longer has a set of all exported symbols to peek at.

@alexcrichton
Copy link
Member Author

re-r? @michaelwoerister

@aidanhs aidanhs added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 13, 2017
@alexcrichton alexcrichton force-pushed the trans-query branch 2 times, most recently from 829de6b to dd8dbc5 Compare September 13, 2017 22:33
@alexcrichton
Copy link
Member Author

Alright and to round this off some more, the last commits now make codegen 100% query-ified.

@@ -666,19 +669,40 @@ fn need_crate_bitcode_for_rlib(sess: &Session) -> bool {
sess.opts.output_types.contains_key(&OutputType::Exe)
}

pub fn start_async_translation(sess: &Session,
pub fn start_async_translation(tcx: TyCtxt,
Copy link
Member

Choose a reason for hiding this comment

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

Nice cleanup!

// other codegen unit is also not exported then like with the foreign
// case we apply a hidden visibility. If the symbol is exported from
// the foreign object file, however, then we leave this at the
// default visibility as we'll just import it naturally.
Copy link
Member

Choose a reason for hiding this comment

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

Great comment!

@michaelwoerister
Copy link
Member

@bors r+

Thanks! Excellent :)

@bors
Copy link
Contributor

bors commented Sep 14, 2017

📌 Commit dd8dbc5 has been approved by michaelwoerister

@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 14, 2017

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Sep 14, 2017

📌 Commit dd8dbc5 has been approved by michaelwoerister

@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 14, 2017

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Contributor

bors commented Sep 14, 2017

📌 Commit dd8dbc5 has been approved by michaelwoerister

@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 14, 2017

📌 Commit 4f8b10b has been approved by michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 15, 2017

☔ The latest upstream changes (presumably #44502) made this pull request unmergeable. Please resolve the merge conflicts.

@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 15, 2017

📌 Commit d1b4773 has been approved by michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 17, 2017

☔ The latest upstream changes (presumably #44634) made this pull request unmergeable. Please resolve the merge conflicts.

This commit refactors the `collect_crate_translation_items` function to only
require the `TyCtxt` instead of a `SharedCrateContext` in preparation for
query-ifying this portion of trans.
This commit refactors the the `partitioning::partition` function to operate with
a `TyCtxt` instead of a `SharedCrateContext` in preparation for making it a
query.
Turns out this was already set up as a query, just wasn't using it yet!
This commit moves the definition of the `ExportedSymbols` structure to the
`rustc` crate and then creates a query that'll be used to construct the
`ExportedSymbols` set. This in turn uses the reachablity query exposed in the
previous commit.
This commit moves the `collect_and_partition_translation_items` function into a
query on `TyCtxt` instead of a free function in trans, allowing us to track
dependencies and such of the function.
Otherwise we may emit double errors related to the `#[export_name]` attribute,
for example, and using a query should ensure that it's only emitted at most
once.
This is a big map that ends up inside of a `CrateContext` during translation for
all codegen units. This means that any change to the map may end up causing an
incremental recompilation of a codegen unit! In order to reduce the amount of
dependencies here between codegen units and the actual input crate this commit
refactors dealing with exported symbols and such into various queries.

The new queries are largely based on existing queries with filled out
implementations for the local crate in addition to external crates, but the main
idea is that while translating codegen untis no unit needs the entire set of
exported symbols, instead they only need queries about particulare `DefId`
instances every now and then.

The linking stage, however, still generates a full list of all exported symbols
from all crates, but that's going to always happen unconditionally anyway, so no
news there!
I believe this comment here is mostly talking about the `ptrcast` function call
below, so move the comment down to that block.
This commit removes the `crate_trans_items` field from the `CrateContext` of
trans. This field, a big map, was calculated during partioning and was a set of
all translation items. This isn't quite incremental-friendly because the map may
change a lot but not have much effect on downstream consumers.

Instead a new query was added for the one location this map was needed, along
with a new comment explaining what the location is doing!
This commit attaches a channel to the LLVM workers to the `TyCtxt` which will
later be used during the codegen query to actually send work to LLVM workers.
Otherwise this commit is just plumbing this channel throughout the compiler to
ensure it reaches the right consumers.
This commit moves the actual code generation in the compiler behind a query
keyed by a codegen unit's name. This ended up entailing quite a few internal
refactorings to enable this, along with a few cut corners:

* The `OutputFilenames` structure is now tracked in the `TyCtxt` as it affects a
  whole bunch of trans and such. This is now behind a query and threaded into
  the construction of the `TyCtxt`.

* The `TyCtxt` now has a channel "out the back" intended to send data to worker
  threads in rustc_trans. This is used as a sort of side effect of the codegen
  query but morally what's happening here is the return value of the query
  (currently unit but morally a path) is only valid once the background threads
  have all finished.

* Dispatching work items to the codegen threads was refactored to only rely on
  data in `TyCtxt`, which mostly just involved refactoring where data was
  stored, moving it from the translation thread to the controller thread's
  `CodegenContext` or the like.

* A new thread locals was introduced in trans to work around the query
  system. This is used in the implementation of `assert_module_sources` which
  looks like an artifact of the old query system and will presumably go away
  once red/green is up and running.
@alexcrichton
Copy link
Member Author

@bors: r=michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 17, 2017

📌 Commit 6d614dd has been approved by michaelwoerister

@bors
Copy link
Contributor

bors commented Sep 17, 2017

⌛ Testing commit 6d614dd with merge e8a76d8...

bors added a commit that referenced this pull request Sep 17, 2017
Refactor translation unit partitioning/collection as a query

This commit is targeted at #44486 with the ultimate goal of making the `collect_and_partition_translation_items` function a query. This mostly just involved query-ifying a few other systems along with plumbing the tcx instead of `SharedCrateContext` in a few locations.

Currently this only tackles the first bullet of #44486 and doesn't add a dedicated query for a particular codegen unit. I wasn't quite sure how to do that yet but figured this was good to put up.

Closes #44486
@bors
Copy link
Contributor

bors commented Sep 18, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: michaelwoerister
Pushing e8a76d8 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants