Skip to content

Use informational target machine for metadata #58605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/librustc_codegen_llvm/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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)
{
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_codegen_llvm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,10 +162,9 @@ pub fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
cgu_name: InternedString)
-> (Stats, ModuleCodegen<ModuleLlvm>)
{
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
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
18 changes: 15 additions & 3 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>,
Expand Down Expand Up @@ -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,
Expand All @@ -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<LlvmCodegenBackend>,
name: &str,
Expand Down
8 changes: 7 additions & 1 deletion src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,13 @@ fn start_executing_work<B: ExtraBackendMethods>(
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)
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we're not putting this "don't optimize if no codegen" into the backend_optimization_level query itself?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally a fix would have happened all the way down inside collect_and_partition_mono_items (which would take care to not ICE when no codegen is happening), however that change is more involved than this and, given that I haven’t setup myself for reproduction, I wasn’t in a place to make such a change.

let cgcx = CodegenContext::<B> {
backend: backend.clone(),
crate_types: sess.crate_types.borrow().clone(),
Expand Down