Skip to content

Commit 961b503

Browse files
Auto merge of #148896 - Kobzol:revert-146627, r=<try>
Revert "Rollup merge of #146627 - madsmtm:jemalloc-simplify, r=jdonszelmann"
2 parents 5dbf406 + ccd8795 commit 961b503

File tree

9 files changed

+167
-38
lines changed

9 files changed

+167
-38
lines changed

Cargo.lock

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,6 @@ dependencies = [
600600
"serde_json",
601601
"tempfile",
602602
"termize",
603-
"tikv-jemalloc-sys",
604603
"toml 0.9.7",
605604
"ui_test",
606605
"walkdir",
@@ -4807,7 +4806,6 @@ dependencies = [
48074806
"stringdex",
48084807
"tempfile",
48094808
"threadpool",
4810-
"tikv-jemalloc-sys",
48114809
"tracing",
48124810
"tracing-subscriber",
48134811
"tracing-tree",
@@ -5539,9 +5537,9 @@ version = "0.1.0"
55395537

55405538
[[package]]
55415539
name = "tikv-jemalloc-sys"
5542-
version = "0.6.1+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
5540+
version = "0.6.0+5.3.0-1-ge13ca993e8ccb9ba9847cc330696e02839f328f7"
55435541
source = "registry+https://github.com/rust-lang/crates.io-index"
5544-
checksum = "cd8aa5b2ab86a2cefa406d889139c162cbb230092f7d1d7cbc1716405d852a3b"
5542+
checksum = "cd3c60906412afa9c2b5b5a48ca6a5abe5736aec9eb48ad05037a677e52e4e2d"
55455543
dependencies = [
55465544
"cc",
55475545
"libc",

compiler/rustc/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ rustc_public_bridge = { path = "../rustc_public_bridge" }
2121
# tidy-alphabetical-end
2222

2323
[dependencies.tikv-jemalloc-sys]
24-
version = "0.6.1"
24+
version = "0.6.0"
2525
optional = true
26-
features = ['override_allocator_on_supported_platforms']
26+
features = ['unprefixed_malloc_on_supported_platforms']
2727

2828
[features]
2929
# tidy-alphabetical-start

compiler/rustc/src/main.rs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,26 @@
77
// distribution. The obvious way to do this is with the `#[global_allocator]`
88
// mechanism. However, for complicated reasons (see
99
// https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
10-
// details) that mechanism doesn't work here. Also, we'd like to use a
11-
// consistent allocator across the rustc <-> llvm boundary, and
12-
// `#[global_allocator]` wouldn't provide that.
10+
// details) that mechanism doesn't work here. Also, we must use a consistent
11+
// allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
12+
// wouldn't provide that.
1313
//
14-
// Instead, we use a lower-level mechanism, namely the
15-
// `"override_allocator_on_supported_platforms"` Cargo feature of jemalloc-sys.
16-
//
17-
// This makes jemalloc-sys override the libc/system allocator's implementation
18-
// of `malloc`, `free`, etc.. This means that Rust's `System` allocator, which
19-
// calls `libc::malloc()` et al., is actually calling into jemalloc.
14+
// Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a
15+
// way such that jemalloc's implementation of `malloc`, `free`, etc., override
16+
// the libc allocator's implementation. This means that Rust's `System`
17+
// allocator, which calls `libc::malloc()` et al., is actually calling into
18+
// jemalloc.
2019
//
2120
// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
2221
// provides an impl of that trait, which is called `Jemalloc`) is that we
2322
// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
2423
// It's unclear how much performance is lost because of this.
2524
//
26-
// NOTE: Even though Cargo passes `--extern` with `tikv_jemalloc_sys`, we still need to `use` the
27-
// crate for the compiler to see the `#[used]`, see https://github.com/rust-lang/rust/issues/64402.
28-
// This is similarly required if we used a crate with `#[global_allocator]`.
25+
// As for the symbol overrides in `main` below: we're pulling in a static copy
26+
// of jemalloc. We need to actually reference its symbols for it to get linked.
27+
// The two crates we link to here, `std` and `rustc_driver`, are both dynamic
28+
// libraries. So we must reference jemalloc symbols one way or another, because
29+
// this file is the only object code in the rustc executable.
2930
//
3031
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
3132
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
@@ -35,9 +36,43 @@
3536
// to compare their performance, see
3637
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
3738
// for an example of how to do so.
38-
#[cfg(feature = "jemalloc")]
39-
use tikv_jemalloc_sys as _;
4039

4140
fn main() {
41+
// See the comment at the top of this file for an explanation of this.
42+
#[cfg(feature = "jemalloc")]
43+
{
44+
use std::os::raw::{c_int, c_void};
45+
46+
use tikv_jemalloc_sys as jemalloc_sys;
47+
48+
#[used]
49+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
50+
#[used]
51+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
52+
jemalloc_sys::posix_memalign;
53+
#[used]
54+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
55+
#[used]
56+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
57+
#[used]
58+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
59+
#[used]
60+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
61+
62+
// On OSX, jemalloc doesn't directly override malloc/free, but instead
63+
// registers itself with the allocator's zone APIs in a ctor. However,
64+
// the linker doesn't seem to consider ctors as "used" when statically
65+
// linking, so we need to explicitly depend on the function.
66+
#[cfg(target_os = "macos")]
67+
{
68+
unsafe extern "C" {
69+
fn _rjem_je_zone_register();
70+
}
71+
72+
#[used]
73+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
74+
}
75+
}
76+
4277
rustc_driver::main()
4378
}

src/librustdoc/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ smallvec = "1.8.1"
2424
stringdex = "=0.0.2"
2525
tempfile = "3"
2626
threadpool = "1.8.1"
27-
tikv-jemalloc-sys = { version = "0.6.1", optional = true, features = ['override_allocator_on_supported_platforms'] }
2827
tracing = "0.1"
2928
tracing-tree = "0.3.0"
3029
unicode-segmentation = "1.9"
@@ -43,7 +42,7 @@ minifier = { version = "0.3.2", default-features = false }
4342
expect-test = "1.4.0"
4443

4544
[features]
46-
jemalloc = ["dep:tikv-jemalloc-sys"]
45+
jemalloc = []
4746

4847
[package.metadata.rust-analyzer]
4948
rustc_private = true

src/librustdoc/lib.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ extern crate rustc_target;
6161
extern crate rustc_trait_selection;
6262
extern crate test;
6363

64+
// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
65+
// about jemalloc.
66+
#[cfg(feature = "jemalloc")]
67+
extern crate tikv_jemalloc_sys as jemalloc_sys;
68+
6469
use std::env::{self, VarError};
6570
use std::io::{self, IsTerminal};
6671
use std::path::Path;
@@ -72,10 +77,6 @@ use rustc_interface::interface;
7277
use rustc_middle::ty::TyCtxt;
7378
use rustc_session::config::{ErrorOutputType, RustcOptGroup, make_crate_type_option};
7479
use rustc_session::{EarlyDiagCtxt, getopts};
75-
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
76-
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
77-
#[cfg(feature = "jemalloc")]
78-
use tikv_jemalloc_sys as _;
7980
use tracing::info;
8081

8182
use crate::clean::utils::DOC_RUST_LANG_ORG_VERSION;
@@ -123,6 +124,37 @@ mod visit_ast;
123124
mod visit_lib;
124125

125126
pub fn main() {
127+
// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
128+
// about jemalloc.
129+
#[cfg(feature = "jemalloc")]
130+
{
131+
use std::os::raw::{c_int, c_void};
132+
133+
#[used]
134+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
135+
#[used]
136+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
137+
jemalloc_sys::posix_memalign;
138+
#[used]
139+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
140+
#[used]
141+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
142+
#[used]
143+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
144+
#[used]
145+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
146+
147+
#[cfg(target_os = "macos")]
148+
{
149+
unsafe extern "C" {
150+
fn _rjem_je_zone_register();
151+
}
152+
153+
#[used]
154+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
155+
}
156+
}
157+
126158
let mut early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
127159

128160
rustc_driver::install_ice_hook(

src/tools/clippy/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ tempfile = { version = "3.20", optional = true }
3131
termize = "0.2"
3232
color-print = "0.3.4"
3333
anstream = "0.6.18"
34-
tikv-jemalloc-sys = { version = "0.6.1", optional = true, features = ['override_allocator_on_supported_platforms'] }
3534

3635
[dev-dependencies]
3736
cargo_metadata = "0.18.1"
@@ -57,7 +56,7 @@ rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }
5756
[features]
5857
integration = ["dep:tempfile"]
5958
internal = ["dep:clippy_lints_internal", "dep:tempfile"]
60-
jemalloc = ["dep:tikv-jemalloc-sys"]
59+
jemalloc = []
6160

6261
[package.metadata.rust-analyzer]
6362
# This package uses #[feature(rustc_private)]

src/tools/clippy/src/driver.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ extern crate rustc_interface;
1313
extern crate rustc_session;
1414
extern crate rustc_span;
1515

16-
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
17-
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
16+
// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
17+
// about jemalloc.
1818
#[cfg(feature = "jemalloc")]
19-
use tikv_jemalloc_sys as _;
19+
extern crate tikv_jemalloc_sys as jemalloc_sys;
2020

2121
use clippy_utils::sym;
2222
use declare_clippy_lint::LintListBuilder;
@@ -189,6 +189,36 @@ const BUG_REPORT_URL: &str = "https://github.com/rust-lang/rust-clippy/issues/ne
189189

190190
#[expect(clippy::too_many_lines)]
191191
pub fn main() {
192+
// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
193+
// about jemalloc.
194+
#[cfg(feature = "jemalloc")]
195+
{
196+
use std::os::raw::{c_int, c_void};
197+
198+
#[used]
199+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
200+
#[used]
201+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = jemalloc_sys::posix_memalign;
202+
#[used]
203+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
204+
#[used]
205+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
206+
#[used]
207+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
208+
#[used]
209+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
210+
211+
#[cfg(target_os = "macos")]
212+
{
213+
unsafe extern "C" {
214+
fn _rjem_je_zone_register();
215+
}
216+
217+
#[used]
218+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
219+
}
220+
}
221+
192222
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
193223

194224
rustc_driver::init_rustc_env_logger(&early_dcx);

src/tools/miri/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ serde_json = { version = "1.0", optional = true }
3333
# But only for some targets, it fails for others. Rustc configures this in its CI, but we can't
3434
# easily use that since we support of-tree builds.
3535
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies.tikv-jemalloc-sys]
36-
version = "0.6.1"
37-
features = ['override_allocator_on_supported_platforms']
36+
version = "0.6.0"
37+
features = ['unprefixed_malloc_on_supported_platforms']
3838

3939
[target.'cfg(unix)'.dependencies]
4040
libc = "0.2"

src/tools/miri/src/bin/miri.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ extern crate rustc_middle;
2020
extern crate rustc_session;
2121
extern crate rustc_span;
2222

23-
/// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
24-
/// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
25-
#[cfg(any(target_os = "linux", target_os = "macos"))]
26-
use tikv_jemalloc_sys as _;
27-
2823
mod log;
2924

3025
use std::env;
@@ -397,7 +392,48 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
397392
Ok(from..to)
398393
}
399394

395+
#[cfg(any(target_os = "linux", target_os = "macos"))]
396+
fn jemalloc_magic() {
397+
// These magic runes are copied from
398+
// <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
399+
// See there for further comments.
400+
use std::os::raw::{c_int, c_void};
401+
402+
use tikv_jemalloc_sys as jemalloc_sys;
403+
404+
#[used]
405+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
406+
#[used]
407+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
408+
jemalloc_sys::posix_memalign;
409+
#[used]
410+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
411+
#[used]
412+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
413+
#[used]
414+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
415+
#[used]
416+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
417+
418+
// On OSX, jemalloc doesn't directly override malloc/free, but instead
419+
// registers itself with the allocator's zone APIs in a ctor. However,
420+
// the linker doesn't seem to consider ctors as "used" when statically
421+
// linking, so we need to explicitly depend on the function.
422+
#[cfg(target_os = "macos")]
423+
{
424+
unsafe extern "C" {
425+
fn _rjem_je_zone_register();
426+
}
427+
428+
#[used]
429+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
430+
}
431+
}
432+
400433
fn main() {
434+
#[cfg(any(target_os = "linux", target_os = "macos"))]
435+
jemalloc_magic();
436+
401437
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
402438

403439
// Snapshot a copy of the environment before `rustc` starts messing with it.

0 commit comments

Comments
 (0)