Skip to content

Commit e631ee5

Browse files
authored
Unrolled build for #145429
Rollup merge of #145429 - bjorn3:codegen_fn_attrs_improvements, r=jdonszelmann Couple of codegen_fn_attrs improvements As noted in #144678 (comment) here is no need to keep link_name and export_name separate, which the third commit fixes by merging them. The second commit removes some dead code and the first commit merges two ifs with equivalent conditions. The last commit is an unrelated change which removes an unused `feature(autodiff)`.
2 parents 16ad385 + f94a0d0 commit e631ee5

File tree

9 files changed

+43
-63
lines changed

9 files changed

+43
-63
lines changed

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
99
#![doc(rust_logo)]
1010
#![feature(assert_matches)]
11-
#![feature(autodiff)]
1211
#![feature(box_patterns)]
1312
#![feature(decl_macro)]
1413
#![feature(if_let_guard)]

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
497497
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", module));
498498

499499
let name =
500-
codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
500+
codegen_fn_attrs.symbol_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
501501
let name = name.as_str();
502502
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
503503
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ pub fn is_call_from_compiler_builtins_to_upstream_monomorphization<'tcx>(
858858
instance: Instance<'tcx>,
859859
) -> bool {
860860
fn is_llvm_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
861-
if let Some(name) = tcx.codegen_fn_attrs(def_id).link_name {
861+
if let Some(name) = tcx.codegen_fn_attrs(def_id).symbol_name {
862862
name.as_str().starts_with("llvm.")
863863
} else {
864864
false

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
66
use rustc_hir::attrs::{AttributeKind, InlineAttr, InstructionSetAttr, UsedBy};
77
use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
9-
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
109
use rustc_hir::{self as hir, Attribute, LangItem, find_attr, lang_items};
1110
use rustc_middle::middle::codegen_fn_attrs::{
1211
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry,
@@ -156,15 +155,21 @@ fn process_builtin_attrs(
156155
match p {
157156
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
158157
AttributeKind::ExportName { name, .. } => {
159-
codegen_fn_attrs.export_name = Some(*name)
158+
codegen_fn_attrs.symbol_name = Some(*name)
160159
}
161160
AttributeKind::Inline(inline, span) => {
162161
codegen_fn_attrs.inline = *inline;
163162
interesting_spans.inline = Some(*span);
164163
}
165164
AttributeKind::Naked(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
166165
AttributeKind::Align { align, .. } => codegen_fn_attrs.alignment = Some(*align),
167-
AttributeKind::LinkName { name, .. } => codegen_fn_attrs.link_name = Some(*name),
166+
AttributeKind::LinkName { name, .. } => {
167+
// FIXME Remove check for foreign functions once #[link_name] on non-foreign
168+
// functions is a hard error
169+
if tcx.is_foreign_item(did) {
170+
codegen_fn_attrs.symbol_name = Some(*name);
171+
}
172+
}
168173
AttributeKind::LinkOrdinal { ordinal, span } => {
169174
codegen_fn_attrs.link_ordinal = Some(*ordinal);
170175
interesting_spans.link_ordinal = Some(*span);
@@ -382,7 +387,7 @@ fn apply_overrides(tcx: TyCtxt<'_>, did: LocalDefId, codegen_fn_attrs: &mut Code
382387
// * `#[rustc_std_internal_symbol]` mangles the symbol name in a special way
383388
// both for exports and imports through foreign items. This is handled further,
384389
// during symbol mangling logic.
385-
} else if codegen_fn_attrs.link_name.is_some() {
390+
} else if codegen_fn_attrs.symbol_name.is_some() {
386391
// * This can be overridden with the `#[link_name]` attribute
387392
} else {
388393
// NOTE: there's one more exception that we cannot apply here. On wasm,
@@ -437,7 +442,7 @@ fn check_result(
437442
}
438443

439444
// error when specifying link_name together with link_ordinal
440-
if let Some(_) = codegen_fn_attrs.link_name
445+
if let Some(_) = codegen_fn_attrs.symbol_name
441446
&& let Some(_) = codegen_fn_attrs.link_ordinal
442447
{
443448
let msg = "cannot use `#[link_name]` with `#[link_ordinal]`";
@@ -484,14 +489,11 @@ fn handle_lang_items(
484489
// strippable by the linker.
485490
//
486491
// Additionally weak lang items have predetermined symbol names.
487-
if let Some(lang_item) = lang_item {
488-
if WEAK_LANG_ITEMS.contains(&lang_item) {
489-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
490-
}
491-
if let Some(link_name) = lang_item.link_name() {
492-
codegen_fn_attrs.export_name = Some(link_name);
493-
codegen_fn_attrs.link_name = Some(link_name);
494-
}
492+
if let Some(lang_item) = lang_item
493+
&& let Some(link_name) = lang_item.link_name()
494+
{
495+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
496+
codegen_fn_attrs.symbol_name = Some(link_name);
495497
}
496498

497499
// error when using no_mangle on a lang item item

compiler/rustc_lint/src/foreign_modules.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl ClashingExternDeclarations {
179179
/// symbol's name.
180180
fn name_of_extern_decl(tcx: TyCtxt<'_>, fi: hir::OwnerId) -> SymbolName {
181181
if let Some((overridden_link_name, overridden_link_name_span)) =
182-
tcx.codegen_fn_attrs(fi).link_name.map(|overridden_link_name| {
182+
tcx.codegen_fn_attrs(fi).symbol_name.map(|overridden_link_name| {
183183
// FIXME: Instead of searching through the attributes again to get span
184184
// information, we could have codegen_fn_attrs also give span information back for
185185
// where the attribute was defined. However, until this is found to be a

compiler/rustc_metadata/src/native_libs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ impl<'tcx> Collector<'tcx> {
701701
.link_ordinal
702702
.map_or(import_name_type, |ord| Some(PeImportNameType::Ordinal(ord)));
703703

704-
let name = codegen_fn_attrs.link_name.unwrap_or_else(|| self.tcx.item_name(item));
704+
let name = codegen_fn_attrs.symbol_name.unwrap_or_else(|| self.tcx.item_name(item));
705705

706706
if self.tcx.sess.target.binary_format == BinaryFormat::Elf {
707707
let name = name.as_str();

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,10 @@ pub struct CodegenFnAttrs {
3535
pub inline: InlineAttr,
3636
/// Parsed representation of the `#[optimize]` attribute
3737
pub optimize: OptimizeAttr,
38-
/// The `#[export_name = "..."]` attribute, indicating a custom symbol a
39-
/// function should be exported under
40-
pub export_name: Option<Symbol>,
41-
/// The `#[link_name = "..."]` attribute, indicating a custom symbol an
42-
/// imported function should be imported as. Note that `export_name`
43-
/// probably isn't set when this is set, this is for foreign items while
44-
/// `#[export_name]` is for Rust-defined functions.
45-
pub link_name: Option<Symbol>,
38+
/// The name this function will be imported/exported under. This can be set
39+
/// using the `#[export_name = "..."]` or `#[link_name = "..."]` attribute
40+
/// depending on if this is a function definition or foreign function.
41+
pub symbol_name: Option<Symbol>,
4642
/// The `#[link_ordinal = "..."]` attribute, indicating an ordinal an
4743
/// imported function has in the dynamic library. Note that this must not
4844
/// be set when `link_name` is set. This is for foreign items with the
@@ -167,8 +163,7 @@ impl CodegenFnAttrs {
167163
flags: CodegenFnAttrFlags::empty(),
168164
inline: InlineAttr::None,
169165
optimize: OptimizeAttr::Default,
170-
export_name: None,
171-
link_name: None,
166+
symbol_name: None,
172167
link_ordinal: None,
173168
target_features: vec![],
174169
safe_target_features: false,
@@ -196,7 +191,7 @@ impl CodegenFnAttrs {
196191

197192
self.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
198193
|| self.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL)
199-
|| self.export_name.is_some()
194+
|| self.symbol_name.is_some()
200195
|| match self.linkage {
201196
// These are private, so make sure we don't try to consider
202197
// them external.

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -193,29 +193,20 @@ fn compute_symbol_name<'tcx>(
193193
// defining crate.
194194
// Weak lang items automatically get #[rustc_std_internal_symbol]
195195
// applied by the code computing the CodegenFnAttrs.
196-
// We are mangling all #[rustc_std_internal_symbol] items that don't
197-
// also have #[no_mangle] as a combination of the rustc version and the
198-
// unmangled linkage name. This is to ensure that if we link against a
199-
// staticlib compiled by a different rustc version, we don't get symbol
200-
// conflicts or even UB due to a different implementation/ABI. Rust
201-
// staticlibs currently export all symbols, including those that are
202-
// hidden in cdylibs.
196+
// We are mangling all #[rustc_std_internal_symbol] items as a
197+
// combination of the rustc version and the unmangled linkage name.
198+
// This is to ensure that if we link against a staticlib compiled by a
199+
// different rustc version, we don't get symbol conflicts or even UB
200+
// due to a different implementation/ABI. Rust staticlibs currently
201+
// export all symbols, including those that are hidden in cdylibs.
203202
// We are using the v0 symbol mangling scheme here as we need to be
204203
// consistent across all crates and in some contexts the legacy symbol
205204
// mangling scheme can't be used. For example both the GCC backend and
206205
// Rust-for-Linux don't support some of the characters used by the
207206
// legacy symbol mangling scheme.
208-
let name = if tcx.is_foreign_item(def_id) {
209-
if let Some(name) = attrs.link_name { name } else { tcx.item_name(def_id) }
210-
} else {
211-
if let Some(name) = attrs.export_name { name } else { tcx.item_name(def_id) }
212-
};
207+
let name = if let Some(name) = attrs.symbol_name { name } else { tcx.item_name(def_id) };
213208

214-
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
215-
return name.to_string();
216-
} else {
217-
return v0::mangle_internal_symbol(tcx, name.as_str());
218-
}
209+
return v0::mangle_internal_symbol(tcx, name.as_str());
219210
}
220211

221212
let wasm_import_module_exception_force_mangling = {
@@ -240,23 +231,16 @@ fn compute_symbol_name<'tcx>(
240231
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&def_id.into())
241232
};
242233

243-
if let Some(name) = attrs.link_name
244-
&& !wasm_import_module_exception_force_mangling
245-
{
246-
// Use provided name
247-
return name.to_string();
248-
}
249-
250-
if let Some(name) = attrs.export_name {
251-
// Use provided name
252-
return name.to_string();
253-
}
234+
if !wasm_import_module_exception_force_mangling {
235+
if let Some(name) = attrs.symbol_name {
236+
// Use provided name
237+
return name.to_string();
238+
}
254239

255-
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
256-
&& !wasm_import_module_exception_force_mangling
257-
{
258-
// Don't mangle
259-
return tcx.item_name(def_id).to_string();
240+
if attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE) {
241+
// Don't mangle
242+
return tcx.item_name(def_id).to_string();
243+
}
260244
}
261245

262246
// If we're dealing with an instance of a function that's inlined from

src/tools/miri/src/shims/foreign_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
146146
return interp_ok(());
147147
}
148148
// Skip over items without an explicitly defined symbol name.
149-
if !(attrs.export_name.is_some()
149+
if !(attrs.symbol_name.is_some()
150150
|| attrs.flags.contains(CodegenFnAttrFlags::NO_MANGLE)
151151
|| attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL))
152152
{

0 commit comments

Comments
 (0)