]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_llvm/src/attributes.rs
Rollup merge of #105847 - compiler-errors:issue-104396, r=oli-obk
[rust.git] / compiler / rustc_codegen_llvm / src / attributes.rs
1 //! Set and unset common attributes on LLVM values.
2
3 use rustc_codegen_ssa::traits::*;
4 use rustc_data_structures::small_str::SmallStr;
5 use rustc_hir::def_id::DefId;
6 use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
7 use rustc_middle::ty::{self, TyCtxt};
8 use rustc_session::config::OptLevel;
9 use rustc_span::symbol::sym;
10 use rustc_target::spec::abi::Abi;
11 use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
12 use smallvec::SmallVec;
13
14 use crate::attributes;
15 use crate::errors::{MissingFeatures, SanitizerMemtagRequiresMte, TargetFeatureDisableOrEnable};
16 use crate::llvm::AttributePlace::Function;
17 use crate::llvm::{self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects};
18 use crate::llvm_util;
19 pub use rustc_attr::{InlineAttr, InstructionSetAttr, OptimizeAttr};
20
21 use crate::context::CodegenCx;
22 use crate::value::Value;
23
24 pub fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
25     if !attrs.is_empty() {
26         llvm::AddFunctionAttributes(llfn, idx, attrs);
27     }
28 }
29
30 pub fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
31     if !attrs.is_empty() {
32         llvm::AddCallSiteAttributes(callsite, idx, attrs);
33     }
34 }
35
36 /// Get LLVM attribute for the provided inline heuristic.
37 #[inline]
38 fn inline_attr<'ll>(cx: &CodegenCx<'ll, '_>, inline: InlineAttr) -> Option<&'ll Attribute> {
39     if !cx.tcx.sess.opts.unstable_opts.inline_llvm {
40         // disable LLVM inlining
41         return Some(AttributeKind::NoInline.create_attr(cx.llcx));
42     }
43     match inline {
44         InlineAttr::Hint => Some(AttributeKind::InlineHint.create_attr(cx.llcx)),
45         InlineAttr::Always => Some(AttributeKind::AlwaysInline.create_attr(cx.llcx)),
46         InlineAttr::Never => {
47             if cx.sess().target.arch != "amdgpu" {
48                 Some(AttributeKind::NoInline.create_attr(cx.llcx))
49             } else {
50                 None
51             }
52         }
53         InlineAttr::None => None,
54     }
55 }
56
57 /// Get LLVM sanitize attributes.
58 #[inline]
59 pub fn sanitize_attrs<'ll>(
60     cx: &CodegenCx<'ll, '_>,
61     no_sanitize: SanitizerSet,
62 ) -> SmallVec<[&'ll Attribute; 4]> {
63     let mut attrs = SmallVec::new();
64     let enabled = cx.tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
65     if enabled.contains(SanitizerSet::ADDRESS) {
66         attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
67     }
68     if enabled.contains(SanitizerSet::MEMORY) {
69         attrs.push(llvm::AttributeKind::SanitizeMemory.create_attr(cx.llcx));
70     }
71     if enabled.contains(SanitizerSet::THREAD) {
72         attrs.push(llvm::AttributeKind::SanitizeThread.create_attr(cx.llcx));
73     }
74     if enabled.contains(SanitizerSet::HWADDRESS) {
75         attrs.push(llvm::AttributeKind::SanitizeHWAddress.create_attr(cx.llcx));
76     }
77     if enabled.contains(SanitizerSet::SHADOWCALLSTACK) {
78         attrs.push(llvm::AttributeKind::ShadowCallStack.create_attr(cx.llcx));
79     }
80     if enabled.contains(SanitizerSet::MEMTAG) {
81         // Check to make sure the mte target feature is actually enabled.
82         let features = cx.tcx.global_backend_features(());
83         let mte_feature =
84             features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
85         if let None | Some("-mte") = mte_feature {
86             cx.tcx.sess.emit_err(SanitizerMemtagRequiresMte);
87         }
88
89         attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
90     }
91     attrs
92 }
93
94 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
95 #[inline]
96 pub fn uwtable_attr(llcx: &llvm::Context) -> &Attribute {
97     // NOTE: We should determine if we even need async unwind tables, as they
98     // take have more overhead and if we can use sync unwind tables we
99     // probably should.
100     llvm::CreateUWTableAttr(llcx, true)
101 }
102
103 pub fn frame_pointer_type_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
104     let mut fp = cx.sess().target.frame_pointer;
105     // "mcount" function relies on stack pointer.
106     // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
107     if cx.sess().instrument_mcount() || matches!(cx.sess().opts.cg.force_frame_pointers, Some(true))
108     {
109         fp = FramePointer::Always;
110     }
111     let attr_value = match fp {
112         FramePointer::Always => "all",
113         FramePointer::NonLeaf => "non-leaf",
114         FramePointer::MayOmit => return None,
115     };
116     Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
117 }
118
119 /// Tell LLVM what instrument function to insert.
120 #[inline]
121 fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
122     if cx.sess().instrument_mcount() {
123         // Similar to `clang -pg` behavior. Handled by the
124         // `post-inline-ee-instrument` LLVM pass.
125
126         // The function name varies on platforms.
127         // See test/CodeGen/mcount.c in clang.
128         let mcount_name = cx.sess().target.mcount.as_ref();
129
130         Some(llvm::CreateAttrStringValue(
131             cx.llcx,
132             "instrument-function-entry-inlined",
133             &mcount_name,
134         ))
135     } else {
136         None
137     }
138 }
139
140 fn nojumptables_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
141     if !cx.sess().opts.unstable_opts.no_jump_tables {
142         return None;
143     }
144
145     Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true"))
146 }
147
148 fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
149     // Currently stack probes seem somewhat incompatible with the address
150     // sanitizer and thread sanitizer. With asan we're already protected from
151     // stack overflow anyway so we don't really need stack probes regardless.
152     if cx
153         .sess()
154         .opts
155         .unstable_opts
156         .sanitizer
157         .intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
158     {
159         return None;
160     }
161
162     // probestack doesn't play nice either with `-C profile-generate`.
163     if cx.sess().opts.cg.profile_generate.enabled() {
164         return None;
165     }
166
167     // probestack doesn't play nice either with gcov profiling.
168     if cx.sess().opts.unstable_opts.profile {
169         return None;
170     }
171
172     let attr_value = match cx.sess().target.stack_probes {
173         StackProbeType::None => return None,
174         // Request LLVM to generate the probes inline. If the given LLVM version does not support
175         // this, no probe is generated at all (even if the attribute is specified).
176         StackProbeType::Inline => "inline-asm",
177         // Flag our internal `__rust_probestack` function as the stack probe symbol.
178         // This is defined in the `compiler-builtins` crate for each architecture.
179         StackProbeType::Call => "__rust_probestack",
180         // Pick from the two above based on the LLVM version.
181         StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
182             if llvm_util::get_version() < min_llvm_version_for_inline {
183                 "__rust_probestack"
184             } else {
185                 "inline-asm"
186             }
187         }
188     };
189     Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
190 }
191
192 fn stackprotector_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
193     let sspattr = match cx.sess().stack_protector() {
194         StackProtector::None => return None,
195         StackProtector::All => AttributeKind::StackProtectReq,
196         StackProtector::Strong => AttributeKind::StackProtectStrong,
197         StackProtector::Basic => AttributeKind::StackProtect,
198     };
199
200     Some(sspattr.create_attr(cx.llcx))
201 }
202
203 pub fn target_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Attribute {
204     let target_cpu = llvm_util::target_cpu(cx.tcx.sess);
205     llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
206 }
207
208 pub fn tune_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
209     llvm_util::tune_cpu(cx.tcx.sess)
210         .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
211 }
212
213 /// Get the `NonLazyBind` LLVM attribute,
214 /// if the codegen options allow skipping the PLT.
215 pub fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
216     // Don't generate calls through PLT if it's not necessary
217     if !cx.sess().needs_plt() {
218         Some(AttributeKind::NonLazyBind.create_attr(cx.llcx))
219     } else {
220         None
221     }
222 }
223
224 /// Get the default optimizations attrs for a function.
225 #[inline]
226 pub(crate) fn default_optimisation_attrs<'ll>(
227     cx: &CodegenCx<'ll, '_>,
228 ) -> SmallVec<[&'ll Attribute; 2]> {
229     let mut attrs = SmallVec::new();
230     match cx.sess().opts.optimize {
231         OptLevel::Size => {
232             attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
233         }
234         OptLevel::SizeMin => {
235             attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
236             attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
237         }
238         _ => {}
239     }
240     attrs
241 }
242
243 fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
244     llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
245 }
246
247 /// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
248 /// attributes.
249 pub fn from_fn_attrs<'ll, 'tcx>(
250     cx: &CodegenCx<'ll, 'tcx>,
251     llfn: &'ll Value,
252     instance: ty::Instance<'tcx>,
253 ) {
254     let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
255
256     let mut to_add = SmallVec::<[_; 16]>::new();
257
258     match codegen_fn_attrs.optimize {
259         OptimizeAttr::None => {
260             to_add.extend(default_optimisation_attrs(cx));
261         }
262         OptimizeAttr::Size => {
263             to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
264             to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
265         }
266         OptimizeAttr::Speed => {}
267     }
268
269     let inline =
270         if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
271             InlineAttr::Hint
272         } else {
273             codegen_fn_attrs.inline
274         };
275     to_add.extend(inline_attr(cx, inline));
276
277     // The `uwtable` attribute according to LLVM is:
278     //
279     //     This attribute indicates that the ABI being targeted requires that an
280     //     unwind table entry be produced for this function even if we can show
281     //     that no exceptions passes by it. This is normally the case for the
282     //     ELF x86-64 abi, but it can be disabled for some compilation units.
283     //
284     // Typically when we're compiling with `-C panic=abort` (which implies this
285     // `no_landing_pads` check) we don't need `uwtable` because we can't
286     // generate any exceptions! On Windows, however, exceptions include other
287     // events such as illegal instructions, segfaults, etc. This means that on
288     // Windows we end up still needing the `uwtable` attribute even if the `-C
289     // panic=abort` flag is passed.
290     //
291     // You can also find more info on why Windows always requires uwtables here:
292     //      https://bugzilla.mozilla.org/show_bug.cgi?id=1302078
293     if cx.sess().must_emit_unwind_tables() {
294         to_add.push(uwtable_attr(cx.llcx));
295     }
296
297     if cx.sess().opts.unstable_opts.profile_sample_use.is_some() {
298         to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
299     }
300
301     // FIXME: none of these three functions interact with source level attributes.
302     to_add.extend(frame_pointer_type_attr(cx));
303     to_add.extend(instrument_function_attr(cx));
304     to_add.extend(nojumptables_attr(cx));
305     to_add.extend(probestack_attr(cx));
306     to_add.extend(stackprotector_attr(cx));
307
308     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
309         to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
310     }
311     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_RETURNS_TWICE) {
312         to_add.push(AttributeKind::ReturnsTwice.create_attr(cx.llcx));
313     }
314     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
315         to_add.push(MemoryEffects::ReadOnly.create_attr(cx.llcx));
316     }
317     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
318         to_add.push(MemoryEffects::None.create_attr(cx.llcx));
319     }
320     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
321         to_add.push(AttributeKind::Naked.create_attr(cx.llcx));
322         // HACK(jubilee): "indirect branch tracking" works by attaching prologues to functions.
323         // And it is a module-level attribute, so the alternative is pulling naked functions into new LLVM modules.
324         // Otherwise LLVM's "naked" functions come with endbr prefixes per https://github.com/rust-lang/rust/issues/98768
325         to_add.push(AttributeKind::NoCfCheck.create_attr(cx.llcx));
326         // Need this for AArch64.
327         to_add.push(llvm::CreateAttrStringValue(cx.llcx, "branch-target-enforcement", "false"));
328     }
329     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR)
330         || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
331     {
332         if llvm_util::get_version() >= (15, 0, 0) {
333             to_add.push(create_alloc_family_attr(cx.llcx));
334             // apply to argument place instead of function
335             let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
336             attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);
337             to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 0));
338             let mut flags = AllocKindFlags::Alloc | AllocKindFlags::Aligned;
339             if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
340                 flags |= AllocKindFlags::Uninitialized;
341             } else {
342                 flags |= AllocKindFlags::Zeroed;
343             }
344             to_add.push(llvm::CreateAllocKindAttr(cx.llcx, flags));
345         }
346         // apply to return place instead of function (unlike all other attributes applied in this function)
347         let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
348         attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
349     }
350     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::REALLOCATOR) {
351         if llvm_util::get_version() >= (15, 0, 0) {
352             to_add.push(create_alloc_family_attr(cx.llcx));
353             to_add.push(llvm::CreateAllocKindAttr(
354                 cx.llcx,
355                 AllocKindFlags::Realloc | AllocKindFlags::Aligned,
356             ));
357             // applies to argument place instead of function place
358             let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
359             attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
360             // apply to argument place instead of function
361             let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
362             attributes::apply_to_llfn(llfn, AttributePlace::Argument(2), &[alloc_align]);
363             to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 3));
364         }
365         let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
366         attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
367     }
368     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::DEALLOCATOR) {
369         if llvm_util::get_version() >= (15, 0, 0) {
370             to_add.push(create_alloc_family_attr(cx.llcx));
371             to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
372             // applies to argument place instead of function place
373             let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
374             attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
375         }
376     }
377     if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
378         to_add.push(llvm::CreateAttrString(cx.llcx, "cmse_nonsecure_entry"));
379     }
380     if let Some(align) = codegen_fn_attrs.alignment {
381         llvm::set_alignment(llfn, align as usize);
382     }
383     to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
384
385     // Always annotate functions with the target-cpu they are compiled for.
386     // Without this, ThinLTO won't inline Rust functions into Clang generated
387     // functions (because Clang annotates functions this way too).
388     to_add.push(target_cpu_attr(cx));
389     // tune-cpu is only conveyed through the attribute for our purpose.
390     // The target doesn't care; the subtarget reads our attribute.
391     to_add.extend(tune_cpu_attr(cx));
392
393     let function_features =
394         codegen_fn_attrs.target_features.iter().map(|f| f.as_str()).collect::<Vec<&str>>();
395
396     if let Some(f) = llvm_util::check_tied_features(
397         cx.tcx.sess,
398         &function_features.iter().map(|f| (*f, true)).collect(),
399     ) {
400         let span = cx
401             .tcx
402             .get_attrs(instance.def_id(), sym::target_feature)
403             .next()
404             .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
405         cx.tcx
406             .sess
407             .create_err(TargetFeatureDisableOrEnable {
408                 features: f,
409                 span: Some(span),
410                 missing_features: Some(MissingFeatures),
411             })
412             .emit();
413         return;
414     }
415
416     let mut function_features = function_features
417         .iter()
418         .flat_map(|feat| {
419             llvm_util::to_llvm_features(cx.tcx.sess, feat).into_iter().map(|f| format!("+{}", f))
420         })
421         .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
422             InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
423             InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
424         }))
425         .collect::<Vec<String>>();
426
427     if cx.tcx.sess.target.is_like_wasm {
428         // If this function is an import from the environment but the wasm
429         // import has a specific module/name, apply them here.
430         if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
431             to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", &module));
432
433             let name =
434                 codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
435             let name = name.as_str();
436             to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
437         }
438
439         // The `"wasm"` abi on wasm targets automatically enables the
440         // `+multivalue` feature because the purpose of the wasm abi is to match
441         // the WebAssembly specification, which has this feature. This won't be
442         // needed when LLVM enables this `multivalue` feature by default.
443         if !cx.tcx.is_closure(instance.def_id()) {
444             let abi = cx.tcx.fn_sig(instance.def_id()).abi();
445             if abi == Abi::Wasm {
446                 function_features.push("+multivalue".to_string());
447             }
448         }
449     }
450
451     let global_features = cx.tcx.global_backend_features(()).iter().map(|s| s.as_str());
452     let function_features = function_features.iter().map(|s| s.as_str());
453     let target_features =
454         global_features.chain(function_features).intersperse(",").collect::<SmallStr<1024>>();
455     if !target_features.is_empty() {
456         to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features));
457     }
458
459     attributes::apply_to_llfn(llfn, Function, &to_add);
460 }
461
462 fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
463     tcx.wasm_import_module_map(id.krate).get(&id)
464 }