Skip to content

Commit da9eabc

Browse files
committed
mk_graph: add new DOT backend behind env gate with GraphBuilder stubs
Introduce a new DOT implementation wired through the GraphBuilder abstraction and gate it behind `SMIR_DOT_NEW`. Add `to_dot_file_new` entry point and a `DOTBuilder` with stubbed trait method implementations to establish the integration path. This enables incremental migration to the new traversal-based architecture while preserving the existing DOT backend as default.
1 parent d99317e commit da9eabc

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/mk_graph/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ pub use util::GraphLabelString;
2828

2929
/// Entry point to write the DOT file
3030
pub fn emit_dotfile(tcx: TyCtxt<'_>) {
31-
let smir_dot = collect_smir(tcx).to_dot_file();
31+
let smir = collect_smir(tcx);
32+
33+
let smir_dot = if std::env::var("SMIR_DOT_NEW").is_ok() {
34+
smir.to_dot_file_new()
35+
} else {
36+
smir.to_dot_file()
37+
};
3238

3339
match mir_output_path(tcx, "smir.dot") {
3440
OutputDest::Stdout => {

src/mk_graph/output/dot.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use crate::MonoItemKind;
1313
use crate::mk_graph::context::GraphContext;
1414
use crate::mk_graph::util::{block_name, is_unqualified, name_lines, short_name, GraphLabelString};
1515

16+
use crate::mk_graph::traverse::render_graph;
17+
use crate::mk_graph::traverse::{GraphBuilder, RenderedFunction};
18+
1619
impl SmirJson {
1720
/// Convert the MIR to DOT (Graphviz) format
1821
pub fn to_dot_file(self) -> String {
@@ -306,3 +309,56 @@ impl SmirJson {
306309
String::from_utf8(bytes).expect("Error converting dot file")
307310
}
308311
}
312+
313+
// =============================================================================
314+
// DOT Builder
315+
// =============================================================================
316+
317+
pub struct DOTBuilder {
318+
buf: String,
319+
}
320+
321+
impl DOTBuilder {
322+
pub fn new() -> Self {
323+
Self { buf: String::new() }
324+
}
325+
}
326+
327+
impl Default for DOTBuilder {
328+
fn default() -> Self {
329+
Self::new()
330+
}
331+
}
332+
333+
impl GraphBuilder for DOTBuilder {
334+
type Output = String;
335+
336+
fn begin_graph(&mut self, _name: &str) {}
337+
338+
fn alloc_legend(&mut self, _lines: &[String]) {}
339+
340+
fn type_legend(&mut self, _lines: &[String]) {}
341+
342+
fn external_function(&mut self, _id: &str, _name: &str) {}
343+
344+
fn render_function(&mut self, _func: &RenderedFunction) {}
345+
346+
fn static_item(&mut self, _id: &str, _name: &str) {}
347+
348+
fn asm_item(&mut self, _id: &str, _content: &str) {}
349+
350+
fn finish(self) -> Self::Output {
351+
self.buf
352+
}
353+
}
354+
355+
// =============================================================================
356+
// Public entry point (new)
357+
// =============================================================================
358+
359+
impl SmirJson {
360+
/// Convert the MIR to DOT using GraphBuilder traversal
361+
pub fn to_dot_file_new(&self) -> String {
362+
render_graph(self, DOTBuilder::new())
363+
}
364+
}

0 commit comments

Comments
 (0)