This is an archive of the discontinued LLVM Phabricator instance.

TargetLowering: fix an infinite DAG combine in SimplifySETCC
ClosedPublic

Authored by jroelofs on Jul 12 2023, 8:53 AM.

Details

Summary

TargetLowering::SimplifySetCC wants to swap the operands of a SETCC to canonicalize the constant to the RHS. The bug here was that it did so whether or not the RHS was already a constant, leading to an infinite loop.

rdar://111847838

Diff Detail

Event Timeline

jroelofs created this revision.Jul 12 2023, 8:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2023, 8:53 AM
jroelofs requested review of this revision.Jul 12 2023, 8:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 12 2023, 8:53 AM
jroelofs added inline comments.Jul 12 2023, 8:54 AM
llvm/test/CodeGen/AArch64/arm64-setcc-swap-infloop.ll
26

The bug is really sensitive to the UB here. Removing any of the UB on these first 3 lines makes the bug go away.

craig.topper added inline comments.Jul 12 2023, 9:08 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4230

Doesn't this change prevent folding constant comparisons? But maybe that's handled by the FoldSetCC above? Was that added later than this comment was written?

jroelofs edited the summary of this revision. (Show Details)Jul 12 2023, 10:41 AM
jroelofs added inline comments.Jul 12 2023, 11:15 AM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4230

Looks like this commit is the original source of that comment:

https://github.com/llvm/llvm-project/commit/65919b5058e155df43c248a18b5ecba52bd4d20f

I don't follow how this ever facilitated constant folding. But yes, looks like FoldSetCC was added later, and it takes care of those duties. I'll update the comment.

jroelofs updated this revision to Diff 539650.Jul 12 2023, 11:24 AM
This revision is now accepted and ready to land.Jul 12 2023, 11:28 AM
This revision was landed with ongoing or failed builds.Jul 12 2023, 11:47 AM
This revision was automatically updated to reflect the committed changes.
jroelofs updated this revision to Diff 539730.Jul 12 2023, 2:16 PM

Add a carveout for the cases the RISCV tests care about.

jroelofs updated this revision to Diff 539735.Jul 12 2023, 2:28 PM

More precise carveout.

nikic added a subscriber: nikic.Jul 12 2023, 2:49 PM

What is the affected RISCV test case?

craig.topper added inline comments.Jul 12 2023, 3:23 PM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4237

I'm not sure about the logic of this carve out. splat_vector is primarily used with scalable vectors. So checking it is not a scalable vector, but N0 is a splat_vector seems odd.

craig.topper added inline comments.Jul 12 2023, 3:26 PM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4237

I think the check you want is just N0ConstOrSplat && !N1ConstOrSplat. The old code only checked !N1ConstOrSplat for scalable vectors. Now you want to check it regardless of scalable vectors.

jroelofs added inline comments.Jul 12 2023, 3:38 PM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4237

I think the check you want is just N0ConstOrSplat && !N1ConstOrSplat.

That's what the landed-but-reverted commit [1] did, but it broke some RISCV tests that IIUC are expecting that (setcc (splat (const)), (buildvector (const))) get normalized to put the constant splat on the RHS (so that subsequent combine(s) can then constant-fold that, I think?).

1: https://reviews.llvm.org/D155095?id=539663

craig.topper added inline comments.Jul 12 2023, 3:47 PM
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4237

I mean remove any mention of !OpVT.isScalableVector.

jroelofs marked an inline comment as not done.Jul 12 2023, 4:00 PM
jroelofs added inline comments.
llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
4237

Oh, makes sense. I think that works.

craig.topper reopened this revision.Jul 12 2023, 4:12 PM
This revision is now accepted and ready to land.Jul 12 2023, 4:12 PM
jroelofs closed this revision.Jul 29 2023, 11:10 AM