From 79139549248ec0af95331674927744a25a1e31aa Mon Sep 17 00:00:00 2001 From: dkcumming Date: Tue, 29 Apr 2025 22:15:42 +0900 Subject: [PATCH 01/10] Added parenthesis around Deref --- src/mk_graph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index 90b3d09..d3203a7 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -395,7 +395,7 @@ fn project(local: String, ps: &[ProjectionElem]) -> String { fn decorate(thing: String, p: &ProjectionElem) -> String { match p { - ProjectionElem::Deref => format!("*{}", thing), + ProjectionElem::Deref => format!("(*{})", thing), ProjectionElem::Field(i, _) => format!("{thing}.{i}"), ProjectionElem::Index(local) => format!("{thing}[_{local}]"), ProjectionElem::ConstantIndex { From f6af8ffa89284089492ece1777f35c2d84915d5c Mon Sep 17 00:00:00 2001 From: dkcumming Date: Tue, 29 Apr 2025 22:16:45 +0900 Subject: [PATCH 02/10] Tweaked formatting of constants --- src/mk_graph.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index d3203a7..61a9360 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -14,10 +14,10 @@ extern crate stable_mir; use rustc_session::config::{OutFileName, OutputType}; extern crate rustc_session; -use stable_mir::mir::{ +use stable_mir::{mir::{ AggregateKind, BasicBlock, BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind, UnwindAction, -}; +}, ty::RigidTy}; use stable_mir::ty::{IndexedVal, Ty}; use crate::{ @@ -376,7 +376,14 @@ trait GraphLabelString { impl GraphLabelString for Operand { fn label(&self) -> String { match &self { - Operand::Constant(ConstOperand { const_, .. }) => format!("const :: {}", const_.ty()), + Operand::Constant(ConstOperand { const_, .. }) => { + let ty = const_.ty(); + match &ty.kind() { + stable_mir::ty::TyKind::RigidTy(RigidTy::Int(_)) + | stable_mir::ty::TyKind::RigidTy(RigidTy::Uint(_)) => format!("const ?_{}", const_.ty()), + _ => format!("const {}", const_.ty()), + } + } Operand::Copy(place) => format!("cp({})", place.label()), Operand::Move(place) => format!("mv({})", place.label()), } From 66550c2b085c5561350083c9238a0997acdac12c Mon Sep 17 00:00:00 2001 From: dkcumming Date: Tue, 29 Apr 2025 22:18:01 +0900 Subject: [PATCH 03/10] Changed printing of Rvalue::AddressOf --- src/mk_graph.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index 61a9360..9346d69 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -444,7 +444,12 @@ impl GraphLabelString for Rvalue { fn label(&self) -> String { use Rvalue::*; match &self { - AddressOf(kind, p) => format!("&{:?} {}", kind, p.label()), + AddressOf(mutability, p) => { + match mutability { + Mutability::Not => format!("&raw {}", p.label()), + Mutability::Mut => format!("&raw mut {}", p.label()), + } + }, Aggregate(kind, operands) => { let os: Vec = operands.iter().map(|op| op.label()).collect(); format!("{} ({})", kind.label(), os.join(", ")) From 1f3dba75b914334bad1a86db2ddb90baf9e4e7b0 Mon Sep 17 00:00:00 2001 From: dkcumming Date: Tue, 29 Apr 2025 22:18:48 +0900 Subject: [PATCH 04/10] First attempt at printing locals with types --- src/mk_graph.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index 9346d69..8c41226 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -87,6 +87,17 @@ impl SmirJson<'_> { c.set_color(Color::LightGrey); } + // Set out the type information of the locals + let mut local_node = c.node_auto(); + let mut vector: Vec = vec![]; + vector.push(String::from("LOCALS")); + for (index, decl) in body.clone().unwrap().local_decls() { + vector.push(format!("{index} = {}", decl.ty)); + } + local_node.set_label(format!("{}", vector.join("\\l")).as_str()); + drop(local_node); + + // Cannot define local functions that capture env. variables. Instead we define _closures_. let process_block = |cluster: &mut Scope<'_, '_>, node_id: usize, b: &BasicBlock| { From c294219edd79977903d0457cbb0517166e4fb5a3 Mon Sep 17 00:00:00 2001 From: dkcumming Date: Tue, 29 Apr 2025 09:45:37 -0400 Subject: [PATCH 05/10] cargo clippy and cargo fmt --- src/mk_graph.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index 8c41226..ac41f13 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -14,11 +14,15 @@ extern crate stable_mir; use rustc_session::config::{OutFileName, OutputType}; extern crate rustc_session; -use stable_mir::{mir::{ - AggregateKind, BasicBlock, BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, NullOp, - Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind, UnwindAction, -}, ty::RigidTy}; use stable_mir::ty::{IndexedVal, Ty}; +use stable_mir::{ + mir::{ + AggregateKind, BasicBlock, BorrowKind, ConstOperand, Mutability, NonDivergingIntrinsic, + NullOp, Operand, Place, ProjectionElem, Rvalue, Statement, StatementKind, TerminatorKind, + UnwindAction, + }, + ty::RigidTy, +}; use crate::{ printer::{collect_smir, FnSymType, SmirJson}, @@ -94,10 +98,9 @@ impl SmirJson<'_> { for (index, decl) in body.clone().unwrap().local_decls() { vector.push(format!("{index} = {}", decl.ty)); } - local_node.set_label(format!("{}", vector.join("\\l")).as_str()); + local_node.set_label(vector.join("\\l").to_string().as_str()); drop(local_node); - // Cannot define local functions that capture env. variables. Instead we define _closures_. let process_block = |cluster: &mut Scope<'_, '_>, node_id: usize, b: &BasicBlock| { @@ -391,7 +394,9 @@ impl GraphLabelString for Operand { let ty = const_.ty(); match &ty.kind() { stable_mir::ty::TyKind::RigidTy(RigidTy::Int(_)) - | stable_mir::ty::TyKind::RigidTy(RigidTy::Uint(_)) => format!("const ?_{}", const_.ty()), + | stable_mir::ty::TyKind::RigidTy(RigidTy::Uint(_)) => { + format!("const ?_{}", const_.ty()) + } _ => format!("const {}", const_.ty()), } } @@ -455,11 +460,9 @@ impl GraphLabelString for Rvalue { fn label(&self) -> String { use Rvalue::*; match &self { - AddressOf(mutability, p) => { - match mutability { - Mutability::Not => format!("&raw {}", p.label()), - Mutability::Mut => format!("&raw mut {}", p.label()), - } + AddressOf(mutability, p) => match mutability { + Mutability::Not => format!("&raw {}", p.label()), + Mutability::Mut => format!("&raw mut {}", p.label()), }, Aggregate(kind, operands) => { let os: Vec = operands.iter().map(|op| op.label()).collect(); From a490bf2c4fee7c78f58c8106018af219195fa327 Mon Sep 17 00:00:00 2001 From: Daniel Cumming <124537596+dkcumming@users.noreply.github.com> Date: Thu, 1 May 2025 11:10:39 -0400 Subject: [PATCH 06/10] Update src/mk_graph.rs Co-authored-by: Jost Berthold --- src/mk_graph.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index ac41f13..008a6db 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -98,7 +98,10 @@ impl SmirJson<'_> { for (index, decl) in body.clone().unwrap().local_decls() { vector.push(format!("{index} = {}", decl.ty)); } + vector.push("".to_string()); local_node.set_label(vector.join("\\l").to_string().as_str()); + local_node.set_style(Style::Filled); + local_node.set("color", "palegreen3"); drop(local_node); // Cannot define local functions that capture env. variables. Instead we define _closures_. From 6a1d1f252ac7feeebc2d48fd71b65371f2dbdbcc Mon Sep 17 00:00:00 2001 From: dkcumming Date: Thu, 1 May 2025 11:15:38 -0400 Subject: [PATCH 07/10] fixed missing arg setting the color --- src/mk_graph.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mk_graph.rs b/src/mk_graph.rs index 008a6db..3b18cf5 100644 --- a/src/mk_graph.rs +++ b/src/mk_graph.rs @@ -101,7 +101,7 @@ impl SmirJson<'_> { vector.push("".to_string()); local_node.set_label(vector.join("\\l").to_string().as_str()); local_node.set_style(Style::Filled); - local_node.set("color", "palegreen3"); + local_node.set("color", "palegreen3", false); drop(local_node); // Cannot define local functions that capture env. variables. Instead we define _closures_. From c5ae1e99b809b84cd94d10983ad1b9e357f73b7c Mon Sep 17 00:00:00 2001 From: dkcumming Date: Thu, 1 May 2025 21:30:16 -0400 Subject: [PATCH 08/10] Added spans to Stable MIR JSON --- src/printer.rs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index baa2983..ebca63f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -1,3 +1,4 @@ +use std::hash::Hash; use std::io::Write; use std::ops::ControlFlow; use std::{ @@ -486,6 +487,7 @@ type LinkMap<'tcx> = HashMap, (ItemSource, FnSymType)>; type AllocMap = HashMap; type TyMap = HashMap)>; +type SpanMap = HashMap; struct TyCollector<'tcx> { tcx: TyCtxt<'tcx>, @@ -574,9 +576,10 @@ struct InternedValueCollector<'tcx, 'local> { link_map: &'local mut LinkMap<'tcx>, visited_allocs: &'local mut AllocMap, ty_visitor: &'local mut TyCollector<'tcx>, + spans: &'local mut SpanMap, } -type InternedValues<'tcx> = (LinkMap<'tcx>, AllocMap, TyMap); +type InternedValues<'tcx> = (LinkMap<'tcx>, AllocMap, TyMap, SpanMap); fn update_link_map<'tcx>( link_map: &mut LinkMap<'tcx>, @@ -689,6 +692,16 @@ fn collect_alloc( } impl MirVisitor for InternedValueCollector<'_, '_> { + fn visit_span(&mut self, span: &stable_mir::ty::Span) { + let span_internal = internal(self.tcx, span); + let diagnostic_string = self + .tcx + .sess + .source_map() + .span_to_diagnostic_string(span_internal); + self.spans.insert(span.to_index(), diagnostic_string); + } + fn visit_terminator(&mut self, term: &Terminator, loc: stable_mir::mir::visit::Location) { use stable_mir::mir::{ConstOperand, Operand::Constant}; use TerminatorKind::*; @@ -771,6 +784,7 @@ fn collect_interned_values<'tcx>(tcx: TyCtxt<'tcx>, items: Vec<&MonoItem>) -> In let mut calls_map = HashMap::new(); let mut visited_allocs = HashMap::new(); let mut ty_visitor = TyCollector::new(tcx); + let mut span_map = HashMap::new(); if link_items_enabled() { for item in items.iter() { if let MonoItem::Fn(inst) = item { @@ -793,6 +807,7 @@ fn collect_interned_values<'tcx>(tcx: TyCtxt<'tcx>, items: Vec<&MonoItem>) -> In link_map: &mut calls_map, visited_allocs: &mut visited_allocs, ty_visitor: &mut ty_visitor, + spans: &mut span_map, } .visit_body(&body) } else { @@ -812,6 +827,7 @@ fn collect_interned_values<'tcx>(tcx: TyCtxt<'tcx>, items: Vec<&MonoItem>) -> In link_map: &mut calls_map, visited_allocs: &mut visited_allocs, ty_visitor: &mut ty_visitor, + spans: &mut span_map, } .visit_body(&body) } else { @@ -824,7 +840,7 @@ fn collect_interned_values<'tcx>(tcx: TyCtxt<'tcx>, items: Vec<&MonoItem>) -> In MonoItem::GlobalAsm(_) => {} } } - (calls_map, visited_allocs, ty_visitor.types) + (calls_map, visited_allocs, ty_visitor.types, span_map) } // Collection Transitive Closure @@ -1046,6 +1062,7 @@ pub struct SmirJson<'t> { pub uneval_consts: Vec<(ConstDef, String)>, pub items: Vec, pub types: Vec<(stable_mir::ty::Ty, TypeMetadata)>, + pub spans: Vec<(usize, String)>, pub debug: Option>, } @@ -1064,7 +1081,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson { let items = collect_items(tcx); let items_clone = items.clone(); let (unevaluated_consts, mut items) = collect_unevaluated_constant_items(tcx, items); - let (calls_map, visited_allocs, visited_tys) = + let (calls_map, visited_allocs, visited_tys, span_map) = collect_interned_values(tcx, items.iter().map(|i| &i.mono_item).collect::>()); // FIXME: We dump extra static items here --- this should be handled better @@ -1112,11 +1129,14 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson { // .filter(|(_, v)| v.is_primitive()) .collect::>(); + let mut spans = span_map.into_iter().collect::>(); + // sort output vectors to stabilise output (a bit) allocs.sort_by(|a, b| a.0.to_index().cmp(&b.0.to_index())); functions.sort_by(|a, b| a.0 .0.to_index().cmp(&b.0 .0.to_index())); items.sort(); types.sort_by(|a, b| a.0.to_index().cmp(&b.0.to_index())); + spans.sort(); SmirJson { name: local_crate.name, @@ -1126,6 +1146,7 @@ pub fn collect_smir(tcx: TyCtxt<'_>) -> SmirJson { uneval_consts: unevaluated_consts.into_iter().collect(), items, types, + spans, debug, } } From 7c9d7509b51a151acfe2f29d6194b39fe66822b7 Mon Sep 17 00:00:00 2001 From: dkcumming Date: Thu, 1 May 2025 22:15:03 -0400 Subject: [PATCH 09/10] Stripped filename and location positions --- src/printer.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index ebca63f..c0a79a7 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -487,7 +487,7 @@ type LinkMap<'tcx> = HashMap, (ItemSource, FnSymType)>; type AllocMap = HashMap; type TyMap = HashMap)>; -type SpanMap = HashMap; +type SpanMap = HashMap; struct TyCollector<'tcx> { tcx: TyCtxt<'tcx>, @@ -694,12 +694,22 @@ fn collect_alloc( impl MirVisitor for InternedValueCollector<'_, '_> { fn visit_span(&mut self, span: &stable_mir::ty::Span) { let span_internal = internal(self.tcx, span); - let diagnostic_string = self + let (source_file, lo_line, lo_col, hi_line, hi_col) = self .tcx .sess .source_map() - .span_to_diagnostic_string(span_internal); - self.spans.insert(span.to_index(), diagnostic_string); + .span_to_location_info(span_internal); + let file_name = match source_file { + Some(sf) => sf + .name + .display(rustc_span::FileNameDisplayPreference::Remapped) + .to_string(), + None => "no-location".to_string(), + }; + self.spans.insert( + span.to_index(), + (file_name, lo_line, lo_col, hi_line, hi_col), + ); } fn visit_terminator(&mut self, term: &Terminator, loc: stable_mir::mir::visit::Location) { @@ -1052,6 +1062,8 @@ fn mk_type_metadata( } } +type SourceData = (String, usize, usize, usize, usize); + /// the serialised data structure as a whole #[derive(Serialize)] pub struct SmirJson<'t> { @@ -1062,7 +1074,7 @@ pub struct SmirJson<'t> { pub uneval_consts: Vec<(ConstDef, String)>, pub items: Vec, pub types: Vec<(stable_mir::ty::Ty, TypeMetadata)>, - pub spans: Vec<(usize, String)>, + pub spans: Vec<(usize, SourceData)>, pub debug: Option>, } From 27a9dfb32e7798e2bb5b7da1e968eddcf2de8307 Mon Sep 17 00:00:00 2001 From: dkcumming Date: Thu, 1 May 2025 22:29:21 -0400 Subject: [PATCH 10/10] Fixed verbosity error in test-ui --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ef26bd2..dddd374 100644 --- a/Makefile +++ b/Makefile @@ -60,10 +60,11 @@ remake-ui-tests: # This will run without saving source files. Run the script manually to do this. bash tests/ui/remake_ui_tests.sh "$$RUST_DIR_ROOT" +test-ui: VERBOSE=0 test-ui: # Check if RUST_DIR_ROOT is set if [ -z "$$RUST_DIR_ROOT" ]; then \ echo "Error: RUST_DIR_ROOT is not set. Please set it to the absolute path to rust compiler checkout."; \ exit 1; \ fi - bash tests/ui/run_ui_tests.sh "$$RUST_DIR_ROOT" "$$VERBOSE" + bash tests/ui/run_ui_tests.sh "$$RUST_DIR_ROOT" "${VERBOSE}"