Skip to content

Commit 29a5d06

Browse files
committed
Auto merge of #165856 - JonathanBrouwer:revert18, r=JonathanBrouwer
Revert "fix incorrect byte count suffix on MSVC" Reverts #165458 because of a performance regression.
2 parents b4fe65c + 0670d02 commit 29a5d06

12 files changed

Lines changed: 45 additions & 234 deletions

File tree

‎compiler/rustc_codegen_llvm/src/declare.rs‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,6 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
169169
instance: Option<Instance<'tcx>>,
170170
) -> Value<'ll> {
171171
debug!("declare_rust_fn(name={:?}, fn_abi={:?})", name, fn_abi);
172-
// Some calling conventions (such as fastcall and vectorcall) decorate
173-
// symbols with the argument byte count. Use rustc's original argument
174-
// layouts and prefix the decorated name with `\x01` so LLVM does not
175-
// decorate it a second time.
176-
let decorated_name = rustc_codegen_ssa::common::windows_calling_convention_name(
177-
&self.tcx.sess.target,
178-
fn_abi.conv,
179-
name,
180-
&fn_abi.args,
181-
)
182-
.map(|name| format!("\x01{name}"));
183-
184-
let name = decorated_name.as_deref().unwrap_or(name);
185172

186173
// Function addresses in Rust are never significant, allowing functions to
187174
// be merged.

‎compiler/rustc_codegen_ssa/src/back/lto.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_session::config::Lto;
1212
use rustc_structures::CrateType;
1313
use tracing::info;
1414

15-
use crate::back::symbol_export::{self, allocator_shim_symbols};
15+
use crate::back::symbol_export::{self, allocator_shim_symbols, symbol_name_for_instance_in_crate};
1616
use crate::back::write::CodegenContext;
1717
use crate::base::allocator_kind_for_codegen;
1818
use crate::diagnostics::{DynamicLinkingWithLTO, LtoDisallowed, LtoDylib, LtoProcMacro};
@@ -104,11 +104,11 @@ pub(crate) fn exported_symbols_for_lto(
104104
tcx.exported_non_generic_symbols(cnum)
105105
.iter()
106106
.chain(tcx.exported_generic_symbols(cnum))
107-
.flat_map(|&(s, info): &(ExportedSymbol<'_>, SymbolExportInfo)| {
107+
.filter_map(|&(s, info): &(ExportedSymbol<'_>, SymbolExportInfo)| {
108108
if info.level.is_below_threshold(export_threshold) || info.used {
109-
symbol_export::symbol_names_for_lto(tcx, s, cnum)
109+
Some(symbol_name_for_instance_in_crate(tcx, s, cnum))
110110
} else {
111-
vec![]
111+
None
112112
}
113113
})
114114
.collect::<Vec<_>>()

‎compiler/rustc_codegen_ssa/src/back/symbol_export.rs‎

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::collections::hash_map::Entry::*;
22

33
use object::pe;
4-
use rustc_abi::CanonAbi;
4+
use rustc_abi::{CanonAbi, X86Call};
55
use rustc_ast::expand::allocator::{AllocatorKind, NO_ALLOC_SHIM_IS_UNSTABLE, global_fn_name};
66
use rustc_crate_store::CrateDepKind;
77
use rustc_data_structures::fx::FxHashSet;
@@ -758,31 +758,9 @@ fn calling_convention_for_symbol<'tcx>(
758758
.unwrap_or((CanonAbi::Rust, &[]))
759759
}
760760

761-
/// Names used to preserve an exported symbol during LTO.
762-
///
763-
/// The restriction pass matches LLVM IR names, which retain the `\x01`
764-
/// marker for explicitly decorated symbols. ThinLTO GUIDs instead use
765-
/// the identifier without that marker, so preserve both spellings.
766-
pub(crate) fn symbol_names_for_lto<'tcx>(
767-
tcx: TyCtxt<'tcx>,
768-
symbol: ExportedSymbol<'tcx>,
769-
instantiating_crate: CrateNum,
770-
) -> Vec<String> {
771-
let name = symbol_name_for_instance_in_crate(tcx, symbol, instantiating_crate);
772-
let (callconv, args) = calling_convention_for_symbol(tcx, symbol);
773-
774-
if let Some(decorated) =
775-
crate::common::windows_calling_convention_name(&tcx.sess.target, callconv, &name, args)
776-
{
777-
vec![format!("\x01{decorated}"), decorated]
778-
} else {
779-
vec![name]
780-
}
781-
}
782761
/// This is the symbol name of the given instance as seen by the linker.
783762
///
784-
/// On Windows, symbols are decorated according to their calling conventions.
785-
/// Vectorcall symbols are also decorated on non-Windows targets.
763+
/// On 32-bit Windows symbols are decorated according to their calling conventions.
786764
pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
787765
tcx: TyCtxt<'tcx>,
788766
symbol: ExportedSymbol<'tcx>,
@@ -799,15 +777,8 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
799777

800778
let target = &tcx.sess.target;
801779
if !target.is_like_windows {
802-
let (callconv, args) = calling_convention_for_symbol(tcx, symbol);
803-
if let Some(name) =
804-
crate::common::windows_calling_convention_name(target, callconv, &undecorated, args)
805-
{
806-
return name;
807-
}
808-
809780
// Mach-O has a global "_" suffix and `object` crate will handle it.
810-
// Other calling conventions do not need decoration here.
781+
// ELF does not have any symbol decorations.
811782
return undecorated;
812783
}
813784

@@ -820,26 +791,27 @@ pub(crate) fn linking_symbol_name_for_instance_in_crate<'tcx>(
820791
_ => return undecorated,
821792
};
822793

823-
// LLVM treats names beginning with '\x01' as already mangled and strips that prefix.
824-
// Names beginning with '?' are left undecorated by LLVM; the prefix is kept.
825-
if let Some(name) = undecorated.strip_prefix('\x01') {
826-
return name.to_string();
827-
}
828-
if undecorated.starts_with('?') {
829-
return undecorated;
830-
}
831794
let (callconv, args) = calling_convention_for_symbol(tcx, symbol);
832795

833-
if let Some(name) =
834-
crate::common::windows_calling_convention_name(target, callconv, &undecorated, args)
835-
{
836-
return name;
837-
}
796+
// Decorate symbols with prefixes, suffixes and total number of bytes of arguments.
797+
// Reference: https://docs.microsoft.com/en-us/cpp/build/reference/decorated-names?view=msvc-170
798+
let (prefix, suffix) = match callconv {
799+
CanonAbi::X86(X86Call::Fastcall) => ("@", "@"),
800+
CanonAbi::X86(X86Call::Stdcall) => ("_", "@"),
801+
CanonAbi::X86(X86Call::Vectorcall) => ("", "@@"),
802+
_ => {
803+
if let Some(prefix) = prefix {
804+
undecorated.insert(0, prefix);
805+
}
806+
return undecorated;
807+
}
808+
};
838809

839-
if let Some(prefix) = prefix {
840-
undecorated.insert(0, prefix);
841-
}
842-
undecorated
810+
let args_in_bytes: u64 = args
811+
.iter()
812+
.map(|abi| abi.layout.size.bytes().next_multiple_of(target.pointer_width as u64 / 8))
813+
.sum();
814+
format!("{prefix}{undecorated}{suffix}{args_in_bytes}")
843815
}
844816

845817
pub(crate) fn exporting_symbol_name_for_instance_in_crate<'tcx>(

‎compiler/rustc_codegen_ssa/src/common.rs‎

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#![allow(non_camel_case_types)]
22

3-
use rustc_abi::{CanonAbi, X86Call};
43
use rustc_attr_ir::PeImportNameType;
54
use rustc_attr_ir::lang_items::LangItem;
65
use rustc_crate_store::{DllCallingConvention, DllImport, DllImportSymbolType};
76
use rustc_middle::mir::interpret::{GlobalAlloc, PointerArithmetic, Scalar};
87
use rustc_middle::ty::layout::TyAndLayout;
9-
use rustc_middle::ty::{self, Instance, ScalarInt, Ty, TyCtxt};
8+
use rustc_middle::ty::{self, Instance, ScalarInt, TyCtxt};
109
use rustc_span::{Span, bug, span_bug};
11-
use rustc_target::callconv::ArgAbi;
12-
use rustc_target::spec::{Arch, CfgAbi, Env, Os, Target};
10+
use rustc_target::spec::{CfgAbi, Env, Os, Target};
1311

1412
use crate::traits::*;
1513

@@ -176,55 +174,6 @@ pub fn is_using_dlltool(target: &Target) -> bool {
176174
target.os == Os::Windows && target.env == Env::Gnu && target.cfg_abi == CfgAbi::Unspecified
177175
}
178176

179-
/// Computes the total argument size used for Windows calling-convention
180-
/// symbol decoration. Each argument is rounded up to the pointer width.
181-
fn windows_abi_argument_byte_sum<'tcx>(args: &[ArgAbi<'tcx, Ty<'tcx>>], target: &Target) -> u64 {
182-
let pointer_bytes = target.pointer_width as u64 / 8;
183-
184-
args.iter().map(|arg| arg.layout.size.bytes().next_multiple_of(pointer_bytes)).sum()
185-
}
186-
187-
/// Computes the linker-visible decorated symbol name for supported Windows
188-
/// calling conventions using the original `FnAbi` argument layouts.
189-
/// Vectorcall is also decorated on non-Windows targets.
190-
///
191-
/// The byte count is computed before backend-specific argument lowering,
192-
/// which may replace indirectly passed arguments with pointers.
193-
///
194-
/// For example, on 32-bit Windows, `extern "fastcall" fn foo(i32, i32)`
195-
/// is decorated as `@foo@8`. On 64-bit targets,
196-
/// `extern "vectorcall" fn foo(i32)` is decorated as `foo@@8`.
197-
///
198-
/// Returns `None` if no decoration is required or if the name begins with
199-
/// `\x01` or `?`, indicating that it must not be decorated further.
200-
///
201-
/// This function does not add LLVM's `\x01` escape prefix. Backend-specific
202-
/// handling of that marker is the responsibility of the caller.
203-
pub fn windows_calling_convention_name<'tcx>(
204-
target: &Target,
205-
conv: CanonAbi,
206-
name: &str,
207-
args: &[ArgAbi<'tcx, Ty<'tcx>>],
208-
) -> Option<String> {
209-
if !target.is_like_windows && conv != CanonAbi::X86(X86Call::Vectorcall) {
210-
return None;
211-
}
212-
213-
if name.starts_with('\x01') || name.starts_with('?') {
214-
return None;
215-
}
216-
217-
let (prefix, suffix) = match (&target.arch, conv) {
218-
(Arch::X86, CanonAbi::X86(X86Call::Fastcall)) => ("@", "@"),
219-
(Arch::X86, CanonAbi::X86(X86Call::Stdcall)) => ("_", "@"),
220-
(Arch::X86 | Arch::X86_64, CanonAbi::X86(X86Call::Vectorcall)) => ("", "@@"),
221-
_ => return None,
222-
};
223-
224-
let args_in_bytes = windows_abi_argument_byte_sum(args, target);
225-
Some(format!("{prefix}{name}{suffix}{args_in_bytes}"))
226-
}
227-
228177
pub fn i686_decorated_name(
229178
dll_import: &DllImport,
230179
mingw: bool,

‎tests/assembly-llvm/win32-byte-count-suffix.rs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ extern "vectorcall" fn eight_vectors_vectorcall(
249249
) {
250250
}
251251

252-
// MSVC passes this argument indirectly, but the decoration uses the
253-
// original 8-byte type size rather than the 4-byte pointer size.
252+
/// FIXME: the mangled name for this type is incorrect on msvc, where the argument is passed
253+
/// indirectly and a pointer takes up 4 bytes, but clang counts the size of the type and uses
254+
/// @8 there. With GNU the value is passed directly, so the suffix is correct.
254255
#[repr(C, align(8))]
255256
struct OverAligned(u32, u32);
256257

@@ -259,16 +260,16 @@ struct OverAligned(u32, u32);
259260
extern "C" fn overaligned(_: OverAligned) {}
260261

261262
// windows-gnu-LABEL: _overaligned_stdcall@8:
262-
// windows-msvc-LABEL: _overaligned_stdcall@8:
263+
// windows-msvc-LABEL: _overaligned_stdcall@4:
263264
#[unsafe(no_mangle)]
264265
extern "stdcall" fn overaligned_stdcall(_: OverAligned) {}
265266

266267
// windows-gnu-LABEL: @overaligned_fastcall@8:
267-
// windows-msvc-LABEL: @overaligned_fastcall@8:
268+
// windows-msvc-LABEL: @overaligned_fastcall@4:
268269
#[unsafe(no_mangle)]
269270
extern "fastcall" fn overaligned_fastcall(_: OverAligned) {}
270271

271272
// windows-gnu-LABEL: overaligned_vectorcall@@8:
272-
// windows-msvc-LABEL: overaligned_vectorcall@@8:
273+
// windows-msvc-LABEL: overaligned_vectorcall@@4:
273274
#[unsafe(no_mangle)]
274275
extern "vectorcall" fn overaligned_vectorcall(_: OverAligned) {}

‎tests/codegen-llvm/abi-x86_64-win-zst.rs‎

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ extern "win64" fn pass_c_maybezst_win64(_: CMaybeZst) {}
4343
#[no_mangle]
4444
extern "win64" fn pass_c_maybezst_2_win64(_: CMaybeZst2) {}
4545

46-
// CHECK: define x86_vectorcallcc void @"\01pass_rust_zst_vectorcall@@0"(
46+
// CHECK: define x86_vectorcallcc void @pass_rust_zst_vectorcall()
4747
#[no_mangle]
4848
extern "vectorcall" fn pass_rust_zst_vectorcall(_: ()) {}
4949

50-
// CHECK: define x86_vectorcallcc void @"\01pass_c_maybezst_vectorcall@@0"(ptr {{[^,]*}})
50+
// CHECK: define x86_vectorcallcc void @pass_c_maybezst_vectorcall(ptr {{[^,]*}})
5151
#[no_mangle]
5252
extern "vectorcall" fn pass_c_maybezst_vectorcall(_: CMaybeZst) {}
5353

54-
// CHECK: define x86_vectorcallcc void @"\01pass_c_maybezst_2_vectorcall@@0"(ptr {{[^,]*}})
54+
// CHECK: define x86_vectorcallcc void @pass_c_maybezst_2_vectorcall(ptr {{[^,]*}})
5555
#[no_mangle]
5656
extern "vectorcall" fn pass_c_maybezst_2_vectorcall(_: CMaybeZst2) {}
5757

@@ -73,21 +73,6 @@ extern "fastcall" fn pass_c_maybezst_fastcall(_: CMaybeZst) {}
7373
#[cfg(windows)] // "fastcall" is not valid on 64bit Linux
7474
extern "fastcall" fn pass_c_maybezst_2_fastcall(_: CMaybeZst2) {}
7575

76-
// Keep the original argument size when a vectorcall argument is passed
77-
// indirectly. This also exercises vectorcall decoration on Linux.
78-
#[repr(C)]
79-
pub struct Big {
80-
pub data: [u64; 4],
81-
}
82-
83-
// CHECK: define x86_vectorcallcc void @"\01vc_big@@32"(
84-
#[unsafe(export_name = "vc_big")]
85-
pub extern "vectorcall" fn vc_big(_: Big) {}
86-
87-
// CHECK: define x86_vectorcallcc void @"\01vc_u64@@8"(
88-
#[unsafe(export_name = "vc_u64")]
89-
pub extern "vectorcall" fn vc_u64(_: u64) {}
90-
9176
// The sysv64 ABI ignores ZST.
9277

9378
// CHECK: define x86_64_sysvcc void @pass_rust_zst_sysv64()
@@ -131,7 +116,7 @@ extern "win64" fn ret_c_maybezst_win64() -> CMaybeZst {
131116
CMaybeZst
132117
}
133118

134-
// CHECK: define x86_vectorcallcc void @"\01ret_c_maybezst_vectorcall@@0"(ptr {{[^,]*}})
119+
// CHECK: define x86_vectorcallcc void @ret_c_maybezst_vectorcall(ptr {{[^,]*}})
135120
#[no_mangle]
136121
extern "vectorcall" fn ret_c_maybezst_vectorcall() -> CMaybeZst {
137122
CMaybeZst

‎tests/codegen-llvm/msvc-x86-callconv-byte-count.rs‎

Lines changed: 0 additions & 46 deletions
This file was deleted.

‎tests/codegen-llvm/unwind-abis/fastcall-unwind-abi.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub trait Sized: MetaSized {}
1616
// `fastcall-unwind` extern functions. `fastcall-unwind` functions MUST NOT have this attribute. We
1717
// disable optimizations above to prevent LLVM from inferring the attribute.
1818

19-
// CHECK: @"\01@rust_item_that_cannot_unwind@0"() unnamed_addr #0
19+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0
2020
#[no_mangle]
2121
pub extern "fastcall" fn rust_item_that_cannot_unwind() {}
2222

23-
// CHECK: @"\01@rust_item_that_can_unwind@0"() unnamed_addr #1
23+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1
2424
#[no_mangle]
2525
pub extern "fastcall-unwind" fn rust_item_that_can_unwind() {}
2626

‎tests/codegen-llvm/unwind-abis/stdcall-unwind-abi.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub trait Sized: MetaSized {}
1616
// extern functions. `stdcall-unwind` functions MUST NOT have this attribute. We disable
1717
// optimizations above to prevent LLVM from inferring the attribute.
1818

19-
// CHECK: @"\01_rust_item_that_cannot_unwind@0"() unnamed_addr #0
19+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0
2020
#[no_mangle]
2121
pub extern "stdcall" fn rust_item_that_cannot_unwind() {}
2222

23-
// CHECK: @"\01_rust_item_that_can_unwind@0"() unnamed_addr #1
23+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1
2424
#[no_mangle]
2525
pub extern "stdcall-unwind" fn rust_item_that_can_unwind() {}
2626

‎tests/codegen-llvm/unwind-abis/system-unwind-abi.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
#![crate_type = "lib"]
99

10-
// CHECK: @{{.*}}rust_item_that_cannot_unwind{{.*}}() unnamed_addr #0
10+
// CHECK: @rust_item_that_cannot_unwind() unnamed_addr #0
1111
#[no_mangle]
1212
pub extern "system" fn rust_item_that_cannot_unwind() {}
1313

14-
// CHECK: @{{.*}}rust_item_that_can_unwind{{.*}}() unnamed_addr #1
14+
// CHECK: @rust_item_that_can_unwind() unnamed_addr #1
1515
#[no_mangle]
1616
pub extern "system-unwind" fn rust_item_that_can_unwind() {}
1717

0 commit comments

Comments
 (0)