<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><blockquote type="cite" class="">On Oct 1, 2018, at 3:25 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" class="">dblaikie@gmail.com</a>> wrote:<br class=""></blockquote><div><blockquote type="cite" class=""><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Mon, Oct 1, 2018 at 3:24 PM JF Bastien <<a href="mailto:jfbastien@apple.com" class="">jfbastien@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><blockquote type="cite" class=""><div class="">On Oct 1, 2018, at 3:16 PM, David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank" class="">dblaikie@gmail.com</a>> wrote:</div><br class="m_-3455304991324924934Apple-interchange-newline"><div class=""><div dir="ltr" class="">(my vote, somewhat biased - is that I'd love to see more investment in Bloaty (to keep all these sort of size analysis tools and tricks in one place), but sort of accept folks are probably going to keep building more infrastructure for this sort of thing in LLVM directly)<br class=""></div></div></blockquote><div class=""><br class=""></div></div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><div class="">I get where that comes from, but it seems a bit like a Valgrind versus sanitizer argument: integrating with the toolchain gives you things you can’t really get otherwise. Valgrind is still great as a self-standing thing.</div></div></div></blockquote><div class=""><br class="">Not sure that's quite the same though - with sanitizer integrating with the optimizers is the key here.<br class=""><br class="">With bloaty - it could, at worst, use LLVM's libDebugInfo as a library to implement the more advanced debug-using features without being less functional than an in-LLVM implementation.<br class=""></div></div></div></div></blockquote><div><br class=""></div><div>I’m a bit biased too, but fwiw: my preference would be to add a new size analysis tool to llvm.</div><div><br class=""></div><div>Such a tool might grow to depend on code for object file parsing, debug info parsing, demangling, and disassembling (all of which bloaty either reimplements or pulls in). Living in-tree should make it easier to pick up bug fixes in these dependencies and reduce maintenance overhead.</div><div><br class=""></div><div>While I really like bloaty, my impression is that it’d be better to implement the functionality I’d like to use in a new tool.</div><div><br class=""></div><div>vedant</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class="">- Dave<br class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><div class=""><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div dir="ltr" class="">On Wed, Sep 26, 2018 at 12:03 PM Vedant Kumar <<a href="mailto:vsk@apple.com" target="_blank" class="">vsk@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br class="">
<br class="">
I worked on a code size analysis tool for a 'week of code' project and think<br class="">
that it might be useful enough to upstream.<br class="">
<br class="">
The tool is inspired by bloaty (<a href="https://github.com/google/bloaty" rel="noreferrer" target="_blank" class="">https://github.com/google/bloaty</a>), but tries to<br class="">
do more to attribute code size in actionable ways.<br class="">
<br class="">
For example, it can calculate how many bytes inlined instances of a function<br class="">
added to a binary. In its diff mode, it can show how much more aggressively a<br class="">
function was inlined compared to a baseline. This can be useful when you're,<br class="">
say, trying to figure out why firmware compiled by a new compiler is just a few<br class="">
bytes over the size limit imposed by your embedded device :). In this case,<br class="">
extra information about inlining can help inform a decision to either tweak the<br class="">
inliner's cost model or to judiciously add a few `noinline` attributes. (Note<br class="">
that if you're willing to recompile & write a few SQL queries, optimization<br class="">
remarks can give you similar information, albeit at the IR level.)<br class="">
<br class="">
As another example, this code size tool can attribute code size to semantically<br class="">
interesting groups of code, like C++/Swift classes, or files. In the diff mode,<br class="">
you can see how the code size of a class/file grew compared to a baseline. The<br class="">
tool understands inheritance, so you can also see interesting high-level trends.<br class="">
E.g `clang::Sema` grew more than `llvm::Pass` between clang-6 and clang-7.<br class="">
<br class="">
Unlike bloaty, this tool focuses exclusively on the text segment. Also unlike<br class="">
bloaty, it uses LLVM's DWARF parser instead of rolling its own. The tool is<br class="">
currently implemented as a sub-tool of llvm-dwarfdump.<br class="">
<br class="">
To get size information about a program, you do:<br class="">
<br class="">
llvm-dwarfdump size-info -baseline <object> -stats-dir <dir><br class="">
<br class="">
This emits four *.stats files into <dir>, each containing a distinct 'view' into<br class="">
the code groups in <object>. There's a file view, a function view, a class view,<br class="">
and an inlining view. Each view is sorted by code size, so you can see the<br class="">
largest functions/classes/etc immediately.<br class="">
<br class="">
The *.stats files are just human-readable text files. As it happens, they use<br class="">
the flamegraph format (<a href="http://brendangregg.com/flamegraphs.html" rel="noreferrer" target="_blank" class="">http://brendangregg.com/flamegraphs.html</a>). This makes it<br class="">
easy to visualize any view as a flamegraph. (If you haven't seen one before,<br class="">
it's a hierarchical visualization where the width of each entry corresponds to<br class="">
its frequency (or in this case size).)<br class="">
<br class="">
To look at code growth between two programs, you'd do:<br class="">
<br class="">
llvm-dwarfdump size-info -baseline <object> -target <object> -stats-dir <dir><br class="">
<br class="">
Similarly, this emits four 'view' files into <dir>, but with a *.diffstats<br class="">
suffix. The format is the same.<br class="">
<br class="">
Pending Work<br class="">
------------<br class="">
<br class="">
I think the main piece of work the tool needs is better testing. Currently<br class="">
there's just a single end-to-end test in clang. It might be better to check in<br class="">
a few binaries so we can check that the tool reports sizes correctly.<br class="">
<br class="">
Also, it may turn out that folks are interested in different ways of visualizing<br class="">
size data. While the textual format of flamegraphs is really convenient for<br class="">
humans to read, the graphs themselves do make more sense when the underlying<br class="">
data have a frequentist interpretation. If there's enough interest I can explore<br class="">
using an alternative format for visualization, e.g:<br class="">
<br class="">
<a href="http://neugierig.org/software/chromium/bloat/" rel="noreferrer" target="_blank" class="">http://neugierig.org/software/chromium/bloat/</a><br class="">
<a href="https://github.com/evmar/webtreemap" rel="noreferrer" target="_blank" class="">https://github.com/evmar/webtreemap</a><br class="">
<br class="">
(Thanks JF for pointing these out!)<br class="">
<br class="">
Here's a link to the source code:<br class="">
<br class="">
<a href="https://github.com/vedantk/llvm-project/tree/sizeinfo" rel="noreferrer" target="_blank" class="">https://github.com/vedantk/llvm-project/tree/sizeinfo</a> <br class="">
<br class="">
Selected Examples<br class="">
-----------------<br class="">
<br class="">
Here are a few interesting snippets from a comparison of clang-6 vs. clang-7.<br class="">
<br class="">
First, let's take a look at the function view diffstat. Here are the 10<br class="">
functions which grew in size the most. On the left hand side, you'll see the<br class="">
demangled function name. The *change* in code size in bytes is reported on the<br class="">
right hand side (only positive changes are reported).<br class="">
<br class="">
clang::Sema::CheckHexagonBuiltinCpu([snip]) [function] 170316<br class="">
ProcessDeclAttribute([snip]) [function] 125893<br class="">
llvm::AArch64InstPrinter::printAliasInstr([snip]) [function] 105133<br class="">
llvm::AArch64AppleInstPrinter::printAliasInstr([snip]) [function] 105133<br class="">
ParseCodeGenArgs([snip]) [function] 64692<br class="">
unswitchNontrivialInvariants([snip]) [function] 40180<br class="">
getAttrKind([snip]) [function] 35811<br class="">
clang::DumpCompilerOptionsAction::ExecuteAction() [function] 32417<br class="">
llvm::UpgradeIntrinsicCall([snip]) [function] 30239<br class="">
bool llvm::InstructionSelector::executeMatchTable<(anonymous namespace)::ARMInstructionSelector const, [snip]) const [function] 29352<br class="">
<br class="">
<br class="">
Next, let's look at the file view diffstat. This can be useful because it goes<br class="">
beyond simply identifying the files which grew the most. It actually describes<br class="">
which *functions* grew the most in those files, creating more opportunites to<br class="">
do something about the code growth.<br class="">
<br class="">
lib/Target/X86/X86ISelLowering.cpp [file];combineX86ShuffleChain([snip]) [function] 24864<br class="">
lib/Target/X86/X86ISelLowering.cpp [file];combineMul([snip]) [function] 14907<br class="">
lib/Target/X86/X86ISelLowering.cpp [file];combineStore([snip]) [function] 12220<br class="">
...<br class="">
tools/clang/lib/Sema/SemaExpr.cpp [file];clang::Sema::CheckCompareOperands([snip]) [function] 16024<br class="">
tools/clang/lib/Sema/SemaExpr.cpp [file];diagnoseTautologicalComparison([snip]) [function] 1740<br class="">
tools/clang/lib/Sema/SemaExpr.cpp [file];clang::Sema::ActOnNumericConstant([snip]) [function] 1436<br class="">
tools/clang/lib/Sema/SemaExpr.cpp [file];checkThreeWayNarrowingConversion([snip]) [function] 1356<br class="">
tools/clang/lib/Sema/SemaExpr.cpp [file];CheckIdentityFieldAssignment([snip]) [function] 1280<br class="">
<br class="">
<br class="">
The class view diffstat is a bit different because it has more levels of<br class="">
nesting than the other views, due to inheritance. This might help give a sense<br class="">
for the high-level changes in a program, but may also be less actionable.<br class="">
<br class="">
clang::Sema [class];clang::Sema::CheckHexagonBuiltinCpu([snip]) [function] 170316<br class="">
clang::Sema [class];clang::Sema::CheckHexagonBuiltinArgument([snip]) [function] 24156<br class="">
clang::Sema [class];clang::Sema::ActOnTag([snip]) [function] 22373<br class="">
...<br class="">
llvm::AArch64InstPrinter [class];llvm::AArch64AppleInstPrinter [class];llvm::AArch64AppleInstPrinter::printAliasInstr([snip]) [function] 105133<br class="">
llvm::AArch64InstPrinter [class];llvm::AArch64AppleInstPrinter [class];llvm::AArch64AppleInstPrinter::printInstruction([snip]) [function] 5824<br class="">
...<br class="">
llvm::Pass [class];llvm::FunctionPass [class];llvm::MachineFunctionPass [class];(anon)::X86SpeculativeLoadHardeningPass [class];(anonymous namespace)::X86SpeculativeLoadHardeningPass::checkAllLoads(llvm::MachineFunction&) [function] 19287<br class="">
...<br class="">
llvm::Pass [class];llvm::FunctionPass [class];llvm::MachineFunctionPass [class];(anon)::MachineLICMBase [class];(anonymous namespace)::MachineLICMBase::runOnMachineFunction(llvm::MachineFunction&) [function] 20343<br class="">
<br class="">
Here's a link to a flamegraph of the class view diffstat (warning: it's big):<br class="">
<br class="">
<a href="http://net.vedantk.com/static/llvm/swift-clang-4.2-vs-5.0.class-view.diffstats.svg" rel="noreferrer" target="_blank" class="">http://net.vedantk.com/static/llvm/swift-clang-4.2-vs-5.0.class-view.diffstats.svg</a><br class="">
<br class="">
Finally, here are a few interesting entries from the inlining view diffstat. As<br class="">
with all of the other views, the right hand side still shows code growth in<br class="">
bytes. For a given inlining target, this size is computed by diffing the sum of<br class="">
PC range lengths from all DW_TAG_inlined_subroutines referring to that target.<br class="">
This allows the size tool to attribute code size to an inlining target even<br class="">
when the inlined code is not contiguous in the caller.<br class="">
<br class="">
llvm::raw_ostream::operator<<(char const*) [inlining-target] 66720<br class="">
llvm::MCRegisterClass::contains(unsigned int) const [inlining-target] 64161<br class="">
llvm::StringRef::StringRef(char const*) [inlining-target] 39262<br class="">
llvm::MCInst::getOperand(unsigned int) const [inlining-target] 33268<br class="">
clang::CodeCompletionResult::~CodeCompletionResult() [inlining-target] 25763<br class="">
llvm::operator+(llvm::Twine const&, llvm::Twine const&) [inlining-target] 25525<br class="">
clang::ASTImporter::Import(clang::SourceLocation) [inlining-target] 21096<br class="">
clang::Sema::Diag(clang::SourceLocation, unsigned int) [inlining-target] 20898<br class="">
<br class="">
Feedback & questions welcome!<br class="">
<br class="">
thanks,<br class="">
vedant<br class="">
</blockquote></div></div>
</div></blockquote></div></div></blockquote></div></div>
</div></blockquote></div><br class=""></body></html>