This is an archive of the discontinued LLVM Phabricator instance.

Avoid keeping internal string_views in Twine.
ClosedPublic

Authored by saugustine on Jul 16 2021, 1:27 PM.

Details

Summary

This is a follow-up to https://reviews.llvm.org/D103935

A Twine's internal layout should not depend on which version of the
C++ standard is in use. Dynamically linking binaries compiled with two
different layouts (eg, --std=c++14 vs --std=c++17) ends up
problematic.

This change avoids that issue by immediately converting a
string_view to a StringRef at the cost of an extra eight-bytes
in Twine, and certain copies and dereferences to StringRefs that
hadn't happened prior to this change.

I do believe the optimizer can elide almost all the extra nominal
work via inlining and the like.

Diff Detail

Event Timeline

saugustine created this revision.Jul 16 2021, 1:27 PM
saugustine requested review of this revision.Jul 16 2021, 1:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 16 2021, 1:27 PM

tstellar:

Can you verify that this fixes your issue?

dblaikie added inline comments.Jul 16 2021, 1:47 PM
llvm/include/llvm/ADT/Twine.h
153–154

I think from a standards-conformance perspective this may need to be decomposed into a raw struct of const char*+length - otherwise, since StringRef isn't trivially constructible (guess that's why you had to add the Child ctor?) it won't be validly constructed and deconstructed as required by C++ for non-trivial types like this.

At least that's my vague/general understanding. There could be some nuance that makes this valid.

(as a follow-up, it'd be good to collapse all the other cases that can be represented by StringRef into this case to reduce the complexity of this code, I think)

164

If this does stay, the trailing semicolon should be removed

This revision switches from a StringRef to an explicit pointer
and length.

dblaikie accepted this revision.Jul 16 2021, 11:27 PM

Looks good to me, with the (ptr, length) ctor made private.

@dexonsmith could you double check this & see if it sounds reasonable?

llvm/include/llvm/ADT/Twine.h
304–308

I probably wouldn't add this ctor (if it's being added it should probably be unit tested - which I guess would be a perk of this design, that most of the string_view implementation would be unit testable even when string_view isn't available) - but if it's staging for common code, it could be added, but made private - then when other things (like StringRef) is changed over to use this ctor it'll get that generic (non-C++17-specific) test coverage.

This revision is now accepted and ready to land.Jul 16 2021, 11:27 PM
dexonsmith accepted this revision.Jul 19 2021, 1:26 PM

Looks good to me, with the (ptr, length) ctor made private.

@dexonsmith could you double check this & see if it sounds reasonable?

LGTM too.

tstellar accepted this revision.Jul 19 2021, 7:34 PM

I've confirmed that this fixes my issue, thank you.

This revision was landed with ongoing or failed builds.Jul 20 2021, 8:47 AM
This revision was automatically updated to reflect the committed changes.
saugustine marked an inline comment as done.