]> git.lizzy.rs Git - rust.git/blob - compiler/rustc/src/main.rs
Rollup merge of #97739 - a2aaron:let_underscore, r=estebank
[rust.git] / compiler / rustc / src / main.rs
1 // A note about jemalloc: rustc uses jemalloc when built for CI and
2 // distribution. The obvious way to do this is with the `#[global_allocator]`
3 // mechanism. However, for complicated reasons (see
4 // https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
5 // details) that mechanism doesn't work here. Also, we must use a consistent
6 // allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
7 // wouldn't provide that.
8 //
9 // Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a
10 // way such that jemalloc's implementation of `malloc`, `free`, etc., override
11 // the libc allocator's implementation. This means that Rust's `System`
12 // allocator, which calls `libc::malloc()` et al., is actually calling into
13 // jemalloc.
14 //
15 // A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
16 // provides an impl of that trait, which is called `Jemalloc`) is that we
17 // cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
18 // It's unclear how much performance is lost because of this.
19 //
20 // As for the symbol overrides in `main` below: we're pulling in a static copy
21 // of jemalloc. We need to actually reference its symbols for it to get linked.
22 // The two crates we link to here, `std` and `rustc_driver`, are both dynamic
23 // libraries. So we must reference jemalloc symbols one way or another, because
24 // this file is the only object code in the rustc executable.
25
26 fn main() {
27     // See the comment at the top of this file for an explanation of this.
28     #[cfg(feature = "jemalloc-sys")]
29     {
30         use std::os::raw::{c_int, c_void};
31
32         #[used]
33         static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
34         #[used]
35         static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
36             jemalloc_sys::posix_memalign;
37         #[used]
38         static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
39         #[used]
40         static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
41         #[used]
42         static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
43         #[used]
44         static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
45
46         // On OSX, jemalloc doesn't directly override malloc/free, but instead
47         // registers itself with the allocator's zone APIs in a ctor. However,
48         // the linker doesn't seem to consider ctors as "used" when statically
49         // linking, so we need to explicitly depend on the function.
50         #[cfg(target_os = "macos")]
51         {
52             extern "C" {
53                 fn _rjem_je_zone_register();
54             }
55
56             #[used]
57             static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
58         }
59     }
60
61     rustc_driver::set_sigpipe_handler();
62     rustc_driver::main()
63 }