Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Roadmap to minimize libtaichi_c_api.so library size #6793

Closed
jim19930609 opened this issue Dec 2, 2022 · 2 comments
Closed

[RFC] Roadmap to minimize libtaichi_c_api.so library size #6793

jim19930609 opened this issue Dec 2, 2022 · 2 comments
Assignees
Labels
doc Documentation related issues & PRs

Comments

@jim19930609
Copy link
Contributor

jim19930609 commented Dec 2, 2022

We roughly planed three stages to optimize the library size for libtaichi_c_api.so:

  • Function level linking
  • Symbol rearrangement and clean up
  • Customized C-API features pruning

1. Turn on function-level linking

1.1 Convert CMake OBJECT targets to STATIC targets

Removes unnecessary entry files that compiles into libtaichi_c_api.so

[Before]
image

[After]
image

1.2 Apply version script to libtaichi_c_api.so

Convert unnecessary global symbols to local symbols, leading to more optimization opportunities.
[Before]
image

[After]
image

1.3 Add compile option -ffunction-sections, -fdata-sections and link option --gc-sections

Turns on function level linking eventually.
[Before]
image

[After]
image

Based on some experiment results, after turning on function-level-linking, we're expecting ~80% code size reduction from 12 MB to 1.7 MB (Vulkan C-API library, #6388)

2. Symbol rearrangement and clean up

Further detect and clean up redundant symbols via symbol analysis (tooling: https://github.com/google/bloaty)

Symbol class Symbol keyword Size / KB Side notes / KB
libtaichi_c_api.so [Total Size] 1700  
 
RTTI + vtable "vtable for" 27  
RTTI + vtable "typeinfo name for" 9  
RTTI + vtable "typeinfo for" 16  
  [Sum] 52  
 
Vulkan "vkapi::" 39 [std::_Sp_counted_ptr]: 5[DeviceObjVk]: 23[vkapi::create_xxxx]: 11
Vulkan "taichi::lang::vulkan" 173 [VulkanStream]: 11[VulkanDevice]: 69[VulkanSurface]: 10[VulkanCommandList]: 32[VulkanResourceBinder]: 16[VulkanPipeline]: 18[VulkanRenderPass]: 3[Helper Functions]: 12
Vulkan "vma" 3 [external] VulkanMemoryAllocatorvmaAllocateMemoryForBuffervmaAllocateMemoryForImage
Vulkan "Vma" 60 [VmaBlockVector]: 8[VmaBlockMetadata]: 26[VmaAllocator_T]: 18[VmaVector + VmaPoolAllocator + VmaJsonWriter]: 8
Vulkan "volk" 29 [external] volkvolkGenLoadInstance
Vulkan "taichi::lang::gfx" 43  
  [Sum] 350  
 
SpirV "taichi::lang::spirv" 24 [Internal]
SpirV "spv" 11 [external] SPIRV-Tools
SpirV "spdlog::" 84 [external] spdlogspdlog::details::R_formatter
  [Sum] 119  
 
Taichi "taichi::" 90 [taichi::BinarySerializer]: 18[taichi::zip]: 6[taichi::lang::aot]: 19[Ndarray]: 3[Type]: 15[Device]: 9[Logger]: 4[Rest]: 12
 
Redundant "fmt::v6" 112 [external]
Redundant "tinfl" 8 [external] miniz (compression)
Redundant "glfw" 114 [external] glfw
Redundant "mz_" 8 [external] miniz (compression)
  [Sum] 242  
 
Standard Library "std::" 20 Standard library
Standard Library "_GLOBAL__sub_I" 47 Iostream related
  [Sum] 67  
 
       
Sections .rodata 178 const strings
Sections .gcc_exept_table 456 Exception handles
Sections .rela.dyn 13.7
Sections .dynstr 4.57
Sections .dynsym 4.64
Sections ... ...  
  [Sum] 673  
       
CAPI [Sum] 25  
Rest [Sum] 63 Third-party libraries[spirv_reflect] ParseDescriptorBlockVariable

3. Customized C-API features

In certain situations, library size can be the killing blocker to ship a product. Therefore we should provide options to further remove unimportant features to further reduce the library size. Some possible options are:

  1. Remove C++ exceptions handling and turn on fno-except
  2. Remove RTTI information and turn on fno-rtti
  3. Prune useless C-API interfaces to the user.
@ailzhang
Copy link
Contributor

ailzhang commented Dec 5, 2022

cc: @ghuau-innopeak

@jim19930609
Copy link
Contributor Author

jim19930609 commented Dec 9, 2022

After turning on function level linking for libtaichi_c_api.so with #6840 as well as stripping the debug info with #6845, significant code size reduction was observed:

  Size (Original) Size (Optimized)
[Linux] Vulkan 14 MB 1.5 MB
[Android] Vulkan 14 MB 1.6 MB
[Linux] Vulkan + LLVM 82 MB 51 MB

libtaichi_c_api.so built with LLVM does not benefit that much from function level linking, mainly because libLLVM.a is not compiled with -ffunction-sections -fdata-sections. We'll have that fixed later.

TODO List:

  1. Recompile third-party libraries with -ffunction-sections -fdata-sections, especially LLVM
  2. Turn on function level linking for taichi_python.so as well, which has two prerequisite tasks:

jim19930609 added a commit that referenced this issue Dec 13, 2022
jim19930609 added a commit that referenced this issue Dec 13, 2022
Issue: #6793

  | Size (Original) | Size (Optimized)
-- | -- | --
Vulkan | 14 MB | 1.5 MB
Vulkan + LLVM | 82 MB | 51 MB

libtaichi_c_api.so built with LLVM does not benefit much from function
level linking, mainly because libLLVM.a is not compiled with
`-ffunction-sections -fdata-sections`. We'll have that fixed later.
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
quadpixels pushed a commit to quadpixels/taichi that referenced this issue May 13, 2023
…i-dev#6845)

Issue: taichi-dev#6793

  | Size (Original) | Size (Optimized)
-- | -- | --
Vulkan | 14 MB | 1.5 MB
Vulkan + LLVM | 82 MB | 51 MB

libtaichi_c_api.so built with LLVM does not benefit much from function
level linking, mainly because libLLVM.a is not compiled with
`-ffunction-sections -fdata-sections`. We'll have that fixed later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Documentation related issues & PRs
Projects
Status: Done
Development

No branches or pull requests

2 participants