]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_llvm/src/llvm_util.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / compiler / rustc_codegen_llvm / src / llvm_util.rs
1 use crate::back::write::create_informational_target_machine;
2 use crate::llvm;
3 use libc::c_int;
4 use rustc_data_structures::fx::FxHashSet;
5 use rustc_feature::UnstableFeatures;
6 use rustc_middle::bug;
7 use rustc_session::config::PrintRequest;
8 use rustc_session::Session;
9 use rustc_span::symbol::sym;
10 use rustc_span::symbol::Symbol;
11 use rustc_target::spec::{MergeFunctions, PanicStrategy};
12 use std::ffi::CString;
13
14 use std::slice;
15 use std::str;
16 use std::sync::atomic::{AtomicBool, Ordering};
17 use std::sync::Once;
18
19 static POISONED: AtomicBool = AtomicBool::new(false);
20 static INIT: Once = Once::new();
21
22 pub(crate) fn init(sess: &Session) {
23 unsafe {
24 // Before we touch LLVM, make sure that multithreading is enabled.
25 INIT.call_once(|| {
26 if llvm::LLVMStartMultithreaded() != 1 {
27 // use an extra bool to make sure that all future usage of LLVM
28 // cannot proceed despite the Once not running more than once.
29 POISONED.store(true, Ordering::SeqCst);
30 }
31
32 configure_llvm(sess);
33 });
34
35 if POISONED.load(Ordering::SeqCst) {
36 bug!("couldn't enable multi-threaded LLVM");
37 }
38 }
39 }
40
41 fn require_inited() {
42 INIT.call_once(|| bug!("llvm is not initialized"));
43 if POISONED.load(Ordering::SeqCst) {
44 bug!("couldn't enable multi-threaded LLVM");
45 }
46 }
47
48 unsafe fn configure_llvm(sess: &Session) {
49 let n_args = sess.opts.cg.llvm_args.len() + sess.target.target.options.llvm_args.len();
50 let mut llvm_c_strs = Vec::with_capacity(n_args + 1);
51 let mut llvm_args = Vec::with_capacity(n_args + 1);
52
53 llvm::LLVMRustInstallFatalErrorHandler();
54
55 fn llvm_arg_to_arg_name(full_arg: &str) -> &str {
56 full_arg.trim().split(|c: char| c == '=' || c.is_whitespace()).next().unwrap_or("")
57 }
58
59 let cg_opts = sess.opts.cg.llvm_args.iter();
60 let tg_opts = sess.target.target.options.llvm_args.iter();
61 let sess_args = cg_opts.chain(tg_opts);
62
63 let user_specified_args: FxHashSet<_> =
64 sess_args.clone().map(|s| llvm_arg_to_arg_name(s)).filter(|s| !s.is_empty()).collect();
65
66 {
67 // This adds the given argument to LLVM. Unless `force` is true
68 // user specified arguments are *not* overridden.
69 let mut add = |arg: &str, force: bool| {
70 if force || !user_specified_args.contains(llvm_arg_to_arg_name(arg)) {
71 let s = CString::new(arg).unwrap();
72 llvm_args.push(s.as_ptr());
73 llvm_c_strs.push(s);
74 }
75 };
76 // Set the llvm "program name" to make usage and invalid argument messages more clear.
77 add("rustc -Cllvm-args=\"...\" with", true);
78 if sess.time_llvm_passes() {
79 add("-time-passes", false);
80 }
81 if sess.print_llvm_passes() {
82 add("-debug-pass=Structure", false);
83 }
84 if !sess.opts.debugging_opts.no_generate_arange_section {
85 add("-generate-arange-section", false);
86 }
87 match sess
88 .opts
89 .debugging_opts
90 .merge_functions
91 .unwrap_or(sess.target.target.options.merge_functions)
92 {
93 MergeFunctions::Disabled | MergeFunctions::Trampolines => {}
94 MergeFunctions::Aliases => {
95 add("-mergefunc-use-aliases", false);
96 }
97 }
98
99 if sess.target.target.target_os == "emscripten"
100 && sess.panic_strategy() == PanicStrategy::Unwind
101 {
102 add("-enable-emscripten-cxx-exceptions", false);
103 }
104
105 // HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
106 // during inlining. Unfortunately these may block other optimizations.
107 add("-preserve-alignment-assumptions-during-inlining=false", false);
108
109 for arg in sess_args {
110 add(&(*arg), true);
111 }
112 }
113
114 if sess.opts.debugging_opts.llvm_time_trace && get_major_version() >= 9 {
115 // time-trace is not thread safe and running it in parallel will cause seg faults.
116 if !sess.opts.debugging_opts.no_parallel_llvm {
117 bug!("`-Z llvm-time-trace` requires `-Z no-parallel-llvm")
118 }
119
120 llvm::LLVMTimeTraceProfilerInitialize();
121 }
122
123 llvm::LLVMInitializePasses();
124
125 ::rustc_llvm::initialize_available_targets();
126
127 llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, llvm_args.as_ptr());
128 }
129
130 pub fn time_trace_profiler_finish(file_name: &str) {
131 unsafe {
132 if get_major_version() >= 9 {
133 let file_name = CString::new(file_name).unwrap();
134 llvm::LLVMTimeTraceProfilerFinish(file_name.as_ptr());
135 }
136 }
137 }
138
139 // WARNING: the features after applying `to_llvm_feature` must be known
140 // to LLVM or the feature detection code will walk past the end of the feature
141 // array, leading to crashes.
142
143 const ARM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
144 ("aclass", Some(sym::arm_target_feature)),
145 ("mclass", Some(sym::arm_target_feature)),
146 ("rclass", Some(sym::arm_target_feature)),
147 ("dsp", Some(sym::arm_target_feature)),
148 ("neon", Some(sym::arm_target_feature)),
149 ("crc", Some(sym::arm_target_feature)),
150 ("crypto", Some(sym::arm_target_feature)),
151 ("v5te", Some(sym::arm_target_feature)),
152 ("v6", Some(sym::arm_target_feature)),
153 ("v6k", Some(sym::arm_target_feature)),
154 ("v6t2", Some(sym::arm_target_feature)),
155 ("v7", Some(sym::arm_target_feature)),
156 ("v8", Some(sym::arm_target_feature)),
157 ("vfp2", Some(sym::arm_target_feature)),
158 ("vfp3", Some(sym::arm_target_feature)),
159 ("vfp4", Some(sym::arm_target_feature)),
160 // This is needed for inline assembly, but shouldn't be stabilized as-is
161 // since it should be enabled per-function using #[instruction_set], not
162 // #[target_feature].
163 ("thumb-mode", Some(sym::arm_target_feature)),
164 ];
165
166 const AARCH64_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
167 ("fp", Some(sym::aarch64_target_feature)),
168 ("neon", Some(sym::aarch64_target_feature)),
169 ("sve", Some(sym::aarch64_target_feature)),
170 ("crc", Some(sym::aarch64_target_feature)),
171 ("crypto", Some(sym::aarch64_target_feature)),
172 ("ras", Some(sym::aarch64_target_feature)),
173 ("lse", Some(sym::aarch64_target_feature)),
174 ("rdm", Some(sym::aarch64_target_feature)),
175 ("fp16", Some(sym::aarch64_target_feature)),
176 ("rcpc", Some(sym::aarch64_target_feature)),
177 ("dotprod", Some(sym::aarch64_target_feature)),
178 ("tme", Some(sym::aarch64_target_feature)),
179 ("v8.1a", Some(sym::aarch64_target_feature)),
180 ("v8.2a", Some(sym::aarch64_target_feature)),
181 ("v8.3a", Some(sym::aarch64_target_feature)),
182 ];
183
184 const X86_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
185 ("adx", Some(sym::adx_target_feature)),
186 ("aes", None),
187 ("avx", None),
188 ("avx2", None),
189 ("avx512bw", Some(sym::avx512_target_feature)),
190 ("avx512cd", Some(sym::avx512_target_feature)),
191 ("avx512dq", Some(sym::avx512_target_feature)),
192 ("avx512er", Some(sym::avx512_target_feature)),
193 ("avx512f", Some(sym::avx512_target_feature)),
194 ("avx512ifma", Some(sym::avx512_target_feature)),
195 ("avx512pf", Some(sym::avx512_target_feature)),
196 ("avx512vbmi", Some(sym::avx512_target_feature)),
197 ("avx512vl", Some(sym::avx512_target_feature)),
198 ("avx512vpopcntdq", Some(sym::avx512_target_feature)),
199 ("bmi1", None),
200 ("bmi2", None),
201 ("cmpxchg16b", Some(sym::cmpxchg16b_target_feature)),
202 ("f16c", Some(sym::f16c_target_feature)),
203 ("fma", None),
204 ("fxsr", None),
205 ("lzcnt", None),
206 ("movbe", Some(sym::movbe_target_feature)),
207 ("pclmulqdq", None),
208 ("popcnt", None),
209 ("rdrand", None),
210 ("rdseed", None),
211 ("rtm", Some(sym::rtm_target_feature)),
212 ("sha", None),
213 ("sse", None),
214 ("sse2", None),
215 ("sse3", None),
216 ("sse4.1", None),
217 ("sse4.2", None),
218 ("sse4a", Some(sym::sse4a_target_feature)),
219 ("ssse3", None),
220 ("tbm", Some(sym::tbm_target_feature)),
221 ("xsave", None),
222 ("xsavec", None),
223 ("xsaveopt", None),
224 ("xsaves", None),
225 ];
226
227 const HEXAGON_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
228 ("hvx", Some(sym::hexagon_target_feature)),
229 ("hvx-length128b", Some(sym::hexagon_target_feature)),
230 ];
231
232 const POWERPC_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
233 ("altivec", Some(sym::powerpc_target_feature)),
234 ("power8-altivec", Some(sym::powerpc_target_feature)),
235 ("power9-altivec", Some(sym::powerpc_target_feature)),
236 ("power8-vector", Some(sym::powerpc_target_feature)),
237 ("power9-vector", Some(sym::powerpc_target_feature)),
238 ("vsx", Some(sym::powerpc_target_feature)),
239 ];
240
241 const MIPS_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] =
242 &[("fp64", Some(sym::mips_target_feature)), ("msa", Some(sym::mips_target_feature))];
243
244 const RISCV_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
245 ("m", Some(sym::riscv_target_feature)),
246 ("a", Some(sym::riscv_target_feature)),
247 ("c", Some(sym::riscv_target_feature)),
248 ("f", Some(sym::riscv_target_feature)),
249 ("d", Some(sym::riscv_target_feature)),
250 ("e", Some(sym::riscv_target_feature)),
251 ];
252
253 const WASM_ALLOWED_FEATURES: &[(&str, Option<Symbol>)] = &[
254 ("simd128", Some(sym::wasm_target_feature)),
255 ("atomics", Some(sym::wasm_target_feature)),
256 ("nontrapping-fptoint", Some(sym::wasm_target_feature)),
257 ];
258
259 /// When rustdoc is running, provide a list of all known features so that all their respective
260 /// primitives may be documented.
261 ///
262 /// IMPORTANT: If you're adding another feature list above, make sure to add it to this iterator!
263 pub fn all_known_features() -> impl Iterator<Item = (&'static str, Option<Symbol>)> {
264 std::iter::empty()
265 .chain(ARM_ALLOWED_FEATURES.iter())
266 .chain(AARCH64_ALLOWED_FEATURES.iter())
267 .chain(X86_ALLOWED_FEATURES.iter())
268 .chain(HEXAGON_ALLOWED_FEATURES.iter())
269 .chain(POWERPC_ALLOWED_FEATURES.iter())
270 .chain(MIPS_ALLOWED_FEATURES.iter())
271 .chain(RISCV_ALLOWED_FEATURES.iter())
272 .chain(WASM_ALLOWED_FEATURES.iter())
273 .cloned()
274 }
275
276 pub fn to_llvm_feature<'a>(sess: &Session, s: &'a str) -> &'a str {
277 let arch = if sess.target.target.arch == "x86_64" { "x86" } else { &*sess.target.target.arch };
278 match (arch, s) {
279 ("x86", "pclmulqdq") => "pclmul",
280 ("x86", "rdrand") => "rdrnd",
281 ("x86", "bmi1") => "bmi",
282 ("x86", "cmpxchg16b") => "cx16",
283 ("aarch64", "fp") => "fp-armv8",
284 ("aarch64", "fp16") => "fullfp16",
285 (_, s) => s,
286 }
287 }
288
289 pub fn target_features(sess: &Session) -> Vec<Symbol> {
290 let target_machine = create_informational_target_machine(sess);
291 supported_target_features(sess)
292 .iter()
293 .filter_map(|&(feature, gate)| {
294 if UnstableFeatures::from_environment().is_nightly_build() || gate.is_none() {
295 Some(feature)
296 } else {
297 None
298 }
299 })
300 .filter(|feature| {
301 let llvm_feature = to_llvm_feature(sess, feature);
302 let cstr = CString::new(llvm_feature).unwrap();
303 unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) }
304 })
305 .map(|feature| Symbol::intern(feature))
306 .collect()
307 }
308
309 pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Option<Symbol>)] {
310 match &*sess.target.target.arch {
311 "arm" => ARM_ALLOWED_FEATURES,
312 "aarch64" => AARCH64_ALLOWED_FEATURES,
313 "x86" | "x86_64" => X86_ALLOWED_FEATURES,
314 "hexagon" => HEXAGON_ALLOWED_FEATURES,
315 "mips" | "mips64" => MIPS_ALLOWED_FEATURES,
316 "powerpc" | "powerpc64" => POWERPC_ALLOWED_FEATURES,
317 "riscv32" | "riscv64" => RISCV_ALLOWED_FEATURES,
318 "wasm32" => WASM_ALLOWED_FEATURES,
319 _ => &[],
320 }
321 }
322
323 pub fn print_version() {
324 // Can be called without initializing LLVM
325 unsafe {
326 println!("LLVM version: {}.{}", llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor());
327 }
328 }
329
330 pub fn get_major_version() -> u32 {
331 unsafe { llvm::LLVMRustVersionMajor() }
332 }
333
334 pub fn print_passes() {
335 // Can be called without initializing LLVM
336 unsafe {
337 llvm::LLVMRustPrintPasses();
338 }
339 }
340
341 pub(crate) fn print(req: PrintRequest, sess: &Session) {
342 require_inited();
343 let tm = create_informational_target_machine(sess);
344 unsafe {
345 match req {
346 PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm),
347 PrintRequest::TargetFeatures => llvm::LLVMRustPrintTargetFeatures(tm),
348 _ => bug!("rustc_codegen_llvm can't handle print request: {:?}", req),
349 }
350 }
351 }
352
353 pub fn target_cpu(sess: &Session) -> &str {
354 let name = match sess.opts.cg.target_cpu {
355 Some(ref s) => &**s,
356 None => &*sess.target.target.options.cpu,
357 };
358 if name != "native" {
359 return name;
360 }
361
362 unsafe {
363 let mut len = 0;
364 let ptr = llvm::LLVMRustGetHostCPUName(&mut len);
365 str::from_utf8(slice::from_raw_parts(ptr as *const u8, len)).unwrap()
366 }
367 }