Skip to content

Compiling for size #898

Closed
Closed

Description

@stellaraccident

We ultimately need to be compiling the runtime portions of IREE for size. To that end, I started fiddling with the CMake build to play with optimization options. It was hard/hacky to get it working at all, and doing it for real will likely require more build surgery.

These experiments were all done on iree-run-module on x86_64 linux. This is not a perfect proxy because:

  • It contains significant IO and string manipulation that would not be present in a real API user
  • It is a standalone binary vs library
  • It has a lot of debug and safety facilities compiled in.

Still, this largely just means that it is not predictive as to the floor of how small we can make things, but it does provide some relative benefit when comparing options and looking at sorted list of size offenders.

Important Flags:

  • -flto=thin: Enables link time, whole program optimizations. Really hard to get right, especially with respect to whole-archive related things. Anecdotally, this can decrease the size of our binary by ~20-30%.
  • -fnoexceptions: Disables language features that require bulky eh_frame sections
  • -fno-unwind-tables -fno-asynchronous-unwind-tables: Disables frame unwind info (assuming no language features use it). Saves up to 50% of the binary size if applied to everything (at the expense of debugability).

General observations:

  • Abseil does not respect global C flags and uses its own. I ended up hacking their CMake files to do exactly what I wanted (the above). Specifically, they don't build with any of the above by default. Abseil alone was contributing ~314KiB of unwind sections before I got all of them.
  • LTO mode is really tricky to use and is largely incompatible with static archives use in whole archive regions. I expect that for when it matters, we are going to want dedicated targets that produce LTO binaries and only depend on LTO inputs. Especially with the LLVM and Abseil deps, this is really hard to get working globally.

Current sizes:

I measured hacked builds of the following configurations:

  • No HAL modules (base): 699KiB stripped binary size (>1.5MiB prior to build optimizations)
  • Only the Vulkan HAL module (vulkan): 972KiB stripped binary size
  • Only VMLA module (vmla): 987KiB stripped binary size
  • Vulkan+VMLA modules (vulkan_vmla): 1.2MiB stripped binary size

I used Josh's bloaty tool to get some size profiles (attached for each) with commands like: bloaty iree-run-module -n 0 -d sections,symbols -s vm

base_bloaty.txt
vulkan_bloaty.txt
vmla_bloaty.txt
vulkan_vmla_bloaty.txt

I had to hack on the build so much to even get these that it is not reproducible. Thanks to Marius for getting the CMake build to this point -- which made this possible!

Activity

  1. stellaraccident commented on Mar 1, 2020

    @stellaraccident
    CollaboratorAuthor

    Note also that I couldn't get the LLVMJit compiling with these optimizations. Without them, it was weighing in at ~13MiB.

  2. GMNGeoffrey commented on Mar 1, 2020

    @GMNGeoffrey
    Member

    If all we care about is the runtime, I think we should be looking at iree-run-module instead of iree-run-mlir

  3. stellaraccident commented on Mar 1, 2020

    @stellaraccident
    CollaboratorAuthor

    Yes, that's what I was looking at (updated the typo in the issue that was referring to the wrong one). It is virtually impossible to confuse the two: iree-run-mlir is 10x larger.

  4. GMNGeoffrey commented on Mar 1, 2020

    @GMNGeoffrey
    Member

    Yes, that's what I was looking at (updated the typo in the issue that was referring to the wrong one). It is virtually impossible to confuse the two: iree-run-mlir is 10x larger.

    Ok good :-D

  5. benvanik commented on Mar 1, 2020

    @benvanik
    Collaborator

    Yeah, we'll need to get a build of an executable that does not use absl flags/the buffer string parsing/etc. I'm also leaning more and more towards our own status that lets us easily strip things out; so much of the long tail in these is random status formatting junk.

    I'm surprised that the packing dispatches aren't getting deduped. That's a shockingly large amount of code. I'll have to look into that.

  6. stellaraccident commented on Mar 1, 2020

    @stellaraccident
    CollaboratorAuthor

    I'm surprised that the packing dispatches aren't getting deduped. That's a shockingly large amount of code. I'll have to look into that.

    That was the easter egg I left in there so I knew you opened the files :)

  7. stellaraccident commented on Mar 1, 2020

    @stellaraccident
    CollaboratorAuthor

    Yeah, we'll need to get a build of an executable that does not use absl flags/the buffer string parsing/etc. I'm also leaning more and more towards our own status that lets us easily strip things out; so much of the long tail in these is random status formatting junk.

    Agreed. But the build surgery needed is a bit gross -- and can be done on iree-run-module as easily on a new binary.

  8. benvanik commented on Mar 1, 2020

    @benvanik
    Collaborator

    wait, but if iree-run-module is basically just flags and buffer parsing, wouldn't we need a new binary to not have flags or buffer parsing? :)

  9. stellaraccident commented on Mar 1, 2020

    @stellaraccident
    CollaboratorAuthor

    wait, but if iree-run-module is basically just flags and buffer parsing, wouldn't we need a new binary to not have flags or buffer parsing? :)

    This might be a good time to create that C-simple API we've discussed, with a couple of easy entry points and package it up as a shared library. It'd be a good size test and something we need anyway.

  10. benvanik commented on Mar 2, 2020

    @benvanik
    Collaborator

    Since I only understand things when boxes are involved, here's iree-run-module on Windows (release build with optimize-for-speed):
    image

    Of note:

    • 600KB of STL/etc
    • 200+KB for absl str_format & co (!)
    • 60KB for absl::Mutex
    • 100KB for InlinedVector inlined calls (likely inlined usage for all callsites)
    • 130KB for statusor (also likely inlined)
    • 70KB for buffer_string_util (the input/output buffer parsing)
    • 100KB for flags
    • 180KB for VMA (may be ways to get this smaller, there are a lot of flags)
    • 35KB for Ruy (in whatever config tit's compiled in)
    • VMLA+host thread queue with all kernels is 250KB, Vulkan+VMA is 380KB

    Unfortunately the STL stuff is busted in the tool (probably space in file path), but looking at the symbolsort dump it looks like:

    • 205KB for std::vector
    • 80KB for memory
    • 60KB for functional (std::function/bind/etc)
    • probably some big annoying timezone table crap

    Zooms of absl:
    image

    Zoom of IREE:
    image

    I think there's likely some linker settings that could help here as 7zip takes the exe from 890KB to 300KB - so there's roughly ~300KB of useful unique data in there. I also haven't tried linking the CRT in shared library mode.

    So, thinking:

    • 12KB for the core VM interpreter is pretty nice
    • ~200-300KB per HAL backend seems reasonable
    • 800+KB for absl::InlinedVector/Mutex/string formatting seems excessive; may be time to excise more of that in favor of types that generate smaller code
    • 200KB for all the status related machinery seems excessive, and more reason to go with the one I'm working on
    • the 150KB of module_abi_packing is likely all the strings and error handling I just added - making that configurable would be nice (debug only/etc)
  11. benvanik commented on Mar 2, 2020

    @benvanik
    Collaborator

    symbolsort dump:
    iree-run-module.txt

  12. stellaraccident commented on Mar 2, 2020

    @stellaraccident
    CollaboratorAuthor
    • 12KB for the core VM interpreter is pretty nice
    • ~200-300KB per HAL backend seems reasonable
    • 800+KB for absl::InlinedVector/Mutex/string formatting seems excessive; may be time to excise more of that in favor of types that generate smaller code

    We need to see this in a more minimal API-only build, I think, in order to make judgments. Dumping absl::Mutex, though, probably should be done regardless -- it's main value proposition is with respect to interop with some of Google's infra, which doesn't apply here. I've always maintained that for distribution to various devices, you want to use the synchronization mechanisms that the platform provides unless if it matters for specific code, in which case you want to be very opinionated about your choice and not just rely on another black box implementation not optimized towards what you want (which is what absl::Mutex is).

    I believe that most of the bulky string/timezone stuff is coming in from either status, logging or the main() parts of iree-run-module which wouldn't go in an API build.

    Also, keep in mind that unless if you did something, I think that ABSL is compiling with exceptions enabled and full unwind tables. In my early trials, this seemed to be causing ABSL to appear much larger than it actually was (relative to the other artifacts). Probably also defeats some deduping inlining and such due to ABI mismatches. Once I got that fixed and LTO enabled, the picture seemed more reasonable. I think the next step is to duplicate these measurements with a more minimal binary and making sure to have the flags set right on all targets.

    • 200KB for all the status related machinery seems excessive, and more reason to go with the one I'm working on

    +1 - we knew this was coming :)

    • the 150KB of module_abi_packing is likely all the strings and error handling I just added - making that configurable would be nice (debug only/etc)

    Not worried about it for debug features that we can/should be disabling with compile options. It would be good to verify.

    I also noticed on the Linux build that function/data sections and gc are not enabled. We should make a pass through and tune the flags -- they basically have had no diligence put on them yet.

    From my perspective, the next steps are something like this (I wouldn't put any of this at the top of the list, at the moment, as I'd rather have the core working well vs playing flag-golf for last mile build tuning):

    1. Create a simple C-API library that knows how to a) initialize a context, b) load modules, c) invoke functions. I think there should be a really simple API for this (regardless of access to the lower-level, higher strength APIs).
    2. Make sure that we have a place where we can set compile/link flags for all runtime parts of the CMake build (I may not have been doing it right, but I spent significant time trying to figure out why common-sense settings like CXXFLAGS or the requisite CMake var equivalents were not making their way to some targets).
    3. Make sure that we can specify flags impacting the runtime components independent from the compiler and tools. I'm not sure how LLVM does it, but the way they treat tablegen as "special" so that it can be built optimized, even in an overall debug build is a variant of what I think we need here.
    4. Make sure we have CMake options for conditionally compiling out verbose error handling, logging and status code (this will help us better estimate the "floor" for a lot of the string and other utilities that are just along for the ride in a verbose build).
    5. Do some of the hygienic replacements you mention above. We don't need a lot of data to know that some of those things are not worth the bloat value.
  13. benvanik commented on Mar 2, 2020

    @benvanik
    Collaborator

    Yeah, I'll get the status/error stuff (#265) done and that'll get us a better baseline. I can also work on the minimal C API this week. Exceptions are on here but MSVC does a better job with them (hence my 800KB vs your 1.2MB), but still would be good to disable.

    iree_copts.cmake should be where everything is set - if things aren't respecting them we should fix that. CMake does have a loose concept of host vs target that we can use as well.

    I want us to be able to use user-provided types in most cases (particularly mutex and logging) and part of the work of factoring that out will let us either use abseil types inside of google and std/pthread/win32 elsewhere (and then let us more easily be adapted). Similar to what VMA does for external mutex support.

  14. 25 remaining items

  15. GMNGeoffrey commented on Aug 14, 2020

    @GMNGeoffrey
    Member

    As part of this work we should set up a CI build for a minimum-size build (IREE_STATUS_MODE=0, IREE_LOGGING_MODE=0, etc).

    I have added that to my TODO list (which is currently a literal piece of paper on my desk :-D). I intend to write down all these sorts of tasks and put them somewhere they can be easily picked off, but that probably won't happen for a couple weeks realistically.

  16. benvanik commented on Aug 29, 2020

    @benvanik
    Collaborator

    Update from a local branch where I'm cleaning up calling conventions/FFI. This reduces the module_abi_packing.h overhead by about half and shrinks small things in many other areas too (a few hundred of the thousand papercuts).

    The size benchmark (full bytecode VM + flatbuffer module + flatbuffer verification) shows 25KB total + 3KB of CRT:
    image

    iree-run-module (VM, HAL, dylib backend, and string buffer parsing/formatting utils) is looking good too:
    image

    This is still pulling in absl flags (which then pulls in synchronization and time utils) totaling 100KB + STL bits and using some non-prod HAL features and file IO (roughly 30-35KB). I'll see about creating a size benchmark without those things. This puts us at around 160-180KB for a full IREE CPU runtime (on x64, which is pretty good). I'll continue iterating to reduce module_abi_packing.h, though an alternative (or additional) solution is to port the hal_module to C (which is good for portability and it's just a C -> C++ -> C so there's no need for it to be C++).

  17. benvanik commented on Aug 29, 2020

    @benvanik
    Collaborator

    No quick fixes for module_abi_packing unfortunately - I tried to do some extern template magic, however LTO takes care of whatever I was able to do with that anyway. That does mean that if more than a single module is using it we'll get the cost amortized across all of them for a lot of cases though it does unfortunately set the min size for the entire binary. The only way to make that smaller is to switch to C and rely on cross-procedure optimizations (maybe I'll try that next friday, as it'll cause me to JIT design some nice C ABI utilities).

  18. benvanik commented on Nov 22, 2020

    @benvanik
    Collaborator

    I think a good majority of the remaining improvements are tracked more specifically in other issues (#3848, #3814, #2843, #3843, etc).

    Filed #3963 to track getting binary size CI logging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

infrastructureruntime

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions