anza-xyz / move

Move compiler targeting llvm supported backends
https://discord.gg/wFgfjG9J
Apache License 2.0
107 stars 32 forks source link

[Bug] Reduce the binary size of move program #429

Open ksolana opened 4 months ago

ksolana commented 4 months ago

A simple assert.move program results in 268k of output.so binary. Run the optimizer to reduce binary size.

ksolana commented 3 months ago

The libmove_native is contributing to those symbols in the final binary

$ language/tools/move-mv-llvm-compiler/tests/rbpf-tests/assert-build/scripts/main.o is only 808 bytes

$ llvm-objdump -D language/tools/move-mv-llvm-compiler/tests/rbpf-tests/assert-build/output.so | grep '<.*>:'| cut -f2- -d' ' | sed 's/[<>:]//g'

main
move_rt_abort
move_rt_vec_destroy
move_rt_vec_copy
move_rt_vec_cmp_eq
move_rt_str_cmp_eq
move_rt_struct_cmp_eq
move_rt_deserialize
move_native_bcs_to_bytes
move_native_bcs_test_from_bytes
move_native_debug_print
move_native_print_stack_trace
move_native_event_write_to_event_store
move_native_hash_sha2_256
move_native_hash_sha3_256
move_native_signer_borrow_address
move_native_string_internal_check_utf8
move_native_string_internal_is_char_boundary
move_native_string_internal_sub_string
move_native_string_internal_index_of
move_native_type_name_get
move_native_unit_test_create_signers_for_testing
move_rt_vec_empty
move_native_vector_length
move_native_vector_borrow
move_native_vector_push_back
move_native_vector_borrow_mut
move_native_vector_pop_back
move_native_vector_destroy_empty
move_native_vector_swap
.rodata
.data.rel.ro
.dynamic
.dynsym
.dynstr
.rel.dyn
.comment
.shstrtab
ksolana commented 3 months ago

while the main.o only refers to abort function

// readelf main.o
Symbol table '.symtab' contains 5 entries:
   Num:    Value          Size Type    Bind   Vis       Ndx Name
     0: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT   UND 
     1: 0000000000000000     0 FILE    LOCAL  DEFAULT   ABS <unknown>
     2: 0000000000000000     0 NOTYPE  LOCAL  DEFAULT     2 
     3: 0000000000000000    24 FUNC    GLOBAL DEFAULT     2 main
     4: 0000000000000000     0 NOTYPE  GLOBAL DEFAULT   UND move_rt_abort
ksolana commented 3 months ago
ksolana commented 3 months ago

Oz reduces the size to 208k. Number of symbols are still the same.

--- a/.cargo/config
+++ b/.cargo/config
@@ -12,5 +12,8 @@ nextest = "run --package x --bin x -- nextest"
 [target.x86_64-pc-windows-msvc]
 rustflags = ["-C", "link-arg=/STACK:8000000"]

+[build]
+rustflags = ["-C", "opt-level=z"]        # custom flags to pass to all compiler invocations
+