Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use mimalloc in rustc #132796

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,12 @@ dependencies = [
"windows-sys 0.59.0",
]

[[package]]
name = "cty"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"

[[package]]
name = "curl"
version = "0.4.46"
Expand Down Expand Up @@ -2000,6 +2006,17 @@ version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"

[[package]]
name = "libmimalloc-sys"
version = "0.1.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23aa6811d3bd4deb8a84dde645f943476d13b248d818edcf8ce0b2f37f036b44"
dependencies = [
"cc",
"cty",
"libc",
]

[[package]]
name = "libredox"
version = "0.1.3"
Expand Down Expand Up @@ -3153,7 +3170,7 @@ checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152"
name = "rustc-main"
version = "0.0.0"
dependencies = [
"jemalloc-sys",
"libmimalloc-sys",
"rustc_codegen_ssa",
"rustc_driver",
"rustc_driver_impl",
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ rustc_smir = { path = "../rustc_smir" }
stable_mir = { path = "../stable_mir" }
# tidy-alphabetical-end

[dependencies.jemalloc-sys]
version = "0.5.0"
[dependencies.libmimalloc-sys]
version = "0.1.39"
optional = true
features = ['unprefixed_malloc_on_supported_platforms']
default-features = false
features = ['extended', 'override']

[features]
# tidy-alphabetical-start
jemalloc = ['dep:jemalloc-sys']
llvm = ['rustc_driver_impl/llvm']
max_level_info = ['rustc_driver_impl/max_level_info']
mimalloc = ['dep:libmimalloc-sys']
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
rustc_use_parallel_compiler = ['rustc_driver_impl/rustc_use_parallel_compiler']
# tidy-alphabetical-end
22 changes: 12 additions & 10 deletions compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,38 @@

fn main() {
// See the comment at the top of this file for an explanation of this.
#[cfg(feature = "jemalloc")]
#[cfg(feature = "mimalloc")]
{
use std::os::raw::{c_int, c_void};

#[used]
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = libmimalloc_sys::calloc;
#[used]
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
jemalloc_sys::posix_memalign;
libmimalloc_sys::posix_memalign;
#[used]
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void =
libmimalloc_sys::aligned_alloc;
#[used]
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = libmimalloc_sys::malloc;
#[used]
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void =
libmimalloc_sys::realloc;
#[used]
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
static _F6: unsafe extern "C" fn(*mut c_void) = libmimalloc_sys::free;

// On OSX, jemalloc doesn't directly override malloc/free, but instead
// On OSX, mimalloc doesn't directly override malloc/free, but instead
// registers itself with the allocator's zone APIs in a ctor. However,
// the linker doesn't seem to consider ctors as "used" when statically
// linking, so we need to explicitly depend on the function.
#[cfg(target_os = "macos")]
{
extern "C" {
fn _rjem_je_zone_register();
fn _mi_macros_override_malloc();
}

#[used]
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
static _F7: unsafe extern "C" fn() = _mi_macros_override_malloc;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2094,11 +2094,11 @@ pub fn run_cargo(
// During check builds we need to keep crate metadata
keep = true;
} else if rlib_only_metadata {
if filename.contains("jemalloc_sys")
if filename.contains("libmimalloc_sys")
|| filename.contains("rustc_smir")
|| filename.contains("stable_mir")
{
// jemalloc_sys and rustc_smir are not linked into librustc_driver.so,
// libmimalloc_sys and rustc_smir are not linked into librustc_driver.so,
// so we need to distribute them as rlib to be able to use them.
keep |= filename.ends_with(".rlib");
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,8 @@ impl Step for Rustdoc {
// to build rustdoc.
//
let mut features = Vec::new();
if builder.config.jemalloc {
features.push("jemalloc".to_string());
if builder.config.mimalloc {
features.push("mimalloc".to_string());
}

// NOTE: Never modify the rustflags here, it breaks the build cache for other tools!
Expand Down
12 changes: 6 additions & 6 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ pub struct Config {
pub hosts: Vec<TargetSelection>,
pub targets: Vec<TargetSelection>,
pub local_rebuild: bool,
pub jemalloc: bool,
pub mimalloc: bool,
pub control_flow_guard: bool,
pub ehcont_guard: bool,

Expand Down Expand Up @@ -1155,7 +1155,7 @@ define_config! {
verify_llvm_ir: Option<bool> = "verify-llvm-ir",
thin_lto_import_instr_limit: Option<u32> = "thin-lto-import-instr-limit",
remap_debuginfo: Option<bool> = "remap-debuginfo",
jemalloc: Option<bool> = "jemalloc",
mimalloc: Option<bool> = "mimalloc",
test_compare_mode: Option<bool> = "test-compare-mode",
llvm_libunwind: Option<String> = "llvm-libunwind",
control_flow_guard: Option<bool> = "control-flow-guard",
Expand Down Expand Up @@ -1711,7 +1711,7 @@ impl Config {
verify_llvm_ir,
thin_lto_import_instr_limit,
remap_debuginfo,
jemalloc,
mimalloc,
test_compare_mode,
llvm_libunwind,
control_flow_guard,
Expand Down Expand Up @@ -1758,7 +1758,7 @@ impl Config {
set(&mut config.rust_strip, strip);
set(&mut config.rust_frame_pointers, frame_pointers);
config.rust_stack_protector = stack_protector;
set(&mut config.jemalloc, jemalloc);
set(&mut config.mimalloc, mimalloc);
set(&mut config.test_compare_mode, test_compare_mode);
set(&mut config.backtrace, backtrace);
config.description = description;
Expand Down Expand Up @@ -3064,7 +3064,7 @@ fn check_incompatible_options_for_ci_rustc(
stack_protector,
strip,
lld_mode,
jemalloc,
mimalloc,
rpath,
channel,
description,
Expand Down Expand Up @@ -3128,7 +3128,7 @@ fn check_incompatible_options_for_ci_rustc(
err!(current_rust_config.lld_mode, lld_mode);
err!(current_rust_config.llvm_tools, llvm_tools);
err!(current_rust_config.llvm_bitcode_linker, llvm_bitcode_linker);
err!(current_rust_config.jemalloc, jemalloc);
err!(current_rust_config.mimalloc, mimalloc);
err!(current_rust_config.default_linker, default_linker);
err!(current_rust_config.stack_protector, stack_protector);
err!(current_rust_config.lto, lto);
Expand Down
4 changes: 2 additions & 2 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,8 @@ impl Build {
crates.is_empty() || possible_features_by_crates.contains(feature)
};
let mut features = vec![];
if self.config.jemalloc && check("jemalloc") {
features.push("jemalloc");
if self.config.mimalloc && check("mimalloc") {
features.push("mimalloc");
}
if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
features.push("llvm");
Expand Down
3 changes: 2 additions & 1 deletion src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"crossbeam-utils",
"crypto-common",
"ctrlc",
"cty",
"darling",
"darling_core",
"darling_macro",
Expand Down Expand Up @@ -304,12 +305,12 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"intl_pluralrules",
"itertools",
"itoa",
"jemalloc-sys",
"jobserver",
"lazy_static",
"leb128",
"libc",
"libloading",
"libmimalloc-sys",
"linux-raw-sys",
"litemap",
"lock_api",
Expand Down
Loading