This is an archive of the discontinued LLVM Phabricator instance.

[X86] Fix the LEA optimization pass
ClosedPublic

Authored by kazu on Sep 9 2022, 10:18 PM.

Details

Summary

The LEA optimization pass visits each basic block of a given machine
function. In each basic block, for each pair of LEAs that differ only
in their displacement fields, we replace all uses of the second LEA
with the first LEA while adjusting the displacement.

Now, without this patch, after all the replacements are made, the
following assert triggers:

assert(MRI->use_empty(LastVReg) &&
       "The LEA's def register must have no uses");

The replacement loop uses:

for (MachineOperand &MO :
     llvm::make_early_inc_range(MRI->use_operands(LastVReg))) {

which is equivalent to:

for (auto UI = MRI->use_begin(LastVReg), UE = MRI->use_end();
     UI != UE;) {
  MachineOperand &MO = *UI++;  // <-- Look!

That is, immediately after the post increment, make_early_inc_range
already has the iterator for the next iteration in its mind.

The problem is that in one iteration of the loop, we could replace two
uses in a debug instruction like:

DBG_VALUE_LIST !"r", !DIExpression(DW_OP_LLVM_arg, 0), %0:gr64, %0:gr64, ...

So, the iterator for the next iteration becomes invalid. We end up
traversing a garbage use list from that point on. In turn, we don't
get to visit remaining uses.

The patch fixes the problem by switching to a "draining" while loop:

while (!MRI->use_empty(LastVReg)) {
  MachineOperand &MO = *MRI->use_begin(LastVReg);
  MachineInstr &MI = *MO.getParent();

The credit goes to Simon Pilgrim for reducing the test case.

Fixes https://github.com/llvm/llvm-project/issues/57673

Diff Detail

Event Timeline

kazu created this revision.Sep 9 2022, 10:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 10:18 PM
kazu requested review of this revision.Sep 9 2022, 10:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2022, 10:18 PM

@kazu Please can you raise a bug with your reduced test case to see if we can help get it reduced any further?

kazu added a comment.Sep 10 2022, 11:44 AM

@kazu Please can you raise a bug with your reduced test case to see if we can help get it reduced any further?

Thank you for the offer. I've filed https://github.com/llvm/llvm-project/issues/57673.

@kazu Please can you raise a bug with your reduced test case to see if we can help get it reduced any further?

Thank you for the offer. I've filed https://github.com/llvm/llvm-project/issues/57673.

I've managed a minor reduction - please can you add as a test case? We might be able to further reduce this yet.

kazu added a comment.Sep 12 2022, 2:38 PM

@kazu Please can you raise a bug with your reduced test case to see if we can help get it reduced any further?

Thank you for the offer. I've filed https://github.com/llvm/llvm-project/issues/57673.

I've managed a minor reduction - please can you add as a test case? We might be able to further reduce this yet.

Thanks a lot Simon for the reduced testcase! You made it possible to see the crash even at today's HEAD, not just the 13-day old revision 23dec4a3524aa5c9cc3cb401987014c6c5ee9de0. I'll upload the revised patch shortly.

kazu updated this revision to Diff 459562.Sep 12 2022, 2:39 PM

Added a test case.

kazu edited the summary of this revision. (Show Details)Sep 12 2022, 2:39 PM
kazu updated this revision to Diff 459564.Sep 12 2022, 2:46 PM

Added a "Fixes" message.

kazu edited the summary of this revision. (Show Details)Sep 12 2022, 2:46 PM
kazu added a comment.Sep 12 2022, 5:38 PM

Please take a look. Thanks!

kazu added a comment.Sep 14 2022, 9:06 AM

I'd appreciate a review. Thanks!

Your fix SGTM - its just a question of whether we can simplify the regression test any further

kazu added a comment.Sep 14 2022, 9:22 AM

Your fix SGTM - its just a question of whether we can simplify the regression test any further

I am not familiar, but should we consider an MIR test to precisely control what goes into the backend?

Yes that might be a good idea - we should keep the ll test file as well if we can.

llvm/test/CodeGen/X86/lea-optimize-crash.ll
3 ↗(On Diff #459564)

rename this file pr57673.ll?

kazu updated this revision to Diff 460241.Sep 14 2022, 2:55 PM

Added an MIR test case.

kazu marked an inline comment as done.Sep 14 2022, 2:59 PM

Yes that might be a good idea - we should keep the ll test file as well if we can.

I added my very first MIR test case. I hope I got it right. Tested with and without the "while" loop change.

Please take a look. Thanks!

kazu added a comment.Sep 18 2022, 10:22 AM

Please take a look. Thanks!

pengfei accepted this revision.Sep 18 2022, 5:38 PM

LGTM.

This revision is now accepted and ready to land.Sep 18 2022, 5:38 PM
This revision was automatically updated to reflect the committed changes.
kazu added a comment.Sep 18 2022, 5:50 PM

LGTM.

Thank you for reviewing the patch!