1 //! Set and unset common attributes on LLVM values.
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;
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};
19 pub use rustc_attr::{InlineAttr, InstructionSetAttr, OptimizeAttr};
21 use crate::context::CodegenCx;
22 use crate::value::Value;
24 pub fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
25 if !attrs.is_empty() {
26 llvm::AddFunctionAttributes(llfn, idx, attrs);
30 pub fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
31 if !attrs.is_empty() {
32 llvm::AddCallSiteAttributes(callsite, idx, attrs);
36 /// Get LLVM attribute for the provided inline heuristic.
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));
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))
53 InlineAttr::None => None,
57 /// Get LLVM sanitize attributes.
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));
68 if enabled.contains(SanitizerSet::MEMORY) {
69 attrs.push(llvm::AttributeKind::SanitizeMemory.create_attr(cx.llcx));
71 if enabled.contains(SanitizerSet::THREAD) {
72 attrs.push(llvm::AttributeKind::SanitizeThread.create_attr(cx.llcx));
74 if enabled.contains(SanitizerSet::HWADDRESS) {
75 attrs.push(llvm::AttributeKind::SanitizeHWAddress.create_attr(cx.llcx));
77 if enabled.contains(SanitizerSet::SHADOWCALLSTACK) {
78 attrs.push(llvm::AttributeKind::ShadowCallStack.create_attr(cx.llcx));
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(());
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);
89 attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
94 /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function.
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
100 llvm::CreateUWTableAttr(llcx, true)
103 pub fn frame_pointer_type_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
104 let mut fp = cx.sess().target.frame_pointer;
105 let opts = &cx.sess().opts;
106 // "mcount" function relies on stack pointer.
107 // See <https://sourceware.org/binutils/docs/gprof/Implementation.html>.
108 if opts.unstable_opts.instrument_mcount || matches!(opts.cg.force_frame_pointers, Some(true)) {
109 fp = FramePointer::Always;
111 let attr_value = match fp {
112 FramePointer::Always => "all",
113 FramePointer::NonLeaf => "non-leaf",
114 FramePointer::MayOmit => return None,
116 Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
119 /// Tell LLVM what instrument function to insert.
121 fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
122 if cx.sess().opts.unstable_opts.instrument_mcount {
123 // Similar to `clang -pg` behavior. Handled by the
124 // `post-inline-ee-instrument` LLVM pass.
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();
130 Some(llvm::CreateAttrStringValue(
132 "instrument-function-entry-inlined",
140 fn nojumptables_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
141 if !cx.sess().opts.unstable_opts.no_jump_tables {
145 Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true"))
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.
157 .intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
162 // probestack doesn't play nice either with `-C profile-generate`.
163 if cx.sess().opts.cg.profile_generate.enabled() {
167 // probestack doesn't play nice either with gcov profiling.
168 if cx.sess().opts.unstable_opts.profile {
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 {
189 Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
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,
200 Some(sspattr.create_attr(cx.llcx))
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)
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))
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))
224 /// Get the default optimizations attrs for a function.
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 {
232 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
234 OptLevel::SizeMin => {
235 attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
236 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
243 fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
244 llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
247 /// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
249 pub fn from_fn_attrs<'ll, 'tcx>(
250 cx: &CodegenCx<'ll, 'tcx>,
252 instance: ty::Instance<'tcx>,
254 let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
256 let mut to_add = SmallVec::<[_; 16]>::new();
258 match codegen_fn_attrs.optimize {
259 OptimizeAttr::None => {
260 to_add.extend(default_optimisation_attrs(cx));
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));
266 OptimizeAttr::Speed => {}
270 if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
273 codegen_fn_attrs.inline
275 to_add.extend(inline_attr(cx, inline));
277 // The `uwtable` attribute according to LLVM is:
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.
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.
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));
297 if cx.sess().opts.unstable_opts.profile_sample_use.is_some() {
298 to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
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));
308 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
309 to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
311 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_RETURNS_TWICE) {
312 to_add.push(AttributeKind::ReturnsTwice.create_attr(cx.llcx));
314 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
315 to_add.push(MemoryEffects::ReadOnly.create_attr(cx.llcx));
317 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
318 to_add.push(MemoryEffects::None.create_attr(cx.llcx));
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"));
329 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR)
330 || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
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;
342 flags |= AllocKindFlags::Zeroed;
344 to_add.push(llvm::CreateAllocKindAttr(cx.llcx, flags));
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]);
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(
355 AllocKindFlags::Realloc | AllocKindFlags::Aligned,
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));
365 let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
366 attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
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]);
377 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
378 to_add.push(llvm::CreateAttrString(cx.llcx, "cmse_nonsecure_entry"));
380 if let Some(align) = codegen_fn_attrs.alignment {
381 llvm::set_alignment(llfn, align as usize);
383 to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
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));
393 let function_features =
394 codegen_fn_attrs.target_features.iter().map(|f| f.as_str()).collect::<Vec<&str>>();
396 if let Some(f) = llvm_util::check_tied_features(
398 &function_features.iter().map(|f| (*f, true)).collect(),
402 .get_attrs(instance.def_id(), sym::target_feature)
404 .map_or_else(|| cx.tcx.def_span(instance.def_id()), |a| a.span);
407 .create_err(TargetFeatureDisableOrEnable {
410 missing_features: Some(MissingFeatures),
416 let mut function_features = function_features
419 llvm_util::to_llvm_features(cx.tcx.sess, feat).into_iter().map(|f| format!("+{}", f))
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(),
425 .collect::<Vec<String>>();
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));
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));
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());
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));
459 attributes::apply_to_llfn(llfn, Function, &to_add);
462 fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
463 tcx.wasm_import_module_map(id.krate).get(&id)