This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add all_equal predicate
ClosedPublic

Authored by kuhar on Aug 21 2022, 1:04 PM.

Details

Summary

llvm::all_equal checks if all values in the given range are equal, i.e., there are no two elements that are not equal.
Similar to llvm::all_of, it returns true when the range is empty.

llvm::all_equal is intended to supersede llvm::is_splat, which will be deprecated and removed in future patches.
See the discussion thread for more details:
https://discourse.llvm.org/t/adt-is-splat-and-empty-ranges/64692.

Diff Detail

Event Timeline

kuhar created this revision.Aug 21 2022, 1:04 PM
Herald added a project: Restricted Project. · View Herald Transcript
kuhar requested review of this revision.Aug 21 2022, 1:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2022, 1:04 PM
dblaikie accepted this revision.Aug 21 2022, 2:18 PM

Great (:

This revision is now accepted and ready to land.Aug 21 2022, 2:18 PM
shchenz accepted this revision.Aug 21 2022, 6:30 PM

LGTM. Thanks for fixing.

This revision was automatically updated to reflect the committed changes.
dtzWill added a subscriber: dtzWill.Sep 8 2022, 2:31 PM

Small question about usage of std::equal here, if you don't mind? Thanks!

llvm/include/llvm/ADT/STLExtras.h
1770

Hi! Just wondering, why does this use std::equal-- which compares ranges (here, range of [2nd to last] vs [1st to (last-1)])-- instead of something like llvm::all_of(Range,[&](auto x)(return x == *Begin;)?

Does this have advantages? Seems slightly more expensive this way, is why I'm asking, but I haven't bounced through the generated code or such.

kuhar added inline comments.Sep 8 2022, 2:45 PM
llvm/include/llvm/ADT/STLExtras.h
1770

This is how is_splat used to be implemented. is_splat was in fact introduced in https://reviews.llvm.org/D49958 to fix previous misuses of std::equal that called it incorrectly.

I haven't looked at the generated code to see which implementation would be better either. Based on the existing uses in the code base, I don't think we ever use all_equal with a large enough number of elements for it to matter. But I'd be happy to know too, if you want to dig deeper.

FWIW, the implementation based on all_of performs one more comparison.