<div dir="ltr">(Adding back cfe-commits, I removed it accidentally)<div class="gmail_extra"><br><div class="gmail_quote">On Wed, Dec 17, 2014 at 3:48 PM, Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">>> The only option I can think of that would wold work is to use comdas<br>
>> like now, but to always produce a guard variable. So when the variable<br>
>> is found to be a constant, we would have<br>
>><br>
>> @_ZGVZ1fvE1y = linkonce_odr constant i64 1, comadat $_Z1fv<br>
><br>
><br>
> That's not sufficient: if the function is taken from the TU with constant<br>
> initialization, the variable won't be initialized. You need to both emit a<br>
> guard variable initialized to 1, *and* emit the initialization code. (But<br>
> only in the rare and detectable case where we can have dynamic init in<br>
> another TU.)<br>
<br>
</span>I see. That is also broken in gcc. Testcases attached. Run with<br>
<br>
CXX -c test2.cpp -fPIC -g<br>
$CXX -c test.cpp -O2 -fPIC -g<br>
$CXX test.o test2.o -o t<br>
./t<br>
<br>
The comparison will fail since we read a 0.<br>
<br>
So you suggestion is that in the case of a known constant that might<br>
not be constant in another TU we emit something like the attached t.ll<br>
file.<br>
<br>
Things to note:<br>
<br>
* The variable and the guard are in the same comdat.<br>
* The function is in its own comdat.<br>
* The variable is initialized to 42 and the guard to 1<br>
* Both the variable and the guard are linkonce, not linkonce_odr,<br>
otherwise llvm could optimize the comparison away.<br>
* The function has the regular dynamic initialization. Just doing a<br>
"store i32 42, i32* @_ZZ1fvE1y, align 4" would cause a race condition,<br>
no?<br></blockquote><div><br></div><div>Yes; what I meant was to use the normal guarded initialization, but the actual initialization step would simply be a copy of the known constant value to the variable (that is, exactly what your code does).</div><div><br></div><div>That IR looks right, except _Z1fv should be 'linkonce_odr' rather than 'define' =)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I will revert the patch for now.<br>
<br>
Cheers,<br>
Rafael<br>
</blockquote></div></div></div>