Skip to content

Rename symbols rather than add symbol aliases for EIIs when possible - #159843

Open
bjorn3 wants to merge 2 commits into
rust-lang:mainfrom
bjorn3:eii_using_symbol_rename
Open

bjorn3 wants to merge 2 commits into
rust-lang:mainfrom
bjorn3:eii_using_symbol_rename

Conversation

@bjorn3

@bjorn3 bjorn3 commented Jul 24, 2026 •

Copy link
Copy Markdown
Member

The symbol name of the EII impl will now get changed to match the EII decl whenever there is no default impl. This already works across all backends and targets unlike the weak symbol aliases that we currently use. When there is a default impl, we will still use weak symbol aliases.

cc #125418

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 24, 2026
@bjorn3 bjorn3 mentioned this pull request Jul 24, 2026
2 of 3 tasks
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch 2 times, most recently from 580bf28 to 3b34082 Compare August 4, 2026 13:43
@rust-bors

This comment has been minimized.

@bjorn3 bjorn3 added the F-extern_item_impls `#![feature(extern_item_impls)]` label Sep 17, 2026
@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from 3b34082 to 479e3fa Compare October 21, 2026 08:43
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch 2 times, most recently from a5312a1 to 222159d Compare October 28, 2026 10:51
@rust-log-analyzer

This comment has been minimized.

@bjorn3

bjorn3 commented Nov 6, 2026

Copy link
Copy Markdown
Member Author

@antoyo @GuillaumeGomez does GCC have bugs around comparing pointers casted to integers?

thread 'main' ($TID) panicked at $DIR/same_address.rs:16:5:
assertion `left == right` failed
  left: 94239728895888
 right: 94239728895888

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from 222159d to 44b1ad1 Compare November 6, 2026 15:42
@antoyo

antoyo commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

@antoyo @GuillaumeGomez does GCC have bugs around comparing pointers casted to integers?

No, not that I know of.

@RalfJung

RalfJung commented Nov 6, 2026

Copy link
Copy Markdown
Member

GCC devs at least historically had strange ideas about pointer equality, so I would not be surprised if they are doing optimizations that are incorrect for Rust.

@rust-log-analyzer

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from 44b1ad1 to 250a379 Compare November 7, 2026 11:22
@rust-log-analyzer

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from 250a379 to e464681 Compare November 7, 2026 12:15
@bjorn3

bjorn3 commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Disabled the test with GCC for now.

@bjorn3
bjorn3 marked this pull request as ready for review November 7, 2026 13:30
@rustbot

rustbot commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

rustc_codegen_gcc is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_gcc instead.

cc @antoyo, @GuillaumeGomez

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 7, 2026
@rustbot

rustbot commented Nov 7, 2026

Copy link
Copy Markdown
Collaborator

r? @wesleywiser

rustbot has assigned @wesleywiser.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 22 candidates

@rust-bors

This comment has been minimized.

@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from e464681 to 58d3ca0 Compare November 12, 2026 11:09
@rustbot

This comment has been minimized.

@rust-bors

This comment has been minimized.

The symbol name of the EII impl will now get changed to match the EII
decl whenever there is no default impl. This already works across all
backends and targets unlike the weak symbol aliases that we currently
use. When there is a default impl, we will still use weak symbol
aliases.
@bjorn3
bjorn3 force-pushed the eii_using_symbol_rename branch from 58d3ca0 to 39bb028 Compare November 14, 2026 11:41
@rustbot

rustbot commented Nov 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

Comment on lines +267 to +269
codegen_fn_attrs.symbol_name = Some(Symbol::intern(
tcx.symbol_name(Instance::mono(tcx, foreign_item)).name,
));

@cezarbbb cezarbbb Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Was it intentional that the renamed symbol now inherits -Zdefault-visibility?

View changes since the review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

EIIs are Rust-level, so with -Zdefault-visibility=hidden a dylib containing the impl no longer exports decl::foo and a downstream crate fails to link with undefined symbol: decl::foo. At the same time, the weak-alias path hardcoded Visibility::Default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While I feel the current changes are more logically intuitive, this does represent a change in behavior, doesn't it?

@bjorn3 bjorn3 Nov 16, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

-Zdefault-visibility=hidden breaks rust dylibs entirely, right?
Edit: Indeed:

$ echo '#[inline(never)] pub fn square(num: i32) -> i32 { num * num }' | rustc +nightly - --crate-type dylib -Cprefer-dynamic -Zdefault-visibility=hidden
$ echo 'fn main() { rust_out::square(1); }' | rustc +nightly - --extern rust_out=librust_out.so
error: linking with `cc` failed: exit status: 1
  |
  = note:  "cc" "-m64" "/tmp/rustchEtbi4/symbols.o" "<1 object files omitted>" "-Wl,--as-needed" "-Wl,-Bdynamic" "/tmp/librust_out.so" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-20a66e70cfa492f6.so" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-*.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/tmp/rustchEtbi4/raw-dylibs" "-B<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "rust_out" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: some arguments are omitted. use `--verbose` to show all linker arguments
  = note: rust-lld: error: undefined symbol: rust_out::square
          >>> referenced by rust_out.8fb83714f74d418f-cgu.0
          >>>               rust_out.rust_out.8fb83714f74d418f-cgu.0.rcgu.o:(rust_out::main)
          collect2: error: ld returned 1 exit status
          

error: aborting due to 1 previous error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, not EII specifically.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. F-extern_item_impls `#![feature(extern_item_impls)]` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. 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.

7 participants