Skip to content

Don't internalize symbols used by global_asm #97900

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 21 additions & 1 deletion compiler/rustc_monomorphize/src/partitioning/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,28 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
cx: &PartitioningCx<'_, 'tcx>,
partitioning: &mut PostInliningPartitioning<'tcx>,
) {
// Collect symbols used by GlobalAsm. A GlobalAsm effectively acts like
// its own codegen unit and needs to be able to access symbols that it
// imports using sym operands.
let mut global_asm_map: FxHashSet<MonoItem<'tcx>> = Default::default();
cx.inlining_map.iter_accesses(|accessor, accessees| {
if let MonoItem::GlobalAsm(_) = accessor {
for accessee in accessees {
global_asm_map.insert(*accessee);
}
}
});

if partitioning.codegen_units.len() == 1 {
// Fast path for when there is only one codegen unit. In this case we
// can internalize all candidates, since there is nowhere else they
// could be accessed from.
for cgu in &mut partitioning.codegen_units {
for candidate in &partitioning.internalization_candidates {
cgu.items_mut().insert(*candidate, (Linkage::Internal, Visibility::Default));
if !global_asm_map.contains(candidate) {
cgu.items_mut()
.insert(*candidate, (Linkage::Internal, Visibility::Default));
}
}
}

Expand Down Expand Up @@ -255,6 +270,11 @@ impl<'tcx> Partitioner<'tcx> for DefaultPartitioning {
}
}

// Don't internalize symbols used by GlobalAsm.
if global_asm_map.contains(accessee) {
continue;
}

// If we got here, we did not find any accesses from other CGUs,
// so it's fine to make this monomorphization internal.
*linkage_and_visibility = (Linkage::Internal, Visibility::Default);
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/issues/issue-96797.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// build-pass
// compile-flags: -O

// regression test for #96797

#![feature(asm_sym)]

use std::arch::global_asm;

#[no_mangle]
fn my_func() {}

global_asm!("call_foobar: jmp {}", sym foobar);

fn foobar() {}

fn main() {
extern "Rust" {
fn call_foobar();
}
unsafe { call_foobar() };
}