|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use syntax_pos::symbol::Symbol; |
| 12 | +use back::write::create_target_machine; |
| 13 | +use llvm; |
| 14 | +use rustc::session::Session; |
| 15 | +use rustc::session::config::PrintRequest; |
| 16 | +use libc::{c_int, c_char}; |
| 17 | +use std::ffi::CString; |
| 18 | + |
| 19 | +pub fn init(sess: &Session) { |
| 20 | + unsafe { |
| 21 | + // Before we touch LLVM, make sure that multithreading is enabled. |
| 22 | + use std::sync::Once; |
| 23 | + static INIT: Once = Once::new(); |
| 24 | + static mut POISONED: bool = false; |
| 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 = true; |
| 30 | + } |
| 31 | + |
| 32 | + configure_llvm(sess); |
| 33 | + }); |
| 34 | + |
| 35 | + if POISONED { |
| 36 | + bug!("couldn't enable multi-threaded LLVM"); |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +unsafe fn configure_llvm(sess: &Session) { |
| 42 | + let mut llvm_c_strs = Vec::new(); |
| 43 | + let mut llvm_args = Vec::new(); |
| 44 | + |
| 45 | + { |
| 46 | + let mut add = |arg: &str| { |
| 47 | + let s = CString::new(arg).unwrap(); |
| 48 | + llvm_args.push(s.as_ptr()); |
| 49 | + llvm_c_strs.push(s); |
| 50 | + }; |
| 51 | + add("rustc"); // fake program name |
| 52 | + if sess.time_llvm_passes() { add("-time-passes"); } |
| 53 | + if sess.print_llvm_passes() { add("-debug-pass=Structure"); } |
| 54 | + |
| 55 | + for arg in &sess.opts.cg.llvm_args { |
| 56 | + add(&(*arg)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + llvm::LLVMInitializePasses(); |
| 61 | + |
| 62 | + llvm::initialize_available_targets(); |
| 63 | + |
| 64 | + llvm::LLVMRustSetLLVMOptions(llvm_args.len() as c_int, |
| 65 | + llvm_args.as_ptr()); |
| 66 | +} |
| 67 | + |
| 68 | +// WARNING: the features must be known to LLVM or the feature |
| 69 | +// detection code will walk past the end of the feature array, |
| 70 | +// leading to crashes. |
| 71 | + |
| 72 | +const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"]; |
| 73 | + |
| 74 | +const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0", |
| 75 | + "sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0", |
| 76 | + "ssse3\0", "tbm\0", "lzcnt\0", "popcnt\0", |
| 77 | + "sse4a\0", "rdrnd\0", "rdseed\0", "fma\0"]; |
| 78 | + |
| 79 | +const HEXAGON_WHITELIST: &'static [&'static str] = &["hvx\0", "hvx-double\0"]; |
| 80 | + |
| 81 | +pub fn target_features(sess: &Session) -> Vec<Symbol> { |
| 82 | + let target_machine = create_target_machine(sess); |
| 83 | + |
| 84 | + let whitelist = match &*sess.target.target.arch { |
| 85 | + "arm" => ARM_WHITELIST, |
| 86 | + "x86" | "x86_64" => X86_WHITELIST, |
| 87 | + "hexagon" => HEXAGON_WHITELIST, |
| 88 | + _ => &[], |
| 89 | + }; |
| 90 | + |
| 91 | + let mut features = Vec::new(); |
| 92 | + for feat in whitelist { |
| 93 | + assert_eq!(feat.chars().last(), Some('\0')); |
| 94 | + if unsafe { llvm::LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } { |
| 95 | + features.push(Symbol::intern(&feat[..feat.len() - 1])); |
| 96 | + } |
| 97 | + } |
| 98 | + features |
| 99 | +} |
| 100 | + |
| 101 | +pub fn print_version() { |
| 102 | + unsafe { |
| 103 | + println!("LLVM version: {}.{}", |
| 104 | + llvm::LLVMRustVersionMajor(), llvm::LLVMRustVersionMinor()); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +pub fn print_passes() { |
| 109 | + unsafe { llvm::LLVMRustPrintPasses(); } |
| 110 | +} |
| 111 | + |
| 112 | +pub fn print(req: PrintRequest, sess: &Session) { |
| 113 | + let tm = create_target_machine(sess); |
| 114 | + unsafe { |
| 115 | + match req { |
| 116 | + PrintRequest::TargetCPUs => llvm::LLVMRustPrintTargetCPUs(tm), |
| 117 | + PrintRequest::TargetFeatures => llvm::LLVMRustPrintTargetFeatures(tm), |
| 118 | + _ => bug!("rustc_trans can't handle print request: {:?}", req), |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +pub fn enable_llvm_debug() { |
| 124 | + unsafe { llvm::LLVMRustSetDebug(1); } |
| 125 | +} |
0 commit comments