rustc_main/main.rs
1// We need this feature as it changes `dylib` linking behavior and allows us to link to `rustc_driver`.
2#![feature(rustc_private)]
3// Several crates are depended upon but unused so that they are present in the sysroot
4#![expect(unused_crate_dependencies)]
5
6use std::process::ExitCode;
7
8// A note about jemalloc: rustc uses jemalloc when built for CI and
9// distribution. The obvious way to do this is with the `#[global_allocator]`
10// mechanism. However, that would not affect LLVM's C / C++ allocations and we also want
11// to use a single allocator in the process to reduce memory usage.
12//
13// Instead, we use a lower-level mechanism, namely the
14// `"override_allocator_on_supported_platforms"` Cargo feature of jemalloc-sys.
15//
16// This makes jemalloc-sys override the libc/system allocator's implementation
17// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
18// calls `libc::malloc()` et al., is actually calling into jemalloc.
19//
20// This override happens for the entire process, ensuring that there's no mixup
21// of C allocators across dylibs / binaries, notably the rustc <-> llvm boundary.
22//
23// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
24// provides an impl of that trait, which is called `Jemalloc`) is that we
25// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
26// It's unclear how much performance is lost because of this.
27//
28// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
29// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
30// https://github.com/rust-lang/rustc-perf/blob/master/collector/README.md#profiling
31//
32// NOTE: if you are reading this comment because you want to replace jemalloc with another allocator
33// to compare their performance, see
34// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
35// for an example of how to do so.
36rustc_driver::override_c_allocator_in_binary!();
37
38fn main() -> ExitCode {
39 rustc_driver::main()
40}