Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5f3b867

Browse files
committed
Sync from rust 65ea825
2 parents fbb276e + 46b55ae commit 5f3b867

File tree

6 files changed

+45
-62
lines changed

6 files changed

+45
-62
lines changed

src/abi/pass_mode.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
100100
}
101101
_ => unreachable!("{:?}", self.layout.abi),
102102
},
103-
PassMode::Cast(ref cast, pad_i32) => {
103+
PassMode::Cast { ref cast, pad_i32 } => {
104104
assert!(!pad_i32, "padding support not yet implemented");
105105
cast_target_to_abi_params(cast)
106106
}
107-
PassMode::Indirect { attrs, extra_attrs: None, on_stack } => {
107+
PassMode::Indirect { attrs, meta_attrs: None, on_stack } => {
108108
if on_stack {
109109
// Abi requires aligning struct size to pointer size
110110
let size = self.layout.size.align_to(tcx.data_layout.pointer_align.abi);
@@ -117,11 +117,11 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
117117
smallvec![apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)]
118118
}
119119
}
120-
PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack } => {
120+
PassMode::Indirect { attrs, meta_attrs: Some(meta_attrs), on_stack } => {
121121
assert!(!on_stack);
122122
smallvec![
123123
apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs),
124-
apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), extra_attrs),
124+
apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), meta_attrs),
125125
]
126126
}
127127
}
@@ -148,14 +148,14 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
148148
}
149149
_ => unreachable!("{:?}", self.layout.abi),
150150
},
151-
PassMode::Cast(ref cast, _) => {
151+
PassMode::Cast { ref cast, .. } => {
152152
(None, cast_target_to_abi_params(cast).into_iter().collect())
153153
}
154-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack } => {
154+
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack } => {
155155
assert!(!on_stack);
156156
(Some(AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn)), vec![])
157157
}
158-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
158+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
159159
unreachable!("unsized return value")
160160
}
161161
}
@@ -229,7 +229,7 @@ pub(super) fn adjust_arg_for_abi<'tcx>(
229229
let (a, b) = arg.load_scalar_pair(fx);
230230
smallvec![a, b]
231231
}
232-
PassMode::Cast(ref cast, _) => to_casted_value(fx, arg, cast),
232+
PassMode::Cast { ref cast, .. } => to_casted_value(fx, arg, cast),
233233
PassMode::Indirect { .. } => {
234234
if is_owned {
235235
match arg.force_stack(fx) {
@@ -287,14 +287,14 @@ pub(super) fn cvalue_for_param<'tcx>(
287287
assert_eq!(block_params.len(), 2, "{:?}", block_params);
288288
Some(CValue::by_val_pair(block_params[0], block_params[1], arg_abi.layout))
289289
}
290-
PassMode::Cast(ref cast, _) => {
290+
PassMode::Cast { ref cast, .. } => {
291291
Some(from_casted_value(fx, &block_params, arg_abi.layout, cast))
292292
}
293-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
293+
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
294294
assert_eq!(block_params.len(), 1, "{:?}", block_params);
295295
Some(CValue::by_ref(Pointer::new(block_params[0]), arg_abi.layout))
296296
}
297-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
297+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
298298
assert_eq!(block_params.len(), 2, "{:?}", block_params);
299299
Some(CValue::by_ref_unsized(
300300
Pointer::new(block_params[0]),

src/abi/returning.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub(super) fn codegen_return_param<'tcx>(
1313
block_params_iter: &mut impl Iterator<Item = Value>,
1414
) -> CPlace<'tcx> {
1515
let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode {
16-
PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(..) => {
16+
PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast { .. } => {
1717
let is_ssa =
1818
ssa_analyzed[RETURN_PLACE].is_ssa(fx, fx.fn_abi.as_ref().unwrap().ret.layout.ty);
1919
(
@@ -26,15 +26,15 @@ pub(super) fn codegen_return_param<'tcx>(
2626
smallvec![],
2727
)
2828
}
29-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
29+
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
3030
let ret_param = block_params_iter.next().unwrap();
3131
assert_eq!(fx.bcx.func.dfg.value_type(ret_param), fx.pointer_type);
3232
(
3333
CPlace::for_ptr(Pointer::new(ret_param), fx.fn_abi.as_ref().unwrap().ret.layout),
3434
smallvec![ret_param],
3535
)
3636
}
37-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
37+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
3838
unreachable!("unsized return value")
3939
}
4040
};
@@ -62,7 +62,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
6262
) {
6363
let (ret_temp_place, return_ptr) = match ret_arg_abi.mode {
6464
PassMode::Ignore => (None, None),
65-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
65+
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
6666
if let Some(ret_ptr) = ret_place.try_to_ptr() {
6767
// This is an optimization to prevent unnecessary copies of the return value when
6868
// the return place is already a memory place as opposed to a register.
@@ -73,10 +73,10 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
7373
(Some(place), Some(place.to_ptr().get_addr(fx)))
7474
}
7575
}
76-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
76+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
7777
unreachable!("unsized return value")
7878
}
79-
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(..) => (None, None),
79+
PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast { .. } => (None, None),
8080
};
8181

8282
let call_inst = f(fx, return_ptr);
@@ -93,21 +93,21 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
9393
ret_place
9494
.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout));
9595
}
96-
PassMode::Cast(ref cast, _) => {
96+
PassMode::Cast { ref cast, .. } => {
9797
let results =
9898
fx.bcx.inst_results(call_inst).iter().copied().collect::<SmallVec<[Value; 2]>>();
9999
let result =
100100
super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast);
101101
ret_place.write_cvalue(fx, result);
102102
}
103-
PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
103+
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
104104
if let Some(ret_temp_place) = ret_temp_place {
105105
// If ret_temp_place is None, it is not necessary to copy the return value.
106106
let ret_temp_value = ret_temp_place.to_cvalue(fx);
107107
ret_place.write_cvalue(fx, ret_temp_value);
108108
}
109109
}
110-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
110+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
111111
unreachable!("unsized return value")
112112
}
113113
}
@@ -116,10 +116,10 @@ pub(super) fn codegen_with_call_return_arg<'tcx>(
116116
/// Codegen a return instruction with the right return value(s) if any.
117117
pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
118118
match fx.fn_abi.as_ref().unwrap().ret.mode {
119-
PassMode::Ignore | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {
119+
PassMode::Ignore | PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: _ } => {
120120
fx.bcx.ins().return_(&[]);
121121
}
122-
PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => {
122+
PassMode::Indirect { attrs: _, meta_attrs: Some(_), on_stack: _ } => {
123123
unreachable!("unsized return value")
124124
}
125125
PassMode::Direct(_) => {
@@ -132,7 +132,7 @@ pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) {
132132
let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx);
133133
fx.bcx.ins().return_(&[ret_val_a, ret_val_b]);
134134
}
135-
PassMode::Cast(ref cast, _) => {
135+
PassMode::Cast { ref cast, .. } => {
136136
let place = fx.get_local_place(RETURN_PLACE);
137137
let ret_val = place.to_cvalue(fx);
138138
let ret_vals = super::pass_mode::to_casted_value(fx, ret_val, cast);

src/base.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,10 @@ pub(crate) fn verify_func(
249249
}
250250

251251
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
252-
if !crate::constant::check_constants(fx) {
252+
if let Err(err) =
253+
fx.mir.post_mono_checks(fx.tcx, ty::ParamEnv::reveal_all(), |c| Ok(fx.monomorphize(c)))
254+
{
255+
err.emit_err(fx.tcx);
253256
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
254257
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
255258
// compilation should have been aborted

src/constant.rs

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
use cranelift_module::*;
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
6-
use rustc_middle::mir::interpret::{
7-
read_target_uint, AllocId, ConstAllocation, ConstValue, ErrorHandled, GlobalAlloc, Scalar,
8-
};
6+
use rustc_middle::mir::interpret::{read_target_uint, AllocId, ConstValue, GlobalAlloc, Scalar};
97

108
use crate::prelude::*;
119

@@ -32,16 +30,6 @@ impl ConstantCx {
3230
}
3331
}
3432

35-
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool {
36-
let mut all_constants_ok = true;
37-
for constant in &fx.mir.required_consts {
38-
if eval_mir_constant(fx, constant).is_none() {
39-
all_constants_ok = false;
40-
}
41-
}
42-
all_constants_ok
43-
}
44-
4533
pub(crate) fn codegen_static(tcx: TyCtxt<'_>, module: &mut dyn Module, def_id: DefId) {
4634
let mut constants_cx = ConstantCx::new();
4735
constants_cx.todo.push(TodoItem::Static(def_id));
@@ -75,30 +63,20 @@ pub(crate) fn codegen_tls_ref<'tcx>(
7563
pub(crate) fn eval_mir_constant<'tcx>(
7664
fx: &FunctionCx<'_, '_, 'tcx>,
7765
constant: &Constant<'tcx>,
78-
) -> Option<(ConstValue<'tcx>, Ty<'tcx>)> {
66+
) -> (ConstValue<'tcx>, Ty<'tcx>) {
7967
let cv = fx.monomorphize(constant.literal);
68+
// This cannot fail because we checked all required_consts in advance.
8069
let val = cv
8170
.eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span))
82-
.map_err(|err| match err {
83-
ErrorHandled::Reported(_) => {
84-
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
85-
}
86-
ErrorHandled::TooGeneric => {
87-
span_bug!(constant.span, "codegen encountered polymorphic constant: {:?}", err);
88-
}
89-
})
90-
.ok();
91-
val.map(|val| (val, cv.ty()))
71+
.expect("erroneous constant not captured by required_consts");
72+
(val, cv.ty())
9273
}
9374

9475
pub(crate) fn codegen_constant_operand<'tcx>(
9576
fx: &mut FunctionCx<'_, '_, 'tcx>,
9677
constant: &Constant<'tcx>,
9778
) -> CValue<'tcx> {
98-
let (const_val, ty) = eval_mir_constant(fx, constant).unwrap_or_else(|| {
99-
span_bug!(constant.span, "erroneous constant not captured by required_consts")
100-
});
101-
79+
let (const_val, ty) = eval_mir_constant(fx, constant);
10280
codegen_const_value(fx, const_val, ty)
10381
}
10482

@@ -115,7 +93,7 @@ pub(crate) fn codegen_const_value<'tcx>(
11593
}
11694

11795
match const_val {
118-
ConstValue::ZeroSized => unreachable!(), // we already handles ZST above
96+
ConstValue::ZeroSized => unreachable!(), // we already handled ZST above
11997
ConstValue::Scalar(x) => match x {
12098
Scalar::Int(int) => {
12199
if fx.clif_type(layout.ty).is_some() {
@@ -199,13 +177,14 @@ pub(crate) fn codegen_const_value<'tcx>(
199177
CValue::by_val(val, layout)
200178
}
201179
},
202-
ConstValue::ByRef { alloc, offset } => CValue::by_ref(
203-
pointer_for_allocation(fx, alloc)
180+
ConstValue::Indirect { alloc_id, offset } => CValue::by_ref(
181+
pointer_for_allocation(fx, alloc_id)
204182
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
205183
layout,
206184
),
207185
ConstValue::Slice { data, start, end } => {
208-
let ptr = pointer_for_allocation(fx, data)
186+
let alloc_id = fx.tcx.reserve_and_set_memory_alloc(data);
187+
let ptr = pointer_for_allocation(fx, alloc_id)
209188
.offset_i64(fx, i64::try_from(start).unwrap())
210189
.get_addr(fx);
211190
let len = fx
@@ -219,9 +198,9 @@ pub(crate) fn codegen_const_value<'tcx>(
219198

220199
fn pointer_for_allocation<'tcx>(
221200
fx: &mut FunctionCx<'_, '_, 'tcx>,
222-
alloc: ConstAllocation<'tcx>,
201+
alloc_id: AllocId,
223202
) -> crate::pointer::Pointer {
224-
let alloc_id = fx.tcx.create_memory_alloc(alloc);
203+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
225204
let data_id = data_id_for_alloc_id(
226205
&mut fx.constants_cx,
227206
&mut *fx.module,
@@ -352,6 +331,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
352331
unreachable!()
353332
}
354333
};
334+
// FIXME: should we have a cache so we don't do this multiple times for the same `ConstAllocation`?
355335
let data_id = *cx.anon_allocs.entry(alloc_id).or_insert_with(|| {
356336
module.declare_anonymous_data(alloc.inner().mutability.is_mut(), false).unwrap()
357337
});
@@ -456,7 +436,7 @@ pub(crate) fn mir_operand_get_const_val<'tcx>(
456436
operand: &Operand<'tcx>,
457437
) -> Option<ConstValue<'tcx>> {
458438
match operand {
459-
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).unwrap().0),
439+
Operand::Constant(const_) => Some(eval_mir_constant(fx, const_).0),
460440
// FIXME(rust-lang/rust#85105): Casts like `IMM8 as u32` result in the const being stored
461441
// inside a temporary before being passed to the intrinsic requiring the const argument.
462442
// This code tries to find a single constant defining definition of the referenced local.

src/inline_asm.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ pub(crate) fn codegen_inline_asm<'tcx>(
242242
}
243243
}
244244
InlineAsmOperand::Const { ref value } => {
245-
let (const_value, ty) = crate::constant::eval_mir_constant(fx, value)
246-
.unwrap_or_else(|| span_bug!(span, "asm const cannot be resolved"));
245+
let (const_value, ty) = crate::constant::eval_mir_constant(fx, value);
247246
let value = rustc_codegen_ssa::common::asm_const_to_str(
248247
fx.tcx,
249248
span,

src/intrinsics/simd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
176176
};
177177

178178
let idx_bytes = match idx_const {
179-
ConstValue::ByRef { alloc, offset } => {
179+
ConstValue::Indirect { alloc_id, offset } => {
180+
let alloc = fx.tcx.global_alloc(alloc_id).unwrap_memory();
180181
let size = Size::from_bytes(
181182
4 * ret_lane_count, /* size_of([u32; ret_lane_count]) */
182183
);

0 commit comments

Comments
 (0)