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

Conversation

tmiasko
Copy link
Contributor

@tmiasko tmiasko commented May 2, 2022

The constants used in SymFn operands have FnDef type,
so the type of the constant identifies the function.

Fixes #96623.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label May 2, 2022
@rust-highfive
Copy link
Collaborator

r? @cjgillot

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2022
@tmiasko
Copy link
Contributor Author

tmiasko commented May 2, 2022

r? @Amanieu

@rust-highfive rust-highfive assigned Amanieu and unassigned cjgillot May 2, 2022
// 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.

The constants used in SymFn operands have FnDef type,
so the type of the constant identifies the function.
@tmiasko tmiasko force-pushed the global-asm-sym-fn branch from 3d071b1 to e626634 Compare May 3, 2022 07:19
@Amanieu
Copy link
Member

Amanieu commented May 3, 2022

@bors r+

@bors
Copy link
Contributor

bors commented May 3, 2022

📌 Commit e626634 has been approved by Amanieu

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 3, 2022
compiler-errors added a commit to compiler-errors/rust that referenced this pull request May 5, 2022
Collect function instance used in `global_asm!` sym operand

The constants used in SymFn operands have FnDef type,
so the type of the constant identifies the function.

Fixes rust-lang#96623.
compiler-errors added a commit to compiler-errors/rust that referenced this pull request May 6, 2022
Collect function instance used in `global_asm!` sym operand

The constants used in SymFn operands have FnDef type,
so the type of the constant identifies the function.

Fixes rust-lang#96623.
bors added a commit to rust-lang-ci/rust that referenced this pull request May 6, 2022
…laumeGomez

Rollup of 10 pull requests

Successful merges:

 - rust-lang#96557 (Allow inline consts to reference generic params)
 - rust-lang#96590 (rustdoc: when running a function-signature search, tweak the tab bar)
 - rust-lang#96650 (Collect function instance used in `global_asm!` sym operand)
 - rust-lang#96733 (turn `append_place_to_string` from recursion into iteration)
 - rust-lang#96748 (Fixes reexports in search)
 - rust-lang#96752 (Put the incompatible_closure_captures lint messages in alphabetical order)
 - rust-lang#96754 (rustdoc: ensure HTML/JS side implementors don't have dups)
 - rust-lang#96772 (Suggest fully qualified path with appropriate params)
 - rust-lang#96776 (Fix two minor issues in hir.rs)
 - rust-lang#96782 (a small `mirror_expr` cleanup)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit c7af4e6 into rust-lang:master May 6, 2022
@rustbot rustbot added this to the 1.62.0 milestone May 6, 2022
@tmiasko tmiasko deleted the global-asm-sym-fn branch May 7, 2022 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

global_asm! sym operands emit functions without function body
7 participants