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