From e3818bb23699711829892a5683f2403ee25c44e7 Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 16 Mar 2021 22:30:51 -0400 Subject: [PATCH] [spv-in] fix treating OpPhi variables as pointers --- src/front/spv/flow.rs | 3 +-- src/front/spv/mod.rs | 33 +++++++++++++++++++++------------ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/front/spv/flow.rs b/src/front/spv/flow.rs index 9eb139b28a..3a96b5405b 100644 --- a/src/front/spv/flow.rs +++ b/src/front/spv/flow.rs @@ -202,13 +202,12 @@ impl FlowGraph { for node_index in self.flow.node_indices() { let phis = std::mem::replace(&mut self.flow[node_index].phis, Vec::new()); for phi in phis.iter() { - let phi_var = &lookup_expression[&phi.id]; for &(variable_id, parent_id) in phi.variables.iter() { let variable = &lookup_expression[&variable_id]; let parent_node = &mut self.flow[self.block_to_node[&parent_id]]; parent_node.block.push(crate::Statement::Store { - pointer: phi_var.handle, + pointer: phi.pointer, value: variable.handle, }); } diff --git a/src/front/spv/mod.rs b/src/front/spv/mod.rs index bac5cd0087..234280e4fe 100644 --- a/src/front/spv/mod.rs +++ b/src/front/spv/mod.rs @@ -94,11 +94,13 @@ impl crate::TypeInner { } /// OpPhi instruction. -#[derive(Clone, Default, Debug)] +#[derive(Debug)] struct PhiInstruction { /// SPIR-V's ID. id: u32, + pointer: Handle, + /// Tuples of (variable, parent). variables: Vec<(u32, u32)>, } @@ -684,25 +686,32 @@ impl> Parser { ty: self.lookup_type.lookup(result_type_id)?.handle, init: None, }); - self.lookup_expression.insert( - result_id, - LookupExpression { - handle: expressions - .append(crate::Expression::LocalVariable(var_handle)), - type_id: result_type_id, - }, - ); + let pointer = expressions.append(crate::Expression::LocalVariable(var_handle)); + let in_count = (inst.wc - 3) / 2; let mut phi = PhiInstruction { id: result_id, - ..Default::default() + pointer, + variables: Vec::with_capacity(in_count as usize), }; - for _ in 0..(inst.wc - 3) / 2 { - phi.variables.push((self.next()?, self.next()?)); + for _ in 0..in_count { + let source_id = self.next()?; + let value = self.next()?; + phi.variables.push((source_id, value)); } phis.push(phi); emitter.start(expressions); + + // Associate the lookup with an actual value, which is emitted + // into the current block. + self.lookup_expression.insert( + result_id, + LookupExpression { + handle: expressions.append(crate::Expression::Load { pointer }), + type_id: result_type_id, + }, + ); } Op::AccessChain => { struct AccessExpression {