惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
IT之家
IT之家
V
Visual Studio Blog
量子位
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
D
Docker
MyScale Blog
MyScale Blog
小众软件
小众软件
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Last Week in AI
Last Week in AI
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 【当耐特】

Rust Blog

Security Advisory for Cargo (CVE-2026-5223) | Rust Blog Security Advisory for Cargo (CVE-2026-5222) | Rust Blog Project goals update — April 2026 (end of 2025H2) | Rust Blog Rust is participating in Outreachy | Rust Blog Raising the baseline for the `nvptx64-nvidia-cuda` target | Rust Blog Announcing Google Summer of Code 2026 selected projects | Rust Blog Announcing Rust 1.95.0 | Rust Blog docs.rs: building fewer targets by default | Rust Blog Changes to WebAssembly targets and handling undefined symbols | Rust Blog Announcing Rust 1.94.1 | Rust Blog Security advisory for Cargo | Rust Blog What we heard about Rust's challenges | Rust Blog Call for Testing: Build Dir Layout v2 | Rust Blog Announcing rustup 1.29.0 | Rust Blog Announcing Rust 1.94.0 | Rust Blog 2025 State of Rust Survey Results | Rust Blog Rust debugging survey 2026 | Rust Blog Update on the October 15, 2018 incident on crates.io Announcing Rust 1.29.2 Announcing Rust 1.29 Announcing Rust 1.28 What is Rust 2018? Announcing Rust 1.27.2 Announcing Rust 1.27.1 Security Advisory for rustdoc Announcing Rust 1.27 Announcing Rust 1.26.2 Announcing Rust 1.26.1 Rust turns three Announcing Rust 1.26
Using rustc_codegen_cranelift for debug builds | Inside R...
Jynn Nelson on behalf of The Compiler Team · 2020-11-15 · via Rust Blog

What is rustc_codegen_cranelift?

rustc_codegen_cranelift, or just cg_clif for short, is a new experimental codegen backend for the Rust compiler. The existing backend is LLVM, which is very good at producing fast, highly optimized code, but is not very good at compiling code quickly. cg_clif, which uses the Cranelift project, would provide a fast backend which greatly improves compile times, at the cost of performing very few optimizations. This is a great fit for debug builds, and the hope is that cg_clif will eventually be the default backend in debug mode.

What is the progress of using rustc_codegen_cranelift for debug builds?

There has been a Major Change Proposal open for some time for making cg_clif part of the main Rust repository. Recently, the MCP was accepted and the compiler team merged rustc_cranelift_codegen into the main Rust git repository. cg_clif is not yet distributed with rustup, but this means you can now build it from source in-tree!

How do I use rustc_codegen_cranelift?

In this section, I'll walk through step-by-step how to build the new backend from source, then use it on your own projects. All code is copy/paste-able, and each step is explained.

First, let's build cg_clif from source.

$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
$ ./prepare.sh
$ ./build.sh

Now, we can start using it to compile a project. For demonstration purposes, I'll be using cargo, but you can use any Rust project supported by cg_clif.

$ cd ..
$ git clone https://github.com/rust-lang/cargo/
$ cd cargo
$ ../rustc_codegen_cranelift/build/cargo.sh build
...
    Finished dev [unoptimized + debuginfo] target(s) in 49.93s

It works! For comparison, let's see how long the equivalent LLVM backend would take.

$ rustup install nightly-2020-10-31
$ cargo +nightly-2020-10-31 build
...
    Finished dev [unoptimized + debuginfo] target(s) in 54.64s

LLVM takes a full 5 seconds longer for a full build. Next, let's try incremental builds:

$ git apply <<EOF
diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs
index bccb41121..703afa754 100644
--- a/src/cargo/lib.rs
+++ b/src/cargo/lib.rs
@@ -36,8 +36,8 @@ use anyhow::Error;
 use log::debug;
 use std::fmt;
 
-pub use crate::util::errors::{InternalError, VerboseError};
 pub use crate::util::{CargoResult, CliError, CliResult, Config};
+pub use crate::util::errors::{InternalError, VerboseError};
 
 pub const CARGO_ENV: &str = "CARGO";
EOF
$ ../rustc_codegen_cranelift/build/cargo.sh build
    Finished dev [unoptimized + debuginfo] target(s) in 7.98s
$ cargo +nightly-2020-10-31 build
   Compiling cargo v0.50.0 (/home/jyn/cargo)
    Finished dev [unoptimized + debuginfo] target(s) in 5.48s

LLVM is actually faster here: serde_derive took longer to run under cranelift, since it wasn't as optimized. Under cranelift it takes ~14% percent of the time, while under LLVM it takes less than 3%.

Building in-tree

This section is mostly for compiler hackers, but feel free to follow along even if you're just interested! The reason this isn't the recommended way to build cg_clif is because the Rust compiler takes a very long time to build.

First, download the Rust repository.

$ git clone https://github.com/rust-lang/rust

Now, let's set up the build system to use cg_clif.

$ cat > config.toml <<EOF
[rust]
codegen-backends = ["cranelift"]
EOF

Finally, let's run the build. This can take a long time, over a half-hour in some cases.

$ ./x.py build

How can I help?

You don't need to be a compiler developer to help improve cg_clif! The best way you can help is by testing cg_clif on different Rust crates across the ecosystem. Just while writing this article, I found two bugs, so there's plenty of work left to be done. Please report any bugs you find to the rustc_codegen_cranelift git repository.

In the future, we hope to distribute cg_clif with Rustup, and if it matures sufficiently, eventually make it the default backend for debug builds.