Skip to content

Commit d705a23

Browse files
authored
Add a preserve_bindings opt to the builder and codegen opts (#830)
1 parent 480cd04 commit d705a23

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

crates/rustc_codegen_spirv/src/codegen_cx/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,9 @@ pub struct CodegenArgs {
251251
pub uniform_buffer_standard_layout: bool,
252252
pub scalar_block_layout: bool,
253253
pub skip_block_layout: bool,
254+
255+
// spirv-opt flags
256+
pub preserve_bindings: bool,
254257
}
255258

256259
impl CodegenArgs {
@@ -289,6 +292,13 @@ impl CodegenArgs {
289292
opts.optflagopt("", "scalar-block-layout", "Enable VK_EXT_scalar_block_layout when checking standard uniform, storage buffer, and push constant layouts. Scalar layout rules are more permissive than relaxed block layout so in effect this will override the --relax-block-layout option.", "");
290293
opts.optflagopt("", "skip-block-layout", "Skip checking standard uniform/storage buffer layout. Overrides any --relax-block-layout or --scalar-block-layout option.", "");
291294

295+
opts.optflagopt(
296+
"",
297+
"preserve-bindings",
298+
"Preserve unused descriptor bindings. Useful for reflection.",
299+
"",
300+
);
301+
292302
let matches = opts.parse(args)?;
293303
let module_output_type =
294304
matches.opt_get_default("module-output", ModuleOutputType::Single)?;
@@ -306,6 +316,8 @@ impl CodegenArgs {
306316
let scalar_block_layout = matches.opt_present("scalar-block-layout");
307317
let skip_block_layout = matches.opt_present("skip-block-layout");
308318

319+
let preserve_bindings = matches.opt_present("preserve-bindings");
320+
309321
let relax_block_layout = if relax_block_layout { Some(true) } else { None };
310322

311323
let spirv_metadata = match spirv_metadata.as_deref() {
@@ -334,6 +346,8 @@ impl CodegenArgs {
334346
uniform_buffer_standard_layout,
335347
scalar_block_layout,
336348
skip_block_layout,
349+
350+
preserve_bindings,
337351
})
338352
}
339353

crates/rustc_codegen_spirv/src/link.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn post_link_single_module(
209209
let opt_options = spirv_tools::opt::Options {
210210
validator_options: Some(val_options.clone()),
211211
max_id_bound: None,
212-
preserve_bindings: false,
212+
preserve_bindings: cg_args.preserve_bindings,
213213
preserve_spec_constants: false,
214214
};
215215

crates/spirv-builder/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ pub struct SpirvBuilder {
166166
pub uniform_buffer_standard_layout: bool,
167167
pub scalar_block_layout: bool,
168168
pub skip_block_layout: bool,
169+
170+
// spirv-opt flags
171+
pub preserve_bindings: bool,
169172
}
170173

171174
impl SpirvBuilder {
@@ -187,6 +190,8 @@ impl SpirvBuilder {
187190
uniform_buffer_standard_layout: false,
188191
scalar_block_layout: false,
189192
skip_block_layout: false,
193+
194+
preserve_bindings: false,
190195
}
191196
}
192197

@@ -278,6 +283,12 @@ impl SpirvBuilder {
278283
self
279284
}
280285

286+
/// Preserve unused descriptor bindings. Useful for reflection.
287+
pub fn preserve_bindings(mut self, v: bool) -> Self {
288+
self.preserve_bindings = v;
289+
self
290+
}
291+
281292
/// Builds the module. If `print_metadata` is [`MetadataPrintout::Full`], you usually don't have to inspect the path
282293
/// in the result, as the environment variable for the path to the module will already be set.
283294
pub fn build(mut self) -> Result<CompileResult, SpirvBuilderError> {
@@ -424,6 +435,9 @@ fn invoke_rustc(builder: &SpirvBuilder) -> Result<PathBuf, SpirvBuilderError> {
424435
if builder.skip_block_layout {
425436
llvm_args.push("--skip-block-layout");
426437
}
438+
if builder.preserve_bindings {
439+
llvm_args.push("--preserve-bindings");
440+
}
427441
let llvm_args = join_checking_for_separators(llvm_args, " ");
428442
if !llvm_args.is_empty() {
429443
rustflags.push(["-Cllvm-args=", &llvm_args].concat());

0 commit comments

Comments
 (0)