1 //! Set and unset common attributes on LLVM values.
5 use rustc_codegen_ssa::traits::*;
6 use rustc_data_structures::const_cstr;
7 use rustc_data_structures::fx::FxHashMap;
8 use rustc_data_structures::small_c_str::SmallCStr;
9 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
10 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
11 use rustc_middle::ty::layout::HasTyCtxt;
12 use rustc_middle::ty::query::Providers;
13 use rustc_middle::ty::{self, TyCtxt};
14 use rustc_session::config::{OptLevel, SanitizerSet};
15 use rustc_session::Session;
17 use crate::attributes;
18 use crate::llvm::AttributePlace::Function;
19 use crate::llvm::{self, Attribute};
21 pub use rustc_attr::{InlineAttr, OptimizeAttr};
23 use crate::context::CodegenCx;
24 use crate::value::Value;
26 /// Mark LLVM function to use provided inline heuristic.
28 fn inline(cx: &CodegenCx<'ll, '_>, val: &'ll Value, inline: InlineAttr) {
29 use self::InlineAttr::*;
31 Hint => Attribute::InlineHint.apply_llfn(Function, val),
32 Always => Attribute::AlwaysInline.apply_llfn(Function, val),
34 if cx.tcx().sess.target.target.arch != "amdgpu" {
35 Attribute::NoInline.apply_llfn(Function, val);
39 Attribute::InlineHint.unapply_llfn(Function, val);
40 Attribute::AlwaysInline.unapply_llfn(Function, val);
41 Attribute::NoInline.unapply_llfn(Function, val);
46 /// Apply LLVM sanitize attributes.
48 pub fn sanitize(cx: &CodegenCx<'ll, '_>, no_sanitize: SanitizerSet, llfn: &'ll Value) {
49 let enabled = cx.tcx.sess.opts.debugging_opts.sanitizer - no_sanitize;
50 if enabled.contains(SanitizerSet::ADDRESS) {
51 llvm::Attribute::SanitizeAddress.apply_llfn(Function, llfn);
53 if enabled.contains(SanitizerSet::MEMORY) {
54 llvm::Attribute::SanitizeMemory.apply_llfn(Function, llfn);
56 if enabled.contains(SanitizerSet::THREAD) {
57 llvm::Attribute::SanitizeThread.apply_llfn(Function, llfn);
61 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
63 pub fn emit_uwtable(val: &'ll Value, emit: bool) {
64 Attribute::UWTable.toggle_llfn(Function, val, emit);
67 /// Tell LLVM if this function should be 'naked', i.e., skip the epilogue and prologue.
69 fn naked(val: &'ll Value, is_naked: bool) {
70 Attribute::Naked.toggle_llfn(Function, val, is_naked);
73 pub fn set_frame_pointer_elimination(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
74 if cx.sess().must_not_eliminate_frame_pointers() {
75 llvm::AddFunctionAttrStringValue(
77 llvm::AttributePlace::Function,
78 const_cstr!("frame-pointer"),
84 /// Tell LLVM what instrument function to insert.
86 fn set_instrument_function(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
87 if cx.sess().instrument_mcount() {
88 // Similar to `clang -pg` behavior. Handled by the
89 // `post-inline-ee-instrument` LLVM pass.
91 // The function name varies on platforms.
92 // See test/CodeGen/mcount.c in clang.
94 CString::new(cx.sess().target.target.options.target_mcount.as_str().as_bytes())
97 llvm::AddFunctionAttrStringValue(
99 llvm::AttributePlace::Function,
100 const_cstr!("instrument-function-entry-inlined"),
106 fn set_probestack(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
107 // Only use stack probes if the target specification indicates that we
108 // should be using stack probes
109 if !cx.sess().target.target.options.stack_probes {
113 // Currently stack probes seem somewhat incompatible with the address
114 // sanitizer and thread sanitizer. With asan we're already protected from
115 // stack overflow anyway so we don't really need stack probes regardless.
121 .intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
126 // probestack doesn't play nice either with `-C profile-generate`.
127 if cx.sess().opts.cg.profile_generate.enabled() {
131 // probestack doesn't play nice either with gcov profiling.
132 if cx.sess().opts.debugging_opts.profile {
136 // Flag our internal `__rust_probestack` function as the stack probe symbol.
137 // This is defined in the `compiler-builtins` crate for each architecture.
138 llvm::AddFunctionAttrStringValue(
140 llvm::AttributePlace::Function,
141 const_cstr!("probe-stack"),
142 const_cstr!("__rust_probestack"),
146 fn translate_obsolete_target_features(feature: &str) -> &str {
147 const LLVM9_FEATURE_CHANGES: &[(&str, &str)] =
148 &[("+fp-only-sp", "-fp64"), ("-fp-only-sp", "+fp64"), ("+d16", "-d32"), ("-d16", "+d32")];
149 if llvm_util::get_major_version() >= 9 {
150 for &(old, new) in LLVM9_FEATURE_CHANGES {
156 for &(old, new) in LLVM9_FEATURE_CHANGES {
165 pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
166 const RUSTC_SPECIFIC_FEATURES: &[&str] = &["crt-static"];
173 .filter(|f| !RUSTC_SPECIFIC_FEATURES.iter().any(|s| f.contains(s)));
180 .filter(|l| !l.is_empty())
181 .map(translate_obsolete_target_features)
184 pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
185 let target_cpu = SmallCStr::new(llvm_util::target_cpu(cx.tcx.sess));
186 llvm::AddFunctionAttrStringValue(
188 llvm::AttributePlace::Function,
189 const_cstr!("target-cpu"),
190 target_cpu.as_c_str(),
194 /// Sets the `NonLazyBind` LLVM attribute on a given function,
195 /// assuming the codegen options allow skipping the PLT.
196 pub fn non_lazy_bind(sess: &Session, llfn: &'ll Value) {
197 // Don't generate calls through PLT if it's not necessary
198 if !sess.needs_plt() {
199 Attribute::NonLazyBind.apply_llfn(Function, llfn);
203 pub(crate) fn default_optimisation_attrs(sess: &Session, llfn: &'ll Value) {
204 match sess.opts.optimize {
206 llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
207 llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
208 llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
210 OptLevel::SizeMin => {
211 llvm::Attribute::MinSize.apply_llfn(Function, llfn);
212 llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
213 llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
216 llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
217 llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
218 llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
224 /// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
226 pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::Instance<'tcx>) {
227 let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
229 match codegen_fn_attrs.optimize {
230 OptimizeAttr::None => {
231 default_optimisation_attrs(cx.tcx.sess, llfn);
233 OptimizeAttr::Speed => {
234 llvm::Attribute::MinSize.unapply_llfn(Function, llfn);
235 llvm::Attribute::OptimizeForSize.unapply_llfn(Function, llfn);
236 llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
238 OptimizeAttr::Size => {
239 llvm::Attribute::MinSize.apply_llfn(Function, llfn);
240 llvm::Attribute::OptimizeForSize.apply_llfn(Function, llfn);
241 llvm::Attribute::OptimizeNone.unapply_llfn(Function, llfn);
245 // FIXME(eddyb) consolidate these two `inline` calls (and avoid overwrites).
246 if instance.def.requires_inline(cx.tcx) {
247 inline(cx, llfn, attributes::InlineAttr::Hint);
250 inline(cx, llfn, codegen_fn_attrs.inline.clone());
252 // The `uwtable` attribute according to LLVM is:
254 // This attribute indicates that the ABI being targeted requires that an
255 // unwind table entry be produced for this function even if we can show
256 // that no exceptions passes by it. This is normally the case for the
257 // ELF x86-64 abi, but it can be disabled for some compilation units.
259 // Typically when we're compiling with `-C panic=abort` (which implies this
260 // `no_landing_pads` check) we don't need `uwtable` because we can't
261 // generate any exceptions! On Windows, however, exceptions include other
262 // events such as illegal instructions, segfaults, etc. This means that on
263 // Windows we end up still needing the `uwtable` attribute even if the `-C
264 // panic=abort` flag is passed.
266 // You can also find more info on why Windows is whitelisted here in:
267 // https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
268 if cx.sess().must_emit_unwind_tables() {
269 attributes::emit_uwtable(llfn, true);
272 set_frame_pointer_elimination(cx, llfn);
273 set_instrument_function(cx, llfn);
274 set_probestack(cx, llfn);
276 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
277 Attribute::Cold.apply_llfn(Function, llfn);
279 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_RETURNS_TWICE) {
280 Attribute::ReturnsTwice.apply_llfn(Function, llfn);
282 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
283 Attribute::ReadOnly.apply_llfn(Function, llfn);
285 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
286 Attribute::ReadNone.apply_llfn(Function, llfn);
288 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
291 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
292 Attribute::NoAlias.apply_llfn(llvm::AttributePlace::ReturnValue, llfn);
294 sanitize(cx, codegen_fn_attrs.no_sanitize, llfn);
296 // Always annotate functions with the target-cpu they are compiled for.
297 // Without this, ThinLTO won't inline Rust functions into Clang generated
298 // functions (because Clang annotates functions this way too).
299 apply_target_cpu_attr(cx, llfn);
301 let features = llvm_target_features(cx.tcx.sess)
302 .map(|s| s.to_string())
303 .chain(codegen_fn_attrs.target_features.iter().map(|f| {
304 let feature = &f.as_str();
305 format!("+{}", llvm_util::to_llvm_feature(cx.tcx.sess, feature))
307 .collect::<Vec<String>>()
310 if !features.is_empty() {
311 let val = CString::new(features).unwrap();
312 llvm::AddFunctionAttrStringValue(
314 llvm::AttributePlace::Function,
315 const_cstr!("target-features"),
320 // Note that currently the `wasm-import-module` doesn't do anything, but
321 // eventually LLVM 7 should read this and ferry the appropriate import
322 // module to the output file.
323 if cx.tcx.sess.target.target.arch == "wasm32" {
324 if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
325 llvm::AddFunctionAttrStringValue(
327 llvm::AttributePlace::Function,
328 const_cstr!("wasm-import-module"),
333 codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
334 let name = CString::new(&name.as_str()[..]).unwrap();
335 llvm::AddFunctionAttrStringValue(
337 llvm::AttributePlace::Function,
338 const_cstr!("wasm-import-name"),
345 pub fn provide(providers: &mut Providers) {
346 providers.target_features_whitelist = |tcx, cnum| {
347 assert_eq!(cnum, LOCAL_CRATE);
348 if tcx.sess.opts.actually_rustdoc {
349 // rustdoc needs to be able to document functions that use all the features, so
350 // whitelist them all
351 llvm_util::all_known_features().map(|(a, b)| (a.to_string(), b)).collect()
353 llvm_util::target_feature_whitelist(tcx.sess)
355 .map(|&(a, b)| (a.to_string(), b))
360 provide_extern(providers);
363 pub fn provide_extern(providers: &mut Providers) {
364 providers.wasm_import_module_map = |tcx, cnum| {
365 // Build up a map from DefId to a `NativeLib` structure, where
366 // `NativeLib` internally contains information about
367 // `#[link(wasm_import_module = "...")]` for example.
368 let native_libs = tcx.native_libraries(cnum);
370 let def_id_to_native_lib = native_libs
372 .filter_map(|lib| lib.foreign_module.map(|id| (id, lib)))
373 .collect::<FxHashMap<_, _>>();
375 let mut ret = FxHashMap::default();
376 for lib in tcx.foreign_modules(cnum).iter() {
377 let module = def_id_to_native_lib.get(&lib.def_id).and_then(|s| s.wasm_import_module);
378 let module = match module {
382 ret.extend(lib.foreign_items.iter().map(|id| {
383 assert_eq!(id.krate, cnum);
384 (*id, module.to_string())
392 fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
393 tcx.wasm_import_module_map(id.krate).get(&id).map(|s| CString::new(&s[..]).unwrap())