Skip to content

Commit

Permalink
[Opt] Fix use after free (erase) in mem2reg (#628)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex McCaskey <amccaskey@nvidia.com>
  • Loading branch information
boschmitt and amccaskey authored Sep 11, 2023
1 parent 77cbcd1 commit 4e63416
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/Optimizer/Transforms/MemToReg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,20 +302,21 @@ class MemToRegPass : public cudaq::opt::impl::MemToRegBase<MemToRegPass> {
processOpWithRegions(func, memAnalysis, cleanUps);

// 3) Cleanup the dead ops.
SmallVector<quake::WrapOp> wrapOps;
for (auto *op : cleanUps) {
if (auto wrap = dyn_cast<quake::WrapOp>(op))
if (auto wrap = dyn_cast<quake::WrapOp>(op)) {
wrapOps.push_back(wrap);
continue;
}
op->dropAllUses();
op->erase();
}
for (auto *op : cleanUps) {
if (auto wrap = dyn_cast<quake::WrapOp>(op)) {
auto ref = wrap.getRefValue();
auto wire = wrap.getWireValue();
if (!ref || (++wire.getUses().begin() != wire.getUses().end())) {
op->dropAllUses();
op->erase();
}
for (auto wrap : wrapOps) {
auto ref = wrap.getRefValue();
auto wire = wrap.getWireValue();
if (!ref || wire.getUses().empty()) {
wrap->dropAllUses();
wrap->erase();
}
}

Expand Down

0 comments on commit 4e63416

Please sign in to comment.