Skip to content

Commit ea0be1e

Browse files
committed
Auto merge of #138764 - Zoxc:win-mimalloc, r=<try>
Use mimalloc as the allocator on `x86_64-pc-windows-msvc` try-job: dist-x86_64-msvc
2 parents f53b654 + 141c108 commit ea0be1e

30 files changed

Lines changed: 197 additions & 51 deletions

File tree

Cargo.lock

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681"
756756
dependencies = [
757757
"serde",
758758
"termcolor",
759-
"unicode-width 0.1.14",
759+
"unicode-width 0.2.2",
760760
]
761761

762762
[[package]]
@@ -977,6 +977,12 @@ dependencies = [
977977
"windows-sys 0.61.2",
978978
]
979979

980+
[[package]]
981+
name = "cty"
982+
version = "0.2.2"
983+
source = "registry+https://github.com/rust-lang/crates.io-index"
984+
checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
985+
980986
[[package]]
981987
name = "curl"
982988
version = "0.4.49"
@@ -2216,6 +2222,16 @@ version = "0.2.15"
22162222
source = "registry+https://github.com/rust-lang/crates.io-index"
22172223
checksum = "f9fbbcab51052fe104eb5e5d351cf728d30a5be1fe14d9be8a3b097481fb97de"
22182224

2225+
[[package]]
2226+
name = "libmimalloc-sys"
2227+
version = "0.1.47"
2228+
source = "registry+https://github.com/rust-lang/crates.io-index"
2229+
checksum = "2d1eacfa31c33ec25e873c136ba5669f00f9866d0688bea7be4d3f7e43067df6"
2230+
dependencies = [
2231+
"cc",
2232+
"cty",
2233+
]
2234+
22192235
[[package]]
22202236
name = "libredox"
22212237
version = "0.1.10"
@@ -2399,6 +2415,15 @@ dependencies = [
23992415
"libc",
24002416
]
24012417

2418+
[[package]]
2419+
name = "mimalloc"
2420+
version = "0.1.50"
2421+
source = "registry+https://github.com/rust-lang/crates.io-index"
2422+
checksum = "b3627c4272df786b9260cabaa46aec1d59c93ede723d4c3ef646c503816b0640"
2423+
dependencies = [
2424+
"libmimalloc-sys",
2425+
]
2426+
24022427
[[package]]
24032428
name = "minifier"
24042429
version = "0.3.6"
@@ -3808,6 +3833,8 @@ dependencies = [
38083833
"ctrlc",
38093834
"jiff",
38103835
"libc",
3836+
"libmimalloc-sys",
3837+
"mimalloc",
38113838
"rustc_abi",
38123839
"rustc_ast",
38133840
"rustc_ast_pretty",

INSTALL.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ See [the rustc-dev-guide for more info][sysllvm].
9696
--set llvm.libzstd=true \
9797
--set llvm.ninja=false \
9898
--set rust.debug-assertions=false \
99-
--set rust.jemalloc \
99+
--set rust.override-allocator=jemalloc \
100100
--set rust.bootstrap-override-lld=true \
101101
--set rust.lto=thin \
102102
--set rust.codegen-units=1

bootstrap.example.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,10 +845,11 @@
845845
# Useful for reproducible builds. Generally only set for releases
846846
#rust.remap-debuginfo = false
847847

848-
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
848+
# Link the compiler and LLVM against the specified allocator instead of the default libc allocator.
849849
# This option is only tested on Linux and OSX. It can also be configured per-target in the
850850
# [target.<tuple>] section.
851-
#rust.jemalloc = false
851+
# Possible options: "jemalloc"
852+
#rust.override-allocator = "jemalloc"
852853

853854
# Run tests in various test suites with the "nll compare mode" in addition to
854855
# running the tests in normal mode. Largely only used on CI and during local
@@ -1120,9 +1121,9 @@
11201121
# order to run `x check`.
11211122
#optimized-compiler-builtins = build.optimized-compiler-builtins (bool or path)
11221123

1123-
# Link the compiler and LLVM against `jemalloc` instead of the default libc allocator.
1124-
# This overrides the global `rust.jemalloc` option. See that option for more info.
1125-
#jemalloc = rust.jemalloc (bool)
1124+
# Link the compiler and LLVM against the specified allocator instead of the default libc allocator.
1125+
# This overrides the global `rust.override-allocator` option. See that option for more info.
1126+
#override-allocator = rust.override-allocator (string)
11261127

11271128
# The linker configuration that will *override* the default linker used for Linux
11281129
# targets in the built compiler.

compiler/rustc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ llvm = ['rustc_driver_impl/llvm']
4040
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
4141
llvm_offload = ['rustc_driver_impl/llvm_offload']
4242
max_level_info = ['rustc_driver_impl/max_level_info']
43+
mimalloc = ['rustc_driver_impl/mimalloc']
4344
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
4445
# tidy-alphabetical-end
4546

compiler/rustc_driver_impl/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,23 @@ features = [
5555
ctrlc = "3.4.4"
5656
# tidy-alphabetical-end
5757

58+
[target.'cfg(target_os = "windows")'.dependencies.mimalloc]
59+
version = "0.1.50"
60+
optional = true
61+
62+
[target.'cfg(target_os = "windows")'.dependencies.libmimalloc-sys]
63+
version = "0.1.47"
64+
optional = true
65+
features = ["extended"]
66+
5867
[features]
5968
# tidy-alphabetical-start
6069
check_only = ['rustc_interface/check_only']
6170
llvm = ['rustc_interface/llvm']
6271
llvm_enzyme = ['rustc_interface/llvm_enzyme']
6372
llvm_offload = ['rustc_interface/llvm_offload']
6473
max_level_info = ['rustc_log/max_level_info']
74+
mimalloc = ['dep:mimalloc', 'dep:libmimalloc-sys']
6575
rustc_randomized_layouts = [
6676
'rustc_index/rustc_randomized_layouts',
6777
'rustc_middle/rustc_randomized_layouts'
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#[cfg(all(target_os = "windows", feature = "mimalloc"))]
2+
#[global_allocator]
3+
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
4+
5+
#[cfg(all(target_os = "windows", feature = "mimalloc"))]
6+
pub mod fns {
7+
use std::os::raw::{c_char, c_int, c_void};
8+
9+
#[unsafe(no_mangle)]
10+
pub unsafe extern "C" fn calloc(items: usize, size: usize) -> *mut c_void {
11+
unsafe { libmimalloc_sys::mi_calloc(items, size) }
12+
}
13+
14+
#[unsafe(no_mangle)]
15+
pub unsafe extern "C" fn posix_memalign(
16+
ptr: *mut *mut c_void,
17+
align: usize,
18+
size: usize,
19+
) -> c_int {
20+
unsafe { libmimalloc_sys::mi_posix_memalign(ptr, align, size) }
21+
}
22+
23+
#[unsafe(no_mangle)]
24+
pub unsafe extern "C" fn strndup(s: *const c_char, n: usize) -> *mut c_char {
25+
unsafe { libmimalloc_sys::mi_strndup(s, n) }
26+
}
27+
28+
#[unsafe(no_mangle)]
29+
pub unsafe extern "C" fn strdup(s: *const c_char) -> *mut c_char {
30+
unsafe { libmimalloc_sys::mi_strdup(s) }
31+
}
32+
33+
#[unsafe(no_mangle)]
34+
pub unsafe extern "C" fn aligned_alloc(align: usize, size: usize) -> *mut c_void {
35+
unsafe { libmimalloc_sys::mi_aligned_alloc(align, size) }
36+
}
37+
38+
#[unsafe(no_mangle)]
39+
pub unsafe extern "C" fn malloc(size: usize) -> *mut c_void {
40+
unsafe { libmimalloc_sys::mi_malloc(size) }
41+
}
42+
43+
#[unsafe(no_mangle)]
44+
pub unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void {
45+
unsafe { libmimalloc_sys::mi_realloc(ptr, size) }
46+
}
47+
48+
#[unsafe(no_mangle)]
49+
pub unsafe extern "C" fn free(ptr: *mut c_void) {
50+
unsafe {
51+
libmimalloc_sys::mi_free(ptr);
52+
}
53+
}
54+
}

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ use rustc_target::json::ToJson;
6363
use rustc_target::spec::{Target, TargetTuple};
6464
use tracing::trace;
6565

66+
pub mod alloc;
67+
6668
#[allow(unused_macros)]
6769
macro do_not_use_print($($t:tt)*) {
6870
std::compile_error!(

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::core::builder::{
2828
};
2929
use crate::core::config::toml::target::DefaultLinuxLinkerOverride;
3030
use crate::core::config::{
31-
CompilerBuiltins, DebuginfoLevel, LlvmLibunwind, RustcLto, TargetSelection,
31+
CompilerBuiltins, DebuginfoLevel, LlvmLibunwind, OverrideAllocator, RustcLto, TargetSelection,
3232
};
3333
use crate::utils::build_stamp;
3434
use crate::utils::build_stamp::BuildStamp;
@@ -1411,7 +1411,9 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
14111411
}
14121412

14131413
// See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the tool build step.
1414-
if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
1414+
if let Some(OverrideAllocator::Jemalloc) = builder.config.override_allocator(target)
1415+
&& env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
1416+
{
14151417
// Build jemalloc on AArch64 with support for page sizes up to 64K
14161418
// See: https://github.com/rust-lang/rust/pull/135081
14171419
if target.starts_with("aarch64") {

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::core::builder;
2020
use crate::core::builder::{
2121
Builder, Cargo as CargoCommand, RunConfig, ShouldRun, Step, StepMetadata, cargo_profile_var,
2222
};
23-
use crate::core::config::{DebuginfoLevel, RustcLto, TargetSelection};
23+
use crate::core::config::{DebuginfoLevel, OverrideAllocator, RustcLto, TargetSelection};
2424
use crate::utils::exec::{BootstrapCommand, command};
2525
use crate::utils::helpers::{add_dylib_path, exe, t};
2626
use crate::{Compiler, FileType, Kind, Mode};
@@ -229,7 +229,9 @@ pub fn prepare_tool_cargo(
229229
cargo.env("LZMA_API_STATIC", "1");
230230

231231
// See also the "JEMALLOC_SYS_WITH_LG_PAGE" setting in the compile build step.
232-
if builder.config.jemalloc(target) && env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none() {
232+
if let Some(OverrideAllocator::Jemalloc) = builder.config.override_allocator(target)
233+
&& env::var_os("JEMALLOC_SYS_WITH_LG_PAGE").is_none()
234+
{
233235
// Build jemalloc on AArch64 with support for page sizes up to 64K
234236
// See: https://github.com/rust-lang/rust/pull/135081
235237
if target.starts_with("aarch64") {
@@ -752,8 +754,8 @@ impl Step for Rustdoc {
752754
// to build rustdoc.
753755
//
754756
let mut extra_features = Vec::new();
755-
if builder.config.jemalloc(target) {
756-
extra_features.push("jemalloc".to_string());
757+
if let Some(allocator) = builder.config.override_allocator(target) {
758+
extra_features.push(allocator.feature_name().to_string());
757759
}
758760

759761
let compilers = RustcPrivateCompilers::from_target_compiler(builder, target_compiler);
@@ -1576,8 +1578,8 @@ tool_rustc_extended!(Clippy {
15761578
stable: true,
15771579
add_bins_to_sysroot: ["clippy-driver"],
15781580
add_features: |builder, target, features| {
1579-
if builder.config.jemalloc(target) {
1580-
features.push("jemalloc".to_string());
1581+
if let Some(allocator) = builder.config.override_allocator(target) {
1582+
features.push(allocator.feature_name().to_string());
15811583
}
15821584
}
15831585
});
@@ -1587,8 +1589,8 @@ tool_rustc_extended!(Miri {
15871589
stable: false,
15881590
add_bins_to_sysroot: ["miri"],
15891591
add_features: |builder, target, features| {
1590-
if builder.config.jemalloc(target) {
1591-
features.push("jemalloc".to_string());
1592+
if let Some(allocator) = builder.config.override_allocator(target) {
1593+
features.push(allocator.feature_name().to_string());
15921594
}
15931595
},
15941596
// Always compile also tests when building miri. Otherwise feature unification can cause rebuilds between building and testing miri.

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ use crate::core::config::toml::target::{
4848
DefaultLinuxLinkerOverride, Target, TomlTarget, default_linux_linker_overrides,
4949
};
5050
use crate::core::config::{
51-
CompilerBuiltins, DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, ReplaceOpt,
52-
RustcLto, SplitDebuginfo, StringOrBool, threads_from_config,
51+
CompilerBuiltins, DebuginfoLevel, DryRun, GccCiMode, LlvmLibunwind, Merge, OverrideAllocator,
52+
ReplaceOpt, RustcLto, SplitDebuginfo, StringOrBool, threads_from_config,
5353
};
5454
use crate::core::download::{
5555
DownloadContext, download_beta_toolchain, is_download_ci_available, maybe_download_rustfmt,
@@ -242,7 +242,7 @@ pub struct Config {
242242
pub hosts: Vec<TargetSelection>,
243243
pub targets: Vec<TargetSelection>,
244244
pub local_rebuild: bool,
245-
pub jemalloc: bool,
245+
pub override_allocator: Option<OverrideAllocator>,
246246
pub control_flow_guard: bool,
247247
pub ehcont_guard: bool,
248248

@@ -570,7 +570,7 @@ impl Config {
570570
thin_lto_import_instr_limit: rust_thin_lto_import_instr_limit,
571571
parallel_frontend_threads: rust_parallel_frontend_threads,
572572
remap_debuginfo: rust_remap_debuginfo,
573-
jemalloc: rust_jemalloc,
573+
override_allocator: rust_override_allocator,
574574
test_compare_mode: rust_test_compare_mode,
575575
llvm_libunwind: rust_llvm_libunwind,
576576
control_flow_guard: rust_control_flow_guard,
@@ -900,7 +900,7 @@ impl Config {
900900
codegen_backends: target_codegen_backends,
901901
runner: target_runner,
902902
optimized_compiler_builtins: target_optimized_compiler_builtins,
903-
jemalloc: target_jemalloc,
903+
override_allocator: target_override_allocator,
904904
} = cfg;
905905

906906
let mut target = Target::from_triple(&triple);
@@ -976,7 +976,7 @@ impl Config {
976976
target.rpath = target_rpath;
977977
target.rustflags = target_rustflags.unwrap_or_default();
978978
target.optimized_compiler_builtins = target_optimized_compiler_builtins;
979-
target.jemalloc = target_jemalloc;
979+
target.override_allocator = target_override_allocator;
980980
if let Some(backends) = target_codegen_backends {
981981
target.codegen_backends =
982982
Some(parse_codegen_backends(backends, &format!("target.{triple}")))
@@ -1371,7 +1371,6 @@ impl Config {
13711371
initial_rustdoc,
13721372
initial_rustfmt,
13731373
initial_sysroot,
1374-
jemalloc: rust_jemalloc.unwrap_or(false),
13751374
jobs: Some(threads_from_config(flags_jobs.or(build_jobs).unwrap_or(0))),
13761375
json_output: flags_json_output,
13771376
keep_stage: flags_keep_stage,
@@ -1433,6 +1432,7 @@ impl Config {
14331432
on_fail: flags_on_fail,
14341433
optimized_compiler_builtins,
14351434
out,
1435+
override_allocator: rust_override_allocator,
14361436
patch_binaries_for_nix: build_patch_binaries_for_nix,
14371437
path_modification_cache,
14381438
paths: flags_paths,
@@ -1864,8 +1864,11 @@ impl Config {
18641864
self.enabled_codegen_backends(target).first().unwrap()
18651865
}
18661866

1867-
pub fn jemalloc(&self, target: TargetSelection) -> bool {
1868-
self.target_config.get(&target).and_then(|cfg| cfg.jemalloc).unwrap_or(self.jemalloc)
1867+
pub fn override_allocator(&self, target: TargetSelection) -> Option<OverrideAllocator> {
1868+
self.target_config
1869+
.get(&target)
1870+
.and_then(|cfg| cfg.override_allocator)
1871+
.or(self.override_allocator)
18691872
}
18701873

18711874
pub fn rpath_enabled(&self, target: TargetSelection) -> bool {

0 commit comments

Comments
 (0)