From 8d4afbe4136933b47082dd1efa9c1132a31dfb0d Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Wed, 20 Feb 2019 22:27:00 +0200 Subject: [PATCH] Use informational target machine for metadata Since there is nothing to optimise there... --- src/librustc_codegen_llvm/back/write.rs | 19 ++++++++++--------- src/librustc_codegen_llvm/base.rs | 6 ++---- src/librustc_codegen_llvm/context.rs | 2 +- src/librustc_codegen_llvm/lib.rs | 18 +++++++++++++++--- src/librustc_codegen_ssa/back/write.rs | 8 +++++++- 5 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/librustc_codegen_llvm/back/write.rs b/src/librustc_codegen_llvm/back/write.rs index dc4dd4e39e17b..7f259024cee91 100644 --- a/src/librustc_codegen_llvm/back/write.rs +++ b/src/librustc_codegen_llvm/back/write.rs @@ -10,9 +10,9 @@ use crate::type_::Type; use crate::context::{is_pie_binary, get_reloc_model}; use crate::common; use crate::LlvmCodegenBackend; +use rustc::hir::def_id::LOCAL_CRATE; use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler}; use rustc_codegen_ssa::traits::*; -use rustc::hir::def_id::LOCAL_CRATE; use rustc::session::config::{self, OutputType, Passes, Lto}; use rustc::session::Session; use rustc::ty::TyCtxt; @@ -82,14 +82,6 @@ pub fn write_output_file( } } -pub fn create_target_machine( - tcx: TyCtxt<'_, '_, '_>, - find_features: bool, -) -> &'static mut llvm::TargetMachine { - target_machine_factory(tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)() - .unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise() ) -} - pub fn create_informational_target_machine( sess: &Session, find_features: bool, @@ -99,6 +91,15 @@ pub fn create_informational_target_machine( }) } +pub fn create_target_machine( + tcx: TyCtxt<'_, '_, '_>, + find_features: bool, +) -> &'static mut llvm::TargetMachine { + target_machine_factory(&tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)() + .unwrap_or_else(|err| { + llvm_err(tcx.sess.diagnostic(), &err).raise() + }) +} pub fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize) { diff --git a/src/librustc_codegen_llvm/base.rs b/src/librustc_codegen_llvm/base.rs index 1dac1557707c9..7ea5e91230905 100644 --- a/src/librustc_codegen_llvm/base.rs +++ b/src/librustc_codegen_llvm/base.rs @@ -13,10 +13,9 @@ //! but one `llvm::Type` corresponds to many `Ty`s; for instance, `tup(int, int, //! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`. -use super::ModuleLlvm; +use super::{LlvmCodegenBackend, ModuleLlvm}; use rustc_codegen_ssa::{ModuleCodegen, ModuleKind}; use rustc_codegen_ssa::base::maybe_create_entry_wrapper; -use super::LlvmCodegenBackend; use crate::llvm; use crate::metadata; @@ -163,10 +162,9 @@ pub fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cgu_name: InternedString) -> (Stats, ModuleCodegen) { - let backend = LlvmCodegenBackend(()); let cgu = tcx.codegen_unit(cgu_name); // Instantiate monomorphizations without filling out definitions yet... - let llvm_module = backend.new_metadata(tcx, &cgu_name.as_str()); + let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str()); let stats = { let cx = CodegenCx::new(tcx, cgu, &llvm_module); let mono_items = cx.codegen_unit diff --git a/src/librustc_codegen_llvm/context.rs b/src/librustc_codegen_llvm/context.rs index 23e3a8425d370..619304ad9afd0 100644 --- a/src/librustc_codegen_llvm/context.rs +++ b/src/librustc_codegen_llvm/context.rs @@ -154,7 +154,7 @@ pub unsafe fn create_module( // Ensure the data-layout values hardcoded remain the defaults. if sess.target.target.options.is_builtin { - let tm = crate::back::write::create_target_machine(tcx, false); + let tm = crate::back::write::create_informational_target_machine(&tcx.sess, false); llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm); llvm::LLVMRustDisposeTargetMachine(tm); diff --git a/src/librustc_codegen_llvm/lib.rs b/src/librustc_codegen_llvm/lib.rs index c75788ecb4fbd..c95feafa29aba 100644 --- a/src/librustc_codegen_llvm/lib.rs +++ b/src/librustc_codegen_llvm/lib.rs @@ -23,7 +23,7 @@ #![deny(rust_2018_idioms)] #![allow(explicit_outlives_requirements)] -use back::write::create_target_machine; +use back::write::{create_target_machine, create_informational_target_machine}; use syntax_pos::symbol::Symbol; extern crate flate2; @@ -112,8 +112,9 @@ pub struct LlvmCodegenBackend(()); impl ExtraBackendMethods for LlvmCodegenBackend { fn new_metadata(&self, tcx: TyCtxt<'_, '_, '_>, mod_name: &str) -> ModuleLlvm { - ModuleLlvm::new(tcx, mod_name) + ModuleLlvm::new_metadata(tcx, mod_name) } + fn write_metadata<'b, 'gcx>( &self, tcx: TyCtxt<'b, 'gcx, 'gcx>, @@ -363,7 +364,6 @@ impl ModuleLlvm { unsafe { let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names()); let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _; - ModuleLlvm { llmod_raw, llcx, @@ -372,6 +372,18 @@ impl ModuleLlvm { } } + fn new_metadata(tcx: TyCtxt<'_, '_, '_>, mod_name: &str) -> Self { + unsafe { + let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names()); + let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _; + ModuleLlvm { + llmod_raw, + llcx, + tm: create_informational_target_machine(&tcx.sess, false), + } + } + } + fn parse( cgcx: &CodegenContext, name: &str, diff --git a/src/librustc_codegen_ssa/back/write.rs b/src/librustc_codegen_ssa/back/write.rs index 859dfb99d92b8..55b7855ac0862 100644 --- a/src/librustc_codegen_ssa/back/write.rs +++ b/src/librustc_codegen_ssa/back/write.rs @@ -1075,7 +1075,13 @@ fn start_executing_work( None }; - let ol = tcx.backend_optimization_level(LOCAL_CRATE); + let ol = if tcx.sess.opts.debugging_opts.no_codegen + || !tcx.sess.opts.output_types.should_codegen() { + // If we know that we won’t be doing codegen, create target machines without optimisation. + config::OptLevel::No + } else { + tcx.backend_optimization_level(LOCAL_CRATE) + }; let cgcx = CodegenContext:: { backend: backend.clone(), crate_types: sess.crate_types.borrow().clone(),