This is an archive of the discontinued LLVM Phabricator instance.

[BranchFolding] Fix PR43964 about branch folder not being debug invariant
ClosedPublic

Authored by bjope on Nov 11 2019, 9:33 AM.

Details

Summary

The fix in BranchFolder related to non debug invariant problems
done in commit ec32dff0b075055 actually introduced some new
problems with debug invariance.

Before that patch ComputeCommonTailLength would move iterators
back, past debug instructions, in order to make ProfitableToMerge
make consistent answers "when one block differs from the other
only by whether debugging pseudos are present at the beginning".
But the changes in ec32dff0b075055 undid that by moving the iterators
forward again.

This patch refactors ComputeCommonTailLength. The function was
really complex, considering that the SkipTopCFIAndReturn part
always moved the iterators forward to the first "real" instruction
in the found tail after ec32dff0b075055.

The patch also restores the logic to "back past possible debugging
pseudos at beginning of block" to make sure ProfitableToMerge
give consistent answers independent of DBG_VALUE instructions
before the tail. That is now done by ProfitableToMerge instead of
being hidden as a side-effect in ComputeCommonTailLength.

Diff Detail

Event Timeline

bjope created this revision.Nov 11 2019, 9:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 11 2019, 9:33 AM
bjope updated this revision to Diff 229559.Nov 15 2019, 8:20 AM

Cleaned up. No longer WIP. Ready for review.

bjope retitled this revision from WIP: BlockFolding not debug invariant (PR43964) to [BranchFolding] Fix PR43964 about branch folder not being debug invariant.Nov 15 2019, 8:21 AM
bjope edited the summary of this revision. (Show Details)
bjope edited the summary of this revision. (Show Details)Nov 15 2019, 8:25 AM
bjope marked an inline comment as done.Nov 15 2019, 9:16 AM
bjope added inline comments.
llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
39

This is the test case that fails without the patch. On trunk there is no rewrite bb.1 will contain two MOV8mi and a RET.

This looks good to me and great refactor. To make sure I understand what's going on: by keeping a temporary iterator in ComputeCommonTailLength, and only "committing" the advance to I1/I2 when we're sure we've found some additional identical tail, this avoids all the concerns about I1/I2 ending up pointing at pseudo instructions?

llvm/lib/CodeGen/BranchFolding.cpp
321

I endorse the vast volume of red in this patch :)

324–326

It looks like there's a skipDebugInstructionsBackward helper in MachineBasicBlock.h already (I'd never heard of it before I went digging). Is it worth reusing that -- it does have a different return value once it hits 'begin'.

325

*not* satisfying?

338–343

while (true)?

349–350

Presumably if we pass isIdenticalTo, MBBI2 must presumably point at a duplicate INLINE_ASM instruction?

llvm/test/CodeGen/X86/branchfolding-debug-invariant.mir
2

I'm enjoying the use of DBG_VALUEs with no operands, avoiding any metdata or IR in these tests!

bjope updated this revision to Diff 230463.Nov 21 2019, 8:43 AM

Update based on review comments.

jmorse accepted this revision.Nov 21 2019, 8:46 AM

LGTM!

This revision is now accepted and ready to land.Nov 21 2019, 8:46 AM
bjope marked 6 inline comments as done.Nov 21 2019, 8:46 AM
bjope added inline comments.
llvm/lib/CodeGen/BranchFolding.cpp
349–350

Thanks, I misread this first (as if the isInlineAsm check was done if isIdenticalTo was false, but the code actually only check isInlineAsm when isIdenticalTo is true, so I've removed my extra FIXME again).

bjope marked an inline comment as done.Nov 21 2019, 8:52 AM

This looks good to me and great refactor. To make sure I understand what's going on: by keeping a temporary iterator in ComputeCommonTailLength, and only "committing" the advance to I1/I2 when we're sure we've found some additional identical tail, this avoids all the concerns about I1/I2 ending up pointing at pseudo instructions?

Yes, simply scan for the next non-pseudo instructions to compare. If they are identical "commit" the advance and continue searching. Otherwise we are done.
ComputeCommonTailLength should give the same result as the old code using that algorithm. But the old code was really hard to read and used goto.

bjope added a comment.Nov 21 2019, 8:54 AM

Thanks for the LGTM, and the earlier review comments. Your idea about using skipDebugInstructionsForward simplified things even more! :-)

This revision was automatically updated to reflect the committed changes.