Skip to content

Commit 823b0c2

Browse files
committed
target-spec: don't validate target env in spirv-builder
1 parent 8f197be commit 823b0c2

File tree

1 file changed

+9
-27
lines changed
  • crates/rustc_codegen_spirv-types/src

1 file changed

+9
-27
lines changed

crates/rustc_codegen_spirv-types/src/target.rs

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,33 @@ use thiserror::Error;
33

44
pub const SPIRV_TARGET_PREFIX: &str = "spirv-unknown-";
55

6-
/// A well-known rust-gpu target.
6+
/// A well-formed rust-gpu target.
77
///
8-
/// Even if [`Self::from_target`] or [`Self::from_env`] accepts the target, it can still fail to compile if the
9-
/// specific rust-gpu compiler in use offers no support for it.
10-
///
11-
/// While the current implementation is just a validated string, that may change in the future.
8+
/// The constructors [`Self::from_target`] or [`Self::from_env`] only check whether the target is well-formed, not
9+
/// whether it is valid. Since `spirv-builder` is backwards compatible with older rust-gpu compilers, only the compiler
10+
/// itself knows what targets it can and cannot support. This also allows adding new targets to the compiler without
11+
/// having to update `spirv-builder` and `cargo-gpu`.
1212
#[derive(Clone)]
1313
pub struct SpirvTarget {
1414
target: String,
1515
}
1616

1717
impl SpirvTarget {
1818
pub fn from_target(target: &str) -> Result<Self, TargetError> {
19-
let target_env = target.strip_prefix(SPIRV_TARGET_PREFIX).ok_or_else(|| {
19+
let _target_env = target.strip_prefix(SPIRV_TARGET_PREFIX).ok_or_else(|| {
2020
TargetError::NonSpirvTarget {
2121
target: target.to_string(),
2222
}
2323
})?;
24-
25-
// used only to split the full list into groups.
26-
#[allow(clippy::match_same_arms)]
27-
match target_env {
28-
// HACK(firestar99) this hardcoded list should be replaced with patterns (eg. `vulkan{}.{}`),
29-
// which would allow us to support new target versions without updating this list.
30-
"spv1.0" | "spv1.1" | "spv1.2" | "spv1.3" | "spv1.4" | "spv1.5" | "spv1.6" => {}
31-
"opengl4.0" | "opengl4.1" | "opengl4.2" | "opengl4.3" | "opengl4.5" => {}
32-
"vulkan1.0" | "vulkan1.1" | "vulkan1.1spv1.4" | "vulkan1.2" | "vulkan1.3"
33-
| "vulkan1.4" => {}
34-
35-
_ => {
36-
return Err(TargetError::UnsupportedSpirvTargetEnv {
37-
target_env: target_env.into(),
38-
});
39-
}
40-
}
41-
4224
Ok(Self {
4325
target: target.to_string(),
4426
})
4527
}
4628

4729
pub fn from_env(target_env: &str) -> Result<Self, TargetError> {
48-
Self::from_target(&format!("{SPIRV_TARGET_PREFIX}{target_env}"))
30+
Ok(Self {
31+
target: format!("{SPIRV_TARGET_PREFIX}{target_env}"),
32+
})
4933
}
5034

5135
pub fn target(&self) -> &str {
@@ -68,6 +52,4 @@ impl Debug for SpirvTarget {
6852
pub enum TargetError {
6953
#[error("SPIR-V target must start with `{SPIRV_TARGET_PREFIX}...`, was `{target}`")]
7054
NonSpirvTarget { target: String },
71-
#[error("SPIR-V target `{SPIRV_TARGET_PREFIX}-{target_env}` is not supported")]
72-
UnsupportedSpirvTargetEnv { target_env: String },
7355
}

0 commit comments

Comments
 (0)