Repository navigation
Description
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
stellaraccident commentedon Mar 1, 2020
Note also that I couldn't get the LLVMJit compiling with these optimizations. Without them, it was weighing in at ~13MiB.
GMNGeoffrey commentedon Mar 1, 2020
If all we care about is the runtime, I think we should be looking at iree-run-module instead of iree-run-mlir
stellaraccident commentedon Mar 1, 2020
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.
GMNGeoffrey commentedon Mar 1, 2020
Ok good :-D
benvanik commentedon Mar 1, 2020
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.
stellaraccident commentedon Mar 1, 2020
That was the easter egg I left in there so I knew you opened the files :)
stellaraccident commentedon Mar 1, 2020
Agreed. But the build surgery needed is a bit gross -- and can be done on iree-run-module as easily on a new binary.
benvanik commentedon Mar 1, 2020
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? :)
stellaraccident commentedon Mar 1, 2020
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.
benvanik commentedon Mar 2, 2020
Since I only understand things when boxes are involved, here's iree-run-module on Windows (release build with optimize-for-speed):

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

Zoom of IREE:

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:
benvanik commentedon Mar 2, 2020
symbolsort dump:
iree-run-module.txt
stellaraccident commentedon Mar 2, 2020
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.
+1 - we knew this was coming :)
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):
benvanik commentedon Mar 2, 2020
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.
25 remaining items
Adding option to start fleshing out size-optimized flags for #898.
Adding option to start fleshing out size-optimized flags for #898.
Adding option to start fleshing out size-optimized flags for #898.
Adding option to start fleshing out size-optimized flags for #898.
GMNGeoffrey commentedon Aug 14, 2020
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.
Adding option to start fleshing out size-optimized flags for #898.
benvanik commentedon Aug 29, 2020
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:

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

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++).
benvanik commentedon Aug 29, 2020
No quick fixes for module_abi_packing unfortunately - I tried to do some
extern templatemagic, 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).Move Vulkan HAL flags into vulkan_driver_module.cc. (#3527)
benvanik commentedon Nov 22, 2020
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.
Enable LTO optimization by default for runtime releases.
Enable LTO optimization by default for runtime releases.
Enable LTO optimization by default for runtime releases. (#16811)
Enable LTO optimization by default for runtime releases. (#16811)