This is an archive of the discontinued LLVM Phabricator instance.

CodeGenPrepare: Replace constant PHI arguments with switch condition value
ClosedPublic

Authored by MatzeB on Apr 27 2022, 1:29 PM.

Details

Summary

We often see code like the following after running SCCP:

switch (x) { case 42: phi(42, ...); }

This tends to produce bad code as we currently materialize the constant
phi-argument in the switch-block. This increases register pressure and if the
pattern repeats for n case statements, we end up generating n constant
values.

This changes CodeGenPrepare to catch this pattern and revert it back to:

switch (x) { case 42: phi(x, ...); }

Diff Detail

Event Timeline

MatzeB created this revision.Apr 27 2022, 1:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2022, 1:29 PM
MatzeB requested review of this revision.Apr 27 2022, 1:29 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 27 2022, 1:29 PM

I think the idea is good, but the implementation is not currently sound. If we are truncating the condition, then we can't later replace a case constant with the condition source unless we mask the high-bits of that source?
I tried to reduce the last test here to show the bug:
https://alive2.llvm.org/ce/z/s5keYc

It would be better to start with only the non-trunc half of this patch, so we can be sure we have that part correct first.

MatzeB planned changes to this revision.May 2 2022, 12:36 PM

You are right, the trunc one was incorrect. That makes things more complicated for my original testcase. Somehow IR optimizations think it is a good idea to shrink the size of the switch input even if it requires a trunc; it certainly makes recovering this pattern in CodeGenPrepare harder.

MatzeB updated this revision to Diff 426887.May 3 2022, 6:33 PM
MatzeB edited the summary of this revision. (Show Details)

Factored the truncation/extension code into a separate diff.

MatzeB updated this revision to Diff 426894.May 3 2022, 7:11 PM
MatzeB added a comment.May 3 2022, 7:55 PM

It seems this breaks llvm-test-suite/MultiSource/Applications/JM/lencod, investigating.

MatzeB updated this revision to Diff 427084.May 4 2022, 11:33 AM

Fixed bug: We must abort the transformations if there is multiple case labels jumping to the same block. This fixes the llvm-test-suite/MultiSource/Applications/JM/lencod I mentioned earlier.

MatzeB updated this revision to Diff 427086.May 4 2022, 11:34 AM
MatzeB updated this revision to Diff 427097.May 4 2022, 11:46 AM

upload correct patch.

spatel added inline comments.May 5 2022, 5:25 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
6984–6985

Something isn't updated properly in this patch? I don't see this TLI hook defined in the previous patches or on current main.

spatel added inline comments.May 5 2022, 5:32 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
6984–6985

Ah - I didn't see the history in the previous patch in this set. So this patch just needs to be updated because the name/signature changed in D124894.

MatzeB updated this revision to Diff 427372.May 5 2022, 9:58 AM

adjust to changes in D124894

llvm/lib/CodeGen/CodeGenPrepare.cpp
6984–6985

Yeah I didn't bother updating the rest of the stack when I updated the diffs further down. Updated it now.

MatzeB marked 2 inline comments as done.May 5 2022, 9:59 AM
MatzeB updated this revision to Diff 427390.May 5 2022, 10:59 AM
MatzeB marked an inline comment as not done.
reames resigned from this revision.May 5 2022, 11:07 AM
bjope added a subscriber: bjope.May 7 2022, 10:35 PM
spatel added inline comments.May 9 2022, 10:35 AM
llvm/lib/CodeGen/CodeGenPrepare.cpp
7046

Is CheckedCase only used to save time (because findCaseDest() is potentially expensive)? I'd put an explanatory comment on this declaration to make its purpose more obvious.

7054–7055

I'd invert this to "if (!= || !=) continue;" just to reduce the indentation. IIUC, we will do that anyway in the next patch.

llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll
68

We should have another smaller test that does have multiple phis in a block but without multiple cases. That would still be replaced, right?

MatzeB updated this revision to Diff 428273.May 9 2022, 8:10 PM
MatzeB marked 3 inline comments as done.

address review comments

MatzeB added inline comments.May 9 2022, 8:15 PM
llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll
68

Of course the number of PHIs shouldn't matter. This here just demonstrates a case label reachable by 2 case values and in that case we shouldn't replace by either of them (hence 2 PHIs to test for the two case values).

spatel accepted this revision.May 10 2022, 5:37 AM

LGTM

This revision is now accepted and ready to land.May 10 2022, 5:37 AM
This revision was landed with ongoing or failed builds.May 10 2022, 10:02 AM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.May 10 2022, 12:34 PM

This broke tests everywhere, e.g. https://lab.llvm.org/buildbot/#/builders/109/builds/38284

Please take a look.

This broke tests everywhere, e.g. https://lab.llvm.org/buildbot/#/builders/109/builds/38284

Please take a look.

Thanks for the notification! I did notice the build breakage mails but had to get out of some meetings first. Anyway I think I fixed things with https://reviews.llvm.org/rG3bf643eb12c5e2263fc3b46b2e22964eb91eee17 now. Will double check that that does indeed cover all problems.

Hi,

The following starts hanging in Codegen Prepare with this patch:

llc -O1 -o /dev/null bbi-69624.ll

The following starts hanging in Codegen Prepare with this patch:

Thanks for the report, I fixed this in https://github.com/llvm/llvm-project/commit/de9ad98d2d6358e6aa773f0bb5258b629bde9389

The following starts hanging in Codegen Prepare with this patch:

Thanks for the report, I fixed this in https://github.com/llvm/llvm-project/commit/de9ad98d2d6358e6aa773f0bb5258b629bde9389

Great, thanks!