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