tqchen opened a new pull request, #588:
URL: https://github.com/apache/tvm-ffi/pull/588

   A binary-layout audit of libtvm_ffi.so showed that error-throw helpers 
(ErrorBuilder ctors/dtor, the TVMFFIError* C ABI exports) live in the middle of 
.text, interleaved with hot C ABI dispatch and container code. The entire 
library compiles into a single monolithic .text section because 
-ffunction-sections is not enabled, so the linker has no way to isolate cold 
code into a separate region.
   
   This change introduces a portable TVM_FFI_COLD_CODE attribute and enables 
-ffunction-sections on the object-library targets so the toolchain can place 
error-only code in a separate cold region of .text. With this, ErrorBuilder 
ctor/dtor and the TVMFFIError* exports move into the cold cluster at the head 
of .text (offsets 0x86f2..0xc7fd in the final layout), and the hot 
TVMFFIBacktrace body shifts from 0x32e00 in baseline to 0x47b80 in the final 
build. Stripped libtvm_ffi.so shrinks from 1,887,800 to 1,826,368 bytes 
(-61,432, -3.3%), driven by tighter linker packing once functions live in their 
own sections.
   
   The change has three pieces:
   
   - A TVM_FFI_COLD_CODE macro in base_details.h that expands to [[gnu::cold]] 
on GCC/Clang and a no-op on MSVC, plus TVM_FFI_UNLIKELY / TVM_FFI_LIKELY 
wrappers around __builtin_expect. The macro is applied to ErrorBuilder ctors 
and the [[noreturn]] destructor (error.h), to SafeCallContext setter methods 
and the TVMFFIError* C ABI helpers (error.cc).
   - A new tvm_ffi_enable_section_layout() helper in cmake/Utils/Library.cmake 
that turns on -ffunction-sections (GCC, Clang, AppleClang) or /Gy (MSVC) on 
tvm_ffi_objs and tvm_ffi_extra_objs. --gc-sections is intentionally not enabled 
because tvm-ffi uses static-init registration patterns that the linker cannot 
trace; that is left for a follow-up.
   - TVM_FFI_UNLIKELY applied to a small set of representative error-check 
branches (the TVM_FFI_CHECK_SAFE_CALL macro, the TVM_FFI_CHECK macro, and 
GlobalFunctionTable::Update's already-registered check) to demonstrate the 
pattern without blanket-annotating the codebase.
   
   Numbers, symbol maps, and the rationale for skipping --gc-sections are 
written up in [docs/dev/binary_layout.md](docs/dev/binary_layout.md).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to