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