Skip to content

Support -Zfunction-sections #118

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

Merged
merged 1 commit into from
Jan 26, 2022
Merged

Conversation

bjorn3
Copy link
Member

@bjorn3 bjorn3 commented Jan 25, 2022

This puts every function and data object in their own section. This allows the linker to omit unused functions and data objects with --gc-sections.

On linux this shrinks a hello world binary without optimizations (neither sysroot nor binary) from 17MB to 13MB. It shrinks a hello world binary with only sysroot optimizations from 14MB to 13MB. For comparison cg_llvm produces a 3.5MB debug mode hello world binary with an optimized sysroot. Cg_clif produces a 10MB debug mode hello world binary without an optimized sysroot.

Fixed #117

Copy link
Contributor

@antoyo antoyo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 25, 2022

While it helps, it helps not nearly as much as I would have hoped. Even with optimizations the executable is bigger than with cg_clif which doesn't do optimizations.

@bjorn3
Copy link
Member Author

bjorn3 commented Jan 25, 2022

---- slice::test_array_chunks_count stdout ----
---- slice::test_array_chunks_count stderr ----
thread 'main' panicked at 'attempt to create unaligned or null slice', /home/runner/work/rustc_codegen_gcc/rustc_codegen_gcc/build_sysroot/sysroot_src/library/core/src/slice/raw.rs:141:9
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: core::slice::raw::debug_check_data_len::rt_check::<[i32; 2]>
   4: <core::slice::raw::debug_check_data_len::rt_check<[i32; 2]> as core::ops::function::FnOnce<(*const [i32; 2],)>>::call_once
   5: core::intrinsics::const_eval_select::<(*const [i32; 2],), core::slice::raw::debug_check_data_len::noop<[i32; 2]>, core::slice::raw::debug_check_data_len::rt_check<[i32; 2]>, ()>
   6: core::slice::raw::debug_check_data_len::<[i32; 2]>
   7: core::slice::raw::from_raw_parts::<[i32; 2]>
   8: <[i32]>::as_chunks_unchecked::<2>
   9: <[i32]>::as_chunks::<2>
  10: <core::slice::iter::ArrayChunks<i32, 2>>::new
  11: <[i32]>::array_chunks::<2>
  12: coretests::slice::test_array_chunks_count
  13: coretests::slice::test_array_chunks_count::{closure#0}
  14: <coretests::slice::test_array_chunks_count::{closure#0} as core::ops::function::FnOnce<()>>::call_once
  15: <fn() as core::ops::function::FnOnce<()>>::call_once
  16: <fn() as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  17: <alloc::boxed::Box<dyn core::ops::function::FnOnce<(), Output = ()> + core::marker::Send> as core::ops::function::FnOnce<()>>::call_once
  18: test::run_test_in_spawned_subprocess
  19: test::test_main_static_abort
  20: coretests::main
  21: <fn() as core::ops::function::FnOnce<()>>::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I think this perturbed the test executable just enough to expose an existing bug.

@antoyo
Copy link
Contributor

antoyo commented Jan 25, 2022

You already found a similar bug a few weeks ago. You had commented the test for unaligned slice.

I started working on this but wasn't able to fix the issue yet. Somehow, the global variable I needed to be aligned on 4 bytes is aligned on 1 byte because that's what rustc says. So, I still need to dig into this to figure out what's happening.

@antoyo
Copy link
Contributor

antoyo commented Jan 26, 2022

Should be fixed in #121. The branches in my gcc fork are currently a mess. I'll fix the master branch if needed.

@antoyo
Copy link
Contributor

antoyo commented Jan 26, 2022

I had forgotten to build the libgccjit artifact. Now it's done and the core tests pass.

This puts every function and data object in their own section. This
allows the linker to omit unused functions and data objects with
--gc-sections.

On linux this shrinks a hello world binary without optimizations
(neither sysroot nor binary) from 17MB to 13MB. It shrinks a hello world
binary with only sysroot optimizations from 14MB to 13MB. For comparison
cg_llvm produces a 3.5MB debug mode hello world binary with an optimized
sysroot. Cg_clif produces a 10MB debug mode hello world binary without
an optimized sysroot.
@bjorn3 bjorn3 force-pushed the function_sections branch from 7f778e9 to 99941cd Compare January 26, 2022 14:14
@bjorn3
Copy link
Member Author

bjorn3 commented Jan 26, 2022

Rebased to trigger CI.

@antoyo antoyo merged commit b7bfb21 into rust-lang:master Jan 26, 2022
@bjorn3 bjorn3 deleted the function_sections branch January 26, 2022 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support -Zfunction-sections
2 participants