This is an archive of the discontinued LLVM Phabricator instance.

[llvm][SLP] Exit early if inputs to comparator are equal
ClosedPublic

Authored by davidberard98 on Jul 20 2023, 11:36 AM.

Details

Summary

TL;DR: This PR modifies a comparator. The comparator is used in a subsequent call to llvm::stable_sort. Sorting comparators should follow strict weak ordering - in particular, (x < x) should return false. This PR adds a fix to avoid an infinite loop when the inputs to the comparator are equal.

Details:

Sometimes when two equivalent tensors passed into the comparator, we encounter infinite looping (at https://github.com/llvm/llvm-project/blob/aae2eaae2cefd3132059925c4592276defdb1faa/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp#L4049)

Although it seems like this comparator will never be called with two equivalent pointers, some sanitizers, e.g. https://chromium.googlesource.com/chromiumos/third_party/gcc/+/refs/heads/stabilize-zako-5712.88.B/libstdc++-v3/include/bits/stl_algo.h#360, will add checks for (x < x). When this sanitizer is used with the current implementation, it triggers a comparator check for (x < x) which runs into the infinite loop

Diff Detail

Event Timeline

davidberard98 created this revision.Jul 20 2023, 11:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2023, 11:36 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
davidberard98 requested review of this revision.Jul 20 2023, 11:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2023, 11:36 AM

I don't remember anything about this code. :)
Can you add a test, and I guess this was submitted through web UI, can you do it with -999 so surrounding code can be viewed?

Add full context

davidberard98 updated this revision to Diff 542643.EditedJul 20 2023, 1:00 PM

added more context - will see if I can get arcanist working to provide more context

testing - I will look further into adding tests

update using arcanist

@ABataev @ayermolo could you let me know if you still want a test for this, and if so do you have suggestions on where/how to add it?

context on why I'm not sure where to add a test - as far as I can tell, BoUpSLP internals are not exposed outside of SLPVectorizer.cpp. Meanwhile, I don't think I can reproduce the error "organically" - i.e. I don't think we ever encounter the case where V1 == V2 except with this CheckedCompare code path linked above. So, I could refactor some of this code to expose PHICompare somewhere where it can be unit tested, but I figured I'd ask first whether this is a good idea or whether you have other suggestions.

ABataev accepted this revision.Jul 20 2023, 4:37 PM

LG, I think it is ok to commit it as is

This revision is now accepted and ready to land.Jul 20 2023, 4:37 PM
davidberard98 retitled this revision from [llvm] Exit early if inputs to comparator are equal to [llvm][SLP] Exit early if inputs to comparator are equal.Jul 20 2023, 5:26 PM

Thanks! I don't have commit access, so could one of you commit it for me?

Thanks! I don't have commit access, so could one of you commit it for me?

Will do tommorow

This revision was landed with ongoing or failed builds.Jul 21 2023, 5:44 AM
This revision was automatically updated to reflect the committed changes.