Skip to content
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

Collect function instance used in global_asm! sym operand #96650

Merged
merged 1 commit into from
May 6, 2022
Merged
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
9 changes: 3 additions & 6 deletions compiler/rustc_monomorphize/src/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,9 @@ fn collect_items_rec<'tcx>(
// depend on any other items.
}
hir::InlineAsmOperand::SymFn { anon_const } => {
let def_id = tcx.hir().body_owner_def_id(anon_const.body).to_def_id();
if let Ok(val) = tcx.const_eval_poly(def_id) {
rustc_data_structures::stack::ensure_sufficient_stack(|| {
collect_const_value(tcx, val, &mut neighbors);
});
}
let fn_ty =
tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
visit_fn_use(tcx, fn_ty, false, *op_sp, &mut neighbors);
}
hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
let instance = Instance::mono(tcx, *def_id);
Expand Down
5 changes: 5 additions & 0 deletions src/test/assembly/asm/global_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// only-linux
// assembly-output: emit-asm
// compile-flags: -C llvm-args=--x86-asm-syntax=intel
// compile-flags: -C symbol-mangling-version=v0

#![feature(asm_const, asm_sym)]
#![crate_type = "rlib"]
Expand All @@ -24,3 +25,7 @@ global_asm!("movl ${}, %ecx", const 5, options(att_syntax));
global_asm!("call {}", sym my_func);
// CHECK: lea rax, [rip + MY_STATIC]
global_asm!("lea rax, [rip + {}]", sym MY_STATIC);
// CHECK: call _RNvCsiubXh4Yz005_10global_asm6foobar
global_asm!("call {}", sym foobar);
// CHECK: _RNvCsiubXh4Yz005_10global_asm6foobar:
fn foobar() { loop {} }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started with an empty function here, and noticed an issue where my_func and foobar had been merged together by mergefunc pass, invalidating the reference from global asm. Probably mergefunc shouldn't touch symbols from llvm.compiler.used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, mergefunc is perfectly valid here. In general Rust doesn't guarantee that two functions will have separate addresses.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, one of the functions is removed completely, and reference from the global asm is left unresolved.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, that does seem like a bug. If something is in llvm.compiler.used then it's likely that it is being referenced using its symbol name, in which case mergefuncs shouldn't rename it. I think we can work around this by removing unnamed_addr from any symbols referenced by llvm.compiler.used in a pass after all values are generated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is might be related to symbol export.

Functions that are referenced from inline asm in text form should be listed in exported_symbols. We currently don't have any special treatment of asm_sym there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give an example of what you have in mind?

As far as I know, for an inline assembly we do consider this aspect. For example, if a function with an inline assembly block might be code generated in a different crate (because it is marked inline or generic) and it references a non-generic function, the referenced function will be exported as necessary, etc. In what situation would it be necessary in the case of global asm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asm_sym is injected into the assembly as text, so when doing LTO/ThinLTO, LLVM would consider the symbol to be unused. This is different from llvm.compiler.used, which only marks the function to be used, not the symbol.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different from llvm.compiler.used, which only marks the function to be used, not the symbol.

I see, but I think this is incorrect. Both llvm.used and llvm.compiler.used are about symbols, not function per se. The LLVM reference seems quite clear about that. Those lists are also limited to named values for that reason.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reread LLVM doc, you are correct about that. Some further digging shows that the issue is actually we generate an LLVM IR that marks the function as internal: https://godbolt.org/z/c4jPajT48 -- removing the internal linkage will make codegen correct. The symbol export actually doesn't affect the outcome directly -- but it is used indirectly from rustc_monomorphize to determine the linkage.