This is an archive of the discontinued LLVM Phabricator instance.

[TableGen] Make OpcodeMappings sort comparator strict weak ordering compliant
ClosedPublic

Authored by danlark on Aug 15 2023, 2:19 AM.

Details

Summary

This did not satisfy equivalence of transitivity. There was an attempt
to fix it in https://reviews.llvm.org/D58687 but it was not fully
correct. Masks might not be equivalent but be equal according to LessThan lambda

I don't have commit rights. Danila Kutenin kutdanila@yandex.ru

Diff Detail

Event Timeline

danlark created this revision.Aug 15 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 2:19 AM
danlark requested review of this revision.Aug 15 2023, 2:19 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 2:19 AM

Sorry, but I'm quite busy right now and I haven't had a chance to look into this in detail. At a glance, I believe that the code here is OK because LessThan is always used after inequality has already been established. However, I agree that LessThan itself is not a "complete" < operator but I don't think it needs to be in this use case.

Friendly ping

I'm also not seeing where the issue is with the current code

danlark added a comment.EditedAug 29 2023, 8:56 AM

I'm also not seeing where the issue is with the current code

Comparator checks that if (LhsMasks.first != RhsMasks.first) and then compares by popcount and left bit. We can construct an example where transitivity of equivalence is broken, for example different masks with the same popcount and left bit

a = (1010, 0000)
b = (0110, 1010)
c = (1010, 1111)

a < b is false, b < a is false (left bit counts from the right)
b < c is false, c < b is false

But a < c because .first are equal and popcount of .second of c is bigger

I thought the zero count is from the most significant bit but I may be wrong. In any case, your point is valid. I see how you can construct an example where the .first fields are not equal, but then the popcount and countl_zero are both equal, leading to a "false" on the LessThan call for both (a,b) and (b,a) comparison.
The change proposed here does provide an absolute ordering so it resolves the issue you found and it's reasonable to go with it. I'm not sure it's the "desired" one (or if it even matters). It will rely more often on the Idx default, than on the two comparisons, and perhaps that's ok.
A more classical comparison would be for LessThan to provide a full <; so comparing on the .first for equivalency, but then determining the follow up on conditions which defines a strict ordering (e.g. bitsToDouble()?).

I would appreciate a code owner chiming in on whether there's a preferred resolution.

aeubanks accepted this revision.Aug 29 2023, 12:50 PM

ah I see, lgtm

This revision is now accepted and ready to land.Aug 29 2023, 12:50 PM

ah I see, lgtm

Thanks, please, submit on my behalf

MaskRay accepted this revision.Aug 29 2023, 1:25 PM
This revision was landed with ongoing or failed builds.Aug 29 2023, 1:55 PM
This revision was automatically updated to reflect the committed changes.