This is an archive of the discontinued LLVM Phabricator instance.

[MachineLICM][WinEH] Don't hoist register reloads out of funclets
ClosedPublic

Authored by kalle-llvm on Jun 20 2023, 6:26 AM.

Details

Summary

This fixes https://github.com/llvm/llvm-project/issues/60766

With MSVC style exception-handling (funclets), no registers are alive when entering the funclet so they must be reloaded from the stack. MachineLICM can sometimes hoist such reloads out of the funclet which is not correct, the register will have been clobbered when entering the funclet. This can happen in any loop that contains a try-catch.

This has been tested on x86_64-pc-window-msvc. I'm not sure if funclets work the same on the other windows archs.

(This is my first patch to llvm, so please bear with me.)

Diff Detail

Unit TestsFailed

Event Timeline

kalle-llvm created this revision.Jun 20 2023, 6:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 6:26 AM
kalle-llvm requested review of this revision.Jun 20 2023, 6:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 6:26 AM
smeenai added a subscriber: smeenai.

I added some appropriate reviewers. You should also include a test case though.

The concept here seems right; we should treat funclet entry as if it clobbers all registers.

I'm not sure about the implementation here; we should have some common utility that encompasses this. Making MachineLICM compute its own version of which registers are live seems liable to lead to bugs. I guess getBeginClobberMask() is supposed to encompass what this is checking for?

Needs tests

llvm/lib/CodeGen/MachineLICM.cpp
540–543

We're moving towards reverse block liveness. Modern liveness queries start at the end of the block and walk backwards, but this is walking forward looking for defs. Can this pass switch to using LiveRegUnits and stepBackwards?

kalle-llvm updated this revision to Diff 536997.Jul 4 2023, 2:17 AM

Sorry for the delay in getting back to this.

I have updated the patch to use getBeginClobberMask() and added a test-case.

rnk accepted this revision.Aug 11 2023, 9:50 AM

This looks correct to me.

llvm/lib/CodeGen/MachineLICM.cpp
540–543

That sounds like a good idea, but it seems out of scope for this bug fix.

This revision is now accepted and ready to land.Aug 11 2023, 9:50 AM
arsenm accepted this revision.Aug 11 2023, 9:52 AM
arsenm added inline comments.
llvm/lib/CodeGen/MachineLICM.cpp
540–543

Yes, but this is just a quick spot fix. For some reason a lot of machine passes invented their own buggy liveness tracking mechanisms and miss the edge cases like this one

Thanks @rnk @arsenm. Could someone land this for me? Any chance to have this in llvm 17?

Karl-Johan Johnsson <kalle@kjjohnsson.se>

Thanks @rnk @arsenm. Could someone land this for me?

Done, thanks for your contribution!

Any chance to have this in llvm 17?

I'd leave that up to @rnk and @arsenm. Unless someone says something to the contrary - I'd suggest opening a backport request according to https://llvm.org/docs/GitHub.html#backporting-fixes-to-the-release-branches - i.e. open a new github issue, add it to the 17.0.x release milestone and type /cherry-pick <commit-id> in the message field; the reviewers of this patch should ideally automatically end up CCd on the actual backport request and can say whether they think it is suitable for backporting.