rustc_codegen_ssa/
lib.rs

1// tidy-alphabetical-start
2#![allow(internal_features)]
3#![allow(rustc::diagnostic_outside_of_impl)]
4#![allow(rustc::untranslatable_diagnostic)]
5#![cfg_attr(doc, recursion_limit = "256")] // FIXME(nnethercote): will be removed by #124141
6#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
7#![doc(rust_logo)]
8#![feature(assert_matches)]
9#![feature(box_patterns)]
10#![feature(debug_closure_helpers)]
11#![feature(file_buffered)]
12#![feature(if_let_guard)]
13#![feature(let_chains)]
14#![feature(negative_impls)]
15#![feature(rustdoc_internals)]
16#![feature(string_from_utf8_lossy_owned)]
17#![feature(trait_alias)]
18#![feature(try_blocks)]
19// tidy-alphabetical-end
20
21//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
22//! The backend-agnostic functions of this crate use functions defined in various traits that
23//! have to be implemented by each backend.
24
25use std::collections::BTreeSet;
26use std::io;
27use std::path::{Path, PathBuf};
28use std::sync::Arc;
29
30use rustc_ast as ast;
31use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
32use rustc_data_structures::unord::UnordMap;
33use rustc_hir::CRATE_HIR_ID;
34use rustc_hir::def_id::CrateNum;
35use rustc_macros::{Decodable, Encodable, HashStable};
36use rustc_middle::dep_graph::WorkProduct;
37use rustc_middle::lint::LintLevelSource;
38use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
39use rustc_middle::middle::dependency_format::Dependencies;
40use rustc_middle::middle::exported_symbols::SymbolExportKind;
41use rustc_middle::ty::TyCtxt;
42use rustc_middle::util::Providers;
43use rustc_serialize::opaque::{FileEncoder, MemDecoder};
44use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
45use rustc_session::Session;
46use rustc_session::config::{CrateType, OutputFilenames, OutputType, RUST_CGU_EXT};
47use rustc_session::cstore::{self, CrateSource};
48use rustc_session::lint::Level;
49use rustc_session::lint::builtin::LINKER_MESSAGES;
50use rustc_session::utils::NativeLibKind;
51use rustc_span::Symbol;
52
53pub mod assert_module_sources;
54pub mod back;
55pub mod base;
56pub mod codegen_attrs;
57pub mod common;
58pub mod debuginfo;
59pub mod errors;
60pub mod meth;
61pub mod mir;
62pub mod mono_item;
63pub mod size_of_val;
64pub mod target_features;
65pub mod traits;
66
67rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
68
69pub struct ModuleCodegen<M> {
70    /// The name of the module. When the crate may be saved between
71    /// compilations, incremental compilation requires that name be
72    /// unique amongst **all** crates. Therefore, it should contain
73    /// something unique to this crate (e.g., a module path) as well
74    /// as the crate name and disambiguator.
75    /// We currently generate these names via CodegenUnit::build_cgu_name().
76    pub name: String,
77    pub module_llvm: M,
78    pub kind: ModuleKind,
79    /// Saving the ThinLTO buffer for embedding in the object file.
80    pub thin_lto_buffer: Option<Vec<u8>>,
81}
82
83impl<M> ModuleCodegen<M> {
84    pub fn new_regular(name: impl Into<String>, module: M) -> Self {
85        Self {
86            name: name.into(),
87            module_llvm: module,
88            kind: ModuleKind::Regular,
89            thin_lto_buffer: None,
90        }
91    }
92
93    pub fn new_allocator(name: impl Into<String>, module: M) -> Self {
94        Self {
95            name: name.into(),
96            module_llvm: module,
97            kind: ModuleKind::Allocator,
98            thin_lto_buffer: None,
99        }
100    }
101
102    pub fn into_compiled_module(
103        self,
104        emit_obj: bool,
105        emit_dwarf_obj: bool,
106        emit_bc: bool,
107        emit_asm: bool,
108        emit_ir: bool,
109        outputs: &OutputFilenames,
110    ) -> CompiledModule {
111        let object = emit_obj.then(|| outputs.temp_path(OutputType::Object, Some(&self.name)));
112        let dwarf_object = emit_dwarf_obj.then(|| outputs.temp_path_dwo(Some(&self.name)));
113        let bytecode = emit_bc.then(|| outputs.temp_path(OutputType::Bitcode, Some(&self.name)));
114        let assembly = emit_asm.then(|| outputs.temp_path(OutputType::Assembly, Some(&self.name)));
115        let llvm_ir =
116            emit_ir.then(|| outputs.temp_path(OutputType::LlvmAssembly, Some(&self.name)));
117
118        CompiledModule {
119            name: self.name.clone(),
120            kind: self.kind,
121            object,
122            dwarf_object,
123            bytecode,
124            assembly,
125            llvm_ir,
126            links_from_incr_cache: Vec::new(),
127        }
128    }
129}
130
131#[derive(Debug, Encodable, Decodable)]
132pub struct CompiledModule {
133    pub name: String,
134    pub kind: ModuleKind,
135    pub object: Option<PathBuf>,
136    pub dwarf_object: Option<PathBuf>,
137    pub bytecode: Option<PathBuf>,
138    pub assembly: Option<PathBuf>, // --emit=asm
139    pub llvm_ir: Option<PathBuf>,  // --emit=llvm-ir, llvm-bc is in bytecode
140    pub links_from_incr_cache: Vec<PathBuf>,
141}
142
143impl CompiledModule {
144    /// Call `emit` function with every artifact type currently compiled
145    pub fn for_each_output(&self, mut emit: impl FnMut(&Path, OutputType)) {
146        if let Some(path) = self.object.as_deref() {
147            emit(path, OutputType::Object);
148        }
149        if let Some(path) = self.bytecode.as_deref() {
150            emit(path, OutputType::Bitcode);
151        }
152        if let Some(path) = self.llvm_ir.as_deref() {
153            emit(path, OutputType::LlvmAssembly);
154        }
155        if let Some(path) = self.assembly.as_deref() {
156            emit(path, OutputType::Assembly);
157        }
158    }
159}
160
161pub(crate) struct CachedModuleCodegen {
162    pub name: String,
163    pub source: WorkProduct,
164}
165
166#[derive(Copy, Clone, Debug, PartialEq, Encodable, Decodable)]
167pub enum ModuleKind {
168    Regular,
169    Metadata,
170    Allocator,
171}
172
173bitflags::bitflags! {
174    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
175    pub struct MemFlags: u8 {
176        const VOLATILE = 1 << 0;
177        const NONTEMPORAL = 1 << 1;
178        const UNALIGNED = 1 << 2;
179    }
180}
181
182#[derive(Clone, Debug, Encodable, Decodable, HashStable)]
183pub struct NativeLib {
184    pub kind: NativeLibKind,
185    pub name: Symbol,
186    pub filename: Option<Symbol>,
187    pub cfg: Option<ast::MetaItemInner>,
188    pub verbatim: bool,
189    pub dll_imports: Vec<cstore::DllImport>,
190}
191
192impl From<&cstore::NativeLib> for NativeLib {
193    fn from(lib: &cstore::NativeLib) -> Self {
194        NativeLib {
195            kind: lib.kind,
196            filename: lib.filename,
197            name: lib.name,
198            cfg: lib.cfg.clone(),
199            verbatim: lib.verbatim.unwrap_or(false),
200            dll_imports: lib.dll_imports.clone(),
201        }
202    }
203}
204
205/// Misc info we load from metadata to persist beyond the tcx.
206///
207/// Note: though `CrateNum` is only meaningful within the same tcx, information within `CrateInfo`
208/// is self-contained. `CrateNum` can be viewed as a unique identifier within a `CrateInfo`, where
209/// `used_crate_source` contains all `CrateSource` of the dependents, and maintains a mapping from
210/// identifiers (`CrateNum`) to `CrateSource`. The other fields map `CrateNum` to the crate's own
211/// additional properties, so that effectively we can retrieve each dependent crate's `CrateSource`
212/// and the corresponding properties without referencing information outside of a `CrateInfo`.
213#[derive(Debug, Encodable, Decodable)]
214pub struct CrateInfo {
215    pub target_cpu: String,
216    pub target_features: Vec<String>,
217    pub crate_types: Vec<CrateType>,
218    pub exported_symbols: UnordMap<CrateType, Vec<String>>,
219    pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
220    pub local_crate_name: Symbol,
221    pub compiler_builtins: Option<CrateNum>,
222    pub profiler_runtime: Option<CrateNum>,
223    pub is_no_builtins: FxHashSet<CrateNum>,
224    pub native_libraries: FxIndexMap<CrateNum, Vec<NativeLib>>,
225    pub crate_name: UnordMap<CrateNum, Symbol>,
226    pub used_libraries: Vec<NativeLib>,
227    pub used_crate_source: UnordMap<CrateNum, Arc<CrateSource>>,
228    pub used_crates: Vec<CrateNum>,
229    pub dependency_formats: Arc<Dependencies>,
230    pub windows_subsystem: Option<String>,
231    pub natvis_debugger_visualizers: BTreeSet<DebuggerVisualizerFile>,
232    pub lint_levels: CodegenLintLevels,
233}
234
235#[derive(Encodable, Decodable)]
236pub struct CodegenResults {
237    pub modules: Vec<CompiledModule>,
238    pub allocator_module: Option<CompiledModule>,
239    pub metadata_module: Option<CompiledModule>,
240    pub metadata: rustc_metadata::EncodedMetadata,
241    pub crate_info: CrateInfo,
242}
243
244pub enum CodegenErrors {
245    WrongFileType,
246    EmptyVersionNumber,
247    EncodingVersionMismatch { version_array: String, rlink_version: u32 },
248    RustcVersionMismatch { rustc_version: String },
249    CorruptFile,
250}
251
252pub fn provide(providers: &mut Providers) {
253    crate::back::symbol_export::provide(providers);
254    crate::base::provide(providers);
255    crate::target_features::provide(providers);
256    crate::codegen_attrs::provide(providers);
257    providers.queries.global_backend_features = |_tcx: TyCtxt<'_>, ()| vec![];
258}
259
260/// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`
261/// uses for the object files it generates.
262pub fn looks_like_rust_object_file(filename: &str) -> bool {
263    let path = Path::new(filename);
264    let ext = path.extension().and_then(|s| s.to_str());
265    if ext != Some(OutputType::Object.extension()) {
266        // The file name does not end with ".o", so it can't be an object file.
267        return false;
268    }
269
270    // Strip the ".o" at the end
271    let ext2 = path.file_stem().and_then(|s| Path::new(s).extension()).and_then(|s| s.to_str());
272
273    // Check if the "inner" extension
274    ext2 == Some(RUST_CGU_EXT)
275}
276
277const RLINK_VERSION: u32 = 1;
278const RLINK_MAGIC: &[u8] = b"rustlink";
279
280impl CodegenResults {
281    pub fn serialize_rlink(
282        sess: &Session,
283        rlink_file: &Path,
284        codegen_results: &CodegenResults,
285        outputs: &OutputFilenames,
286    ) -> Result<usize, io::Error> {
287        let mut encoder = FileEncoder::new(rlink_file)?;
288        encoder.emit_raw_bytes(RLINK_MAGIC);
289        // `emit_raw_bytes` is used to make sure that the version representation does not depend on
290        // Encoder's inner representation of `u32`.
291        encoder.emit_raw_bytes(&RLINK_VERSION.to_be_bytes());
292        encoder.emit_str(sess.cfg_version);
293        Encodable::encode(codegen_results, &mut encoder);
294        Encodable::encode(outputs, &mut encoder);
295        encoder.finish().map_err(|(_path, err)| err)
296    }
297
298    pub fn deserialize_rlink(
299        sess: &Session,
300        data: Vec<u8>,
301    ) -> Result<(Self, OutputFilenames), CodegenErrors> {
302        // The Decodable machinery is not used here because it panics if the input data is invalid
303        // and because its internal representation may change.
304        if !data.starts_with(RLINK_MAGIC) {
305            return Err(CodegenErrors::WrongFileType);
306        }
307        let data = &data[RLINK_MAGIC.len()..];
308        if data.len() < 4 {
309            return Err(CodegenErrors::EmptyVersionNumber);
310        }
311
312        let mut version_array: [u8; 4] = Default::default();
313        version_array.copy_from_slice(&data[..4]);
314        if u32::from_be_bytes(version_array) != RLINK_VERSION {
315            return Err(CodegenErrors::EncodingVersionMismatch {
316                version_array: String::from_utf8_lossy(&version_array).to_string(),
317                rlink_version: RLINK_VERSION,
318            });
319        }
320
321        let Ok(mut decoder) = MemDecoder::new(&data[4..], 0) else {
322            return Err(CodegenErrors::CorruptFile);
323        };
324        let rustc_version = decoder.read_str();
325        if rustc_version != sess.cfg_version {
326            return Err(CodegenErrors::RustcVersionMismatch {
327                rustc_version: rustc_version.to_string(),
328            });
329        }
330
331        let codegen_results = CodegenResults::decode(&mut decoder);
332        let outputs = OutputFilenames::decode(&mut decoder);
333        Ok((codegen_results, outputs))
334    }
335}
336
337/// A list of lint levels used in codegen.
338///
339/// When using `-Z link-only`, we don't have access to the tcx and must work
340/// solely from the `.rlink` file. `Lint`s are defined too early to be encodeable.
341/// Instead, encode exactly the information we need.
342#[derive(Copy, Clone, Debug, Encodable, Decodable)]
343pub struct CodegenLintLevels {
344    linker_messages: (Level, LintLevelSource),
345}
346
347impl CodegenLintLevels {
348    pub fn from_tcx(tcx: TyCtxt<'_>) -> Self {
349        Self { linker_messages: tcx.lint_level_at_node(LINKER_MESSAGES, CRATE_HIR_ID) }
350    }
351}