Skip to content

Commit 7c1187b

Browse files
committed
auto merge of #13833 : alexcrichton/rust/ffunction-sections, r=thestinger
The compiler has previously been producing binaries on the order of 1.8MB for hello world programs "fn main() {}". This is largely a result of the compilation model used by compiling entire libraries into a single object file and because static linking is favored by default. When linking, linkers will pull in the entire contents of an object file if any symbol from the object file is used. This means that if any symbol from a rust library is used, the entire library is pulled in unconditionally, regardless of whether the library is used or not. Traditional C/C++ projects do not normally encounter these large executable problems because their archives (rust's rlibs) are composed of many objects. Because of this, linkers can eliminate entire objects from being in the final executable. With rustc, however, the linker does not have the opportunity to leave out entire object files. In order to get similar benefits from dead code stripping at link time, this commit enables the -ffunction-sections and -fdata-sections flags in LLVM, as well as passing --gc-sections to the linker *by default*. This means that each function and each global will be placed into its own section, allowing the linker to GC all unused functions and data symbols. By enabling these flags, rust is able to generate much smaller binaries default. On linux, a hello world binary went from 1.8MB to 597K (a 67% reduction in size). The output size of dynamic libraries remained constant, but the output size of rlibs increased, as seen below: libarena - 2.27% bigger libcollections - 0.64% bigger libflate - 0.85% bigger libfourcc - 14.67% bigger libgetopts - 4.52% bigger libglob - 2.74% bigger libgreen - 9.68% bigger libhexfloat - 13.68% bigger liblibc - 10.79% bigger liblog - 10.95% bigger libnative - 8.34% bigger libnum - 2.31% bigger librand - 1.71% bigger libregex - 6.43% bigger librustc - 4.21% bigger librustdoc - 8.98% bigger librustuv - 4.11% bigger libsemver - 2.68% bigger libserialize - 1.92% bigger libstd - 3.59% bigger libsync - 3.96% bigger libsyntax - 4.96% bigger libterm - 13.96% bigger libtest - 6.03% bigger libtime - 2.86% bigger liburl - 6.59% bigger libuuid - 4.70% bigger libworkcache - 8.44% bigger This increase in size is a result of encoding many more section names into each object file (rlib). These increases are moderate enough that this change seems worthwhile to me, due to the drastic improvements seen in the final artifacts. The overall increase of the stage2 target folder (not the size of an install) went from 337MB to 348MB (3% increase). Additionally, linking is generally slower when executed with all these new sections plus the --gc-sections flag. The stage0 compiler takes 1.4s to link the `rustc` binary, where the stage1 compiler takes 1.9s to link the binary. Three megabytes are shaved off the binary. I found this increase in link time to be acceptable relative to the benefits of code size gained. This commit only enables --gc-sections for *executables*, not dynamic libraries. LLVM does all the heavy lifting when producing an object file for a dynamic library, so there is little else for the linker to do (remember that we only have one object file). I conducted similar experiments by putting a *module's* functions and data symbols into its own section (granularity moved to a module level instead of a function/static level). The size benefits of a hello world were seen to be on the order of 400K rather than 1.2MB. It seemed that enough benefit was gained using ffunction-sections that this route was less desirable, despite the lesser increases in binary rlib size.
2 parents 30e3733 + 957e695 commit 7c1187b

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

src/librustc/back/link.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ pub mod write {
152152
(sess.targ_cfg.os == abi::OsMacos &&
153153
sess.targ_cfg.arch == abi::X86_64);
154154

155+
// OSX has -dead_strip, which doesn't rely on ffunction_sections
156+
let ffunction_sections = sess.targ_cfg.os != abi::OsMacos;
157+
let fdata_sections = ffunction_sections;
158+
155159
let reloc_model = match sess.opts.cg.relocation_model.as_slice() {
156160
"pic" => lib::llvm::RelocPIC,
157161
"static" => lib::llvm::RelocStatic,
@@ -173,9 +177,11 @@ pub mod write {
173177
lib::llvm::CodeModelDefault,
174178
reloc_model,
175179
opt_level,
176-
true,
180+
true /* EnableSegstk */,
177181
use_softfp,
178-
no_fp_elim
182+
no_fp_elim,
183+
ffunction_sections,
184+
fdata_sections,
179185
)
180186
})
181187
})
@@ -1136,16 +1142,22 @@ fn link_args(sess: &Session,
11361142
args.push("-nodefaultlibs".to_owned());
11371143
}
11381144

1145+
// If we're building a dylib, we don't use --gc-sections because LLVM has
1146+
// already done the best it can do, and we also don't want to eliminate the
1147+
// metadata. If we're building an executable, however, --gc-sections drops
1148+
// the size of hello world from 1.8MB to 597K, a 67% reduction.
1149+
if !dylib && sess.targ_cfg.os != abi::OsMacos {
1150+
args.push("-Wl,--gc-sections".to_owned());
1151+
}
1152+
11391153
if sess.targ_cfg.os == abi::OsLinux {
11401154
// GNU-style linkers will use this to omit linking to libraries which
11411155
// don't actually fulfill any relocations, but only for libraries which
11421156
// follow this flag. Thus, use it before specifying libraries to link to.
11431157
args.push("-Wl,--as-needed".to_owned());
11441158

1145-
// GNU-style linkers support optimization with -O. --gc-sections
1146-
// removes metadata and potentially other useful things, so don't
1147-
// include it. GNU ld doesn't need a numeric argument, but other linkers
1148-
// do.
1159+
// GNU-style linkers support optimization with -O. GNU ld doesn't need a
1160+
// numeric argument, but other linkers do.
11491161
if sess.opts.optimize == session::Default ||
11501162
sess.opts.optimize == session::Aggressive {
11511163
args.push("-Wl,-O1".to_owned());

src/librustc/lib/llvm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1748,7 +1748,9 @@ pub mod llvm {
17481748
Level: CodeGenOptLevel,
17491749
EnableSegstk: bool,
17501750
UseSoftFP: bool,
1751-
NoFramePointerElim: bool) -> TargetMachineRef;
1751+
NoFramePointerElim: bool,
1752+
FunctionSections: bool,
1753+
DataSections: bool) -> TargetMachineRef;
17521754
pub fn LLVMRustDisposeTargetMachine(T: TargetMachineRef);
17531755
pub fn LLVMRustAddAnalysisPasses(T: TargetMachineRef,
17541756
PM: PassManagerRef,

src/rustllvm/PassWrapper.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ LLVMRustCreateTargetMachine(const char *triple,
6969
CodeGenOpt::Level OptLevel,
7070
bool EnableSegmentedStacks,
7171
bool UseSoftFloat,
72-
bool NoFramePointerElim) {
72+
bool NoFramePointerElim,
73+
bool FunctionSections,
74+
bool DataSections) {
7375
std::string Error;
7476
Triple Trip(Triple::normalize(triple));
7577
const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Trip.getTriple(),
@@ -97,6 +99,8 @@ LLVMRustCreateTargetMachine(const char *triple,
9799
RM,
98100
CM,
99101
OptLevel);
102+
TM->setDataSections(DataSections);
103+
TM->setFunctionSections(FunctionSections);
100104
return wrap(TM);
101105
}
102106

0 commit comments

Comments
 (0)