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