build: add function/data-section garbage collection#56
Merged
Conversation
Direct response to the r/embedded thread with markrages and fb39ca4: adds -ffunction-sections/-fdata-sections at compile time and the platform-correct dead-stripping flag at link time, so unused functions and data are dropped at function granularity instead of whole-object-file granularity. Real gap this closes: if an application links more than one numx module that needs the same private helper (e.g. fft and autodiff both need trig), each module's private copy was previously kept in full since each lives in its own translation unit with no shared internal header. Handles the platform split properly rather than assuming GNU ld everywhere: Apple's linker doesn't understand --gc-sections and fails outright if you pass it, it uses -dead_strip instead. MSVC uses /Gy plus /OPT:REF /OPT:ICF. GNU ld, lld, and embedded cross-toolchains (arm-none-eabi-gcc, xtensa-esp32s3-elf-gcc) accept --gc-sections normally. Verified, not just compiled: confirmed via nm that a linalg-only binary contains none of NTT's twiddle-factor data tables (real const data, not something that could be silently inlined away), while an NTT-using binary does. Measured a real size reduction on a desktop build; the proportional effect on a bare-metal embedded .elf with no OS/dynamic-loader overhead diluting it would be larger. Full test suite still 335/335 passing. Also fixes project(numx VERSION 0.1.0 ...) on line 2, stale since the v1.0.0 release, noticed while editing the same block.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Direct response to markrages and fb39ca4 on r/embedded, pointed out that per-module code duplication isn't actually the best way to get small binaries, function-section garbage collection does it properly regardless of whether code is shared or duplicated.
Real gap this closes
If an application links more than one numx module that needs the same private helper (fft and autodiff both implement trig independently), each module's copy was kept in full, no way to dedupe or strip what's unused.
Platform handling
Apple's linker doesn't understand
--gc-sectionsand fails outright (verified, this broke the build on first attempt, see commit history on the branch). Uses-dead_stripon Apple,--gc-sectionson GNU ld/lld/embedded cross-toolchains,/Gy+/OPT:REF /OPT:ICFon MSVC.Verification
nmthat a linalg-only binary contains none of NTT's twiddle-factor tables (real const data, can't be inlined away), while an NTT-using binary doesAlso included
Fixed
project(numx VERSION 0.1.0 ...), stale since the v1.0.0 tag, noticed while editing the adjacent block.