This is an archive of the discontinued LLVM Phabricator instance.

[SelectionDAG] Use correct boolean representation in FoldConstantArithmetic
ClosedPublic

Authored by bjope on Apr 28 2022, 8:06 AM.

Details

Summary

The description of SETCC says

/// SetCC operator - This evaluates to a true value iff the condition is
/// true.  If the result value type is not i1 then the high bits conform
/// to getBooleanContents.

Without this patch, we sign extended the i1 to the used larger type
regardless of getBooleanContents. This resulted in miscompiles, as
shown in the attached testcase that ended up returning -1 instead of
1 when using -mattr=+v.

Fixes https://github.com/llvm/llvm-project/issues/55168

Diff Detail

Event Timeline

bjope created this revision.Apr 28 2022, 8:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 8:06 AM
bjope requested review of this revision.Apr 28 2022, 8:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 28 2022, 8:06 AM
RKSimon added inline comments.Apr 28 2022, 8:17 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5587

"ExtendToBooleanContent" should be sufficient

bjope added inline comments.Apr 28 2022, 8:23 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5649

Not sure if it perhaps would be OK (or even more correct) to always extend according to getBooleanContents here if ScalarResult:s type is i1?

(when preparing this patch it felt less intrusive to just consider the case when SVT has been adjusted down for a SETCC above)

bjope updated this revision to Diff 425791.Apr 28 2022, 8:24 AM

Shorten a variable name according to review feedback.

I want to look more closely at this. Aren't the extended bits are going into a build_vector or splat_vector that is supposed to ignore the extra bits because they are past the size of the result element type?

I want to look more closely at this. Aren't the extended bits are going into a build_vector or splat_vector that is supposed to ignore the extra bits because they are past the size of the result element type?

Oh, I see. I missed that we produce an i1 setcc result. So it's not extending past the element result type.

bjope marked an inline comment as done.Apr 28 2022, 8:45 AM
craig.topper added inline comments.Apr 28 2022, 8:58 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5654

Can we pick the extend opcode outside the loop?

bjope updated this revision to Diff 425810.Apr 28 2022, 9:08 AM

Calculate the ExtendCode outside the loop (also no longer needing the extra bool
to determine how we extend inside the loop).

(Thanks Craig for this idea!)

bjope marked an inline comment as done.Apr 28 2022, 9:08 AM
This revision is now accepted and ready to land.Apr 28 2022, 9:10 AM
This revision was landed with ongoing or failed builds.Apr 28 2022, 9:42 AM
This revision was automatically updated to reflect the committed changes.