This is an archive of the discontinued LLVM Phabricator instance.

[InstSimplify] Check the NonZero for power of two value
ClosedPublic

Authored by Allen on Aug 2 2023, 5:26 AM.

Diff Detail

Event Timeline

Allen created this revision.Aug 2 2023, 5:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2023, 5:26 AM
Allen requested review of this revision.Aug 2 2023, 5:26 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2023, 5:26 AM

Thanks, I can confirm that this fixes my issue in a full rebuild and rerun of the tests!

foad added a subscriber: foad.Aug 2 2023, 8:48 AM

I've seen some correctness problems caused by D154953 (running Vulkan CTS testing on the AMDGPU target) and I can confirm that this patch fixes them.

Is there any indication why this is necessary? The alive2 proof of the original transform seems to allow for INT_MIN: https://alive2.llvm.org/ce/z/yZ_I2a

Allen updated this revision to Diff 546673.Aug 2 2023, 7:49 PM
Allen edited the summary of this revision. (Show Details)
Allen retitled this revision from [InstSimplify] Check the isNonNegative for Power2 value to [InstSimplify] Check the NonZero for power of two value.
Allen added a comment.Aug 2 2023, 7:52 PM

Is there any indication why this is necessary? The alive2 proof of the original transform seems to allow for INT_MIN: https://alive2.llvm.org/ce/z/yZ_I2a

Thanks, you are right, and we should prevent the case if the mask value is -1, https://alive2.llvm.org/ce/z/WU_j4a

Can someone follow up on this patch? I'd kinda like the breakage in git main to get fixed - or perhaps we'd need to revert if we can't settle on a fix soon.

goldstein.w.n added inline comments.Aug 3 2023, 1:53 PM
llvm/lib/Analysis/ValueTracking.cpp
2022 ↗(On Diff #546673)

What is this change for? Is this part of the bugfix?

Allen marked an inline comment as done.Aug 3 2023, 2:45 PM
Allen added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
2022 ↗(On Diff #546673)

Yes. This is similar to the following logic in line 2045.

// A shift left or a logical shift right of a power of two is a power of two
// or zero.
if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
               match(V, m_LShr(m_Value(X), m_Value()))))
  return isKnownToBeAPowerOfTwo(X, /*OrZero*/ true, Depth, Q);

This is try to match following code, because we can assume the vscale always bigger than 1, and it is a power of 2 if it has the attribute of vscale_range

%vscale = call i64 @llvm.vscale.i64()
  %shift = shl nuw nsw i64 %vscale, 11
Allen marked an inline comment as done.Aug 3 2023, 2:53 PM

hi @goldstein.w.n, because this patch it fixing some correctness problems which block many people, so would you help me to review this in high priority, thanks

Allen added a comment.Aug 3 2023, 3:02 PM

Can someone follow up on this patch? I'd kinda like the breakage in git main to get fixed - or perhaps we'd need to revert if we can't settle on a fix soon.

sorry for blocking your work, I'll revert that code if I don't land on this fixing today.

goldstein.w.n accepted this revision.Aug 3 2023, 3:09 PM

LGTM.

llvm/lib/Analysis/ValueTracking.cpp
2022 ↗(On Diff #546673)

But is it related to making the existing codes correct or just improving the coverage? I don't see how its related to the bug. If its not related to the bug, imo it should be split to a seperate patch.

This revision is now accepted and ready to land.Aug 3 2023, 3:09 PM
Allen marked an inline comment as done.Aug 3 2023, 3:59 PM
Allen added inline comments.
llvm/lib/Analysis/ValueTracking.cpp
2022 ↗(On Diff #546673)

Thanks very much again. I'll seperate this patch before landing.

This change is necessary to match the following code.
otherwise the case and_add_shl_vscale_not_power2_negative will don't have a chance to fold the return value because we only check the shl recursively when the OrZero is true now.

%vscale = call i64 @llvm.vscale.i64()
  %shift = shl nuw nsw i64 %vscale, 11
Allen updated this revision to Diff 547075.Aug 3 2023, 6:12 PM
Allen marked an inline comment as done.

rebase to top

This revision was landed with ongoing or failed builds.Aug 3 2023, 6:15 PM
This revision was automatically updated to reflect the committed changes.