1use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr};
3use rustc_hir::def_id::DefId;
4use rustc_middle::middle::codegen_fn_attrs::{
5 CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
6};
7use rustc_middle::ty::{self, TyCtxt};
8use rustc_session::config::{BranchProtection, FunctionReturn, OptLevel, PAuthKey, PacRet};
9use rustc_symbol_mangling::mangle_internal_symbol;
10use rustc_target::spec::{FramePointer, SanitizerSet, StackProbeType, StackProtector};
11use smallvec::SmallVec;
12
13use crate::context::SimpleCx;
14use crate::errors::SanitizerMemtagRequiresMte;
15use crate::llvm::AttributePlace::Function;
16use crate::llvm::{
17 self, AllocKindFlags, Attribute, AttributeKind, AttributePlace, MemoryEffects, Value,
18};
19use crate::{Session, attributes, llvm_util};
20
21pub(crate) fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
22 if !attrs.is_empty() {
23 llvm::AddFunctionAttributes(llfn, idx, attrs);
24 }
25}
26
27pub(crate) fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
28 if !attrs.is_empty() {
29 llvm::AddCallSiteAttributes(callsite, idx, attrs);
30 }
31}
32
33pub(crate) fn inline_attr<'ll, 'tcx>(
35 cx: &SimpleCx<'ll>,
36 tcx: TyCtxt<'tcx>,
37 instance: ty::Instance<'tcx>,
38) -> Option<&'ll Attribute> {
39 let codegen_fn_attrs = tcx.codegen_fn_attrs(instance.def_id());
41 let inline = match (codegen_fn_attrs.inline, &codegen_fn_attrs.optimize) {
42 (_, OptimizeAttr::DoNotOptimize) => InlineAttr::Never,
43 (InlineAttr::None, _) if instance.def.requires_inline(tcx) => InlineAttr::Hint,
44 (inline, _) => inline,
45 };
46
47 if !tcx.sess.opts.unstable_opts.inline_llvm {
48 return Some(AttributeKind::NoInline.create_attr(cx.llcx));
50 }
51 match inline {
52 InlineAttr::Hint => Some(AttributeKind::InlineHint.create_attr(cx.llcx)),
53 InlineAttr::Always | InlineAttr::Force { .. } => {
54 Some(AttributeKind::AlwaysInline.create_attr(cx.llcx))
55 }
56 InlineAttr::Never => {
57 if tcx.sess.target.arch != "amdgpu" {
58 Some(AttributeKind::NoInline.create_attr(cx.llcx))
59 } else {
60 None
61 }
62 }
63 InlineAttr::None => None,
64 }
65}
66
67#[inline]
68fn patchable_function_entry_attrs<'ll>(
69 cx: &SimpleCx<'ll>,
70 sess: &Session,
71 attr: Option<PatchableFunctionEntry>,
72) -> SmallVec<[&'ll Attribute; 2]> {
73 let mut attrs = SmallVec::new();
74 let patchable_spec = attr.unwrap_or_else(|| {
75 PatchableFunctionEntry::from_config(sess.opts.unstable_opts.patchable_function_entry)
76 });
77 let entry = patchable_spec.entry();
78 let prefix = patchable_spec.prefix();
79 if entry > 0 {
80 attrs.push(llvm::CreateAttrStringValue(
81 cx.llcx,
82 "patchable-function-entry",
83 &format!("{}", entry),
84 ));
85 }
86 if prefix > 0 {
87 attrs.push(llvm::CreateAttrStringValue(
88 cx.llcx,
89 "patchable-function-prefix",
90 &format!("{}", prefix),
91 ));
92 }
93 attrs
94}
95
96#[inline]
98pub(crate) fn sanitize_attrs<'ll, 'tcx>(
99 cx: &SimpleCx<'ll>,
100 tcx: TyCtxt<'tcx>,
101 no_sanitize: SanitizerSet,
102) -> SmallVec<[&'ll Attribute; 4]> {
103 let mut attrs = SmallVec::new();
104 let enabled = tcx.sess.opts.unstable_opts.sanitizer - no_sanitize;
105 if enabled.contains(SanitizerSet::ADDRESS) || enabled.contains(SanitizerSet::KERNELADDRESS) {
106 attrs.push(llvm::AttributeKind::SanitizeAddress.create_attr(cx.llcx));
107 }
108 if enabled.contains(SanitizerSet::MEMORY) {
109 attrs.push(llvm::AttributeKind::SanitizeMemory.create_attr(cx.llcx));
110 }
111 if enabled.contains(SanitizerSet::THREAD) {
112 attrs.push(llvm::AttributeKind::SanitizeThread.create_attr(cx.llcx));
113 }
114 if enabled.contains(SanitizerSet::HWADDRESS) {
115 attrs.push(llvm::AttributeKind::SanitizeHWAddress.create_attr(cx.llcx));
116 }
117 if enabled.contains(SanitizerSet::SHADOWCALLSTACK) {
118 attrs.push(llvm::AttributeKind::ShadowCallStack.create_attr(cx.llcx));
119 }
120 if enabled.contains(SanitizerSet::MEMTAG) {
121 let features = tcx.global_backend_features(());
123 let mte_feature =
124 features.iter().map(|s| &s[..]).rfind(|n| ["+mte", "-mte"].contains(&&n[..]));
125 if let None | Some("-mte") = mte_feature {
126 tcx.dcx().emit_err(SanitizerMemtagRequiresMte);
127 }
128
129 attrs.push(llvm::AttributeKind::SanitizeMemTag.create_attr(cx.llcx));
130 }
131 if enabled.contains(SanitizerSet::SAFESTACK) {
132 attrs.push(llvm::AttributeKind::SanitizeSafeStack.create_attr(cx.llcx));
133 }
134 attrs
135}
136
137#[inline]
139pub(crate) fn uwtable_attr(llcx: &llvm::Context, use_sync_unwind: Option<bool>) -> &Attribute {
140 let async_unwind = !use_sync_unwind.unwrap_or(false);
144 llvm::CreateUWTableAttr(llcx, async_unwind)
145}
146
147pub(crate) fn frame_pointer_type_attr<'ll>(
148 cx: &SimpleCx<'ll>,
149 sess: &Session,
150) -> Option<&'ll Attribute> {
151 let mut fp = sess.target.frame_pointer;
152 let opts = &sess.opts;
153 if opts.unstable_opts.instrument_mcount {
156 fp.ratchet(FramePointer::Always);
157 }
158 fp.ratchet(opts.cg.force_frame_pointers);
159 let attr_value = match fp {
160 FramePointer::Always => "all",
161 FramePointer::NonLeaf => "non-leaf",
162 FramePointer::MayOmit => return None,
163 };
164 Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
165}
166
167fn function_return_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
168 let function_return_attr = match sess.opts.unstable_opts.function_return {
169 FunctionReturn::Keep => return None,
170 FunctionReturn::ThunkExtern => AttributeKind::FnRetThunkExtern,
171 };
172
173 Some(function_return_attr.create_attr(cx.llcx))
174}
175
176#[inline]
178fn instrument_function_attr<'ll>(
179 cx: &SimpleCx<'ll>,
180 sess: &Session,
181) -> SmallVec<[&'ll Attribute; 4]> {
182 let mut attrs = SmallVec::new();
183 if sess.opts.unstable_opts.instrument_mcount {
184 let mcount_name = match &sess.target.llvm_mcount_intrinsic {
190 Some(llvm_mcount_intrinsic) => llvm_mcount_intrinsic.as_ref(),
191 None => sess.target.mcount.as_ref(),
192 };
193
194 attrs.push(llvm::CreateAttrStringValue(
195 cx.llcx,
196 "instrument-function-entry-inlined",
197 mcount_name,
198 ));
199 }
200 if let Some(options) = &sess.opts.unstable_opts.instrument_xray {
201 if options.always {
205 attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-always"));
206 }
207 if options.never {
208 attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-never"));
209 }
210 if options.ignore_loops {
211 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-ignore-loops"));
212 }
213 let threshold = options.instruction_threshold.unwrap_or(200);
216 attrs.push(llvm::CreateAttrStringValue(
217 cx.llcx,
218 "xray-instruction-threshold",
219 &threshold.to_string(),
220 ));
221 if options.skip_entry {
222 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-entry"));
223 }
224 if options.skip_exit {
225 attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-exit"));
226 }
227 }
228 attrs
229}
230
231fn nojumptables_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
232 if !sess.opts.unstable_opts.no_jump_tables {
233 return None;
234 }
235
236 Some(llvm::CreateAttrStringValue(cx.llcx, "no-jump-tables", "true"))
237}
238
239fn probestack_attr<'ll, 'tcx>(cx: &SimpleCx<'ll>, tcx: TyCtxt<'tcx>) -> Option<&'ll Attribute> {
240 if tcx
244 .sess
245 .opts
246 .unstable_opts
247 .sanitizer
248 .intersects(SanitizerSet::ADDRESS | SanitizerSet::THREAD)
249 {
250 return None;
251 }
252
253 if tcx.sess.opts.cg.profile_generate.enabled() {
255 return None;
256 }
257
258 let attr_value = match tcx.sess.target.stack_probes {
259 StackProbeType::None => return None,
260 StackProbeType::Inline => "inline-asm",
263 StackProbeType::Call => &mangle_internal_symbol(tcx, "__rust_probestack"),
266 StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
268 if llvm_util::get_version() < min_llvm_version_for_inline {
269 &mangle_internal_symbol(tcx, "__rust_probestack")
270 } else {
271 "inline-asm"
272 }
273 }
274 };
275 Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
276}
277
278fn stackprotector_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
279 let sspattr = match sess.stack_protector() {
280 StackProtector::None => return None,
281 StackProtector::All => AttributeKind::StackProtectReq,
282 StackProtector::Strong => AttributeKind::StackProtectStrong,
283 StackProtector::Basic => AttributeKind::StackProtect,
284 };
285
286 Some(sspattr.create_attr(cx.llcx))
287}
288
289fn backchain_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
290 if sess.target.arch != "s390x" {
291 return None;
292 }
293
294 let requested_features = sess.opts.cg.target_feature.split(',');
295 let found_positive = requested_features.clone().any(|r| r == "+backchain");
296
297 if found_positive { Some(llvm::CreateAttrString(cx.llcx, "backchain")) } else { None }
298}
299
300pub(crate) fn target_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> &'ll Attribute {
301 let target_cpu = llvm_util::target_cpu(sess);
302 llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
303}
304
305pub(crate) fn tune_cpu_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll Attribute> {
306 llvm_util::tune_cpu(sess)
307 .map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
308}
309
310pub(crate) fn target_features_attr<'ll, 'tcx>(
312 cx: &SimpleCx<'ll>,
313 tcx: TyCtxt<'tcx>,
314 function_features: Vec<String>,
315) -> Option<&'ll Attribute> {
316 let global_features = tcx.global_backend_features(()).iter().map(String::as_str);
317 let function_features = function_features.iter().map(String::as_str);
318 let target_features =
319 global_features.chain(function_features).intersperse(",").collect::<String>();
320 (!target_features.is_empty())
321 .then(|| llvm::CreateAttrStringValue(cx.llcx, "target-features", &target_features))
322}
323
324pub(crate) fn non_lazy_bind_attr<'ll>(
327 cx: &SimpleCx<'ll>,
328 sess: &Session,
329) -> Option<&'ll Attribute> {
330 if !sess.needs_plt() { Some(AttributeKind::NonLazyBind.create_attr(cx.llcx)) } else { None }
332}
333
334#[inline]
336pub(crate) fn default_optimisation_attrs<'ll>(
337 cx: &SimpleCx<'ll>,
338 sess: &Session,
339) -> SmallVec<[&'ll Attribute; 2]> {
340 let mut attrs = SmallVec::new();
341 match sess.opts.optimize {
342 OptLevel::Size => {
343 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
344 }
345 OptLevel::SizeMin => {
346 attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
347 attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
348 }
349 _ => {}
350 }
351 attrs
352}
353
354fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
355 llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
356}
357
358pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
362 cx: &SimpleCx<'ll>,
363 tcx: TyCtxt<'tcx>,
364 llfn: &'ll Value,
365 codegen_fn_attrs: &CodegenFnAttrs,
366 instance: Option<ty::Instance<'tcx>>,
367) {
368 let sess = tcx.sess;
369 let mut to_add = SmallVec::<[_; 16]>::new();
370
371 match codegen_fn_attrs.optimize {
372 OptimizeAttr::Default => {
373 to_add.extend(default_optimisation_attrs(cx, sess));
374 }
375 OptimizeAttr::DoNotOptimize => {
376 to_add.push(llvm::AttributeKind::OptimizeNone.create_attr(cx.llcx));
377 }
378 OptimizeAttr::Size => {
379 to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
380 to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
381 }
382 OptimizeAttr::Speed => {}
383 }
384
385 if sess.must_emit_unwind_tables() {
386 to_add.push(uwtable_attr(cx.llcx, sess.opts.unstable_opts.use_sync_unwind));
387 }
388
389 if sess.opts.unstable_opts.profile_sample_use.is_some() {
390 to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
391 }
392
393 to_add.extend(frame_pointer_type_attr(cx, sess));
395 to_add.extend(function_return_attr(cx, sess));
396 to_add.extend(instrument_function_attr(cx, sess));
397 to_add.extend(nojumptables_attr(cx, sess));
398 to_add.extend(probestack_attr(cx, tcx));
399 to_add.extend(stackprotector_attr(cx, sess));
400
401 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NO_BUILTINS) {
402 to_add.push(llvm::CreateAttrString(cx.llcx, "no-builtins"));
403 }
404
405 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::COLD) {
406 to_add.push(AttributeKind::Cold.create_attr(cx.llcx));
407 }
408 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_PURE) {
409 to_add.push(MemoryEffects::ReadOnly.create_attr(cx.llcx));
410 }
411 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::FFI_CONST) {
412 to_add.push(MemoryEffects::None.create_attr(cx.llcx));
413 }
414 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
415 } else {
419 to_add.extend(sanitize_attrs(cx, tcx, codegen_fn_attrs.no_sanitize));
421
422 if let Some(BranchProtection { bti, pac_ret, gcs }) =
424 sess.opts.unstable_opts.branch_protection
425 {
426 assert!(sess.target.arch == "aarch64");
427 if bti {
428 to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
429 }
430 if gcs {
431 to_add.push(llvm::CreateAttrString(cx.llcx, "guarded-control-stack"));
432 }
433 if let Some(PacRet { leaf, pc, key }) = pac_ret {
434 if pc {
435 to_add.push(llvm::CreateAttrString(cx.llcx, "branch-protection-pauth-lr"));
436 }
437 to_add.push(llvm::CreateAttrStringValue(
438 cx.llcx,
439 "sign-return-address",
440 if leaf { "all" } else { "non-leaf" },
441 ));
442 to_add.push(llvm::CreateAttrStringValue(
443 cx.llcx,
444 "sign-return-address-key",
445 if key == PAuthKey::A { "a_key" } else { "b_key" },
446 ));
447 }
448 }
449 }
450 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR)
451 || codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR_ZEROED)
452 {
453 to_add.push(create_alloc_family_attr(cx.llcx));
454 if let Some(instance) = instance
455 && let Some(zv) =
456 tcx.get_attr(instance.def_id(), rustc_span::sym::rustc_allocator_zeroed_variant)
457 && let Some(name) = zv.value_str()
458 {
459 to_add.push(llvm::CreateAttrStringValue(
460 cx.llcx,
461 "alloc-variant-zeroed",
462 &mangle_internal_symbol(tcx, name.as_str()),
463 ));
464 }
465 let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
467 attributes::apply_to_llfn(llfn, AttributePlace::Argument(1), &[alloc_align]);
468 to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 0));
469 let mut flags = AllocKindFlags::Alloc | AllocKindFlags::Aligned;
470 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
471 flags |= AllocKindFlags::Uninitialized;
472 } else {
473 flags |= AllocKindFlags::Zeroed;
474 }
475 to_add.push(llvm::CreateAllocKindAttr(cx.llcx, flags));
476 let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
479 attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
480 }
481 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::REALLOCATOR) {
482 to_add.push(create_alloc_family_attr(cx.llcx));
483 to_add.push(llvm::CreateAllocKindAttr(
484 cx.llcx,
485 AllocKindFlags::Realloc | AllocKindFlags::Aligned,
486 ));
487 let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
489 attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
490 let alloc_align = AttributeKind::AllocAlign.create_attr(cx.llcx);
492 attributes::apply_to_llfn(llfn, AttributePlace::Argument(2), &[alloc_align]);
493 to_add.push(llvm::CreateAllocSizeAttr(cx.llcx, 3));
494 let no_alias = AttributeKind::NoAlias.create_attr(cx.llcx);
495 attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
496 }
497 if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::DEALLOCATOR) {
498 to_add.push(create_alloc_family_attr(cx.llcx));
499 to_add.push(llvm::CreateAllocKindAttr(cx.llcx, AllocKindFlags::Free));
500 let allocated_pointer = AttributeKind::AllocatedPointer.create_attr(cx.llcx);
502 attributes::apply_to_llfn(llfn, AttributePlace::Argument(0), &[allocated_pointer]);
503 }
504 if let Some(align) = codegen_fn_attrs.alignment {
505 llvm::set_alignment(llfn, align);
506 }
507 if let Some(backchain) = backchain_attr(cx, sess) {
508 to_add.push(backchain);
509 }
510 to_add.extend(patchable_function_entry_attrs(
511 cx,
512 sess,
513 codegen_fn_attrs.patchable_function_entry,
514 ));
515
516 to_add.push(target_cpu_attr(cx, sess));
520 to_add.extend(tune_cpu_attr(cx, sess));
523
524 let function_features =
525 codegen_fn_attrs.target_features.iter().map(|f| f.name.as_str()).collect::<Vec<&str>>();
526
527 if function_features.is_empty() {
530 if let Some(instance) = instance
531 && let Some(inline_attr) = inline_attr(cx, tcx, instance)
532 {
533 to_add.push(inline_attr);
534 }
535 }
536
537 let function_features = function_features
538 .iter()
539 .flat_map(|feat| llvm_util::to_llvm_features(sess, feat))
541 .flat_map(|feat| feat.into_iter().map(|f| format!("+{f}")))
543 .chain(codegen_fn_attrs.instruction_set.iter().map(|x| match x {
544 InstructionSetAttr::ArmA32 => "-thumb-mode".to_string(),
545 InstructionSetAttr::ArmT32 => "+thumb-mode".to_string(),
546 }))
547 .collect::<Vec<String>>();
548
549 if sess.target.is_like_wasm {
550 if let Some(instance) = instance
553 && let Some(module) = wasm_import_module(tcx, instance.def_id())
554 {
555 to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", module));
556
557 let name =
558 codegen_fn_attrs.symbol_name.unwrap_or_else(|| tcx.item_name(instance.def_id()));
559 let name = name.as_str();
560 to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
561 }
562 }
563
564 to_add.extend(target_features_attr(cx, tcx, function_features));
565
566 attributes::apply_to_llfn(llfn, Function, &to_add);
567}
568
569fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
570 tcx.wasm_import_module_map(id.krate).get(&id)
571}