11# Parallel Compilation
22
3- As of <!-- date-check --> August 2022, the only stage of the compiler that
4- is already parallel is codegen. Some parts of the compiler already have
5- parallel implementations, such as query evaluation, type check and
6- monomorphization, but the general version of the compiler does not include
7- these parallelization functions. ** To try out the current parallel compiler** ,
8- one can install rustc from source code with ` parallel-compiler = true ` in
9- the ` config.toml ` .
3+ As 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 ` .
109
1110The lack of parallelism at other stages (for example, macro expansion) also
1211represents an opportunity for improving compiler performance.
1312
1413These next few sections describe where and how parallelism is currently used,
1514and the current status of making parallel compilation the default in ` rustc ` .
1615
17- ## Codegen
16+ ## Code Generation
1817
19- During [ monomorphization] [ monomorphization ] the compiler splits up all the code to
18+ During monomorphization the compiler splits up all the code to
2019be generated into smaller chunks called _ codegen units_ . These are then generated by
2120independent instances of LLVM running in parallel. At the end, the linker
2221is run to combine all the codegen units together into one binary. This process
@@ -45,22 +44,22 @@ are implemented differently depending on whether `parallel-compiler` is true.
4544| LockGuard | parking_lot::MutexGuard | std::cell::RefMut |
4645| MappedLockGuard | parking_lot::MappedMutexGuard | std::cell::RefMut |
4746
48- - These thread-safe data structures interspersed during compilation can
49- cause a lot of lock contention, which actually degrades performance as the
50- number of threads increases beyond 4. This inspires us to audit the use
51- of these data structures, leading to either refactoring to reduce use of
52- shared state, or persistent documentation covering invariants, atomicity,
53- and lock orderings.
47+ - These thread-safe data structures are interspersed during compilation which
48+ can cause lock contention resulting in degraded performance as the number of
49+ threads increases beyond 4. So we audit the use of these data structures
50+ which leads to either a refactoring so as to reduce the use of shared state,
51+ or the authoring of persistent documentation covering the specific of the
52+ invariants, the atomicity, and the lock orderings.
5453
5554- On the other hand, we still need to figure out what other invariants
5655 during compilation might not hold in parallel compilation.
5756
5857### WorkLocal
5958
60- ` WorkLocal ` is a special data structure implemented for parallel compiler.
61- It holds worker-locals values for each thread in a thread pool. You can only
62- access the worker local value through the Deref impl on the thread pool it
63- was constructed on. It will panic otherwise.
59+ ` WorkLocal ` is a special data structure implemented for parallel compilers. It
60+ holds worker-locals values for each thread in a thread pool. You can only
61+ access the worker local value through the ` Deref ` ` impl ` on the thread pool it
62+ was constructed on. It panics otherwise.
6463
6564` WorkLocal ` is used to implement the ` Arena ` allocator in the parallel
6665environment, which is critical in parallel queries. Its implementation
@@ -115,7 +114,7 @@ There are still many loops that have the potential to use parallel iterators.
115114The query model has some properties that make it actually feasible to evaluate
116115multiple queries in parallel without too much of an effort:
117116
118- - All data a query provider can access is accessed via the query context, so
117+ - All data a query provider can access is via the query context, so
119118 the query context can take care of synchronizing access.
120119- Query results are required to be immutable so they can safely be used by
121120 different threads concurrently.
@@ -141,25 +140,24 @@ the previous `Data Structures` and `Parallel Iterators`. See [this tracking issu
141140## Rustdoc
142141
143142As of <!-- date-check--> November 2022, there are still a number of steps
144- to complete before rustdoc rendering can be made parallel. More details on
145- this issue can be found [ here ] [ parallel-rustdoc ] .
143+ to complete before ` rustdoc ` rendering can be made parallel (see a discussion of
144+ [ parallel ` rustdoc ` ] [ parallel-rustdoc ] ) .
146145
147146## Resources
148147
149- Here are some resources that can be used to learn more (note that some of them
150- are a bit out of date):
148+ Here are some resources that can be used to learn more:
151149
150+ - [ This IRLO thread by alexchricton about performance] [ irlo1 ]
152151- [ This IRLO thread by Zoxc, one of the pioneers of the effort] [ irlo0 ]
153152- [ This list of interior mutability in the compiler by nikomatsakis] [ imlist ]
154- - [ This IRLO thread by alexchricton about performance] [ irlo1 ]
155153
156154[ `rayon` ] : https://crates.io/crates/rayon
157- [ rustc-rayon ] : https://github.com/rust-lang/rustc-rayon
158- [ irlo0 ] : https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606
155+ [ Arc ] : https://doc.rust-lang.org/std/sync/struct.Arc.html
159156[ imlist ] : https://github.com/nikomatsakis/rustc-parallelization/blob/master/interior-mutability-list.md
157+ [ irlo0 ] : https://internals.rust-lang.org/t/parallelizing-rustc-using-rayon/6606
160158[ irlo1 ] : https://internals.rust-lang.org/t/help-test-parallel-rustc/11503
161- [ tracking ] : https://github.com/rust-lang/rust/issues/48685
162159[ monomorphization ] : backend/monomorph.md
163160[ parallel-rustdoc ] : https://github.com/rust-lang/rust/issues/82741
164- [ Arc ] : https://doc.rust-lang.org/std/sync/struct.Arc.html
165161[ Rc ] : https://doc.rust-lang.org/std/rc/struct.Rc.html
162+ [ rustc-rayon ] : https://github.com/rust-lang/rustc-rayon
163+ [ tracking ] : https://github.com/rust-lang/rust/issues/48685
0 commit comments