Skip to content

Commit e3ac2c6

Browse files
committed
Implement missing ABI structures in StableMIR
1 parent 6db96de commit e3ac2c6

File tree

6 files changed

+273
-25
lines changed

6 files changed

+273
-25
lines changed

compiler/rustc_smir/src/rustc_smir/convert/abi.rs

+59-7
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ use crate::rustc_smir::{Stable, Tables};
66
use rustc_middle::ty;
77
use rustc_target::abi::call::Conv;
88
use stable_mir::abi::{
9-
ArgAbi, CallConvention, FieldsShape, FnAbi, Layout, LayoutShape, PassMode, TagEncoding,
10-
TyAndLayout, ValueAbi, VariantsShape,
9+
AddressSpace, ArgAbi, CallConvention, FieldsShape, FnAbi, IntegerLength, Layout, LayoutShape,
10+
PassMode, Primitive, Scalar, TagEncoding, TyAndLayout, ValueAbi, VariantsShape, WrappingRange,
1111
};
12-
use stable_mir::ty::{Align, IndexedVal, Size, VariantIdx};
13-
use stable_mir::{opaque, Opaque};
12+
use stable_mir::opaque;
13+
use stable_mir::target::MachineSize as Size;
14+
use stable_mir::ty::{Align, IndexedVal, VariantIdx};
1415

1516
impl<'tcx> Stable<'tcx> for rustc_target::abi::VariantIdx {
1617
type T = VariantIdx;
@@ -220,7 +221,7 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Size {
220221
type T = Size;
221222

222223
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
223-
self.bytes_usize()
224+
Size::from_bits(self.bits_usize())
224225
}
225226
}
226227

@@ -233,9 +234,60 @@ impl<'tcx> Stable<'tcx> for rustc_abi::Align {
233234
}
234235

235236
impl<'tcx> Stable<'tcx> for rustc_abi::Scalar {
236-
type T = Opaque;
237+
type T = Scalar;
238+
239+
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
240+
match self {
241+
rustc_abi::Scalar::Initialized { value, valid_range } => Scalar::Initialized {
242+
value: value.stable(tables),
243+
valid_range: valid_range.stable(tables),
244+
},
245+
rustc_abi::Scalar::Union { value } => Scalar::Union { value: value.stable(tables) },
246+
}
247+
}
248+
}
249+
250+
impl<'tcx> Stable<'tcx> for rustc_abi::Primitive {
251+
type T = Primitive;
252+
253+
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
254+
match self {
255+
rustc_abi::Primitive::Int(length, signed) => {
256+
Primitive::Int { length: length.stable(tables), signed: *signed }
257+
}
258+
rustc_abi::Primitive::F32 => Primitive::F32,
259+
rustc_abi::Primitive::F64 => Primitive::F64,
260+
rustc_abi::Primitive::Pointer(space) => Primitive::Pointer(space.stable(tables)),
261+
}
262+
}
263+
}
264+
265+
impl<'tcx> Stable<'tcx> for rustc_abi::AddressSpace {
266+
type T = AddressSpace;
267+
268+
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
269+
AddressSpace(self.0)
270+
}
271+
}
272+
273+
impl<'tcx> Stable<'tcx> for rustc_abi::Integer {
274+
type T = IntegerLength;
275+
276+
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
277+
match self {
278+
rustc_abi::Integer::I8 => IntegerLength::I8,
279+
rustc_abi::Integer::I16 => IntegerLength::I16,
280+
rustc_abi::Integer::I32 => IntegerLength::I32,
281+
rustc_abi::Integer::I64 => IntegerLength::I64,
282+
rustc_abi::Integer::I128 => IntegerLength::I128,
283+
}
284+
}
285+
}
286+
287+
impl<'tcx> Stable<'tcx> for rustc_abi::WrappingRange {
288+
type T = WrappingRange;
237289

238290
fn stable(&self, _tables: &mut Tables<'_>) -> Self::T {
239-
opaque(self)
291+
WrappingRange { start: self.start, end: self.end }
240292
}
241293
}

compiler/stable_mir/src/abi.rs

+155-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
use crate::compiler_interface::with;
2+
use crate::error;
23
use crate::mir::FieldIdx;
3-
use crate::ty::{Align, IndexedVal, Size, Ty, VariantIdx};
4+
use crate::target::{MachineInfo, MachineSize as Size};
5+
use crate::ty::{Align, IndexedVal, Ty, VariantIdx};
6+
use crate::Error;
47
use crate::Opaque;
8+
use std::fmt::{self, Debug};
59
use std::num::NonZeroUsize;
610
use std::ops::RangeInclusive;
711

@@ -100,7 +104,7 @@ impl LayoutShape {
100104

101105
/// Returns `true` if the type is sized and a 1-ZST (meaning it has size 0 and alignment 1).
102106
pub fn is_1zst(&self) -> bool {
103-
self.is_sized() && self.size == 0 && self.abi_align == 1
107+
self.is_sized() && self.size.bits() == 0 && self.abi_align == 1
104108
}
105109
}
106110

@@ -245,8 +249,155 @@ impl ValueAbi {
245249
}
246250
}
247251

248-
/// We currently do not support `Scalar`, and use opaque instead.
249-
type Scalar = Opaque;
252+
/// Information about one scalar component of a Rust type.
253+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
254+
pub enum Scalar {
255+
Initialized {
256+
/// The primitive type used to represent this value.
257+
value: Primitive,
258+
/// The range that represents valid values.
259+
/// The range must be valid for the `primitive` size.
260+
valid_range: WrappingRange,
261+
},
262+
Union {
263+
/// Unions never have niches, so there is no `valid_range`.
264+
/// Even for unions, we need to use the correct registers for the kind of
265+
/// values inside the union, so we keep the `Primitive` type around.
266+
/// It is also used to compute the size of the scalar.
267+
value: Primitive,
268+
},
269+
}
270+
271+
impl Scalar {
272+
pub fn has_niche(&self, target: &MachineInfo) -> bool {
273+
match self {
274+
Scalar::Initialized { value, valid_range } => {
275+
!valid_range.is_full(value.size(target)).unwrap()
276+
}
277+
Scalar::Union { .. } => false,
278+
}
279+
}
280+
}
281+
282+
/// Fundamental unit of memory access and layout.
283+
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
284+
pub enum Primitive {
285+
/// The `bool` is the signedness of the `Integer` type.
286+
///
287+
/// One would think we would not care about such details this low down,
288+
/// but some ABIs are described in terms of C types and ISAs where the
289+
/// integer arithmetic is done on {sign,zero}-extended registers, e.g.
290+
/// a negative integer passed by zero-extension will appear positive in
291+
/// the callee, and most operations on it will produce the wrong values.
292+
Int {
293+
length: IntegerLength,
294+
signed: bool,
295+
},
296+
F32,
297+
F64,
298+
Pointer(AddressSpace),
299+
}
300+
301+
impl Primitive {
302+
pub fn size(self, target: &MachineInfo) -> Size {
303+
match self {
304+
Primitive::Int { length, .. } => Size::from_bits(length.bits()),
305+
Primitive::F32 => Size::from_bits(32),
306+
Primitive::F64 => Size::from_bits(64),
307+
Primitive::Pointer(_) => target.pointer_width,
308+
}
309+
}
310+
}
311+
312+
/// Enum representing the existing integer lengths.
313+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
314+
pub enum IntegerLength {
315+
I8,
316+
I16,
317+
I32,
318+
I64,
319+
I128,
320+
}
321+
322+
impl IntegerLength {
323+
pub fn bits(self) -> usize {
324+
match self {
325+
IntegerLength::I8 => 8,
326+
IntegerLength::I16 => 16,
327+
IntegerLength::I32 => 32,
328+
IntegerLength::I64 => 64,
329+
IntegerLength::I128 => 128,
330+
}
331+
}
332+
}
333+
334+
/// An identifier that specifies the address space that some operation
335+
/// should operate on. Special address spaces have an effect on code generation,
336+
/// depending on the target and the address spaces it implements.
337+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
338+
pub struct AddressSpace(pub u32);
339+
340+
impl AddressSpace {
341+
/// The default address space, corresponding to data space.
342+
pub const DATA: Self = AddressSpace(0);
343+
}
344+
345+
/// Inclusive wrap-around range of valid values (bitwise representation), that is, if
346+
/// start > end, it represents `start..=MAX`, followed by `0..=end`.
347+
///
348+
/// That is, for an i8 primitive, a range of `254..=2` means following
349+
/// sequence:
350+
///
351+
/// 254 (-2), 255 (-1), 0, 1, 2
352+
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
353+
pub struct WrappingRange {
354+
pub start: u128,
355+
pub end: u128,
356+
}
357+
358+
impl WrappingRange {
359+
/// Returns `true` if `size` completely fills the range.
360+
#[inline]
361+
pub fn is_full(&self, size: Size) -> Result<bool, Error> {
362+
let Some(max_value) = size.unsigned_int_max() else {
363+
return Err(error!("Expected size <= 128 bits, but found {} instead", size.bits()));
364+
};
365+
if self.start <= max_value && self.end <= max_value {
366+
Ok(self.start == 0 && max_value == self.end)
367+
} else {
368+
Err(error!("Range `{self:?}` out of bounds for size `{}` bits.", size.bits()))
369+
}
370+
}
371+
372+
/// Returns `true` if `v` is contained in the range.
373+
#[inline(always)]
374+
pub fn contains(&self, v: u128) -> bool {
375+
if self.wraps_around() {
376+
self.start <= v || v <= self.end
377+
} else {
378+
self.start <= v && v <= self.end
379+
}
380+
}
381+
382+
/// Returns `true` if the range wraps around.
383+
/// I.e., the range represents the union of `self.start..=MAX` and `0..=self.end`.
384+
/// Returns `false` if this is a non-wrapping range, i.e.: `self.start..=self.end`.
385+
#[inline]
386+
pub fn wraps_around(&self) -> bool {
387+
self.start > self.end
388+
}
389+
}
390+
391+
impl Debug for WrappingRange {
392+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
393+
if self.start > self.end {
394+
write!(fmt, "(..={}) | ({}..)", self.end, self.start)?;
395+
} else {
396+
write!(fmt, "{}..={}", self.start, self.end)?;
397+
}
398+
Ok(())
399+
}
400+
}
250401

251402
/// General language calling conventions.
252403
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]

compiler/stable_mir/src/error.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
//! - [Error]: Generic error that represents the reason why a request that could not be fulfilled.
66
77
use std::fmt::{Debug, Display, Formatter};
8-
use std::{error, fmt, io};
8+
use std::{fmt, io};
99

1010
macro_rules! error {
1111
($fmt: literal $(,)?) => { Error(format!($fmt)) };
1212
($fmt: literal, $($arg:tt)*) => { Error(format!($fmt, $($arg)*)) };
13-
}
13+
}
14+
15+
pub(crate) use error;
1416

1517
/// An error type used to represent an error that has already been reported by the compiler.
1618
#[derive(Clone, Copy, PartialEq, Eq)]
@@ -72,8 +74,9 @@ where
7274
}
7375
}
7476

75-
impl error::Error for Error {}
76-
impl<T> error::Error for CompilerError<T> where T: Display + Debug {}
77+
impl std::error::Error for Error {}
78+
79+
impl<T> std::error::Error for CompilerError<T> where T: Display + Debug {}
7780

7881
impl From<io::Error> for Error {
7982
fn from(value: io::Error) -> Self {

compiler/stable_mir/src/target.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,29 @@ pub enum Endian {
3030
}
3131

3232
/// Represent the size of a component.
33-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
33+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
3434
pub struct MachineSize {
3535
num_bits: usize,
3636
}
3737

3838
impl MachineSize {
39+
#[inline(always)]
3940
pub fn bytes(self) -> usize {
4041
self.num_bits / 8
4142
}
4243

44+
#[inline(always)]
4345
pub fn bits(self) -> usize {
4446
self.num_bits
4547
}
4648

49+
#[inline(always)]
4750
pub fn from_bits(num_bits: usize) -> MachineSize {
4851
MachineSize { num_bits }
4952
}
53+
54+
#[inline]
55+
pub fn unsigned_int_max(self) -> Option<u128> {
56+
(self.num_bits <= 128).then(|| u128::MAX >> (128 - self.bits()))
57+
}
5058
}

compiler/stable_mir/src/ty.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ impl TyKind {
324324

325325
#[inline]
326326
pub fn is_cstr(&self) -> bool {
327-
let TyKind::RigidTy(RigidTy::Adt(def, _)) = self else { return false };
327+
let TyKind::RigidTy(RigidTy::Adt(def, _)) = self else {
328+
return false;
329+
};
328330
with(|cx| cx.adt_is_cstr(*def))
329331
}
330332

@@ -1032,10 +1034,13 @@ pub struct BoundTy {
10321034
}
10331035

10341036
pub type Bytes = Vec<Option<u8>>;
1037+
1038+
/// Size in bytes.
10351039
pub type Size = usize;
10361040

10371041
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
10381042
pub struct Prov(pub AllocId);
1043+
10391044
pub type Align = u64;
10401045
pub type Promoted = u32;
10411046
pub type InitMaskMaterialized = Vec<u64>;

0 commit comments

Comments
 (0)