<div dir="ltr"><div>The contract for 'attribute((const))' as specified in GCC's docs may be open to your interpretation -- and in fact there has been a discussion on the GCC lists in the past about this topic: <<a href="https://gcc.gnu.org/legacy-ml/gcc/2015-09/msg00354.html" target="_blank">https://gcc.gnu.org/legacy-ml/gcc/2015-09/msg00354.html</a>>. But I think it's far too late to change to the semantics you propose. The actual semantics -- both as implemented and as depended upon in practice -- are that calls to an `__attribute__((const))` function can be arbitrarily moved, added, or removed within one thread of computation, but <i>not</i> globally across the entire program.</div><div><br></div><div>In any case, what it lowers to in Clang is the LLVM IR function-attribute readnone, which I'd argue is even more clearly correct to use on an LLVM function returning the address of a TLS global variable. Note that llvm infers that property via the FunctionAttrs pass, e.g. for this function:</div><div><font face="monospace">@t = thread_local global i32 0, align 4<br>define i32* @foo() {<br>ret i32* @t<br>}<br></font></div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Nov 24, 2020 at 8:13 AM Joerg Sonnenberger via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Mon, Nov 23, 2020 at 05:07:49PM -0500, James Y Knight via llvm-dev wrote:<br>
> Special handling for pthread_self doesn't seem reasonable -- all other<br>
> functions marked '__attribute__((const))' or 'readnone' have the exact same<br>
> problem.<br>
<br>
I disagree. I would strongly argue that attribute const for pthread_self<br>
is a violation of the attribute contract. As seen, it rather obviously<br>
depends on observable state of the program. It should be pure only.<br>
<br>
Joerg<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>