Auto merge of #155726 - Zoxc:global-jemalloc-only, r=<try>
Some checks failed
CI / Calculate job matrix (push) Has been cancelled
CI / ${{ matrix.full_name }} (push) Has been cancelled
CI / publish toolstate (push) Has been cancelled

[TEST] Use only global Rust jemalloc
This commit is contained in:
bors
2026-04-27 07:55:15 +00:00
9 changed files with 23 additions and 76 deletions

View File

@@ -756,7 +756,7 @@ checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681"
dependencies = [
"serde",
"termcolor",
"unicode-width 0.1.14",
"unicode-width 0.2.2",
]
[[package]]
@@ -3429,7 +3429,6 @@ dependencies = [
"rustc_public",
"rustc_public_bridge",
"rustc_windows_rc",
"tikv-jemalloc-sys",
"wasi 0.14.2+wasi-0.2.4",
]
@@ -3837,6 +3836,7 @@ dependencies = [
"rustc_target",
"serde_json",
"shlex",
"tikv-jemallocator",
"tracing",
"windows 0.61.3",
]
@@ -5551,6 +5551,16 @@ dependencies = [
"libc",
]
[[package]]
name = "tikv-jemallocator"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0359b4327f954e0567e69fb191cf1436617748813819c94b8cd4a431422d053a"
dependencies = [
"libc",
"tikv-jemalloc-sys",
]
[[package]]
name = "tinystr"
version = "0.8.2"

View File

@@ -26,16 +26,10 @@ rustc_public_bridge = { path = "../rustc_public_bridge" }
getrandom = "=0.3.3"
wasi = "=0.14.2"
[dependencies.tikv-jemalloc-sys]
version = "0.6.1"
optional = true
features = ['override_allocator_on_supported_platforms']
[features]
# tidy-alphabetical-start
check_only = ['rustc_driver_impl/check_only']
jemalloc = ['dep:tikv-jemalloc-sys']
jemalloc = ['rustc_driver_impl/jemalloc']
llvm = ['rustc_driver_impl/llvm']
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
llvm_offload = ['rustc_driver_impl/llvm_offload']

View File

@@ -5,41 +5,6 @@
use std::process::ExitCode;
// A note about jemalloc: rustc uses jemalloc when built for CI and
// distribution. The obvious way to do this is with the `#[global_allocator]`
// mechanism. However, for complicated reasons (see
// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
// details) that mechanism doesn't work here. Also, we'd like to use a
// consistent allocator across the rustc <-> llvm boundary, and
// `#[global_allocator]` wouldn't provide that.
//
// Instead, we use a lower-level mechanism, namely the
// `"override_allocator_on_supported_platforms"` Cargo feature of jemalloc-sys.
//
// This makes jemalloc-sys override the libc/system allocator's implementation
// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
// calls `libc::malloc()` et al., is actually calling into jemalloc.
//
// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
// provides an impl of that trait, which is called `Jemalloc`) is that we
// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
// It's unclear how much performance is lost because of this.
//
// NOTE: Even though Cargo passes `--extern` with `tikv_jemalloc_sys`, we still need to `use` the
// crate for the compiler to see the `#[used]`, see https://github.com/rust-lang/rust/issues/64402.
// This is similarly required if we used a crate with `#[global_allocator]`.
//
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
//
// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
// to compare their performance, see
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
// for an example of how to do so.
#[cfg(feature = "jemalloc")]
use tikv_jemalloc_sys as _;
fn main() -> ExitCode {
rustc_driver::main()
}

View File

@@ -55,9 +55,14 @@ features = [
ctrlc = "3.4.4"
# tidy-alphabetical-end
[dependencies.tikv-jemallocator]
version = "0.6.1"
optional = true
[features]
# tidy-alphabetical-start
check_only = ['rustc_interface/check_only']
jemalloc = ['dep:tikv-jemallocator']
llvm = ['rustc_interface/llvm']
llvm_enzyme = ['rustc_interface/llvm_enzyme']
llvm_offload = ['rustc_interface/llvm_offload']

View File

@@ -63,6 +63,10 @@ use rustc_target::json::ToJson;
use rustc_target::spec::{Target, TargetTuple};
use tracing::trace;
#[cfg(feature = "jemalloc")]
#[global_allocator]
static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
#[allow(unused_macros)]
macro do_not_use_print($($t:tt)*) {
std::compile_error!(

View File

@@ -56,15 +56,6 @@ extern crate rustc_target;
extern crate rustc_trait_selection;
extern crate test;
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this.
///
/// FIXME(madsmtm): This is loaded from the sysroot that was built with the other `rustc` crates
/// above, instead of via Cargo as you'd normally do. This is currently needed for LTO due to
/// https://github.com/rust-lang/cc-rs/issues/1613.
#[cfg(feature = "jemalloc")]
extern crate tikv_jemalloc_sys as _;
use std::env::{self, VarError};
use std::io::{self, IsTerminal};
use std::path::Path;

View File

@@ -11,15 +11,6 @@ extern crate rustc_interface;
extern crate rustc_session;
extern crate rustc_span;
/// See docs in <https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs>
/// and <https://github.com/rust-lang/rust/pull/146627> for why we need this.
///
/// FIXME(madsmtm): This is loaded from the sysroot that was built with the other `rustc` crates
/// above, instead of via Cargo as you'd normally do. This is currently needed for LTO due to
/// <https://github.com/rust-lang/cc-rs/issues/1613>.
#[cfg(feature = "jemalloc")]
extern crate tikv_jemalloc_sys as _;
use clippy_utils::sym;
use declare_clippy_lint::LintListBuilder;
use rustc_interface::interface;

View File

@@ -18,20 +18,6 @@ extern crate rustc_middle;
extern crate rustc_session;
extern crate rustc_span;
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this.
///
/// FIXME(madsmtm): This is loaded from the sysroot that was built with the other `rustc` crates
/// above, instead of via Cargo as you'd normally do. This is currently needed for LTO due to
/// https://github.com/rust-lang/cc-rs/issues/1613.
#[cfg(feature = "jemalloc")]
// Make sure `--all-features` works: only Linux and macOS actually use jemalloc, and not on arm32.
#[cfg(all(
any(target_os = "linux", target_os = "macos"),
any(target_arch = "x86_64", target_arch = "x86", target_arch = "aarch64"),
))]
extern crate tikv_jemalloc_sys as _;
mod log;
use std::env;

View File

@@ -442,6 +442,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"thorin-dwp",
"thread_local",
"tikv-jemalloc-sys",
"tikv-jemallocator",
"tinystr",
"tinyvec",
"tinyvec_macros",