Skip to content

Commit

Permalink
add decoration-id rewrite to inliner
Browse files Browse the repository at this point in the history
  • Loading branch information
SiebenCorgie committed May 6, 2023
1 parent a64857a commit d2ec9f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
-->


### Fixed 🩹
- [PR#1059](https://github.com/EmbarkStudios/rust-gpu/pull/1059) Fix bug where the `inline` pass would not rewrite `OpDecorate` IDs, which would subsequently loose those decorations in later DCE passes.

## [0.7.0]

### Added ⭐
Expand Down
21 changes: 21 additions & 0 deletions crates/rustc_codegen_spirv/src/linker/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub fn inline(sess: &Session, module: &mut Module) -> super::Result<()> {
let mut inliner = Inliner {
header: module.header.as_mut().unwrap(),
types_global_values: &mut module.types_global_values,
annotations: &mut module.annotations,
void,
functions: &functions,
legal_globals: &legal_globals,
Expand Down Expand Up @@ -337,6 +338,7 @@ fn should_inline(
struct Inliner<'m, 'map> {
header: &'m mut ModuleHeader,
types_global_values: &'m mut Vec<Instruction>,
annotations: &'m mut Vec<Instruction>,
void: Word,
functions: &'map FunctionMap,
legal_globals: &'map FxHashMap<Word, LegalGlobal>,
Expand All @@ -350,6 +352,24 @@ impl Inliner<'_, '_> {
result
}

///Applies all rewrite rules to the decorations in the header.
fn apply_rewrite_for_decorations(&mut self, rewrite_rules: &FxHashMap<Word, Word>) {
// NOTE(siebencorgie): We don't care *what* decoration we rewrite atm. AFAIK there is no case where rewriting
// the decoration on inline wouldn't be valid.
for annotation_idx in 0..self.annotations.len() {
if self.annotations[annotation_idx].class.opcode == Op::Decorate {
if let Some(id) = self.annotations[annotation_idx].operands[0].id_ref_any_mut() {
if let Some(&rewrite) = rewrite_rules.get(id) {
//Copy decoration instruction and push it.
let mut instcpy = self.annotations[annotation_idx].clone();
*instcpy.operands[0].id_ref_any_mut().unwrap() = rewrite;
self.annotations.push(instcpy);
}
}
}
}
}

fn ptr_ty(&mut self, pointee: Word) -> Word {
// TODO: This is horribly slow, fix this
let existing = self.types_global_values.iter().find(|inst| {
Expand Down Expand Up @@ -448,6 +468,7 @@ impl Inliner<'_, '_> {
// fn is inlined multiple times.
self.add_clone_id_rules(&mut rewrite_rules, &inlined_callee_blocks);
apply_rewrite_rules(&rewrite_rules, &mut inlined_callee_blocks);
self.apply_rewrite_for_decorations(&rewrite_rules);

// Split the block containing the `OpFunctionCall` into pre-call vs post-call.
let pre_call_block_idx = block_idx;
Expand Down

0 comments on commit d2ec9f6

Please sign in to comment.