Skip to content

Commit 31719b5

Browse files
authored
Rollup merge of #136439 - yotamofek:pr/codegen-ssa-no-indexing, r=Noratrieb
Misc. `rustc_codegen_ssa` cleanups 🧹 Just a bunch of stuff I found while reading the crate's code. Each commit can stand on its own. Maybe r? `@Noratrieb` because I saw you did some similar cleanups on these files a while ago? (feel free to re-assign, I'm just guessing)
2 parents b522e7c + 73d5fe1 commit 31719b5

File tree

4 files changed

+95
-110
lines changed

4 files changed

+95
-110
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+13-19
Original file line numberDiff line numberDiff line change
@@ -244,22 +244,17 @@ pub fn each_linked_rlib(
244244

245245
fmts
246246
} else {
247-
for combination in info.dependency_formats.iter().combinations(2) {
248-
let (ty1, list1) = &combination[0];
249-
let (ty2, list2) = &combination[1];
250-
if list1 != list2 {
251-
return Err(errors::LinkRlibError::IncompatibleDependencyFormats {
252-
ty1: format!("{ty1:?}"),
253-
ty2: format!("{ty2:?}"),
254-
list1: format!("{list1:?}"),
255-
list2: format!("{list2:?}"),
256-
});
257-
}
258-
}
259-
if info.dependency_formats.is_empty() {
260-
return Err(errors::LinkRlibError::MissingFormat);
247+
let mut dep_formats = info.dependency_formats.iter();
248+
let (ty1, list1) = dep_formats.next().ok_or(errors::LinkRlibError::MissingFormat)?;
249+
if let Some((ty2, list2)) = dep_formats.find(|(_, list2)| list1 != *list2) {
250+
return Err(errors::LinkRlibError::IncompatibleDependencyFormats {
251+
ty1: format!("{ty1:?}"),
252+
ty2: format!("{ty2:?}"),
253+
list1: format!("{list1:?}"),
254+
list2: format!("{list2:?}"),
255+
});
261256
}
262-
info.dependency_formats.first().unwrap().1
257+
list1
263258
};
264259

265260
let used_dep_crates = info.used_crates.iter();
@@ -626,10 +621,9 @@ fn link_staticlib(
626621

627622
let mut all_rust_dylibs = vec![];
628623
for &cnum in crates {
629-
match fmts.get(cnum) {
630-
Some(&Linkage::Dynamic) => {}
631-
_ => continue,
632-
}
624+
let Some(Linkage::Dynamic) = fmts.get(cnum) else {
625+
continue;
626+
};
633627
let crate_name = codegen_results.crate_info.crate_name[&cnum];
634628
let used_crate_source = &codegen_results.crate_info.used_crate_source[&cnum];
635629
if let Some((path, _)) = &used_crate_source.dylib {

compiler/rustc_codegen_ssa/src/back/write.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -573,10 +573,10 @@ fn produce_final_output_artifacts(
573573
};
574574

575575
let copy_if_one_unit = |output_type: OutputType, keep_numbered: bool| {
576-
if compiled_modules.modules.len() == 1 {
576+
if let [module] = &compiled_modules.modules[..] {
577577
// 1) Only one codegen unit. In this case it's no difficulty
578578
// to copy `foo.0.x` to `foo.x`.
579-
let module_name = Some(&compiled_modules.modules[0].name[..]);
579+
let module_name = Some(&module.name[..]);
580580
let path = crate_output.temp_path(output_type, module_name);
581581
let output = crate_output.path(output_type);
582582
if !output_type.is_text_output() && output.is_tty() {
@@ -708,8 +708,8 @@ fn produce_final_output_artifacts(
708708
}
709709

710710
if sess.opts.json_artifact_notifications {
711-
if compiled_modules.modules.len() == 1 {
712-
compiled_modules.modules[0].for_each_output(|_path, ty| {
711+
if let [module] = &compiled_modules.modules[..] {
712+
module.for_each_output(|_path, ty| {
713713
if sess.opts.output_types.contains_key(&ty) {
714714
let descr = ty.shorthand();
715715
// for single cgu file is renamed to drop cgu specific suffix
@@ -865,7 +865,7 @@ pub(crate) fn compute_per_cgu_lto_type(
865865
// require LTO so the request for LTO is always unconditionally
866866
// passed down to the backend, but we don't actually want to do
867867
// anything about it yet until we've got a final product.
868-
let is_rlib = sess_crate_types.len() == 1 && sess_crate_types[0] == CrateType::Rlib;
868+
let is_rlib = matches!(sess_crate_types, [CrateType::Rlib]);
869869

870870
match sess_lto {
871871
Lto::ThinLocal if !linker_does_lto && !is_allocator => ComputedLtoType::Thin,
@@ -1538,8 +1538,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
15381538
// Spin up what work we can, only doing this while we've got available
15391539
// parallelism slots and work left to spawn.
15401540
if codegen_state != Aborted {
1541-
while !work_items.is_empty() && running_with_own_token < tokens.len() {
1542-
let (item, _) = work_items.pop().unwrap();
1541+
while running_with_own_token < tokens.len()
1542+
&& let Some((item, _)) = work_items.pop()
1543+
{
15431544
spawn_work(
15441545
&cgcx,
15451546
&mut llvm_start_time,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+71-82
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::str::FromStr;
22

33
use rustc_abi::ExternAbi;
4-
use rustc_ast::attr::list_contains_name;
54
use rustc_ast::expand::autodiff_attrs::{
65
AutoDiffAttrs, DiffActivity, DiffMode, valid_input_activity, valid_ret_activity,
76
};
@@ -377,24 +376,20 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
377376
let segments =
378377
set.path.segments.iter().map(|x| x.ident.name).collect::<Vec<_>>();
379378
match segments.as_slice() {
380-
[sym::arm, sym::a32] | [sym::arm, sym::t32] => {
381-
if !tcx.sess.target.has_thumb_interworking {
382-
struct_span_code_err!(
383-
tcx.dcx(),
384-
attr.span,
385-
E0779,
386-
"target does not support `#[instruction_set]`"
387-
)
388-
.emit();
389-
None
390-
} else if segments[1] == sym::a32 {
391-
Some(InstructionSetAttr::ArmA32)
392-
} else if segments[1] == sym::t32 {
393-
Some(InstructionSetAttr::ArmT32)
394-
} else {
395-
unreachable!()
396-
}
379+
[sym::arm, sym::a32 | sym::t32]
380+
if !tcx.sess.target.has_thumb_interworking =>
381+
{
382+
struct_span_code_err!(
383+
tcx.dcx(),
384+
attr.span,
385+
E0779,
386+
"target does not support `#[instruction_set]`"
387+
)
388+
.emit();
389+
None
397390
}
391+
[sym::arm, sym::a32] => Some(InstructionSetAttr::ArmA32),
392+
[sym::arm, sym::t32] => Some(InstructionSetAttr::ArmT32),
398393
_ => {
399394
struct_span_code_err!(
400395
tcx.dcx(),
@@ -435,7 +430,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
435430
&& let Some((sym::align, literal)) = item.singleton_lit_list()
436431
{
437432
rustc_attr_parsing::parse_alignment(&literal.kind)
438-
.map_err(|msg| {
433+
.inspect_err(|msg| {
439434
struct_span_code_err!(
440435
tcx.dcx(),
441436
literal.span,
@@ -536,25 +531,27 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
536531
}
537532

538533
if attr.is_word() {
539-
InlineAttr::Hint
540-
} else if let Some(ref items) = attr.meta_item_list() {
541-
inline_span = Some(attr.span);
542-
if items.len() != 1 {
543-
struct_span_code_err!(tcx.dcx(), attr.span, E0534, "expected one argument").emit();
544-
InlineAttr::None
545-
} else if list_contains_name(items, sym::always) {
546-
InlineAttr::Always
547-
} else if list_contains_name(items, sym::never) {
548-
InlineAttr::Never
549-
} else {
550-
struct_span_code_err!(tcx.dcx(), items[0].span(), E0535, "invalid argument")
551-
.with_help("valid inline arguments are `always` and `never`")
552-
.emit();
534+
return InlineAttr::Hint;
535+
}
536+
let Some(ref items) = attr.meta_item_list() else {
537+
return ia;
538+
};
553539

554-
InlineAttr::None
555-
}
540+
inline_span = Some(attr.span);
541+
let [item] = &items[..] else {
542+
struct_span_code_err!(tcx.dcx(), attr.span, E0534, "expected one argument").emit();
543+
return InlineAttr::None;
544+
};
545+
if item.has_name(sym::always) {
546+
InlineAttr::Always
547+
} else if item.has_name(sym::never) {
548+
InlineAttr::Never
556549
} else {
557-
ia
550+
struct_span_code_err!(tcx.dcx(), item.span(), E0535, "invalid argument")
551+
.with_help("valid inline arguments are `always` and `never`")
552+
.emit();
553+
554+
InlineAttr::None
558555
}
559556
});
560557
codegen_fn_attrs.inline = attrs.iter().fold(codegen_fn_attrs.inline, |ia, attr| {
@@ -586,23 +583,25 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
586583
let err = |sp, s| struct_span_code_err!(tcx.dcx(), sp, E0722, "{}", s).emit();
587584
if attr.is_word() {
588585
err(attr.span, "expected one argument");
589-
ia
590-
} else if let Some(ref items) = attr.meta_item_list() {
591-
inline_span = Some(attr.span);
592-
if items.len() != 1 {
593-
err(attr.span, "expected one argument");
594-
OptimizeAttr::Default
595-
} else if list_contains_name(items, sym::size) {
596-
OptimizeAttr::Size
597-
} else if list_contains_name(items, sym::speed) {
598-
OptimizeAttr::Speed
599-
} else if list_contains_name(items, sym::none) {
600-
OptimizeAttr::DoNotOptimize
601-
} else {
602-
err(items[0].span(), "invalid argument");
603-
OptimizeAttr::Default
604-
}
586+
return ia;
587+
}
588+
let Some(ref items) = attr.meta_item_list() else {
589+
return OptimizeAttr::Default;
590+
};
591+
592+
inline_span = Some(attr.span);
593+
let [item] = &items[..] else {
594+
err(attr.span, "expected one argument");
595+
return OptimizeAttr::Default;
596+
};
597+
if item.has_name(sym::size) {
598+
OptimizeAttr::Size
599+
} else if item.has_name(sym::speed) {
600+
OptimizeAttr::Speed
601+
} else if item.has_name(sym::none) {
602+
OptimizeAttr::DoNotOptimize
605603
} else {
604+
err(item.span(), "invalid argument");
606605
OptimizeAttr::Default
607606
}
608607
});
@@ -644,25 +643,20 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
644643
// llvm/llvm-project#70563).
645644
if !codegen_fn_attrs.target_features.is_empty()
646645
&& matches!(codegen_fn_attrs.inline, InlineAttr::Always)
646+
&& let Some(span) = inline_span
647647
{
648-
if let Some(span) = inline_span {
649-
tcx.dcx().span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`");
650-
}
648+
tcx.dcx().span_err(span, "cannot use `#[inline(always)]` with `#[target_feature]`");
651649
}
652650

653-
if !codegen_fn_attrs.no_sanitize.is_empty() && codegen_fn_attrs.inline.always() {
654-
if let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span) {
655-
let hir_id = tcx.local_def_id_to_hir_id(did);
656-
tcx.node_span_lint(
657-
lint::builtin::INLINE_NO_SANITIZE,
658-
hir_id,
659-
no_sanitize_span,
660-
|lint| {
661-
lint.primary_message("`no_sanitize` will have no effect after inlining");
662-
lint.span_note(inline_span, "inlining requested here");
663-
},
664-
)
665-
}
651+
if !codegen_fn_attrs.no_sanitize.is_empty()
652+
&& codegen_fn_attrs.inline.always()
653+
&& let (Some(no_sanitize_span), Some(inline_span)) = (no_sanitize_span, inline_span)
654+
{
655+
let hir_id = tcx.local_def_id_to_hir_id(did);
656+
tcx.node_span_lint(lint::builtin::INLINE_NO_SANITIZE, hir_id, no_sanitize_span, |lint| {
657+
lint.primary_message("`no_sanitize` will have no effect after inlining");
658+
lint.span_note(inline_span, "inlining requested here");
659+
})
666660
}
667661

668662
// Weak lang items have the same semantics as "std internal" symbols in the
@@ -692,10 +686,10 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
692686
// Any linkage to LLVM intrinsics for now forcibly marks them all as never
693687
// unwinds since LLVM sometimes can't handle codegen which `invoke`s
694688
// intrinsic functions.
695-
if let Some(name) = &codegen_fn_attrs.link_name {
696-
if name.as_str().starts_with("llvm.") {
697-
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
698-
}
689+
if let Some(name) = &codegen_fn_attrs.link_name
690+
&& name.as_str().starts_with("llvm.")
691+
{
692+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NEVER_UNWIND;
699693
}
700694

701695
if let Some(features) = check_tied_features(
@@ -756,18 +750,13 @@ fn should_inherit_track_caller(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
756750

757751
fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &hir::Attribute) -> Option<u16> {
758752
use rustc_ast::{LitIntType, LitKind, MetaItemLit};
759-
let meta_item_list = attr.meta_item_list();
760-
let meta_item_list = meta_item_list.as_deref();
761-
let sole_meta_list = match meta_item_list {
762-
Some([item]) => item.lit(),
763-
Some(_) => {
764-
tcx.dcx().emit_err(errors::InvalidLinkOrdinalNargs { span: attr.span });
765-
return None;
766-
}
767-
_ => None,
753+
let meta_item_list = attr.meta_item_list()?;
754+
let [sole_meta_list] = &meta_item_list[..] else {
755+
tcx.dcx().emit_err(errors::InvalidLinkOrdinalNargs { span: attr.span });
756+
return None;
768757
};
769758
if let Some(MetaItemLit { kind: LitKind::Int(ordinal, LitIntType::Unsuffixed), .. }) =
770-
sole_meta_list
759+
sole_meta_list.lit()
771760
{
772761
// According to the table at
773762
// https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#import-header, the

compiler/rustc_codegen_ssa/src/mir/block.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
10031003
let destination = target.map(|target| (return_dest, target));
10041004

10051005
// Split the rust-call tupled arguments off.
1006-
let (first_args, untuple) = if abi == ExternAbi::RustCall && !args.is_empty() {
1007-
let (tup, args) = args.split_last().unwrap();
1006+
let (first_args, untuple) = if abi == ExternAbi::RustCall
1007+
&& let Some((tup, args)) = args.split_last()
1008+
{
10081009
(args, Some(tup))
10091010
} else {
10101011
(args, None)

0 commit comments

Comments
 (0)