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

Pass through of target features to llvm-bitcode-linker and handling them #136608

Merged
merged 2 commits into from
Feb 21, 2025

Conversation

kulst
Copy link

@kulst kulst commented Feb 5, 2025

When using the llvm-bitcode-linker (linker-flavor=llbc) target-features are not passed through and are not handled by it.
The llvm-bitcode-linker is mainly used as a self contained linker to link llvm bitcode for the nvptx64 target. It uses llvm-link, opt and llc internally. To produce a .ptx file of a specific ptx-version it is necessary to pass the version to llc with the --mattr option. Without explicitly setting it, the emitted .ptx-version is the minimum supported version of the --target-cpu.

I would like to be able to explicitly set the ptx version as some llvm problems only occur in earlier .ptx-versions.

Therefore this pull request adds support for passing target features to llvm-bitcode-linker and handling them.
I was not quite sure if adding these features to rustc_target/src/target_features.rs is necessary or not. If so I will gladly add these.

r? @kjetilkjeka 

@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Noratrieb (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 5, 2025
@rust-log-analyzer

This comment has been minimized.

@kulst kulst force-pushed the ptx_target_features branch from f4e1ac4 to 935529b Compare February 5, 2025 23:27
@workingjubilee
Copy link
Member

r? @kjetilkjeka

but I don't think it will work 🤔

@rustbot
Copy link
Collaborator

rustbot commented Feb 6, 2025

Failed to set assignee to kjetilkjeka: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@kjetilkjeka
Copy link
Contributor

kjetilkjeka commented Feb 6, 2025

Thanks for wanting to fix this bug in the llbm-bitcode-linker.

Therefore this pull request adds support for passing target features to llvm-bitcode-linker and handling them.
I was not quite sure if adding these features to rustc_target/src/target_features.rs is necessary or not. If so I will gladly add these.

The features should be added in rustc_target/src/target_features.rs but it doesn't have to be done in this PR. It's sufficient that this PR fixes the behavior of llvm-bitcode-linker and then a different PR can add target_features for nvptx.

After this PR is merged it's already possible to use +ptx85 as a feature but a warning will be emited

warning: unknown and unstable feature specified for `-Ctarget-feature`: `ptx85`
  |
  = note: it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
  = help: consider filing a feature request

By adding the features to rustc_target/src/target_features.rs it's possible to avoid the warning and reason about incompatible and implied features (I haven't tested the llvm behavior but assume that if +ptx85 is specified then either +ptx78 should be incompatible or implied).

@kulst
Copy link
Author

kulst commented Feb 6, 2025

Thanks for the feedback.

I would prefer to add these features to rustc_target/src/target_features.rs in a different PR.

The behavior of llc that I could observe during my tests is the highest ptx version specified wins. The logic in llvm is generated by tablegen and looks like this:

if (Bits[NVPTX::PTX32] && PTXVersion < 32) PTXVersion = 32;
if (Bits[NVPTX::PTX40] && PTXVersion < 40) PTXVersion = 40;
// ...
if (Bits[NVPTX::PTX87] && PTXVersion < 87) PTXVersion = 87;

So -mattr=+ptx85,+ptx78 produces ptx85.

@kulst kulst force-pushed the ptx_target_features branch from 935529b to 8f353ae Compare February 8, 2025 17:38
@rust-log-analyzer

This comment has been minimized.

@kulst kulst force-pushed the ptx_target_features branch from 8f353ae to 851724c Compare February 8, 2025 19:12
@kulst
Copy link
Author

kulst commented Feb 8, 2025

I changed the implementation so that the existing global_backend_features query is used. This query calls global_llvm_features which to my knowledge is the current logic used to collect the target features that get passed to llvm.
To make this possible it is neccessary to query before link stage, as the TyCtxt is no longer present there. Therefore an additional field is added for the CrateInfo type to persist the target features until link stage.

Other options would have been:

  1. Using sess.target_features: These are collected by target_features_cfg which does only incorporate features known to rustc. This would contradict with the current behavior of rustc to pass also target_features to llvm unknown to rustc.
  2. Duplicating the logic that is used by either of the two mentioned functions: As we already have two functions that use a similar logic this did not seem to be the right solution to me.

@kulst kulst force-pushed the ptx_target_features branch from 851724c to 556281f Compare February 9, 2025 11:11
@kulst
Copy link
Author

kulst commented Feb 9, 2025

I slightly changed the implementation. rustc_codegen_ssa acts now as a provider for the default global_backend_features query. This ensures the query is always registered even if a CodegenBackend does not register it by its provide method.
I am now quite happy with the implementation and am looking forward getting additional feedback.

@kulst kulst requested a review from kjetilkjeka February 10, 2025 16:54
@kjetilkjeka
Copy link
Contributor

It looks like you found the right place to hook onto the target_feature.

It seems to me like CrateInfo is an old name and since it was merged with LinkerInfo it has also contained fields like target_cpu. Based on that I agree that it's also appropriate to include the target_features 👍

I'm not an approved reviewer for rustc, but it looks great from my perspective. If I could I would have r+'ed.

For the follow up, where you add the ptx features. Also add a couple of tests where we check that using features like ptx85 and/or ptx75 gives the correct behavior in ptx. I see that there's currently no tests for global_llvm_features and a comment that express a bit uncertainty about the exact precedence.

Thanks a lot for contributing to the llvm-bitcode-linker and also for help improving the experience of using the nvptx in Rust. I greatly look forward to unlock everything that comes with the debug symbols. ❤️

@kulst
Copy link
Author

kulst commented Feb 11, 2025

Thanks again for the review and the positive feedback!
I will take your comments into consideration for the follow up.
I wasn't sure if you are allowed to officially review rustc pull requests as an nvptx target maintainer. But since I was interested in your feedback anyway, I decided to give it a try.

@Noratrieb
Copy link
Member

r? bjorn3 I think you're the best person to judge the approach from a compiler side

@rustbot rustbot assigned bjorn3 and unassigned Noratrieb Feb 16, 2025
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 16, 2025
kulst added 2 commits February 16, 2025 21:57
Bitcode linkers like llvm-bitcode-linker or bpf linker hand over the target features to llvm during link stage. During link stage the `TyCtxt` is already gone so it is not possible to create a query for the global backend features any longer. The features preserved in `Session.target_features` only incorporate target features known to rustc. This would contradict with the behaviour during codegen stage which also passes target features to llvm which are unknown to rustc.
This commit adds target features as a field to the `CrateInfo` struct and queries the target features in its new function. This way the target features are preserved beyond tcx and available at link stage.
To make sure the `global_backend_features` query is always registered even if the CodegenBackend does not register it, this registration is added to the `provide`function of the `rustc_codegen_ssa` crate.
The .ptx version produced by llc can be specified by passing it with --mattr. Currently it is not possible to specify the .ptx version with -Ctarget-feature because these are not passed through to llvm-bitcode-linker and handled by it. This commit adds both.
--target-feature and -mattr are passed with equals to mitigate issues when the value starts with a - (minus).
@kulst kulst force-pushed the ptx_target_features branch from 556281f to 831d9f3 Compare February 16, 2025 21:13
@kulst kulst requested a review from bjorn3 February 16, 2025 22:54
@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 Feb 16, 2025
@bjorn3
Copy link
Member

bjorn3 commented Feb 20, 2025

@bors r+

@bors
Copy link
Contributor

bors commented Feb 20, 2025

📌 Commit 831d9f3 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 Feb 20, 2025
workingjubilee added a commit to workingjubilee/rustc that referenced this pull request Feb 20, 2025
Pass through of target features to llvm-bitcode-linker and handling them

When using the llvm-bitcode-linker (`linker-flavor=llbc`) target-features are not passed through and are not handled by it.
The llvm-bitcode-linker is mainly used as a self contained linker to link llvm bitcode for the nvptx64 target. It uses `llvm-link`, `opt` and `llc` internally. To produce a `.ptx` file of a specific ptx-version it is necessary to pass the version to llc with the `--mattr` option. Without explicitly setting it, the emitted `.ptx`-version is the minimum supported version of the `--target-cpu`.

I would like to be able to explicitly set the ptx version as [some llvm problems only occur in earlier `.ptx`-versions](llvm/llvm-project#112998).

Therefore this pull request adds support for passing target features to llvm-bitcode-linker and handling them.
I was not quite sure if adding these features to `rustc_target/src/target_features.rs` is necessary or not. If so I will gladly add these.

    r? `@kjetilkjeka`
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 20, 2025
…kingjubilee

Rollup of 14 pull requests

Successful merges:

 - rust-lang#131651 (Create a generic AVR target: avr-none)
 - rust-lang#136473 (infer linker flavor by linker name if it's sufficiently specific)
 - rust-lang#136608 (Pass through of target features to llvm-bitcode-linker and handling them)
 - rust-lang#136985 (Do not ignore uninhabited types for function-call ABI purposes. (Remove BackendRepr::Uninhabited))
 - rust-lang#137192 (Remove obsolete Windows ThinLTO+TLS workaround)
 - rust-lang#137204 (Clarify MIR dialects and phases)
 - rust-lang#137270 (Fix `*-win7-windows-msvc` target since 26eeac1)
 - rust-lang#137298 (Check signature WF when lowering MIR body)
 - rust-lang#137299 (Simplify `Postorder` customization.)
 - rust-lang#137312 (Update references to cc_detect.rs)
 - rust-lang#137313 (Some codegen_llvm cleanups)
 - rust-lang#137318 (Workaround Cranelift not yet properly supporting vectors smaller than 128bit)
 - rust-lang#137322 (Update docs for default features of wasm targets)
 - rust-lang#137324 (Make x86 QNX target name consistent with other Rust targets)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 21, 2025
…kingjubilee

Rollup of 12 pull requests

Successful merges:

 - rust-lang#131651 (Create a generic AVR target: avr-none)
 - rust-lang#134340 (Stabilize `num_midpoint_signed` feature)
 - rust-lang#136473 (infer linker flavor by linker name if it's sufficiently specific)
 - rust-lang#136608 (Pass through of target features to llvm-bitcode-linker and handling them)
 - rust-lang#136985 (Do not ignore uninhabited types for function-call ABI purposes. (Remove BackendRepr::Uninhabited))
 - rust-lang#137270 (Fix `*-win7-windows-msvc` target since 26eeac1)
 - rust-lang#137312 (Update references to cc_detect.rs)
 - rust-lang#137318 (Workaround Cranelift not yet properly supporting vectors smaller than 128bit)
 - rust-lang#137322 (Update docs for default features of wasm targets)
 - rust-lang#137324 (Make x86 QNX target name consistent with other Rust targets)
 - rust-lang#137338 (skip submodule updating logics on tarballs)
 - rust-lang#137340 (Add a notice about missing GCC sources into source tarballs)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6d74563 into rust-lang:master Feb 21, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Feb 21, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 21, 2025
Rollup merge of rust-lang#136608 - kulst:ptx_target_features, r=bjorn3

Pass through of target features to llvm-bitcode-linker and handling them

When using the llvm-bitcode-linker (`linker-flavor=llbc`) target-features are not passed through and are not handled by it.
The llvm-bitcode-linker is mainly used as a self contained linker to link llvm bitcode for the nvptx64 target. It uses `llvm-link`, `opt` and `llc` internally. To produce a `.ptx` file of a specific ptx-version it is necessary to pass the version to llc with the `--mattr` option. Without explicitly setting it, the emitted `.ptx`-version is the minimum supported version of the `--target-cpu`.

I would like to be able to explicitly set the ptx version as [some llvm problems only occur in earlier `.ptx`-versions](llvm/llvm-project#112998).

Therefore this pull request adds support for passing target features to llvm-bitcode-linker and handling them.
I was not quite sure if adding these features to `rustc_target/src/target_features.rs` is necessary or not. If so I will gladly add these.

    r? ``@kjetilkjeka``
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.

8 participants