Skip to content

Fix compiletest's --target-arch handling.#307

Merged
LegNeato merged 1 commit intoRust-GPU:mainfrom
nnethercote:fix-compiletest-target-arch
Nov 19, 2025
Merged

Fix compiletest's --target-arch handling.#307
LegNeato merged 1 commit intoRust-GPU:mainfrom
nnethercote:fix-compiletest-target-arch

Conversation

@nnethercote
Copy link
Copy Markdown
Collaborator

compiletests lets you specify one or more target architectures. E.g. on CI we run this:

cargo run -p compiletests --release --no-default-features -- \
    --target-arch compute_61,compute_70,compute_90

While trying out various different target architectures, including invalid ones, I get strange results.

First, if the target arch has fewer than 8 chars (e.g. --target-arch blah), then nvvm panics:

thread 'rustc' panicked at crates/nvvm/src/lib.rs:246:38: byte
index 8 is out of bounds of `blah`

Second, if the target arch has 8 or more chars but is invalid, it is just ignored. This means invalid flags like --target-arch compute_999 run, but just use the default target architecture.

This commit fixes the problems.

  • Fix the slicing into the -arch= option to avoid the bounds panic.
  • In CodegenArgs::parse, check for the Err case and panic instead of swallowing it. A panic isn't ideal, but it's much better than silently accepting and ignoring invalid input.
  • Use a String instead of &'static str for the Err case, so the error message is more informative, i.e. it includes the bad flag value.

Also, the commit improves the NvvmOption::from_str test.

  • Move inputs next to expected outputs, which makes it easier to read and update.
  • Add the missing compute capabilities.
  • Add some checks for invalid inputs.

compiletests lets you specify one or more target architectures. E.g. on
CI we run this:
```
cargo run -p compiletests --release --no-default-features -- \
    --target-arch compute_61,compute_70,compute_90
```
While trying out various different target architectures, including
invalid ones, I get strange results.

First, if the target arch has fewer than 8 chars (e.g. `--target-arch
blah`), then nvvm panics:
```
thread 'rustc' panicked at crates/nvvm/src/lib.rs:246:38: byte
index 8 is out of bounds of `blah`
```

Second, if the target arch has 8 or more chars but is invalid, it is
just ignored. This means invalid flags like `--target-arch compute_999`
run, but just use the default target architecture.

This commit fixes the problems.
- Fix the slicing into the `-arch=` option to avoid the bounds panic.
- In `CodegenArgs::parse`, check for the `Err` case and panic instead of
  swallowing it. A panic isn't ideal, but it's much better than silently
  accepting and ignoring invalid input.
- Use a `String` instead of `&'static str` for the `Err` case, so the
  error message is more informative, i.e. it includes the bad flag
  value.

Also, the commit improves the `NvvmOption::from_str` test.
- Move inputs next to expected outputs, which makes it easier to
  read and update.
- Add the missing compute capabilities.
- Add some checks for invalid inputs.
@nnethercote nnethercote requested a review from LegNeato November 19, 2025 02:24
Copy link
Copy Markdown
Contributor

@LegNeato LegNeato left a comment

Choose a reason for hiding this comment

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

Thanks for catching these

@LegNeato LegNeato merged commit 8c9e53a into Rust-GPU:main Nov 19, 2025
5 of 8 checks passed
@nnethercote nnethercote deleted the fix-compiletest-target-arch branch November 19, 2025 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants