This is an archive of the discontinued LLVM Phabricator instance.

[mlir][ODS] Use StringLiteral instead of StringRef when applicable
ClosedPublic

Authored by vinograd47 on Feb 3 2021, 5:23 AM.

Details

Summary

Use StringLiteral for function return type if it is known to return
constant string literals only.

This will make it visible to API users, that such values can be safely
stored, since they refers to constant data, which will never be deallocated.

StringRef is general is not safe to store for a long term,
since it might refer to temporal data allocated in heap.

Diff Detail

Event Timeline

vinograd47 created this revision.Feb 3 2021, 5:23 AM
vinograd47 requested review of this revision.Feb 3 2021, 5:23 AM

I "think" that's right? Adding @bkramer though :)

mehdi_amini accepted this revision.Feb 3 2021, 9:50 AM
This revision is now accepted and ready to land.Feb 3 2021, 9:50 AM
bkramer accepted this revision.Feb 3 2021, 10:02 AM

I don't think we have used StringLiteral to convey that "this string lives forever" so far, but I'm not opposed to it.

@mehdi_amini I don't have commit access. Could you please land this change?

Also, per the comment of StringLiteral (https://github.com/llvm/llvm-project/blob/b198f1f86ce09b86825dd6d80de2c72a617e27f7/llvm/include/llvm/ADT/StringRef.h#L867-L868):

/// In order to avoid the invocation of a global constructor, StringLiteral should *only* be used in a constexpr context

The usage here does not seem to fit into that category?