]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/lib.rs
Auto merge of #97870 - eggyal:inplace_fold_spec, r=wesleywiser
[rust.git] / compiler / rustc_codegen_cranelift / src / lib.rs
1 #![feature(rustc_private)]
2 // Note: please avoid adding other feature gates where possible
3 #![warn(rust_2018_idioms)]
4 #![warn(unused_lifetimes)]
5 #![warn(unreachable_pub)]
6
7 extern crate jobserver;
8 #[macro_use]
9 extern crate rustc_middle;
10 extern crate rustc_ast;
11 extern crate rustc_codegen_ssa;
12 extern crate rustc_data_structures;
13 extern crate rustc_errors;
14 extern crate rustc_fs_util;
15 extern crate rustc_hir;
16 extern crate rustc_incremental;
17 extern crate rustc_index;
18 extern crate rustc_interface;
19 extern crate rustc_metadata;
20 extern crate rustc_session;
21 extern crate rustc_span;
22 extern crate rustc_target;
23
24 // This prevents duplicating functions and statics that are already part of the host rustc process.
25 #[allow(unused_extern_crates)]
26 extern crate rustc_driver;
27
28 use std::any::Any;
29 use std::cell::{Cell, RefCell};
30 use std::sync::Arc;
31
32 use rustc_codegen_ssa::traits::CodegenBackend;
33 use rustc_codegen_ssa::CodegenResults;
34 use rustc_data_structures::profiling::SelfProfilerRef;
35 use rustc_errors::ErrorGuaranteed;
36 use rustc_metadata::EncodedMetadata;
37 use rustc_middle::dep_graph::{WorkProduct, WorkProductId};
38 use rustc_session::config::OutputFilenames;
39 use rustc_session::Session;
40 use rustc_span::Symbol;
41
42 use cranelift_codegen::isa::TargetIsa;
43 use cranelift_codegen::settings::{self, Configurable};
44
45 pub use crate::config::*;
46 use crate::prelude::*;
47
48 mod abi;
49 mod allocator;
50 mod analyze;
51 mod archive;
52 mod base;
53 mod cast;
54 mod codegen_i128;
55 mod common;
56 mod compiler_builtins;
57 mod concurrency_limiter;
58 mod config;
59 mod constant;
60 mod debuginfo;
61 mod discriminant;
62 mod driver;
63 mod global_asm;
64 mod inline_asm;
65 mod intrinsics;
66 mod linkage;
67 mod main_shim;
68 mod num;
69 mod optimize;
70 mod pointer;
71 mod pretty_clif;
72 mod toolchain;
73 mod trap;
74 mod unsize;
75 mod value_and_place;
76 mod vtable;
77
78 mod prelude {
79     pub(crate) use rustc_span::{FileNameDisplayPreference, Span};
80
81     pub(crate) use rustc_hir::def_id::{DefId, LOCAL_CRATE};
82     pub(crate) use rustc_middle::bug;
83     pub(crate) use rustc_middle::mir::{self, *};
84     pub(crate) use rustc_middle::ty::layout::{self, LayoutOf, TyAndLayout};
85     pub(crate) use rustc_middle::ty::{
86         self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut,
87         TypeFoldable, TypeVisitable, UintTy,
88     };
89     pub(crate) use rustc_target::abi::{Abi, Scalar, Size, VariantIdx};
90
91     pub(crate) use rustc_data_structures::fx::FxHashMap;
92
93     pub(crate) use rustc_index::vec::Idx;
94
95     pub(crate) use cranelift_codegen::ir::condcodes::{FloatCC, IntCC};
96     pub(crate) use cranelift_codegen::ir::function::Function;
97     pub(crate) use cranelift_codegen::ir::types;
98     pub(crate) use cranelift_codegen::ir::{
99         AbiParam, Block, FuncRef, Inst, InstBuilder, MemFlags, Signature, SourceLoc, StackSlot,
100         StackSlotData, StackSlotKind, TrapCode, Type, Value,
101     };
102     pub(crate) use cranelift_codegen::isa::{self, CallConv};
103     pub(crate) use cranelift_codegen::Context;
104     pub(crate) use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
105     pub(crate) use cranelift_module::{self, DataContext, FuncId, Linkage, Module};
106
107     pub(crate) use crate::abi::*;
108     pub(crate) use crate::base::{codegen_operand, codegen_place};
109     pub(crate) use crate::cast::*;
110     pub(crate) use crate::common::*;
111     pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
112     pub(crate) use crate::pointer::Pointer;
113     pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
114 }
115
116 struct PrintOnPanic<F: Fn() -> String>(F);
117 impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
118     fn drop(&mut self) {
119         if ::std::thread::panicking() {
120             println!("{}", (self.0)());
121         }
122     }
123 }
124
125 /// The codegen context holds any information shared between the codegen of individual functions
126 /// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
127 struct CodegenCx {
128     profiler: SelfProfilerRef,
129     output_filenames: Arc<OutputFilenames>,
130     should_write_ir: bool,
131     global_asm: String,
132     inline_asm_index: Cell<usize>,
133     debug_context: Option<DebugContext>,
134     unwind_context: UnwindContext,
135     cgu_name: Symbol,
136 }
137
138 impl CodegenCx {
139     fn new(
140         tcx: TyCtxt<'_>,
141         backend_config: BackendConfig,
142         isa: &dyn TargetIsa,
143         debug_info: bool,
144         cgu_name: Symbol,
145     ) -> Self {
146         assert_eq!(pointer_ty(tcx), isa.pointer_type());
147
148         let unwind_context =
149             UnwindContext::new(isa, matches!(backend_config.codegen_mode, CodegenMode::Aot));
150         let debug_context = if debug_info && !tcx.sess.target.options.is_like_windows {
151             Some(DebugContext::new(tcx, isa))
152         } else {
153             None
154         };
155         CodegenCx {
156             profiler: tcx.prof.clone(),
157             output_filenames: tcx.output_filenames(()).clone(),
158             should_write_ir: crate::pretty_clif::should_write_ir(tcx),
159             global_asm: String::new(),
160             inline_asm_index: Cell::new(0),
161             debug_context,
162             unwind_context,
163             cgu_name,
164         }
165     }
166 }
167
168 pub struct CraneliftCodegenBackend {
169     pub config: RefCell<Option<BackendConfig>>,
170 }
171
172 impl CodegenBackend for CraneliftCodegenBackend {
173     fn init(&self, sess: &Session) {
174         use rustc_session::config::Lto;
175         match sess.lto() {
176             Lto::No | Lto::ThinLocal => {}
177             Lto::Thin | Lto::Fat => sess.warn("LTO is not supported. You may get a linker error."),
178         }
179
180         let mut config = self.config.borrow_mut();
181         if config.is_none() {
182             let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
183                 .unwrap_or_else(|err| sess.fatal(&err));
184             *config = Some(new_config);
185         }
186     }
187
188     fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
189         vec![]
190     }
191
192     fn print_version(&self) {
193         println!("Cranelift version: {}", cranelift_codegen::VERSION);
194     }
195
196     fn codegen_crate(
197         &self,
198         tcx: TyCtxt<'_>,
199         metadata: EncodedMetadata,
200         need_metadata_module: bool,
201     ) -> Box<dyn Any> {
202         tcx.sess.abort_if_errors();
203         let config = self.config.borrow().clone().unwrap();
204         match config.codegen_mode {
205             CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
206             CodegenMode::Jit | CodegenMode::JitLazy => {
207                 #[cfg(feature = "jit")]
208                 driver::jit::run_jit(tcx, config);
209
210                 #[cfg(not(feature = "jit"))]
211                 tcx.sess.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
212             }
213         }
214     }
215
216     fn join_codegen(
217         &self,
218         ongoing_codegen: Box<dyn Any>,
219         sess: &Session,
220         _outputs: &OutputFilenames,
221     ) -> Result<(CodegenResults, FxHashMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
222         Ok(ongoing_codegen
223             .downcast::<driver::aot::OngoingCodegen>()
224             .unwrap()
225             .join(sess, self.config.borrow().as_ref().unwrap()))
226     }
227
228     fn link(
229         &self,
230         sess: &Session,
231         codegen_results: CodegenResults,
232         outputs: &OutputFilenames,
233     ) -> Result<(), ErrorGuaranteed> {
234         use rustc_codegen_ssa::back::link::link_binary;
235
236         link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
237     }
238 }
239
240 fn target_triple(sess: &Session) -> target_lexicon::Triple {
241     match sess.target.llvm_target.parse() {
242         Ok(triple) => triple,
243         Err(err) => sess.fatal(&format!("target not recognized: {}", err)),
244     }
245 }
246
247 fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Box<dyn isa::TargetIsa + 'static> {
248     use target_lexicon::BinaryFormat;
249
250     let target_triple = crate::target_triple(sess);
251
252     let mut flags_builder = settings::builder();
253     flags_builder.enable("is_pic").unwrap();
254     let enable_verifier = if backend_config.enable_verifier { "true" } else { "false" };
255     flags_builder.set("enable_verifier", enable_verifier).unwrap();
256     flags_builder.set("regalloc_checker", enable_verifier).unwrap();
257
258     let tls_model = match target_triple.binary_format {
259         BinaryFormat::Elf => "elf_gd",
260         BinaryFormat::Macho => "macho",
261         BinaryFormat::Coff => "coff",
262         _ => "none",
263     };
264     flags_builder.set("tls_model", tls_model).unwrap();
265
266     flags_builder.set("enable_simd", "true").unwrap();
267
268     flags_builder.set("enable_llvm_abi_extensions", "true").unwrap();
269
270     use rustc_session::config::OptLevel;
271     match sess.opts.optimize {
272         OptLevel::No => {
273             flags_builder.set("opt_level", "none").unwrap();
274         }
275         OptLevel::Less | OptLevel::Default => {}
276         OptLevel::Size | OptLevel::SizeMin | OptLevel::Aggressive => {
277             flags_builder.set("opt_level", "speed_and_size").unwrap();
278         }
279     }
280
281     if target_triple.architecture == target_lexicon::Architecture::X86_64 {
282         // Windows depends on stack probes to grow the committed part of the stack
283         flags_builder.enable("enable_probestack").unwrap();
284         flags_builder.set("probestack_strategy", "inline").unwrap();
285     } else {
286         // __cranelift_probestack is not provided and inline stack probes are only supported on x86_64
287         flags_builder.set("enable_probestack", "false").unwrap();
288     }
289
290     let flags = settings::Flags::new(flags_builder);
291
292     let isa_builder = match sess.opts.cg.target_cpu.as_deref() {
293         Some("native") => {
294             let builder = cranelift_native::builder_with_options(true).unwrap();
295             builder
296         }
297         Some(value) => {
298             let mut builder =
299                 cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
300                     sess.fatal(&format!("can't compile for {}: {}", target_triple, err));
301                 });
302             if let Err(_) = builder.enable(value) {
303                 sess.fatal("the specified target cpu isn't currently supported by Cranelift.");
304             }
305             builder
306         }
307         None => {
308             let mut builder =
309                 cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
310                     sess.fatal(&format!("can't compile for {}: {}", target_triple, err));
311                 });
312             if target_triple.architecture == target_lexicon::Architecture::X86_64 {
313                 // Don't use "haswell" as the default, as it implies `has_lzcnt`.
314                 // macOS CI is still at Ivy Bridge EP, so `lzcnt` is interpreted as `bsr`.
315                 builder.enable("nehalem").unwrap();
316             }
317             builder
318         }
319     };
320
321     match isa_builder.finish(flags) {
322         Ok(target_isa) => target_isa,
323         Err(err) => sess.fatal(&format!("failed to build TargetIsa: {}", err)),
324     }
325 }
326
327 /// This is the entrypoint for a hot plugged rustc_codegen_cranelift
328 #[no_mangle]
329 pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
330     Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
331 }