Repository navigation
Improve backend parallelism when combined with parallel frontend - #166135
TechnoPorg wants to merge 1 commit into
Conversation
c0011a8 to
c47bb90
Compare
|
I don't remember if this default config benchmarks parallelism, but we'll need to test the different thread numbers anyway, so I don't think the order matters. @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Improve backend parallelism when combined with parallel frontend
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (ba1999e): comparison URL. Overall result: ✅ improvements - no action neededBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. @rustbot label: -S-waiting-on-perf -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 2.4%, secondary 0.2%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 2.2%, secondary 0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 466.495s -> 463.406s (-0.66%) |
c47bb90 to
be10173
Compare
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Improve backend parallelism when combined with parallel frontend
|
The job Click to see the possible cause of the failure (guessed by this bot) |
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (e9b2a4c): comparison URL. Overall result: ❌✅ regressions and improvements - BENCHMARK(S) FAILEDBenchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up. Next, please: If you can, justify the regressions found in this try perf run in writing along with @bors rollup=iffy rustc-perf ❗ ❗ ❗ ❗ ❗
❗ ❗ ❗ ❗ ❗ Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 6.6%, secondary 0.1%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 16.3%, secondary 0.6%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis perf run didn't have relevant results for this metric. Bootstrap: 458.8s -> 456.098s (-0.59%) |
|
This PR has accumulated enough hacks that I'd like to get input on the approach before I spend time fixing it up. The basic idea is "for each CGU, run LLVM passes in the same worker threads used to generate LLVM IR from MIR"; in practice, though, this is tricky to do. My first prototype only used frontend threads for this, but since there are fewer of those than there are backend threads, I amended it today to use a new thread pool taking the backend thread count, and that helped less than it was expected to, plus seems to have caused exciting new bugs and perf regressions. I made an attempt at reusing the existing codegen coordinator for everything, but didn't get far because I realized the change to support scoped threads (needed for the TyCtxt in IR generation) would be quite invasive. I also don't like the idea of having separate thread pools for IR generation and LLVM passes, since it means no LLVM worker can start until all CGUs have been codegenned. It is worth noting that I have gotten large (8-10%) improvements on local benchmarks of clean builds with this branch (and @ZuseZ4 if I'm reading your Zulip message correctly, you have reproduced the same results?). @petrochenkov as someone who knows a lot about the parallel frontend, I would be particularly interested to hear your thoughts. |
|
Disclaimer: I used an LLM for an earlier unshared prototype when the perf wasn't looking as expected, and today since I was wondering how hard it is to change this (emphasis mine):
I shared the numbers with @TechnoPorg, but not the code. It's ~150 lines on top of the original commit of this PR (pre force-push). On main, we only lower the first N (=-Zthreads) CGUs in parallel, then main thread handles the rest, which is obviously a bottleneck. This pr's original commit lets the frontend pool lower all of them, but it means that we're now limited to the number of frontend threads even in our backend. Since backend parallelization is easier, we use more threads there, so I agree with @TechnoPorg's last comment that only (re)using frontend threads is quite limited. The draft I benched was using a queue, so all frontend threads (minus the main thread) lower a CGU and then put their modules into the queue, if the queue has space. If the queue is full, the thread itself continues to run LLVM on the module instead of putting it in the queue (such that our peak mem doesn't grow by much). The main thread still loops similar to the main branch, if the coordinator requests a module then the main thread just passes one from the queue. Only if the queue is empty then the main thread will lower a cgu by itself (like it does today on the main branch) The speedup was equal or (significantly) higher than the rss increase due to the queue. The queue size was fixed to the number of frontend threads, but that should probably be tuned, just like a few other things (left out for simplicity). @TechnoPorg I'd rather review some human written code, but the numbers seem quite promising. Do you want to give this a try? |
|
☔ The latest upstream changes (presumably #166567) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
With this change, when using the parallel frontend, all CGUs will generate LLVM IR from MIR in parallel, and LLVM passes will start immediately after IR is generated.
This is part of https://goals.rust-lang.org/2026/tpde.html, but has significant compile-time wins for the LLVM backend as well. There is a Zulip thread where this change was briefly discussed at #t-compiler/performance > Parallelizing LLVM IR generation.
As described in this old parallel frontend blogpost, MIR is currently lowered to LLVM IR sequentially, then sent to LLVM worker threads by the coordinator. Right now, the parallel frontend has a mechanism to codegen the first batch (the batch contains as many CGUs as there are frontend threads) of CGUs in parallel, then go back to the serial path. This has two disadvantages:
The code currently has a comment that this can be made entirely parallel once we no longer need to support the serial frontend. However, doing so now with the unstable parallel frontend has a surprisingly small diff (thanks to refactors since it was originally written), and, based on local benchmarks, the benefits of doing so have gotten more significant.
After spending a long time going back and forth between different possible refactorings, I ended up at this "minimum compilable product". It still needs a bit of reworking before I'll consider it ready for review, but I wanted to collect some perf data and run try jobs first. There is also some unavoidable jank due to the need to simultaneously support serial and parallel rustc, and the fact that @bjorn3's work to move LTO out of the codegen coordinator is not finished yet.
r? @ZuseZ4