• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //! The Rust compiler.
2 //!
3 //! # Note
4 //!
5 //! This API is completely unstable and subject to change.
6 
7 #![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
8 #![feature(extern_types)]
9 #![feature(hash_raw_entry)]
10 #![feature(iter_intersperse)]
11 #![feature(let_chains)]
12 #![feature(never_type)]
13 #![feature(impl_trait_in_assoc_type)]
14 #![recursion_limit = "256"]
15 #![allow(rustc::potential_query_instability)]
16 #![deny(rustc::untranslatable_diagnostic)]
17 #![deny(rustc::diagnostic_outside_of_impl)]
18 
19 #[macro_use]
20 extern crate rustc_macros;
21 #[macro_use]
22 extern crate tracing;
23 
24 use back::write::{create_informational_target_machine, create_target_machine};
25 
26 use errors::ParseTargetMachineConfig;
27 pub use llvm_util::target_features;
28 use rustc_ast::expand::allocator::AllocatorKind;
29 use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
30 use rustc_codegen_ssa::back::write::{
31     CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
32 };
33 use rustc_codegen_ssa::traits::*;
34 use rustc_codegen_ssa::ModuleCodegen;
35 use rustc_codegen_ssa::{CodegenResults, CompiledModule};
36 use rustc_data_structures::fx::FxIndexMap;
37 use rustc_errors::{DiagnosticMessage, ErrorGuaranteed, FatalError, Handler, SubdiagnosticMessage};
38 use rustc_fluent_macro::fluent_messages;
39 use rustc_metadata::EncodedMetadata;
40 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
41 use rustc_middle::query::Providers;
42 use rustc_middle::ty::TyCtxt;
43 use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest};
44 use rustc_session::Session;
45 use rustc_span::symbol::Symbol;
46 
47 use std::any::Any;
48 use std::ffi::CStr;
49 
50 mod back {
51     pub mod archive;
52     pub mod lto;
53     mod profiling;
54     pub mod write;
55 }
56 
57 mod abi;
58 mod allocator;
59 mod asm;
60 mod attributes;
61 mod base;
62 mod builder;
63 mod callee;
64 mod common;
65 mod consts;
66 mod context;
67 mod coverageinfo;
68 mod debuginfo;
69 mod declare;
70 mod errors;
71 mod intrinsic;
72 
73 // The following is a workaround that replaces `pub mod llvm;` and that fixes issue 53912.
74 #[path = "llvm/mod.rs"]
75 mod llvm_;
76 pub mod llvm {
77     pub use super::llvm_::*;
78 }
79 
80 mod llvm_util;
81 mod mono_item;
82 mod type_;
83 mod type_of;
84 mod va_arg;
85 mod value;
86 
87 fluent_messages! { "../messages.ftl" }
88 
89 #[derive(Clone)]
90 pub struct LlvmCodegenBackend(());
91 
92 struct TimeTraceProfiler {
93     enabled: bool,
94 }
95 
96 impl TimeTraceProfiler {
new(enabled: bool) -> Self97     fn new(enabled: bool) -> Self {
98         if enabled {
99             unsafe { llvm::LLVMTimeTraceProfilerInitialize() }
100         }
101         TimeTraceProfiler { enabled }
102     }
103 }
104 
105 impl Drop for TimeTraceProfiler {
drop(&mut self)106     fn drop(&mut self) {
107         if self.enabled {
108             unsafe { llvm::LLVMTimeTraceProfilerFinishThread() }
109         }
110     }
111 }
112 
113 impl ExtraBackendMethods for LlvmCodegenBackend {
codegen_allocator<'tcx>( &self, tcx: TyCtxt<'tcx>, module_name: &str, kind: AllocatorKind, alloc_error_handler_kind: AllocatorKind, ) -> ModuleLlvm114     fn codegen_allocator<'tcx>(
115         &self,
116         tcx: TyCtxt<'tcx>,
117         module_name: &str,
118         kind: AllocatorKind,
119         alloc_error_handler_kind: AllocatorKind,
120     ) -> ModuleLlvm {
121         let mut module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
122         unsafe {
123             allocator::codegen(tcx, &mut module_llvm, module_name, kind, alloc_error_handler_kind);
124         }
125         module_llvm
126     }
compile_codegen_unit( &self, tcx: TyCtxt<'_>, cgu_name: Symbol, ) -> (ModuleCodegen<ModuleLlvm>, u64)127     fn compile_codegen_unit(
128         &self,
129         tcx: TyCtxt<'_>,
130         cgu_name: Symbol,
131     ) -> (ModuleCodegen<ModuleLlvm>, u64) {
132         base::compile_codegen_unit(tcx, cgu_name)
133     }
target_machine_factory( &self, sess: &Session, optlvl: OptLevel, target_features: &[String], ) -> TargetMachineFactoryFn<Self>134     fn target_machine_factory(
135         &self,
136         sess: &Session,
137         optlvl: OptLevel,
138         target_features: &[String],
139     ) -> TargetMachineFactoryFn<Self> {
140         back::write::target_machine_factory(sess, optlvl, target_features)
141     }
142 
spawn_thread<F, T>(time_trace: bool, f: F) -> std::thread::JoinHandle<T> where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static,143     fn spawn_thread<F, T>(time_trace: bool, f: F) -> std::thread::JoinHandle<T>
144     where
145         F: FnOnce() -> T,
146         F: Send + 'static,
147         T: Send + 'static,
148     {
149         std::thread::spawn(move || {
150             let _profiler = TimeTraceProfiler::new(time_trace);
151             f()
152         })
153     }
154 
spawn_named_thread<F, T>( time_trace: bool, name: String, f: F, ) -> std::io::Result<std::thread::JoinHandle<T>> where F: FnOnce() -> T, F: Send + 'static, T: Send + 'static,155     fn spawn_named_thread<F, T>(
156         time_trace: bool,
157         name: String,
158         f: F,
159     ) -> std::io::Result<std::thread::JoinHandle<T>>
160     where
161         F: FnOnce() -> T,
162         F: Send + 'static,
163         T: Send + 'static,
164     {
165         std::thread::Builder::new().name(name).spawn(move || {
166             let _profiler = TimeTraceProfiler::new(time_trace);
167             f()
168         })
169     }
170 }
171 
172 impl WriteBackendMethods for LlvmCodegenBackend {
173     type Module = ModuleLlvm;
174     type ModuleBuffer = back::lto::ModuleBuffer;
175     type TargetMachine = &'static mut llvm::TargetMachine;
176     type TargetMachineError = crate::errors::LlvmError<'static>;
177     type ThinData = back::lto::ThinData;
178     type ThinBuffer = back::lto::ThinBuffer;
print_pass_timings(&self)179     fn print_pass_timings(&self) {
180         unsafe {
181             llvm::LLVMRustPrintPassTimings();
182         }
183     }
run_link( cgcx: &CodegenContext<Self>, diag_handler: &Handler, modules: Vec<ModuleCodegen<Self::Module>>, ) -> Result<ModuleCodegen<Self::Module>, FatalError>184     fn run_link(
185         cgcx: &CodegenContext<Self>,
186         diag_handler: &Handler,
187         modules: Vec<ModuleCodegen<Self::Module>>,
188     ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
189         back::write::link(cgcx, diag_handler, modules)
190     }
run_fat_lto( cgcx: &CodegenContext<Self>, modules: Vec<FatLTOInput<Self>>, cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>, ) -> Result<LtoModuleCodegen<Self>, FatalError>191     fn run_fat_lto(
192         cgcx: &CodegenContext<Self>,
193         modules: Vec<FatLTOInput<Self>>,
194         cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
195     ) -> Result<LtoModuleCodegen<Self>, FatalError> {
196         back::lto::run_fat(cgcx, modules, cached_modules)
197     }
run_thin_lto( cgcx: &CodegenContext<Self>, modules: Vec<(String, Self::ThinBuffer)>, cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>, ) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError>198     fn run_thin_lto(
199         cgcx: &CodegenContext<Self>,
200         modules: Vec<(String, Self::ThinBuffer)>,
201         cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
202     ) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
203         back::lto::run_thin(cgcx, modules, cached_modules)
204     }
optimize( cgcx: &CodegenContext<Self>, diag_handler: &Handler, module: &ModuleCodegen<Self::Module>, config: &ModuleConfig, ) -> Result<(), FatalError>205     unsafe fn optimize(
206         cgcx: &CodegenContext<Self>,
207         diag_handler: &Handler,
208         module: &ModuleCodegen<Self::Module>,
209         config: &ModuleConfig,
210     ) -> Result<(), FatalError> {
211         back::write::optimize(cgcx, diag_handler, module, config)
212     }
optimize_fat( cgcx: &CodegenContext<Self>, module: &mut ModuleCodegen<Self::Module>, ) -> Result<(), FatalError>213     fn optimize_fat(
214         cgcx: &CodegenContext<Self>,
215         module: &mut ModuleCodegen<Self::Module>,
216     ) -> Result<(), FatalError> {
217         let diag_handler = cgcx.create_diag_handler();
218         back::lto::run_pass_manager(cgcx, &diag_handler, module, false)
219     }
optimize_thin( cgcx: &CodegenContext<Self>, thin: ThinModule<Self>, ) -> Result<ModuleCodegen<Self::Module>, FatalError>220     unsafe fn optimize_thin(
221         cgcx: &CodegenContext<Self>,
222         thin: ThinModule<Self>,
223     ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
224         back::lto::optimize_thin_module(thin, cgcx)
225     }
codegen( cgcx: &CodegenContext<Self>, diag_handler: &Handler, module: ModuleCodegen<Self::Module>, config: &ModuleConfig, ) -> Result<CompiledModule, FatalError>226     unsafe fn codegen(
227         cgcx: &CodegenContext<Self>,
228         diag_handler: &Handler,
229         module: ModuleCodegen<Self::Module>,
230         config: &ModuleConfig,
231     ) -> Result<CompiledModule, FatalError> {
232         back::write::codegen(cgcx, diag_handler, module, config)
233     }
prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer)234     fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
235         back::lto::prepare_thin(module)
236     }
serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer)237     fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
238         (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
239     }
240 }
241 
242 unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
243 unsafe impl Sync for LlvmCodegenBackend {}
244 
245 impl LlvmCodegenBackend {
new() -> Box<dyn CodegenBackend>246     pub fn new() -> Box<dyn CodegenBackend> {
247         Box::new(LlvmCodegenBackend(()))
248     }
249 }
250 
251 impl CodegenBackend for LlvmCodegenBackend {
locale_resource(&self) -> &'static str252     fn locale_resource(&self) -> &'static str {
253         crate::DEFAULT_LOCALE_RESOURCE
254     }
255 
init(&self, sess: &Session)256     fn init(&self, sess: &Session) {
257         llvm_util::init(sess); // Make sure llvm is inited
258     }
259 
provide(&self, providers: &mut Providers)260     fn provide(&self, providers: &mut Providers) {
261         providers.global_backend_features =
262             |tcx, ()| llvm_util::global_llvm_features(tcx.sess, true)
263     }
264 
print(&self, req: PrintRequest, sess: &Session)265     fn print(&self, req: PrintRequest, sess: &Session) {
266         match req {
267             PrintRequest::RelocationModels => {
268                 println!("Available relocation models:");
269                 for name in &[
270                     "static",
271                     "pic",
272                     "pie",
273                     "dynamic-no-pic",
274                     "ropi",
275                     "rwpi",
276                     "ropi-rwpi",
277                     "default",
278                 ] {
279                     println!("    {}", name);
280                 }
281                 println!();
282             }
283             PrintRequest::CodeModels => {
284                 println!("Available code models:");
285                 for name in &["tiny", "small", "kernel", "medium", "large"] {
286                     println!("    {}", name);
287                 }
288                 println!();
289             }
290             PrintRequest::TlsModels => {
291                 println!("Available TLS models:");
292                 for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
293                     println!("    {}", name);
294                 }
295                 println!();
296             }
297             PrintRequest::StackProtectorStrategies => {
298                 println!(
299                     r#"Available stack protector strategies:
300     all
301         Generate stack canaries in all functions.
302 
303     strong
304         Generate stack canaries in a function if it either:
305         - has a local variable of `[T; N]` type, regardless of `T` and `N`
306         - takes the address of a local variable.
307 
308           (Note that a local variable being borrowed is not equivalent to its
309           address being taken: e.g. some borrows may be removed by optimization,
310           while by-value argument passing may be implemented with reference to a
311           local stack variable in the ABI.)
312 
313     basic
314         Generate stack canaries in functions with local variables of `[T; N]`
315         type, where `T` is byte-sized and `N` >= 8.
316 
317     none
318         Do not generate stack canaries.
319 "#
320                 );
321             }
322             req => llvm_util::print(req, sess),
323         }
324     }
325 
print_passes(&self)326     fn print_passes(&self) {
327         llvm_util::print_passes();
328     }
329 
print_version(&self)330     fn print_version(&self) {
331         llvm_util::print_version();
332     }
333 
target_features(&self, sess: &Session, allow_unstable: bool) -> Vec<Symbol>334     fn target_features(&self, sess: &Session, allow_unstable: bool) -> Vec<Symbol> {
335         target_features(sess, allow_unstable)
336     }
337 
codegen_crate<'tcx>( &self, tcx: TyCtxt<'tcx>, metadata: EncodedMetadata, need_metadata_module: bool, ) -> Box<dyn Any>338     fn codegen_crate<'tcx>(
339         &self,
340         tcx: TyCtxt<'tcx>,
341         metadata: EncodedMetadata,
342         need_metadata_module: bool,
343     ) -> Box<dyn Any> {
344         Box::new(rustc_codegen_ssa::base::codegen_crate(
345             LlvmCodegenBackend(()),
346             tcx,
347             crate::llvm_util::target_cpu(tcx.sess).to_string(),
348             metadata,
349             need_metadata_module,
350         ))
351     }
352 
join_codegen( &self, ongoing_codegen: Box<dyn Any>, sess: &Session, outputs: &OutputFilenames, ) -> Result<(CodegenResults, FxIndexMap<WorkProductId, WorkProduct>), ErrorGuaranteed>353     fn join_codegen(
354         &self,
355         ongoing_codegen: Box<dyn Any>,
356         sess: &Session,
357         outputs: &OutputFilenames,
358     ) -> Result<(CodegenResults, FxIndexMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
359         let (codegen_results, work_products) = ongoing_codegen
360             .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
361             .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
362             .join(sess);
363 
364         if sess.opts.unstable_opts.llvm_time_trace {
365             sess.time("llvm_dump_timing_file", || {
366                 let file_name = outputs.with_extension("llvm_timings.json");
367                 llvm_util::time_trace_profiler_finish(&file_name);
368             });
369         }
370 
371         Ok((codegen_results, work_products))
372     }
373 
link( &self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames, ) -> Result<(), ErrorGuaranteed>374     fn link(
375         &self,
376         sess: &Session,
377         codegen_results: CodegenResults,
378         outputs: &OutputFilenames,
379     ) -> Result<(), ErrorGuaranteed> {
380         use crate::back::archive::LlvmArchiveBuilderBuilder;
381         use rustc_codegen_ssa::back::link::link_binary;
382 
383         // Run the linker on any artifacts that resulted from the LLVM run.
384         // This should produce either a finished executable or library.
385         link_binary(sess, &LlvmArchiveBuilderBuilder, &codegen_results, outputs)
386     }
387 }
388 
389 pub struct ModuleLlvm {
390     llcx: &'static mut llvm::Context,
391     llmod_raw: *const llvm::Module,
392     tm: &'static mut llvm::TargetMachine,
393 }
394 
395 unsafe impl Send for ModuleLlvm {}
396 unsafe impl Sync for ModuleLlvm {}
397 
398 impl ModuleLlvm {
new(tcx: TyCtxt<'_>, mod_name: &str) -> Self399     fn new(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
400         unsafe {
401             let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
402             let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
403             ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx, mod_name) }
404         }
405     }
406 
new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self407     fn new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
408         unsafe {
409             let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
410             let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
411             ModuleLlvm { llmod_raw, llcx, tm: create_informational_target_machine(tcx.sess) }
412         }
413     }
414 
parse( cgcx: &CodegenContext<LlvmCodegenBackend>, name: &CStr, buffer: &[u8], handler: &Handler, ) -> Result<Self, FatalError>415     fn parse(
416         cgcx: &CodegenContext<LlvmCodegenBackend>,
417         name: &CStr,
418         buffer: &[u8],
419         handler: &Handler,
420     ) -> Result<Self, FatalError> {
421         unsafe {
422             let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
423             let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?;
424             let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, name.to_str().unwrap());
425             let tm = match (cgcx.tm_factory)(tm_factory_config) {
426                 Ok(m) => m,
427                 Err(e) => {
428                     return Err(handler.emit_almost_fatal(ParseTargetMachineConfig(e)));
429                 }
430             };
431 
432             Ok(ModuleLlvm { llmod_raw, llcx, tm })
433         }
434     }
435 
llmod(&self) -> &llvm::Module436     fn llmod(&self) -> &llvm::Module {
437         unsafe { &*self.llmod_raw }
438     }
439 }
440 
441 impl Drop for ModuleLlvm {
drop(&mut self)442     fn drop(&mut self) {
443         unsafe {
444             llvm::LLVMRustDisposeTargetMachine(&mut *(self.tm as *mut _));
445             llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));
446         }
447     }
448 }
449