Skip to content

Commit 48fc410

Browse files
committed
Sync from rust 53a741fc4b8cf2d8e7b1b2336ed8edf889db84f4
2 parents 7f0b2bb + c15f855 commit 48fc410

File tree

4 files changed

+48
-68
lines changed

4 files changed

+48
-68
lines changed

src/allocator.rs

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33

44
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
55
use rustc_ast::expand::allocator::{
6-
ALLOCATOR_METHODS, AllocatorKind, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE,
7-
alloc_error_handler_name, default_fn_name, global_fn_name,
6+
AllocatorMethod, AllocatorTy, NO_ALLOC_SHIM_IS_UNSTABLE, default_fn_name, global_fn_name,
87
};
9-
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
8+
use rustc_codegen_ssa::base::{allocator_kind_for_codegen, allocator_shim_contents};
109
use rustc_session::config::OomStrategy;
1110
use rustc_symbol_mangling::mangle_internal_symbol;
1211

@@ -15,75 +14,57 @@ use crate::prelude::*;
1514
/// Returns whether an allocator shim was created
1615
pub(crate) fn codegen(tcx: TyCtxt<'_>, module: &mut dyn Module) -> bool {
1716
let Some(kind) = allocator_kind_for_codegen(tcx) else { return false };
18-
codegen_inner(
19-
tcx,
20-
module,
21-
kind,
22-
tcx.alloc_error_handler_kind(()).unwrap(),
23-
tcx.sess.opts.unstable_opts.oom,
24-
);
17+
let methods = allocator_shim_contents(tcx, kind);
18+
codegen_inner(tcx, module, &methods, tcx.sess.opts.unstable_opts.oom);
2519
true
2620
}
2721

2822
fn codegen_inner(
2923
tcx: TyCtxt<'_>,
3024
module: &mut dyn Module,
31-
kind: AllocatorKind,
32-
alloc_error_handler_kind: AllocatorKind,
25+
methods: &[AllocatorMethod],
3326
oom_strategy: OomStrategy,
3427
) {
3528
let usize_ty = module.target_config().pointer_type();
3629

37-
if kind == AllocatorKind::Default {
38-
for method in ALLOCATOR_METHODS {
39-
let mut arg_tys = Vec::with_capacity(method.inputs.len());
40-
for input in method.inputs.iter() {
41-
match input.ty {
42-
AllocatorTy::Layout => {
43-
arg_tys.push(usize_ty); // size
44-
arg_tys.push(usize_ty); // align
45-
}
46-
AllocatorTy::Ptr => arg_tys.push(usize_ty),
47-
AllocatorTy::Usize => arg_tys.push(usize_ty),
30+
for method in methods {
31+
let mut arg_tys = Vec::with_capacity(method.inputs.len());
32+
for input in method.inputs.iter() {
33+
match input.ty {
34+
AllocatorTy::Layout => {
35+
arg_tys.push(usize_ty); // size
36+
arg_tys.push(usize_ty); // align
37+
}
38+
AllocatorTy::Ptr => arg_tys.push(usize_ty),
39+
AllocatorTy::Usize => arg_tys.push(usize_ty),
4840

49-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
41+
AllocatorTy::Never | AllocatorTy::ResultPtr | AllocatorTy::Unit => {
42+
panic!("invalid allocator arg")
5043
}
5144
}
52-
let output = match method.output {
53-
AllocatorTy::ResultPtr => Some(usize_ty),
54-
AllocatorTy::Unit => None,
45+
}
46+
let output = match method.output {
47+
AllocatorTy::ResultPtr => Some(usize_ty),
48+
AllocatorTy::Never | AllocatorTy::Unit => None,
5549

56-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
57-
panic!("invalid allocator output")
58-
}
59-
};
50+
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
51+
panic!("invalid allocator output")
52+
}
53+
};
6054

61-
let sig = Signature {
62-
call_conv: module.target_config().default_call_conv,
63-
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
64-
returns: output.into_iter().map(AbiParam::new).collect(),
65-
};
66-
crate::common::create_wrapper_function(
67-
module,
68-
sig,
69-
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
70-
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
71-
);
72-
}
55+
let sig = Signature {
56+
call_conv: module.target_config().default_call_conv,
57+
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
58+
returns: output.into_iter().map(AbiParam::new).collect(),
59+
};
60+
crate::common::create_wrapper_function(
61+
module,
62+
sig,
63+
&mangle_internal_symbol(tcx, &global_fn_name(method.name)),
64+
&mangle_internal_symbol(tcx, &default_fn_name(method.name)),
65+
);
7366
}
7467

75-
let sig = Signature {
76-
call_conv: module.target_config().default_call_conv,
77-
params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)],
78-
returns: vec![],
79-
};
80-
crate::common::create_wrapper_function(
81-
module,
82-
sig,
83-
&mangle_internal_symbol(tcx, "__rust_alloc_error_handler"),
84-
&mangle_internal_symbol(tcx, alloc_error_handler_name(alloc_error_handler_kind)),
85-
);
86-
8768
{
8869
let sig = Signature {
8970
call_conv: module.target_config().default_call_conv,

src/base.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,11 +624,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
624624
let val = codegen_operand(fx, operand);
625625
lval.write_cvalue(fx, val);
626626
}
627-
Rvalue::CopyForDeref(place) => {
628-
let cplace = codegen_place(fx, place);
629-
let val = cplace.to_cvalue(fx);
630-
lval.write_cvalue(fx, val)
631-
}
632627
Rvalue::Ref(_, _, place) | Rvalue::RawPtr(_, place) => {
633628
let place = codegen_place(fx, place);
634629
let ref_ = place.place_ref(fx, lval.layout());
@@ -952,11 +947,11 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
952947
let operand = codegen_operand(fx, operand);
953948
lval.write_cvalue_transmute(fx, operand);
954949
}
950+
Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
955951
}
956952
}
957953
StatementKind::StorageLive(_)
958954
| StatementKind::StorageDead(_)
959-
| StatementKind::Deinit(_)
960955
| StatementKind::ConstEvalCounter
961956
| StatementKind::Nop
962957
| StatementKind::FakeRead(..)

src/constant.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use cranelift_module::*;
66
use rustc_const_eval::interpret::CTFE_ALLOC_SALT;
77
use rustc_data_structures::fx::FxHashSet;
88
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
9-
use rustc_middle::mir::interpret::{AllocId, GlobalAlloc, Scalar, read_target_uint};
9+
use rustc_middle::mir::interpret::{
10+
AllocId, GlobalAlloc, PointerArithmetic, Scalar, read_target_uint,
11+
};
1012
use rustc_middle::ty::{ExistentialTraitRef, ScalarInt};
1113

1214
use crate::prelude::*;
@@ -139,8 +141,11 @@ pub(crate) fn codegen_const_value<'tcx>(
139141
let base_addr = match fx.tcx.global_alloc(alloc_id) {
140142
GlobalAlloc::Memory(alloc) => {
141143
if alloc.inner().len() == 0 {
142-
assert_eq!(offset, Size::ZERO);
143-
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes() as i64)
144+
let val = alloc.inner().align.bytes().wrapping_add(offset.bytes());
145+
fx.bcx.ins().iconst(
146+
fx.pointer_type,
147+
fx.tcx.truncate_to_target_usize(val) as i64,
148+
)
144149
} else {
145150
let data_id = data_id_for_alloc_id(
146151
&mut fx.constants_cx,
@@ -329,7 +334,7 @@ fn data_id_for_static(
329334
let mut data = DataDescription::new();
330335
data.set_align(align);
331336
let data_gv = module.declare_data_in_data(data_id, &mut data);
332-
data.define(std::iter::repeat(0).take(pointer_ty(tcx).bytes() as usize).collect());
337+
data.define(std::iter::repeat_n(0, pointer_ty(tcx).bytes() as usize).collect());
333338
data.write_data_addr(0, data_gv, 0);
334339
match module.define_data(ref_data_id, &data) {
335340
// Every time the static is referenced there will be another definition of this global,
@@ -612,7 +617,6 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
612617
StatementKind::Assign(_)
613618
| StatementKind::FakeRead(_)
614619
| StatementKind::SetDiscriminant { .. }
615-
| StatementKind::Deinit(_)
616620
| StatementKind::StorageLive(_)
617621
| StatementKind::StorageDead(_)
618622
| StatementKind::Retag(_, _)

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ extern crate rustc_fs_util;
2727
extern crate rustc_hir;
2828
extern crate rustc_incremental;
2929
extern crate rustc_index;
30+
extern crate rustc_log;
3031
extern crate rustc_metadata;
3132
extern crate rustc_session;
3233
extern crate rustc_span;
3334
extern crate rustc_symbol_mangling;
3435
extern crate rustc_target;
35-
#[macro_use]
36-
extern crate tracing;
3736

3837
// This prevents duplicating functions and statics that are already part of the host rustc process.
3938
#[allow(unused_extern_crates)]
@@ -47,6 +46,7 @@ use cranelift_codegen::isa::TargetIsa;
4746
use cranelift_codegen::settings::{self, Configurable};
4847
use rustc_codegen_ssa::traits::CodegenBackend;
4948
use rustc_codegen_ssa::{CodegenResults, TargetConfig};
49+
use rustc_log::tracing::info;
5050
use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
5151
use rustc_session::Session;
5252
use rustc_session::config::OutputFilenames;

0 commit comments

Comments
 (0)