Skip to content

Commit b719578

Browse files
committed
Use name-discarding LLVM context
This is only applicable when neither of --emit=llvm-ir or --emit=llvm-bc are not requested. In case either of these outputs are wanted, but the benefits of such context are desired as well, -Zfewer_names option provides the same functionality regardless of the outputs requested.
1 parent 8e7a609 commit b719578

File tree

8 files changed

+27
-6
lines changed

8 files changed

+27
-6
lines changed

src/librustc/session/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
10821082
"gather borrowck statistics"),
10831083
no_landing_pads: bool = (false, parse_bool, [TRACKED],
10841084
"omit landing pads for unwinding"),
1085+
fewer_names: bool = (false, parse_bool, [TRACKED],
1086+
"reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR)"),
10851087
debug_llvm: bool = (false, parse_bool, [UNTRACKED],
10861088
"enable debug output from LLVM"),
10871089
meta_stats: bool = (false, parse_bool, [UNTRACKED],
@@ -2811,6 +2813,10 @@ mod tests {
28112813
opts.debugging_opts.no_landing_pads = true;
28122814
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
28132815

2816+
opts = reference.clone();
2817+
opts.debugging_opts.fewer_names = true;
2818+
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
2819+
28142820
opts = reference.clone();
28152821
opts.debugging_opts.no_trans = true;
28162822
assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());

src/librustc/session/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use lint;
1818
use middle::allocator::AllocatorKind;
1919
use middle::dependency_format;
2020
use session::search_paths::PathKind;
21-
use session::config::{BorrowckMode, DebugInfoLevel};
21+
use session::config::{BorrowckMode, DebugInfoLevel, OutputType};
2222
use ty::tls;
2323
use util::nodemap::{FxHashMap, FxHashSet};
2424
use util::common::{duration_to_secs_str, ErrorReported};
@@ -504,6 +504,13 @@ impl Session {
504504
pub fn linker_flavor(&self) -> LinkerFlavor {
505505
self.opts.debugging_opts.linker_flavor.unwrap_or(self.target.target.linker_flavor)
506506
}
507+
508+
pub fn fewer_names(&self) -> bool {
509+
let more_names = self.opts.output_types.contains_key(&OutputType::LlvmAssembly) ||
510+
self.opts.output_types.contains_key(&OutputType::Bitcode);
511+
self.opts.debugging_opts.fewer_names || !more_names
512+
}
513+
507514
pub fn no_landing_pads(&self) -> bool {
508515
self.opts.debugging_opts.no_landing_pads || self.panic_strategy() == PanicStrategy::Abort
509516
}

src/librustc_llvm/ffi.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ pub enum ModuleBuffer {}
514514
#[link(name = "rustllvm", kind = "static")]
515515
extern "C" {
516516
// Create and destroy contexts.
517-
pub fn LLVMContextCreate() -> ContextRef;
517+
pub fn LLVMRustContextCreate(shouldDiscardNames: bool) -> ContextRef;
518518
pub fn LLVMContextDispose(C: ContextRef);
519519
pub fn LLVMGetMDKindIDInContext(C: ContextRef, Name: *const c_char, SLen: c_uint) -> c_uint;
520520

src/librustc_trans/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ impl ThinModule {
607607
// into that context. One day, however, we may do this for upstream
608608
// crates but for locally translated modules we may be able to reuse
609609
// that LLVM Context and Module.
610-
let llcx = llvm::LLVMContextCreate();
610+
let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
611611
let llmod = llvm::LLVMRustParseBitcodeForThinLTO(
612612
llcx,
613613
self.data().as_ptr(),

src/librustc_trans/back/write.rs

+2
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ pub struct CodegenContext {
323323
pub thinlto: bool,
324324
pub no_landing_pads: bool,
325325
pub save_temps: bool,
326+
pub fewer_names: bool,
326327
pub exported_symbols: Arc<ExportedSymbols>,
327328
pub opts: Arc<config::Options>,
328329
pub crate_types: Vec<config::CrateType>,
@@ -1407,6 +1408,7 @@ fn start_executing_work(tcx: TyCtxt,
14071408
unsafe { llvm::LLVMRustThinLTOAvailable() },
14081409

14091410
no_landing_pads: sess.no_landing_pads(),
1411+
fewer_names: sess.fewer_names(),
14101412
save_temps: sess.opts.cg.save_temps,
14111413
opts: Arc::new(sess.opts.clone()),
14121414
time_passes: sess.time_passes(),

src/librustc_trans/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ pub fn is_pie_binary(sess: &Session) -> bool {
197197
}
198198

199199
pub unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
200-
let llcx = llvm::LLVMContextCreate();
200+
let llcx = llvm::LLVMRustContextCreate(sess.fewer_names());
201201
let mod_name = CString::new(mod_name).unwrap();
202202
let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
203203

src/rustllvm/RustWrapper.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,17 @@ extern "C" char *LLVMRustGetLastError(void) {
7676
return Ret;
7777
}
7878

79-
void LLVMRustSetLastError(const char *Err) {
79+
extern "C" void LLVMRustSetLastError(const char *Err) {
8080
free((void *)LastError);
8181
LastError = strdup(Err);
8282
}
8383

84+
extern "C" LLVMContextRef LLVMRustContextCreate(bool shouldDiscardNames) {
85+
auto ctx = new LLVMContext();
86+
ctx->setDiscardValueNames(shouldDiscardNames);
87+
return wrap(ctx);
88+
}
89+
8490
extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
8591
const char *Triple) {
8692
unwrap(M)->setTargetTriple(Triple::normalize(Triple));

src/rustllvm/rustllvm.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
#include "llvm/IR/IRPrintingPasses.h"
7272
#include "llvm/Linker/Linker.h"
7373

74-
void LLVMRustSetLastError(const char *);
74+
extern "C" void LLVMRustSetLastError(const char *);
7575

7676
enum class LLVMRustResult { Success, Failure };
7777

0 commit comments

Comments
 (0)