This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Fix miscompile bugs related to smul.fix.sat with scale zero
ClosedPublic

Authored by bjope on Aug 30 2021, 12:50 PM.

Details

Summary

When expanding a SMULFIXSAT ISD node (usually originating from
a smul.fix.sat intrinsic) we've applied some optimizations for
the special case when the scale is zero. The idea has been that
it would be cheaper to use an SMULO instruction (if legal) to
perform the multiplication and at the same time detect any overflow.
And in case of overflow we could use some SELECT:s to replace the
result with the saturated min/max value. The only tricky part
is to know if we overflowed on the min or max value, i.e. if the
product is positive or negative. Unfortunately the implementation
has been incorrect as it has looked at the product returned by the
SMULO to determine the sign of the product. In case of overflow that
product is truncated and won't give us the correct sign bit.

This patch is adding an extra XOR of the multiplication operands,
which is used to determine the sign of the non truncated product.

This patch fixes PR51677.

Diff Detail

Event Timeline

bjope created this revision.Aug 30 2021, 12:50 PM
bjope requested review of this revision.Aug 30 2021, 12:50 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2021, 12:50 PM
lebedev.ri accepted this revision.Aug 30 2021, 1:06 PM

LG
This should probably be backported.

This revision is now accepted and ready to land.Aug 30 2021, 1:06 PM
This revision was landed with ongoing or failed builds.Aug 30 2021, 1:09 PM
This revision was automatically updated to reflect the committed changes.
bjope added a subscriber: tstellar.Aug 30 2021, 1:13 PM

LG
This should probably be backported.

Thanks!

About backporting. I just add this to some meta tickets in bugzilla? Or email @tstellar?
For 13.0.0 I found https://bugs.llvm.org/show_bug.cgi?id=51236 (which for some reason is written on OpenMP). Is that enough or should it be added to older versions as well?

Please file a bug and put release-13.0.0 in the blocks field if you want this backported.