• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, TypeVisitableExt, UintTy,
88     };
89     pub(crate) use rustc_target::abi::{Abi, FieldIdx, Scalar, Size, VariantIdx, FIRST_VARIANT};
90 
91     pub(crate) use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
92 
93     pub(crate) use rustc_index::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, DataDescription, 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, CValue};
114 }
115 
116 struct PrintOnPanic<F: Fn() -> String>(F);
117 impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
drop(&mut self)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 {
new( tcx: TyCtxt<'_>, backend_config: BackendConfig, isa: &dyn TargetIsa, debug_info: bool, cgu_name: Symbol, ) -> Self139     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 {
locale_resource(&self) -> &'static str173     fn locale_resource(&self) -> &'static str {
174         // FIXME(rust-lang/rust#100717) - cranelift codegen backend is not yet translated
175         ""
176     }
177 
init(&self, sess: &Session)178     fn init(&self, sess: &Session) {
179         use rustc_session::config::Lto;
180         match sess.lto() {
181             Lto::No | Lto::ThinLocal => {}
182             Lto::Thin | Lto::Fat => sess.warn("LTO is not supported. You may get a linker error."),
183         }
184 
185         let mut config = self.config.borrow_mut();
186         if config.is_none() {
187             let new_config = BackendConfig::from_opts(&sess.opts.cg.llvm_args)
188                 .unwrap_or_else(|err| sess.fatal(err));
189             *config = Some(new_config);
190         }
191     }
192 
target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol>193     fn target_features(&self, _sess: &Session, _allow_unstable: bool) -> Vec<rustc_span::Symbol> {
194         vec![]
195     }
196 
print_version(&self)197     fn print_version(&self) {
198         println!("Cranelift version: {}", cranelift_codegen::VERSION);
199     }
200 
codegen_crate( &self, tcx: TyCtxt<'_>, metadata: EncodedMetadata, need_metadata_module: bool, ) -> Box<dyn Any>201     fn codegen_crate(
202         &self,
203         tcx: TyCtxt<'_>,
204         metadata: EncodedMetadata,
205         need_metadata_module: bool,
206     ) -> Box<dyn Any> {
207         tcx.sess.abort_if_errors();
208         let config = self.config.borrow().clone().unwrap();
209         match config.codegen_mode {
210             CodegenMode::Aot => driver::aot::run_aot(tcx, config, metadata, need_metadata_module),
211             CodegenMode::Jit | CodegenMode::JitLazy => {
212                 #[cfg(feature = "jit")]
213                 driver::jit::run_jit(tcx, config);
214 
215                 #[cfg(not(feature = "jit"))]
216                 tcx.sess.fatal("jit support was disabled when compiling rustc_codegen_cranelift");
217             }
218         }
219     }
220 
join_codegen( &self, ongoing_codegen: Box<dyn Any>, sess: &Session, _outputs: &OutputFilenames, ) -> Result<(CodegenResults, FxIndexMap<WorkProductId, WorkProduct>), ErrorGuaranteed>221     fn join_codegen(
222         &self,
223         ongoing_codegen: Box<dyn Any>,
224         sess: &Session,
225         _outputs: &OutputFilenames,
226     ) -> Result<(CodegenResults, FxIndexMap<WorkProductId, WorkProduct>), ErrorGuaranteed> {
227         Ok(ongoing_codegen
228             .downcast::<driver::aot::OngoingCodegen>()
229             .unwrap()
230             .join(sess, self.config.borrow().as_ref().unwrap()))
231     }
232 
link( &self, sess: &Session, codegen_results: CodegenResults, outputs: &OutputFilenames, ) -> Result<(), ErrorGuaranteed>233     fn link(
234         &self,
235         sess: &Session,
236         codegen_results: CodegenResults,
237         outputs: &OutputFilenames,
238     ) -> Result<(), ErrorGuaranteed> {
239         use rustc_codegen_ssa::back::link::link_binary;
240 
241         link_binary(sess, &crate::archive::ArArchiveBuilderBuilder, &codegen_results, outputs)
242     }
243 }
244 
target_triple(sess: &Session) -> target_lexicon::Triple245 fn target_triple(sess: &Session) -> target_lexicon::Triple {
246     match sess.target.llvm_target.parse() {
247         Ok(triple) => triple,
248         Err(err) => sess.fatal(format!("target not recognized: {}", err)),
249     }
250 }
251 
build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::TargetIsa + 'static>252 fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Arc<dyn isa::TargetIsa + 'static> {
253     use target_lexicon::BinaryFormat;
254 
255     let target_triple = crate::target_triple(sess);
256 
257     let mut flags_builder = settings::builder();
258     flags_builder.enable("is_pic").unwrap();
259     let enable_verifier = if backend_config.enable_verifier { "true" } else { "false" };
260     flags_builder.set("enable_verifier", enable_verifier).unwrap();
261     flags_builder.set("regalloc_checker", enable_verifier).unwrap();
262 
263     let tls_model = match target_triple.binary_format {
264         BinaryFormat::Elf => "elf_gd",
265         BinaryFormat::Macho => "macho",
266         BinaryFormat::Coff => "coff",
267         _ => "none",
268     };
269     flags_builder.set("tls_model", tls_model).unwrap();
270 
271     flags_builder.set("enable_simd", "true").unwrap();
272 
273     flags_builder.set("enable_llvm_abi_extensions", "true").unwrap();
274 
275     use rustc_session::config::OptLevel;
276     match sess.opts.optimize {
277         OptLevel::No => {
278             flags_builder.set("opt_level", "none").unwrap();
279         }
280         OptLevel::Less | OptLevel::Default => {}
281         OptLevel::Size | OptLevel::SizeMin | OptLevel::Aggressive => {
282             flags_builder.set("opt_level", "speed_and_size").unwrap();
283         }
284     }
285 
286     if let target_lexicon::Architecture::Aarch64(_)
287     | target_lexicon::Architecture::Riscv64(_)
288     | target_lexicon::Architecture::X86_64 = target_triple.architecture
289     {
290         // Windows depends on stack probes to grow the committed part of the stack.
291         // On other platforms it helps prevents stack smashing.
292         flags_builder.enable("enable_probestack").unwrap();
293         flags_builder.set("probestack_strategy", "inline").unwrap();
294     } else {
295         // __cranelift_probestack is not provided and inline stack probes are only supported on
296         // AArch64, Riscv64 and x86_64.
297         flags_builder.set("enable_probestack", "false").unwrap();
298     }
299 
300     let flags = settings::Flags::new(flags_builder);
301 
302     let isa_builder = match sess.opts.cg.target_cpu.as_deref() {
303         Some("native") => {
304             let builder = cranelift_native::builder_with_options(true).unwrap();
305             builder
306         }
307         Some(value) => {
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 let Err(_) = builder.enable(value) {
313                 sess.fatal("the specified target cpu isn't currently supported by Cranelift.");
314             }
315             builder
316         }
317         None => {
318             let mut builder =
319                 cranelift_codegen::isa::lookup(target_triple.clone()).unwrap_or_else(|err| {
320                     sess.fatal(format!("can't compile for {}: {}", target_triple, err));
321                 });
322             if target_triple.architecture == target_lexicon::Architecture::X86_64 {
323                 // Don't use "haswell" as the default, as it implies `has_lzcnt`.
324                 // macOS CI is still at Ivy Bridge EP, so `lzcnt` is interpreted as `bsr`.
325                 builder.enable("nehalem").unwrap();
326             }
327             builder
328         }
329     };
330 
331     match isa_builder.finish(flags) {
332         Ok(target_isa) => target_isa,
333         Err(err) => sess.fatal(format!("failed to build TargetIsa: {}", err)),
334     }
335 }
336 
337 /// This is the entrypoint for a hot plugged rustc_codegen_cranelift
338 #[no_mangle]
__rustc_codegen_backend() -> Box<dyn CodegenBackend>339 pub fn __rustc_codegen_backend() -> Box<dyn CodegenBackend> {
340     Box::new(CraneliftCodegenBackend { config: RefCell::new(None) })
341 }
342