Skip to content

Commit 4deb4bc

Browse files
committed
optimize position independent code in executables
Position independent code has fewer requirements in executables, so pass the appropriate flag to LLVM in order to allow more optimization. At the moment this means faster thread-local storage.
1 parent 86509d8 commit 4deb4bc

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/librustc/back/write.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use std::sync::{Arc, Mutex};
3434
use std::task::TaskBuilder;
3535
use libc::{c_uint, c_int, c_void};
3636

37-
3837
#[deriving(Clone, PartialEq, PartialOrd, Ord, Eq)]
3938
pub enum OutputType {
4039
OutputTypeBitcode,
@@ -44,7 +43,6 @@ pub enum OutputType {
4443
OutputTypeExe,
4544
}
4645

47-
4846
pub fn llvm_err(handler: &diagnostic::Handler, msg: String) -> ! {
4947
unsafe {
5048
let cstr = llvm::LLVMRustGetLastError();
@@ -202,6 +200,10 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef {
202200
(sess.targ_cfg.os == abi::OsMacos &&
203201
sess.targ_cfg.arch == abi::X86_64);
204202

203+
let any_library = sess.crate_types.borrow().iter().any(|ty| {
204+
*ty != config::CrateTypeExecutable
205+
});
206+
205207
// OSX has -dead_strip, which doesn't rely on ffunction_sections
206208
// FIXME(#13846) this should be enabled for windows
207209
let ffunction_sections = sess.targ_cfg.os != abi::OsMacos &&
@@ -240,6 +242,7 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef {
240242
true /* EnableSegstk */,
241243
use_softfp,
242244
no_fp_elim,
245+
!any_library && reloc_model == llvm::RelocPIC,
243246
ffunction_sections,
244247
fdata_sections,
245248
)

src/librustc_llvm/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ pub enum CodeGenOptLevel {
353353
CodeGenLevelAggressive = 3,
354354
}
355355

356+
#[deriving(PartialEq)]
356357
#[repr(C)]
357358
pub enum RelocMode {
358359
RelocDefault = 0,
@@ -1907,6 +1908,7 @@ extern {
19071908
EnableSegstk: bool,
19081909
UseSoftFP: bool,
19091910
NoFramePointerElim: bool,
1911+
PositionIndependentExecutable: bool,
19101912
FunctionSections: bool,
19111913
DataSections: bool) -> TargetMachineRef;
19121914
pub fn LLVMRustDisposeTargetMachine(T: TargetMachineRef);

src/rustllvm/PassWrapper.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ LLVMRustCreateTargetMachine(const char *triple,
7171
bool EnableSegmentedStacks,
7272
bool UseSoftFloat,
7373
bool NoFramePointerElim,
74+
bool PositionIndependentExecutable,
7475
bool FunctionSections,
7576
bool DataSections) {
7677
std::string Error;
@@ -83,6 +84,7 @@ LLVMRustCreateTargetMachine(const char *triple,
8384
}
8485

8586
TargetOptions Options;
87+
Options.PositionIndependentExecutable = PositionIndependentExecutable;
8688
Options.NoFramePointerElim = NoFramePointerElim;
8789
#if LLVM_VERSION_MINOR < 5
8890
Options.EnableSegmentedStacks = EnableSegmentedStacks;

0 commit comments

Comments
 (0)