Skip to content

Commit db4dbc8

Browse files
authored
Rollup merge of rust-lang#124251 - scottmcm:unop-ptr-metadata, r=oli-obk
Add an intrinsic for `ptr::metadata` The follow-up to rust-lang#123840, so we can remove `PtrComponents` and `PtrRepr` from libcore entirely (well, after a bootstrap update). As discussed in <https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/.60ptr_metadata.60.20in.20MIR/near/435637808>, this introduces `UnOp::PtrMetadata` taking a raw pointer and returning the associated metadata value. By no longer going through a `union`, this should also help future PRs better optimize pointer operations. r? ``@oli-obk``
2 parents 9ddcc59 + 676fec7 commit db4dbc8

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

src/base.rs

+25-13
Original file line numberDiff line numberDiff line change
@@ -616,22 +616,34 @@ fn codegen_stmt<'tcx>(
616616
Rvalue::UnaryOp(un_op, ref operand) => {
617617
let operand = codegen_operand(fx, operand);
618618
let layout = operand.layout();
619-
let val = operand.load_scalar(fx);
620619
let res = match un_op {
621-
UnOp::Not => match layout.ty.kind() {
622-
ty::Bool => {
623-
let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
624-
CValue::by_val(res, layout)
620+
UnOp::Not => {
621+
let val = operand.load_scalar(fx);
622+
match layout.ty.kind() {
623+
ty::Bool => {
624+
let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0);
625+
CValue::by_val(res, layout)
626+
}
627+
ty::Uint(_) | ty::Int(_) => {
628+
CValue::by_val(fx.bcx.ins().bnot(val), layout)
629+
}
630+
_ => unreachable!("un op Not for {:?}", layout.ty),
625631
}
626-
ty::Uint(_) | ty::Int(_) => {
627-
CValue::by_val(fx.bcx.ins().bnot(val), layout)
632+
}
633+
UnOp::Neg => {
634+
let val = operand.load_scalar(fx);
635+
match layout.ty.kind() {
636+
ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
637+
ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout),
638+
_ => unreachable!("un op Neg for {:?}", layout.ty),
628639
}
629-
_ => unreachable!("un op Not for {:?}", layout.ty),
630-
},
631-
UnOp::Neg => match layout.ty.kind() {
632-
ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
633-
ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout),
634-
_ => unreachable!("un op Neg for {:?}", layout.ty),
640+
}
641+
UnOp::PtrMetadata => match layout.abi {
642+
Abi::Scalar(_) => CValue::zst(dest_layout),
643+
Abi::ScalarPair(_, _) => {
644+
CValue::by_val(operand.load_scalar_pair(fx).1, dest_layout)
645+
}
646+
_ => bug!("Unexpected `PtrToMetadata` operand: {operand:?}"),
635647
},
636648
};
637649
lval.write_cvalue(fx, res);

src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ pub(crate) fn codegen_const_value<'tcx>(
100100
assert!(layout.is_sized(), "unsized const value");
101101

102102
if layout.is_zst() {
103-
return CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout);
103+
return CValue::zst(layout);
104104
}
105105

106106
match const_val {

src/value_and_place.rs

+8
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ impl<'tcx> CValue<'tcx> {
9595
CValue(CValueInner::ByValPair(value, extra), layout)
9696
}
9797

98+
/// Create an instance of a ZST
99+
///
100+
/// The is represented by a dangling pointer of suitable alignment.
101+
pub(crate) fn zst(layout: TyAndLayout<'tcx>) -> CValue<'tcx> {
102+
assert!(layout.is_zst());
103+
CValue::by_ref(crate::Pointer::dangling(layout.align.pref), layout)
104+
}
105+
98106
pub(crate) fn layout(&self) -> TyAndLayout<'tcx> {
99107
self.1
100108
}

0 commit comments

Comments
 (0)