Skip to content

Instantly share code, notes, and snippets.

@tonycoz
Created January 16, 2025 01:15
Show Gist options
  • Save tonycoz/27051a55d276d368ebb21e07965e1bd8 to your computer and use it in GitHub Desktop.
Save tonycoz/27051a55d276d368ebb21e07965e1bd8 to your computer and use it in GitHub Desktop.
process -fsave-optimization-report files to summarize which functions are inlined
#!perl
use v5.36;
use YAML::XS qw(Load);
use File::Find;
use Getopt::Long;
my $non_inline;
my $verbose;
my $lessthan = 100;
GetOptions("n" => \$non_inline,
"v" => \$verbose,
"l=n" => \$lessthan);
my @files;
find(
sub {
# haven't been able to test LTO yet
# || /\.opt\.yaml\.thin\.\d+\.yaml$/
/\.opt\.yaml$/
or return;
push @files, $File::Find::name;
},
shift || "."
);
unless (@files) {
die <<'DIE';
No optimization reports found, you need to build
with -fsave-optimization-record
DIE
}
my $inline_files = qr/(?:\bsv_inline\.h|\binline\.h)$/;
my %funcs;
my %details;
for my $file (@files) {
open my $fh, "<:raw", $file
or die "Cannot open $file: $!\n";
my @data = do { local $/ = "...\n"; <$fh> };
close $fh;
for my $rep (@data) {
my $data = Load($rep);
next if $data->{Pass} ne "inline";
# no definition visible, we can't inline
next if $data->{Name} eq "NoDefinition";
my ($callee) = grep $_->{Callee}, $data->{Args}->@*;
my ($caller) = grep $_->{Caller}, $data->{Args}->@*;
if (!$non_inline &&
(!$callee->{DebugLoc} ||
$callee->{DebugLoc}{File} !~ $inline_files)) {
next;
}
my $func = $callee->{Callee};
++$funcs{$func}{$data->{Name}};
if ($verbose) {
push $details{$func}->@*,
[
$caller->{Caller}, $data->{Name},
$caller->{DebugLoc}{File}, $caller->{DebugLoc}{Line}
];
}
}
}
for my $func (sort keys %funcs) {
my $rec = $funcs{$func};
my $inlined = ($rec->{Inlined} || 0) + ($rec->{AlwaysInline} || 0);
my $not_inlined = ($rec->{TooCostly} || 0) + ($rec->{NeverInline} || 0);
my $total = $inlined + $not_inlined;
my $pc_inlined = sprintf("%.1f", 100 * $inlined / $total);
if ($pc_inlined <= $lessthan) {
print "$func $pc_inlined% $inlined/$total\n";
if ($verbose) {
print " @$_\n" for $details{$func}->@*;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment