Skip to main content

rustc_codegen_llvm/
lib.rs

1//! The Rust compiler.
2//!
3//! # Note
4//!
5//! This API is completely unstable and subject to change.
6
7// tidy-alphabetical-start
8#![feature(assert_matches)]
9#![feature(extern_types)]
10#![feature(file_buffered)]
11#![feature(if_let_guard)]
12#![feature(impl_trait_in_assoc_type)]
13#![feature(iter_intersperse)]
14#![feature(macro_derive)]
15#![feature(once_cell_try)]
16#![feature(trim_prefix_suffix)]
17#![feature(try_blocks)]
18// tidy-alphabetical-end
19
20use std::any::Any;
21use std::ffi::CStr;
22use std::mem::ManuallyDrop;
23use std::path::PathBuf;
24
25use back::owned_target_machine::OwnedTargetMachine;
26use back::write::{create_informational_target_machine, create_target_machine};
27use context::SimpleCx;
28use errors::ParseTargetMachineConfig;
29use llvm_util::target_config;
30use rustc_ast::expand::allocator::AllocatorMethod;
31use rustc_codegen_ssa::back::lto::{SerializedModule, ThinModule};
32use rustc_codegen_ssa::back::write::{
33    CodegenContext, FatLtoInput, ModuleConfig, SharedEmitter, TargetMachineFactoryConfig,
34    TargetMachineFactoryFn,
35};
36use rustc_codegen_ssa::traits::*;
37use rustc_codegen_ssa::{CodegenResults, CompiledModule, ModuleCodegen, TargetConfig};
38use rustc_data_structures::fx::FxIndexMap;
39use rustc_errors::{DiagCtxt, DiagCtxtHandle};
40use rustc_metadata::EncodedMetadata;
41use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
42use rustc_middle::ty::TyCtxt;
43use rustc_middle::util::Providers;
44use rustc_session::Session;
45use rustc_session::config::{OptLevel, OutputFilenames, PrintKind, PrintRequest};
46use rustc_span::{Symbol, sym};
47use rustc_target::spec::{RelocModel, TlsModel};
48
49use crate::llvm::ToLlvmBool;
50
51mod abi;
52mod allocator;
53mod asm;
54mod attributes;
55mod back;
56mod base;
57mod builder;
58mod callee;
59mod common;
60mod consts;
61mod context;
62mod coverageinfo;
63mod debuginfo;
64mod declare;
65mod errors;
66mod intrinsic;
67mod llvm;
68mod llvm_util;
69mod macros;
70mod mono_item;
71mod type_;
72mod type_of;
73mod typetree;
74mod va_arg;
75mod value;
76
77pub(crate) use macros::TryFromU32;
78
79#[derive(#[automatically_derived]
impl ::core::clone::Clone for LlvmCodegenBackend {
    #[inline]
    fn clone(&self) -> LlvmCodegenBackend {
        LlvmCodegenBackend(::core::clone::Clone::clone(&self.0))
    }
}Clone)]
80pub struct LlvmCodegenBackend(());
81
82struct TimeTraceProfiler {
83    enabled: bool,
84}
85
86impl TimeTraceProfiler {
87    fn new(enabled: bool) -> Self {
88        if enabled {
89            unsafe { llvm::LLVMRustTimeTraceProfilerInitialize() }
90        }
91        TimeTraceProfiler { enabled }
92    }
93}
94
95impl Drop for TimeTraceProfiler {
96    fn drop(&mut self) {
97        if self.enabled {
98            unsafe { llvm::LLVMRustTimeTraceProfilerFinishThread() }
99        }
100    }
101}
102
103impl ExtraBackendMethods for LlvmCodegenBackend {
104    fn codegen_allocator<'tcx>(
105        &self,
106        tcx: TyCtxt<'tcx>,
107        module_name: &str,
108        methods: &[AllocatorMethod],
109    ) -> ModuleLlvm {
110        let module_llvm = ModuleLlvm::new_metadata(tcx, module_name);
111        let cx =
112            SimpleCx::new(module_llvm.llmod(), &module_llvm.llcx, tcx.data_layout.pointer_size());
113        unsafe {
114            allocator::codegen(tcx, cx, module_name, methods);
115        }
116        module_llvm
117    }
118    fn compile_codegen_unit(
119        &self,
120        tcx: TyCtxt<'_>,
121        cgu_name: Symbol,
122    ) -> (ModuleCodegen<ModuleLlvm>, u64) {
123        base::compile_codegen_unit(tcx, cgu_name)
124    }
125    fn target_machine_factory(
126        &self,
127        sess: &Session,
128        optlvl: OptLevel,
129        target_features: &[String],
130    ) -> TargetMachineFactoryFn<Self> {
131        back::write::target_machine_factory(sess, optlvl, target_features)
132    }
133
134    fn spawn_named_thread<F, T>(
135        time_trace: bool,
136        name: String,
137        f: F,
138    ) -> std::io::Result<std::thread::JoinHandle<T>>
139    where
140        F: FnOnce() -> T,
141        F: Send + 'static,
142        T: Send + 'static,
143    {
144        std::thread::Builder::new().name(name).spawn(move || {
145            let _profiler = TimeTraceProfiler::new(time_trace);
146            f()
147        })
148    }
149}
150
151impl WriteBackendMethods for LlvmCodegenBackend {
152    type Module = ModuleLlvm;
153    type ModuleBuffer = back::lto::ModuleBuffer;
154    type TargetMachine = OwnedTargetMachine;
155    type TargetMachineError = crate::errors::LlvmError<'static>;
156    type ThinData = back::lto::ThinData;
157    type ThinBuffer = back::lto::ThinBuffer;
158    fn print_pass_timings(&self) {
159        let timings = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintPassTimings(s) }).unwrap();
160        { ::std::io::_print(format_args!("{0}", timings)); };print!("{timings}");
161    }
162    fn print_statistics(&self) {
163        let stats = llvm::build_string(|s| unsafe { llvm::LLVMRustPrintStatistics(s) }).unwrap();
164        { ::std::io::_print(format_args!("{0}", stats)); };print!("{stats}");
165    }
166    fn run_and_optimize_fat_lto(
167        cgcx: &CodegenContext<Self>,
168        shared_emitter: &SharedEmitter,
169        exported_symbols_for_lto: &[String],
170        each_linked_rlib_for_lto: &[PathBuf],
171        modules: Vec<FatLtoInput<Self>>,
172    ) -> ModuleCodegen<Self::Module> {
173        let mut module = back::lto::run_fat(
174            cgcx,
175            shared_emitter,
176            exported_symbols_for_lto,
177            each_linked_rlib_for_lto,
178            modules,
179        );
180
181        let dcx = DiagCtxt::new(Box::new(shared_emitter.clone()));
182        let dcx = dcx.handle();
183        back::lto::run_pass_manager(cgcx, dcx, &mut module, false);
184
185        module
186    }
187    fn run_thin_lto(
188        cgcx: &CodegenContext<Self>,
189        dcx: DiagCtxtHandle<'_>,
190        exported_symbols_for_lto: &[String],
191        each_linked_rlib_for_lto: &[PathBuf],
192        modules: Vec<(String, Self::ThinBuffer)>,
193        cached_modules: Vec<(SerializedModule<Self::ModuleBuffer>, WorkProduct)>,
194    ) -> (Vec<ThinModule<Self>>, Vec<WorkProduct>) {
195        back::lto::run_thin(
196            cgcx,
197            dcx,
198            exported_symbols_for_lto,
199            each_linked_rlib_for_lto,
200            modules,
201            cached_modules,
202        )
203    }
204    fn optimize(
205        cgcx: &CodegenContext<Self>,
206        shared_emitter: &SharedEmitter,
207        module: &mut ModuleCodegen<Self::Module>,
208        config: &ModuleConfig,
209    ) {
210        back::write::optimize(cgcx, shared_emitter, module, config)
211    }
212    fn optimize_thin(
213        cgcx: &CodegenContext<Self>,
214        shared_emitter: &SharedEmitter,
215        thin: ThinModule<Self>,
216    ) -> ModuleCodegen<Self::Module> {
217        back::lto::optimize_thin_module(cgcx, shared_emitter, thin)
218    }
219    fn codegen(
220        cgcx: &CodegenContext<Self>,
221        shared_emitter: &SharedEmitter,
222        module: ModuleCodegen<Self::Module>,
223        config: &ModuleConfig,
224    ) -> CompiledModule {
225        back::write::codegen(cgcx, shared_emitter, module, config)
226    }
227    fn prepare_thin(module: ModuleCodegen<Self::Module>) -> (String, Self::ThinBuffer) {
228        back::lto::prepare_thin(module)
229    }
230    fn serialize_module(module: ModuleCodegen<Self::Module>) -> (String, Self::ModuleBuffer) {
231        (module.name, back::lto::ModuleBuffer::new(module.module_llvm.llmod()))
232    }
233}
234
235impl LlvmCodegenBackend {
236    pub fn new() -> Box<dyn CodegenBackend> {
237        Box::new(LlvmCodegenBackend(()))
238    }
239}
240
241impl CodegenBackend for LlvmCodegenBackend {
242    fn name(&self) -> &'static str {
243        "llvm"
244    }
245
246    fn init(&self, sess: &Session) {
247        llvm_util::init(sess); // Make sure llvm is inited
248
249        // autodiff is based on Enzyme, a library which we might not have available, when it was
250        // neither build, nor downloaded via rustup. If autodiff is used, but not available we emit
251        // an early error here and abort compilation.
252        {
253            use rustc_session::config::AutoDiff;
254
255            use crate::back::lto::enable_autodiff_settings;
256            if sess.opts.unstable_opts.autodiff.contains(&AutoDiff::Enable) {
257                match llvm::EnzymeWrapper::get_or_init(&sess.opts.sysroot) {
258                    Ok(_) => {}
259                    Err(llvm::EnzymeLibraryError::NotFound { err }) => {
260                        sess.dcx().emit_fatal(crate::errors::AutoDiffComponentMissing { err });
261                    }
262                    Err(llvm::EnzymeLibraryError::LoadFailed { err }) => {
263                        sess.dcx().emit_fatal(crate::errors::AutoDiffComponentUnavailable { err });
264                    }
265                }
266                enable_autodiff_settings(&sess.opts.unstable_opts.autodiff);
267            }
268        }
269    }
270
271    fn provide(&self, providers: &mut Providers) {
272        providers.queries.global_backend_features =
273            |tcx, ()| llvm_util::global_llvm_features(tcx.sess, false)
274    }
275
276    fn print(&self, req: &PrintRequest, out: &mut String, sess: &Session) {
277        use std::fmt::Write;
278        match req.kind {
279            PrintKind::RelocationModels => {
280                out.write_fmt(format_args!("Available relocation models:\n"))writeln!(out, "Available relocation models:").unwrap();
281                for name in RelocModel::ALL.iter().map(RelocModel::desc).chain(["default"]) {
282                    out.write_fmt(format_args!("    {0}\n", name))writeln!(out, "    {name}").unwrap();
283                }
284                out.write_fmt(format_args!("\n"))writeln!(out).unwrap();
285            }
286            PrintKind::CodeModels => {
287                out.write_fmt(format_args!("Available code models:\n"))writeln!(out, "Available code models:").unwrap();
288                for name in &["tiny", "small", "kernel", "medium", "large"] {
289                    out.write_fmt(format_args!("    {0}\n", name))writeln!(out, "    {name}").unwrap();
290                }
291                out.write_fmt(format_args!("\n"))writeln!(out).unwrap();
292            }
293            PrintKind::TlsModels => {
294                out.write_fmt(format_args!("Available TLS models:\n"))writeln!(out, "Available TLS models:").unwrap();
295                for name in TlsModel::ALL.iter().map(TlsModel::desc) {
296                    out.write_fmt(format_args!("    {0}\n", name))writeln!(out, "    {name}").unwrap();
297                }
298                out.write_fmt(format_args!("\n"))writeln!(out).unwrap();
299            }
300            PrintKind::StackProtectorStrategies => {
301                out.write_fmt(format_args!("Available stack protector strategies:\n    all\n        Generate stack canaries in all functions.\n\n    strong\n        Generate stack canaries in a function if it either:\n        - has a local variable of `[T; N]` type, regardless of `T` and `N`\n        - takes the address of a local variable.\n\n          (Note that a local variable being borrowed is not equivalent to its\n          address being taken: e.g. some borrows may be removed by optimization,\n          while by-value argument passing may be implemented with reference to a\n          local stack variable in the ABI.)\n\n    basic\n        Generate stack canaries in functions with local variables of `[T; N]`\n        type, where `T` is byte-sized and `N` >= 8.\n\n    none\n        Do not generate stack canaries.\n\n"))writeln!(
302                    out,
303                    r#"Available stack protector strategies:
304    all
305        Generate stack canaries in all functions.
306
307    strong
308        Generate stack canaries in a function if it either:
309        - has a local variable of `[T; N]` type, regardless of `T` and `N`
310        - takes the address of a local variable.
311
312          (Note that a local variable being borrowed is not equivalent to its
313          address being taken: e.g. some borrows may be removed by optimization,
314          while by-value argument passing may be implemented with reference to a
315          local stack variable in the ABI.)
316
317    basic
318        Generate stack canaries in functions with local variables of `[T; N]`
319        type, where `T` is byte-sized and `N` >= 8.
320
321    none
322        Do not generate stack canaries.
323"#
324                )
325                .unwrap();
326            }
327            _other => llvm_util::print(req, out, sess),
328        }
329    }
330
331    fn print_passes(&self) {
332        llvm_util::print_passes();
333    }
334
335    fn print_version(&self) {
336        llvm_util::print_version();
337    }
338
339    fn has_zstd(&self) -> bool {
340        llvm::LLVMRustLLVMHasZstdCompression()
341    }
342
343    fn target_config(&self, sess: &Session) -> TargetConfig {
344        target_config(sess)
345    }
346
347    fn replaced_intrinsics(&self) -> Vec<Symbol> {
348        <[_]>::into_vec(::alloc::boxed::box_new([sym::unchecked_funnel_shl,
                sym::unchecked_funnel_shr, sym::carrying_mul_add]))vec![sym::unchecked_funnel_shl, sym::unchecked_funnel_shr, sym::carrying_mul_add]
349    }
350
351    fn codegen_crate<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Box<dyn Any> {
352        Box::new(rustc_codegen_ssa::base::codegen_crate(
353            LlvmCodegenBackend(()),
354            tcx,
355            crate::llvm_util::target_cpu(tcx.sess).to_string(),
356        ))
357    }
358
359    fn join_codegen(
360        &self,
361        ongoing_codegen: Box<dyn Any>,
362        sess: &Session,
363        outputs: &OutputFilenames,
364    ) -> (CodegenResults, FxIndexMap<WorkProductId, WorkProduct>) {
365        let (codegen_results, work_products) = ongoing_codegen
366            .downcast::<rustc_codegen_ssa::back::write::OngoingCodegen<LlvmCodegenBackend>>()
367            .expect("Expected LlvmCodegenBackend's OngoingCodegen, found Box<Any>")
368            .join(sess);
369
370        if sess.opts.unstable_opts.llvm_time_trace {
371            sess.time("llvm_dump_timing_file", || {
372                let file_name = outputs.with_extension("llvm_timings.json");
373                llvm_util::time_trace_profiler_finish(&file_name);
374            });
375        }
376
377        (codegen_results, work_products)
378    }
379
380    fn link(
381        &self,
382        sess: &Session,
383        codegen_results: CodegenResults,
384        metadata: EncodedMetadata,
385        outputs: &OutputFilenames,
386    ) {
387        use rustc_codegen_ssa::back::link::link_binary;
388
389        use crate::back::archive::LlvmArchiveBuilderBuilder;
390
391        // Run the linker on any artifacts that resulted from the LLVM run.
392        // This should produce either a finished executable or library.
393        link_binary(
394            sess,
395            &LlvmArchiveBuilderBuilder,
396            codegen_results,
397            metadata,
398            outputs,
399            self.name(),
400        );
401    }
402}
403
404pub struct ModuleLlvm {
405    llcx: &'static mut llvm::Context,
406    llmod_raw: *const llvm::Module,
407
408    // This field is `ManuallyDrop` because it is important that the `TargetMachine`
409    // is disposed prior to the `Context` being disposed otherwise UAFs can occur.
410    tm: ManuallyDrop<OwnedTargetMachine>,
411}
412
413unsafe impl Send for ModuleLlvm {}
414unsafe impl Sync for ModuleLlvm {}
415
416impl ModuleLlvm {
417    fn new(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
418        unsafe {
419            let llcx = llvm::LLVMContextCreate();
420            llvm::LLVMContextSetDiscardValueNames(llcx, tcx.sess.fewer_names().to_llvm_bool());
421            let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
422            ModuleLlvm {
423                llmod_raw,
424                llcx,
425                tm: ManuallyDrop::new(create_target_machine(tcx, mod_name)),
426            }
427        }
428    }
429
430    fn new_metadata(tcx: TyCtxt<'_>, mod_name: &str) -> Self {
431        unsafe {
432            let llcx = llvm::LLVMContextCreate();
433            llvm::LLVMContextSetDiscardValueNames(llcx, tcx.sess.fewer_names().to_llvm_bool());
434            let llmod_raw = context::create_module(tcx, llcx, mod_name) as *const _;
435            ModuleLlvm {
436                llmod_raw,
437                llcx,
438                tm: ManuallyDrop::new(create_informational_target_machine(tcx.sess, false)),
439            }
440        }
441    }
442
443    fn tm_from_cgcx(
444        cgcx: &CodegenContext<LlvmCodegenBackend>,
445        name: &str,
446        dcx: DiagCtxtHandle<'_>,
447    ) -> OwnedTargetMachine {
448        let tm_factory_config = TargetMachineFactoryConfig::new(cgcx, name);
449        match (cgcx.tm_factory)(tm_factory_config) {
450            Ok(m) => m,
451            Err(e) => {
452                dcx.emit_fatal(ParseTargetMachineConfig(e));
453            }
454        }
455    }
456
457    fn parse(
458        cgcx: &CodegenContext<LlvmCodegenBackend>,
459        name: &CStr,
460        buffer: &[u8],
461        dcx: DiagCtxtHandle<'_>,
462    ) -> Self {
463        unsafe {
464            let llcx = llvm::LLVMContextCreate();
465            llvm::LLVMContextSetDiscardValueNames(llcx, cgcx.fewer_names.to_llvm_bool());
466            let llmod_raw = back::lto::parse_module(llcx, name, buffer, dcx);
467            let tm = ModuleLlvm::tm_from_cgcx(cgcx, name.to_str().unwrap(), dcx);
468
469            ModuleLlvm { llmod_raw, llcx, tm: ManuallyDrop::new(tm) }
470        }
471    }
472
473    fn llmod(&self) -> &llvm::Module {
474        unsafe { &*self.llmod_raw }
475    }
476}
477
478impl Drop for ModuleLlvm {
479    fn drop(&mut self) {
480        unsafe {
481            ManuallyDrop::drop(&mut self.tm);
482            llvm::LLVMContextDispose(&mut *(self.llcx as *mut _));
483        }
484    }
485}