Skip to content

Commit 4fec737

Browse files
committed
Auto merge of #58605 - nagisa:fix-the-metadata, r=michaelwoerister
Use informational target machine for metadata Since there is nothing to optimise there... Should fix #58323 but haven’t tested locally. r? @michaelwoerister
2 parents 70a497a + 8d4afbe commit 4fec737

File tree

5 files changed

+35
-18
lines changed

5 files changed

+35
-18
lines changed

src/librustc_codegen_llvm/back/write.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use crate::type_::Type;
1010
use crate::context::{is_pie_binary, get_reloc_model};
1111
use crate::common;
1212
use crate::LlvmCodegenBackend;
13+
use rustc::hir::def_id::LOCAL_CRATE;
1314
use rustc_codegen_ssa::back::write::{CodegenContext, ModuleConfig, run_assembler};
1415
use rustc_codegen_ssa::traits::*;
15-
use rustc::hir::def_id::LOCAL_CRATE;
1616
use rustc::session::config::{self, OutputType, Passes, Lto};
1717
use rustc::session::Session;
1818
use rustc::ty::TyCtxt;
@@ -82,14 +82,6 @@ pub fn write_output_file(
8282
}
8383
}
8484

85-
pub fn create_target_machine(
86-
tcx: TyCtxt<'_, '_, '_>,
87-
find_features: bool,
88-
) -> &'static mut llvm::TargetMachine {
89-
target_machine_factory(tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)()
90-
.unwrap_or_else(|err| llvm_err(tcx.sess.diagnostic(), &err).raise() )
91-
}
92-
9385
pub fn create_informational_target_machine(
9486
sess: &Session,
9587
find_features: bool,
@@ -99,6 +91,15 @@ pub fn create_informational_target_machine(
9991
})
10092
}
10193

94+
pub fn create_target_machine(
95+
tcx: TyCtxt<'_, '_, '_>,
96+
find_features: bool,
97+
) -> &'static mut llvm::TargetMachine {
98+
target_machine_factory(&tcx.sess, tcx.backend_optimization_level(LOCAL_CRATE), find_features)()
99+
.unwrap_or_else(|err| {
100+
llvm_err(tcx.sess.diagnostic(), &err).raise()
101+
})
102+
}
102103

103104
pub fn to_llvm_opt_settings(cfg: config::OptLevel) -> (llvm::CodeGenOptLevel, llvm::CodeGenOptSize)
104105
{

src/librustc_codegen_llvm/base.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313
//! but one `llvm::Type` corresponds to many `Ty`s; for instance, `tup(int, int,
1414
//! int)` and `rec(x=int, y=int, z=int)` will have the same `llvm::Type`.
1515
16-
use super::ModuleLlvm;
16+
use super::{LlvmCodegenBackend, ModuleLlvm};
1717
use rustc_codegen_ssa::{ModuleCodegen, ModuleKind};
1818
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
19-
use super::LlvmCodegenBackend;
2019

2120
use crate::llvm;
2221
use crate::metadata;
@@ -163,10 +162,9 @@ pub fn compile_codegen_unit<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
163162
cgu_name: InternedString)
164163
-> (Stats, ModuleCodegen<ModuleLlvm>)
165164
{
166-
let backend = LlvmCodegenBackend(());
167165
let cgu = tcx.codegen_unit(cgu_name);
168166
// Instantiate monomorphizations without filling out definitions yet...
169-
let llvm_module = backend.new_metadata(tcx, &cgu_name.as_str());
167+
let llvm_module = ModuleLlvm::new(tcx, &cgu_name.as_str());
170168
let stats = {
171169
let cx = CodegenCx::new(tcx, cgu, &llvm_module);
172170
let mono_items = cx.codegen_unit

src/librustc_codegen_llvm/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ pub unsafe fn create_module(
154154

155155
// Ensure the data-layout values hardcoded remain the defaults.
156156
if sess.target.target.options.is_builtin {
157-
let tm = crate::back::write::create_target_machine(tcx, false);
157+
let tm = crate::back::write::create_informational_target_machine(&tcx.sess, false);
158158
llvm::LLVMRustSetDataLayoutFromTargetMachine(llmod, tm);
159159
llvm::LLVMRustDisposeTargetMachine(tm);
160160

src/librustc_codegen_llvm/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#![deny(rust_2018_idioms)]
2424
#![allow(explicit_outlives_requirements)]
2525

26-
use back::write::create_target_machine;
26+
use back::write::{create_target_machine, create_informational_target_machine};
2727
use syntax_pos::symbol::Symbol;
2828

2929
extern crate flate2;
@@ -112,8 +112,9 @@ pub struct LlvmCodegenBackend(());
112112

113113
impl ExtraBackendMethods for LlvmCodegenBackend {
114114
fn new_metadata(&self, tcx: TyCtxt<'_, '_, '_>, mod_name: &str) -> ModuleLlvm {
115-
ModuleLlvm::new(tcx, mod_name)
115+
ModuleLlvm::new_metadata(tcx, mod_name)
116116
}
117+
117118
fn write_metadata<'b, 'gcx>(
118119
&self,
119120
tcx: TyCtxt<'b, 'gcx, 'gcx>,
@@ -363,7 +364,6 @@ impl ModuleLlvm {
363364
unsafe {
364365
let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
365366
let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
366-
367367
ModuleLlvm {
368368
llmod_raw,
369369
llcx,
@@ -372,6 +372,18 @@ impl ModuleLlvm {
372372
}
373373
}
374374

375+
fn new_metadata(tcx: TyCtxt<'_, '_, '_>, mod_name: &str) -> Self {
376+
unsafe {
377+
let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
378+
let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
379+
ModuleLlvm {
380+
llmod_raw,
381+
llcx,
382+
tm: create_informational_target_machine(&tcx.sess, false),
383+
}
384+
}
385+
}
386+
375387
fn parse(
376388
cgcx: &CodegenContext<LlvmCodegenBackend>,
377389
name: &str,

src/librustc_codegen_ssa/back/write.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,13 @@ fn start_executing_work<B: ExtraBackendMethods>(
10751075
None
10761076
};
10771077

1078-
let ol = tcx.backend_optimization_level(LOCAL_CRATE);
1078+
let ol = if tcx.sess.opts.debugging_opts.no_codegen
1079+
|| !tcx.sess.opts.output_types.should_codegen() {
1080+
// If we know that we won’t be doing codegen, create target machines without optimisation.
1081+
config::OptLevel::No
1082+
} else {
1083+
tcx.backend_optimization_level(LOCAL_CRATE)
1084+
};
10791085
let cgcx = CodegenContext::<B> {
10801086
backend: backend.clone(),
10811087
crate_types: sess.crate_types.borrow().clone(),

0 commit comments

Comments
 (0)