Hacker News new | past | comments | ask | show | jobs | submit login
Cranelift, a low-level retargetable code generator written in Rust (github.com)
63 points by exrook on Aug 11, 2018 | hide | past | web | favorite | 32 comments



It'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-t...


Well, 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.


Also 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.


That 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.


It works for me. Note that `|` is a bitwise-or (not that it really matters, it just removes a branch)

    const True = -1
    const False = 0
    const ternary = (condition, left, right) => \
      (condition & left) | (~condition & right)

    ternary(True, 0, 1) // 0
    ternary(False, 0, 1) // 1


Oh right, because the right-hand side evaluates to 0 as well in this case. Makes sense.


A typical Rust bool would use `b1`, which doesn't have an observable representation.


We did, eventually, after years, define it as a full byte though. https://github.com/rust-lang/rust/pull/46156


Also, the [bint instruction](https://cranelift.readthedocs.io/en/latest/ir.html#inst-bint) converts a bool value to an integer 0 or 1.


Boolean true is -1 in Visual Basic, too: https://docs.microsoft.com/en-us/dotnet/visual-basic/languag...


It was traditional in earlier BASICs too; I think I remember it being the case in QuickBASIC and AmigaBASIC.

I assume someone once upon a time thought it was a nice thing for ~x to be equal to !x.


The 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."


Hopefully will replace LLVM one day.


It will be an herculean effort though, given the amount of companies that contribute to LLVM.


Fortunately, this isn't required for the project to be successful :-).


Depends 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.


Indeed, pragmatism is why we don't define success in those terms.


Not as "replace" but be a decent alternative, easier and less complicated?


Faster 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.


Previously called “cretone”, by the way.


I heard that the project was renamed because no one could pronounced "Cretonne". :)


Plus it has the danger of sounding too much like "cretin"


Does 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.


It 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.


Any example or tutorial?


I can't say if it's complete or work in progress, but here is a demo: https://github.com/sunfishcode/simplejit-demo


Nice! This should be amazing for writing toy compiled languages! This looks way easier to get up and running than an LLVM backend.


There'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 ¯\_(ツ)_/¯.


I 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.


Fwiw, "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.


Sure, 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".


I grow tired of "written in Go" or "written in Python". At least Rust means high performance and no GC.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: