So I’ve been having some interesting conversations with @sunfishcode, and I wanted to talk about an idea that he floated to me and try to get some feedback. The idea is to add an alternate backend to LLVM based on Cretonne. Cretonne is a compiler IR, similar to LLVM, being implemented (in Rust!) for use with WASM. It is intended for use in SpiderMonkey, Mozilla’s JS engine. The idea is that if you load WASM, it will be converted to Cretonne and optimized – similarly, when executing JS, Cretonne can be used as the backend for the JIT compiler (at least in aggressive stages). However, it’s important to emphasize that Cretonne is intended to be independent of SpiderMonkey, and in particular not to be limited to WASM/jit use-cases. I’m sure @stoklund an say more.
Short term, the idea would be for Cretonne, to serve as the IR when doing “debug” compilation (not “release”). It has a lot of advantages over LLVM in this respect:
- It is defined for speed. Trust me when I tell you that the SpiderMonkey people care about latency.
- We have more control and input over it, so it should be easier to ensure that our needs are met.
- It will help us validate that Rust is independent from LLVM.
Longer term, we might be able to use Cretonne for release targets as well. It also has some other advantages that come into play at that point:
- It does not have undefined behavior. At all.
- We can have better control of its alias analysis, which may be useful.
There are of course disadvantages which suggest that we will want to continue using LLVM for a long time:
- LLVM has a lot of existing optimizations
- But as long as we use it when compiling in release mode, that’s not a problem
- LLVM supports a lot of architectures
- So we’ll still need to use it sometimes, even for debug builds
If people are excited about this idea, @sunfish and @stoklund are potentially interested in pushing it forward. It’d be great to think over what the necessary steps are. He and I drafted a few major blockers:
- The easy part is extending rustc:
- create the cretonne IR from MIR
- The harder parts are extending Cetonne:
- support writing out a native .o file
- debuginfo / DWARF output
Probably we would start by focusing on linux, as it is the most straightforward, then try to support windows next. Naturally the existing LLVM targets (both debug/release) would continue to be supported in the meantime (and probably forever; certainly for the foreseeable future).