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

Don't internalize __llvm_profile_counter_bias #102900

Merged
merged 1 commit into from
Dec 11, 2022
Merged

Conversation

abrachet
Copy link
Contributor

Currently, LLVM profiling runtime counter relocation cannot be used by rust during LTO because symbols are being internalized before all symbol information is known.

This mode makes LLVM emit a __llvm_profile_counter_bias symbol which is referenced by the profiling initialization, which itself is pulled in by the rust driver here [1].

It is enabled with -Cllvm-args=-runtime-counter-relocation for platforms which are opt-in to this mode like Linux. On these platforms there will be no link error, rather just surprising behavior for a user which request runtime counter relocation. The profiling runtime will not see that symbol go on as if it were never there. On Fuchsia, the profiling runtime must have this symbol which will cause a hard link error.

As an aside, I don't have enough context as to why rust's LTO model is how it is. AFAICT, the internalize pass is only safe to run at link time when all symbol information is actually known, this being an example as to why. I think special casing this symbol as a known one that LLVM can emit which should not have it's visbility de-escalated should be fine given how seldom this pattern of defining an undefined symbol to get initilization code pulled in is. From a quick grep, __llvm_profile_runtime is the only symbol that rustc does this for.

[1]

self.cmd.arg("__llvm_profile_runtime");

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

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @cjgillot (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 10, 2022
@bjorn3
Copy link
Member

bjorn3 commented Oct 11, 2022

I think it should be added to

["__llvm_profile_raw_version", "__llvm_profile_filename"];
instead.

@abrachet
Copy link
Contributor Author

I think it should be added to

["__llvm_profile_raw_version", "__llvm_profile_filename"];

instead.

Done

@klensy
Copy link
Contributor

klensy commented Oct 12, 2022

You can squash commits to remove now unneeded changes.

@abrachet
Copy link
Contributor Author

You can squash commits to remove now unneeded changes.

Done

@cjgillot
Copy link
Contributor

r? @bjorn3

@rust-highfive rust-highfive assigned bjorn3 and unassigned cjgillot Oct 13, 2022
@bjorn3
Copy link
Member

bjorn3 commented Oct 13, 2022

Could you add a test for this?

@abrachet
Copy link
Contributor Author

Could you add a test for this?

Sure, let me know if that test looks ok.

@bjorn3
Copy link
Member

bjorn3 commented Oct 13, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Oct 13, 2022

📌 Commit 2f7b4d9 has been approved by bjorn3

It is now in the queue for this repository.

@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 Oct 13, 2022
@bors
Copy link
Contributor

bors commented Oct 16, 2022

⌛ Testing commit 2f7b4d9 with merge a16abce7a31818d541760097059538aa6c5aa1e4...

@bors
Copy link
Contributor

bors commented Oct 16, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 16, 2022
@rust-log-analyzer

This comment has been minimized.

@abrachet
Copy link
Contributor Author

It looks like this creates an undefined reference to these symbols (in /tmp/.../symbols.o), which is fine on other platforms but I guess doesn't work so well with how weak symbols are implemented in Windows, see https://github.com/llvm/llvm-project/blob/6db71b8f1418170324b49d20f1f7b3f7c5086066/compiler-rt/lib/profile/InstrProfilingFile.c#L168-L178.

As far as I can tell the fix is to stop indiscriminately adding this to the exported symbols list and only do so when __llvm_profile_counter_bias is emitted, which seems untenable given llvm decides to do this based on target (which currently is only one target but could likely grow) or backend flag. Alternatively we could go back to putting this in prepare_lto, though here is admittedly much better because this is where other similar symbols are handled. WDYT?

@bjorn3
Copy link
Member

bjorn3 commented Oct 18, 2022

I'm not quite sure. Maybe @nikic has an idea?

Manishearth added a commit to Manishearth/rust that referenced this pull request Nov 11, 2022
Don't internalize __llvm_profile_counter_bias

Currently, LLVM profiling runtime counter relocation cannot be used by rust during LTO because symbols are being internalized before all symbol information is known.

This mode makes LLVM emit a __llvm_profile_counter_bias symbol which is referenced by the profiling initialization, which itself is pulled in by the rust driver here [1].

It is enabled with -Cllvm-args=-runtime-counter-relocation for platforms which are opt-in to this mode like Linux. On these platforms there will be no link error, rather just surprising behavior for a user which request runtime counter relocation. The profiling runtime will not see that symbol go on as if it were never there. On Fuchsia, the profiling runtime must have this symbol which will cause a hard link error.

As an aside, I don't have enough context as to why rust's LTO model is how it is. AFAICT, the internalize pass is only safe to run at link time when all symbol information is actually known, this being an example as to why. I think special casing this symbol as a known one that LLVM can emit which should not have it's visbility de-escalated should be fine given how seldom this pattern of defining an undefined symbol to get initilization code pulled in is. From a quick grep, __llvm_profile_runtime is the only symbol that rustc does this for.

[1] https://github.com/rust-lang/rust/blob/0265a3e93bf1b89d97cae113ed214954d5c35e22/compiler/rustc_codegen_ssa/src/back/linker.rs#L598
Currently, LLVM profiling runtime counter relocation cannot be
used by rust during LTO because symbols are being internalized
before all symbol information is known.

This mode makes LLVM emit a __llvm_profile_counter_bias symbol
which is referenced by the profiling initialization, which itself
is pulled in by the rust driver here [1].

It is enabled with -Cllvm-args=-runtime-counter-relocation for
platforms which are opt-in to this mode like Linux. On these
platforms there will be no link error, rather just surprising
behavior for a user which request runtime counter relocation.
The profiling runtime will not see that symbol go on as if it
were never there. On Fuchsia, the profiling runtime must have
this symbol which will cause a hard link error.

As an aside, I don't have enough context as to why rust's LTO
model is how it is. AFAICT, the internalize pass is only safe
to run at link time when all symbol information is actually
known, this being an example as to why. I think special casing
this symbol as a known one that LLVM can emit which should not
have it's visbility de-escalated should be fine given how
seldom this pattern of defining an undefined symbol to get
initilization code pulled in is. From a quick grep,
__llvm_profile_runtime is the only symbol that rustc does this
for.

[1] https://github.com/rust-lang/rust/blob/0265a3e93bf1b89d97cae113ed214954d5c35e22/compiler/rustc_codegen_ssa/src/back/linker.rs#L598
@abrachet
Copy link
Contributor Author

abrachet commented Dec 7, 2022

Looks like -runtime-counter-relocation is ignored for Mach-O targets [1]. I've added an // ignore-macos line in the test which should hopefully fix the test failure on the bots. Sorry about that

[1] https://github.com/llvm/llvm-project/blob/main/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp#L443

@bjorn3
Copy link
Member

bjorn3 commented Dec 7, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Dec 7, 2022

📌 Commit 5d88d36 has been approved by bjorn3

It is now in the queue for this repository.

@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 Dec 7, 2022
@bors
Copy link
Contributor

bors commented Dec 7, 2022

⌛ Testing commit 5d88d36 with merge d31d00eef1d034f6e478f0088c74516ae3a98f44...

@bors
Copy link
Contributor

bors commented Dec 7, 2022

💔 Test failed - checks-actions

@bors bors added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Dec 7, 2022
@ehuss
Copy link
Contributor

ehuss commented Dec 8, 2022

@bors retry

Failed to update toolstate
(has been fixed)

@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 Dec 8, 2022
@rust-log-analyzer

This comment was marked as resolved.

@abrachet
Copy link
Contributor Author

abrachet commented Dec 8, 2022

Looks like the same error, should we retry again?

@bjorn3
Copy link
Member

bjorn3 commented Dec 8, 2022

@bors retry Failed to update toolstate

@bjorn3
Copy link
Member

bjorn3 commented Dec 8, 2022

@bors retry

@ehuss
Copy link
Contributor

ehuss commented Dec 8, 2022

This PR is already in the queue. The rust-log-analyzer comment was just delayed by about 12 hours.

@bors
Copy link
Contributor

bors commented Dec 11, 2022

⌛ Testing commit 5d88d36 with merge d137783...

@bors
Copy link
Contributor

bors commented Dec 11, 2022

☀️ Test successful - checks-actions
Approved by: bjorn3
Pushing d137783 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Dec 11, 2022
@bors bors merged commit d137783 into rust-lang:master Dec 11, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 11, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (d137783): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.4% [2.0%, 2.7%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.7%, -2.3%] 2
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.5% [2.5%, 2.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Aaron1011 pushed a commit to Aaron1011/rust that referenced this pull request Jan 6, 2023
Don't internalize __llvm_profile_counter_bias

Currently, LLVM profiling runtime counter relocation cannot be used by rust during LTO because symbols are being internalized before all symbol information is known.

This mode makes LLVM emit a __llvm_profile_counter_bias symbol which is referenced by the profiling initialization, which itself is pulled in by the rust driver here [1].

It is enabled with -Cllvm-args=-runtime-counter-relocation for platforms which are opt-in to this mode like Linux. On these platforms there will be no link error, rather just surprising behavior for a user which request runtime counter relocation. The profiling runtime will not see that symbol go on as if it were never there. On Fuchsia, the profiling runtime must have this symbol which will cause a hard link error.

As an aside, I don't have enough context as to why rust's LTO model is how it is. AFAICT, the internalize pass is only safe to run at link time when all symbol information is actually known, this being an example as to why. I think special casing this symbol as a known one that LLVM can emit which should not have it's visbility de-escalated should be fine given how seldom this pattern of defining an undefined symbol to get initilization code pulled in is. From a quick grep, __llvm_profile_runtime is the only symbol that rustc does this for.

[1] https://github.com/rust-lang/rust/blob/0265a3e93bf1b89d97cae113ed214954d5c35e22/compiler/rustc_codegen_ssa/src/back/linker.rs#L598
@tmandry tmandry added the O-fuchsia Operating system: Fuchsia label May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-fuchsia Operating system: Fuchsia 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.