Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sorting on totals #10

Merged
merged 2 commits into from
Jul 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ categories = ["development-tools::cargo-plugins", "development-tools::debugging"
isatty = "0.1"
rustc-demangle = "0.1"
tempdir = "0.3"
structopt = "0.2.10"
133 changes: 102 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
extern crate isatty;
extern crate rustc_demangle;
extern crate tempdir;
#[macro_use]
extern crate structopt;

use isatty::stderr_isatty;
use rustc_demangle::demangle;
use std::collections::HashMap as Map;
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::io::{self, Read, Write, ErrorKind};
use std::io::{self, ErrorKind, Read, Write};
use std::path::{Path, PathBuf};
use std::process::{self, Child, Stdio, Command};

extern crate isatty;
use isatty::stderr_isatty;

extern crate rustc_demangle;
use rustc_demangle::demangle;

extern crate tempdir;
use std::process::{self, Child, Command, Stdio};
use structopt::StructOpt;
use tempdir::TempDir;

#[derive(StructOpt, Debug)]
#[structopt(
name = "cargo-llvm-lines",
bin_name = "cargo",
about = "Print amount of lines of LLVM IR that is generated for the current project"
)]
enum Opt {
#[structopt(
name = "llvm-lines", raw(setting = "structopt::clap::AppSettings::AllowExternalSubcommands")
)]
LLVMLines {
#[structopt(long = "filter-cargo", raw(hidden = "true"))]
filter_cargo: bool,
#[structopt(long = "lib", raw(hidden = "true"))]
lib: bool,

#[structopt(long = "bin", raw(hidden = "true"))]
bin: Option<String>,

/// Set the sort order to number of instantiations
#[structopt(short = "i", long = "sort-insts")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pick a different name for this flag. "Inst" commonly stands for "instruction" not "instantiation", especially in the context of LLVM. For example InstCombine, MCInst, RISCVInstPrinter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, any suggestions? I have very little opinion on this

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bloaty has a -s <sortby> flag which seems reasonable to me. In our case it could be -s lines (the default) or -s copies with long equivalents --sort=lines and --sort=copies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to that

sort_insts: bool,
},
}

fn main() {
let result = cargo_llvm_lines();
let Opt::LLVMLines {
filter_cargo,
sort_insts,
..
} = Opt::from_args();

let sort_order = match sort_insts {
false => SortOrder::TotalLines,
_ => SortOrder::Copies,
};
let result = cargo_llvm_lines(filter_cargo, sort_order);

process::exit(match result {
Ok(code) => code,
Err(err) => {
Expand All @@ -26,25 +63,26 @@ fn main() {
});
}

fn cargo_llvm_lines() -> io::Result<i32> {
match env::args_os().last().unwrap().to_str().unwrap_or("") {
"--filter-cargo" => filter_err(ignore_cargo_err),
_ => {}
fn cargo_llvm_lines(filter_cargo: bool, sort_order: SortOrder) -> io::Result<i32> {
if filter_cargo {
filter_err(ignore_cargo_err);
}

let outdir = TempDir::new("cargo-llvm-lines").expect("failed to create tmp file");
let outfile = outdir.path().join("crate");

run_cargo_rustc(outfile)?;
let ir = read_llvm_ir(outdir)?;
count_lines(ir);
count_lines(ir, sort_order);

Ok(0)
}

fn run_cargo_rustc(outfile: PathBuf) -> io::Result<()> {
let mut cmd = Command::new("cargo");
let args: Vec<_> = env::args_os().collect();
let args: Vec<_> = env::args_os()
.filter(|s| !["--sort-insts", "-i"].contains(&s.to_string_lossy().as_ref()))
.collect();
cmd.args(&wrap_args(args.clone(), outfile.as_ref()));
cmd.env("CARGO_INCREMENTAL", "");

Expand Down Expand Up @@ -88,7 +126,12 @@ impl Instantiations {
}
}

fn count_lines(content: String) {
enum SortOrder {
TotalLines,
Copies,
}

fn count_lines(content: String, sort_order: SortOrder) {
let mut instantiations = Map::<String, Instantiations>::new();
let mut current_function = None;
let mut count = 0;
Expand All @@ -98,7 +141,8 @@ fn count_lines(content: String) {
current_function = parse_function_name(line);
} else if line == "}" {
if let Some(name) = current_function.take() {
instantiations.entry(name)
instantiations
.entry(name)
.or_insert_with(Default::default)
.record_lines(count);
}
Expand All @@ -109,16 +153,32 @@ fn count_lines(content: String) {
}

let mut data = instantiations.into_iter().collect::<Vec<_>>();
data.sort_by(|a, b| {
let key_lo = (b.1.total_lines, b.1.copies, &a.0);
let key_hi = (a.1.total_lines, a.1.copies, &b.0);
key_lo.cmp(&key_hi)
});

match sort_order {
SortOrder::TotalLines => {
data.sort_by(|a, b| {
let key_lo = (b.1.total_lines, b.1.copies, &a.0);
let key_hi = (a.1.total_lines, a.1.copies, &b.0);
key_lo.cmp(&key_hi)
});
}
SortOrder::Copies => {
data.sort_by(|a, b| {
let key_lo = (b.1.copies, b.1.total_lines, &a.0);
let key_hi = (a.1.copies, a.1.total_lines, &b.0);
key_lo.cmp(&key_hi)
});
}
}

let stdout = io::stdout();
let mut handle = stdout.lock();
for row in data {
let _ = writeln!(handle, "{:7} {:4} {}", row.1.total_lines, row.1.copies, row.0);
let _ = writeln!(
handle,
"{:7} {:4} {}",
row.1.total_lines, row.1.copies, row.0
);
}
}

Expand All @@ -141,9 +201,7 @@ fn has_hash(name: &str) -> bool {
return false;
}
}
bytes.next() == Some(b'h')
&& bytes.next() == Some(b':')
&& bytes.next() == Some(b':')
bytes.next() == Some(b'h') && bytes.next() == Some(b':') && bytes.next() == Some(b':')
}

fn is_ascii_hexdigit(byte: u8) -> bool {
Expand Down Expand Up @@ -179,8 +237,20 @@ unsafe fn create_stdios(child: &Child) -> (Stdio, Stdio) {
#[cfg(target_os = "windows")]
unsafe fn create_stdios(child: &Child) -> (Stdio, Stdio) {
use std::os::windows::io::{AsRawHandle, FromRawHandle};
let stdout = Stdio::from_raw_handle(child.stdout.as_ref().map(AsRawHandle::as_raw_handle).unwrap());
let stderr = Stdio::from_raw_handle(child.stderr.as_ref().map(AsRawHandle::as_raw_handle).unwrap());
let stdout = Stdio::from_raw_handle(
child
.stdout
.as_ref()
.map(AsRawHandle::as_raw_handle)
.unwrap(),
);
let stderr = Stdio::from_raw_handle(
child
.stderr
.as_ref()
.map(AsRawHandle::as_raw_handle)
.unwrap(),
);

(stdout, stderr)
}
Expand Down Expand Up @@ -267,9 +337,10 @@ fn ignore_cargo_err(line: &str) -> bool {
requested",
"ignoring specified output filename for 'link' output because multiple \
outputs were requested",
"ignoring --out-dir flag due to -o flag.",
"ignoring --out-dir flag due to -o flag",
"due to multiple output types requested, the explicitly specified \
output file name will be adapted for each output type",
"ignoring -C extra-filename flag due to -o flag",
];
for s in &blacklist {
if line.contains(s) {
Expand Down