Skip to content

Commit b200834

Browse files
committed
Auto merge of #138764 - Zoxc:win-mimalloc, r=<try>
Use mimalloc as the global allocator on `x86_64-pc-windows-msvc` This adds using mimalloc as the global allocator for the compiler as an option to `bootstrap.toml` and enables it for the `x86_64-pc-windows-msvc` dist builder. try-job: dist-x86_64-msvc
2 parents a4a11ac + 014c141 commit b200834

File tree

9 files changed

+55
-0
lines changed

9 files changed

+55
-0
lines changed

Cargo.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,16 @@ version = "0.2.11"
20302030
source = "registry+https://github.com/rust-lang/crates.io-index"
20312031
checksum = "8355be11b20d696c8f18f6cc018c4e372165b1fa8126cef092399c9951984ffa"
20322032

2033+
[[package]]
2034+
name = "libmimalloc-sys"
2035+
version = "0.1.40"
2036+
source = "registry+https://github.com/rust-lang/crates.io-index"
2037+
checksum = "07d0e07885d6a754b9c7993f2625187ad694ee985d60f23355ff0e7077261502"
2038+
dependencies = [
2039+
"cc",
2040+
"libc",
2041+
]
2042+
20332043
[[package]]
20342044
name = "libredox"
20352045
version = "0.1.3"
@@ -2217,6 +2227,15 @@ dependencies = [
22172227
"libc",
22182228
]
22192229

2230+
[[package]]
2231+
name = "mimalloc"
2232+
version = "0.1.44"
2233+
source = "registry+https://github.com/rust-lang/crates.io-index"
2234+
checksum = "99585191385958383e13f6b822e6b6d8d9cf928e7d286ceb092da92b43c87bc1"
2235+
dependencies = [
2236+
"libmimalloc-sys",
2237+
]
2238+
22202239
[[package]]
22212240
name = "mime"
22222241
version = "0.3.17"
@@ -3518,6 +3537,7 @@ version = "0.0.0"
35183537
dependencies = [
35193538
"ctrlc",
35203539
"libc",
3540+
"mimalloc",
35213541
"rustc_abi",
35223542
"rustc_ast",
35233543
"rustc_ast_lowering",

bootstrap.example.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,12 @@
748748
# [target.<tuple>] section.
749749
#jemalloc = false
750750

751+
# Use mimalloc as the global allocator for the compiler. Rust code will prefer `mimalloc` over
752+
# `jemalloc` if that is also enabled.
753+
# This option is only tested on Windows. It can also be configured per-target in the
754+
# [target.<tuple>] section.
755+
#mimalloc = false
756+
751757
# Run tests in various test suites with the "nll compare mode" in addition to
752758
# running the tests in normal mode. Largely only used on CI and during local
753759
# development of NLL

compiler/rustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ features = ['unprefixed_malloc_on_supported_platforms']
3030
jemalloc = ['dep:tikv-jemalloc-sys']
3131
llvm = ['rustc_driver_impl/llvm']
3232
max_level_info = ['rustc_driver_impl/max_level_info']
33+
mimalloc = ['rustc_driver_impl/mimalloc']
3334
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
3435
# tidy-alphabetical-end

compiler/rustc_driver_impl/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ time = { version = "0.3.36", default-features = false, features = ["alloc", "for
5454
tracing = { version = "0.1.35" }
5555
# tidy-alphabetical-end
5656

57+
[dependencies.mimalloc]
58+
version = "0.1.44"
59+
optional = true
60+
5761
[target.'cfg(all(unix, any(target_env = "gnu", target_os = "macos")))'.dependencies]
5862
# tidy-alphabetical-start
5963
libc = "0.2"
@@ -74,6 +78,7 @@ ctrlc = "3.4.4"
7478
# tidy-alphabetical-start
7579
llvm = ['rustc_interface/llvm']
7680
max_level_info = ['rustc_log/max_level_info']
81+
mimalloc = ['dep:mimalloc']
7782
rustc_randomized_layouts = [
7883
'rustc_index/rustc_randomized_layouts',
7984
'rustc_middle/rustc_randomized_layouts'

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ use time::OffsetDateTime;
7070
use time::macros::format_description;
7171
use tracing::trace;
7272

73+
#[cfg(feature = "mimalloc")]
74+
#[global_allocator]
75+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
76+
7377
#[allow(unused_macros)]
7478
macro do_not_use_print($($t:tt)*) {
7579
std::compile_error!(

src/bootstrap/src/core/config/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ pub struct Config {
346346
jemalloc: bool,
347347
#[cfg(test)]
348348
pub jemalloc: bool,
349+
pub mimalloc: bool,
349350
pub control_flow_guard: bool,
350351
pub ehcont_guard: bool,
351352

@@ -666,6 +667,7 @@ pub struct Target {
666667
pub codegen_backends: Option<Vec<String>>,
667668
pub optimized_compiler_builtins: Option<bool>,
668669
pub jemalloc: Option<bool>,
670+
pub mimalloc: Option<bool>,
669671
}
670672

671673
impl Target {
@@ -1228,6 +1230,7 @@ define_config! {
12281230
thin_lto_import_instr_limit: Option<u32> = "thin-lto-import-instr-limit",
12291231
remap_debuginfo: Option<bool> = "remap-debuginfo",
12301232
jemalloc: Option<bool> = "jemalloc",
1233+
mimalloc: Option<bool> = "mimalloc",
12311234
test_compare_mode: Option<bool> = "test-compare-mode",
12321235
llvm_libunwind: Option<String> = "llvm-libunwind",
12331236
control_flow_guard: Option<bool> = "control-flow-guard",
@@ -1270,6 +1273,7 @@ define_config! {
12701273
runner: Option<String> = "runner",
12711274
optimized_compiler_builtins: Option<bool> = "optimized-compiler-builtins",
12721275
jemalloc: Option<bool> = "jemalloc",
1276+
mimalloc: Option<bool> = "mimalloc",
12731277
}
12741278
}
12751279

@@ -1895,6 +1899,7 @@ impl Config {
18951899
thin_lto_import_instr_limit,
18961900
remap_debuginfo,
18971901
jemalloc,
1902+
mimalloc,
18981903
test_compare_mode,
18991904
llvm_libunwind,
19001905
control_flow_guard,
@@ -1967,6 +1972,7 @@ impl Config {
19671972
set(&mut config.rust_frame_pointers, frame_pointers);
19681973
config.rust_stack_protector = stack_protector;
19691974
set(&mut config.jemalloc, jemalloc);
1975+
set(&mut config.mimalloc, mimalloc);
19701976
set(&mut config.test_compare_mode, test_compare_mode);
19711977
set(&mut config.backtrace, backtrace);
19721978
if rust_description.is_some() {
@@ -2234,6 +2240,7 @@ impl Config {
22342240
target.rpath = cfg.rpath;
22352241
target.optimized_compiler_builtins = cfg.optimized_compiler_builtins;
22362242
target.jemalloc = cfg.jemalloc;
2243+
target.mimalloc = cfg.mimalloc;
22372244

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

2825+
pub fn mimalloc(&self, target: TargetSelection) -> bool {
2826+
self.target_config.get(&target).and_then(|cfg| cfg.mimalloc).unwrap_or(self.mimalloc)
2827+
}
2828+
28182829
pub fn default_codegen_backend(&self, target: TargetSelection) -> Option<String> {
28192830
self.codegen_backends(target).first().cloned()
28202831
}
@@ -3392,6 +3403,7 @@ fn check_incompatible_options_for_ci_rustc(
33923403
strip,
33933404
lld_mode,
33943405
jemalloc,
3406+
mimalloc,
33953407
rpath,
33963408
channel,
33973409
description,
@@ -3455,6 +3467,7 @@ fn check_incompatible_options_for_ci_rustc(
34553467
err!(current_rust_config.llvm_tools, llvm_tools, "rust");
34563468
err!(current_rust_config.llvm_bitcode_linker, llvm_bitcode_linker, "rust");
34573469
err!(current_rust_config.jemalloc, jemalloc, "rust");
3470+
err!(current_rust_config.mimalloc, mimalloc, "rust");
34583471
err!(current_rust_config.default_linker, default_linker, "rust");
34593472
err!(current_rust_config.stack_protector, stack_protector, "rust");
34603473
err!(current_rust_config.lto, lto, "rust");

src/bootstrap/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,9 @@ impl Build {
713713
if self.config.jemalloc(target) && check("jemalloc") {
714714
features.push("jemalloc");
715715
}
716+
if self.config.mimalloc(target) && check("mimalloc") {
717+
features.push("mimalloc");
718+
}
716719
if (self.config.llvm_enabled(target) || kind == Kind::Check) && check("llvm") {
717720
features.push("llvm");
718721
}

src/ci/github-actions/jobs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ auto:
584584
--enable-full-tools
585585
--enable-profiler
586586
--set rust.codegen-units=1
587+
--set rust.mimalloc
587588
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
588589
DIST_REQUIRE_ALL_TOOLS: 1
589590
CODEGEN_BACKENDS: llvm,cranelift

src/tools/tidy/src/deps.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
316316
"leb128",
317317
"libc",
318318
"libloading",
319+
"libmimalloc-sys",
319320
"linux-raw-sys",
320321
"litemap",
321322
"lock_api",
@@ -325,6 +326,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
325326
"measureme",
326327
"memchr",
327328
"memmap2",
329+
"mimalloc",
328330
"miniz_oxide",
329331
"nix",
330332
"nu-ansi-term",

0 commit comments

Comments
 (0)