Skip to content

Commit

Permalink
feat: prettify assertions on textual ir
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwalker committed Oct 11, 2023
1 parent befd696 commit f1b1f35
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 62 deletions.
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions codegen/masm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ impl TestByEmulationHarness {
// Apply pre-codegen transformations
self.apply_rewrite_passes(function, &mut analysis)?;

let mut original = String::with_capacity(1024);
miden_hir::write_function(&mut original, &function).expect("formatting failed");
println!("{}", &original);
println!("{}", function);

// Make sure all analyses are available
analysis.ensure_all(&function);
Expand Down
3 changes: 3 additions & 0 deletions hir-transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ miden-hir-analysis = { path = "../hir-analysis" }
miden-hir-pass = { path = "../hir-pass" }
rustc-hash.workspace = true
smallvec.workspace = true

[dev-dependencies]
pretty_assertions.workspace = true
13 changes: 5 additions & 8 deletions hir-transform/src/inline_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ mod tests {
AbiParam, Function, FunctionBuilder, Immediate, InstBuilder, Signature, SourceSpan, Type,
};
use miden_hir_analysis::FunctionAnalysis;
use pretty_assertions::{assert_eq, assert_ne};

use crate::{InlineBlocks, RewritePass};

Expand Down Expand Up @@ -420,9 +421,7 @@ mod tests {
builder.ins().ret(Some(ptr6), SourceSpan::UNKNOWN);
}

let mut original = String::with_capacity(1024);
miden_hir::write_function(&mut original, &function).expect("formatting failed");

let original = function.to_string();
let mut analysis = FunctionAnalysis::new(&function);
let mut pass = InlineBlocks;
pass.run(&mut function, &mut analysis)
Expand Down Expand Up @@ -453,10 +452,8 @@ block6(v7: u32):
}
";

let mut inlined = String::with_capacity(1024);
miden_hir::write_function(&mut inlined, &function).expect("formatting failed");

assert_changed!(original, inlined);
assert_formatter_output!(expected, inlined.as_str());
let inlined = function.to_string();
assert_ne!(inlined, original);
assert_eq!(inlined.as_str(), expected);
}
}
34 changes: 0 additions & 34 deletions hir-transform/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
#[cfg(test)]
macro_rules! assert_formatter_output {
($lhs:expr, $rhs:expr) => {{
let lhs = $lhs;
let rhs = $rhs;
if lhs != rhs {
panic!(
r#"
assertion failed: `(left matches right)`
left: `{}`,
right: `{}`"#,
lhs, rhs
);
}
}};
}

#[cfg(test)]
macro_rules! assert_changed {
($lhs:expr, $rhs:expr) => {{
let lhs = &$lhs;
let rhs = &$rhs;
if lhs == rhs {
panic!(
r#"
assertion failed: `(expected ir to change, but it did not)`
output: `
{}`"#,
lhs
);
}
}};
}

pub(crate) mod adt;
mod inline_blocks;
mod split_critical_edges;
Expand Down
13 changes: 5 additions & 8 deletions hir-transform/src/split_critical_edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ mod tests {
AbiParam, Function, FunctionBuilder, Immediate, InstBuilder, Signature, SourceSpan, Type,
};
use miden_hir_analysis::FunctionAnalysis;
use pretty_assertions::{assert_eq, assert_ne};

use crate::{RewritePass, SplitCriticalEdges};

Expand Down Expand Up @@ -239,9 +240,7 @@ mod tests {
builder.ins().ret(Some(result1), SourceSpan::UNKNOWN);
}

let mut original = String::with_capacity(1024);
miden_hir::write_function(&mut original, &function).expect("formatting failed");

let original = function.to_string();
let mut analysis = FunctionAnalysis::new(&function);
let mut pass = SplitCriticalEdges;
pass.run(&mut function, &mut analysis)
Expand Down Expand Up @@ -277,10 +276,8 @@ block3(v6: u32):
}
";

let mut inlined = String::with_capacity(1024);
miden_hir::write_function(&mut inlined, &function).expect("formatting failed");

assert_changed!(original, inlined);
assert_formatter_output!(expected, inlined.as_str());
let transformed = function.to_string();
assert_ne!(transformed, original);
assert_eq!(transformed.as_str(), expected);
}
}
16 changes: 7 additions & 9 deletions hir-transform/src/treeify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ mod tests {
ModuleBuilder,
};
use miden_hir_analysis::FunctionAnalysis;
use pretty_assertions::{assert_eq, assert_ne};

use crate::{RewritePass, Treeify};

Expand Down Expand Up @@ -693,9 +694,7 @@ mod tests {
.remove()
.expect("undefined function");

let mut original = String::with_capacity(1024);
miden_hir::write_function(&mut original, &function).expect("formatting failed");

let original = function.to_string();
let mut analysis = FunctionAnalysis::new(&function);
let mut pass = Treeify;
pass.run(&mut function, &mut analysis)
Expand Down Expand Up @@ -741,14 +740,13 @@ block5:
block6:
v26 = incr v8 : u32
br block3(v7, v26, v9)
v27 = const.u32 0 : u32
br block3(v7, v26, v27)
}
";

let mut inlined = String::with_capacity(1024);
miden_hir::write_function(&mut inlined, &function).expect("formatting failed");

assert_changed!(original, inlined);
assert_formatter_output!(expected, inlined.as_str());
let transformed = function.to_string();
assert_ne!(transformed, original);
assert_eq!(transformed.as_str(), expected);
}
}

0 comments on commit f1b1f35

Please sign in to comment.