Skip to content

enum SpirvTargetEnv containing all available targets #311

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
run: cargo test -p rustc_codegen_spirv --release --no-default-features --features "use-installed-tools"

- name: workspace test (excluding examples & difftest)
run: cargo test --release --workspace --exclude "example-runner-*" --exclude "difftest*" --no-default-features --features "use-installed-tools"
run: cargo test --release --workspace --exclude "example-runner-*" --exclude "difftest*" --no-default-features --features use-installed-tools,include_str,clap

# Examples
- name: cargo check examples
Expand Down
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions crates/rustc_codegen_spirv-target-specs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ repository.workspace = true
[features]
include_str = []
dir_path = []
serde = ["dep:serde"]

[dependencies]
strum = { version = "0.26.3", features = ["derive"] }
thiserror = "2.0.12"
serde = { version = "1.0", features = ["derive"], optional = true }

[dev-dependencies]
serde_json = "1.0"
139 changes: 64 additions & 75 deletions crates/rustc_codegen_spirv-target-specs/src/include_str.rs
Original file line number Diff line number Diff line change
@@ -1,75 +1,64 @@
/// Metadata for the compile targets supported by `rust-gpu`
pub const TARGET_SPECS: &[(&str, &str)] = &[
(
"spirv-unknown-opengl4.0.json",
include_str!("../target-specs/spirv-unknown-opengl4.0.json"),
),
(
"spirv-unknown-opengl4.1.json",
include_str!("../target-specs/spirv-unknown-opengl4.1.json"),
),
(
"spirv-unknown-opengl4.2.json",
include_str!("../target-specs/spirv-unknown-opengl4.2.json"),
),
(
"spirv-unknown-opengl4.3.json",
include_str!("../target-specs/spirv-unknown-opengl4.3.json"),
),
(
"spirv-unknown-opengl4.5.json",
include_str!("../target-specs/spirv-unknown-opengl4.5.json"),
),
(
"spirv-unknown-spv1.0.json",
include_str!("../target-specs/spirv-unknown-spv1.0.json"),
),
(
"spirv-unknown-spv1.1.json",
include_str!("../target-specs/spirv-unknown-spv1.1.json"),
),
(
"spirv-unknown-spv1.2.json",
include_str!("../target-specs/spirv-unknown-spv1.2.json"),
),
(
"spirv-unknown-spv1.3.json",
include_str!("../target-specs/spirv-unknown-spv1.3.json"),
),
(
"spirv-unknown-spv1.4.json",
include_str!("../target-specs/spirv-unknown-spv1.4.json"),
),
(
"spirv-unknown-spv1.5.json",
include_str!("../target-specs/spirv-unknown-spv1.5.json"),
),
(
"spirv-unknown-spv1.6.json",
include_str!("../target-specs/spirv-unknown-spv1.6.json"),
),
(
"spirv-unknown-vulkan1.0.json",
include_str!("../target-specs/spirv-unknown-vulkan1.0.json"),
),
(
"spirv-unknown-vulkan1.1.json",
include_str!("../target-specs/spirv-unknown-vulkan1.1.json"),
),
(
"spirv-unknown-vulkan1.1spv1.4.json",
include_str!("../target-specs/spirv-unknown-vulkan1.1spv1.4.json"),
),
(
"spirv-unknown-vulkan1.2.json",
include_str!("../target-specs/spirv-unknown-vulkan1.2.json"),
),
(
"spirv-unknown-vulkan1.3.json",
include_str!("../target-specs/spirv-unknown-vulkan1.3.json"),
),
(
"spirv-unknown-vulkan1.4.json",
include_str!("../target-specs/spirv-unknown-vulkan1.4.json"),
),
];
//! Metadata for the compile targets supported by `rust-gpu`

use crate::SpirvTargetEnv;

impl SpirvTargetEnv {
pub fn include_str(&self) -> &'static str {
match self {
SpirvTargetEnv::OpenGL_4_0 => {
include_str!("../target-specs/spirv-unknown-opengl4.0.json")
}
SpirvTargetEnv::OpenGL_4_1 => {
include_str!("../target-specs/spirv-unknown-opengl4.1.json")
}
SpirvTargetEnv::OpenGL_4_2 => {
include_str!("../target-specs/spirv-unknown-opengl4.2.json")
}
SpirvTargetEnv::OpenGL_4_3 => {
include_str!("../target-specs/spirv-unknown-opengl4.3.json")
}
SpirvTargetEnv::OpenGL_4_5 => {
include_str!("../target-specs/spirv-unknown-opengl4.5.json")
}
SpirvTargetEnv::Spv_1_0 => {
include_str!("../target-specs/spirv-unknown-spv1.0.json")
}
SpirvTargetEnv::Spv_1_1 => {
include_str!("../target-specs/spirv-unknown-spv1.1.json")
}
SpirvTargetEnv::Spv_1_2 => {
include_str!("../target-specs/spirv-unknown-spv1.2.json")
}
SpirvTargetEnv::Spv_1_3 => {
include_str!("../target-specs/spirv-unknown-spv1.3.json")
}
SpirvTargetEnv::Spv_1_4 => {
include_str!("../target-specs/spirv-unknown-spv1.4.json")
}
SpirvTargetEnv::Spv_1_5 => {
include_str!("../target-specs/spirv-unknown-spv1.5.json")
}
SpirvTargetEnv::Spv_1_6 => {
include_str!("../target-specs/spirv-unknown-spv1.6.json")
}
SpirvTargetEnv::Vulkan_1_0 => {
include_str!("../target-specs/spirv-unknown-vulkan1.0.json")
}
SpirvTargetEnv::Vulkan_1_1 => {
include_str!("../target-specs/spirv-unknown-vulkan1.1.json")
}
SpirvTargetEnv::Vulkan_1_1_Spv_1_4 => {
include_str!("../target-specs/spirv-unknown-vulkan1.1spv1.4.json")
}
SpirvTargetEnv::Vulkan_1_2 => {
include_str!("../target-specs/spirv-unknown-vulkan1.2.json")
}
SpirvTargetEnv::Vulkan_1_3 => {
include_str!("../target-specs/spirv-unknown-vulkan1.3.json")
}
SpirvTargetEnv::Vulkan_1_4 => {
include_str!("../target-specs/spirv-unknown-vulkan1.4.json")
}
}
Comment on lines +7 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

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

Quick question since we keep changing these things, since I went with JSON files to minimal deviation from previous behavior, and because they wouldn't cause spurious rebuilds (relative to the spirv-builder version being used, since I would expect shaders to be rebuilt most of the times spirv-builder itself is rebuilt):

Could cargo-gpu generate the JSON file on-demand (and maybe cache it)? Especially since there's include_str! going on here, I feel like that's already what's happening?

Because the only difference between these files is, for spirv-unknown-X:

  • env is set to X
  • llvm-target is set to spirv-unknown-X
$ echo crates/rustc_codegen_spirv-target-specs/target-specs/*.json | xargs -n1 diff -U0 crates/rustc_codegen_spirv-targe
t-specs/target-specs/spirv-unknown-spv1.0.json | rg '^[\-+] ' -r '' | sed 's/: .*/: .../' | sort -u
 "env": ...
 "llvm-target": ...

(run this without sed 's/: .*/: .../' to see the actual values)

They could even be generated by string interpolation, if not serde_json, since the target env name doesn't even need escaping!

Copy link
Member Author

Choose a reason for hiding this comment

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

cargo-gpu only needs them for the "legacy path" aka rustc_codegen_spirv versions which did not depend on *target-specs. And that "legacy" dependency is fixed to version 0.9 on crates, so technically these changes don't actually do anything. It just feels wrong not to adjust them as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, I'm not 100% sure what's going on, are you saying that most of the time, the .json files in the source tree get used?

I would actually be happier if cargo-gpu/spirv-builder created these from one template - I don't mind rustc_codegen_spirv-target-specs existing (and we can release older versions of that crate, if we want, I guess?) - but its main job could be "take this target_env string and return a JSON blob", without having the result of that templating be baked in the way it is today.

Copy link
Member Author

@Firestar99 Firestar99 Jul 1, 2025

Choose a reason for hiding this comment

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

Wow, I actually wrote some nice docs for what's going on: https://github.com/Rust-GPU/cargo-gpu/blob/main/crates/cargo-gpu/src/target_specs.rs

And I get that they actually don't change much, just something I didn't know / want to worry about back when I wrote this.

}
}
168 changes: 166 additions & 2 deletions crates/rustc_codegen_spirv-target-specs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,174 @@
#![doc = include_str!("../README.md")]

use core::str::FromStr;
use std::fmt::{Debug, Display, Formatter};
use strum::{Display, EnumIter, EnumString, IntoStaticStr};
use thiserror::Error;

/// directory with all the `target-specs` jsons for our codegen backend
#[cfg(feature = "dir_path")]
pub const TARGET_SPEC_DIR_PATH: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/target-specs");

#[cfg(feature = "include_str")]
mod include_str;
#[cfg(feature = "include_str")]
pub use include_str::TARGET_SPECS;
#[cfg(feature = "serde")]
mod serde_feature;
#[cfg(feature = "serde")]
pub use serde_feature::*;

pub const SPIRV_ARCH: &str = "spirv";
pub const SPIRV_VENDOR: &str = "unknown";
pub const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";

/// All target envs rust-gpu supports. The corresponding target tripple is `spirv-unknown-ENV` with `ENV` replaced by any of the values below.
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, EnumString, IntoStaticStr, EnumIter, Display)]
pub enum SpirvTargetEnv {
#[strum(to_string = "opengl4.0")]
OpenGL_4_0,
#[strum(to_string = "opengl4.1")]
OpenGL_4_1,
#[strum(to_string = "opengl4.2")]
OpenGL_4_2,
#[strum(to_string = "opengl4.3")]
OpenGL_4_3,
#[strum(to_string = "opengl4.5")]
OpenGL_4_5,
#[strum(to_string = "spv1.0")]
Spv_1_0,
#[strum(to_string = "spv1.1")]
Spv_1_1,
#[strum(to_string = "spv1.2")]
Spv_1_2,
#[strum(to_string = "spv1.3")]
Spv_1_3,
#[strum(to_string = "spv1.4")]
Spv_1_4,
#[strum(to_string = "spv1.5")]
Spv_1_5,
#[strum(to_string = "spv1.6")]
Spv_1_6,
#[strum(to_string = "vulkan1.0")]
Vulkan_1_0,
#[strum(to_string = "vulkan1.1")]
Vulkan_1_1,
#[strum(to_string = "vulkan1.1spv1.4")]
Vulkan_1_1_Spv_1_4,
#[strum(to_string = "vulkan1.2")]
Vulkan_1_2,
#[strum(to_string = "vulkan1.3")]
Vulkan_1_3,
#[strum(to_string = "vulkan1.4")]
Vulkan_1_4,
}

#[derive(Clone, Error, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SpirvTargetParseError {
#[error("Expected `rustc_codegen_spirv` target with prefix `{SPIRV_TARGET_PREFIX}`, got `{0}`")]
WrongPrefix(String),
#[error(
"Target `{SPIRV_TARGET_PREFIX}{0}` not supported by `rustc_codegen_spirv`, see `enum SpirvTargetEnv` for possible env values"
)]
UnknownEnv(String),
}

impl Debug for SpirvTargetParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
}

impl SpirvTargetEnv {
pub fn parse_triple(target: &str) -> Result<Self, SpirvTargetParseError> {
let env = target
.strip_prefix(SPIRV_TARGET_PREFIX)
.ok_or_else(|| SpirvTargetParseError::WrongPrefix(target.to_string()))?;
FromStr::from_str(env).map_err(|_e| SpirvTargetParseError::UnknownEnv(env.to_string()))
}

pub fn as_str(&self) -> &'static str {
self.into()
}

pub fn target_triple(&self) -> String {
format!("{SPIRV_TARGET_PREFIX}{}", self.as_str())
}

pub fn target_json_file_name(&self) -> String {
format!("{SPIRV_TARGET_PREFIX}{}.json", self.as_str())
}

#[cfg(feature = "dir_path")]
pub fn target_json_path(&self) -> String {
format!(
"{TARGET_SPEC_DIR_PATH}/{SPIRV_TARGET_PREFIX}{}.json",
self.as_str()
)
}

pub fn iter() -> impl DoubleEndedIterator<Item = Self> {
<Self as strum::IntoEnumIterator>::iter()
}
}

pub trait IntoSpirvTarget: Sized {
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError>;
}

impl IntoSpirvTarget for SpirvTargetEnv {
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError> {
Ok(*self)
}
}

impl IntoSpirvTarget for &str {
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError> {
SpirvTargetEnv::parse_triple(self)
}
}

impl IntoSpirvTarget for String {
fn to_spirv_target_env(&self) -> Result<SpirvTargetEnv, SpirvTargetParseError> {
SpirvTargetEnv::parse_triple(self)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
pub fn test_prefix() {
assert_eq!(
SPIRV_TARGET_PREFIX,
&format!("{SPIRV_ARCH}-{SPIRV_VENDOR}-")
);
}

#[test]
pub fn test_triple_parse_roundtrip() {
for target in SpirvTargetEnv::iter() {
let parsed = SpirvTargetEnv::parse_triple(&target.target_triple()).unwrap();
assert_eq!(target, parsed);
}
}

#[test]
#[cfg(feature = "dir_path")]
pub fn test_target_json_path() {
for target in SpirvTargetEnv::iter() {
let file = std::path::PathBuf::from(target.target_json_path());
assert!(file.is_file(), "{}", file.display());
}
}

#[test]
#[cfg(all(feature = "dir_path", feature = "include_str"))]
pub fn test_target_json_content() {
for target in SpirvTargetEnv::iter() {
let content = std::fs::read_to_string(target.target_json_path()).unwrap();
assert_eq!(content, target.include_str());
}
}
}
Loading