<div dir="ltr"><div>Will this only be strictly for binary size, or can we use it for memory size too?</div><div><br></div>One thing I implemented in llvm-pdbutil kind of as a side-exercise to see if it found anything useful was a padding detector.  It turns out it's really annoyingly difficult to reconstruct an exact class layout from debug info, but I think it's about 85% correct now (although for now it only works on Windows until the native high-level PDB access api is complete -- currently only the native low level api is complete).  It will allow you to sort all classes by amount of padding or percentage of class size attributable to padding.  We shaved a couple of percent off of V8's memory usage with this tool.<div><br></div><div>Granted, it's better to have the compiler detect this if possible, but we're talking about a tool that can be run on an arbitrary executable not necessarily built with a compiler we control.</div><div><br></div><div>BTW, even though DWARF doesn't describe types which parameterize templates, it does give you mangled names, so you should be able to reconstruct those types from the mangled names.</div></div><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 1, 2018 at 2:25 PM Vedant Kumar via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></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">Hi Ben,<div><br><div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><blockquote type="cite"><div>On Oct 1, 2018, at 12:48 PM, Ben Craig <<a href="mailto:ben.craig@ni.com" target="_blank">ben.craig@ni.com</a>> wrote:</div><br class="m_7054597131123120838Apple-interchange-newline"><div><div class="m_7054597131123120838WordSection1" style="font-family:Helvetica;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Something that I’ve been looking to do for a while now is to do this at the .o level, and have something to combine the per .o results as well.  I’ve wanted to do that to figure out where I can speed up builds that are overly slow because of redundant template instantiations.<u></u><u></u></div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">You might also consider a view that goes across templates and across namespaces.  It can be useful to see that X% of your code is in std::map instantiations (for example).<span style="font-size:11pt">  This seems similar to how you have inheritance covered.  I’ve also wanted to find ways to visualize the opposite… where I have class Foo and I want to see its total cost, including the size of std::vector<Foo>.</span></div></div></div></blockquote><div><br></div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div>These are great ideas. DWARF might not provide enough information to generate these views (it doesn’t explicitly describe the types which parameterize classes, or the names of un-specialized templates). But it should be possible to piece some of this together by parsing type names.</div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div><br></div><div>vedant</div></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><div><br></div><blockquote type="cite"><div><div class="m_7054597131123120838WordSection1" style="font-family:Helvetica;font-size:13px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><u></u><u></u></div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></div><div style="border-style:none none none solid;border-left-width:1.5pt;border-left-color:blue;padding:0in 0in 0in 4pt"><div><div style="border-style:solid none none;border-top-width:1pt;border-top-color:rgb(225,225,225);padding:3pt 0in 0in"><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><b>From:</b><span class="m_7054597131123120838Apple-converted-space"> </span>llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" target="_blank">llvm-dev-bounces@lists.llvm.org</a>><span class="m_7054597131123120838Apple-converted-space"> </span><b>On Behalf Of<span class="m_7054597131123120838Apple-converted-space"> </span></b>Jake Ehrlich via llvm-dev<br><b>Sent:</b><span class="m_7054597131123120838Apple-converted-space"> </span>Friday, September 28, 2018 3:51 PM<br><b>To:</b><span class="m_7054597131123120838Apple-converted-space"> </span>Vedant Kumar <<a href="mailto:vsk@apple.com" target="_blank">vsk@apple.com</a>><br><b>Cc:</b><span class="m_7054597131123120838Apple-converted-space"> </span>Apple Inc. <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>><br><b>Subject:</b><span class="m_7054597131123120838Apple-converted-space"> </span>Re: [llvm-dev] RFC: Adding a code size analysis tool<u></u><u></u></div></div></div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></div><div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Fantastic! I have been looking at creating a tool that a) only spits out actionable size reductions (preferably with a specific action should be specified) and b) only analyzes the size of allocated sections. The other deficiency I've seen with bloaty is speed and scaling. It's very hard to get bloaty to analyze across a large system of interdependent shared libraries. You can add me as a reviewer to any changes as I would very much like to see such a tool exist.<br><br>> <span style="color:rgb(33,33,33)">Unlike bloaty, this <span class="m_7054597131123120838inbox-inbox-lg">tool</span> focuses exclusively on the text segment.<br><br>I'd like to see support for everything within PT_LOAD segments, not just the executable parts. Everything else you've said is basically what I wanted.</span><u></u><u></u></div></div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif"><u></u> <u></u></div><div><div><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">On Wed, Sep 26, 2018 at 12:03 PM Vedant Kumar via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" style="color:purple;text-decoration:underline" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<u></u><u></u></div></div><blockquote style="border-style:none none none solid;border-left-width:1pt;border-left-color:rgb(204,204,204);padding:0in 0in 0in 6pt;margin-left:4.8pt;margin-right:0in"><div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">Hello,<br><br>I worked on a code size analysis tool for a 'week of code' project and think<br>that it might be useful enough to upstream.<br><br>The tool is inspired by bloaty (<a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_google_bloaty&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=rUbgj05XdrQ_kilEakWhwzGJ8YFX-fhMe18O4iypAfA&e=" style="color:purple;text-decoration:underline" target="_blank">https://github.com/google/bloaty</a>), but tries to<br>do more to attribute code size in actionable ways.<br><br>For example, it can calculate how many bytes inlined instances of a function<br>added to a binary. In its diff mode, it can show how much more aggressively a<br>function was inlined compared to a baseline. This can be useful when you're,<br>say, trying to figure out why firmware compiled by a new compiler is just a few<br>bytes over the size limit imposed by your embedded device :). In this case,<br>extra information about inlining can help inform a decision to either tweak the<br>inliner's cost model or to judiciously add a few `noinline` attributes. (Note<br>that if you're willing to recompile & write a few SQL queries, optimization<br>remarks can give you similar information, albeit at the IR level.)<br><br>As another example, this code size tool can attribute code size to semantically<br>interesting groups of code, like C++/Swift classes, or files. In the diff mode,<br>you can see how the code size of a class/file grew compared to a baseline. The<br>tool understands inheritance, so you can also see interesting high-level trends.<br>E.g `clang::Sema` grew more than `llvm::Pass` between clang-6 and clang-7.<br><br>Unlike bloaty, this tool focuses exclusively on the text segment. Also unlike<br>bloaty, it uses LLVM's DWARF parser instead of rolling its own. The tool is<br>currently implemented as a sub-tool of llvm-dwarfdump.<br><br>To get size information about a program, you do:<br><br>  llvm-dwarfdump size-info -baseline <object> -stats-dir <dir><br><br>This emits four *.stats files into <dir>, each containing a distinct 'view' into<br>the code groups in <object>. There's a file view, a function view, a class view,<br>and an inlining view. Each view is sorted by code size, so you can see the<br>largest functions/classes/etc immediately.<br><br>The *.stats files are just human-readable text files. As it happens, they use<br>the flamegraph format (<a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__brendangregg.com_flamegraphs.html&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=m4EVbw6_KOkrA5-3qUMtnY1DQIjeDLvCl7hNKSWmuhU&e=" style="color:purple;text-decoration:underline" target="_blank">http://brendangregg.com/flamegraphs.html</a>). This makes it<br>easy to visualize any view as a flamegraph. (If you haven't seen one before,<br>it's a hierarchical visualization where the width of each entry corresponds to<br>its frequency (or in this case size).)<br><br>To look at code growth between two programs, you'd do:<br><br>  llvm-dwarfdump size-info -baseline <object> -target <object> -stats-dir <dir><br><br>Similarly, this emits four 'view' files into <dir>, but with a *.diffstats<br>suffix. The format is the same.<br><br>Pending Work<br>------------<br><br>I think the main piece of work the tool needs is better testing. Currently<br>there's just a single end-to-end test in clang. It might be better to check in<br>a few binaries so we can check that the tool reports sizes correctly.<br><br>Also, it may turn out that folks are interested in different ways of visualizing<br>size data. While the textual format of flamegraphs is really convenient for<br>humans to read, the graphs themselves do make more sense when the underlying<br>data have a frequentist interpretation. If there's enough interest I can explore<br>using an alternative format for visualization, e.g:<br><br> <span class="m_7054597131123120838Apple-converted-space"> </span><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__neugierig.org_software_chromium_bloat_&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=2Upr9RUNU0l11KRmg-q55MYe1-LiLeO8I1-Uu1-2_6E&e=" style="color:purple;text-decoration:underline" target="_blank">http://neugierig.org/software/chromium/bloat/</a><br> <span class="m_7054597131123120838Apple-converted-space"> </span><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_evmar_webtreemap&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=VlUC_1511ebsevn6Q87mtH-pSC_vX7uoRSGlJCjTVsQ&e=" style="color:purple;text-decoration:underline" target="_blank">https://github.com/evmar/webtreemap</a><br><br>(Thanks JF for pointing these out!)<br><br>Here's a link to the source code:<br><br> <span class="m_7054597131123120838Apple-converted-space"> </span><a href="https://urldefense.proofpoint.com/v2/url?u=https-3A__github.com_vedantk_llvm-2Dproject_tree_sizeinfo&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=nVZKEg23-nVbh0QtuYgPJibiDRIiWQ113rHCuNdfPRQ&e=" style="color:purple;text-decoration:underline" target="_blank">https://github.com/vedantk/llvm-project/tree/sizeinfo</a> <span class="m_7054597131123120838Apple-converted-space"> </span><br><br>Selected Examples<br>-----------------<br><br>Here are a few interesting snippets from a comparison of clang-6 vs. clang-7.<br><br>First, let's take a look at the function view diffstat. Here are the 10<br>functions which grew in size the most. On the left hand side, you'll see the<br>demangled function name. The *change* in code size in bytes is reported on the<br>right hand side (only positive changes are reported).<br><br>  clang::Sema::CheckHexagonBuiltinCpu([snip]) [function] 170316<br>  ProcessDeclAttribute([snip]) [function] 125893<br>  llvm::AArch64InstPrinter::printAliasInstr([snip]) [function] 105133<br>  llvm::AArch64AppleInstPrinter::printAliasInstr([snip]) [function] 105133<br>  ParseCodeGenArgs([snip]) [function] 64692<br>  unswitchNontrivialInvariants([snip]) [function] 40180<br>  getAttrKind([snip]) [function] 35811<br>  clang::DumpCompilerOptionsAction::ExecuteAction() [function] 32417<br>  llvm::UpgradeIntrinsicCall([snip]) [function] 30239<br>  bool llvm::InstructionSelector::executeMatchTable<(anonymous namespace)::ARMInstructionSelector const, [snip]) const [function] 29352<br><br><br>Next, let's look at the file view diffstat. This can be useful because it goes<br>beyond simply identifying the files which grew the most. It actually describes<br>which *functions* grew the most in those files, creating more opportunites to<br>do something about the code growth.<br><br>  lib/Target/X86/X86ISelLowering.cpp [file];combineX86ShuffleChain([snip]) [function] 24864<br>  lib/Target/X86/X86ISelLowering.cpp [file];combineMul([snip]) [function] 14907<br>  lib/Target/X86/X86ISelLowering.cpp [file];combineStore([snip]) [function] 12220<br>  ...<br>  tools/clang/lib/Sema/SemaExpr.cpp [file];clang::Sema::CheckCompareOperands([snip]) [function] 16024<br>  tools/clang/lib/Sema/SemaExpr.cpp [file];diagnoseTautologicalComparison([snip]) [function] 1740<br>  tools/clang/lib/Sema/SemaExpr.cpp [file];clang::Sema::ActOnNumericConstant([snip]) [function] 1436<br>  tools/clang/lib/Sema/SemaExpr.cpp [file];checkThreeWayNarrowingConversion([snip]) [function] 1356<br>  tools/clang/lib/Sema/SemaExpr.cpp [file];CheckIdentityFieldAssignment([snip]) [function] 1280<br><br><br>The class view diffstat is a bit different because it has more levels of<br>nesting than the other views, due to inheritance. This might help give a sense<br>for the high-level changes in a program, but may also be less actionable.<br><br>  clang::Sema [class];clang::Sema::CheckHexagonBuiltinCpu([snip]) [function] 170316<br>  clang::Sema [class];clang::Sema::CheckHexagonBuiltinArgument([snip]) [function] 24156<br>  clang::Sema [class];clang::Sema::ActOnTag([snip]) [function] 22373<br>  ...<br>  llvm::AArch64InstPrinter [class];llvm::AArch64AppleInstPrinter [class];llvm::AArch64AppleInstPrinter::printAliasInstr([snip]) [function] 105133<br>  llvm::AArch64InstPrinter [class];llvm::AArch64AppleInstPrinter [class];llvm::AArch64AppleInstPrinter::printInstruction([snip]) [function] 5824<br>  ...<br>  llvm::Pass [class];llvm::FunctionPass [class];llvm::MachineFunctionPass [class];(anon)::X86SpeculativeLoadHardeningPass [class];(anonymous namespace)::X86SpeculativeLoadHardeningPass::checkAllLoads(llvm::MachineFunction&) [function] 19287<br>  ...<br>  llvm::Pass [class];llvm::FunctionPass [class];llvm::MachineFunctionPass [class];(anon)::MachineLICMBase [class];(anonymous namespace)::MachineLICMBase::runOnMachineFunction(llvm::MachineFunction&) [function] 20343<br><br>Here's a link to a flamegraph of the class view diffstat (warning: it's big):<br><br> <span class="m_7054597131123120838Apple-converted-space"> </span><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__net.vedantk.com_static_llvm_swift-2Dclang-2D4.2-2Dvs-2D5.0.class-2Dview.diffstats.svg&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=zA8Dt4NET3Uksk2Rhs5I0AsRMdr3xRuKBd4eyFc2prk&e=" style="color:purple;text-decoration:underline" target="_blank">http://net.vedantk.com/static/llvm/swift-clang-4.2-vs-5.0.class-view.diffstats.svg</a><br><br>Finally, here are a few interesting entries from the inlining view diffstat. As<br>with all of the other views, the right hand side still shows code growth in<br>bytes. For a given inlining target, this size is computed by diffing the sum of<br>PC range lengths from all DW_TAG_inlined_subroutines referring to that target.<br>This allows the size tool to attribute code size to an inlining target even<br>when the inlined code is not contiguous in the caller.<br><br>  llvm::raw_ostream::operator<<(char const*) [inlining-target] 66720<br>  llvm::MCRegisterClass::contains(unsigned int) const [inlining-target] 64161<br>  llvm::StringRef::StringRef(char const*) [inlining-target] 39262<br>  llvm::MCInst::getOperand(unsigned int) const [inlining-target] 33268<br>  clang::CodeCompletionResult::~CodeCompletionResult() [inlining-target] 25763<br>  llvm::operator+(llvm::Twine const&, llvm::Twine const&) [inlining-target] 25525<br>  clang::ASTImporter::Import(clang::SourceLocation) [inlining-target] 21096<br>  clang::Sema::Diag(clang::SourceLocation, unsigned int) [inlining-target] 20898<br><br>Feedback & questions welcome!<br><br>thanks,<br>vedant<br>_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" style="color:purple;text-decoration:underline" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.llvm.org_cgi-2Dbin_mailman_listinfo_llvm-2Ddev&d=DwMFaQ&c=I_0YwoKy7z5LMTVdyO6YCiE2uzI1jjZZuIPelcSjixA&r=y8mub81SfUi-UCZRX0Vl1g&m=TUijEuSCtxtUJpZmMT25zPk7FzJ8WtWuvS23OJjbOfc&s=uTy3MnOb2G9Rlej_43g2XmZZjPZzY57kL_hx7zEirk8&e=" style="color:purple;text-decoration:underline" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></div></blockquote></div></div></div></div></blockquote></div></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>