Fix miscompile due to SSA aliasing - #3775
Stefan J. Wernli (swernli) wants to merge 4 commits into
Conversation
This change updates variable mapping logic in RIR passes to differentiate between "strong" and "weak" mappings, where the former indicates the mapping should be followed through successive links until the terminator operand is reached and the latter indicates no further mapping should be performed. Fixes #3769
| if var_stor_to_keep.contains(&var.variable_id) { | ||
| // Only keep stores to variables that are in the set to keep. | ||
| *operand = operand.mapped(var_map); | ||
| } else if let Operand::Variable(mapped_var) = operand.mapped(var_map) |
There was a problem hiding this comment.
This doesn't depend on whether it's deep or shallow?
There was a problem hiding this comment.
Not directly, but operand.mapped(var_map) will follow the deep vs shallow rules, so stop at the first shallow mapping.
operation Main() : Int {
use q0 = Qubit();
use q1 = Qubit();
X(q0);
X(q1);
mutable n = 0;
if MResetZ(q0) == One {
n = 1;
}
let h = n;
if MResetZ(q1) == One {
n = 2;
}
h + n
}Expected: 3 Speculative explanation (copilot): At ssa_transform.rs:167, only Shallow(n) aliases are redirected when the earlier phi for n is created. An alias that is still Deep(n) remains tied to the mutable ID. Later, ssa_transform.rs:190 can overwrite the shallow mapping created by a branch-local mutation. |
This is the resolved in the latest changes. |
This change updates variable mapping logic in RIR passes to differentiate between "deep" and "shallow" mappings, where the former indicates the mapping should be followed through successive links until the terminator operand is reached and the latter indicates no further mapping should be performed. Fixes #3769