Skip to content

Commit f000ad8

Browse files
committed
Boop
1 parent 1dc4219 commit f000ad8

File tree

23 files changed

+629
-248
lines changed

23 files changed

+629
-248
lines changed

rustc_codegen_spirv/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@ repository = "https://github.com/EmbarkStudios/rust-gpu"
1313
crate-type = ["dylib"]
1414

1515
[features]
16+
# By default, the use-compiled-tools is enabled, as doesn't require additional
17+
# setup steps for the user. This does however mean that you will need to disable
18+
# default features and explicitly enable `use-installed-tools` if you are using
19+
# this in an environment with spirv-tools in PATH, and you don't want to take
20+
# the compile time cost
21+
default = ["use-compiled-tools"]
1622
# If enabled, uses spirv-tools binaries installed in PATH, instead of
1723
# compiling and linking the spirv-tools C++ code
1824
use-installed-tools = ["spirv-tools/use-installed"]
25+
# If enabled will compile and link the C++ code for the spirv tools, the compiled
26+
# version is preferred if both this and `use-installed-tools` are enabled
27+
use-compiled-tools = ["spirv-tools/use-compiled"]
1928

2029
[dependencies]
2130
bimap = "0.5"

rustc_codegen_spirv/src/link.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,15 @@ fn link_exe(
155155
}
156156

157157
fn do_spirv_opt(sess: &Session, spv_binary: Vec<u32>, filename: &Path) -> Vec<u32> {
158-
use spirv_tools::{error, opt};
159-
160-
let mut optimizer = opt::Optimizer::new(spirv_tools::TargetEnv::default());
158+
use spirv_tools::{error, opt::{
159+
self,
160+
Optimizer,
161+
}};
162+
163+
#[cfg(feature = "use-compiled-tools")]
164+
let mut optimizer = opt::compiled::CompiledOptimizer;
165+
#[cfg(all(feature = "use-installed-tools", not(feature = "use-compiled-tools")))]
166+
let mut optimizer = opt::tool::ToolOptimizer;
161167

162168
optimizer
163169
.register_size_passes()

spirv-tools-sys/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ license = "MIT OR Apache-2.0"
1111
# preserves the types so that spirv-tools can still work without needing
1212
# to keep copies of some of the basic enums etc
1313
use-installed = []
14+
# Forces compilation of the C++ code, even if `use-installed` is enabled
15+
use-compiled = []
1416

1517
[build-dependencies]
1618
cc = { version = "1.0", features = ["parallel"] }

spirv-tools-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn val(build: &mut Build) {
219219
}
220220

221221
fn main() {
222-
if std::env::var("CARGO_FEATURE_USE_INSTALLED").is_ok() {
222+
if std::env::var("CARGO_FEATURE_USE_INSTALLED").is_ok() && std::env::var("CARGO_FEATURE_USE_COMPILED").is_err() {
223223
println!("use-installed feature on, skipping compilation of C++ code");
224224
return;
225225
}

spirv-tools-sys/src/c/opt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ extern "C" {
242242
((spvtools::Optimizer*)optimizer)->RegisterWebGPUToVulkanPasses();
243243
}
244244

245-
SPIRV_TOOLS_EXPORT void optimizer_register_legalization_passes(Optimus* optimizer) {
245+
SPIRV_TOOLS_EXPORT void optimizer_register_hlsl_legalization_passes(Optimus* optimizer) {
246246
((spvtools::Optimizer*)optimizer)->RegisterLegalizationPasses();
247247
}
248248
}

spirv-tools-sys/src/opt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ extern "C" {
582582
///
583583
/// This sequence of passes is subject to constant review and will change
584584
/// from time to time.
585-
pub fn optimizer_register_legalization_passes(opt: *mut Optimizer);
585+
pub fn optimizer_register_hlsl_legalization_passes(opt: *mut Optimizer);
586586

587587
// Some passes take arguments, so we create those separately on a
588588
// case-by-case basis

spirv-tools/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = "MIT OR Apache-2.0"
77

88
[features]
99
use-installed = ["spirv-tools-sys/use-installed", "memchr"]
10+
use-compiled = ["spirv-tools-sys/use-compiled"]
1011

1112
[dependencies]
1213
spirv-tools-sys = { path = "../spirv-tools-sys" }
@@ -18,3 +19,4 @@ structopt = "0.3"
1819

1920
[[example]]
2021
name = "as"
22+
required-features = ["use-compiled"]

spirv-tools/examples/as.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ struct Args {
1313
preserve_ids: bool,
1414
/// Use specified environment.
1515
#[structopt(long = "target-env", parse(try_from_str))]
16-
target_env: Option<spirv_tools::shared::TargetEnv>,
16+
target_env: Option<spirv_tools::TargetEnv>,
1717
/// The input file. Use '-' for stdin.
1818
#[structopt(name = "FILE")]
1919
input: String,
2020
}
2121

2222
fn main() {
23-
use spirv_tools::assembler;
23+
use spirv_tools::assembler::{self, Assembler};
2424

2525
let args = Args::from_args();
2626

@@ -39,7 +39,7 @@ fn main() {
3939
preserve_numeric_ids: args.preserve_ids,
4040
};
4141

42-
let assembler = assembler::Assembler::new(args.target_env.unwrap_or_default());
42+
let assembler = assembler::compiled::CompiledAssembler::default();
4343

4444
match assembler.assemble(&contents, assembler_opts) {
4545
Ok(binary) => {

spirv-tools/src/assembler.rs

Lines changed: 11 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use spirv_tools_sys::{assembler, shared};
1+
#[cfg(feature = "use-compiled")]
2+
pub mod compiled;
3+
4+
#[cfg(feature = "use-installed")]
5+
pub mod tool;
26

37
#[derive(Copy, Clone, Default)]
48
pub struct AssemblerOptions {
@@ -15,76 +19,18 @@ impl Into<u32> for AssemblerOptions {
1519
let mut res = 0; //assembler::BinaryOptions::None as u32;
1620

1721
if self.preserve_numeric_ids {
18-
res |= assembler::BinaryOptions::PreserveNumberIds as u32;
22+
res |= spirv_tools_sys::assembler::BinaryOptions::PreserveNumberIds as u32;
1923
}
2024

2125
res
2226
}
2327
}
2428

25-
pub struct Assembler {
26-
inner: *mut shared::ToolContext,
27-
}
28-
29-
impl Assembler {
30-
pub fn new(target_env: shared::TargetEnv) -> Self {
31-
Self {
32-
inner: unsafe { shared::context_create(target_env) },
33-
}
34-
}
35-
36-
pub fn assemble(
29+
pub trait Assembler: Default {
30+
fn with_env(target_env: crate::TargetEnv) -> Self;
31+
fn assemble(
3732
&self,
3833
text: &str,
3934
options: AssemblerOptions,
40-
) -> Result<crate::shared::Binary, crate::error::Error> {
41-
unsafe {
42-
let mut binary = std::ptr::null_mut();
43-
let mut diagnostic = std::ptr::null_mut();
44-
45-
let res = assembler::assemble(
46-
self.inner,
47-
text.as_ptr() as *const _,
48-
text.len(),
49-
options.into(),
50-
&mut binary,
51-
&mut diagnostic,
52-
);
53-
54-
// Always wrap diagnostic, it's fine if it's null
55-
use std::convert::TryFrom;
56-
let diagnostic = crate::error::Diagnostic::try_from(diagnostic).ok();
57-
58-
match res {
59-
shared::SpirvResult::Success => {
60-
if binary.is_null() {
61-
return Err(crate::error::Error {
62-
inner: shared::SpirvResult::InternalError,
63-
diagnostics: vec![crate::error::Diagnostic {
64-
line: 0,
65-
column: 0,
66-
index: 0,
67-
message: "spirv assemble indicated success but did not return a valid binary".to_owned(),
68-
is_text: true,
69-
}],
70-
});
71-
}
72-
73-
Ok(crate::shared::Binary::new(binary))
74-
}
75-
other => Err(crate::error::Error {
76-
inner: other,
77-
diagnostics: vec![diagnostic],
78-
}),
79-
}
80-
}
81-
}
82-
}
83-
84-
impl Drop for Assembler {
85-
fn drop(&mut self) {
86-
unsafe {
87-
shared::context_destroy(self.inner);
88-
}
89-
}
90-
}
35+
) -> Result<crate::binary::Binary, crate::error::Error>;
36+
}

spirv-tools/src/assembler/compiled.rs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
use spirv_tools_sys::{assembler, shared};
2+
3+
pub struct CompiledAssembler {
4+
inner: *mut shared::ToolContext,
5+
}
6+
7+
use super::Assembler;
8+
9+
impl Assembler for CompiledAssembler {
10+
fn with_env(target_env: crate::TargetEnv) -> Self {
11+
Self {
12+
inner: unsafe { shared::context_create(target_env) },
13+
}
14+
}
15+
16+
fn assemble(
17+
&self,
18+
text: &str,
19+
options: super::AssemblerOptions,
20+
) -> Result<crate::binary::Binary, crate::error::Error> {
21+
unsafe {
22+
let mut binary = std::ptr::null_mut();
23+
let mut diagnostic = std::ptr::null_mut();
24+
25+
let res = assembler::assemble(
26+
self.inner,
27+
text.as_ptr() as *const _,
28+
text.len(),
29+
options.into(),
30+
&mut binary,
31+
&mut diagnostic,
32+
);
33+
34+
// Always wrap diagnostic, it's fine if it's null
35+
use std::convert::TryFrom;
36+
let diagnostic = crate::error::Diagnostic::try_from(diagnostic).ok();
37+
38+
match res {
39+
shared::SpirvResult::Success => {
40+
if binary.is_null() {
41+
return Err(crate::error::Error {
42+
inner: shared::SpirvResult::InternalError,
43+
diagnostic: Some("spirv assemble indicated success but did not return a valid binary".to_owned().into()),
44+
});
45+
}
46+
47+
Ok(crate::binary::Binary::External(crate::binary::external::ExternalBinary::new(binary)))
48+
}
49+
other => Err(crate::error::Error {
50+
inner: other,
51+
diagnostic,
52+
}),
53+
}
54+
}
55+
}
56+
}
57+
58+
impl Default for CompiledAssembler {
59+
fn default() -> Self {
60+
Self::with_env(crate::TargetEnv::default())
61+
}
62+
}
63+
64+
impl Drop for CompiledAssembler {
65+
fn drop(&mut self) {
66+
unsafe {
67+
shared::context_destroy(self.inner);
68+
}
69+
}
70+
}

spirv-tools/src/assembler/tool.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
pub struct ToolAssembler {
2+
target_env: crate::TargetEnv,
3+
}
4+
5+
use super::Assembler;
6+
7+
impl Assembler for ToolAssembler {
8+
fn with_env(target_env: crate::TargetEnv) -> Self {
9+
Self {
10+
target_env,
11+
}
12+
}
13+
14+
fn assemble(
15+
&self,
16+
text: &str,
17+
options: super::AssemblerOptions,
18+
) -> Result<crate::binary::Binary, crate::error::Error> {
19+
let mut cmd = std::process::Command::new("spirv-as");
20+
cmd.arg("--target-env").arg(self.target_env.to_string());
21+
22+
if options.preserve_numeric_ids {
23+
cmd.arg("--preserve-numeric-ids");
24+
}
25+
26+
cmd.arg("-o").arg("-");
27+
28+
let cmd_output = crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Retrieve)?;
29+
30+
use std::convert::TryFrom;
31+
crate::binary::Binary::try_from(cmd_output.binary)
32+
}
33+
}
34+
35+
impl Default for ToolAssembler {
36+
fn default() -> Self {
37+
Self::with_env(crate::TargetEnv::default())
38+
}
39+
}

0 commit comments

Comments
 (0)