From 284df9fc34ffb5d1e746c31902cbd1982144b45b Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 28 Jun 2023 11:31:28 -0300 Subject: [PATCH 1/6] Wrap SMIR bool and tuple into a Rigid variant --- compiler/rustc_smir/src/rustc_smir/mod.rs | 11 ++++++----- compiler/rustc_smir/src/stable_mir/ty.rs | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 874e34bef60..76cb8386618 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,8 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::stable_mir::{self, ty::TyKind, Context}; +use crate::stable_mir::ty::{RigidTy, TyKind}; +use crate::stable_mir::{self, Context}; use rustc_middle::mir; use rustc_middle::ty::{self, Ty, TyCtxt}; use rustc_span::def_id::{CrateNum, DefId, LOCAL_CRATE}; @@ -69,7 +70,7 @@ pub struct Tables<'tcx> { impl<'tcx> Tables<'tcx> { fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind { match ty.kind() { - ty::Bool => TyKind::Bool, + ty::Bool => TyKind::RigidTy(RigidTy::Bool), ty::Char => todo!(), ty::Int(_) => todo!(), ty::Uint(_) => todo!(), @@ -90,9 +91,9 @@ impl<'tcx> Tables<'tcx> { ty::GeneratorWitness(_) => todo!(), ty::GeneratorWitnessMIR(_, _) => todo!(), ty::Never => todo!(), - ty::Tuple(fields) => { - TyKind::Tuple(fields.iter().map(|ty| self.intern_ty(ty)).collect()) - } + ty::Tuple(fields) => TyKind::RigidTy(RigidTy::Tuple( + fields.iter().map(|ty| self.intern_ty(ty)).collect(), + )), ty::Alias(_, _) => todo!(), ty::Param(_) => todo!(), ty::Bound(_, _) => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index f27801b0f6c..d9d858ebe08 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -10,6 +10,10 @@ impl Ty { } pub enum TyKind { + RigidTy(RigidTy), +} + +pub enum RigidTy { Bool, Tuple(Vec), } From 61adcaf87b605f1b2d73f62cda813329cb448807 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 5 Jul 2023 18:50:13 -0300 Subject: [PATCH 2/6] Add rustc_ty_to_ty basic tests --- compiler/rustc_smir/src/stable_mir/ty.rs | 2 ++ tests/ui-fulldeps/stable-mir/crate-info.rs | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index d9d858ebe08..86c30370ff5 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -9,10 +9,12 @@ impl Ty { } } +#[derive(Clone, Debug)] pub enum TyKind { RigidTy(RigidTy), } +#[derive(Clone, Debug)] pub enum RigidTy { Bool, Tuple(Vec), diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index a3db2e9ef24..7cbb7898f2c 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -7,6 +7,7 @@ // edition: 2021 #![feature(rustc_private)] +#![feature(assert_matches)] extern crate rustc_driver; extern crate rustc_hir; @@ -19,6 +20,7 @@ use rustc_hir::def::DefKind; use rustc_interface::{interface, Queries}; use rustc_middle::ty::TyCtxt; use rustc_smir::{rustc_internal, stable_mir}; +use std::assert_matches::assert_matches; use std::io::Write; const CRATE_NAME: &str = "input"; @@ -63,6 +65,18 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { other => panic!("{other:?}"), } + let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); + let body = types.body(); + assert_eq!(body.locals.len(), 2); + assert_matches!( + body.locals[0].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) + ); + assert_matches!( + body.locals[1].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) + ); + let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); assert_eq!(body.blocks.len(), 2); @@ -153,6 +167,10 @@ fn generate_input(path: &str) -> std::io::Result<()> { x_64.wrapping_add(y_64) }} + pub fn types(b: bool) -> bool {{ + b + }} + pub fn drop(_: String) {{}} pub fn assert(x: i32) -> i32 {{ From 73e816e37ce028274d603ab33d8717d35dc9e914 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 5 Jul 2023 19:01:11 -0300 Subject: [PATCH 3/6] Add Char ty to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 2 +- compiler/rustc_smir/src/stable_mir/ty.rs | 1 + tests/ui-fulldeps/stable-mir/crate-info.rs | 8 ++++++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 76cb8386618..3a66eefdb79 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -71,7 +71,7 @@ impl<'tcx> Tables<'tcx> { fn rustc_ty_to_ty(&mut self, ty: Ty<'tcx>) -> TyKind { match ty.kind() { ty::Bool => TyKind::RigidTy(RigidTy::Bool), - ty::Char => todo!(), + ty::Char => TyKind::RigidTy(RigidTy::Char), ty::Int(_) => todo!(), ty::Uint(_) => todo!(), ty::Float(_) => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 86c30370ff5..936fb0d3e4f 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -17,5 +17,6 @@ pub enum TyKind { #[derive(Clone, Debug)] pub enum RigidTy { Bool, + Char, Tuple(Vec), } diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 7cbb7898f2c..e3b4f6e7df9 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -67,7 +67,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); - assert_eq!(body.locals.len(), 2); + assert_eq!(body.locals.len(), 3); assert_matches!( body.locals[0].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) @@ -76,6 +76,10 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { body.locals[1].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) ); + assert_matches!( + body.locals[2].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Char) + ); let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); @@ -167,7 +171,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { x_64.wrapping_add(y_64) }} - pub fn types(b: bool) -> bool {{ + pub fn types(b: bool, _: char) -> bool {{ b }} From 458ead41d621a6aacdf0d27169a4aa9d1ff54f98 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 5 Jul 2023 19:06:49 -0300 Subject: [PATCH 4/6] Add Int ty to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 11 +++++++++-- compiler/rustc_smir/src/stable_mir/ty.rs | 11 +++++++++++ tests/ui-fulldeps/stable-mir/crate-info.rs | 8 ++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index 3a66eefdb79..d2b13c32cc2 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,7 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::stable_mir::ty::{RigidTy, TyKind}; +use crate::stable_mir::ty::{IntTy, RigidTy, TyKind}; use crate::stable_mir::{self, Context}; use rustc_middle::mir; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -72,7 +72,14 @@ impl<'tcx> Tables<'tcx> { match ty.kind() { ty::Bool => TyKind::RigidTy(RigidTy::Bool), ty::Char => TyKind::RigidTy(RigidTy::Char), - ty::Int(_) => todo!(), + ty::Int(int_ty) => match int_ty { + ty::IntTy::Isize => TyKind::RigidTy(RigidTy::Int(IntTy::Isize)), + ty::IntTy::I8 => TyKind::RigidTy(RigidTy::Int(IntTy::I8)), + ty::IntTy::I16 => TyKind::RigidTy(RigidTy::Int(IntTy::I16)), + ty::IntTy::I32 => TyKind::RigidTy(RigidTy::Int(IntTy::I32)), + ty::IntTy::I64 => TyKind::RigidTy(RigidTy::Int(IntTy::I64)), + ty::IntTy::I128 => TyKind::RigidTy(RigidTy::Int(IntTy::I128)), + }, ty::Uint(_) => todo!(), ty::Float(_) => todo!(), ty::Adt(_, _) => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 936fb0d3e4f..f31c078b4ab 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -18,5 +18,16 @@ pub enum TyKind { pub enum RigidTy { Bool, Char, + Int(IntTy), Tuple(Vec), } + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum IntTy { + Isize, + I8, + I16, + I32, + I64, + I128, +} diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index e3b4f6e7df9..8ac32d23c6f 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -67,7 +67,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); - assert_eq!(body.locals.len(), 3); + assert_eq!(body.locals.len(), 4); assert_matches!( body.locals[0].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) @@ -80,6 +80,10 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { body.locals[2].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Char) ); + assert_matches!( + body.locals[3].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Int(stable_mir::ty::IntTy::I32)) + ); let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); @@ -171,7 +175,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { x_64.wrapping_add(y_64) }} - pub fn types(b: bool, _: char) -> bool {{ + pub fn types(b: bool, _: char, _: i32) -> bool {{ b }} From 42eccffce3e950e95d9e965d4dd58ecb44633d55 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 5 Jul 2023 19:26:52 -0300 Subject: [PATCH 5/6] Add Uint ty to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 11 +++++++++-- compiler/rustc_smir/src/stable_mir/ty.rs | 11 +++++++++++ tests/ui-fulldeps/stable-mir/crate-info.rs | 8 ++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index d2b13c32cc2..adce55d639f 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,7 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::stable_mir::ty::{IntTy, RigidTy, TyKind}; +use crate::stable_mir::ty::{IntTy, RigidTy, TyKind, UintTy}; use crate::stable_mir::{self, Context}; use rustc_middle::mir; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -80,7 +80,14 @@ impl<'tcx> Tables<'tcx> { ty::IntTy::I64 => TyKind::RigidTy(RigidTy::Int(IntTy::I64)), ty::IntTy::I128 => TyKind::RigidTy(RigidTy::Int(IntTy::I128)), }, - ty::Uint(_) => todo!(), + ty::Uint(uint_ty) => match uint_ty { + ty::UintTy::Usize => TyKind::RigidTy(RigidTy::Uint(stable_mir::ty::UintTy::Usize)), + ty::UintTy::U8 => TyKind::RigidTy(RigidTy::Uint(UintTy::U8)), + ty::UintTy::U16 => TyKind::RigidTy(RigidTy::Uint(UintTy::U16)), + ty::UintTy::U32 => TyKind::RigidTy(RigidTy::Uint(UintTy::U32)), + ty::UintTy::U64 => TyKind::RigidTy(RigidTy::Uint(UintTy::U64)), + ty::UintTy::U128 => TyKind::RigidTy(RigidTy::Uint(UintTy::U128)), + }, ty::Float(_) => todo!(), ty::Adt(_, _) => todo!(), ty::Foreign(_) => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index f31c078b4ab..2db3bdb7af1 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -19,6 +19,7 @@ pub enum RigidTy { Bool, Char, Int(IntTy), + Uint(UintTy), Tuple(Vec), } @@ -31,3 +32,13 @@ pub enum IntTy { I64, I128, } + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum UintTy { + Usize, + U8, + U16, + U32, + U64, + U128, +} diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 8ac32d23c6f..29cc6e78d88 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -67,7 +67,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); - assert_eq!(body.locals.len(), 4); + assert_eq!(body.locals.len(), 5); assert_matches!( body.locals[0].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) @@ -84,6 +84,10 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { body.locals[3].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Int(stable_mir::ty::IntTy::I32)) ); + assert_matches!( + body.locals[4].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Uint(stable_mir::ty::UintTy::U64)) + ); let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); @@ -175,7 +179,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { x_64.wrapping_add(y_64) }} - pub fn types(b: bool, _: char, _: i32) -> bool {{ + pub fn types(b: bool, _: char, _: i32, _: u64) -> bool {{ b }} From 9ca51b92d4a056d22d3b3d22e54bbb29ed205a40 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Wed, 5 Jul 2023 19:30:24 -0300 Subject: [PATCH 6/6] Add Float ty to SMIR --- compiler/rustc_smir/src/rustc_smir/mod.rs | 9 ++++++--- compiler/rustc_smir/src/stable_mir/ty.rs | 7 +++++++ tests/ui-fulldeps/stable-mir/crate-info.rs | 10 ++++++++-- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs index adce55d639f..85d5bb00c4e 100644 --- a/compiler/rustc_smir/src/rustc_smir/mod.rs +++ b/compiler/rustc_smir/src/rustc_smir/mod.rs @@ -7,7 +7,7 @@ //! //! For now, we are developing everything inside `rustc`, thus, we keep this module private. -use crate::stable_mir::ty::{IntTy, RigidTy, TyKind, UintTy}; +use crate::stable_mir::ty::{FloatTy, IntTy, RigidTy, TyKind, UintTy}; use crate::stable_mir::{self, Context}; use rustc_middle::mir; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -81,14 +81,17 @@ impl<'tcx> Tables<'tcx> { ty::IntTy::I128 => TyKind::RigidTy(RigidTy::Int(IntTy::I128)), }, ty::Uint(uint_ty) => match uint_ty { - ty::UintTy::Usize => TyKind::RigidTy(RigidTy::Uint(stable_mir::ty::UintTy::Usize)), + ty::UintTy::Usize => TyKind::RigidTy(RigidTy::Uint(UintTy::Usize)), ty::UintTy::U8 => TyKind::RigidTy(RigidTy::Uint(UintTy::U8)), ty::UintTy::U16 => TyKind::RigidTy(RigidTy::Uint(UintTy::U16)), ty::UintTy::U32 => TyKind::RigidTy(RigidTy::Uint(UintTy::U32)), ty::UintTy::U64 => TyKind::RigidTy(RigidTy::Uint(UintTy::U64)), ty::UintTy::U128 => TyKind::RigidTy(RigidTy::Uint(UintTy::U128)), }, - ty::Float(_) => todo!(), + ty::Float(float_ty) => match float_ty { + ty::FloatTy::F32 => TyKind::RigidTy(RigidTy::Float(FloatTy::F32)), + ty::FloatTy::F64 => TyKind::RigidTy(RigidTy::Float(FloatTy::F64)), + }, ty::Adt(_, _) => todo!(), ty::Foreign(_) => todo!(), ty::Str => todo!(), diff --git a/compiler/rustc_smir/src/stable_mir/ty.rs b/compiler/rustc_smir/src/stable_mir/ty.rs index 2db3bdb7af1..3181af46e9c 100644 --- a/compiler/rustc_smir/src/stable_mir/ty.rs +++ b/compiler/rustc_smir/src/stable_mir/ty.rs @@ -20,6 +20,7 @@ pub enum RigidTy { Char, Int(IntTy), Uint(UintTy), + Float(FloatTy), Tuple(Vec), } @@ -42,3 +43,9 @@ pub enum UintTy { U64, U128, } + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum FloatTy { + F32, + F64, +} diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 29cc6e78d88..3c0d684a4d7 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -67,7 +67,7 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { let types = get_item(tcx, &items, (DefKind::Fn, "types")).unwrap(); let body = types.body(); - assert_eq!(body.locals.len(), 5); + assert_eq!(body.locals.len(), 6); assert_matches!( body.locals[0].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Bool) @@ -88,6 +88,12 @@ fn test_stable_mir(tcx: TyCtxt<'_>) { body.locals[4].kind(), stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Uint(stable_mir::ty::UintTy::U64)) ); + assert_matches!( + body.locals[5].kind(), + stable_mir::ty::TyKind::RigidTy(stable_mir::ty::RigidTy::Float( + stable_mir::ty::FloatTy::F64 + )) + ); let drop = get_item(tcx, &items, (DefKind::Fn, "drop")).unwrap(); let body = drop.body(); @@ -179,7 +185,7 @@ fn generate_input(path: &str) -> std::io::Result<()> { x_64.wrapping_add(y_64) }} - pub fn types(b: bool, _: char, _: i32, _: u64) -> bool {{ + pub fn types(b: bool, _: char, _: i32, _: u64, _: f64) -> bool {{ b }}