Skip to content

Commit 2623a0e

Browse files
committed
Merge remote-tracking branch 'upstream/main' into proc_macro_group
2 parents cceaa65 + b64df9d commit 2623a0e

File tree

322 files changed

+14018
-7585
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

322 files changed

+14018
-7585
lines changed

compiler/rustc/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ wasi = "=0.14.2"
2828

2929

3030
[dependencies.tikv-jemalloc-sys]
31-
version = "0.6.0"
31+
version = "0.6.1"
3232
optional = true
33-
features = ['unprefixed_malloc_on_supported_platforms']
33+
features = ['override_allocator_on_supported_platforms']
3434

3535
[features]
3636
# tidy-alphabetical-start
3737
check_only = ['rustc_driver_impl/check_only']
3838
jemalloc = ['dep:tikv-jemalloc-sys']
3939
llvm = ['rustc_driver_impl/llvm']
4040
llvm_enzyme = ['rustc_driver_impl/llvm_enzyme']
41+
llvm_offload = ['rustc_driver_impl/llvm_offload']
4142
max_level_info = ['rustc_driver_impl/max_level_info']
4243
rustc_randomized_layouts = ['rustc_driver_impl/rustc_randomized_layouts']
4344
# tidy-alphabetical-end

compiler/rustc/src/main.rs

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,25 @@
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 must use a consistent
11-
// allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
12-
// wouldn't provide that.
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.
1313
//
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.
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.
1920
//
2021
// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
2122
// provides an impl of that trait, which is called `Jemalloc`) is that we
2223
// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
2324
// It's unclear how much performance is lost because of this.
2425
//
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.
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]`.
3029
//
3130
// NOTE: if you are reading this comment because you want to set a custom `global_allocator` for
3231
// benchmarking, consider using the benchmarks in the `rustc-perf` collector suite instead:
@@ -36,43 +35,9 @@
3635
// to compare their performance, see
3736
// https://github.com/rust-lang/rust/commit/b90cfc887c31c3e7a9e6d462e2464db1fe506175#diff-43914724af6e464c1da2171e4a9b6c7e607d5bc1203fa95c0ab85be4122605ef
3837
// for an example of how to do so.
38+
#[cfg(feature = "jemalloc")]
39+
use tikv_jemalloc_sys as _;
3940

4041
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-
7742
rustc_driver::main()
7843
}

compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ tracing = "0.1"
4747
# tidy-alphabetical-start
4848
check_only = ["rustc_llvm/check_only"]
4949
llvm_enzyme = []
50+
llvm_offload = []
5051
# tidy-alphabetical-end
5152

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,13 @@ pub(crate) unsafe fn llvm_optimize(
771771
llvm_plugins.len(),
772772
)
773773
};
774+
775+
if cgcx.target_is_like_gpu && config.offload.contains(&config::Offload::Enable) {
776+
unsafe {
777+
llvm::LLVMRustBundleImages(module.module_llvm.llmod(), module.module_llvm.tm.raw());
778+
}
779+
}
780+
774781
result.into_result().unwrap_or_else(|()| llvm_err(dcx, LlvmError::RunLlvmPasses))
775782
}
776783

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1718,6 +1718,37 @@ unsafe extern "C" {
17181718
) -> &'a Value;
17191719
}
17201720

1721+
#[cfg(feature = "llvm_offload")]
1722+
pub(crate) use self::Offload::*;
1723+
1724+
#[cfg(feature = "llvm_offload")]
1725+
mod Offload {
1726+
use super::*;
1727+
unsafe extern "C" {
1728+
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1729+
pub(crate) fn LLVMRustBundleImages<'a>(M: &'a Module, TM: &'a TargetMachine) -> bool;
1730+
pub(crate) fn LLVMRustOffloadMapper<'a>(OldFn: &'a Value, NewFn: &'a Value);
1731+
}
1732+
}
1733+
1734+
#[cfg(not(feature = "llvm_offload"))]
1735+
pub(crate) use self::Offload_fallback::*;
1736+
1737+
#[cfg(not(feature = "llvm_offload"))]
1738+
mod Offload_fallback {
1739+
use super::*;
1740+
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1741+
/// Marked as unsafe to match the real offload wrapper which is unsafe due to FFI.
1742+
#[allow(unused_unsafe)]
1743+
pub(crate) unsafe fn LLVMRustBundleImages<'a>(_M: &'a Module, _TM: &'a TargetMachine) -> bool {
1744+
unimplemented!("This rustc version was not built with LLVM Offload support!");
1745+
}
1746+
#[allow(unused_unsafe)]
1747+
pub(crate) unsafe fn LLVMRustOffloadMapper<'a>(_OldFn: &'a Value, _NewFn: &'a Value) {
1748+
unimplemented!("This rustc version was not built with LLVM Offload support!");
1749+
}
1750+
}
1751+
17211752
// FFI bindings for `DIBuilder` functions in the LLVM-C API.
17221753
// Try to keep these in the same order as in `llvm/include/llvm-c/DebugInfo.h`.
17231754
//
@@ -2025,7 +2056,6 @@ unsafe extern "C" {
20252056
) -> &Attribute;
20262057

20272058
// Operations on functions
2028-
pub(crate) fn LLVMRustOffloadMapper<'a>(Fn: &'a Value, Fn: &'a Value);
20292059
pub(crate) fn LLVMRustGetOrInsertFunction<'a>(
20302060
M: &'a Module,
20312061
Name: *const c_char,

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2424
) {
2525
match *rvalue {
2626
mir::Rvalue::Use(ref operand) => {
27+
if let mir::Operand::Constant(const_op) = operand {
28+
let val = self.eval_mir_constant(&const_op);
29+
if val.all_bytes_uninit(self.cx.tcx()) {
30+
return;
31+
}
32+
}
2733
let cg_operand = self.codegen_operand(bx, operand);
2834
// Crucially, we do *not* use `OperandValue::Ref` for types with
2935
// `BackendRepr::Scalar | BackendRepr::ScalarPair`. This ensures we match the MIR

compiler/rustc_const_eval/src/interpret/operand.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ impl<'tcx, Prov: Provenance> OpTy<'tcx, Prov> {
522522
pub(super) fn op(&self) -> &Operand<Prov> {
523523
&self.op
524524
}
525+
526+
pub fn is_immediate_uninit(&self) -> bool {
527+
matches!(self.op, Operand::Immediate(Immediate::Uninit))
528+
}
525529
}
526530

527531
impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for OpTy<'tcx, Prov> {

compiler/rustc_driver_impl/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ctrlc = "3.4.4"
7575
check_only = ['rustc_interface/check_only']
7676
llvm = ['rustc_interface/llvm']
7777
llvm_enzyme = ['rustc_interface/llvm_enzyme']
78+
llvm_offload = ['rustc_interface/llvm_offload']
7879
max_level_info = ['rustc_log/max_level_info']
7980
rustc_randomized_layouts = [
8081
'rustc_index/rustc_randomized_layouts',

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ impl CodeSuggestion {
348348
hi_opt: Option<&Loc>,
349349
) -> usize {
350350
let mut line_count = 0;
351-
// Convert CharPos to Usize, as CharPose is character offset
351+
// Convert `CharPos` to `usize`, as `CharPos` is character offset
352352
// Extract low index and high index
353353
let (lo, hi_opt) = (lo.col.to_usize(), hi_opt.map(|hi| hi.col.to_usize()));
354354
if let Some(line) = line_opt {

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,5 @@ rustc_abi = { path = "../rustc_abi" }
5959
check_only = ['rustc_codegen_llvm?/check_only']
6060
llvm = ['dep:rustc_codegen_llvm']
6161
llvm_enzyme = ['rustc_builtin_macros/llvm_enzyme', 'rustc_codegen_llvm/llvm_enzyme']
62+
llvm_offload = ['rustc_codegen_llvm/llvm_offload']
6263
# tidy-alphabetical-end

0 commit comments

Comments
 (0)