Skip to content

Commit 032286e

Browse files
authored
Add a spirv-builder option to include all debug info (#742)
1 parent 66d6c55 commit 032286e

File tree

5 files changed

+53
-33
lines changed

5 files changed

+53
-33
lines changed

crates/rustc_codegen_spirv/src/codegen_cx/mod.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,21 @@ impl<'tcx> CodegenCx<'tcx> {
228228
}
229229
}
230230

231+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
232+
pub enum SpirvMetadata {
233+
None,
234+
NameVariables,
235+
Full,
236+
}
237+
231238
pub struct CodegenArgs {
232239
pub module_output_type: ModuleOutputType,
233240
pub disassemble: bool,
234241
pub disassemble_fn: Option<String>,
235242
pub disassemble_entry: Option<String>,
236243
pub disassemble_globals: bool,
237244

238-
pub name_variables: bool,
245+
pub spirv_metadata: SpirvMetadata,
239246

240247
// spirv-val flags
241248
pub relax_struct_store: bool,
@@ -273,12 +280,7 @@ impl CodegenArgs {
273280
);
274281
opts.optflagopt("", "disassemble-globals", "print globals to stderr", "");
275282

276-
opts.optflagopt(
277-
"",
278-
"name-variables",
279-
"Keep OpName for OpVariables, strip all others.",
280-
"",
281-
);
283+
opts.optopt("", "spirv-metadata", "how much metadata to include", "");
282284

283285
opts.optflagopt("", "relax-struct-store", "Allow store from one struct type to a different type with compatible layout and members.", "");
284286
opts.optflagopt("", "relax-logical-pointer", "Allow allocating an object of a pointer type and returning a pointer value from a function in logical addressing mode", "");
@@ -295,7 +297,7 @@ impl CodegenArgs {
295297
let disassemble_entry = matches.opt_str("disassemble-entry");
296298
let disassemble_globals = matches.opt_present("disassemble-globals");
297299

298-
let name_variables = matches.opt_present("name-variables");
300+
let spirv_metadata = matches.opt_str("spirv-metadata");
299301

300302
let relax_struct_store = matches.opt_present("relax-struct-store");
301303
let relax_logical_pointer = matches.opt_present("relax-logical-pointer");
@@ -306,14 +308,25 @@ impl CodegenArgs {
306308

307309
let relax_block_layout = if relax_block_layout { Some(true) } else { None };
308310

311+
let spirv_metadata = match spirv_metadata.as_deref() {
312+
None => SpirvMetadata::None,
313+
Some("full") => SpirvMetadata::Full,
314+
Some("name-variables") => SpirvMetadata::NameVariables,
315+
Some(v) => {
316+
return Err(rustc_session::getopts::Fail::UnrecognizedOption(
317+
v.to_string(),
318+
))
319+
}
320+
};
321+
309322
Ok(Self {
310323
module_output_type,
311324
disassemble,
312325
disassemble_fn,
313326
disassemble_entry,
314327
disassemble_globals,
315328

316-
name_variables,
329+
spirv_metadata,
317330

318331
relax_struct_store,
319332
relax_logical_pointer,

crates/rustc_codegen_spirv/src/link.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::codegen_cx::{CodegenArgs, ModuleOutputType};
1+
use crate::codegen_cx::{CodegenArgs, ModuleOutputType, SpirvMetadata};
22
use crate::{
33
linker, CompileResult, ModuleResult, SpirvCodegenBackend, SpirvModuleBuffer, SpirvThinBuffer,
44
};
@@ -210,7 +210,7 @@ fn post_link_single_module(
210210
};
211211

212212
let spv_binary = if sess.opts.optimize != OptLevel::No
213-
|| (sess.opts.debuginfo == DebugInfo::None && !cg_args.name_variables)
213+
|| (sess.opts.debuginfo == DebugInfo::None && cg_args.spirv_metadata == SpirvMetadata::None)
214214
{
215215
if env::var("NO_SPIRV_OPT").is_err() {
216216
let _timer = sess.timer("link_spirv_opt");
@@ -273,7 +273,7 @@ fn do_spirv_opt(
273273
}
274274
}
275275

276-
if sess.opts.debuginfo == DebugInfo::None && !cg_args.name_variables {
276+
if sess.opts.debuginfo == DebugInfo::None && cg_args.spirv_metadata == SpirvMetadata::None {
277277
optimizer
278278
.register_pass(opt::Passes::EliminateDeadConstant)
279279
.register_pass(opt::Passes::StripDebugInfo);
@@ -528,7 +528,7 @@ fn do_link(
528528
compact_ids: env::var("NO_COMPACT_IDS").is_err(),
529529
structurize: env::var("NO_STRUCTURIZE").is_err(),
530530
emit_multiple_modules: cg_args.module_output_type == ModuleOutputType::Multiple,
531-
name_variables: cg_args.name_variables,
531+
spirv_metadata: cg_args.spirv_metadata,
532532
};
533533

534534
let link_result = linker::link(sess, modules, &options);

crates/rustc_codegen_spirv/src/linker/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ mod specializer;
1515
mod structurizer;
1616
mod zombies;
1717

18+
use crate::codegen_cx::SpirvMetadata;
1819
use crate::decorations::{CustomDecoration, UnrollLoopsDecoration};
1920
use rspirv::binary::{Assemble, Consumer};
2021
use rspirv::dr::{Block, Instruction, Loader, Module, ModuleHeader, Operand};
@@ -30,7 +31,7 @@ pub struct Options {
3031
pub dce: bool,
3132
pub structurize: bool,
3233
pub emit_multiple_modules: bool,
33-
pub name_variables: bool,
34+
pub spirv_metadata: SpirvMetadata,
3435
}
3536

3637
pub enum LinkResult {
@@ -246,7 +247,7 @@ pub fn link(sess: &Session, mut inputs: Vec<Module>, opts: &Options) -> Result<L
246247
}
247248
}
248249

249-
if opts.name_variables {
250+
if opts.spirv_metadata == SpirvMetadata::NameVariables {
250251
let _timer = sess.timer("link_name_variables");
251252
simple_passes::name_variables_pass(&mut output);
252253
}

crates/rustc_codegen_spirv/src/linker/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{link, LinkResult, Options};
2+
use crate::codegen_cx::SpirvMetadata;
23
use pipe::pipe;
34
use rspirv::dr::{Loader, Module};
45
use rustc_driver::handle_options;
@@ -93,7 +94,7 @@ fn assemble_and_link(binaries: &[&[u8]]) -> Result<Module, String> {
9394
dce: false,
9495
structurize: false,
9596
emit_multiple_modules: false,
96-
name_variables: false,
97+
spirv_metadata: SpirvMetadata::None,
9798
},
9899
);
99100
assert_eq!(compiler.session().has_errors(), res.is_err());

crates/spirv-builder/src/lib.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ impl fmt::Display for SpirvBuilderError {
125125

126126
impl Error for SpirvBuilderError {}
127127

128-
pub enum MemoryModel {
129-
Simple,
130-
Vulkan,
131-
GLSL450,
132-
}
133-
134-
#[derive(Debug, PartialEq, Eq)]
128+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
135129
pub enum MetadataPrintout {
136130
/// Print no cargo metadata.
137131
None,
@@ -143,14 +137,25 @@ pub enum MetadataPrintout {
143137
Full,
144138
}
145139

140+
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
141+
pub enum SpirvMetadata {
142+
/// Strip all names and other debug information from SPIR-V output.
143+
None,
144+
/// Only include OpNames for public interface variables (uniforms and the like), to allow
145+
/// shader reflection.
146+
NameVariables,
147+
/// Include all OpNames for everything, and OpLines. Significantly increases binary size.
148+
Full,
149+
}
150+
146151
pub struct SpirvBuilder {
147152
path_to_crate: PathBuf,
148153
print_metadata: MetadataPrintout,
149154
release: bool,
150155
target: String,
151156
deny_warnings: bool,
152157
multimodule: bool,
153-
name_variables: bool,
158+
spirv_metadata: SpirvMetadata,
154159
capabilities: Vec<Capability>,
155160
extensions: Vec<String>,
156161

@@ -172,7 +177,7 @@ impl SpirvBuilder {
172177
target: target.into(),
173178
deny_warnings: false,
174179
multimodule: false,
175-
name_variables: false,
180+
spirv_metadata: SpirvMetadata::None,
176181
capabilities: Vec::new(),
177182
extensions: Vec::new(),
178183

@@ -210,12 +215,10 @@ impl SpirvBuilder {
210215
self
211216
}
212217

213-
/// Keep `OpName` reflection information for global `OpVariable`s (which means things like
214-
/// uniforms and shader input/outputs) but strip `OpName`s for everything else (functions,
215-
/// types, and so on). This is useful if you want a small binary size without debugging
216-
/// information, but need variable name reflection to work.
217-
pub fn name_variables(mut self, v: bool) -> Self {
218-
self.name_variables = v;
218+
/// Sets the level of metadata (primarily `OpName` and `OpLine`) included in the SPIR-V binary.
219+
/// Including metadata significantly increases binary size.
220+
pub fn spirv_metadata(mut self, v: SpirvMetadata) -> Self {
221+
self.spirv_metadata = v;
219222
self
220223
}
221224

@@ -399,8 +402,10 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
399402
if builder.multimodule {
400403
llvm_args.push("--module-output=multiple");
401404
}
402-
if builder.name_variables {
403-
llvm_args.push("--name-variables");
405+
match builder.spirv_metadata {
406+
SpirvMetadata::None => (),
407+
SpirvMetadata::NameVariables => llvm_args.push("--spirv-metadata=name-variables"),
408+
SpirvMetadata::Full => llvm_args.push("--spirv-metadata=full"),
404409
}
405410
if builder.relax_struct_store {
406411
llvm_args.push("--relax-struct-store");

0 commit comments

Comments
 (0)