Quite some exciting progress since the last progress report! There have been 180 commits since the last progress report.

As of today, rustc_codegen_cranelift is available on nightly! :tada: You can run rustup component add rustc-codegen-cranelift-preview --toolchain nightly to install it and then either CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift cargo +nightly build -Zcodegen-backend to use it for the current invocation or add

[unstable]
codegen-backend = true

[profile.dev]
codegen-backend = "cranelift"

to .cargo/config.toml or

# This line needs to come before anything else in Cargo.toml
cargo-features = ["codegen-backend"]

[profile.dev]
codegen-backend = "cranelift"

to Cargo.toml to enable it by default for debug builds. You can also set codegen-backend for individual packages using [profile.dev.package.my_program] codegen-backend = "cranelift". This would for example allow building a game engine using LLVM all optimizations enabled, but your game logic using Cranelift for faster iteration.

The following targets are currently supported:

  • x86_64-unknown-linux-gnu
  • x86_64-unknown-linux-musl
  • x86_64-apple-darwin
  • aarch64-unknown-linux-gnu
  • aarch64-unknown-linux-musl

Windows support has been omitted for now. And for macOS currently on supports x86_64 as Apple invented their own calling convention for arm64 for which variadic functions can’t easily be implemented as hack. If you are using an M1 processor, you could try installing the x86_64 version of rustc and then using Rosetta 2. Rosetta 2 will hurt performance though, so you will need to try if it is faster than the LLVM backend with arm64 rustc.

Also be aware that there are currently still some missing features.

Achievements in the past three months

Distributing as rustup component

As I already indicated at the start of this progress report, cg_clif is now available as rustup component.

Moved to the rust-lang org

Rustc_codegen_cranelift is now part of the rust-lang github organization: https://github.com/rust-lang/rustc_codegen_cranelift/

Risc-V support

While Cranelift has had a riscv64 backend for a couple of months now, only recently some of the features have been implemented as well as some bug fixes have been done by @afonso360 to make cg_clif work on linux riscv64gc. Once that was done I only needed to add inline assembly support for riscv64.

  • #1398: Add riscv64 linux support

SIMD

A whole bunch more x86_64 and arm64 vendor intrinsics have been implemented. This includes arm64 vendor intrinsics used by newer regex versions and the x86_64 vendor intrinsics used by rav1e and image. In addition a bunch of the new platform independent simd intrinsics used by std::simd have been implemented. The hack to disable detection of target features using is_x86_feature_detected!() has now been removed when inline asm support is enabled. This hack never worked when using a standard library compiled by LLVM anyway and enough vendor intrinsics are now supported to not need it anymore most of the time.

  • c974bc8: Update regex and implement necessary AArch64 vendor intrinsics
  • f1ede97: Update portable-simd test and implement new simd_* platform intrinsics
  • e5ba1e8: Implement llvm intrinsics necessary for rav1e
  • a558968: Implement all llvm intrinsics necessary for the image crate

Inline assembly

Inline assembly is now supported on arm64 and riscv64 as well as macOS and Windows. Futhermore support for inline assembly is now being tested in cg_clif’s CI. With the exception of sym operands, inline assembly is now declared as stable for usage with cg_clif.

  • #1396: Support inline asm on AArch64
  • #1397: Test inline asm support on CI
  • issue #1204: Full asm!() support
  • #1403: Support and stabilize inline asm on all platforms

Challenges

SIMD

While core::simd is fully supported through emulation using scalar operations, many platform specific vendor intrinsics in core::arch are not supported. This has been improving though with the most important x86_64 and arm64 vendor intrinsics implemented.

If your program uses any unsupported vendor intrinsics you will get a compile time warning and if it actually gets reached, the program will abort with an error message indicating which intrinsic is unimplemented. Please open an issue if this happens.

  • issue #171: std::arch SIMD intrinsics

Cleanup during stack unwinding on panics

Cranelift currently doesn’t have support for cleanup during stack unwinding. I’m working on implementing this and integrating it with cg_clif.

Until this is fixed panic::catch_unwind() will not work and panicking in a single thread will abort the entire process just like panic=abort would. This also means you will have to use -Zpanic-abort-tests in combination with setting panic = "abort" if you want a test failure to not bring down the entire test harness.

Contributing

Contributions are always appreciated. Feel free to take a look at good first issues and ping me (@bjorn3) for help on either the relevant github issue or preferably on the rust lang zulip if you get stuck.