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