Skip to content

Commit

Permalink
Use fjall as global_allocator for rustc_driver and C allocator …
Browse files Browse the repository at this point in the history
…for `rustc-main`
  • Loading branch information
Zoxc committed Mar 24, 2024
1 parent 8f5e879 commit 4fd8e85
Showing 7 changed files with 86 additions and 27 deletions.
20 changes: 19 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
@@ -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#03ef49c19c1688a2d5e73fa4881c12bdc61c02f8"
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"
9 changes: 3 additions & 6 deletions compiler/rustc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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']
59 changes: 47 additions & 12 deletions compiler/rustc/src/main.rs
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions compiler/rustc_driver/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions compiler/rustc_driver/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::*;
3 changes: 2 additions & 1 deletion src/tools/tidy/src/deps.rs
Original file line number Diff line number Diff line change
@@ -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",
17 changes: 10 additions & 7 deletions src/tools/tidy/src/extdeps.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}*/
}

0 comments on commit 4fd8e85

Please sign in to comment.