This is an archive of the discontinued LLVM Phabricator instance.

[MCP] Do not remove redundant copy for COPY from undef
ClosedPublic

Authored by Pierre-vh on Jun 9 2023, 12:29 AM.

Details

Summary

I don't think we can safely remove the second COPY as redundant in such cases.
The first COPY (which has undef src) may be lowered to a KILL instruction instead, resulting in no COPY being emitted at all.

Testcase is X86 so it's in the same place as other testcases for this function, but this was initially spotted on AMDGPU with the following:

renamable $vgpr24 = PRED_COPY undef renamable $vgpr25, implicit $exec
renamable $vgpr24 = PRED_COPY killed renamable $vgpr25, implicit $exec

The second COPY waas removed as redundant, and the first one was lowered to a KILL (= removed too), causing $vgpr24 to not have $vgpr25's value.

Fixes SWDEV-401507

Diff Detail

Event Timeline

Pierre-vh created this revision.Jun 9 2023, 12:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2023, 12:29 AM
Pierre-vh requested review of this revision.Jun 9 2023, 12:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 9 2023, 12:29 AM
arsenm added inline comments.Jun 9 2023, 4:05 AM
llvm/test/CodeGen/X86/machine-copy-prop.mir
232

I don’t see why this test isn’t failing the verifier. There’s no def of rdi, so the second and third uses should also be undef.

For the one read is undef and later are not, there still needs to be a live in of some kind

arsenm added inline comments.Jun 9 2023, 4:12 AM
llvm/test/CodeGen/X86/machine-copy-prop.mir
227

I think tracksRegLiveness strikes again (why is this an option?)

Pierre-vh added inline comments.Jun 9 2023, 4:20 AM
llvm/test/CodeGen/X86/machine-copy-prop.mir
227

Indeed, it's because tracksRegLiveness is false. If I set it to true it indeed fails the verifier.
I just copied and slightly modified a test in the same file, so I think it's alright. Would you prefer if I changed it? I guess I just need an implicit def of $rdi

foad added a comment.Jun 9 2023, 4:24 AM

Code change LGTM.

arsenm added inline comments.Jun 9 2023, 4:25 AM
llvm/test/CodeGen/X86/machine-copy-prop.mir
227

Then all the tests are broken. They should all enable liveness

arsenm added inline comments.Jun 9 2023, 4:42 AM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
466

Instead of not doing the fold, can you just strip the undef from the first copy?

Pierre-vh added inline comments.Jun 9 2023, 5:00 AM
llvm/lib/CodeGen/MachineCopyPropagation.cpp
466

I think it should be the same in the end, but`DestSourcePair` only returns const operands, so I can't modify the undef flag from the previous COPY unless I change the members of that class to be non-const, or I directly access MI.getOperand(1) but then it defeats the purpose of having an abstraction over COPY instrs operands, IMO

I also think in the end it doesn't really matter, the undef COPY just gets deleted anyway. This is such a niche case already (no tests covered it, and it's only been discovered now randomly) that I would say let's not overthink it, maybe let's just add a TODO for it?

Pierre-vh updated this revision to Diff 529918.Jun 9 2023, 5:00 AM
Pierre-vh marked 3 inline comments as done.

Fix liveness in test

arsenm accepted this revision.Jun 9 2023, 5:02 AM
This revision is now accepted and ready to land.Jun 9 2023, 5:02 AM
This revision was landed with ongoing or failed builds.Jun 9 2023, 5:24 AM
This revision was automatically updated to reflect the committed changes.