Skip to content

Commit 470ae9a

Browse files
committed
Get tests to work both with installed and compiled tools
1 parent f000ad8 commit 470ae9a

File tree

15 files changed

+137
-68
lines changed

15 files changed

+137
-68
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
- run: rustup component add rustfmt clippy rust-src rustc-dev llvm-tools-preview
5959
- run: cargo fmt --all -- --check
6060
- run: cargo clippy --workspace --all-targets -- -D warnings
61+
6162
cargo-deny:
6263
runs-on: ubuntu-latest
6364
steps:

rustc_codegen_spirv/src/link.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,12 @@ 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-
self,
160-
Optimizer,
161-
}};
158+
use spirv_tools::{
159+
error,
160+
opt::{self, Optimizer},
161+
};
162162

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;
163+
let mut optimizer = opt::create(None);
167164

168165
optimizer
169166
.register_size_passes()
@@ -172,7 +169,7 @@ fn do_spirv_opt(sess: &Session, spv_binary: Vec<u32>, filename: &Path) -> Vec<u3
172169

173170
let result = optimizer.optimize(
174171
&spv_binary,
175-
&mut |msg: error::Message<'_>| {
172+
&mut |msg: error::Message| {
176173
use error::MessageLevel as Level;
177174

178175
// TODO: Adds spans here? Not sure how useful with binary, but maybe?
@@ -203,9 +200,9 @@ fn do_spirv_opt(sess: &Session, spv_binary: Vec<u32>, filename: &Path) -> Vec<u3
203200
}
204201

205202
fn do_spirv_val(sess: &Session, spv_binary: &[u32], filename: &Path) {
206-
use spirv_tools::val;
203+
use spirv_tools::val::{self, Validator};
207204

208-
let validator = val::Validator::new(spirv_tools::TargetEnv::default());
205+
let validator = val::create(None);
209206

210207
if validator.validate(spv_binary, None).is_err() {
211208
let mut err = sess.struct_err("error occurred during validation");

rustc_codegen_spirv/src/linker/test.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ impl<'a> std::fmt::Debug for PrettyString<'a> {
1313
}
1414

1515
fn assemble_spirv(spirv: &str) -> Result<Vec<u8>> {
16-
use spirv_tools::assembler;
16+
use spirv_tools::assembler::{self, Assembler};
1717

18-
let assembler = assembler::Assembler::new(spirv_tools::default_target_env());
18+
let assembler = assembler::create(None);
1919

2020
let spv_binary = assembler.assemble(spirv, assembler::AssemblerOptions::default())?;
2121

@@ -25,10 +25,12 @@ fn assemble_spirv(spirv: &str) -> Result<Vec<u8>> {
2525

2626
#[allow(unused)]
2727
fn validate(spirv: &[u32]) {
28-
let validator = spirv_tools::val::Validator::new(spirv_tools::default_target_env());
28+
use spirv_tools::val::{self, Validator};
29+
30+
let validator = val::create(None);
2931

3032
validator
31-
.validate(spirv, &spirv_tools::val::ValidatorOptions::default())
33+
.validate(spirv, None)
3234
.expect("validation error occurred");
3335
}
3436

spirv-builder/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ authors = ["Embark <[email protected]>"]
55
edition = "2018"
66
license = "MIT OR Apache-2.0"
77

8-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9-
108
[dependencies]
119
memchr = "2.3"
1210
raw-string = "0.3.5"

spirv-tools/examples/as.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ fn main() {
3939
preserve_numeric_ids: args.preserve_ids,
4040
};
4141

42-
let assembler = assembler::compiled::CompiledAssembler::default();
42+
let assembler =
43+
assembler::compiled::CompiledAssembler::with_env(args.target_env.unwrap_or_default());
4344

4445
match assembler.assemble(&contents, assembler_opts) {
4546
Ok(binary) => {

spirv-tools/src/assembler.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,18 @@ pub trait Assembler: Default {
3333
text: &str,
3434
options: AssemblerOptions,
3535
) -> Result<crate::binary::Binary, crate::error::Error>;
36-
}
36+
}
37+
38+
pub fn create(te: Option<crate::TargetEnv>) -> impl Assembler {
39+
let target_env = te.unwrap_or_default();
40+
41+
#[cfg(feature = "use-compiled")]
42+
{
43+
compiled::CompiledAssembler::with_env(target_env)
44+
}
45+
46+
#[cfg(all(feature = "use-installed", not(feature = "use-compiled")))]
47+
{
48+
tool::ToolAssembler::with_env(target_env)
49+
}
50+
}

spirv-tools/src/assembler/compiled.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ impl Assembler for CompiledAssembler {
4444
});
4545
}
4646

47-
Ok(crate::binary::Binary::External(crate::binary::external::ExternalBinary::new(binary)))
47+
let bin = crate::binary::external::ExternalBinary::new(binary);
48+
Ok(crate::binary::Binary::External(bin))
4849
}
4950
other => Err(crate::error::Error {
5051
inner: other,

spirv-tools/src/assembler/tool.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use super::Assembler;
66

77
impl Assembler for ToolAssembler {
88
fn with_env(target_env: crate::TargetEnv) -> Self {
9-
Self {
10-
target_env,
11-
}
9+
Self { target_env }
1210
}
1311

1412
fn assemble(
@@ -25,7 +23,8 @@ impl Assembler for ToolAssembler {
2523

2624
cmd.arg("-o").arg("-");
2725

28-
let cmd_output = crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Retrieve)?;
26+
let cmd_output =
27+
crate::cmd::exec(cmd, Some(text.as_bytes()), crate::cmd::Output::Retrieve)?;
2928

3029
use std::convert::TryFrom;
3130
crate::binary::Binary::try_from(cmd_output.binary)

spirv-tools/src/cmd.rs

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,26 @@ impl From<CmdError> for crate::error::Error {
2121
use crate::SpirvResult;
2222

2323
match ce {
24-
CmdError::BinaryNotFound(e) => {
25-
Self {
26-
inner: SpirvResult::Unsupported,
27-
diagnostic: Some(format!("failed spawn executable: {}", e).into()),
28-
}
29-
}
30-
CmdError::Io(e) => {
31-
Self {
32-
inner: SpirvResult::EndOfStream,
33-
diagnostic: Some(format!("i/o error occurred communicating with executable: {}", e).into()),
34-
}
35-
}
36-
CmdError::ToolErrors { exit_code: _, messages } => {
24+
CmdError::BinaryNotFound(e) => Self {
25+
inner: SpirvResult::Unsupported,
26+
diagnostic: Some(format!("failed spawn executable: {}", e).into()),
27+
},
28+
CmdError::Io(e) => Self {
29+
inner: SpirvResult::EndOfStream,
30+
diagnostic: Some(
31+
format!("i/o error occurred communicating with executable: {}", e).into(),
32+
),
33+
},
34+
CmdError::ToolErrors {
35+
exit_code: _,
36+
messages,
37+
} => {
3738
// The C API just puts the last message as the diagnostic, so just do the
3839
// same for now
39-
let diagnostic = messages.into_iter().last().map(crate::error::Diagnostic::from);
40+
let diagnostic = messages
41+
.into_iter()
42+
.last()
43+
.map(crate::error::Diagnostic::from);
4044

4145
Self {
4246
inner: SpirvResult::InternalError, // this isn't really correct
@@ -172,14 +176,17 @@ pub fn exec(
172176
}
173177

174178
if retrieve_output {
179+
// Handle case where there is a '\n' in the stream, but it's not the
180+
// end of an output message
181+
if !maybe_msg || messages.is_empty() && !binary.is_empty() {
182+
binary.push(b'\n');
183+
}
184+
175185
binary.extend_from_slice(line);
176186
}
177187

178188
maybe_msg = false;
179189
}
180190

181-
Ok(CmdOutput {
182-
binary,
183-
messages,
184-
})
191+
Ok(CmdOutput { binary, messages })
185192
}

spirv-tools/src/error.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use spirv_tools_sys::{diagnostics, shared};
22

3-
pub use shared::SpirvResult;
43
pub use diagnostics::MessageLevel;
4+
pub use shared::SpirvResult;
55

66
#[derive(Debug, PartialEq)]
77
pub struct Error {
@@ -66,7 +66,7 @@ impl std::convert::TryFrom<*mut diagnostics::Diagnostic> for Diagnostic {
6666
}
6767
}
6868

69-
impl From<String> for Diagnostic{
69+
impl From<String> for Diagnostic {
7070
fn from(message: String) -> Self {
7171
Self {
7272
line: 0,
@@ -90,6 +90,7 @@ impl From<Message> for Diagnostic {
9090
}
9191
}
9292

93+
#[derive(Debug)]
9394
pub struct Message {
9495
pub level: MessageLevel,
9596
pub source: Option<String>,
@@ -100,6 +101,7 @@ pub struct Message {
100101
}
101102

102103
impl Message {
104+
#[cfg(feature = "use-installed")]
103105
pub(crate) fn fatal(message: String) -> Self {
104106
Self {
105107
level: MessageLevel::Fatal,
@@ -133,7 +135,11 @@ impl Message {
133135

134136
Self {
135137
level,
136-
source: if source.is_empty() { None } else { Some(source.into_owned()) },
138+
source: if source.is_empty() {
139+
None
140+
} else {
141+
Some(source.into_owned())
142+
},
137143
line,
138144
column,
139145
index,
@@ -142,6 +148,7 @@ impl Message {
142148
}
143149
}
144150

151+
#[cfg(feature = "use-installed")]
145152
pub(crate) fn parse(s: &str) -> Option<Self> {
146153
s.find(": ")
147154
.and_then(|i| {

spirv-tools/src/opt.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Options {
2222

2323
pub trait Optimizer {
2424
fn with_env(target_env: crate::TargetEnv) -> Self;
25-
25+
2626
fn optimize<MC: crate::error::MessageCallback>(
2727
&self,
2828
input: &[u32],
@@ -50,3 +50,17 @@ pub trait Optimizer {
5050
/// from time to time.
5151
fn register_hlsl_legalization_passes(&mut self) -> &mut Self;
5252
}
53+
54+
pub fn create(te: Option<crate::TargetEnv>) -> impl Optimizer {
55+
let target_env = te.unwrap_or_default();
56+
57+
#[cfg(feature = "use-compiled")]
58+
{
59+
compiled::CompiledOptimizer::with_env(target_env)
60+
}
61+
62+
#[cfg(all(feature = "use-installed", not(feature = "use-compiled")))]
63+
{
64+
tool::ToolOptimizer::with_env(target_env)
65+
}
66+
}

spirv-tools/src/opt/compiled.rs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ impl Drop for Options {
4444
}
4545
}
4646

47-
pub struct Optimizer {
47+
pub struct CompiledOptimizer {
4848
inner: *mut opt::Optimizer,
4949
}
5050

51-
impl Optimizer {
52-
#[inline]
53-
pub fn new(target: crate::TargetEnv) -> Self {
51+
use super::Optimizer;
52+
53+
impl Optimizer for CompiledOptimizer {
54+
fn with_env(target: crate::TargetEnv) -> Self {
5455
Self {
5556
inner: unsafe { opt::optimizer_create(target) },
5657
}
5758
}
5859

59-
#[inline]
60-
pub fn optimize<MC: error::MessageCallback>(
60+
fn optimize<MC: error::MessageCallback>(
6161
&self,
6262
input: &[u32],
6363
msg_callback: &mut MC,
@@ -122,7 +122,9 @@ impl Optimizer {
122122
});
123123
}
124124

125-
Ok(crate::binary::Binary::External(crate::binary::external::ExternalBinary::new(binary)))
125+
Ok(crate::binary::Binary::External(
126+
crate::binary::external::ExternalBinary::new(binary),
127+
))
126128
}
127129
other => Err(error::Error {
128130
inner: other,
@@ -134,7 +136,7 @@ impl Optimizer {
134136

135137
/// Register a single pass with the the optimizer.
136138
#[inline]
137-
pub fn register_pass(&mut self, pass: super::Passes) -> &mut Self {
139+
fn register_pass(&mut self, pass: super::Passes) -> &mut Self {
138140
unsafe { opt::optimizer_register_pass(self.inner, pass) }
139141
self
140142
}
@@ -143,7 +145,7 @@ impl Optimizer {
143145
/// This sequence of passes is subject to constant review and will change
144146
/// from time to time.
145147
#[inline]
146-
pub fn register_performance_passes(&mut self) -> &mut Self {
148+
fn register_performance_passes(&mut self) -> &mut Self {
147149
unsafe { opt::optimizer_register_performance_passes(self.inner) }
148150
self
149151
}
@@ -152,7 +154,7 @@ impl Optimizer {
152154
/// This sequence of passes is subject to constant review and will change
153155
/// from time to time.
154156
#[inline]
155-
pub fn register_size_passes(&mut self) -> &mut Self {
157+
fn register_size_passes(&mut self) -> &mut Self {
156158
unsafe { opt::optimizer_register_size_passes(self.inner) }
157159
self
158160
}
@@ -184,19 +186,19 @@ impl Optimizer {
184186
/// This sequence of passes is subject to constant review and will change
185187
/// from time to time.
186188
#[inline]
187-
pub fn register_hlsl_legalization_passes(&mut self) -> &mut Self {
189+
fn register_hlsl_legalization_passes(&mut self) -> &mut Self {
188190
unsafe { opt::optimizer_register_hlsl_legalization_passes(self.inner) }
189191
self
190192
}
191193
}
192194

193-
impl Default for Optimizer {
195+
impl Default for CompiledOptimizer {
194196
fn default() -> Self {
195-
Self::new(crate::TargetEnv::default())
197+
Self::with_env(crate::TargetEnv::default())
196198
}
197199
}
198200

199-
impl Drop for Optimizer {
201+
impl Drop for CompiledOptimizer {
200202
#[inline]
201203
fn drop(&mut self) {
202204
unsafe { opt::optimizer_destroy(self.inner) }

0 commit comments

Comments
 (0)