11# Parallel Compilation
22
33As of <!-- date-check --> August 2022, the only stage of the compiler that is
4- parallel is codegen. Some parts of the compiler have parallel implementations,
5- such as query evaluation, type check and [ monomorphization] [ monomorphization ] ,
6- but the general version of the compiler does not include parallelization
7- functions. ** To try out the current parallel compiler** , install ` rustc ` from
8- source code with ` parallel-compiler = true ` in the ` Config.toml ` .
4+ parallel is [ code generation stage] [ codegen ] (codegen). Some parts of the
5+ compiler have parallel implementations, such as query evaluation, type check
6+ and [ monomorphization] [ monomorphization ] , but the general version of the
7+ compiler does not include parallelization functions. ** To try out the current
8+ parallel compiler** , install ` rustc ` from source code with `parallel-compiler =
9+ true` in the ` Config.toml`.
910
1011The lack of parallelism at other stages (for example, macro expansion) also
1112represents an opportunity for improving compiler performance.
1213
1314These next few sections describe where and how parallelism is currently used,
1415and the current status of making parallel compilation the default in ` rustc ` .
1516
17+ [ codegen ] : backend/codegen.md
18+
1619## Code Generation
1720
1821During monomorphization the compiler splits up all the code to
1922be generated into smaller chunks called _ codegen units_ . These are then generated by
2023independent instances of LLVM running in parallel. At the end, the linker
2124is run to combine all the codegen units together into one binary. This process
22- occurs in the ` rustc_codegen_ssa::base ` module.
25+ occurs in the [ ` rustc_codegen_ssa::base ` ] module.
26+
27+ [ `rustc_codegen_ssa::base` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_codegen_ssa/base/index.html
2328
2429## Data Structures
2530
2631The underlying thread-safe data-structures used in the parallel compiler
27- can be found in the ` rustc_data_structures::sync ` module. These data structures
32+ can be found in the [ ` rustc_data_structures::sync ` ] module. These data structures
2833are implemented differently depending on whether ` parallel-compiler ` is true.
2934
3035| data structure | parallel | non-parallel |
@@ -54,24 +59,29 @@ are implemented differently depending on whether `parallel-compiler` is true.
5459- On the other hand, we still need to figure out what other invariants
5560 during compilation might not hold in parallel compilation.
5661
57- ### WorkLocal
62+ [ `rustc_data_structures::sync` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/sync/index.html
5863
59- ` WorkLocal ` is a special data structure implemented for parallel compilers. It
64+ ### WorkerLocal
65+
66+ [ ` WorkerLocal ` ] is a special data structure implemented for parallel compilers. It
6067holds worker-locals values for each thread in a thread pool. You can only
6168access the worker local value through the ` Deref ` ` impl ` on the thread pool it
6269was constructed on. It panics otherwise.
6370
64- ` WorkLocal ` is used to implement the ` Arena ` allocator in the parallel
65- environment, which is critical in parallel queries. Its implementation
66- is located in the ` rustc-rayon-core:: worker_local` module. However, in the
67- non-parallel compiler, it is implemented as ` (OneThread<T>) ` , whose ` T `
71+ ` WorkerLocal ` is used to implement the ` Arena ` allocator in the parallel
72+ environment, which is critical in parallel queries. Its implementation is
73+ located in the [ ` rustc_data_structures::sync:: worker_local` ] module. However,
74+ in the non-parallel compiler, it is implemented as ` (OneThread<T>) ` , whose ` T `
6875can be accessed directly through ` Deref::deref ` .
6976
77+ [ `rustc_data_structures::sync::worker_local` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/sync/worker_local/index.html
78+ [ `WorkerLocal` ] : https://doc.rust-lang.org/nightly/nightly-rustc/rustc_data_structures/sync/worker_local/struct.WorkerLocal.html
79+
7080## Parallel Iterator
7181
72- The parallel iterators provided by the [ ` rayon ` ] crate are easy ways
73- to implement parallelism. In the current implementation of the parallel
74- compiler we use a custom [ fork] [ rustc-rayon ] of [ ` rayon ` ] to run tasks in parallel.
82+ The parallel iterators provided by the [ ` rayon ` ] crate are easy ways to
83+ implement parallelism. In the current implementation of the parallel compiler
84+ we use a custom [ fork] [ rustc-rayon ] of ` rayon ` to run tasks in parallel.
7585
7686Some iterator functions are implemented to run loops in parallel
7787when ` parallel-compiler ` is true.
@@ -87,10 +97,9 @@ when `parallel-compiler` is true.
8797| ** ModuleItems::par_impl_items** (&self, f: impl Fn(ImplItemId)) | run ` f ` on all impl items in the module | rustc_middle::hir |
8898| ** ModuleItems::par_foreign_items** (&self, f: impl Fn(ForeignItemId)) | run ` f ` on all foreign items in the module | rustc_middle::hir |
8999
90- There are a lot of loops in the compiler which can possibly be
91- parallelized using these functions. As of <!-- date-check--> August
92- 2022, scenarios where the parallel iterator function has been used
93- are as follows:
100+ There are a lot of loops in the compiler which can possibly be parallelized
101+ using these functions. As of <!-- date-check--> August 2022, scenarios where
102+ the parallel iterator function has been used are as follows:
94103
95104| caller | scenario | callee |
96105| ------------------------------------------------------- | ------------------------------------------------------------ | ------------------------ |
@@ -112,7 +121,7 @@ There are still many loops that have the potential to use parallel iterators.
112121## Query System
113122
114123The query model has some properties that make it actually feasible to evaluate
115- multiple queries in parallel without too much of an effort:
124+ multiple queries in parallel without too much effort:
116125
117126- All data a query provider can access is via the query context, so
118127 the query context can take care of synchronizing access.
@@ -134,14 +143,15 @@ When a query `foo` is evaluated, the cache table for `foo` is locked.
134143 the compiler uses an extra thread * (named deadlock handler)* to detect, remove and
135144 report the cycle error.
136145
137- Parallel query still has a lot of work to do, most of which is related to
138- the previous ` Data Structures ` and ` Parallel Iterators ` . See [ this tracking issue] [ tracking ] .
146+ The parallel query feature still has implementation to do, most of which is
147+ related to the previous ` Data Structures ` and ` Parallel Iterators ` . See [ this
148+ open feature tracking issue] [ tracking ] .
139149
140150## Rustdoc
141151
142- As of <!-- date-check--> November 2022, there are still a number of steps
143- to complete before ` rustdoc ` rendering can be made parallel (see a discussion of
144- [ parallel ` rustdoc ` ] [ parallel-rustdoc ] ).
152+ As of <!-- date-check--> November 2022, there are still a number of steps to
153+ complete before ` rustdoc ` rendering can be made parallel (see a open discussion
154+ of [ parallel ` rustdoc ` ] [ parallel-rustdoc ] ).
145155
146156## Resources
147157
0 commit comments