<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jan 6, 2018 at 7:16 PM, Eliot Miranda <span dir="ltr"><<a href="mailto:eliot.miranda@gmail.com" target="_blank">eliot.miranda@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Clément,<div class="gmail_extra"><br><div class="gmail_quote"><span class="gmail-">On Sat, Jan 6, 2018 at 12:00 AM, Clément Bera <span dir="ltr"><<a href="mailto:bera.clement@gmail.com" target="_blank">bera.clement@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Eliot, (sorry long mail but I think you will enjoy this discussion; read it)<div><br></div><div>Looking deeper into tight loops performance to get some boost out of Sista, I have questions and remarks.</div><div><br></div><div>First thing is when I compare 3 versions or array copies (code at the bottom of mail).</div><div>Version [1] : copyArray : written in plain Smalltalk</div><div>Version [2] : fastCopyArray : sista generated code (only unchecked operations)</div><div>Version [3] : primCopyArray : simply call the machine code primitive</div><div><br></div><div>For small arrays everything is fine, sista code is good. But for large arrays I get the following results (here a copy of 1000 elements):</div><div>1) 85k/second</div><div>2) 390k/second</div><div>3) 2,000k/second</div><div><br></div><div>So the sista version is ~5x compared to pure smalltalk code, but ~5x slower to the primitive.</div><div><br></div><div>My guess is that the reason for this is that the hand written primitive is written with a 7 instructions tight loop (2 additions, 1 read, 1 write, 2 jumps, 1 comp), while the sista generated code requires 6 temp read, 1 temp write and a couple cheap instructions in addition. We discussed earlier than register allocation might bring a 20% performance boost, but it seems tight loops are quite important and in this case the performance difference is around 5x (500%).</div></div></blockquote><div><br></div></span><div>This is for me very good news.  I thought there was potential in good register allocation and this confirms it.</div><span class="gmail-"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>So big question is that should I had more macro-instructions (let's call them stubs) in the sista extended bytecode set such as bytesEquals which bulkCompares the bytes of 2 objects, since performance difference is bigger than expected, or should I wait for better register allocation algorithm ? </div></div></blockquote><div><br></div></span><div>I think *very much* we should take the register allocation route.  If we go the other route we're following the well-worn path of systems like Python where basic execution performance is poor and is solved by special-casing specific operations and implementing them with C primitives.  Eventually one gets to a system where all the performance critical parts are in C, which has a couple of really bad effects.  First, nothing one does to increase basic performance has any effect since all the time is in C, so one looses the impetus to improve basic language performance.  Second, much of the system is in large mono;ethic and unchangeable chunks of C, so the system looses its dynamic language derived flexibility.  We definitely want to move in the other direction.  So let's instead put in the effort to make register allocation work well.  We know what has to be done.  It isn't easy but the long-term benefits are great.  We should be able to make many primitives entirely optional in the JIT.  [Brain fart: We may even be able to generate primitives for the Interpreter versions using our framework]</div><span class="gmail-"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>I believe hand-written stub will always be faster than sista generated code (since a couple things can't be expressed at the bytecode level), but based on multiple benchmarks I think if the better register allocator generates code with the same number of memory read-write (including temp access on stack) the difference should be less than 20% performance difference as we expected. <br></div></div></blockquote><div><br></div></span><div>Good, then i think we are in agreement.  We can live without stubs.</div><span class="gmail-"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>It's not clear how many stubs would be needed. I have a prototype of array copy, other things such as indexOf or fillObject are needed. Maybe I can add only 3-4 for now to show massive speed-ups like we did with bytesEquals. <br></div></div></blockquote><div><br></div></span><div>I don't think it's the right way to go.  Having these for performaNc comparison is good, but having them as general features of inline primitives is, I think, a long-term strategic error, as outlined above.</div></div></div></div></blockquote><div><br></div><div>Yeah my main concern is with maintaining huge Assembly code stubs. I did it for byteEquals already, and this is hard to maintain (80 lines in total for the stub ?). Having 5 of those is anooying.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>Let's note that we could try to share the stubs with normal primitives (primStringReplace with the arraycopy stub for example) to avoid too much maintenance cost. The problem is that normal primitives have complexity with all the checks at the beginning, then a generic case (copy without knowing anything about the operands) while sista stubs have no checks, but complexity is in generating better code when some operands are constants. So it's not really the same thing.<br></div></div></blockquote><div><br></div></span><div>Yes, this does seem interesting.  But medium-term interpreter performance isn't critical.  Let's get the JIT performance good first.</div></div></div></div></blockquote><div><br></div><div>I mean primitive in the JIT like genPrimStringReplace versus inline primitives. </div><div><br></div><div>I would love to have interpreter primitives and the main interpreter loop generated from the JIT back-end, so switching from the interpreter primitives and the interpreter loop to jitted code would be cheap (same calling conventions, etc.). But let's do that later.</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>Second question is about RTL code for tight loops. I looked into V8 and it seems that for tight loop they try to use backward conditional instead of forward conditional + backward unconditional. What do you think will be the difference if I do it for array copy (the copy is guaranteed not to be empty at this point, example below Original-New) ? I wonder if conditional back jumps are not slower than conditional forward jumps or something like that (I am always confused with branch performance, and the performance is different depending on caches so small benchs may not be relevant). What's your take on this ?<br></div></div></blockquote><div><br></div></span><div>That's a good question.  I don't know.  The current layout is dictated by the fact that backwards jumps are interrupt points along with byttecode-to-machine-code-pc-<wbr>mappng.  The code look like this, this being from SequenceableCollection>><wbr>indexOf:startingAt:</div><div><br></div><div><div><span class="gmail-m_-5924498853493625753gmail-Apple-tab-span" style="white-space:pre-wrap">   </span>startIndex to: self size do: [ :index |</div><div><span class="gmail-m_-5924498853493625753gmail-Apple-tab-span" style="white-space:pre-wrap">               </span>(self at: index) == anElement ifTrue: [ ^index ] ].</div></div><div><span style="white-space:pre-wrap">  ^0</span><br></div><div><span style="white-space:pre-wrap"><br></span></div><div>The end of the loop's bytecodes are</div><div><br></div><div><div>61 <B0> send: +</div><div>62 <6A> popIntoTemp: 2</div><div>63 <A3 ED> jumpTo: 46</div><div>65 <75> pushConstant: 0</div><div>66 <7C> returnTop</div></div><div><br></div><div>is compiled into</div><div><br></div><div><div>000010f2: movl $0x00100028=#+, %ecx : B9 28 00 10 00 </div><div>000010f7: call .+0xfffff37c (0x00000478=ceSend1Args) : E8 7C F3 FF FF </div><div>IsSendCall + bc 61/62:</div><div>000010fc: movl %edx, -28(%ebp) : 89 55 E4 </div><div>000010ff: movl %ds:0x80000='stackLimit', %eax : A1 00 00 08 00 </div><div>00001104: cmpl %eax, %esp : 39 C4 </div><div>00001106: jnb .+0xffffff5e (0x0000106a=indexOf:<wbr>startingAt:@6A) : 0F 83 5E FF FF FF </div><div>0000110c: call .+0xfffffa87 (0x00000b98=<wbr>ceCheckForInterruptsTrampoline<wbr>) : E8 87 FA FF FF </div><div>IsRelativeCall:</div><div>HasBytecodePC bc 62/63:</div><div>00001111: jmp .+0xffffff54 (0x0000106a=indexOf:<wbr>startingAt:@6A) : E9 54 FF FF FF </div><div>00001116: movl $0x00000001, %edx : BA 01 00 00 00 </div><div>0000111b: movl %ebp, %esp : 89 EC </div><div>0000111d: popl %ebp : 5D </div><div>0000111e: ret $0x000c : C2 0C 00 </div></div><div><br></div><div>So the suspension point is the pc following the call of ceCheckForInterruptsTrampoline<wbr>, which is 00001111.  If execution continues after interruption the jump is taken and the loop continues, which is as required.  [I remember an early state in Cog;'s development at Qwaq when I had't implemented this and loops were terminating every time there was an interrupt, such as when one wiggled the mouse,  The system almost worked; it wad quite bizarre :-) ].</div><div><br></div><div>But notice that the conditional jump at 00001106 *is* backwards as you desire.  It seems we're already doing the right thing, at least with the backwards jump.  What do we do with backwards jumps in SistaV1 inline primitives?</div></div></div></div></blockquote><div><br></div><div>I have added backjumpNoInterrupt.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div></div><div>And then the question is that should I be able to generate that kind of loop from the sista back-end (I don't really like it, I prefer to limit the number of back-jumps else it makes some things harder) ? Else I can do it in the stubs for most critical performance things and ignore it in other cases.<br></div><div><br></div><div><b>Original</b></div><div><div><span style="white-space:pre-wrap">      </span>instr := cogit CmpR: startReg R: stopReg.</div><div><span style="white-space:pre-wrap">        </span>jumpFinished := cogit JumpBelow: 0.</div><div><span style="white-space:pre-wrap">      </span>cogit MoveXwr: repStartReg R: replReg R: TempReg.</div><div><span style="white-space:pre-wrap">        </span>cogit MoveR: TempReg Xwr: startReg R: arrayReg.</div><div><span style="white-space:pre-wrap">  </span>cogit AddCq: 1 R: startReg.</div><div><span style="white-space:pre-wrap">      </span>cogit AddCq: 1 R: repStartReg.</div><div><span style="white-space:pre-wrap">   </span>cogit Jump: instr.</div><span style="white-space:pre-wrap">  </span>jumpFinished jmpTarget: (jumpEmpty jmpTarget: cogit genPrimReturn).</div><div>Total 7 instructions in the loop</div><div><b>New</b></div><div><div><span style="white-space:pre-wrap">       </span>instr := cogit MoveXwr: repStartReg R: replReg R: TempReg.</div><div><span style="white-space:pre-wrap">       </span>cogit MoveR: TempReg Xwr: startReg R: arrayReg.</div><div><span style="white-space:pre-wrap">  </span>cogit AddCq: 1 R: startReg.</div><div><span style="white-space:pre-wrap">      </span>cogit AddCq: 1 R: repStartReg.</div><div>        cogit CmpR: startReg R: stopReg.</div><div><span style="white-space:pre-wrap">        </span>cogit JumpGreaterOrEqual: instr.</div><div>        jumpEmpty jmpTarget: cogit genPrimReturn.</div></div><div>Total 6 instructions in the loop</div><div>But a conditional back-jump.</div></div></blockquote><div><br></div></span><div>Yes, the latter looks better and matches what we're doing with the backwards jump.</div><span class="gmail-"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div>NB : Eliot I tried incrementing the pointer instead of the index and use a 0 displacement read/writes and it made no performance difference on Intel. Saves a register in the loop but we don't need them. It makes a difference in the inlined stub however since other registers can be used for other things in the method where it's inlined.</div></div></blockquote><div><br></div></span><div>In general we should always try to reduce register pressure.  So if we can write the primitive using incremented pointers instead of base registers and incremented indexes we should.  In some complex loop, once we have good register allocation, that extra register will be put to good use and we can expect that we would see increased performance.</div><span class="gmail-"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><br></div><div><b>[1]</b> Smalltalk code</div><div><div>1 to: (y - x + 1) do: [ :i |</div><div><span style="white-space:pre-wrap">                </span>array2 at: y2 + i put: (array at: x + i) ].</div><div><b>[2] </b>(sista bytecodes of the tight loop)</div><div><div>32 <45> pushTemp: 5</div><div>33 <46> pushTemp: 6</div><div>34 <F8 F3 87> smiLessOrEqual:</div><div>37 <EF 1B> jumpFalse: 66</div><div>39 <43> pushTemp: 3</div><div>40 <45> pushTemp: 5</div><div>41 <40> pushTemp: 0</div><div>42 <45> pushTemp: 5</div><div>43 <F8 10 88> pointerAt:</div><div>46 <F8 B8 8B> pointerAt:put:</div><div>49 <D8> pop</div><div>50 <45> pushTemp: 5</div><div>51 <E1 00 E8 01> pushConstant: 1</div><div>55 <F8 D0 87> smiAdd:</div><div>58 <D5> popIntoTemp: 5</div><div>59 <E1 FF E8 DE> pushConstant: -34</div><div>63 <F8 70 97> backjumpNoInterrupt</div></div></div></div></blockquote><div><br></div></span><div>I'm confused by "59 <E1 FF E8 DE> pushConstant: -34"; what's that?  Are these actually extension bytes for the backwards jump?  Can't be because the E8 wouldn't be there.  So what is this?</div></div></div></div></blockquote><div><br></div><div>-34 is the distance of the backward jump pushed on stack. #backjumpNoInterrupt is an inlined primitive expecting the distance to be on stack with a long form pushInteger bytecode (the guaranteed long form simplifies Cogit's scan for fixups and latestContinuation). That way no inline primitive require extensions and the callPrimitive bytecode does not use any extensions. I changed the inlined primitives from 6000 and onwards to be jumps with different number of parameters (jumpReadOnly, jumpYoung, backwardJumpNotInterrupt, etc.). Works really well. I am finishing patches to Scorch and evaluation of performance to commit to VMMaker.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><b>[3]</b> primitive call</div><div>array2 replaceFrom: x to: y with: array startingAt: y2<br></div><div>Current tight loop RTL:</div><div><div><span style="white-space:pre-wrap">      </span>instr := cogit CmpR: startReg R: stopReg.</div><div><span style="white-space:pre-wrap">        </span>jumpFinished := cogit JumpBelow: 0.</div><div><span style="white-space:pre-wrap">      </span>cogit MoveXwr: repStartReg R: replReg R: TempReg.</div><div><span style="white-space:pre-wrap">        </span>cogit MoveR: TempReg Xwr: startReg R: arrayReg.</div><div><span style="white-space:pre-wrap">  </span>cogit AddCq: 1 R: startReg.</div><div><span style="white-space:pre-wrap">      </span>cogit AddCq: 1 R: repStartReg.</div><div><span style="white-space:pre-wrap">   </span>cogit Jump: instr.</div><div><span style="white-space:pre-wrap">       </span>jumpFinished jmpTarget: (jumpEmpty jmpTarget: cogit genPrimReturn).</div></div></div></div></blockquote><div><br></div></span><div>Note that if you were to maintain a pointer and the delta between the source and the destination pointers, instead of a source pointer and a destination pointer, you'd only have to increment one register (for the one pointer) in the loop which would save another instruction.</div><span class="gmail-"><div> </div></span></div></div></div></blockquote><div><br></div><div>Yeah I wanted to do that at some point. It can be improved.</div><div><br></div><div>I did the simplest that could possibly work and the performance is better than the C primitive so I did not go any further. Remember that later we'll need to compare this performance with the sista generated code. For byte copies it needs to be improved (slower than C primitive).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><span class="gmail-m_-5924498853493625753gmail-HOEnZb"><font color="#888888"><div><br></div>-- <br><div class="gmail-m_-5924498853493625753gmail-m_8351852914845639042gmail_signature"><div dir="ltr"><span style="font-size:12.8px">Clément Béra</span><div style="font-size:12.8px"><a href="https://clementbera.wordpress.com/" target="_blank">https://clementbera.wordpress.<wbr>com/</a><br></div><div style="font-size:12.8px"><span style="line-height:16px">Bâtiment B <a href="https://maps.google.com/?q=40,+avenue+Halley+59650%C2%A0Villeneuve+d'Ascq&entry=gmail&source=g" target="_blank">40, avenue Halley 59650 </a></span><span style="font-weight:bold;line-height:16px"><a href="https://maps.google.com/?q=40,+avenue+Halley+59650%C2%A0Villeneuve+d'Ascq&entry=gmail&source=g" target="_blank">Villeneuve d</a>'<a href="https://maps.google.com/?q=40,+avenue+Halley+59650%C2%A0Villeneuve+d'Ascq&entry=gmail&source=g" target="_blank">Ascq</a></span></div></div></div>
</font></span></div></div>
</blockquote></span></div><br>This is exciting!!</div><div class="gmail_extra"><br><div class="gmail-m_-5924498853493625753gmail_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>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><span style="font-size:12.8px">Clément Béra</span><div style="font-size:12.8px">Pharo consortium engineer</div><div style="font-size:12.8px"><a href="https://clementbera.wordpress.com/" target="_blank">https://clementbera.wordpress.com/</a><br></div><div style="font-size:12.8px"><span style="line-height:16px">Bâtiment B 40, avenue Halley 59650 </span><span style="font-weight:bold;line-height:16px">Villeneuve d'Ascq</span></div></div></div>
</div></div>