Skip to content

Commit c151bb4

Browse files
committed
Sync from rust 8df945c4717ffaf923b57bf30c473df6fc98bc85
2 parents bd2f72f + d7c3c45 commit c151bb4

File tree

3 files changed

+49
-10
lines changed

3 files changed

+49
-10
lines changed

src/constant.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,20 +193,21 @@ pub(crate) fn codegen_const_value<'tcx>(
193193
place.to_cvalue(fx)
194194
}
195195
}
196-
Scalar::Ptr(ptr) => {
197-
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
196+
Scalar::Ptr(ptr, _size) => {
197+
let (alloc_id, offset) = ptr.into_parts(); // we know the `offset` is relative
198+
let alloc_kind = fx.tcx.get_global_alloc(alloc_id);
198199
let base_addr = match alloc_kind {
199200
Some(GlobalAlloc::Memory(alloc)) => {
200201
let data_id = data_id_for_alloc_id(
201202
&mut fx.constants_cx,
202203
fx.module,
203-
ptr.alloc_id,
204+
alloc_id,
204205
alloc.mutability,
205206
);
206207
let local_data_id =
207208
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
208209
if fx.clif_comments.enabled() {
209-
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
210+
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
210211
}
211212
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
212213
}
@@ -226,10 +227,10 @@ pub(crate) fn codegen_const_value<'tcx>(
226227
}
227228
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
228229
}
229-
None => bug!("missing allocation {:?}", ptr.alloc_id),
230+
None => bug!("missing allocation {:?}", alloc_id),
230231
};
231-
let val = if ptr.offset.bytes() != 0 {
232-
fx.bcx.ins().iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap())
232+
let val = if offset.bytes() != 0 {
233+
fx.bcx.ins().iadd_imm(base_addr, i64::try_from(offset.bytes()).unwrap())
233234
} else {
234235
base_addr
235236
};
@@ -406,7 +407,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
406407
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
407408
data_ctx.define(bytes.into_boxed_slice());
408409

409-
for &(offset, (_tag, reloc)) in alloc.relocations().iter() {
410+
for &(offset, alloc_id) in alloc.relocations().iter() {
410411
let addend = {
411412
let endianness = tcx.data_layout.endian;
412413
let offset = offset.bytes() as usize;
@@ -417,7 +418,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
417418
read_target_uint(endianness, bytes).unwrap()
418419
};
419420

420-
let reloc_target_alloc = tcx.get_global_alloc(reloc).unwrap();
421+
let reloc_target_alloc = tcx.get_global_alloc(alloc_id).unwrap();
421422
let data_id = match reloc_target_alloc {
422423
GlobalAlloc::Function(instance) => {
423424
assert_eq!(addend, 0);
@@ -427,7 +428,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
427428
continue;
428429
}
429430
GlobalAlloc::Memory(target_alloc) => {
430-
data_id_for_alloc_id(cx, module, reloc, target_alloc.mutability)
431+
data_id_for_alloc_id(cx, module, alloc_id, target_alloc.mutability)
431432
}
432433
GlobalAlloc::Static(def_id) => {
433434
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)

src/intrinsics/mod.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,40 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
11151115
);
11161116
ret.write_cvalue(fx, CValue::by_val(res, ret.layout()));
11171117
};
1118+
1119+
raw_eq, <T>(v lhs_ref, v rhs_ref) {
1120+
fn type_by_size(size: Size) -> Option<Type> {
1121+
Type::int(size.bits().try_into().ok()?)
1122+
}
1123+
1124+
let size = fx.layout_of(T).layout.size;
1125+
let is_eq_value =
1126+
if size == Size::ZERO {
1127+
// No bytes means they're trivially equal
1128+
fx.bcx.ins().iconst(types::I8, 1)
1129+
} else if let Some(clty) = type_by_size(size) {
1130+
// Can't use `trusted` for these loads; they could be unaligned.
1131+
let mut flags = MemFlags::new();
1132+
flags.set_notrap();
1133+
let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0);
1134+
let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0);
1135+
let eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val);
1136+
fx.bcx.ins().bint(types::I8, eq)
1137+
} else {
1138+
// Just call `memcmp` (like slices do in core) when the
1139+
// size is too large or it's not a power-of-two.
1140+
let ptr_ty = pointer_ty(fx.tcx);
1141+
let signed_bytes = i64::try_from(size.bytes()).unwrap();
1142+
let bytes_val = fx.bcx.ins().iconst(ptr_ty, signed_bytes);
1143+
let params = vec![AbiParam::new(ptr_ty); 3];
1144+
let returns = vec![AbiParam::new(types::I32)];
1145+
let args = &[lhs_ref, rhs_ref, bytes_val];
1146+
let cmp = fx.lib_call("memcmp", params, returns, args)[0];
1147+
let eq = fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0);
1148+
fx.bcx.ins().bint(types::I8, eq)
1149+
};
1150+
ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
1151+
};
11181152
}
11191153

11201154
if let Some((_, dest)) = destination {

src/value_and_place.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,10 @@ impl<'tcx> CPlace<'tcx> {
453453
ptr.store(fx, data, MemFlags::trusted());
454454
ptr.load(fx, dst_ty, MemFlags::trusted())
455455
}
456+
457+
// `CValue`s should never contain SSA-only types, so if you ended
458+
// up here having seen an error like `B1 -> I8`, then before
459+
// calling `write_cvalue` you need to add a `bint` instruction.
456460
_ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty, dst_ty),
457461
};
458462
//fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index()));

0 commit comments

Comments
 (0)