-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathlib.rs
More file actions
174 lines (152 loc) · 5.08 KB
/
lib.rs
File metadata and controls
174 lines (152 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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 = "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());
}
}
}