This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fix pr47668
AbandonedPublic

Authored by aqjune on Sep 28 2020, 10:31 AM.

Details

Summary

This patch fixes pr47668 (https://llvm.org/pr47668) by checking whether the shift amount is smaller than the bitwidth of variables.

This is done by checking whether Z is a constant and it is smaller than the bitwidth.

When the shift amount is undef, it is unsafe in theory because 1 << undef is poison, causing the miscompilation happen again.
To prevent this, I made the undef simply fold into zero, since shl 0 can be optimized later.

Proof to the first and second changes:
https://alive2.llvm.org/ce/z/CVBwu9 , https://alive2.llvm.org/ce/z/h2ZWoW

The changes in f_var2* and f_var3* are checking whether src and tgt are identical, so proof not needed

Diff Detail

Event Timeline

aqjune created this revision.Sep 28 2020, 10:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 28 2020, 10:31 AM
aqjune requested review of this revision.Sep 28 2020, 10:31 AM

This is missing alive link.
I think the fix is wrong, based solely on the fact that it is huge.
I would think the fix should be:

  • if the shift amount is a variable, check that it is not undef
  • if it is a constant, sanitize undef elts to 0

Am i wrong?

aqjune edited the summary of this revision. (Show Details)Sep 30 2020, 2:12 PM

Hi, I'm back!

The complexity comes from dealing with the case when the input is a vector.
It iterates over the elements of a vector and puts the undef-eliminated result into ZWithoutUndef.

Hi, I'm back!

The complexity comes from dealing with the case when the input is a vector.
It iterates over the elements of a vector and puts the undef-eliminated result into ZWithoutUndef.

Please do reply to the question i asked, not re-explain the code.

aqjune updated this revision to Diff 295413.Sep 30 2020, 2:22 PM

Soothe clang-format warning

  • if the shift amount is a variable, check that it is not undef
  • if it is a constant, sanitize undef elts to 0

In the first case, the variable should be checked to be less than the bitwidth as well, which is slightly expensive so not contained in this patch.

The second case is what this change contains. Additionally, it had to check whether the value was less than the bitwidth, which made the code slightly longer.

Did my explanation make sense?
The message was that it was needed to check the shift bitwidth as well, which slightly made the code longer.

aqjune abandoned this revision.Jul 30 2021, 5:22 PM

Just found that this old patch is still open - I'd like to revisit this later since freeze can be used to resurrect this transformation. This will require investigation into the impact of freeze.