Skip to content

Rollup of 8 pull requests #133697

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
45791dd
Support linker arguments that contain commas
madsmtm Nov 24, 2024
cb6f8fa
Support rpath with -Clinker-flavor=ld
madsmtm Nov 12, 2024
6bbf832
Remove unnecessary 0 link args optimization
madsmtm Nov 24, 2024
7cf3f8b
Do not emit `missing_doc_code_examples` rustdoc lint on module and a …
GuillaumeGomez Nov 28, 2024
9142cae
add `LinkageInfo` to keep track of how we figured out the linkage
folkertdev Aug 8, 2024
bdf64e1
squashed changes to inlining and const eval
folkertdev Jul 19, 2024
d67a5c0
test the new global asm output of naked functions
folkertdev Jul 19, 2024
a93192c
add `InstructionSetAttr::as_str` and make the type `Copy`
folkertdev Jul 20, 2024
49a297a
squashed changes to tests
folkertdev Jul 20, 2024
23d9741
move slice::swap_unchecked constness to slice_swap_unchecked feature …
RalfJung Nov 30, 2024
ede5f01
move swap_nonoverlapping constness to separate feature gate
RalfJung Nov 30, 2024
2a05e5b
add test for bytewise ptr::swap of a pointer
RalfJung Nov 30, 2024
9836196
Fix chaining `carrying_add`s
scottmcm Nov 30, 2024
589ebb8
make naked functions always have external linkage *in LLVM*. If we do…
folkertdev Jul 26, 2024
2e24cdb
squashed changes:
folkertdev Aug 11, 2024
b118d05
Extend documentation for `missing_doc_code_examples` rustdoc lint in …
GuillaumeGomez Nov 30, 2024
64420b6
skip `predefine_fn` for naked functions
folkertdev Nov 30, 2024
cf738f6
Revert "add `LinkageInfo` to keep track of how we figured out the lin…
folkertdev Nov 30, 2024
805649b
Check let source before suggesting annotation
compiler-errors Dec 1, 2024
17c6efa
Disentangle hir node match logic in adjust_fulfillment_errors
compiler-errors Nov 24, 2024
30afeb0
Adjust HostEffect error spans correctly to point at args
compiler-errors Nov 24, 2024
d5c5d58
Pull out expr handling
compiler-errors Dec 1, 2024
b87e935
Revert "Reject raw lifetime followed by \' as well"
compiler-errors Nov 26, 2024
d878fd8
Only error raw lifetime followed by \' in edition 2021+
compiler-errors Nov 26, 2024
d3028b5
Rollup merge of #128004 - folkertdev:naked-fn-asm, r=Amanieu
matthiaskrgr Dec 1, 2024
22632f3
Rollup merge of #132974 - madsmtm:linker-arguments-with-commas, r=pet…
matthiaskrgr Dec 1, 2024
4590eec
Rollup merge of #133403 - compiler-errors:adjust-host-effect-preds, r…
matthiaskrgr Dec 1, 2024
9f7755e
Rollup merge of #133482 - compiler-errors:raw-lt-tick, r=estebank
matthiaskrgr Dec 1, 2024
8b898d1
Rollup merge of #133595 - GuillaumeGomez:missing_doc_code_examples, r…
matthiaskrgr Dec 1, 2024
c3e24bc
Rollup merge of #133669 - RalfJung:const_swap_splitup, r=dtolnay
matthiaskrgr Dec 1, 2024
31e6e6c
Rollup merge of #133674 - scottmcm:chain-carrying-add, r=Amanieu
matthiaskrgr Dec 1, 2024
6f9f594
Rollup merge of #133691 - compiler-errors:let-source, r=lqd
matthiaskrgr Dec 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion compiler/rustc_attr/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,21 @@ pub enum InlineAttr {
Never,
}

#[derive(Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic)]
#[derive(Copy, Clone, Encodable, Decodable, Debug, PartialEq, Eq, HashStable_Generic)]
pub enum InstructionSetAttr {
ArmA32,
ArmT32,
}

impl InstructionSetAttr {
pub fn as_str(self) -> &'static str {
match self {
Self::ArmA32 => sym::a32.as_str(),
Self::ArmT32 => sym::t32.as_str(),
}
}
}

#[derive(Clone, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum OptimizeAttr {
None,
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_codegen_gcc/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,13 @@ impl<'gcc, 'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
template_str.push_str("\n.popsection");
self.context.add_top_level_asm(None, &template_str);
}

fn mangled_name(&self, instance: Instance<'tcx>) -> String {
// TODO(@Amanieu): Additional mangling is needed on
// some targets to add a leading underscore (Mach-O)
// or byte count suffixes (x86 Windows).
self.tcx.symbol_name(instance).name.to_string()
}
}

fn modifier_to_gcc(
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ impl<'tcx> AsmCodegenMethods<'tcx> for CodegenCx<'_, 'tcx> {
);
}
}

fn mangled_name(&self, instance: Instance<'tcx>) -> String {
let llval = self.get_fn(instance);
llvm::build_string(|s| unsafe {
llvm::LLVMRustGetMangledName(llval, s);
})
.expect("symbol is not valid UTF-8")
}
}

pub(crate) fn inline_asm_call<'ll>(
Expand Down
14 changes: 3 additions & 11 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,17 +395,9 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
to_add.push(MemoryEffects::None.create_attr(cx.llcx));
}
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
to_add.push(AttributeKind::Naked.create_attr(cx.llcx));
// HACK(jubilee): "indirect branch tracking" works by attaching prologues to functions.
// And it is a module-level attribute, so the alternative is pulling naked functions into
// new LLVM modules. Otherwise LLVM's "naked" functions come with endbr prefixes per
// https://github.com/rust-lang/rust/issues/98768
to_add.push(AttributeKind::NoCfCheck.create_attr(cx.llcx));
if llvm_util::get_version() < (19, 0, 0) {
// Prior to LLVM 19, branch-target-enforcement was disabled by setting the attribute to
// the string "false". Now it is disabled by absence of the attribute.
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "branch-target-enforcement", "false"));
}
// do nothing; a naked function is converted into an extern function
// and a global assembly block. LLVM's support for naked functions is
// not used.
} else {
// Do not set sanitizer attributes for naked functions.
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ fn link_sanitizer_runtime(
let filename = format!("rustc{channel}_rt.{name}");
let path = find_sanitizer_runtime(sess, &filename);
let rpath = path.to_str().expect("non-utf8 component in path");
linker.cc_args(&["-Wl,-rpath", "-Xlinker", rpath]);
linker.link_args(&["-rpath", rpath]);
linker.link_dylib_by_name(&filename, false, true);
} else if sess.target.is_like_msvc && flavor == LinkerFlavor::Msvc(Lld::No) && name == "asan" {
// MSVC provides the `/INFERASANLIBS` argument to automatically find the
Expand Down Expand Up @@ -2210,7 +2210,7 @@ fn add_rpath_args(
is_like_osx: sess.target.is_like_osx,
linker_is_gnu: sess.target.linker_flavor.is_gnu(),
};
cmd.cc_args(&rpath::get_rpath_flags(&rpath_config));
cmd.link_args(&rpath::get_rpath_linker_args(&rpath_config));
}
}

Expand Down
50 changes: 36 additions & 14 deletions compiler/rustc_codegen_ssa/src/back/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ use super::command::Command;
use super::symbol_export;
use crate::errors;

#[cfg(test)]
mod tests;

/// Disables non-English messages from localized linkers.
/// Such messages may cause issues with text encoding on Windows (#35785)
/// and prevent inspection of linker output in case of errors, which we occasionally do.
Expand Down Expand Up @@ -178,23 +181,42 @@ fn verbatim_args<L: Linker + ?Sized>(
}
l
}
/// Add underlying linker arguments to C compiler command, by wrapping them in
/// `-Wl` or `-Xlinker`.
fn convert_link_args_to_cc_args(cmd: &mut Command, args: impl IntoIterator<Item: AsRef<OsStr>>) {
let mut combined_arg = OsString::from("-Wl");
for arg in args {
// If the argument itself contains a comma, we need to emit it
// as `-Xlinker`, otherwise we can use `-Wl`.
if arg.as_ref().as_encoded_bytes().contains(&b',') {
// Emit current `-Wl` argument, if any has been built.
if combined_arg != OsStr::new("-Wl") {
cmd.arg(combined_arg);
// Begin next `-Wl` argument.
combined_arg = OsString::from("-Wl");
}

// Emit `-Xlinker` argument.
cmd.arg("-Xlinker");
cmd.arg(arg);
} else {
// Append to `-Wl` argument.
combined_arg.push(",");
combined_arg.push(arg);
}
}
// Emit final `-Wl` argument.
if combined_arg != OsStr::new("-Wl") {
cmd.arg(combined_arg);
}
}
/// Arguments for the underlying linker.
/// Add options to pass them through cc wrapper if `Linker` is a cc wrapper.
fn link_args<L: Linker + ?Sized>(
l: &mut L,
args: impl IntoIterator<Item: AsRef<OsStr>, IntoIter: ExactSizeIterator>,
) -> &mut L {
let args = args.into_iter();
fn link_args<L: Linker + ?Sized>(l: &mut L, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut L {
if !l.is_cc() {
verbatim_args(l, args);
} else if args.len() != 0 {
// FIXME: Support arguments with commas, see `rpaths_to_flags` for the example.
let mut combined_arg = OsString::from("-Wl");
for arg in args {
combined_arg.push(",");
combined_arg.push(arg);
}
l.cmd().arg(combined_arg);
} else {
convert_link_args_to_cc_args(l.cmd(), args);
}
l
}
Expand Down Expand Up @@ -224,7 +246,7 @@ macro_rules! generate_arg_methods {
verbatim_args(self, iter::once(arg))
}
#[allow(unused)]
pub(crate) fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>, IntoIter: ExactSizeIterator>) -> &mut Self {
pub(crate) fn link_args(&mut self, args: impl IntoIterator<Item: AsRef<OsStr>>) -> &mut Self {
link_args(self, args)
}
#[allow(unused)]
Expand Down
32 changes: 32 additions & 0 deletions compiler/rustc_codegen_ssa/src/back/linker/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use super::*;

#[test]
fn test_rpaths_to_args() {
let mut cmd = Command::new("foo");
convert_link_args_to_cc_args(&mut cmd, &["-rpath", "path1", "-rpath", "path2"]);
assert_eq!(cmd.get_args(), [OsStr::new("-Wl,-rpath,path1,-rpath,path2")]);
}

#[test]
fn test_xlinker() {
let mut cmd = Command::new("foo");
convert_link_args_to_cc_args(&mut cmd, &[
"arg1",
"arg2",
"arg3,with,comma",
"arg4,with,comma",
"arg5",
"arg6,with,comma",
]);

assert_eq!(cmd.get_args(), [
OsStr::new("-Wl,arg1,arg2"),
OsStr::new("-Xlinker"),
OsStr::new("arg3,with,comma"),
OsStr::new("-Xlinker"),
OsStr::new("arg4,with,comma"),
OsStr::new("-Wl,arg5"),
OsStr::new("-Xlinker"),
OsStr::new("arg6,with,comma"),
]);
}
34 changes: 11 additions & 23 deletions compiler/rustc_codegen_ssa/src/back/rpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,27 @@ pub(super) struct RPathConfig<'a> {
pub linker_is_gnu: bool,
}

pub(super) fn get_rpath_flags(config: &RPathConfig<'_>) -> Vec<OsString> {
pub(super) fn get_rpath_linker_args(config: &RPathConfig<'_>) -> Vec<OsString> {
debug!("preparing the RPATH!");

let rpaths = get_rpaths(config);
let mut flags = rpaths_to_flags(rpaths);
let mut args = Vec::with_capacity(rpaths.len() * 2); // the minimum needed capacity

for rpath in rpaths {
args.push("-rpath".into());
args.push(rpath);
}

if config.linker_is_gnu {
// Use DT_RUNPATH instead of DT_RPATH if available
flags.push("-Wl,--enable-new-dtags".into());
args.push("--enable-new-dtags".into());

// Set DF_ORIGIN for substitute $ORIGIN
flags.push("-Wl,-z,origin".into());
}

flags
}

fn rpaths_to_flags(rpaths: Vec<OsString>) -> Vec<OsString> {
let mut ret = Vec::with_capacity(rpaths.len()); // the minimum needed capacity

for rpath in rpaths {
if rpath.to_string_lossy().contains(',') {
ret.push("-Wl,-rpath".into());
ret.push("-Xlinker".into());
ret.push(rpath);
} else {
let mut single_arg = OsString::from("-Wl,-rpath,");
single_arg.push(rpath);
ret.push(single_arg);
}
args.push("-z".into());
args.push("origin".into());
}

ret
args
}

fn get_rpaths(config: &RPathConfig<'_>) -> Vec<OsString> {
Expand Down
23 changes: 1 addition & 22 deletions compiler/rustc_codegen_ssa/src/back/rpath/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
use std::ffi::OsString;
use std::path::{Path, PathBuf};

use super::{RPathConfig, get_rpath_relative_to_output, minimize_rpaths, rpaths_to_flags};

#[test]
fn test_rpaths_to_flags() {
let flags = rpaths_to_flags(vec!["path1".into(), "path2".into()]);
assert_eq!(flags, ["-Wl,-rpath,path1", "-Wl,-rpath,path2"]);
}
use super::*;

#[test]
fn test_minimize1() {
Expand Down Expand Up @@ -69,15 +60,3 @@ fn test_rpath_relative_issue_119571() {
// Should not panic when lib only contains filename.
let _ = get_rpath_relative_to_output(config, Path::new("libstd.so"));
}

#[test]
fn test_xlinker() {
let args = rpaths_to_flags(vec!["a/normal/path".into(), "a,comma,path".into()]);

assert_eq!(args, vec![
OsString::from("-Wl,-rpath,a/normal/path"),
OsString::from("-Wl,-rpath"),
OsString::from("-Xlinker"),
OsString::from("a,comma,path")
]);
}
11 changes: 7 additions & 4 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
}
});

// naked function MUST NOT be inlined! This attribute is required for the rust compiler itself,
// but not for the code generation backend because at that point the naked function will just be
// a declaration, with a definition provided in global assembly.
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
codegen_fn_attrs.inline = InlineAttr::Never;
}

codegen_fn_attrs.optimize = attrs.iter().fold(OptimizeAttr::None, |ia, attr| {
if !attr.has_name(sym::optimize) {
return ia;
Expand Down Expand Up @@ -626,10 +633,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
}
}

if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
codegen_fn_attrs.inline = InlineAttr::Never;
}

// Weak lang items have the same semantics as "std internal" symbols in the
// sense that they're preserved through all our LTO passes and only
// strippable by the linker.
Expand Down
6 changes: 6 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod coverageinfo;
pub mod debuginfo;
mod intrinsic;
mod locals;
mod naked_asm;
pub mod operand;
pub mod place;
mod rvalue;
Expand Down Expand Up @@ -176,6 +177,11 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
debug!("fn_abi: {:?}", fn_abi);

if cx.tcx().codegen_fn_attrs(instance.def_id()).flags.contains(CodegenFnAttrFlags::NAKED) {
crate::mir::naked_asm::codegen_naked_asm::<Bx>(cx, &mir, instance);
return;
}

let debug_context = cx.create_function_debug_context(instance, fn_abi, llfn, mir);

let start_llbb = Bx::append_block(cx, llfn, "start");
Expand Down
Loading
Loading