<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">Hi Nicolas,</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jan 8, 2019 at 1:51 PM Nicolas Cellier <<a href="mailto:nicolas.cellier.aka.nice@gmail.com">nicolas.cellier.aka.nice@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"> <div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Hi all,</div><div>particularly Clement and Eliot,</div><div><br></div><div>One of the most annoying limit of bytecode is the number of arguments (<16 in V3), not so much annoying for pure Smalltalk, but certainly so for FFI (FORTRAN 77 lacks structures so existing code base often have functions with many arguments).</div><div>For scientific Smalltalk, some of those old FORTRAN libraries are still around nowadays (LAPACK is an example).</div></div></div></div></div></div></div></div></div></blockquote><div><br></div><div>Agreed.  There are VW users out there with autogenerated code that requires more than 15 arguments.  Clément and I already have a design in mind, which is much more elegant than using the extra bit below.  However, it does require that we change the maximum Context stack size, which is one reason (the other being lack of time) why we haven't implemented this so far.</div><div><br></div><div>]In 2008 my closure design introduced indirection vectors for closed over arguments and among the five bytecodes added to implement it was the Create Array bytes ode that can do one of two things:</div><div><br></div><div>V3PlusClosures:</div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>138   10001010 jkkkkkkk<span class="gmail-Apple-tab-span" style="white-space:pre">              </span>Push (Array new: kkkkkkk) (j = 0)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                              </span>or<span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Pop kkkkkkk elements into: (Array new: kkkkkkk) (j = 1)</div></div><div><br></div><div>SistaV1:</div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>231<span class="gmail-Apple-tab-span" style="white-space:pre">           </span>11100111<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>jkkkkkkk<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Push (Array new: kkkkkkk) (j = 0)</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">                                              </span>&<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>Pop kkkkkkk elements into: (Array new: kkkkkkk) (j = 1)</div></div><div><br></div><div>This bytecode is used to create indirection vectors, and to create tuples of size <= 8.  e.g. { thisContext method symbolic. 2. 3. 4. 5. 6. 7. 8 }</div><div>#('89 <52> pushThisContext: </div><div>90 <81> send: method</div><div>91 <80> send: symbolic</div><div>92 <E8 02> pushConstant: 2</div><div>94 <E8 03> pushConstant: 3</div><div>96 <E8 04> pushConstant: 4</div><div>98 <E8 05> pushConstant: 5</div><div>100 <E8 06> pushConstant: 6</div><div>102 <E8 07> pushConstant: 7</div><div>104 <E8 08> pushConstant: 8</div><div>106 <E7 88> pop 8 into (Array new: 8)</div><div>108 <5C> returnTop</div><div><br></div><div>We call this version of the bytecode the cons array bytecode.  The other form, used to create in direction vectors is the greater array bytecode.</div><div><br></div><div>(c.f. { thisContext method symbolic. 2. 3. 4. 5. 6. 7. 8. 9 } which produces much more code but requires only 2 elements of stack depth).</div><div><br></div><div>There are also three bytecodes used to access indirection vectors:</div><div><br></div><div><div>V3PlusClosures:</div><div></div><div><span class="gmail-Apple-tab-span" style="white-space:pre"> </span>140   10001100 kkkkkkkk jjjjjjjj <span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Push Temp At kkkkkkkk In Temp Vector At: jjjjjjjj</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">      </span>141   10001101 kkkkkkkk jjjjjjjj <span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Store Temp At kkkkkkkk In Temp Vector At: jjjjjjjj</div><div><span class="gmail-Apple-tab-span" style="white-space:pre">     </span>142   10001110 kkkkkkkk jjjjjjjj <span class="gmail-Apple-tab-span" style="white-space:pre">    </span>Pop and Store Temp At kkkkkkkk In Temp Vector At: jjjjjjjj</div></div><div>SistaV1:</div><div><div><span class="gmail-Apple-tab-span" style="white-space:pre">       </span>251<span class="gmail-Apple-tab-span" style="white-space:pre">           </span>11111011 kkkkkkkk<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>sjjjjjjj<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Push Temp At kkkkkkkk In Temp Vector At: jjjjjjj, s = 1 implies remote inst var access instead of remote temp vector access </div><div>*<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>252<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>(3)<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>11111100 kkkkkkkk<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>sjjjjjjj<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Store Temp At kkkkkkkk In Temp Vector At: jjjjjjj s = 1 implies remote inst var access instead of remote temp vector access </div><div>*<span class="gmail-Apple-tab-span" style="white-space:pre"> </span>253<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>(3)<span class="gmail-Apple-tab-span" style="white-space:pre">   </span>11111101 kkkkkkkk<span class="gmail-Apple-tab-span" style="white-space:pre">     </span>sjjjjjjj<span class="gmail-Apple-tab-span" style="white-space:pre">      </span>Pop and Store Temp At kkkkkkkk In Temp Vector At: jjjjjjj s = 1 implies remote inst var access instead of remote temp vector access</div></div><div><br></div><div>So the insight is that if we pass arguments beyond 14 in an indirection vector we can have up to 15 + 127 = 142 arguments without needing any extra bits in a CompiledMethod header or range in a bytecode.  We simply pop arguments beyond the 14th into an indirection vector, using the cons array bytecode.  Yes, this is slow compared to "native" support, but such methods are extremely rare, and supporting them this way means we have less waste elsewhere.  It will require some sophistication in the Decompiler, but otherwise seems quite simple.  </div><div><br></div><div>With this design, as far as the VM is concerned the maximum argument count is still 15.  Only the image need bother with how to record the argument count for a method that has 15 or more arguments, and indeed a method with 15 arguments can still use all 15 arguments without having to create an indirection vector.  This isolates the effects to the compiler (arguments beyond the 14th in methods with more than 15 arguments must be accessed using the indirection vector bytecodes above), but otherwise are quite localized: indirection vector creation occurs immediately after normal argument marshaling and immediately before the send bytecode.</div><div><br></div><div>Does this design appeal to you?  If it does, then we should discuss when and how it should be implemented.  One thing would be to make the maximum size of a Context, defined at the image level by CompiledCode's LargeFrame class variable, but hard coded into the VM, some kind of VM parameter, e.g. stored in the image header and read at start-up.  It would be quite easy to add this.  If we did so we should also ensure the stack page size calculation allows for a stack page big enough for one or two huge frames.  Note that the design also means that a large stack is needed only to *marshal* arguments, not to activate a method with many arguments, since the excess arguments are stored in an indirection vector.  </div><div><br></div><div>P.S. Indeed we could use the scheme used for arbitrary sized tuples to marshall extra arguments, but this would affect code generation much more.  Different code would have to be used to marshall each argument beyond 15; whereas using the cons array bytecode</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"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>I patched the old Squeak compiler in Smallapack to workaround this limitation (it was easy enough to pass a single Array, and invoke FFI with many args).<br></div><div>In modern Pharo flavour, this is more involved with the new OpalCompiler (iit does not seem to be designed for extensibility as it seems necessary to patch many pieces/subclasses for a single feature change...).</div><div><br></div><div>But we now have Sista V1 bytecodes which removed a lot of limitations (# inst vars, #literals, max jump offset ...). Alas I don't see a modified limit for number of arguments (source: <a href="https://hal.inria.fr/hal-01088801/document" target="_blank">https://hal.inria.fr/hal-01088801/document</a> a bytecode set for adaptive optimization): there is still a limit of 4 reserved bits in compiled method header documented in link above.</div><div>Though, there is an adjacent unused bit now...</div><div>In Squeak,/Pharo, EncoderForSistaV1>>genSend:numArgs: suggests that the limit is 31 (sic)<br></div><div><br>    (nArgs < 0 or: [nArgs > 31]) ifTrue:<br>        [^self outOfRangeError: 'numArgs' index: nArgs range: 0 to: 31 "!!"].</div><div><br></div><div>or at least 2047 if we believe code below:</div><div><br>    "234        11101010    i i i i i j j j    Send Literal Selector #iiiii (+ Extend A * 32) with jjj (+ Extend B * 8) Arguments"<br></div><div><br></div><div><a href="https://github.com/pharo-project/pharo/blob/50992c3e5fed790b7e660954aee983f4681da658/src/Kernel-BytecodeEncoders/EncoderForSistaV1.class.st" target="_blank">https://github.com/pharo-project/pharo/blob/50992c3e5fed790b7e660954aee983f4681da658/src/Kernel-BytecodeEncoders/EncoderForSistaV1.class.st</a></div><div><br></div><div>Pharo also limit the numArgs to 15 whatever the encoding in CompiledMethod>><span class="gmail-m_-4305280914427063382gmail-pl-c1">newBytes:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">trailerBytes:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">nArgs:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">nTemps:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">nStack:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">nLits:</span><span class="gmail-m_-4305280914427063382gmail-pl-c1">primitive:</span><br></div><div><a href="https://github.com/pharo-project/pharo/blob/50992c3e5fed790b7e660954aee983f4681da658/src/Kernel/CompiledMethod.class.st" target="_blank">https://github.com/pharo-project/pharo/blob/50992c3e5fed790b7e660954aee983f4681da658/src/Kernel/CompiledMethod.class.st</a></div><div><br></div><div>But Squeak does not limit nArgs at all in</div><div>EncoderForSistaV1>>computeMethodHeaderForNumArgs:numTemps:numLits:primitive:<br></div><div><br></div><div>So my questions:</div><div>- is that doc up-to-date?</div><div>- if so, couldn't we expand the limit to 31 args by using the unused bit?</div><div><br></div><div>Note: there is another unused bit in V3 (not adjacent), and the double extended (send) byte code has room for 31 args in V3 too, since only the first 3 bits of second byte encode the type of operation...<br></div></div></div></div></div></div></div></div></div>
</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></div></div></div></div></div></div></div>