1 use crate::back
::write
::create_informational_target_machine
;
4 use rustc_codegen_ssa
::target_features
::supported_target_features
;
5 use rustc_data_structures
::fx
::FxHashSet
;
6 use rustc_feature
::UnstableFeatures
;
8 use rustc_session
::config
::PrintRequest
;
9 use rustc_session
::Session
;
10 use rustc_span
::symbol
::Symbol
;
11 use rustc_target
::spec
::{MergeFunctions, PanicStrategy}
;
12 use std
::ffi
::CString
;
16 use std
::sync
::atomic
::{AtomicBool, Ordering}
;
19 static POISONED
: AtomicBool
= AtomicBool
::new(false);
20 static INIT
: Once
= Once
::new();
22 pub(crate) fn init(sess
: &Session
) {
24 // Before we touch LLVM, make sure that multithreading is enabled.
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
);
35 if POISONED
.load(Ordering
::SeqCst
) {
36 bug
!("couldn't enable multi-threaded LLVM");
42 INIT
.call_once(|| bug
!("llvm is not initialized"));
43 if POISONED
.load(Ordering
::SeqCst
) {
44 bug
!("couldn't enable multi-threaded LLVM");
48 unsafe fn configure_llvm(sess
: &Session
) {
49 let n_args
= sess
.opts
.cg
.llvm_args
.len() + sess
.target
.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);
53 llvm
::LLVMRustInstallFatalErrorHandler();
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("")
59 let cg_opts
= sess
.opts
.cg
.llvm_args
.iter();
60 let tg_opts
= sess
.target
.llvm_args
.iter();
61 let sess_args
= cg_opts
.chain(tg_opts
);
63 let user_specified_args
: FxHashSet
<_
> =
64 sess_args
.clone().map(|s
| llvm_arg_to_arg_name(s
)).filter(|s
| !s
.is_empty()).collect();
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());
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);
81 if sess
.print_llvm_passes() {
82 add("-debug-pass=Structure", false);
84 if !sess
.opts
.debugging_opts
.no_generate_arange_section
{
85 add("-generate-arange-section", false);
87 match sess
.opts
.debugging_opts
.merge_functions
.unwrap_or(sess
.target
.merge_functions
) {
88 MergeFunctions
::Disabled
| MergeFunctions
::Trampolines
=> {}
89 MergeFunctions
::Aliases
=> {
90 add("-mergefunc-use-aliases", false);
94 if sess
.target
.os
== "emscripten" && sess
.panic_strategy() == PanicStrategy
::Unwind
{
95 add("-enable-emscripten-cxx-exceptions", false);
98 // HACK(eddyb) LLVM inserts `llvm.assume` calls to preserve align attributes
99 // during inlining. Unfortunately these may block other optimizations.
100 add("-preserve-alignment-assumptions-during-inlining=false", false);
102 for arg
in sess_args
{
107 if sess
.opts
.debugging_opts
.llvm_time_trace
&& get_major_version() >= 9 {
108 // time-trace is not thread safe and running it in parallel will cause seg faults.
109 if !sess
.opts
.debugging_opts
.no_parallel_llvm
{
110 bug
!("`-Z llvm-time-trace` requires `-Z no-parallel-llvm")
113 llvm
::LLVMTimeTraceProfilerInitialize();
116 llvm
::LLVMInitializePasses();
118 rustc_llvm
::initialize_available_targets();
120 llvm
::LLVMRustSetLLVMOptions(llvm_args
.len() as c_int
, llvm_args
.as_ptr());
123 pub fn time_trace_profiler_finish(file_name
: &str) {
125 if get_major_version() >= 9 {
126 let file_name
= CString
::new(file_name
).unwrap();
127 llvm
::LLVMTimeTraceProfilerFinish(file_name
.as_ptr());
132 // WARNING: the features after applying `to_llvm_feature` must be known
133 // to LLVM or the feature detection code will walk past the end of the feature
134 // array, leading to crashes.
135 pub fn to_llvm_feature
<'a
>(sess
: &Session
, s
: &'a
str) -> &'a
str {
136 let arch
= if sess
.target
.arch
== "x86_64" { "x86" }
else { &*sess.target.arch }
;
138 ("x86", "pclmulqdq") => "pclmul",
139 ("x86", "rdrand") => "rdrnd",
140 ("x86", "bmi1") => "bmi",
141 ("x86", "cmpxchg16b") => "cx16",
142 ("aarch64", "fp") => "fp-armv8",
143 ("aarch64", "fp16") => "fullfp16",
148 pub fn target_features(sess
: &Session
) -> Vec
<Symbol
> {
149 let target_machine
= create_informational_target_machine(sess
);
150 supported_target_features(sess
)
152 .filter_map(|&(feature
, gate
)| {
153 if UnstableFeatures
::from_environment().is_nightly_build() || gate
.is_none() {
160 let llvm_feature
= to_llvm_feature(sess
, feature
);
161 let cstr
= CString
::new(llvm_feature
).unwrap();
162 unsafe { llvm::LLVMRustHasFeature(target_machine, cstr.as_ptr()) }
164 .map(|feature
| Symbol
::intern(feature
))
168 pub fn print_version() {
169 // Can be called without initializing LLVM
171 println
!("LLVM version: {}.{}", llvm
::LLVMRustVersionMajor(), llvm
::LLVMRustVersionMinor());
175 pub fn get_major_version() -> u32 {
176 unsafe { llvm::LLVMRustVersionMajor() }
179 pub fn print_passes() {
180 // Can be called without initializing LLVM
182 llvm
::LLVMRustPrintPasses();
186 pub(crate) fn print(req
: PrintRequest
, sess
: &Session
) {
188 let tm
= create_informational_target_machine(sess
);
191 PrintRequest
::TargetCPUs
=> llvm
::LLVMRustPrintTargetCPUs(tm
),
192 PrintRequest
::TargetFeatures
=> llvm
::LLVMRustPrintTargetFeatures(tm
),
193 _
=> bug
!("rustc_codegen_llvm can't handle print request: {:?}", req
),
198 fn handle_native(name
: &str) -> &str {
199 if name
!= "native" {
205 let ptr
= llvm
::LLVMRustGetHostCPUName(&mut len
);
206 str::from_utf8(slice
::from_raw_parts(ptr
as *const u8, len
)).unwrap()
210 pub fn target_cpu(sess
: &Session
) -> &str {
211 let name
= match sess
.opts
.cg
.target_cpu
{
213 None
=> &*sess
.target
.cpu
,
219 pub fn tune_cpu(sess
: &Session
) -> Option
<&str> {
220 match sess
.opts
.debugging_opts
.tune_cpu
{
221 Some(ref s
) => Some(handle_native(&**s
)),