<div dir="ltr"><div dir="ltr"><div>Hi Pablo,</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 11, 2019 at 11:33 AM <a href="mailto:tesonep@gmail.com">tesonep@gmail.com</a> <<a href="mailto:tesonep@gmail.com">tesonep@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">Hi Nicolas,<br>
  thanks for the comment, you are right the problem is the bad<br>
original code. But my opinion is that it just is not reporting the<br>
situation correctly, generating a warning or non-optimizing the code<br>
looks more like a expected behavior. Because as I have said, using a<br>
constant as index in the last statement generates a meaningful warning<br>
and the non-optimizated version of the function.<br>
<br>
And again as you said, the only thing to learn about all this is that<br>
we should not write crappy code.<br></blockquote><div><br></div><div>Hang on.  "crappy code" is too strong.  Back in tre ANSI days it was not undefined behavior, and in fact I think this only because undefined behavior because C compilers were faced with lots of code where variables were typed int but were being used on 64-bit machines.  Had the C world used long as its default type then there would have been no need to make casting the address of a long into the address of an int undefined behavior.  So this isn't crappy code (if you think of what it means in memory on a 64-bit little endnan machine it is perfectly sensible).  It /is/ code that is inappropriate given C99.</div><div><br></div><div>So yes, we have to bow to the C gods and generate C99 compliant code, but what we were doing here wasn't crappy, it was merely code that became undefined behavior so that C compilers could generate more efficient code for 64-bit systems in the presence of code that contains lots of integer variables declared as int.  So let's just say that we have to generate C99 compliant code.  You'll notice that the Smalltalk code in the original does exactly what you suggest Pablo.</div><div><br></div><div><div>fetchLong32: fieldIndex ofFloatObject: oop</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">        </span>"index by word size, and return a pointer as long as the word size"</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">  </span></div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>| bits |</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>(self isImmediateFloat: oop) ifFalse:</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">          </span>[^self fetchLong32: fieldIndex ofObject: oop].</div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>bits := self smallFloatBitsOf: oop.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">    </span>^self</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">          </span>cCode: [(self cCoerceSimple: (self addressOf: bits) to: #'int *') at: fieldIndex]</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">              </span>inSmalltalk:</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                   </span>[self flag: #endian.</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                   </span> fieldIndex = 0</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                </span>ifTrue: [bits bitAnd: 16rFFFFFFFF]</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                             </span>ifFalse: [bits >> 32]]</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">
<br>
On Wed, Dec 11, 2019 at 7:11 PM Nicolas Cellier<br>
<<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> wrote:<br>
><br>
> Of course, when I say "your" code, it's the code you have shown, and probably "our" (VMMaker) code ;)<br>
><br>
> Le mer. 11 déc. 2019 à 19:05, Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com" target="_blank">nicolas.cellier.aka.nice@gmail.com</a>> a écrit :<br>
>><br>
>> Hi Pablo (again),<br>
>> no, not a bug.<br>
>><br>
>> The problem is in the source code. The compiler has the right to presume that your code is exempt of UB, because you cannot depend on UB (obviously).<br>
>> So it can eliminate all code which corresponds to UB.<br>
>><br>
>> The compiler has the right to assume that a pointer to an int cannot point to a long (UB).<br>
>> So modifying a long cannot have any sort of impact on the content of the int pointer.<br>
>> So the compiler can decouple both path return int content and assign long.<br>
>> But assigning the long has no effect, so the code can be suppressed altogether.<br>
>><br>
>> Le mer. 11 déc. 2019 à 18:54, <a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a> <<a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a>> a écrit :<br>
>>><br>
>>> Hi Aliaksei,<br>
>>>       to me it looks like a bug of GCC optimization. Basically, it is<br>
>>> assuming that the x variable is used but never read or its value is<br>
>>> never used. Also it assumes the same of the i variable, as we are only<br>
>>> accessing indirectly to the memory where it locates (the code is even<br>
>>> assuming that the variable exists, but it can be optimize out as in<br>
>>> this scenario). Even though, the original C code is valid C code, we<br>
>>> are not helping the compiler writing code like that. So I have<br>
>>> rewritten the code in a way that does not use indirect memory access<br>
>>> to the stack space.<br>
>>><br>
>>> One thing more that makes me think is a bug, if you use an int<br>
>>> constant as the index and not a parameter, the error does not occur<br>
>>> (the code is not badly optimized) and there is a warning about the<br>
>>> not-so-great access to the stack.<br>
>>><br>
>>> On Wed, Dec 11, 2019 at 6:01 PM Aliaksei Syrel <<a href="mailto:alex.syrel@gmail.com" target="_blank">alex.syrel@gmail.com</a>> wrote:<br>
>>> ><br>
>>> > Hi Pablo,<br>
>>> ><br>
>>> > Wow! Thank you for the detective story :)<br>
>>> ><br>
>>> > Do I understand correctly that the original code causes undefined behavior and therefore can be changed (or even removed) by the compiler?<br>
>>> > (because it returns something that is referencing memory on the stack)<br>
>>> ><br>
>>> > Please keep posting similar things in future! It is very educative :)<br>
>>> ><br>
>>> > Cheers,<br>
>>> > Alex<br>
>>> ><br>
>>> ><br>
>>> > On Wed, 11 Dec 2019 at 17:35, <a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a> <<a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a>> wrote:<br>
>>> >><br>
>>> >> Hi,<br>
>>> >>     this mail is related to Pharo because it is knowledge I found<br>
>>> >> debugging the build of the VM, but the rest is to document it and<br>
>>> >> perhaps someone will found it interesting (also I couldn't find it<br>
>>> >> easily using Google). Sorry for the long mail!<br>
>>> >><br>
>>> >> The problem<br>
>>> >> ==========<br>
>>> >><br>
>>> >> The following code does not produce good code in 8.3 when using optimizations:<br>
>>> >><br>
>>> >> long __attribute__ ((noinline)) myFunc(long i, int index){<br>
>>> >>    long v;<br>
>>> >>    long x = i >> 3;<br>
>>> >><br>
>>> >>    v = x;<br>
>>> >>    return ((int*)(&v))[index];<br>
>>> >> }<br>
>>> >><br>
>>> >> #include <stdio.h><br>
>>> >><br>
>>> >> int main(){<br>
>>> >><br>
>>> >>     long i;<br>
>>> >>     int x;<br>
>>> >><br>
>>> >>     scanf("%ld", &i);<br>
>>> >>     scanf("%d", &x);<br>
>>> >><br>
>>> >>     printf("%ld",myFunc(i,x));<br>
>>> >> }<br>
>>> >><br>
>>> >> Basically, with -02, it generates the following code:<br>
>>> >><br>
>>> >> myFunc:<br>
>>> >>      movslq %esi, %rsi<br>
>>> >>      movslq -8(%rsp,%rsi,4), %rax<br>
>>> >>      ret<br>
>>> >><br>
>>> >> And with -01 it generates the following code:<br>
>>> >><br>
>>> >> myFunc:<br>
>>> >>      sarq $3, %rdi<br>
>>> >>      movq %rdi, -8(%rsp)<br>
>>> >>      movslq %esi, %rsi<br>
>>> >>      movslq -8(%rsp,%rsi,4), %rax<br>
>>> >>      ret<br>
>>> >><br>
>>> >> As you can see, the more optimized version is losing the bit shift (or<br>
>>> >> any other operation).<br>
>>> >> To detect the problem it was not easy, basically, I have used the<br>
>>> >> Pharo Tests to detect the failing function and then to understand the<br>
>>> >> generation of code in GCC, we need to debug its generation.<br>
>>> >><br>
>>> >> The first snippet is generated using:<br>
>>> >><br>
>>> >> gcc -O2 prb.c -S -o prb.s -Wall<br>
>>> >><br>
>>> >> and the second using:<br>
>>> >><br>
>>> >> gcc -O1 prb.c -S -o prb.s -Wall<br>
>>> >><br>
>>> >> The GCC pipeline has different steps, I have centered my self in the<br>
>>> >> tree optimizations.<br>
>>> >> GCC dumps each of the intermediate states of the compilation, working<br>
>>> >> with C like trees. For example to get the optimized version of the<br>
>>> >> program, before generating the Assembler we have to do:<br>
>>> >><br>
>>> >> gcc -O2 prb.c -S -o prb.s -Wall -fdump-tree-optimized=/dev/stdout<br>
>>> >><br>
>>> >> This will generate:<br>
>>> >><br>
>>> >> __attribute__((noinline))<br>
>>> >> myFunc (long int i, int index)<br>
>>> >> {<br>
>>> >>   long int v;<br>
>>> >>   long unsigned int _1;<br>
>>> >>   long unsigned int _2;<br>
>>> >>   int * _3;<br>
>>> >>   int _4;<br>
>>> >>   long int _8;<br>
>>> >><br>
>>> >>   <bb 2> [local count: 1073741825]:<br>
>>> >>   _1 = (long unsigned int) index_7(D);<br>
>>> >>   _2 = _1 * 4;<br>
>>> >>   _3 = &v + _2;<br>
>>> >>   _4 = *_3;<br>
>>> >>   _8 = (long int) _4;<br>
>>> >>   v ={v} {CLOBBER};<br>
>>> >>   return _8;<br>
>>> >><br>
>>> >> }<br>
>>> >><br>
>>> >> This is the optimized SSA (static single assign) version of the<br>
>>> >> function (<a href="https://gcc.gnu.org/onlinedocs/gccint/SSA.html" rel="noreferrer" target="_blank">https://gcc.gnu.org/onlinedocs/gccint/SSA.html</a>)<br>
>>> >> As you can see in this version the code is already optimized, and our<br>
>>> >> operations not correctly generated. We expect to see something like:<br>
>>> >><br>
>>> >> __attribute__((noinline))<br>
>>> >> myFunc (long int i, int index)<br>
>>> >> {<br>
>>> >>   long int x;<br>
>>> >>   long int v;<br>
>>> >>   long unsigned int _1;<br>
>>> >>   long unsigned int _2;<br>
>>> >>   int * _3;<br>
>>> >>   int _4;<br>
>>> >>   long int _10;<br>
>>> >><br>
>>> >>   <bb 2> [local count: 1073741825]:<br>
>>> >>   x_6 = i_5(D) >> 3;<br>
>>> >>   v = x_6;<br>
>>> >>   _1 = (long unsigned int) index_9(D);<br>
>>> >>   _2 = _1 * 4;<br>
>>> >>   _3 = &v + _2;<br>
>>> >>   _4 = *_3;<br>
>>> >>   _10 = (long int) _4;<br>
>>> >>   v ={v} {CLOBBER};<br>
>>> >>   return _10;<br>
>>> >><br>
>>> >> }<br>
>>> >><br>
>>> >> In the first SSA version, we are lacking the shift operation:<br>
>>> >><br>
>>> >>   x_6 = i_5(D) >> 3;<br>
>>> >>   v = x_6;<br>
>>> >><br>
>>> >> So, we need to see in which of the optimizations and transformations<br>
>>> >> our code is lost:<br>
>>> >> To see all the steps we should execute:<br>
>>> >><br>
>>> >> gcc -O2 prb.c -S -o prb.s -Wall -fdump-tree-all<br>
>>> >><br>
>>> >> This will generate a lot, really a lot, of files in the same directory.<br>
>>> >> They have the name: prb.c.xxx.yyyy<br>
>>> >> Where xxx is the number of the step, and yyyy is the name of the step.<br>
>>> >> Each of the files contains the result of applying the changes, so what<br>
>>> >> I have done is looking in binary search where my code was lost.<br>
>>> >><br>
>>> >> I have found that the problem was in the first dead code elimination<br>
>>> >> step (prb.c.041t.cddce1)<br>
>>> >> GCC does not like the crap code that we are writing, as we are copying<br>
>>> >> a stack variable and then accessing directly to the memory:<br>
>>> >><br>
>>> >> v = x;<br>
>>> >> return ((int*)(&v))[index];<br>
>>> >><br>
>>> >> So, basically, it was assuming that we were only using v and not x, so<br>
>>> >> all the code modifying x is described (this optimization is called<br>
>>> >> "dead store code deletion"). Basically tries to remove all the code<br>
>>> >> that is affecting stack (temporary) variables that are never used.<br>
>>> >><br>
>>> >> I am not sure if this is a bug in GCC, so I will report it. But I have<br>
>>> >> fixed the problem writing the code in a proper way.<br>
>>> >><br>
>>> >> Sorry again for the long mail, I wanted to store the knowledge and<br>
>>> >> propagate it before I eventually will flush it. I like these things,<br>
>>> >> but I am not debugging the GCC optimization pipeline all the days.<br>
>>> >><br>
>>> >> --<br>
>>> >> Pablo Tesone.<br>
>>> >> <a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a><br>
>>> >><br>
>>><br>
>>><br>
>>> --<br>
>>> Pablo Tesone.<br>
>>> <a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a><br>
>>><br>
<br>
<br>
-- <br>
Pablo Tesone.<br>
<a href="mailto:tesonep@gmail.com" target="_blank">tesonep@gmail.com</a><br>
<br>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><span style="font-size:small;border-collapse:separate"><div>_,,,^..^,,,_<br></div><div>best, Eliot</div></span></div></div></div></div></div>