Skip to content

Commit 58ab4a0

Browse files
committed
rustc: Enable -f{function,data}-sections
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 ( 292872 => 299508) libcollections - 0.64% bigger ( 6765884 => 6809076) libflate - 0.83% bigger ( 186516 => 188060) libfourcc - 14.71% bigger ( 307290 => 352498) libgetopts - 4.42% bigger ( 761468 => 795102) libglob - 2.73% bigger ( 899932 => 924542) libgreen - 9.63% bigger ( 1281718 => 1405124) libhexfloat - 13.88% bigger ( 333738 => 380060) liblibc - 10.79% bigger ( 551280 => 610736) liblog - 10.93% bigger ( 218208 => 242060) libnative - 8.26% bigger ( 1362096 => 1474658) libnum - 2.34% bigger ( 2583400 => 2643916) librand - 1.72% bigger ( 1608684 => 1636394) libregex - 6.50% bigger ( 1747768 => 1861398) librustc - 4.21% bigger (151820192 => 158218924) librustdoc - 8.96% bigger ( 13142604 => 14320544) librustuv - 4.13% bigger ( 4366896 => 4547304) libsemver - 2.66% bigger ( 396166 => 406686) libserialize - 1.91% bigger ( 6878396 => 7009822) libstd - 3.59% bigger ( 39485286 => 40902218) libsync - 3.95% bigger ( 1386390 => 1441204) libsyntax - 4.96% bigger ( 35757202 => 37530798) libterm - 13.99% bigger ( 924580 => 1053902) libtest - 6.04% bigger ( 2455720 => 2604092) libtime - 2.84% bigger ( 1075708 => 1106242) liburl - 6.53% bigger ( 590458 => 629004) libuuid - 4.63% bigger ( 326350 => 341466) libworkcache - 8.45% bigger ( 1230702 => 1334750) 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.
1 parent a1ad41b commit 58ab4a0

File tree

8 files changed

+38
-8
lines changed

8 files changed

+38
-8
lines changed

src/librustc/back/link.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ 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+
// FIXME(#13846) this should be enabled for windows
157+
let ffunction_sections = sess.targ_cfg.os != abi::OsMacos &&
158+
sess.targ_cfg.os != abi::OsWin32;
159+
let fdata_sections = ffunction_sections;
160+
155161
let reloc_model = match sess.opts.cg.relocation_model.as_slice() {
156162
"pic" => lib::llvm::RelocPIC,
157163
"static" => lib::llvm::RelocStatic,
@@ -173,9 +179,11 @@ pub mod write {
173179
lib::llvm::CodeModelDefault,
174180
reloc_model,
175181
opt_level,
176-
true,
182+
true /* EnableSegstk */,
177183
use_softfp,
178-
no_fp_elim
184+
no_fp_elim,
185+
ffunction_sections,
186+
fdata_sections,
179187
)
180188
})
181189
})
@@ -1136,16 +1144,22 @@ fn link_args(sess: &Session,
11361144
args.push("-nodefaultlibs".to_owned());
11371145
}
11381146

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

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.
1161+
// GNU-style linkers support optimization with -O. GNU ld doesn't need a
1162+
// numeric argument, but other linkers do.
11491163
if sess.opts.optimize == session::Default ||
11501164
sess.opts.optimize == session::Aggressive {
11511165
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

src/test/debug-info/basic-types-globals-metadata.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ static F64: f64 = 3.5;
6666

6767
fn main() {
6868
_zzz();
69+
70+
let a = (B, I, C, I8, I16, I32, I64, U, U8, U16, U32, U64, F32, F64);
6971
}
7072

7173
fn _zzz() {()}

src/test/debug-info/basic-types-globals.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ static F64: f64 = 3.5;
7070

7171
fn main() {
7272
_zzz();
73+
74+
let a = (B, I, C, I8, I16, I32, I64, U, U8, U16, U32, U64, F32, F64);
7375
}
7476

7577
fn _zzz() {()}

src/test/debug-info/basic-types-metadata.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn main() {
6767
let f32: f32 = 2.5;
6868
let f64: f64 = 3.5;
6969
_zzz();
70+
if 1 == 1 { _yyy(); }
7071
}
7172

7273
fn _zzz() {()}

src/test/debug-info/c-style-enum.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ fn main() {
121121
};
122122

123123
zzz();
124+
125+
let a = SINGLE_VARIANT;
126+
let a = unsafe { AUTO_ONE };
127+
let a = unsafe { MANUAL_ONE };
124128
}
125129

126130
fn zzz() {()}

src/test/debug-info/limited-debuginfo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct Struct {
3838

3939
fn main() {
4040
some_function(101, 202);
41+
some_other_function(1, 2);
4142
}
4243

4344

0 commit comments

Comments
 (0)