Skip to content

Commit

Permalink
Merge pull request #1721 from Kobzol/codegen-diff-ext
Browse files Browse the repository at this point in the history
Improve output of codegen diff
  • Loading branch information
Kobzol authored Sep 18, 2023
2 parents de4dc96 + e79767e commit b9ce069
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion collector/src/bin/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ fn main_result() -> anyhow::Result<i32> {

let mut benchmark_groups =
get_runtime_benchmark_groups(&runtime_benchmark_dir, Some(group))?;
let group = benchmark_groups.pop().unwrap();
let group = benchmark_groups.pop().expect("Benchmark group not found");
assert!(benchmark_groups.is_empty());

codegen_diff(codegen_type, toolchain1, toolchain2, group)?;
Expand Down
35 changes: 28 additions & 7 deletions collector/src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use std::io::IsTerminal;
use std::io::{IsTerminal, Write};
use std::path::Path;
use std::process::Command;

Expand Down Expand Up @@ -58,8 +58,9 @@ pub fn codegen_diff(
.context("Cannot generate codegen using compiler 1")?;
let codegen2 =
get_codegen(&toolchain2, &group.path, codegen_type, *toolchain2_index)
.context("Cannot generate codegen using compiler 1")?;
.context("Cannot generate codegen using compiler 2")?;
if codegen1.trim() != codegen2.trim() {
log::debug!("Analysed function {}", function.name);
return Ok(Some(CodegenDiff::new(function, codegen1, codegen2)));
}
}
Expand Down Expand Up @@ -98,6 +99,7 @@ pub fn codegen_diff(

let mut output = std::io::stdout().lock();
let use_color = output.is_terminal();
write_stats(&mut output, &diffs).context("Cannot write stats")?;
for diff in diffs {
write_diff(&mut output, use_color, &diff).context("Cannot write diff")?;
}
Expand Down Expand Up @@ -157,11 +159,30 @@ impl<'a> CodegenDiff<'a> {
}
}

fn write_diff<W: std::io::Write>(
writer: &mut W,
use_color: bool,
diff: &CodegenDiff,
) -> anyhow::Result<()> {
fn write_stats<W: Write>(writer: &mut W, diffs: &[CodegenDiff]) -> anyhow::Result<()> {
writeln!(writer, "Function size stats:")?;
for diff in diffs {
let size_before = diff.codegen1.len();
let size_after = diff.codegen2.len();
if size_before == size_after {
continue;
}
let percent = (size_after as f64 / size_before as f64) - 1.0;
let percent = percent * 100.0;
writeln!(
writer,
"{}: {size_before} -> {size_after} ({}{:.2}%)",
diff.function.name,
if percent.is_sign_positive() { "+" } else { "-" },
percent.abs()
)?;
}
writer.write_all(b"\n")?;

Ok(())
}

fn write_diff<W: Write>(writer: &mut W, use_color: bool, diff: &CodegenDiff) -> anyhow::Result<()> {
use console::Style;

let text_diff = TextDiff::from_lines(&diff.codegen1, &diff.codegen2);
Expand Down

0 comments on commit b9ce069

Please sign in to comment.