This is an archive of the discontinued LLVM Phabricator instance.

[GlobalISel] fix crash in narrowScalarExtract if DstRegs only has one register
ClosedPublic

Authored by gargaroff on Mar 6 2020, 7:23 AM.

Details

Summary

When narrowing a scalar G_EXTRACT where the destination lines up perfectly with a single result of the emitted G_UNMERGE_VALUES a COPY should be emitted instead of unconditionally trying to emit a G_MERGE_VALUES.

Diff Detail

Event Timeline

gargaroff created this revision.Mar 6 2020, 7:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 6 2020, 7:23 AM
gargaroff edited the summary of this revision. (Show Details)Mar 6 2020, 7:24 AM
gargaroff added reviewers: arsenm, dsanders.
arsenm added inline comments.Mar 6 2020, 10:33 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
3688–3694

Could this just write the original result into DstReg and avoid the copy?

gargaroff added inline comments.Mar 9 2020, 2:43 AM
llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
3688–3694

Hm, the only thing we could reasonably do here to avoid the copy is to change the operand of the defining instruction of DstRegs[0]. The code for this would look something like this:

else {
  MachineInstr *DefInst = MRI.getVRegDef(DstRegs[0]);
  assert(DefInst && "DstRegs[0] should have been defined");

  unsigned Idx = DefInst->getOpcode() == TargetOpcode::G_UNMERGE_VALUES ? OpStart / NarrowSize : 0;

  DefInst->getOperand(Idx).setReg(DstReg);
}

I mean it avoids the copy I guess, but not sure if that is really better here?

I also tried to rewrite the code to just use DstReg where we want to have it in the first place, but that just clutters the code with if-checks, so also not really nice.

Let me know what you think.

arsenm accepted this revision.Mar 11 2020, 3:51 PM

I don't really love extra copies, especially since nothing is trying to get rid of them during legalization but it's not really important

This revision is now accepted and ready to land.Mar 11 2020, 3:51 PM
gargaroff closed this revision.Mar 12 2020, 1:17 AM