<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Fri, Nov 25, 2016 at 6:10 AM Mueller-Roemer, Johannes Sebastian via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">What about going for<br class="gmail_msg">
<br class="gmail_msg">
template<unsigned N><br class="gmail_msg">
constexpr StringRef(const char (&Str)[N])<br class="gmail_msg">
<br class="gmail_msg">
and avoiding strlen entirely for string literals?<br class="gmail_msg"></blockquote><div><br></div><div>You'd at least want an assert in there (that N - 1 == strlen(Str)) in case a StringRef is ever constructed from a non-const char buffer that's only partially filled.<br><br>But if we can write this in such a way that it performs well on good implementations - that seems sufficient. If getting good performance out of the compiler means bootstrapping - that's pretty much the status quo already, as I understand it.<br><br>So I wouldn't personally worry too much about performance degredation when built with MSVC - if, when building a stage 2 on Windows (building Clang with MSVC build Clang) you do end up with a compiler with the desired performance characteristics - then that's probably sufficient.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br class="gmail_msg">
-----Original Message-----<br class="gmail_msg">
From: llvm-dev [mailto:<a href="mailto:llvm-dev-bounces@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev-bounces@lists.llvm.org</a>] On Behalf Of Malcolm Parsons via llvm-dev<br class="gmail_msg">
Sent: Friday, November 25, 2016 13:34<br class="gmail_msg">
To: Hal Finkel <<a href="mailto:hfinkel@anl.gov" class="gmail_msg" target="_blank">hfinkel@anl.gov</a>><br class="gmail_msg">
Cc: <a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
Subject: Re: [llvm-dev] RFC: Constructing StringRefs at compile time<br class="gmail_msg">
<br class="gmail_msg">
On 24 November 2016 at 15:04, Hal Finkel <<a href="mailto:hfinkel@anl.gov" class="gmail_msg" target="_blank">hfinkel@anl.gov</a>> wrote:<br class="gmail_msg">
>> Creating constexpr StringRefs isn't trivial as strlen isn't portably<br class="gmail_msg">
>> constexpr and std::char_traits<char>::length is only constexpr in<br class="gmail_msg">
>> C++17.<br class="gmail_msg">
><br class="gmail_msg">
> Why don't we just create our own traits class that has a constexpr length, and then we can switch over to the standard one when we switch to C++17?<br class="gmail_msg">
<br class="gmail_msg">
GCC and Clang treat __builtin_strlen as constexpr.<br class="gmail_msg">
MSVC 2015 doesn't support C++14 extended constexpr. I don't know how well it optimises a recursive strlen.<br class="gmail_msg">
<br class="gmail_msg">
This works as an optimisation for GCC and Clang, and doesn't make things worse for MSVC:<br class="gmail_msg">
<br class="gmail_msg">
/// Construct a string ref from a cstring.<br class="gmail_msg">
LLVM_ATTRIBUTE_ALWAYS_INLINE<br class="gmail_msg">
+#if __has_builtin(__builtin_strlen)<br class="gmail_msg">
+ /*implicit*/ constexpr StringRef(const char *Str)<br class="gmail_msg">
+ : Data(Str), Length(Str ? __builtin_strlen(Str) : 0) {} #else<br class="gmail_msg">
/*implicit*/ StringRef(const char *Str)<br class="gmail_msg">
: Data(Str), Length(Str ? ::strlen(Str) : 0) {}<br class="gmail_msg">
+#endif<br class="gmail_msg">
<br class="gmail_msg">
--<br class="gmail_msg">
Malcolm Parsons<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
</blockquote></div></div>