This is an archive of the discontinued LLVM Phabricator instance.

[RegAllocGreedy] Rematerialization crashes while updating operands in bundled MIs
ClosedPublic

Authored by dshtilman on May 20 2015, 10:39 PM.

Details

Summary

Hi Quentin,

This patch fixes rematerialization in InlineSpiller so it can handle bundles of MIs properly.

Context:
Before InlineSpiller rematerializes a vreg, it iterates over operands of each MI in a bundle, collecting all (MI, OpNo) pairs that reference that vreg.

Then if it does rematerialize, it goes through the pair list and replaces the operands with the new (rematerialized) vreg. The problem is, it tries to replace all of these operands in the main MI ! This works fine for single MIs. However, if we are processing a bundle of MIs and the list contains multiple pairs - the rematerialization will either crash trying to access a non-existing operand of the main MI, or silently corrupt one of the existing ones. It will also ignore other MIs in the bundle.

The obvious fix is to use the MI pointers saved in collected (MI, OpNo) pairs. This must have been the original intent of the pair list but somehow these pointers got lost.

Please review.

Thanks,
Dmitri

Diff Detail

Event Timeline

dshtilman updated this revision to Diff 26203.May 20 2015, 10:39 PM
dshtilman retitled this revision from to [RegAllocGreedy] Rematerialization crashes while updating operands in bundled MIs.
dshtilman updated this object.
dshtilman edited the test plan for this revision. (Show Details)
dshtilman added a reviewer: qcolombet.
dshtilman added a subscriber: resistor.
dshtilman added a subscriber: Unknown Object (MLST).
dshtilman updated this revision to Diff 26204.May 20 2015, 10:49 PM

Hi Quentin,

This patch fixes rematerialization in InlineSpiller so it can handle bundles of MIs properly.

Context:
Before InlineSpiller rematerializes a vreg, it iterates over operands of each MI in a bundle, collecting all (MI, OpNo) pairs that reference that vreg.

Then if it does rematerialize, it goes through the pair list and replaces the operands with the new (rematerialized) vreg. The problem is, it tries to replace all of these operands in the main MI ! This works fine for single MIs. However, if we are processing a bundle of MIs and the list contains multiple pairs - the rematerialization will either crash trying to access a non-existing operand of the main MI, or silently corrupt one of the existing ones. It will also ignore other MIs in the bundle.

The obvious fix is to use the MI pointers saved in collected (MI, OpNo) pairs. This must have been the original intent of the pair list but somehow these pointers got lost.

Please review.

Thanks,
Dmitri

qcolombet accepted this revision.May 21 2015, 9:43 AM
qcolombet edited edge metadata.

Hi Dmitri,

The bug is scary!

This LGTM to a few nitpicks while you are here.
Also I assume this is not triggered by any in-tree target. If it is, please add a test case.

Thanks,
-Quentin

lib/CodeGen/InlineSpiller.cpp
924

MI is not reused anywhere so just fold the expression here:
MachineOperand &MO = Ops[i].first->getOperand(Ops[i].second);

That way we won’t have the shadow variable.

1102–1103

While you are here, add an assert in that loop:
MI == Ops[i].first

This revision is now accepted and ready to land.May 21 2015, 9:43 AM
dshtilman updated this revision to Diff 26275.May 21 2015, 2:03 PM
dshtilman edited edge metadata.

Scary indeed!

I addressed your comments. This hasn't been triggered by an in-tree target,
so at this point I don't have any tests that I could add.

The loop in InlineSpiller::foldMemoryOperand() is actually guarded by

// Don't attempt folding in bundles.
MachineInstr *MI = Ops.front().first;
if (Ops.back().first != MI || MI->isBundled())
  return false;

so we're unlikely to run into similar issues in this loop.
But I agree it's good to have an extra assert here just to be sure.

Thanks,
Dmitri

Thanks Dmitri!

Do you want me to commit on your behalf?

Cheers,
-Quentin

Yes, please do.

Thanks,
Dmitri

Eugene.Zelenko added a subscriber: Eugene.Zelenko.

Committed in rL237964.