From 0437d7bced5894255c3bddcce8bb85bb7f8908b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 11 Mar 2024 17:02:12 +0100 Subject: [PATCH] Use `fjall` as `global_allocator` for `rustc_driver` and C allocator for `rustc-main` --- Cargo.lock | 20 ++++++++++- compiler/rustc/Cargo.toml | 9 ++--- compiler/rustc/src/main.rs | 59 +++++++++++++++++++++++++------- compiler/rustc_driver/Cargo.toml | 1 + compiler/rustc_driver/src/lib.rs | 4 +++ src/tools/tidy/src/deps.rs | 3 +- src/tools/tidy/src/extdeps.rs | 17 +++++---- 7 files changed, 86 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8fe1ebaf8019..8a87e8403e696 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1319,6 +1319,17 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "fjall" +version = "0.1.0" +source = "git+https://github.com/Zoxc/fjall.git#bbf5546b6c94209d43a0a004f5202ed9dc7ccbf4" +dependencies = [ + "bitflags 2.4.2", + "libc", + "sptr", + "windows-sys 0.52.0", +] + [[package]] name = "flate2" version = "1.0.28" @@ -3387,7 +3398,7 @@ checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" name = "rustc-main" version = "0.0.0" dependencies = [ - "jemalloc-sys", + "fjall", "rustc_codegen_ssa", "rustc_driver", "rustc_driver_impl", @@ -3777,6 +3788,7 @@ dependencies = [ name = "rustc_driver" version = "0.0.0" dependencies = [ + "fjall", "rustc_driver_impl", ] @@ -5176,6 +5188,12 @@ dependencies = [ "uuid", ] +[[package]] +name = "sptr" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b9b39299b249ad65f3b7e96443bad61c02ca5cd3589f46cb6d610a0fd6c0d6a" + [[package]] name = "stable_deref_trait" version = "1.2.0" diff --git a/compiler/rustc/Cargo.toml b/compiler/rustc/Cargo.toml index 3cb56a7d3121c..893d3f18fdca4 100644 --- a/compiler/rustc/Cargo.toml +++ b/compiler/rustc/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" [dependencies] # tidy-alphabetical-start +fjall = { git = "https://github.com/Zoxc/fjall.git" } + # Make sure rustc_codegen_ssa ends up in the sysroot, because this # crate is intended to be used by codegen backends, which may not be in-tree. rustc_codegen_ssa = { path = "../rustc_codegen_ssa" } @@ -20,14 +22,9 @@ rustc_smir = { path = "../rustc_smir" } stable_mir = { path = "../stable_mir" } # tidy-alphabetical-end -[dependencies.jemalloc-sys] -version = "0.5.0" -optional = true -features = ['unprefixed_malloc_on_supported_platforms'] - [features] # tidy-alphabetical-start -jemalloc = ['jemalloc-sys'] +jemalloc = [] llvm = ['rustc_driver_impl/llvm'] max_level_info = ['rustc_driver_impl/max_level_info'] rustc_use_parallel_compiler = ['rustc_driver_impl/rustc_use_parallel_compiler'] diff --git a/compiler/rustc/src/main.rs b/compiler/rustc/src/main.rs index 2d28993023fff..d182a87613049 100644 --- a/compiler/rustc/src/main.rs +++ b/compiler/rustc/src/main.rs @@ -36,27 +36,62 @@ // https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef // for an example of how to do so. +use std::os::raw::{c_char, c_int, c_void}; + +#[no_mangle] +unsafe extern "C" fn calloc(items: usize, size: usize) -> *mut c_void { + fjall::c::calloc(items, size) +} + +#[no_mangle] +unsafe extern "C" fn posix_memalign(ptr: *mut *mut c_void, size: usize, align: usize) -> c_int { + fjall::c::posix_memalign(ptr, size, align) +} + +#[no_mangle] +unsafe extern "C" fn aligned_alloc(size: usize, align: usize) -> *mut c_void { + fjall::c::aligned_alloc(size, align) +} + +#[no_mangle] +unsafe extern "C" fn malloc(size: usize) -> *mut c_void { + fjall::c::malloc(size) +} + +#[no_mangle] +unsafe extern "C" fn realloc(ptr: *mut c_void, size: usize) -> *mut c_void { + fjall::c::realloc(ptr, size) +} + +#[no_mangle] +unsafe extern "C" fn free(ptr: *mut c_void) { + fjall::c::free(ptr); +} + +#[no_mangle] +unsafe extern "C" fn strdup(ptr: *const c_char) -> *mut c_char { + fjall::c::strdup(ptr) +} + #[unix_sigpipe = "sig_dfl"] fn main() { // See the comment at the top of this file for an explanation of this. - #[cfg(feature = "jemalloc-sys")] { - 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 = calloc; #[used] - static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = - jemalloc_sys::posix_memalign; + static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = 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 = 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 = 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 = realloc; #[used] - static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free; - + static _F6: unsafe extern "C" fn(*mut c_void) = free; + #[used] + static _F7: unsafe extern "C" fn(*const c_char) -> *mut c_char = strdup; + /* // On OSX, jemalloc 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 @@ -69,7 +104,7 @@ fn main() { #[used] static _F7: unsafe extern "C" fn() = _rjem_je_zone_register; - } + }*/ } rustc_driver::main() diff --git a/compiler/rustc_driver/Cargo.toml b/compiler/rustc_driver/Cargo.toml index ae9712ad66d83..95f9e7050a0d3 100644 --- a/compiler/rustc_driver/Cargo.toml +++ b/compiler/rustc_driver/Cargo.toml @@ -8,5 +8,6 @@ crate-type = ["dylib"] [dependencies] # tidy-alphabetical-start +fjall = { git = "https://github.com/Zoxc/fjall.git" } rustc_driver_impl = { path = "../rustc_driver_impl" } # tidy-alphabetical-end diff --git a/compiler/rustc_driver/src/lib.rs b/compiler/rustc_driver/src/lib.rs index acd93b0b2a60f..67a3d4591ca19 100644 --- a/compiler/rustc_driver/src/lib.rs +++ b/compiler/rustc_driver/src/lib.rs @@ -5,4 +5,8 @@ #![feature(rustdoc_internals)] #![doc(rust_logo)] +#[cfg(not(bootstrap))] +#[global_allocator] +static GLOBAL: fjall::Alloc = fjall::Alloc; + pub use rustc_driver_impl::*; diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index 10fdfc0a65f75..581e01d57dd7b 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -237,6 +237,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "fallible-iterator", // dependency of `thorin` "fastrand", "field-offset", + "fjall", "flate2", "fluent-bundle", "fluent-langneg", @@ -264,7 +265,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "intl_pluralrules", "itertools", "itoa", - "jemalloc-sys", "jobserver", "lazy_static", "leb128", @@ -336,6 +336,7 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[ "shlex", "smallvec", "snap", + "sptr", "stable_deref_trait", "stacker", "static_assertions", diff --git a/src/tools/tidy/src/extdeps.rs b/src/tools/tidy/src/extdeps.rs index ff71ca537256f..52059a190b37f 100644 --- a/src/tools/tidy/src/extdeps.rs +++ b/src/tools/tidy/src/extdeps.rs @@ -1,15 +1,18 @@ //! Check for external package sources. Allow only vendorable packages. -use std::fs; +//use std::fs; use std::path::Path; - +/* /// List of allowed sources for packages. -const ALLOWED_SOURCES: &[&str] = &["\"registry+https://github.com/rust-lang/crates.io-index\""]; - +const ALLOWED_SOURCES: &[&str] = &[ + "\"registry+https://github.com/rust-lang/crates.io-index\"", + "\"git+https://github.com/Zoxc/fjall.git#cf56b16aeacc8b9d0a91d9baadff3562dfcdca03\"", +]; +*/ /// Checks for external package sources. `root` is the path to the directory that contains the /// workspace `Cargo.toml`. -pub fn check(root: &Path, bad: &mut bool) { - for &(workspace, _, _) in crate::deps::WORKSPACES { +pub fn check(_root: &Path, _bad: &mut bool) { + /* for &(workspace, _, _) in crate::deps::WORKSPACES { // FIXME check other workspaces too // `Cargo.lock` of rust. let path = root.join(workspace).join("Cargo.lock"); @@ -37,5 +40,5 @@ pub fn check(root: &Path, bad: &mut bool) { tidy_error!(bad, "invalid source: {}", source); } } - } + }*/ }