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(bool_to_option)]
9 #![feature(crate_visibility_modifier)]
11 #![feature(extern_types)]
12 #![feature(once_cell)]
14 #![recursion_limit = "256"]
15 #![allow(rustc::potential_query_instability)]
18 extern crate rustc_macros;
20 use back::write::{create_informational_target_machine, create_target_machine};
22 pub use llvm_util::target_features;
23 use rustc_ast::expand::allocator::AllocatorKind;
24 use rustc_codegen_ssa::back::lto::{LtoModuleCodegen, SerializedModule, ThinModule};
25 use rustc_codegen_ssa::back::write::{
26 CodegenContext, FatLTOInput, ModuleConfig, TargetMachineFactoryConfig, TargetMachineFactoryFn,
28 use rustc_codegen_ssa::traits::*;
29 use rustc_codegen_ssa::ModuleCodegen;
30 use rustc_codegen_ssa::{CodegenResults, CompiledModule};
31 use rustc_data_structures::fx::FxHashMap;
32 use rustc_errors::{ErrorReported, FatalError, Handler};
33 use rustc_metadata::EncodedMetadata;
34 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
35 use rustc_middle::ty::TyCtxt;
36 use rustc_session::config::{OptLevel, OutputFilenames, PrintRequest};
37 use rustc_session::Session;
38 use rustc_span::symbol::Symbol;
65 // The following is a work around that replaces `pub mod llvm;` and that fixes issue 53912.
66 #[path = "llvm/mod.rs"]
69 pub use super::llvm_::*;
80 pub struct LlvmCodegenBackend(());
82 struct TimeTraceProfiler {
86 impl TimeTraceProfiler {
87 fn new(enabled: bool) -> Self {
89 unsafe { llvm::LLVMTimeTraceProfilerInitialize() }
91 TimeTraceProfiler { enabled }
95 impl Drop for TimeTraceProfiler {
98 unsafe { llvm::LLVMTimeTraceProfilerFinishThread() }
103 impl ExtraBackendMethods for LlvmCodegenBackend {
104 fn new_metadata(&self, tcx: TyCtxt<'_>, mod_name: &str) -> ModuleLlvm {
105 ModuleLlvm::new_metadata(tcx, mod_name)
108 fn codegen_allocator<'tcx>(
111 module_llvm: &mut ModuleLlvm,
114 has_alloc_error_handler: bool,
116 unsafe { allocator::codegen(tcx, module_llvm, module_name, kind, has_alloc_error_handler) }
118 fn compile_codegen_unit(
122 ) -> (ModuleCodegen<ModuleLlvm>, u64) {
123 base::compile_codegen_unit(tcx, cgu_name)
125 fn target_machine_factory(
129 ) -> TargetMachineFactoryFn<Self> {
130 back::write::target_machine_factory(sess, optlvl)
132 fn target_cpu<'b>(&self, sess: &'b Session) -> &'b str {
133 llvm_util::target_cpu(sess)
135 fn tune_cpu<'b>(&self, sess: &'b Session) -> Option<&'b str> {
136 llvm_util::tune_cpu(sess)
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 Context = llvm::Context;
172 type TargetMachine = &'static mut llvm::TargetMachine;
173 type ThinData = back::lto::ThinData;
174 type ThinBuffer = back::lto::ThinBuffer;
175 fn print_pass_timings(&self) {
177 llvm::LLVMRustPrintPassTimings();
181 cgcx: &CodegenContext<Self>,
182 diag_handler: &Handler,
183 modules: Vec<ModuleCodegen<Self::Module>>,
184 ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
185 back::write::link(cgcx, diag_handler, modules)
188 cgcx: &CodegenContext<Self>,
189 modules: Vec<FatLTOInput<Self>>,
190 cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
191 ) -> Result<LtoModuleCodegen<Self>, FatalError> {
192 back::lto::run_fat(cgcx, modules, cached_modules)
195 cgcx: &CodegenContext<Self>,
196 modules: Vec<(String, Self::ThinBuffer)>,
197 cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
198 ) -> Result<(Vec<LtoModuleCodegen<Self>>, Vec<WorkProduct>), FatalError> {
199 back::lto::run_thin(cgcx, modules, cached_modules)
202 cgcx: &CodegenContext<Self>,
203 diag_handler: &Handler,
204 module: &ModuleCodegen<Self::Module>,
205 config: &ModuleConfig,
206 ) -> Result<(), FatalError> {
207 back::write::optimize(cgcx, diag_handler, module, config)
209 unsafe fn optimize_thin(
210 cgcx: &CodegenContext<Self>,
211 thin: &mut ThinModule<Self>,
212 ) -> Result<ModuleCodegen<Self::Module>, FatalError> {
213 back::lto::optimize_thin_module(thin, cgcx)
216 cgcx: &CodegenContext<Self>,
217 diag_handler: &Handler,
218 module: ModuleCodegen<Self::Module>,
219 config: &ModuleConfig,
220 ) -> Result<CompiledModule, FatalError> {
221 back::write::codegen(cgcx, diag_handler, module, config)
223 fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
224 back::lto::prepare_thin(module)
226 fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
227 (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
229 fn run_lto_pass_manager(
230 cgcx: &CodegenContext<Self>,
231 module: &ModuleCodegen<Self::Module>,
232 config: &ModuleConfig,
234 ) -> Result<(), FatalError> {
235 let diag_handler = cgcx.create_diag_handler();
236 back::lto::run_pass_manager(cgcx, &diag_handler, module, config, thin)
240 unsafe impl Send for LlvmCodegenBackend {} // Llvm is on a per-thread basis
241 unsafe impl Sync for LlvmCodegenBackend {}
243 impl LlvmCodegenBackend {
244 pub fn new() -> Box<dyn CodegenBackend> {
245 Box::new(LlvmCodegenBackend(()))
249 impl CodegenBackend for LlvmCodegenBackend {
250 fn init(&self, sess: &Session) {
251 llvm_util::init(sess); // Make sure llvm is inited
254 fn print(&self, req: PrintRequest, sess: &Session) {
256 PrintRequest::RelocationModels => {
257 println!("Available relocation models:");
268 println!(" {}", name);
272 PrintRequest::CodeModels => {
273 println!("Available code models:");
274 for name in &["tiny", "small", "kernel", "medium", "large"] {
275 println!(" {}", name);
279 PrintRequest::TlsModels => {
280 println!("Available TLS models:");
281 for name in &["global-dynamic", "local-dynamic", "initial-exec", "local-exec"] {
282 println!(" {}", name);
286 PrintRequest::StackProtectorStrategies => {
288 r#"Available stack protector strategies:
290 Generate stack canaries in all functions.
293 Generate stack canaries in a function if it either:
294 - has a local variable of `[T; N]` type, regardless of `T` and `N`
295 - takes the address of a local variable.
297 (Note that a local variable being borrowed is not equivalent to its
298 address being taken: e.g. some borrows may be removed by optimization,
299 while by-value argument passing may be implemented with reference to a
300 local stack variable in the ABI.)
303 Generate stack canaries in functions with:
304 - local variables of `[T; N]` type, where `T` is byte-sized and `N` > 8.
307 Do not generate stack canaries.
311 req => llvm_util::print(req, sess),
315 fn print_passes(&self) {
316 llvm_util::print_passes();
319 fn print_version(&self) {
320 llvm_util::print_version();
323 fn target_features(&self, sess: &Session) -> Vec<Symbol> {
324 target_features(sess)
327 fn codegen_crate<'tcx>(
330 metadata: EncodedMetadata,
331 need_metadata_module: bool,
333 Box::new(rustc_codegen_ssa::base::codegen_crate(
334 LlvmCodegenBackend(()),
336 crate::llvm_util::target_cpu(tcx.sess).to_string(),
338 need_metadata_module,
344 ongoing_codegen: Box<dyn Any>,
346 outputs: &OutputFilenames,
347 ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorReported> {
348 let (codegen_results, work_products) = ongoing_codegen
349 .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
350 .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
353 sess.time("llvm_dump_timing_file", || {
354 if sess.opts.debugging_opts.llvm_time_trace {
355 let file_name = outputs.with_extension("llvm_timings.json");
356 llvm_util::time_trace_profiler_finish(&file_name);
360 Ok((codegen_results, work_products))
366 codegen_results: CodegenResults,
367 outputs: &OutputFilenames,
368 ) -> Result<(), ErrorReported> {
369 use crate::back::archive::LlvmArchiveBuilder;
370 use rustc_codegen_ssa::back::link::link_binary;
372 // Run the linker on any artifacts that resulted from the LLVM run.
373 // This should produce either a finished executable or library.
374 link_binary::<LlvmArchiveBuilder<'_>>(sess, &codegen_results, outputs)
378 pub struct ModuleLlvm {
379 llcx: &'static mut llvm::Context,
380 llmod_raw: *const llvm::Module,
381 tm: &'static mut llvm::TargetMachine,
384 unsafe impl Send for ModuleLlvm {}
385 unsafe impl Sync for ModuleLlvm {}
388 fn new(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
390 let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
391 let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
392 ModuleLlvm { llmod_raw, llcx, tm: create_target_machine(tcx, mod_name) }
396 fn new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
398 let llcx = llvm::LLVMRustContextCreate(tcx.sess.fewer_names());
399 let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
400 ModuleLlvm { llmod_raw, llcx, tm: create_informational_target_machine(tcx.sess) }
405 cgcx: &CodegenContext<LlvmCodegenBackend>,
409 ) -> Result<Self, FatalError> {
411 let llcx = llvm::LLVMRustContextCreate(cgcx.fewer_names);
412 let llmod_raw = back::lto::parse_module(llcx, name, buffer, handler)?;
413 let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, name.to_str().unwrap());
414 let tm = match (cgcx.tm_factory)(tm_factory_config) {
417 handler.struct_err(&e).emit();
418 return Err(FatalError);
422 Ok(ModuleLlvm { llmod_raw, llcx, tm })
426 fn llmod(&self) -> &llvm::Module {
427 unsafe { &*self.llmod_raw }
431 impl Drop for ModuleLlvm {
434 llvm::LLVMRustDisposeTargetMachine(&mut *(self.tm as *mut _));
435 llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));