rust-lang / rustc_codegen_gcc

libgccjit AOT codegen for rustc
Apache License 2.0
908 stars 60 forks source link

Binary size when compiling with rustc_codegen_gcc #52

Open gui2maraes opened 3 years ago

gui2maraes commented 3 years ago

I tried compiling the default "Hello, world!" program with rustc_codegen_gcc, and noticed the resulting binary has a size of ~20MB, opposed to the default rustc ~3MB binary. I don't know if it has something to do with optimization, or static libraries.

I also tried building the binary with --release, and got an ICE due to LTO not being implemented. Are these two things related?

bjorn3 commented 3 years ago

The standard library is probably not being compiled with optimizations. I think ./build.sh --release will compile both cg_gcc and the standard library with optimizations. cg_clif for example which doesn't do optimizations like inlining, but does take much care to produce locally efficient code, has a hello world of 12MB. The cg_ssa backend framework produces less efficient code and instead requires an optimizer to run to eliminate those inefficiencies.

bjorn3 commented 3 years ago

As for the LTO crash adding the following to the Cargo.toml of your hello world project will likely fix it:

[profile.release]
lto = false

It may be necessary to add lto = false here to make ./build.sh --release work. I haven't tried though.

https://github.com/antoyo/rustc_codegen_gcc/blob/360b8deec47272f1d4c981e6f088790b5228c531/build_sysroot/Cargo.toml#L18-L19

gui2maraes commented 3 years ago

I'm kind of lost here. How is the standard library linked? I only found libstd.so files, and I don't think rustc recompiles the stdlib for every binary. Is it stored as separate components?

Also, what is sysroot, and why does it need to be patched and recompiled?

bjorn3 commented 3 years ago

The standard library is statically linked by default and not dynamically linked. See the *.rlib files. The sysroot is the collection of all the crates that form the standard library and various supporting crates like compiler_builtins or profiler_runtime. It needs to be recompiled by cg_gcc as cg_gcc is not abi compatible with cg_llvm and thus trying to link to the cg_llvm built standard library would cause a crash.

gui2maraes commented 3 years ago

Ok, think I understand now. Should I close the issue?

antoyo commented 3 years ago

To be honest, I've seen those huge binaries as well. So, let's keep this issue open for now.