Skip to content

Commit 8f39b86

Browse files
authored
Rollup merge of #128679 - RalfJung:codegen-fn-attrs, r=nikic
codegen: better centralize function declaration attribute computation For some reason, the codegen backend has two functions that compute which attributes a function declaration gets: `apply_attrs_llfn` and `attributes::from_fn_attrs`. They are called in different places, on entirely different layers of abstraction. To me the code seems cleaner if we centralize this entirely in `apply_attrs_llfn`, so that's what this PR does.
2 parents e342295 + 273c67d commit 8f39b86

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

Diff for: compiler/rustc_codegen_llvm/src/abi.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
55
use rustc_codegen_ssa::mir::place::{PlaceRef, PlaceValue};
66
use rustc_codegen_ssa::traits::*;
77
use rustc_codegen_ssa::MemFlags;
8-
use rustc_middle::bug;
98
use rustc_middle::ty::layout::LayoutOf;
109
pub use rustc_middle::ty::layout::{FAT_PTR_ADDR, FAT_PTR_EXTRA};
1110
use rustc_middle::ty::Ty;
11+
use rustc_middle::{bug, ty};
1212
use rustc_session::config;
1313
pub use rustc_target::abi::call::*;
1414
use rustc_target::abi::{self, HasDataLayout, Int, Size};
1515
pub use rustc_target::spec::abi::Abi;
1616
use rustc_target::spec::SanitizerSet;
1717
use smallvec::SmallVec;
1818

19+
use crate::attributes::llfn_attrs_from_instance;
1920
use crate::builder::Builder;
2021
use crate::context::CodegenCx;
2122
use crate::llvm::{self, Attribute, AttributePlace};
@@ -310,7 +311,16 @@ pub trait FnAbiLlvmExt<'ll, 'tcx> {
310311
fn llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
311312
fn ptr_to_llvm_type(&self, cx: &CodegenCx<'ll, 'tcx>) -> &'ll Type;
312313
fn llvm_cconv(&self) -> llvm::CallConv;
313-
fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value);
314+
315+
/// Apply attributes to a function declaration/definition.
316+
fn apply_attrs_llfn(
317+
&self,
318+
cx: &CodegenCx<'ll, 'tcx>,
319+
llfn: &'ll Value,
320+
instance: Option<ty::Instance<'tcx>>,
321+
);
322+
323+
/// Apply attributes to a function call.
314324
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value);
315325
}
316326

@@ -396,7 +406,12 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
396406
self.conv.into()
397407
}
398408

399-
fn apply_attrs_llfn(&self, cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value) {
409+
fn apply_attrs_llfn(
410+
&self,
411+
cx: &CodegenCx<'ll, 'tcx>,
412+
llfn: &'ll Value,
413+
instance: Option<ty::Instance<'tcx>>,
414+
) {
400415
let mut func_attrs = SmallVec::<[_; 3]>::new();
401416
if self.ret.layout.abi.is_uninhabited() {
402417
func_attrs.push(llvm::AttributeKind::NoReturn.create_attr(cx.llcx));
@@ -477,6 +492,11 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
477492
}
478493
}
479494
}
495+
496+
// If the declaration has an associated instance, compute extra attributes based on that.
497+
if let Some(instance) = instance {
498+
llfn_attrs_from_instance(cx, llfn, instance);
499+
}
480500
}
481501

482502
fn apply_attrs_callsite(&self, bx: &mut Builder<'_, 'll, 'tcx>, callsite: &'ll Value) {

Diff for: compiler/rustc_codegen_llvm/src/attributes.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,10 @@ fn create_alloc_family_attr(llcx: &llvm::Context) -> &llvm::Attribute {
324324
llvm::CreateAttrStringValue(llcx, "alloc-family", "__rust_alloc")
325325
}
326326

327+
/// Helper for `FnAbi::apply_attrs_llfn`:
327328
/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
328329
/// attributes.
329-
pub fn from_fn_attrs<'ll, 'tcx>(
330+
pub fn llfn_attrs_from_instance<'ll, 'tcx>(
330331
cx: &CodegenCx<'ll, 'tcx>,
331332
llfn: &'ll Value,
332333
instance: ty::Instance<'tcx>,

Diff for: compiler/rustc_codegen_llvm/src/callee.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use rustc_middle::ty::{self, Instance, TypeVisitableExt};
1010
use tracing::debug;
1111

1212
use crate::context::CodegenCx;
13+
use crate::llvm;
1314
use crate::value::Value;
14-
use crate::{attributes, llvm};
1515

1616
/// Codegens a reference to a fn/method item, monomorphizing and
1717
/// inlining as it goes.
@@ -78,8 +78,6 @@ pub fn get_fn<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, instance: Instance<'tcx>) ->
7878
};
7979
debug!("get_fn: not casting pointer!");
8080

81-
attributes::from_fn_attrs(cx, llfn, instance);
82-
8381
// Apply an appropriate linkage/visibility value to our item that we
8482
// just declared.
8583
//

Diff for: compiler/rustc_codegen_llvm/src/declare.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
137137
llvm::Visibility::Default,
138138
fn_abi.llvm_type(self),
139139
);
140-
fn_abi.apply_attrs_llfn(self, llfn);
140+
fn_abi.apply_attrs_llfn(self, llfn, instance);
141141

142142
if self.tcx.sess.is_sanitizer_cfi_enabled() {
143143
if let Some(instance) = instance {

Diff for: compiler/rustc_codegen_llvm/src/mono_item.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::debug;
1212
use crate::context::CodegenCx;
1313
use crate::errors::SymbolAlreadyDefined;
1414
use crate::type_of::LayoutLlvmExt;
15-
use crate::{attributes, base, llvm};
15+
use crate::{base, llvm};
1616

1717
impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> {
1818
fn predefine_static(
@@ -87,8 +87,6 @@ impl<'tcx> PreDefineMethods<'tcx> for CodegenCx<'_, 'tcx> {
8787

8888
debug!("predefine_fn: instance = {:?}", instance);
8989

90-
attributes::from_fn_attrs(self, lldecl, instance);
91-
9290
unsafe {
9391
if self.should_assume_dso_local(lldecl, false) {
9492
llvm::LLVMRustSetDSOLocal(lldecl, true);

0 commit comments

Comments
 (0)