Skip to content

Adapt NVPTX target-feature handling for LLVM 22#157868

Open
kulst wants to merge 1 commit into
rust-lang:mainfrom
kulst:nvptx-fixup-target-features
Open

Adapt NVPTX target-feature handling for LLVM 22#157868
kulst wants to merge 1 commit into
rust-lang:mainfrom
kulst:nvptx-fixup-target-features

Conversation

@kulst

@kulst kulst commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Previously, we did not explicitly enable a PTX version target-feature, because LLVM implied one from the selected CPU.

Starting with LLVM 22, the NVPTX backend still infers the minimum required PTX version from the selected CPU when none is selected explicitly, but no longer reflects that version in the enabled subtarget features. As a result, cfg could miss the PTX version of the emitted PTX. Also, if a PTX version below the required minimum is selected explicitly, LLVM errors.
We missed this because the corresponding NVPTX target-feature test was mistakenly disabled.

With this patch we always select a PTX version explicitly, whichever is higher between

  • Rust's minimum required PTX version, and
  • the CPU's minimum required PTX version.

The patch also re-enables the NVPTX target-feature test to catch such changes in the future.
It additionally forbids PTX and sm versions below Rust's minimum required versions by marking the corresponding target-features as Forbidden.
Lastly it adds PTX and sm versions present in LLVM 22 but missing as Rust target-features.

This will not prevent all issues. In fact, the following combinations might still miscompile:

-Ctarget-cpu=sm_101 -Ctarget-feature=+ptx90 // sm_101 was renamed starting with ptx90
-Ctarget-cpu=sm_70 -Ctarget-feature=+sm_80,+ptx81 // .target directive is sm_70 but instructions are selected from sm_80

cc: @kjetilkjeka
r? @ZuseZ4
@rustbot label +O-NVPTX

@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. O-NVPTX Target: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.html labels Jun 13, 2026
@rust-log-analyzer

This comment has been minimized.

@kulst kulst force-pushed the nvptx-fixup-target-features branch 2 times, most recently from 7b7a043 to abd0003 Compare June 13, 2026 19:50
@rust-log-analyzer

This comment has been minimized.

Previously, Rust did not explicitly enable a PTX version target-feature,
because LLVM implied one from the selected CPU.

Starting with LLVM 22, the NVPTX backend still infers the minimum required
PTX version from the selected CPU when none is selected explicitly, but no
longer reflects that version in the enabled subtarget features. As a result,
cfg could miss the PTX version of the emitted PTX. Also, if a PTX version
below the required minimum is selected explicitly, LLVM errors.

We missed this because the corresponding NVPTX target-feature test was
mistakenly disabled.

To keep cfg evaluation consistent and avoid LLVM errors, always select an
explicit PTX version: whichever is higher between
 - Rust's minimum required PTX version, and
 - the CPU's minimum required PTX version.

Re-enable the NVPTX target-feature test and adapt it to Rust's minimum
required PTX and sm versions.

Forbid PTX and sm versions below Rust's minimum required versions by marking
the corresponding target-features as `Forbidden`.

Add PTX and sm versions present in LLVM 22 but missing as Rust
target-features.
@kulst kulst force-pushed the nvptx-fixup-target-features branch from abd0003 to 8ec03af Compare June 13, 2026 21:14
@rustbot rustbot added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. O-NVPTX Target: the NVPTX LLVM backend for running rust on GPUs, https://llvm.org/docs/NVPTXUsage.html 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants