-
Notifications
You must be signed in to change notification settings - Fork 60
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
Firestar99
wants to merge
7
commits into
main
Choose a base branch
from
target_env_enum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+507
−267
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
be49382
target enum: add `enum SpirvTargetEnv` containing all targets
Firestar99 8ca7f4f
target enum: make `SpirvTargetEnv::parse_triple()` return a Result
Firestar99 be2b142
spirv-builder: clap testing
Firestar99 bb65113
target enum: serde `SpirvTargetEnv` like a target string
Firestar99 d80832e
target enum: change `SpirvBuilder.target` from String to `SpirvTarget…
Firestar99 51d17b9
target enum: propagate parsing error to `build()` to reduce breakage
Firestar99 59d108b
target enum: fix serde impl for `target` not round tripping
Firestar99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 64 additions & 75 deletions
139
crates/rustc_codegen_spirv-target-specs/src/include_str.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 timesspirv-builder
itself is rebuilt):Could
cargo-gpu
generate the JSON file on-demand (and maybe cache it)? Especially since there'sinclude_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 toX
llvm-target
is set tospirv-unknown-X
(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!There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 mindrustc_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 thistarget_env
string and return a JSON blob", without having the result of that templating be baked in the way it is today.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.