Repository: rust
Issue: Query Parallelization Tracking Issue
Labels: C-enhancement,I-compiletime,T-compiler,C-tracking-issue,WG-compiler-performance,A-parallel-queries,C-optimization

This issue is a sub-issue of https://github.com/rust-lang/rust/issues/48547: it tracks the in-progress effort to parallelize rustc across queries. This work is being spearheaded by @zoxc. 

### Goals

Allow rustc to execute queries in parallel with one another. Enable the use of rayon or other tools for intra-query parallelization as well. See [this internals thread](https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606) for more information.

### Overview of the plan

- [x] Make all types in the compiler send and sync by landing @Zoxc's existing work
    - The existing work largely replaces `RefCell` usage with `Mutex`
    - In many cases, [the interior mutability itself can be refactored away](https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606/24?u=nikomatsakis). The plan is do this as a second step.
- [x] Set up CI so this gets some testing (https://github.com/rust-lang/rust/issues/48607)
- [x] Implement a thread pool that is integrated with Cargo's jobserver (https://github.com/rust-lang/rust/pull/56946)
- [ ] Build list of all sources of shared state in the compiler by auditing PR history https://github.com/rust-lang/rust/issues/63643
- [ ] Audit each source of shared state, leading to either refactoring to reduce use of shared state, or persistent documentation covering invariants, atomicity, and lock orderings

### Pending refactorings

- [ ] [ParseSess.included_mod_stack](https://github.com/rust-lang/rust/blob/master/src/libsyntax/parse/mod.rs#L51) might be problematic
- [ ] Ensure that the err_count() API is not used to discover if errors happen in parallel code (cc  #49737)
- [ ] Find a way to order error messages deterministically so that if a query depends on another query, its error messages appear after the other query (cc #49737)
- [ ] Make `mk_attr_id` use a scoped thread local or make it part of `ParseSess`
- [ ] See if `GlobalCtxt.rcache`, `OnDiskCache.file_index_to_file` and `OnDiskCache.synthetic_expansion_infos`  are faster as thread-locals
- [ ] Review usages of `Session.lint_store` and `Session.buffered_lints`
- [ ] Fix https://github.com/rust-lang/rust/issues/50507, as it may cause issues with parallel rustc
- [ ] Find a way to deal with marking attributes as used
- [ ] Ensure Rayon executes all remaining work when we panic inside the thread pool

### Completed refactorings

- [x] Remove HIR inlining (https://github.com/rust-lang/rust/issues/49690)
- [x] [Make GlobalCtxt implement Sync](#50108)
- [x] Use scoped thread locals instead of globals, https://github.com/rust-lang/rust/pull/46193
- [x] Misc fixes required to use a rayon thread pool, https://github.com/rust-lang/rust/pull/46564
- [x] Ensure metadata [loaded by LlvmMetadataLoader](https://github.com/rust-lang/rust/blob/master/src/librustc_trans/metadata.rs#L25) does not get freed on the wrong thread
- [x] [CurrentDepGraph.task_stack](https://github.com/rust-lang/rust/blob/master/src/librustc/dep_graph/graph.rs#L764) has to be made a `QueryJob` field. (implemented in https://github.com/rust-lang/rust/pull/49732)
- [x] Check that optimization fuel is not used with parallel queries
- [x] Review `libproc_macro` for issues, Find out which types should be `Send`, `Sync`. Deal with `Deref` impls for `Symbol`. (https://github.com/rust-lang/rust/pull/49219)
- [x] [Run CI with cfg(parallel_queries)](https://github.com/rust-lang/rust/issues/48607)
- [x] Refactor away `CStore::next_crate_num`
- [x] Make `FileMap.lines`, `FileMap.multibyte_chars`,  and `FileMap.non_narrow_chars` immutable (implemented in #50997)
- [x] When executing queries, instead of keeping around a lock to the query map, create a job object immediately instead (https://github.com/rust-lang/rust/pull/50102)
- [x] Ensure all diagnostics are emitted before returning from `DepGraph.try_mark_green`
- [x] Refactor `DepGraphData.previous_work_products` so it becomes immutable (#50501) (implemented in #50524)
- [x] Remove `DepGraphData.work_products` by threaded the value through `save_trans_partition() -> copy_module_artifacts_into_incr_comp_cache() -> OngoingCrateTranslation::join()` (#50500) (implemented in #50885)
- [x] The `TransitiveRelation` doesn't really need to use a `RefCell`. In the future, it won't even be shared, but regardless the caching scheme [could be reworked to avoid `RefCell`](https://github.com/rust-lang/rust/pull/48587#issuecomment-369336651) (implemented in https://github.com/rust-lang/rust/pull/99702).
- [x] Refactor `GlobalCtxt::layout_depth` so it does not need global mutable state (https://github.com/rust-lang/rust/issues/49735) (implemented in https://github.com/rust-lang/rust/pull/100748)