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 as the global allocator on x86_64-pc-windows-msvc #138764

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
22 changes: 21 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
@@ -2021,7 +2021,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc2f4eb4bc735547cfed7c0a4922cbd04a4655978c09b54f1f7b228750664c34"
dependencies = [
"cfg-if",
"windows-targets 0.48.5",
"windows-targets 0.52.6",
]

[[package]]
@@ -2030,6 +2030,16 @@ version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"

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

[[package]]
name = "libredox"
version = "0.1.3"
@@ -2217,6 +2227,15 @@ dependencies = [
"libc",
]

[[package]]
name = "mimalloc"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99585191385958383e13f6b822e6b6d8d9cf928e7d286ceb092da92b43c87bc1"
dependencies = [
"libmimalloc-sys",
]

[[package]]
name = "mime"
version = "0.3.17"
@@ -3518,6 +3537,7 @@ version = "0.0.0"
dependencies = [
"ctrlc",
"libc",
"mimalloc",
"rustc_abi",
"rustc_ast",
"rustc_ast_lowering",
6 changes: 6 additions & 0 deletions bootstrap.example.toml
Original file line number Diff line number Diff line change
@@ -748,6 +748,12 @@
# [target.<tuple>] section.
#jemalloc = false

# Use mimalloc as the global allocator for the compiler. Rust code will prefer `mimalloc` over
# `jemalloc` if that is also enabled.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reject setting both? That feels needlessly confusing to me and probably not hugely needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably just remove jemalloc if mimalloc v3 solves all regressions over it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name this field override-allocators in case we'd want to swap allocators in the future?

# This option is only tested on Windows. It can also be configured per-target in the
# [target.<tuple>] section.
#mimalloc = false

# Run tests in various test suites with the "nll compare mode" in addition to
# running the tests in normal mode. Largely only used on CI and during local
# development of NLL
1 change: 1 addition & 0 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -30,5 +30,6 @@ features = ['unprefixed_malloc_on_supported_platforms']
jemalloc = ['dep:tikv-jemalloc-sys']
llvm = ['rustc_driver_impl/llvm']
max_level_info = ['rustc_driver_impl/max_level_info']
mimalloc = ['rustc_driver_impl/mimalloc']
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
# tidy-alphabetical-end
5 changes: 5 additions & 0 deletions compiler/rustc_driver_impl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -54,6 +54,10 @@ time = { version = "0.3.36", default-features = false, features = ["alloc", "for
tracing = { version = "0.1.35" }
# tidy-alphabetical-end

[dependencies.mimalloc]
version = "0.1.44"
optional = true

[target.'cfg(all(unix, any(target_env = "gnu", target_os = "macos")))'.dependencies]
# tidy-alphabetical-start
libc = "0.2"
@@ -74,6 +78,7 @@ ctrlc = "3.4.4"
# tidy-alphabetical-start
llvm = ['rustc_interface/llvm']
max_level_info = ['rustc_log/max_level_info']
mimalloc = ['dep:mimalloc']
rustc_randomized_layouts = [
'rustc_index/rustc_randomized_layouts',
'rustc_middle/rustc_randomized_layouts'
4 changes: 4 additions & 0 deletions compiler/rustc_driver_impl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -70,6 +70,10 @@ use time::OffsetDateTime;
use time::macros::format_description;
use tracing::trace;

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just temporary, or the long-term state? As-is this doesn't affect LLVM's malloc calls?

IOW, why is this not changign the rustc/src/main.rs malloc overrides?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Long term. It's better than the linker hacks in main.rs.

Overriding the C allocator only in main.rs doesn't affect DLLs making it unsound without further work dealing with loadable backends and LLVM plugins.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't this still a regression for LLVM's allocations? It seems odd to measure perf for a halfway shift like this to mimalloc...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd still want the linker hacks on Linux / macOS for LLVM, using global_allocator just makes Rust allocation overriding less hacky.

As for perf on this PR, LLVM uses Windows' heap allocator both before and after.


#[allow(unused_macros)]
macro do_not_use_print($($t:tt)*) {
std::compile_error!(
13 changes: 13 additions & 0 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
@@ -345,6 +345,7 @@ pub struct Config {
jemalloc: bool,
#[cfg(test)]
pub jemalloc: bool,
pub mimalloc: bool,
pub control_flow_guard: bool,
pub ehcont_guard: bool,

@@ -663,6 +664,7 @@ pub struct Target {
pub codegen_backends: Option<Vec<String>>,
pub optimized_compiler_builtins: Option<bool>,
pub jemalloc: Option<bool>,
pub mimalloc: Option<bool>,
}

impl Target {
@@ -1225,6 +1227,7 @@ define_config! {
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",
@@ -1267,6 +1270,7 @@ define_config! {
runner: Option<String> = "runner",
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
jemalloc: Option<bool> = "jemalloc",
mimalloc: Option<bool> = "mimalloc",
}
}

@@ -1891,6 +1895,7 @@ impl Config {
thin_lto_import_instr_limit,
remap_debuginfo,
jemalloc,
mimalloc,
test_compare_mode,
llvm_libunwind,
control_flow_guard,
@@ -1963,6 +1968,7 @@ impl Config {
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);
if rust_description.is_some() {
@@ -2230,6 +2236,7 @@ impl Config {
target.rpath = cfg.rpath;
target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
target.jemalloc = cfg.jemalloc;
target.mimalloc = cfg.mimalloc;

if let Some(ref backends) = cfg.codegen_backends {
let available_backends = ["llvm", "cranelift", "gcc"];
@@ -2811,6 +2818,10 @@ impl Config {
self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
}

pub fn mimalloc(&self, target: TargetSelection) -> bool {
self.target_config.get(&target).and_then(|cfg| cfg.mimalloc).unwrap_or(self.mimalloc)
}

pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<String> {
self.codegen_backends(target).first().cloned()
}
@@ -3374,6 +3385,7 @@ fn check_incompatible_options_for_ci_rustc(
strip,
lld_mode,
jemalloc,
mimalloc,
rpath,
channel,
description,
@@ -3437,6 +3449,7 @@ fn check_incompatible_options_for_ci_rustc(
err!(current_rust_config.llvm_tools, llvm_tools, "rust");
err!(current_rust_config.llvm_bitcode_linker, llvm_bitcode_linker, "rust");
err!(current_rust_config.jemalloc, jemalloc, "rust");
err!(current_rust_config.mimalloc, mimalloc, "rust");
err!(current_rust_config.default_linker, default_linker, "rust");
err!(current_rust_config.stack_protector, stack_protector, "rust");
err!(current_rust_config.lto, lto, "rust");
3 changes: 3 additions & 0 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -712,6 +712,9 @@ impl Build {
if self.config.jemalloc(target) && check("jemalloc") {
features.push("jemalloc");
}
if self.config.mimalloc(target) && check("mimalloc") {
features.push("mimalloc");
}
if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
features.push("llvm");
}
1 change: 1 addition & 0 deletions src/ci/github-actions/jobs.yml
Original file line number Diff line number Diff line change
@@ -584,6 +584,7 @@ auto:
--enable-full-tools
--enable-profiler
--set rust.codegen-units=1
--set rust.mimalloc
SCRIPT: python x.py build --set rust.debug=true opt-dist && PGO_HOST=x86_64-pc-windows-msvc ./build/x86_64-pc-windows-msvc/stage0-tools-bin/opt-dist windows-ci -- python x.py dist bootstrap --include-default-paths
DIST_REQUIRE_ALL_TOOLS: 1
CODEGEN_BACKENDS: llvm,cranelift
2 changes: 2 additions & 0 deletions src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
@@ -316,6 +316,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"leb128",
"libc",
"libloading",
"libmimalloc-sys",
"linux-raw-sys",
"litemap",
"lock_api",
@@ -325,6 +326,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
"measureme",
"memchr",
"memmap2",
"mimalloc",
"miniz_oxide",
"nix",
"nu-ansi-term",
Loading