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