about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSimonas Kazlauskas <git@kazlauskas.me>2018-01-04 22:19:23 +0200
committerSimonas Kazlauskas <git@kazlauskas.me>2018-01-05 19:08:44 +0200
commitb719578f48c06047aea5f5b307ec1e3affb8f4ff (patch)
tree67e426fd9cde4fbcc08b7f08256ed95a468a0245
parent8e7a609e635b728eba65d471c985ab462dc4cfc7 (diff)
downloadrust-b719578f48c06047aea5f5b307ec1e3affb8f4ff.tar.gz
rust-b719578f48c06047aea5f5b307ec1e3affb8f4ff.zip
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.
-rw-r--r--src/librustc/session/config.rs6
-rw-r--r--src/librustc/session/mod.rs9
-rw-r--r--src/librustc_llvm/ffi.rs2
-rw-r--r--src/librustc_trans/back/lto.rs2
-rw-r--r--src/librustc_trans/back/write.rs2
-rw-r--r--src/librustc_trans/context.rs2
-rw-r--r--src/rustllvm/RustWrapper.cpp8
-rw-r--r--src/rustllvm/rustllvm.h2
8 files changed, 27 insertions, 6 deletions
diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs
index 6ad9bd94bf2..05b1d584e9c 100644
--- a/src/librustc/session/config.rs
+++ b/src/librustc/session/config.rs
@@ -1082,6 +1082,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
         "gather borrowck statistics"),
     no_landing_pads: bool = (false, parse_bool, [TRACKED],
         "omit landing pads for unwinding"),
+    fewer_names: bool = (false, parse_bool, [TRACKED],
+        "reduce memory use by retaining fewer names within compilation artifacts (LLVM-IR)"),
     debug_llvm: bool = (false, parse_bool, [UNTRACKED],
         "enable debug output from LLVM"),
     meta_stats: bool = (false, parse_bool, [UNTRACKED],
@@ -2812,6 +2814,10 @@ mod tests {
         assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
 
         opts = reference.clone();
+        opts.debugging_opts.fewer_names = true;
+        assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
+
+        opts = reference.clone();
         opts.debugging_opts.no_trans = true;
         assert!(reference.dep_tracking_hash() != opts.dep_tracking_hash());
 
diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs
index 54bcc64d068..9e09a5d8fbe 100644
--- a/src/librustc/session/mod.rs
+++ b/src/librustc/session/mod.rs
@@ -18,7 +18,7 @@ use lint;
 use middle::allocator::AllocatorKind;
 use middle::dependency_format;
 use session::search_paths::PathKind;
-use session::config::{BorrowckMode, DebugInfoLevel};
+use session::config::{BorrowckMode, DebugInfoLevel, OutputType};
 use ty::tls;
 use util::nodemap::{FxHashMap, FxHashSet};
 use util::common::{duration_to_secs_str, ErrorReported};
@@ -504,6 +504,13 @@ impl Session {
     pub fn linker_flavor(&self) -> LinkerFlavor {
         self.opts.debugging_opts.linker_flavor.unwrap_or(self.target.target.linker_flavor)
     }
+
+    pub fn fewer_names(&self) -> bool {
+        let more_names = self.opts.output_types.contains_key(&OutputType::LlvmAssembly) ||
+                         self.opts.output_types.contains_key(&OutputType::Bitcode);
+        self.opts.debugging_opts.fewer_names || !more_names
+    }
+
     pub fn no_landing_pads(&self) -> bool {
         self.opts.debugging_opts.no_landing_pads || self.panic_strategy() == PanicStrategy::Abort
     }
diff --git a/src/librustc_llvm/ffi.rs b/src/librustc_llvm/ffi.rs
index 7d653494465..274a0d7cca8 100644
--- a/src/librustc_llvm/ffi.rs
+++ b/src/librustc_llvm/ffi.rs
@@ -514,7 +514,7 @@ pub enum ModuleBuffer {}
 #[link(name = "rustllvm", kind = "static")]
 extern "C" {
     // Create and destroy contexts.
-    pub fn LLVMContextCreate() -> ContextRef;
+    pub fn LLVMRustContextCreate(shouldDiscardNames: bool) -> ContextRef;
     pub fn LLVMContextDispose(C: ContextRef);
     pub fn LLVMGetMDKindIDInContext(C: ContextRef, Name: *const c_char, SLen: c_uint) -> c_uint;
 
diff --git a/src/librustc_trans/back/lto.rs b/src/librustc_trans/back/lto.rs
index 29871684092..60b24a578c6 100644
--- a/src/librustc_trans/back/lto.rs
+++ b/src/librustc_trans/back/lto.rs
@@ -607,7 +607,7 @@ impl ThinModule {
         // into that context. One day, however, we may do this for upstream
         // crates but for locally translated modules we may be able to reuse
         // that LLVM Context and Module.
-        let llcx = llvm::LLVMContextCreate();
+        let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
         let llmod = llvm::LLVMRustParseBitcodeForThinLTO(
             llcx,
             self.data().as_ptr(),
diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs
index d8e95cd2cf2..4d1bcd9bf46 100644
--- a/src/librustc_trans/back/write.rs
+++ b/src/librustc_trans/back/write.rs
@@ -323,6 +323,7 @@ pub struct CodegenContext {
     pub thinlto: bool,
     pub no_landing_pads: bool,
     pub save_temps: bool,
+    pub fewer_names: bool,
     pub exported_symbols: Arc<ExportedSymbols>,
     pub opts: Arc<config::Options>,
     pub crate_types: Vec<config::CrateType>,
@@ -1407,6 +1408,7 @@ fn start_executing_work(tcx: TyCtxt,
             unsafe { llvm::LLVMRustThinLTOAvailable() },
 
         no_landing_pads: sess.no_landing_pads(),
+        fewer_names: sess.fewer_names(),
         save_temps: sess.opts.cg.save_temps,
         opts: Arc::new(sess.opts.clone()),
         time_passes: sess.time_passes(),
diff --git a/src/librustc_trans/context.rs b/src/librustc_trans/context.rs
index d5e71062f74..248b37c43b4 100644
--- a/src/librustc_trans/context.rs
+++ b/src/librustc_trans/context.rs
@@ -197,7 +197,7 @@ pub fn is_pie_binary(sess: &Session) -> bool {
 }
 
 pub unsafe fn create_context_and_module(sess: &Session, mod_name: &str) -> (ContextRef, ModuleRef) {
-    let llcx = llvm::LLVMContextCreate();
+    let llcx = llvm::LLVMRustContextCreate(sess.fewer_names());
     let mod_name = CString::new(mod_name).unwrap();
     let llmod = llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx);
 
diff --git a/src/rustllvm/RustWrapper.cpp b/src/rustllvm/RustWrapper.cpp
index 6f51ea67cb1..1ebbd73e6fe 100644
--- a/src/rustllvm/RustWrapper.cpp
+++ b/src/rustllvm/RustWrapper.cpp
@@ -76,11 +76,17 @@ extern "C" char *LLVMRustGetLastError(void) {
   return Ret;
 }
 
-void LLVMRustSetLastError(const char *Err) {
+extern "C" void LLVMRustSetLastError(const char *Err) {
   free((void *)LastError);
   LastError = strdup(Err);
 }
 
+extern "C" LLVMContextRef LLVMRustContextCreate(bool shouldDiscardNames) {
+  auto ctx = new LLVMContext();
+  ctx->setDiscardValueNames(shouldDiscardNames);
+  return wrap(ctx);
+}
+
 extern "C" void LLVMRustSetNormalizedTarget(LLVMModuleRef M,
                                             const char *Triple) {
   unwrap(M)->setTargetTriple(Triple::normalize(Triple));
diff --git a/src/rustllvm/rustllvm.h b/src/rustllvm/rustllvm.h
index 8c2f855c226..714173f8602 100644
--- a/src/rustllvm/rustllvm.h
+++ b/src/rustllvm/rustllvm.h
@@ -71,7 +71,7 @@
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/Linker/Linker.h"
 
-void LLVMRustSetLastError(const char *);
+extern "C" void LLVMRustSetLastError(const char *);
 
 enum class LLVMRustResult { Success, Failure };