This is an archive of the discontinued LLVM Phabricator instance.

Stabilize alloca slices sort in SROA
ClosedPublic

Authored by rampitec on Jun 5 2020, 3:06 PM.

Details

Summary

Slice::operator<() has a non-deterministic behavior. If we have
identical slices comparison will depend on the order or operands.
Normally that does not result in unstable compilation results
because the order in which slices are inserted into the vector
is deterministic and llvm::sort() normally behaves as a stable
sort, although that is not guaranteed.

However, there is test option -sroa-random-shuffle-slices which
is used to check exactly this aspect. The vector is first randomly
shuffled and then sorted. The same shuffling happens without this
option under expensive llvm checks.

I have managed to write a test which has hit this problem.

There are no fields in the Slice class to resolve the instability.
We only have offsets, IsSplittable and Use, but neither Use nor
User have anything suitable for predictable comparison.

I have switched to stable_sort which has to be sufficient and
removed that randon shuffle option.

Diff Detail

Event Timeline

rampitec created this revision.Jun 5 2020, 3:06 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 5 2020, 3:06 PM

I haven't looked at the code changes deeply, but would it be simpler to use std::stable_sort?

I haven't looked at the code changes deeply, but would it be simpler to use std::stable_sort?

std::stable_sort is C++17, we are still at C++14. It should be sufficient to use stable_sort here, but it will also require to remove -sroa-random-shuffle-slices option.

rampitec updated this revision to Diff 269246.Jun 8 2020, 8:17 AM

Top up Ordinal to full dword along with the bool flag. It was supposed to be 31 bits from the beginning.

I haven't looked at the code changes deeply, but would it be simpler to use std::stable_sort?

std::stable_sort is C++17, we are still at C++14. It should be sufficient to use stable_sort here, but it will also require to remove -sroa-random-shuffle-slices option.

I stand corrected, in C++17 it only added some versions. Yes, I can change it to stable_sort, I seems to be sufficient.

rampitec updated this revision to Diff 269255.Jun 8 2020, 8:45 AM

Just switched to std::stable_sort().

efriedma accepted this revision.Jun 8 2020, 1:50 PM

LGTM

This revision is now accepted and ready to land.Jun 8 2020, 1:50 PM
rampitec edited the summary of this revision. (Show Details)Jun 8 2020, 2:03 PM
This revision was automatically updated to reflect the committed changes.