This repository was archived by the owner on Oct 31, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Added a transformation that gets rid of temporary composites. #690
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f745f9
Added an optimization that gets rid of temporary composites.
ElectronicRU 948ef84
Correctness fixes to transitive unused removal:
ElectronicRU 4e80784
cargo fmt
ElectronicRU a862efc
clippy
ElectronicRU fc78eb0
Make transformation per-function & rely on DCE for eliminating dead c…
ElectronicRU b835301
Forgot to mark CompositeInsert as pure & additional line cleaning
ElectronicRU 532f5a3
Rustfmt
ElectronicRU c101206
Remove duplicate lines only once
ElectronicRU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
crates/rustc_codegen_spirv/src/linker/destructure_composites.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| //! Simplify `OpCompositeExtract` pointing to `OpCompositeConstruct`s / `OpCompositeInsert`s. | ||
| //! Such constructions arise after inlining, when using multi-argument closures | ||
| //! (and other `Fn*` trait implementations). These composites can frequently be invalid, | ||
| //! containing pointers, `OpFunctionArgument`s, etc. After simplification, components | ||
| //! will become valid targets for `OpLoad`/`OpStore`. | ||
| use super::apply_rewrite_rules; | ||
| use rspirv::dr::{Function, Instruction}; | ||
| use rspirv::spirv::Op; | ||
| use rustc_data_structures::fx::FxHashMap; | ||
|
|
||
| pub fn destructure_composites(function: &mut Function) { | ||
| let mut rewrite_rules = FxHashMap::default(); | ||
| let reference: FxHashMap<_, _> = function | ||
| .all_inst_iter() | ||
| .filter_map(|inst| match inst.class.opcode { | ||
| Op::CompositeConstruct => Some((inst.result_id.unwrap(), inst.clone())), | ||
| Op::CompositeInsert if inst.operands.len() == 3 => { | ||
| Some((inst.result_id.unwrap(), inst.clone())) | ||
| } | ||
| _ => None, | ||
| }) | ||
| .collect(); | ||
| for inst in function.all_inst_iter_mut() { | ||
| if inst.class.opcode == Op::CompositeExtract && inst.operands.len() == 2 { | ||
| let mut composite = inst.operands[0].unwrap_id_ref(); | ||
| let index = inst.operands[1].unwrap_literal_int32(); | ||
|
|
||
| let origin = loop { | ||
| if let Some(inst) = reference.get(&composite) { | ||
| match inst.class.opcode { | ||
| Op::CompositeInsert => { | ||
| let insert_index = inst.operands[2].unwrap_literal_int32(); | ||
| if insert_index == index { | ||
| break Some(inst.operands[0].unwrap_id_ref()); | ||
| } | ||
| composite = inst.operands[1].unwrap_id_ref(); | ||
| } | ||
| Op::CompositeConstruct => { | ||
| break inst.operands.get(index as usize).map(|o| o.unwrap_id_ref()); | ||
| } | ||
| _ => unreachable!(), | ||
| } | ||
| } else { | ||
| break None; | ||
| } | ||
| }; | ||
|
|
||
| if let Some(origin_id) = origin { | ||
| rewrite_rules.insert( | ||
| inst.result_id.unwrap(), | ||
| rewrite_rules.get(&origin_id).map_or(origin_id, |id| *id), | ||
| ); | ||
| *inst = Instruction::new(Op::Nop, None, None, vec![]); | ||
| continue; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Transitive closure computation | ||
| let mut closed_rewrite_rules = rewrite_rules.clone(); | ||
| for (_, value) in closed_rewrite_rules.iter_mut() { | ||
| while let Some(next) = rewrite_rules.get(value) { | ||
| *value = *next; | ||
| } | ||
| } | ||
|
|
||
| // Remove instructions replaced by NOPs, as well as unused composite values. | ||
| for block in function.blocks.iter_mut() { | ||
| block | ||
| .instructions | ||
| .retain(|inst| inst.class.opcode != Op::Nop); | ||
| } | ||
| apply_rewrite_rules(&closed_rewrite_rules, &mut function.blocks); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| mod test; | ||
|
|
||
| mod dce; | ||
| mod destructure_composites; | ||
| mod duplicates; | ||
| mod import_export_link; | ||
| mod inline; | ||
|
|
@@ -27,6 +28,7 @@ pub struct Options { | |
| pub dce: bool, | ||
| pub inline: bool, | ||
| pub mem2reg: bool, | ||
| pub destructure: bool, | ||
| pub structurize: bool, | ||
| pub emit_multiple_modules: bool, | ||
| pub name_variables: bool, | ||
|
|
@@ -228,6 +230,10 @@ pub fn link(sess: &Session, mut inputs: Vec<Module>, opts: &Options) -> Result<L | |
| // mem2reg produces minimal SSA form, not pruned, so DCE the dead ones | ||
| dce::dce_phi(func); | ||
| } | ||
| if opts.destructure { | ||
| let _timer = sess.timer("link_destructure"); | ||
| destructure_composites::destructure_composites(func); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -240,11 +246,6 @@ pub fn link(sess: &Session, mut inputs: Vec<Module>, opts: &Options) -> Result<L | |
| } | ||
| } | ||
|
|
||
| { | ||
| let _timer = sess.timer("link_remove_duplicate_lines"); | ||
| duplicates::remove_duplicate_lines(&mut output); | ||
| } | ||
|
|
||
| if opts.name_variables { | ||
| let _timer = sess.timer("link_name_variables"); | ||
| simple_passes::name_variables_pass(&mut output); | ||
|
|
@@ -289,6 +290,11 @@ pub fn link(sess: &Session, mut inputs: Vec<Module>, opts: &Options) -> Result<L | |
| dce::dce(output); | ||
| } | ||
|
|
||
| { | ||
| let _timer = sess.timer("link_remove_duplicate_lines"); | ||
| duplicates::remove_duplicate_lines(output); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than running this again, please move the original one here and only run it once. The original is where it is now because none of the later passes could modify function contents, but now with #691, DCE can. |
||
|
|
||
| if opts.compact_ids { | ||
| let _timer = sess.timer("link_compact_ids"); | ||
| // compact the ids https://github.com/KhronosGroup/SPIRV-Tools/blob/e02f178a716b0c3c803ce31b9df4088596537872/source/opt/compact_ids_pass.cpp#L43 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| // build-pass | ||
|
|
||
| use spirv_std; | ||
|
|
||
| fn closure_user<F: FnMut(&u32, u32)>(ptr: &u32, xmax: u32, mut callback: F) { | ||
| for i in 0..xmax { | ||
| callback(ptr, i); | ||
| } | ||
| } | ||
|
|
||
| #[spirv(fragment)] | ||
| pub fn main(ptr: &mut u32) { | ||
| closure_user(ptr, 10, |ptr, i| { | ||
| if *ptr == i { spirv_std::arch::kill(); } | ||
| }); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.