rustc_data_structures/tagged_ptr.rs
1//! This module implements tagged pointers. In order to utilize the pointer
2//! packing, you must have a tag type implementing the [`Tag`] trait.
3//!
4//! We assert that the tag and the reference type is compatible at compile
5//! time.
6
7use std::fmt;
8use std::hash::{Hash, Hasher};
9use std::marker::PhantomData;
10use std::num::NonZero;
11use std::ops::Deref;
12use std::ptr::NonNull;
13
14use crate::aligned::Aligned;
15use crate::stable_hasher::{HashStable, StableHasher};
16
17/// This describes tags that the [`TaggedRef`] struct can hold.
18///
19/// # Safety
20///
21/// - The [`BITS`] constant must be correct.
22/// - No more than [`BITS`] least-significant bits may be set in the returned usize.
23/// - [`Eq`] and [`Hash`] must be implementable with the returned `usize` from `into_usize`.
24///
25/// [`BITS`]: Tag::BITS
26pub unsafe trait Tag: Copy {
27 /// Number of least-significant bits in the return value of [`into_usize`]
28 /// which may be non-zero. In other words this is the bit width of the
29 /// value.
30 ///
31 /// [`into_usize`]: Tag::into_usize
32 const BITS: u32;
33
34 /// Turns this tag into an integer.
35 ///
36 /// The inverse of this function is [`from_usize`].
37 ///
38 /// This function guarantees that only the least-significant [`Self::BITS`]
39 /// bits can be non-zero.
40 ///
41 /// [`from_usize`]: Tag::from_usize
42 /// [`Self::BITS`]: Tag::BITS
43 fn into_usize(self) -> usize;
44
45 /// Re-creates the tag from the integer returned by [`into_usize`].
46 ///
47 /// # Safety
48 ///
49 /// The passed `tag` must be returned from [`into_usize`].
50 ///
51 /// [`into_usize`]: Tag::into_usize
52 unsafe fn from_usize(tag: usize) -> Self;
53}
54
55/// Returns the number of bits available for use for tags in a pointer to `T`
56/// (this is based on `T`'s alignment).
57pub const fn bits_for<T: ?Sized + Aligned>() -> u32 {
58 let alignment = crate::aligned::align_of::<T>();
59 let alignment = alignment.as_nonzero_usize();
60 alignment.trailing_zeros()
61}
62
63/// Returns the correct [`Tag::BITS`] constant for a set of tag values.
64pub const fn bits_for_tags(mut tags: &[usize]) -> u32 {
65 let mut bits = 0;
66
67 while let &[tag, ref rest @ ..] = tags {
68 tags = rest;
69
70 // bits required to represent `tag`,
71 // position of the most significant 1
72 let b = usize::BITS - tag.leading_zeros();
73 if b > bits {
74 bits = b;
75 }
76 }
77
78 bits
79}
80
81/// A covariant [`Copy`] tagged borrow. This is essentially `{ pointer: &'a P, tag: T }` packed
82/// in a single reference.
83pub struct TaggedRef<'a, Pointee: Aligned + ?Sized, T: Tag> {
84 /// This is semantically a pair of `pointer: &'a P` and `tag: T` fields,
85 /// however we pack them in a single pointer, to save space.
86 ///
87 /// We pack the tag into the **most**-significant bits of the pointer to
88 /// ease retrieval of the value. A left shift is a multiplication and
89 /// those are embeddable in instruction encoding, for example:
90 ///
91 /// ```asm
92 /// // (<https://godbolt.org/z/jqcYPWEr3>)
93 /// example::shift_read3:
94 /// mov eax, dword ptr [8*rdi]
95 /// ret
96 ///
97 /// example::mask_read3:
98 /// and rdi, -8
99 /// mov eax, dword ptr [rdi]
100 /// ret
101 /// ```
102 ///
103 /// This is ASM outputted by rustc for reads of values behind tagged
104 /// pointers for different approaches of tagging:
105 /// - `shift_read3` uses `<< 3` (the tag is in the most-significant bits)
106 /// - `mask_read3` uses `& !0b111` (the tag is in the least-significant bits)
107 ///
108 /// The shift approach thus produces less instructions and is likely faster
109 /// (see <https://godbolt.org/z/Y913sMdWb>).
110 ///
111 /// Encoding diagram:
112 /// ```text
113 /// [ packed.addr ]
114 /// [ tag ] [ pointer.addr >> T::BITS ] <-- usize::BITS - T::BITS bits
115 /// ^
116 /// |
117 /// T::BITS bits
118 /// ```
119 ///
120 /// The tag can be retrieved by `packed.addr() >> T::BITS` and the pointer
121 /// can be retrieved by `packed.map_addr(|addr| addr << T::BITS)`.
122 packed: NonNull<Pointee>,
123 tag_pointer_ghost: PhantomData<(&'a Pointee, T)>,
124}
125
126impl<'a, P, T> TaggedRef<'a, P, T>
127where
128 P: Aligned + ?Sized,
129 T: Tag,
130{
131 /// Tags `pointer` with `tag`.
132 ///
133 /// [`TaggedRef`]: crate::tagged_ptr::TaggedRef
134 #[inline]
135 pub fn new(pointer: &'a P, tag: T) -> Self {
136 Self { packed: Self::pack(NonNull::from(pointer), tag), tag_pointer_ghost: PhantomData }
137 }
138
139 /// Retrieves the pointer.
140 #[inline]
141 pub fn pointer(self) -> &'a P {
142 // SAFETY: pointer_raw returns the original pointer
143 unsafe { self.pointer_raw().as_ref() }
144 }
145
146 /// Retrieves the tag.
147 #[inline]
148 pub fn tag(&self) -> T {
149 // Unpack the tag, according to the `self.packed` encoding scheme
150 let tag = self.packed.addr().get() >> Self::TAG_BIT_SHIFT;
151
152 // Safety:
153 // The shift retrieves the original value from `T::into_usize`,
154 // satisfying `T::from_usize`'s preconditions.
155 unsafe { T::from_usize(tag) }
156 }
157
158 /// Sets the tag to a new value.
159 #[inline]
160 pub fn set_tag(&mut self, tag: T) {
161 self.packed = Self::pack(self.pointer_raw(), tag);
162 }
163
164 const TAG_BIT_SHIFT: u32 = usize::BITS - T::BITS;
165 const ASSERTION: () = { assert!(T::BITS <= bits_for::<P>()) };
166
167 /// Pack pointer `ptr` with a `tag`, according to `self.packed` encoding scheme.
168 #[inline]
169 fn pack(ptr: NonNull<P>, tag: T) -> NonNull<P> {
170 // Trigger assert!
171 let () = Self::ASSERTION;
172
173 let packed_tag = tag.into_usize() << Self::TAG_BIT_SHIFT;
174
175 ptr.map_addr(|addr| {
176 // Safety:
177 // - The pointer is `NonNull` => it's address is `NonZero<usize>`
178 // - `P::BITS` least significant bits are always zero (`Pointer` contract)
179 // - `T::BITS <= P::BITS` (from `Self::ASSERTION`)
180 //
181 // Thus `addr >> T::BITS` is guaranteed to be non-zero.
182 //
183 // `{non_zero} | packed_tag` can't make the value zero.
184
185 let packed = (addr.get() >> T::BITS) | packed_tag;
186 unsafe { NonZero::new_unchecked(packed) }
187 })
188 }
189
190 /// Retrieves the original raw pointer from `self.packed`.
191 #[inline]
192 pub(super) fn pointer_raw(&self) -> NonNull<P> {
193 self.packed.map_addr(|addr| unsafe { NonZero::new_unchecked(addr.get() << T::BITS) })
194 }
195}
196
197impl<P, T> Copy for TaggedRef<'_, P, T>
198where
199 P: Aligned + ?Sized,
200 T: Tag,
201{
202}
203
204impl<P, T> Clone for TaggedRef<'_, P, T>
205where
206 P: Aligned + ?Sized,
207 T: Tag,
208{
209 #[inline]
210 fn clone(&self) -> Self {
211 *self
212 }
213}
214
215impl<P, T> Deref for TaggedRef<'_, P, T>
216where
217 P: Aligned + ?Sized,
218 T: Tag,
219{
220 type Target = P;
221
222 #[inline]
223 fn deref(&self) -> &Self::Target {
224 self.pointer()
225 }
226}
227
228impl<P, T> fmt::Debug for TaggedRef<'_, P, T>
229where
230 P: Aligned + fmt::Debug + ?Sized,
231 T: Tag + fmt::Debug,
232{
233 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
234 f.debug_struct("TaggedRef")
235 .field("pointer", &self.pointer())
236 .field("tag", &self.tag())
237 .finish()
238 }
239}
240
241impl<P, T> PartialEq for TaggedRef<'_, P, T>
242where
243 P: Aligned + ?Sized,
244 T: Tag,
245{
246 #[inline]
247 #[allow(ambiguous_wide_pointer_comparisons)]
248 fn eq(&self, other: &Self) -> bool {
249 self.packed == other.packed
250 }
251}
252
253impl<P, T: Tag> Eq for TaggedRef<'_, P, T> {}
254
255impl<P, T: Tag> Hash for TaggedRef<'_, P, T> {
256 #[inline]
257 fn hash<H: Hasher>(&self, state: &mut H) {
258 self.packed.hash(state);
259 }
260}
261
262impl<'a, P, T, Hcx> HashStable<Hcx> for TaggedRef<'a, P, T>
263where
264 P: HashStable<Hcx> + Aligned + ?Sized,
265 T: Tag + HashStable<Hcx>,
266{
267 fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
268 self.pointer().hash_stable(hcx, hasher);
269 self.tag().hash_stable(hcx, hasher);
270 }
271}
272
273// Safety:
274// `TaggedRef<P, T, ..>` is semantically just `{ ptr: P, tag: T }`, as such
275// it's ok to implement `Sync` as long as `P: Sync, T: Sync`
276unsafe impl<P, T> Sync for TaggedRef<'_, P, T>
277where
278 P: Sync + Aligned + ?Sized,
279 T: Sync + Tag,
280{
281}
282
283// Safety:
284// `TaggedRef<P, T, ..>` is semantically just `{ ptr: P, tag: T }`, as such
285// it's ok to implement `Send` as long as `P: Send, T: Send`
286unsafe impl<P, T> Send for TaggedRef<'_, P, T>
287where
288 P: Sync + Aligned + ?Sized,
289 T: Send + Tag,
290{
291}
292
293#[cfg(test)]
294mod tests;