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