This is an archive of the discontinued LLVM Phabricator instance.

[ADT] Add STLForwardCompat.h and llvm::disjunction
ClosedPublic

Authored by scott.linder on Apr 16 2021, 11:31 AM.

Details

Summary

Move some types in STLExtras.h which are named and behave identically to
STL types from future standards into a dedicated header. This keeps them
organized (they are not "extras" in the same sense as most types in
STLExtras.h are) and fixes circular dependencies in future patches.

Diff Detail

Event Timeline

scott.linder created this revision.Apr 16 2021, 11:31 AM
scott.linder requested review of this revision.Apr 16 2021, 11:31 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 16 2021, 11:31 AM

Remove SHIM macro; it wasn't actually doing anything (there was a typo in the
lint identifier), it seems like clang-tidy just ignored the identifier when it
was wrapped in a macro.

I don't think "shim" is the right term here - if anything I'd think things like our range-based adapters to standard algorithms (llvm::sort(R, C) -> std::sort(R.begin(), R.end(), C)) is a "shim" in the sense of the word that I know of - a thin adapter ( https://en.wikipedia.org/wiki/Shim_(computing) - "a shim is a library that transparently intercepts API calls and changes the arguments passed, handles the operation itself or redirects the operation elsewhere.").

These are STL reimplementations, though that's a bit of a mouthful. STLCompat.h, perhaps?

llvm/include/llvm/ADT/STLShims.h
22 ↗(On Diff #338178)

I'm not sure this macro is adding value - what's the motivation for it compared to a comment pointing out that a given thing is intended to be equivalent to the standard entity of the same name?

Oh, and the patch description specifically calls out "STL types" - but presumably we might put STL variables in here too (like the _v variants of various traits)

I don't think "shim" is the right term here - if anything I'd think things like our range-based adapters to standard algorithms (llvm::sort(R, C) -> std::sort(R.begin(), R.end(), C)) is a "shim" in the sense of the word that I know of - a thin adapter ( https://en.wikipedia.org/wiki/Shim_(computing) - "a shim is a library that transparently intercepts API calls and changes the arguments passed, handles the operation itself or redirects the operation elsewhere.").

These are STL reimplementations, though that's a bit of a mouthful. STLCompat.h, perhaps?

Sure, I'm amenable to other names. I originally planned to use "polyfill" (https://en.wikipedia.org/wiki/Polyfill_(programming)) but landed on shim because:

The definition of "shim" I was going for is the one from that speakingjs book: "A shim is a library that brings a new API to an older environment, using only the means of that environment."

I don't like just "reimplementation", even though it is accurate, because it doesn't really convey the intent; we will be deleting our version ASAP. We really don't care about the implementation per-se, we just want to code to a future specification. If we were dissatisfied with the standard implementations then "reimplementation" would seem more apt.

I also feel like just "Compat" is a bit ambiguous, but maybe I just think of "backwards" compat first. What do you think about STLForwardCompat.h ? That also brings up the questionof what happens when someone compiles LLVM with a newer standard than C++14, i.e. is this really "compatible" with the newer standards if we don't detect whether they are present? I am not sure how best to approach doing that in general.

I don't think "shim" is the right term here - if anything I'd think things like our range-based adapters to standard algorithms (llvm::sort(R, C) -> std::sort(R.begin(), R.end(), C)) is a "shim" in the sense of the word that I know of - a thin adapter ( https://en.wikipedia.org/wiki/Shim_(computing) - "a shim is a library that transparently intercepts API calls and changes the arguments passed, handles the operation itself or redirects the operation elsewhere.").

These are STL reimplementations, though that's a bit of a mouthful. STLCompat.h, perhaps?

Sure, I'm amenable to other names. I originally planned to use "polyfill" (https://en.wikipedia.org/wiki/Polyfill_(programming)) but landed on shim because:

Ah, yeah, that's not one I've come across personally.

The definition of "shim" I was going for is the one from that speakingjs book: "A shim is a library that brings a new API to an older environment, using only the means of that environment."

Yeah, it's certainly got some related use.

I don't like just "reimplementation", even though it is accurate, because it doesn't really convey the intent; we will be deleting our version ASAP. We really don't care about the implementation per-se, we just want to code to a future specification. If we were dissatisfied with the standard implementations then "reimplementation" would seem more apt.

I also feel like just "Compat" is a bit ambiguous, but maybe I just think of "backwards" compat first. What do you think about STLForwardCompat.h ? That also brings up the questionof what happens when someone compiles LLVM with a newer standard than C++14, i.e. is this really "compatible" with the newer standards if we don't detect whether they are present? I am not sure how best to approach doing that in general.

ForwardCompat sounds OK to me, but given the other definition of shim, I'm probably OK-ish with that too.

Re: Newer standards. I think it's still compatible/doesn't make the name wrong if people are compiling in C++14 mode. But if we really wanted to, probably the closer we get to migrating to building with C++>14 by default, we might change these shims/whatever they are to be defined as aliases for the real implementations when compiling with a version that supports the implementation to help align things if they happen to be divergent in any way. Then eventually when the newer version is adopted as an LLVM requirement, we'd remove the fallback (so the llvm::* would only be an alias for the std:: version at that point) and then eventually remove the aliases.

scott.linder retitled this revision from [ADT] Add STLShims.h and llvm::disjunction to [ADT] Add STLForwardCompat.h and llvm::disjunction.
scott.linder edited the summary of this revision. (Show Details)
  • Rename STLShims -> STLForwardCompat
  • Change "STL types" -> "STL features"
scott.linder edited the summary of this revision. (Show Details)

Fix a typo in the description

dblaikie accepted this revision.Apr 18 2021, 11:00 AM
dblaikie added inline comments.
llvm/include/llvm/ADT/STLForwardCompat.h
37–38

Hmm, seems this isn't actually conforming implementation (the standard one says that it short-circuits - doesn't try to evaluate later expressions if an early one is true/false - and that the type returned by std::conditional is the type of that last condition that proved the rule, I think?). But probably close enough since it's already committed.

llvm/unittests/ADT/STLForwardCompatTest.cpp
23–31

Was there no existing testing of conjunction? (ie: something that should be deleted as the testing is added/moved here?)

This revision is now accepted and ready to land.Apr 18 2021, 11:00 AM
scott.linder added inline comments.Apr 19 2021, 11:01 AM
llvm/include/llvm/ADT/STLForwardCompat.h
37–38

My understanding of the definition of std::conditional led me to believe this definition does satisfy the "short-circuiting" property, and I tried to include at least a (potentially incomplete?) trivial test of that using an incomplete_type following an element that proves the rule of the predicate (i.e. false for conjunction, true for disjunction)

llvm/unittests/ADT/STLForwardCompatTest.cpp
23–31

Not sure, I assume it just wasn't noticed in review? There are tests of it indirectly via Any, see https://reviews.llvm.org/D48807

(good enough to commit - but happy to also keep discussing the finer nuances of spec conformance here if you like)

llvm/include/llvm/ADT/STLForwardCompat.h
37–38

Oh, right you are!

Is it worth testing the exact type of disjunction/conjunction, rather than only their boolean value? To test that aspect of the spec conformance? Hmm - wonder what the spec says about whether the type parameters must be able to be derived from?

llvm/unittests/ADT/STLForwardCompatTest.cpp
23–31

Yep, my bad. Thanks for adding in testing now

This revision was landed with ongoing or failed builds.Apr 30 2021, 10:30 AM
This revision was automatically updated to reflect the committed changes.