5 //! This API is completely unstable and subject to change.
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8 #![feature(hash_raw_entry)]
9 #![feature(let_chains)]
10 #![feature(extern_types)]
11 #![feature(once_cell)]
12 #![feature(iter_intersperse)]
13 #![recursion_limit = "256"]
14 #![allow(rustc::potential_query_instability)]
15 #![deny(rustc::untranslatable_diagnostic)]
16 #![deny(rustc::diagnostic_outside_of_impl)]
19 extern crate rustc_macros;
23 use back::write::{create_informational_target_machine, create_target_machine};
25 use errors::FailParsingTargetMachineConfigToTargetMachine;
26 pub use llvm_util::target_features;
27 use rustc_ast::expand::allocator::AllocatorKind;
28 use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
29 use rustc_codegen_ssa::back::write::{
30 CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
32 use rustc_codegen_ssa::traits::*;
33 use rustc_codegen_ssa::ModuleCodegen;
34 use rustc_codegen_ssa::{CodegenResults, CompiledModule};
35 use rustc_data_structures::fx::FxHashMap;
36 use rustc_errors::{ErrorGuaranteed, FatalError, Handler};
37 use rustc_metadata::EncodedMetadata;
38 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
39 use rustc_middle::ty::query::Providers;
40 use rustc_middle::ty::TyCtxt;
41 use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest};
42 use rustc_session::Session;
43 use rustc_span::symbol::Symbol;
71 // The following is a work around that replaces `pub mod llvm;` and that fixes issue 53912.
72 #[path = "llvm/mod.rs"]
75 pub use super::llvm_::*;
86 pub struct LlvmCodegenBackend(());
88 struct TimeTraceProfiler {
92 impl TimeTraceProfiler {
93 fn new(enabled: bool) -> Self {
95 unsafe { llvm::LLVMTimeTraceProfilerInitialize() }
97 TimeTraceProfiler { enabled }
101 impl Drop for TimeTraceProfiler {
104 unsafe { llvm::LLVMTimeTraceProfilerFinishThread() }
109 impl ExtraBackendMethods for LlvmCodegenBackend {
110 fn codegen_allocator<'tcx>(
115 alloc_error_handler_kind: AllocatorKind,
117 let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
119 allocator::codegen(tcx, &mut module_llvm, module_name, kind, alloc_error_handler_kind);
123 fn compile_codegen_unit(
127 ) -> (ModuleCodegen<ModuleLlvm>, u64) {
128 base::compile_codegen_unit(tcx, cgu_name)
130 fn target_machine_factory(
134 target_features: &[String],
135 ) -> TargetMachineFactoryFn<Self> {
136 back::write::target_machine_factory(sess, optlvl, target_features)
139 fn spawn_thread<F, T>(time_trace: bool, f: F) -> std::thread::JoinHandle<T>
145 std::thread::spawn(move || {
146 let _profiler = TimeTraceProfiler::new(time_trace);
151 fn spawn_named_thread<F, T>(
155 ) -> std::io::Result<std::thread::JoinHandle<T>>
161 std::thread::Builder::new().name(name).spawn(move || {
162 let _profiler = TimeTraceProfiler::new(time_trace);
168 impl WriteBackendMethods for LlvmCodegenBackend {
169 type Module = ModuleLlvm;
170 type ModuleBuffer = back::lto::ModuleBuffer;
171 type TargetMachine = &'static mut llvm::TargetMachine;
172 type ThinData = back::lto::ThinData;
173 type ThinBuffer = back::lto::ThinBuffer;
174 fn print_pass_timings(&self) {
176 llvm::LLVMRustPrintPassTimings();
180 cgcx: &CodegenContext<Self>,
181 diag_handler: &Handler,
182 modules: Vec<ModuleCodegen<Self::Module>>,
183 ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
184 back::write::link(cgcx, diag_handler, modules)
187 cgcx: &CodegenContext<Self>,
188 modules: Vec<FatLTOInput<Self>>,
189 cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
190 ) -> Result<LtoModuleCodegen<Self>, FatalError> {
191 back::lto::run_fat(cgcx, modules, cached_modules)
194 cgcx: &CodegenContext<Self>,
195 modules: Vec<(String, Self::ThinBuffer)>,
196 cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
197 ) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
198 back::lto::run_thin(cgcx, modules, cached_modules)
201 cgcx: &CodegenContext<Self>,
202 diag_handler: &Handler,
203 module: &ModuleCodegen<Self::Module>,
204 config: &ModuleConfig,
205 ) -> Result<(), FatalError> {
206 back::write::optimize(cgcx, diag_handler, module, config)
209 cgcx: &CodegenContext<Self>,
210 module: &mut ModuleCodegen<Self::Module>,
211 ) -> Result<(), FatalError> {
212 let diag_handler = cgcx.create_diag_handler();
213 back::lto::run_pass_manager(cgcx, &diag_handler, module, false)
215 unsafe fn optimize_thin(
216 cgcx: &CodegenContext<Self>,
217 thin: ThinModule<Self>,
218 ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
219 back::lto::optimize_thin_module(thin, cgcx)
222 cgcx: &CodegenContext<Self>,
223 diag_handler: &Handler,
224 module: ModuleCodegen<Self::Module>,
225 config: &ModuleConfig,
226 ) -> Result<CompiledModule, FatalError> {
227 back::write::codegen(cgcx, diag_handler, module, config)
229 fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
230 back::lto::prepare_thin(module)
232 fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
233 (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
237 unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
238 unsafe impl Sync for LlvmCodegenBackend {}
240 impl LlvmCodegenBackend {
241 pub fn new() -> Box<dyn CodegenBackend> {
242 Box::new(LlvmCodegenBackend(()))
246 impl CodegenBackend for LlvmCodegenBackend {
247 fn init(&self, sess: &Session) {
248 llvm_util::init(sess); // Make sure llvm is inited
251 fn provide(&self, providers: &mut Providers) {
252 providers.global_backend_features =
253 |tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
256 fn print(&self, req: PrintRequest, sess: &Session) {
258 PrintRequest::RelocationModels => {
259 println!("Available relocation models:");
270 println!(" {}", name);
274 PrintRequest::CodeModels => {
275 println!("Available code models:");
276 for name in &["tiny", "small", "kernel", "medium", "large"] {
277 println!(" {}", name);
281 PrintRequest::TlsModels => {
282 println!("Available TLS models:");
283 for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
284 println!(" {}", name);
288 PrintRequest::StackProtectorStrategies => {
290 r#"Available stack protector strategies:
292 Generate stack canaries in all functions.
295 Generate stack canaries in a function if it either:
296 - has a local variable of `[T; N]` type, regardless of `T` and `N`
297 - takes the address of a local variable.
299 (Note that a local variable being borrowed is not equivalent to its
300 address being taken: e.g. some borrows may be removed by optimization,
301 while by-value argument passing may be implemented with reference to a
302 local stack variable in the ABI.)
305 Generate stack canaries in functions with local variables of `[T; N]`
306 type, where `T` is byte-sized and `N` >= 8.
309 Do not generate stack canaries.
313 req => llvm_util::print(req, sess),
317 fn print_passes(&self) {
318 llvm_util::print_passes();
321 fn print_version(&self) {
322 llvm_util::print_version();
325 fn target_features(&self, sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
326 target_features(sess, allow_unstable)
329 fn codegen_crate<'tcx>(
332 metadata: EncodedMetadata,
333 need_metadata_module: bool,
335 Box::new(rustc_codegen_ssa::base::codegen_crate(
336 LlvmCodegenBackend(()),
338 crate::llvm_util::target_cpu(tcx.sess).to_string(),
340 need_metadata_module,
346 ongoing_codegen: Box<dyn Any>,
348 outputs: &OutputFilenames,
349 ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
350 let (codegen_results, work_products) = ongoing_codegen
351 .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
352 .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
355 sess.time("llvm_dump_timing_file", || {
356 if sess.opts.unstable_opts.llvm_time_trace {
357 let file_name = outputs.with_extension("llvm_timings.json");
358 llvm_util::time_trace_profiler_finish(&file_name);
362 Ok((codegen_results, work_products))
368 codegen_results: CodegenResults,
369 outputs: &OutputFilenames,
370 ) -> Result<(), ErrorGuaranteed> {
371 use crate::back::archive::LlvmArchiveBuilderBuilder;
372 use rustc_codegen_ssa::back::link::link_binary;
374 // Run the linker on any artifacts that resulted from the LLVM run.
375 // This should produce either a finished executable or library.
376 link_binary(sess, &LlvmArchiveBuilderBuilder, &codegen_results, outputs)
380 pub struct ModuleLlvm {
381 llcx: &'static mut llvm::Context,
382 llmod_raw: *const llvm::Module,
383 tm: &'static mut llvm::TargetMachine,
386 unsafe impl Send for ModuleLlvm {}
387 unsafe impl Sync for ModuleLlvm {}
390 fn new(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
392 let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
393 let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
394 ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx, mod_name) }
398 fn new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
400 let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
401 let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
402 ModuleLlvm { llmod_raw, llcx, tm: create_informational_target_machine(tcx.sess) }
407 cgcx: &CodegenContext<LlvmCodegenBackend>,
411 ) -> Result<Self, FatalError> {
413 let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
414 let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?;
415 let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, name.to_str().unwrap());
416 let tm = match (cgcx.tm_factory)(tm_factory_config) {
419 handler.emit_err(FailParsingTargetMachineConfigToTargetMachine { error: e });
420 return Err(FatalError);
424 Ok(ModuleLlvm { llmod_raw, llcx, tm })
428 fn llmod(&self) -> &llvm::Module {
429 unsafe { &*self.llmod_raw }
433 impl Drop for ModuleLlvm {
436 llvm::LLVMRustDisposeTargetMachine(&mut *(self.tm as *mut _));
437 llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));