]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_llvm/attributes.rs
Rollup merge of #62760 - chansuke:dedupe-error-messages, r=zackmdavis
[rust.git] / src / librustc_codegen_llvm / attributes.rs
1 //! Set and unset common attributes on LLVM values.
2
3 use std::ffi::CString;
4
5 use rustc::hir::{CodegenFnAttrFlags, CodegenFnAttrs};
6 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
7 use rustc::session::Session;
8 use rustc::session::config::{Sanitizer, OptLevel};
9 use rustc::ty::{self, TyCtxt, PolyFnSig};
10 use rustc::ty::layout::HasTyCtxt;
11 use rustc::ty::query::Providers;
12 use rustc_data_structures::small_c_str::SmallCStr;
13 use rustc_data_structures::fx::FxHashMap;
14 use rustc_target::spec::PanicStrategy;
15 use rustc_codegen_ssa::traits::*;
16
17 use crate::abi::Abi;
18 use crate::attributes;
19 use crate::llvm::{self, Attribute};
20 use crate::llvm::AttributePlace::Function;
21 use crate::llvm_util;
22 pub use syntax::attr::{self, InlineAttr, OptimizeAttr};
23
24 use crate::context::CodegenCx;
25 use crate::value::Value;
26
27 /// Mark LLVM function to use provided inline heuristic.
28 #[inline]
29 pub fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
30     use self::InlineAttr::*;
31     match inline {
32         Hint   => Attribute::InlineHint.apply_llfn(Function, val),
33         Always => Attribute::AlwaysInline.apply_llfn(Function, val),
34         Never  => {
35             if cx.tcx().sess.target.target.arch != "amdgpu" {
36                 Attribute::NoInline.apply_llfn(Function, val);
37             }
38         },
39         None   => {
40             Attribute::InlineHint.unapply_llfn(Function, val);
41             Attribute::AlwaysInline.unapply_llfn(Function, val);
42             Attribute::NoInline.unapply_llfn(Function, val);
43         },
44     };
45 }
46
47 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
48 #[inline]
49 pub fn emit_uwtable(val: &'ll Value, emit: bool) {
50     Attribute::UWTable.toggle_llfn(Function, val, emit);
51 }
52
53 /// Tell LLVM whether the function can or cannot unwind.
54 #[inline]
55 fn unwind(val: &'ll Value, can_unwind: bool) {
56     Attribute::NoUnwind.toggle_llfn(Function, val, !can_unwind);
57 }
58
59 /// Tell LLVM if this function should be 'naked', i.e., skip the epilogue and prologue.
60 #[inline]
61 pub fn naked(val: &'ll Value, is_naked: bool) {
62     Attribute::Naked.toggle_llfn(Function, val, is_naked);
63 }
64
65 pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
66     if cx.sess().must_not_eliminate_frame_pointers() {
67         llvm::AddFunctionAttrStringValue(
68             llfn, llvm::AttributePlace::Function,
69             const_cstr!("no-frame-pointer-elim"), const_cstr!("true"));
70     }
71 }
72
73 /// Tell LLVM what instrument function to insert.
74 #[inline]
75 pub fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
76     if cx.sess().instrument_mcount() {
77         // Similar to `clang -pg` behavior. Handled by the
78         // `post-inline-ee-instrument` LLVM pass.
79
80         // The function name varies on platforms.
81         // See test/CodeGen/mcount.c in clang.
82         let mcount_name = CString::new(
83             cx.sess().target.target.options.target_mcount.as_str().as_bytes()).unwrap();
84
85         llvm::AddFunctionAttrStringValue(
86             llfn, llvm::AttributePlace::Function,
87             const_cstr!("instrument-function-entry-inlined"), &mcount_name);
88     }
89 }
90
91 pub fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
92     // Only use stack probes if the target specification indicates that we
93     // should be using stack probes
94     if !cx.sess().target.target.options.stack_probes {
95         return
96     }
97
98     // Currently stack probes seem somewhat incompatible with the address
99     // sanitizer. With asan we're already protected from stack overflow anyway
100     // so we don't really need stack probes regardless.
101     if let Some(Sanitizer::Address) = cx.sess().opts.debugging_opts.sanitizer {
102         return
103     }
104
105     // probestack doesn't play nice either with `-C profile-generate`.
106     if cx.sess().opts.cg.profile_generate.enabled() {
107         return;
108     }
109
110     // probestack doesn't play nice either with gcov profiling.
111     if cx.sess().opts.debugging_opts.profile {
112         return;
113     }
114
115     // Flag our internal `__rust_probestack` function as the stack probe symbol.
116     // This is defined in the `compiler-builtins` crate for each architecture.
117     llvm::AddFunctionAttrStringValue(
118         llfn, llvm::AttributePlace::Function,
119         const_cstr!("probe-stack"), const_cstr!("__rust_probestack"));
120 }
121
122 fn translate_obsolete_target_features(feature: &str) -> &str {
123     const LLVM9_FEATURE_CHANGES: &[(&str, &str)] = &[
124         ("+fp-only-sp", "-fp64"),
125         ("-fp-only-sp", "+fp64"),
126         ("+d16", "-d32"),
127         ("-d16", "+d32"),
128     ];
129     if llvm_util::get_major_version() >= 9 {
130         for &(old, new) in LLVM9_FEATURE_CHANGES {
131             if feature == old {
132                 return new;
133             }
134         }
135     } else {
136         for &(old, new) in LLVM9_FEATURE_CHANGES {
137             if feature == new {
138                 return old;
139             }
140         }
141     }
142     feature
143 }
144
145 pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
146     const RUSTC_SPECIFIC_FEATURES: &[&str] = &[
147         "crt-static",
148     ];
149
150     let cmdline = sess.opts.cg.target_feature.split(',')
151         .filter(|f| !RUSTC_SPECIFIC_FEATURES.iter().any(|s| f.contains(s)));
152     sess.target.target.options.features.split(',')
153         .chain(cmdline)
154         .filter(|l| !l.is_empty())
155         .map(translate_obsolete_target_features)
156 }
157
158 pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
159     let target_cpu = SmallCStr::new(llvm_util::target_cpu(cx.tcx.sess));
160     llvm::AddFunctionAttrStringValue(
161             llfn,
162             llvm::AttributePlace::Function,
163             const_cstr!("target-cpu"),
164             target_cpu.as_c_str());
165 }
166
167 /// Sets the `NonLazyBind` LLVM attribute on a given function,
168 /// assuming the codegen options allow skipping the PLT.
169 pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
170     // Don't generate calls through PLT if it's not necessary
171     if !sess.needs_plt() {
172         Attribute::NonLazyBind.apply_llfn(Function, llfn);
173     }
174 }
175
176 pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
177     match sess.opts.optimize {
178         OptLevel::Size => {
179             llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
180             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
181             llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
182         },
183         OptLevel::SizeMin => {
184             llvm::Attribute::MinSize.apply_llfn(Function, llfn);
185             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
186             llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
187         }
188         OptLevel::No => {
189             llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
190             llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
191             llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
192         }
193         _ => {}
194     }
195 }
196
197
198 /// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
199 /// attributes.
200 pub fn from_fn_attrs(
201     cx: &CodegenCx<'ll, 'tcx>,
202     llfn: &'ll Value,
203     id: Option<DefId>,
204     sig: PolyFnSig<'tcx>,
205 ) {
206     let codegen_fn_attrs = id.map(|id| cx.tcx.codegen_fn_attrs(id))
207         .unwrap_or_else(|| CodegenFnAttrs::new());
208
209     match codegen_fn_attrs.optimize {
210         OptimizeAttr::None => {
211             default_optimisation_attrs(cx.tcx.sess, llfn);
212         }
213         OptimizeAttr::Speed => {
214             llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
215             llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
216             llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
217         }
218         OptimizeAttr::Size => {
219             llvm::Attribute::MinSize.apply_llfn(Function, llfn);
220             llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
221             llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
222         }
223     }
224
225     inline(cx, llfn, codegen_fn_attrs.inline);
226
227     // The `uwtable` attribute according to LLVM is:
228     //
229     //     This attribute indicates that the ABI being targeted requires that an
230     //     unwind table entry be produced for this function even if we can show
231     //     that no exceptions passes by it. This is normally the case for the
232     //     ELF x86-64 abi, but it can be disabled for some compilation units.
233     //
234     // Typically when we're compiling with `-C panic=abort` (which implies this
235     // `no_landing_pads` check) we don't need `uwtable` because we can't
236     // generate any exceptions! On Windows, however, exceptions include other
237     // events such as illegal instructions, segfaults, etc. This means that on
238     // Windows we end up still needing the `uwtable` attribute even if the `-C
239     // panic=abort` flag is passed.
240     //
241     // You can also find more info on why Windows is whitelisted here in:
242     //      https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
243     if !cx.sess().no_landing_pads() ||
244        cx.sess().target.target.options.requires_uwtable {
245         attributes::emit_uwtable(llfn, true);
246     }
247
248     set_frame_pointer_elimination(cx, llfn);
249     set_instrument_function(cx, llfn);
250     set_probestack(cx, llfn);
251
252     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
253         Attribute::Cold.apply_llfn(Function, llfn);
254     }
255     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_RETURNS_TWICE) {
256         Attribute::ReturnsTwice.apply_llfn(Function, llfn);
257     }
258     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
259         naked(llfn, true);
260     }
261     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
262         Attribute::NoAlias.apply_llfn(
263             llvm::AttributePlace::ReturnValue, llfn);
264     }
265
266     unwind(llfn, if cx.tcx.sess.panic_strategy() != PanicStrategy::Unwind {
267         // In panic=abort mode we assume nothing can unwind anywhere, so
268         // optimize based on this!
269         false
270     } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::UNWIND) {
271         // If a specific #[unwind] attribute is present, use that
272         true
273     } else if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_ALLOCATOR_NOUNWIND) {
274         // Special attribute for allocator functions, which can't unwind
275         false
276     } else if let Some(id) = id {
277         let sig = cx.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
278         if cx.tcx.is_foreign_item(id) {
279             // Foreign items like `extern "C" { fn foo(); }` are assumed not to
280             // unwind
281             false
282         } else if sig.abi != Abi::Rust && sig.abi != Abi::RustCall {
283             // Any items defined in Rust that *don't* have the `extern` ABI are
284             // defined to not unwind. We insert shims to abort if an unwind
285             // happens to enforce this.
286             false
287         } else {
288             // Anything else defined in Rust is assumed that it can possibly
289             // unwind
290             true
291         }
292     } else {
293         // assume this can possibly unwind, avoiding the application of a
294         // `nounwind` attribute below.
295         true
296     });
297
298     // Always annotate functions with the target-cpu they are compiled for.
299     // Without this, ThinLTO won't inline Rust functions into Clang generated
300     // functions (because Clang annotates functions this way too).
301     apply_target_cpu_attr(cx, llfn);
302
303     let features = llvm_target_features(cx.tcx.sess)
304         .map(|s| s.to_string())
305         .chain(
306             codegen_fn_attrs.target_features
307                 .iter()
308                 .map(|f| {
309                     let feature = &*f.as_str();
310                     format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
311                 })
312         )
313         .collect::<Vec<String>>()
314         .join(",");
315
316     if !features.is_empty() {
317         let val = CString::new(features).unwrap();
318         llvm::AddFunctionAttrStringValue(
319             llfn, llvm::AttributePlace::Function,
320             const_cstr!("target-features"), &val);
321     }
322
323     // Note that currently the `wasm-import-module` doesn't do anything, but
324     // eventually LLVM 7 should read this and ferry the appropriate import
325     // module to the output file.
326     if let Some(id) = id {
327         if cx.tcx.sess.target.target.arch == "wasm32" {
328             if let Some(module) = wasm_import_module(cx.tcx, id) {
329                 llvm::AddFunctionAttrStringValue(
330                     llfn,
331                     llvm::AttributePlace::Function,
332                     const_cstr!("wasm-import-module"),
333                     &module,
334                 );
335             }
336         }
337     }
338 }
339
340 pub fn provide(providers: &mut Providers<'_>) {
341     providers.target_features_whitelist = |tcx, cnum| {
342         assert_eq!(cnum, LOCAL_CRATE);
343         if tcx.sess.opts.actually_rustdoc {
344             // rustdoc needs to be able to document functions that use all the features, so
345             // whitelist them all
346             tcx.arena.alloc(llvm_util::all_known_features()
347                 .map(|(a, b)| (a.to_string(), b))
348                 .collect())
349         } else {
350             tcx.arena.alloc(llvm_util::target_feature_whitelist(tcx.sess)
351                 .iter()
352                 .map(|&(a, b)| (a.to_string(), b))
353                 .collect())
354         }
355     };
356
357     provide_extern(providers);
358 }
359
360 pub fn provide_extern(providers: &mut Providers<'_>) {
361     providers.wasm_import_module_map = |tcx, cnum| {
362         // Build up a map from DefId to a `NativeLibrary` structure, where
363         // `NativeLibrary` internally contains information about
364         // `#[link(wasm_import_module = "...")]` for example.
365         let native_libs = tcx.native_libraries(cnum);
366
367         let def_id_to_native_lib = native_libs.iter().filter_map(|lib|
368             if let Some(id) = lib.foreign_module {
369                 Some((id, lib))
370             } else {
371                 None
372             }
373         ).collect::<FxHashMap<_, _>>();
374
375         let mut ret = FxHashMap::default();
376         for lib in tcx.foreign_modules(cnum).iter() {
377             let module = def_id_to_native_lib
378                 .get(&lib.def_id)
379                 .and_then(|s| s.wasm_import_module);
380             let module = match module {
381                 Some(s) => s,
382                 None => continue,
383             };
384             ret.extend(lib.foreign_items.iter().map(|id| {
385                 assert_eq!(id.krate, cnum);
386                 (*id, module.to_string())
387             }));
388         }
389
390         tcx.arena.alloc(ret)
391     };
392 }
393
394 fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
395     tcx.wasm_import_module_map(id.krate)
396         .get(&id)
397         .map(|s| CString::new(&s[..]).unwrap())
398 }