Skip to content

Commit 6c90c57

Browse files
authored
Merge pull request #169 from folkertdev/improve-rustc-codegen
[2025H1] add 'improve rustc codegen' goal
2 parents 259ebfc + 7a20b67 commit 6c90c57

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed

src/2025h1/improve-rustc-codegen.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Improve rustc codegen
2+
3+
<!--
4+
5+
> **Instructions:** Copy this template to a fresh file with a name based on your plan.
6+
> Give it a title that describes what you plan to get done in the next 6 months
7+
> (e.g., "stabilize X" or "nightly support for X" or "gather data about X").
8+
> Feel free to replace any text with anything, but there are placeholders
9+
> designed to help you get started.
10+
11+
-->
12+
13+
| Metadata | |
14+
| -------- | ------------------------------------------------------------ |
15+
| Owner(s) | @folkertdev, @bjorn3 |
16+
| Teams | T-compiler |
17+
| Status | Proposed |
18+
19+
## Summary
20+
21+
We want to improve rustc codegen, based on [this initialive](https://trifectatech.org/initiatives/workplans/codegen/) by the Trifecta Tech Foundation. The work focusses on improving state machine code generation, and finding (and hopefully fixing) cases where clang produces better code than rustc for roughly equivalent input.
22+
23+
## Motivation
24+
25+
Matching C performance is crucial for rust adoption in performance-sensitive domains. Rust is doing well overall, but not good enough.
26+
27+
In compression, video decoding and other high-performance areas, nobody will use rust if it is even a couple percent slower: latency, power (i.e. battery) consumption and other factors are just more important than whatever advantages rust can bring. In particular, we've observed that C code translated to rust code, whether manually or mechanically, often performs a couple percent worse than the original C.
28+
29+
Given that we clang and rustc both use LLVM for code generation, there is no fundamental reason that rust should be slower.
30+
31+
### The status quo
32+
33+
Our target audience is users of rust in performance-sensitive domains, where the rustc codegen hinders adoption of rust. Concretely we have most experience with, and knowledge of the bottlenecks in these projects:
34+
35+
- the [`zlib-rs`](https://github.com/trifectatechfoundation/zlib-rs) and [`libbzip2-rs-sys`](https://github.com/trifectatechfoundation/libbzip2-rs) compression algorithms
36+
- the [`rav1d`](https://github.com/memorysafety/rav1d/tree/main) av1 decoder
37+
38+
In the compression libraries, we spotted a specific pattern (in rust terms, a `loop` containing a `match`) where rust is not able to generate good code today. We wrote [RFC 3720](https://github.com/rust-lang/rfcs/pull/3720) to tackle this problem.
39+
40+
In the case of rav1d, the performance is several percent worse than its C equivalent dav1d. The rav1d project used [c2rust](https://github.com/immunant/c2rust) to translate the dav1d C source to rust. Hence the two code bases are basically equivalent, and we'd expect basically identical performance.
41+
42+
The rav1d developers were unable to track down the reason that rav1d performs worse than dav1d: their impression (that we have confirmed with various rustc developers) is that rustc+llvm is just slightly worse at generating code than clang+llvm, because llvm overfits to what clang gives it.
43+
44+
### The next 6 months
45+
46+
#### Improve state machine codegen
47+
48+
The problem, and a range of possible solutions, is described in [RFC 3720](https://github.com/rust-lang/rfcs/pull/3720).
49+
50+
- recognize the problematic pattern in zlib-rs in HIR, based on a fragile heuristic
51+
- ensure it is eventually turned into a `goto` to the actual target in MIR
52+
- evaluate how effective that is for other projects (e.g. rustc itself)
53+
- depending on how RFC 3720 evolves, implement the specific proposal (syntax, lints, error messages)
54+
55+
#### Finding performance bottlenecks
56+
57+
We want to build a tool that uses `creduce` and `c2rust` to find small examples where clang+llvm produces meaningfully better code than rust+llvm.
58+
59+
The output will be either issues with small rust snippets that have suboptimal codegen (compared to clang) or PRs fixing these problems.
60+
61+
### The "shiny future" we are working towards
62+
63+
The shiny future is to improve rust codegen to encourage wider adoption of rust in performance-sensitive domains.
64+
65+
<!--
66+
67+
## Design axioms
68+
69+
*This section is optional, but including [design axioms][da] can help you signal how you intend to balance constraints and tradeoffs (e.g., "prefer ease of use over performance" or vice versa). Teams should review the axioms and make sure they agree. [Read more about design axioms][da].*
70+
71+
[da]: ../about/design_axioms.md
72+
73+
-->
74+
75+
## Ownership and team asks
76+
77+
**Owner:** *Identify a specific person or small group of people if possible, else the group that will provide the owner. Github user names are commonly used to remove ambiguity.*
78+
79+
*This section defines the specific work items that are planned and who is expected to do them. It should also include what will be needed from Rust teams. The table below shows some common sets of asks and work, but feel free to adjust it as needed. Every row in the table should either correspond to something done by a contributor or something asked of a team. For items done by a contributor, list the contributor, or ![Heap wanted][] if you don't yet know who will do it. For things asked of teams, list ![Team][] and the name of the team. The things typically asked of teams are defined in the [Definitions](#definitions) section below.*
80+
81+
| Subgoal | Owner(s) or team(s) | Notes |
82+
| ---------------------------------------------- | ----------------------- | ----- |
83+
| Discussion and moral support | ![Team][] [compiler] | |
84+
| Nightly experiment: improved state machine codegen | | |
85+
| ↳ Lang-team experiment | ![Team][] [lang] | |
86+
| ↳ refine RFC 3720 | @folkertdev | |
87+
| ↳ Implementation | @folkertdev, @bjorn3 | |
88+
| ↳ Standard reviews | ![Team][] [compiler] | |
89+
90+
91+
### Definitions
92+
93+
Definitions for terms used above:
94+
95+
* *Discussion and moral support* is the lowest level offering, basically committing the team to nothing but good vibes and general support for this endeavor.
96+
* *Author RFC* and *Implementation* means actually writing the code, document, whatever.
97+
* *Design meeting* means holding a synchronous meeting to review a proposal and provide feedback (no decision expected).
98+
* *RFC decisions* means reviewing an RFC and deciding whether to accept.
99+
* *Org decisions* means reaching a decision on an organizational or policy matter.
100+
* *Secondary review* of an RFC means that the team is "tangentially" involved in the RFC and should be expected to briefly review.
101+
* *Stabilizations* means reviewing a stabilization and report and deciding whether to stabilize.
102+
* *Standard reviews* refers to reviews for PRs against the repository; these PRs are not expected to be unduly large or complicated.
103+
* *Prioritized nominations* refers to prioritized lang-team response to nominated issues, with the expectation that there will be *some* response from the next weekly triage meeting.
104+
* *Dedicated review* means identifying an individual (or group of individuals) who will review the changes, as they're expected to require significant context.
105+
* Other kinds of decisions:
106+
* [Lang team experiments](https://lang-team.rust-lang.org/how_to/experiment.html) are used to add nightly features that do not yet have an RFC. They are limited to trusted contributors and are used to resolve design details such that an RFC can be written.
107+
* Compiler [Major Change Proposal (MCP)](https://forge.rust-lang.org/compiler/mcp.html) is used to propose a 'larger than average' change and get feedback from the compiler team.
108+
* Library [API Change Proposal (ACP)](https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html) describes a change to the standard library.
109+
110+
## Frequently asked questions
111+
112+
None yet
113+
114+
<!--
115+
116+
### What do I do with this space?
117+
118+
*This is a good place to elaborate on your reasoning above -- for example, why did you put the design axioms in the order that you did? It's also a good place to put the answers to any questions that come up during discussion. The expectation is that this FAQ section will grow as the goal is discussed and eventually should contain a complete summary of the points raised along the way.*
119+
120+
-->

0 commit comments

Comments
 (0)