6 ms·
Cranelift, a low-level retargetable code generator written in Rust
- steveklabnik 8y agoPreviously called “cretone”, by the way.
- ndh2 8y agoAny example or tutorial?
- sunfish 8y agoThere's a tutorial in development, but it's not finished yet. So right now isn't the optimal time for an HN post, but HN doesn't always ask people before linking to their projects ¯\_(ツ)_/¯.
- dmytrish 8y agoI can't say if it's complete or work in progress, but here is a demo: https://github.com/sunfishcode/simplejit-demo https://github.com/sunfishcode/simplejit-demo
- smaddox 8y agoNice! This should be amazing for writing toy compiled languages! This looks way easier to get up and running than an LLVM backend.
- seeekr 8y agoThe README says: "Cranelift is designed to be a code generator for WebAssembly, but it is general enough to be useful elsewhere too. The initial planned uses that affected its design are: WebAssembly compiler for the SpiderMonkey engine in Firefox. Backend for the IonMonkey JavaScript JIT compiler in Firefox. Debug build backend for the Rust compiler."
- xvilka 8y agoHopefully will replace LLVM one day.
- pjmlp 8y agoIt will be an herculean effort though, given the amount of companies that contribute to LLVM.
- sunfish 8y agoFortunately, this isn't required for the project to be successful :-).
- pjmlp 8y agoDepends on how successful is going to be measured. If it is to generate code with the same optimization quality and hardware support as LLVM, not really. There are still cases where LLVM is playing catch up with GCC. I also would like to see a pure Rust compiler toolchain, but sometimes pragmatism wins.
- sunfish 8y agoIndeed, pragmatism is why we don't define success in those terms.
- mamcx 8y agoNot as "replace" but be a decent alternative, easier and less complicated?
- qznc 8y agoFaster would be the most desirable attribute in my opinion. Rust should be much faster at least for development builds and LLVM is one of the parts which slows it down.
- kodablah 8y agoDoes it, or are there plans to, translate the IR to WASM (i.e. a WASM backend instead of just frontend)? This seems to have a more expressive set of instructions than WASM and could be a good target for compilers.
- sunfish 8y agoIt is something we're thinking about. You might have to limit yourself to a subset of the full Cranelift IR for this, but I think we could make that subset big enough to be interesting.
- smaddox 8y agoIt's interesting that booleans are stored as 0 or -1 [1]. I wonder what drove that decision. I just checked, and Rust's LLVM stores booleans as 0 or 1. [1] https://cranelift.readthedocs.io/en/latest/ir.html#boolean-types https://cranelift.readthedocs.io/en/latest/ir.html#boolean-t...
- sunfish 8y agoA typical Rust bool would use `b1`, which doesn't have an observable representation.
- sunfish 8y agoAlso, the [bint instruction](https://cranelift.readthedocs.io/en/latest/ir.html#inst-bint https://cranelift.readthedocs.io/en/latest/ir.html#inst-bint) converts a bool value to an integer 0 or 1.
- steveklabnik 8y agoWe did, eventually, after years, define it as a full byte though. https://github.com/rust-lang/rust/pull/46156 https://github.com/rust-lang/rust/pull/46156
- KMag 8y agoWell, for one, if true is -1 (using two's complement), then a bunch of conditionals and bit-twiddling become more simple. For instance: x = some_bool ? a : b; can be compiled as a branchless: x = (some_bool & a) | (~some_bool & b); This is why Forth also represents true as -1.
- pcwalton 8y agoAlso why SIMD fairly universally uses -1 for true. C arguably picked the wrong semantics here. Hardware picks up the slack, however, with the setcc instructions on x86, csinc on AArch64, etc.
- eridius 8y agoThat would only be true if `a` is guaranteed to be a non-zero value though, right? e.g. `x = some_bool ? 0 : 1` would not be able to be represented that way.
- jeandejean 8y agoI understand the enthusiasm for Rust, but I'm growing tired of these project advertised as "written in Rust" like it proves a point in itself.
- sunfish 8y agoFwiw, "written in Rust" was added by whoever submitted the HN link; the project's own web page and documentation don't describe it like that.
- stmw 8y agoSure, it doesn't prove a point, but the headline does show the level of growth Rust is enjoying... It's not that different than "game X on Linux" in the early days, or "ML in Python".
- Apaec 8y agoI grow tired of "written in Go" or "written in Python". At least Rust means high performance and no GC.