-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Make TypeId const comparable #142789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Make TypeId const comparable #142789
Changes from all commits
f7ab598
52b9ece
b16524d
552ba12
bc9d010
8e4dec6
7404aa3
871e4eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ use std::borrow::{Borrow, Cow}; | |
use std::fmt; | ||
use std::hash::Hash; | ||
|
||
use rustc_abi::{Align, Size}; | ||
use rustc_abi::{Align, FieldIdx, Size}; | ||
use rustc_ast::Mutability; | ||
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, IndexEntry}; | ||
use rustc_hir::def_id::{DefId, LocalDefId}; | ||
|
@@ -403,6 +403,22 @@ impl<'tcx> interpret::Machine<'tcx> for CompileTimeMachine<'tcx> { | |
let cmp = ecx.guaranteed_cmp(a, b)?; | ||
ecx.write_scalar(Scalar::from_u8(cmp), dest)?; | ||
} | ||
sym::type_id_eq => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think const-eval and Miri can use the same implementation. |
||
let a = ecx.project_field(&args[0], FieldIdx::ZERO)?; | ||
let b = ecx.project_field(&args[1], FieldIdx::ZERO)?; | ||
|
||
let a = ecx.project_index(&a, 0)?; | ||
let a = ecx.deref_pointer(&a)?; | ||
let (a, offset_a, _) = ecx.ptr_get_alloc_id(a.ptr(), 0)?; | ||
let GlobalAlloc::Type { ty: a } = ecx.tcx.global_alloc(a) else { bug!() }; | ||
|
||
let b = ecx.project_index(&b, 0)?; | ||
let b = ecx.deref_pointer(&b)?; | ||
let (b, offset_b, _) = ecx.ptr_get_alloc_id(b.ptr(), 0)?; | ||
let GlobalAlloc::Type { ty: b } = ecx.tcx.global_alloc(b) else { bug!() }; | ||
|
||
ecx.write_scalar(Scalar::from_bool(a == b && offset_a == offset_b), dest)?; | ||
} | ||
sym::const_allocate => { | ||
let size = ecx.read_scalar(&args[0])?.to_target_usize(ecx)?; | ||
let align = ecx.read_scalar(&args[1])?.to_target_usize(ecx)?; | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,8 @@ use std::assert_matches::assert_matches; | |||||||
|
||||||||
use rustc_abi::Size; | ||||||||
use rustc_apfloat::ieee::{Double, Half, Quad, Single}; | ||||||||
use rustc_ast::Mutability; | ||||||||
use rustc_middle::mir::interpret::{AllocId, AllocInit, alloc_range}; | ||||||||
use rustc_middle::mir::{self, BinOp, ConstValue, NonDivergingIntrinsic}; | ||||||||
use rustc_middle::ty::layout::TyAndLayout; | ||||||||
use rustc_middle::ty::{Ty, TyCtxt}; | ||||||||
|
@@ -29,6 +31,37 @@ pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAll | |||||||
tcx.mk_const_alloc(alloc) | ||||||||
} | ||||||||
|
||||||||
pub(crate) fn alloc_type_id<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> AllocId { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think ideally this would return an Or, if we want to optimize for performance, this function should take as argument an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have a local commit for making it write to a dest directly. Which is a change I want to do for all the intrinsics that are currently generating a ConstValue and writing that from within the interpreter I can land that first on master if you prefer. Or just move the commit for typeid into this PR |
||||||||
let size = Size::from_bytes(16); | ||||||||
let align = tcx.data_layout.pointer_align; | ||||||||
let mut alloc = Allocation::new(size, *align, AllocInit::Uninit, ()); | ||||||||
Comment on lines
+35
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any chance we can assert that this matches the size and align of the real |
||||||||
let ptr_size = tcx.data_layout.pointer_size; | ||||||||
let type_id_hash = tcx.type_id_hash(ty).as_u128(); | ||||||||
alloc | ||||||||
.write_scalar( | ||||||||
&tcx, | ||||||||
alloc_range(Size::ZERO, Size::from_bytes(16)), | ||||||||
Scalar::from_u128(type_id_hash), | ||||||||
) | ||||||||
.unwrap(); | ||||||||
|
||||||||
// Give the first pointer-size bytes provenance that knows about the type id | ||||||||
|
||||||||
let alloc_id = tcx.reserve_and_set_type_id_alloc(ty); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's two |
||||||||
let offset = alloc | ||||||||
.read_scalar(&tcx, alloc_range(Size::ZERO, ptr_size), false) | ||||||||
.unwrap() | ||||||||
.to_target_usize(&tcx) | ||||||||
.unwrap(); | ||||||||
let ptr = Pointer::new(alloc_id.into(), Size::from_bytes(offset)); | ||||||||
let val = Scalar::from_pointer(ptr, &tcx); | ||||||||
alloc.write_scalar(&tcx, alloc_range(Size::ZERO, ptr_size), val).unwrap(); | ||||||||
|
||||||||
alloc.mutability = Mutability::Not; | ||||||||
|
||||||||
tcx.reserve_and_set_memory_alloc(tcx.mk_const_alloc(alloc)) | ||||||||
} | ||||||||
|
||||||||
impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { | ||||||||
/// Returns `true` if emulation happened. | ||||||||
/// Here we implement the intrinsics that are common to all Miri instances; individual machines can add their own | ||||||||
|
@@ -63,7 +96,8 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> { | |||||||
sym::type_id => { | ||||||||
let tp_ty = instance.args.type_at(0); | ||||||||
ensure_monomorphic_enough(tcx, tp_ty)?; | ||||||||
let val = ConstValue::from_u128(tcx.type_id_hash(tp_ty).as_u128()); | ||||||||
let alloc_id = alloc_type_id(tcx, tp_ty); | ||||||||
let val = ConstValue::Indirect { alloc_id, offset: Size::ZERO }; | ||||||||
let val = self.const_val_to_op(val, dest.layout.ty, Some(dest.layout))?; | ||||||||
self.copy_op(&val, dest)?; | ||||||||
} | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have to emit an int-to-ptr cast here? I think we should avoid that if at all possible...