Doesn't modern hardware have pretty good branch prediction? In which case the order of the branches may not matter unless it's a long chain of calls? Vs say an inner loop that hasn't been inlined?<div><br></div><div>Either way, I'd love be stay in the loop on this topic, for work I'm building a strongly normalizing language that supports both strict and call by need evaluation strategies. <span></span><br><br>On Friday, October 23, 2015, Ryan Newton <<a href="mailto:rrnewton@gmail.com">rrnewton@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><ol><li>Small tweaks: The CMM code above seems to be <i>betting</i> than the thunk is unevaluated, because it does the stack check and stack write <i>before</i> the predicate test that checks if the thunk is evaluated (<span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(167,29,93)">if</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap"> (R1 & </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(0,134,179)">7</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap"> != </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(0,134,179)">0</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap">) </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(167,29,93)">goto</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap"> c3aO; </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(167,29,93)">else</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap"> </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(167,29,93)">goto</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap"> c3aP;</span>). With a bang-pattern function, couldn't it make the opposite bet? That is, branch on whether the thunk is evaluated first, and then the wasted computation is only a single correctly predicted branch (and a read of a tag that we need to read anyway). <br></li></ol></div></div></blockquote><div>Oh, a small further addition would be needed for this tweak. In the generated code above "<span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap">Sp = Sp + </span><span style="font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap;color:rgb(0,134,179)">8</span><span style="color:rgb(51,51,51);font-family:Consolas,'Liberation Mono',Menlo,Courier,monospace;font-size:12px;line-height:18.2px;white-space:pre-wrap">;"</span> happens <i>late</i>, but I think it could happen right after the call to the thunk. In general, does it seem feasible to separate the slowpath from fastpath as in the following tweak of the example CMM?</div><div><br></div><div><div><br></div><div><font face="monospace, monospace"><b> // Skip to the chase if it's already evaluated:</b></font></div><div><font face="monospace, monospace"><b> start:</b></font></div><div><font face="monospace, monospace"><b> if (R2 & 7 != 0) goto fastpath; else goto slowpath;</b></font></div><div><font face="monospace, monospace"><b><br></b></font></div><div><font face="monospace, monospace"><b> slowpath: // Formerly c3aY</b></font></div><div><font face="monospace, monospace"><b> if ((Sp + -8) < SpLim) goto c3aZ; else goto c3b0;</b></font></div><div><font face="monospace, monospace"><b> c3aZ:</b></font></div><div><font face="monospace, monospace"><b> // nop</b></font></div><div><font face="monospace, monospace"><b> R1 = PicBaseReg + foo_closure;</b></font></div><div><font face="monospace, monospace"><b> call (I64[BaseReg - 8])(R2, R1) args: 8, res: 0, upd: 8;</b></font></div><div><font face="monospace, monospace"><b> c3b0:</b></font></div><div><font face="monospace, monospace"><b> I64[Sp - 8] = PicBaseReg + block_c3aO_info;</b></font></div><div><font face="monospace, monospace"><b> R1 = R2;</b></font></div><div><font face="monospace, monospace"><b> Sp = Sp - 8;</b></font></div><div><br></div><div><font face="monospace, monospace"><b> call (I64[R1])(R1) returns to fastpath, args: 8, res: 8, upd: 8;</b></font></div><div><font face="monospace, monospace"><b> // Sp bump moved to here so it's separate from "fastpath"</b></font></div><div><font face="monospace, monospace"><b> Sp = Sp + 8;</b></font></div><div><font face="monospace, monospace"><b><br></b></font></div><div><font face="monospace, monospace"><b> fastpath: // Formerly c3aO</b></font></div><div><font face="monospace, monospace"><b> if (R1 & 7 >= 2) goto c3aW; else goto c3aX;</b></font></div><div><font face="monospace, monospace"><b> c3aW:</b></font></div><div><font face="monospace, monospace"><b> R1 = P64[R1 + 6] & (-8);</b></font></div><div><font face="monospace, monospace"><b> call (I64[R1])(R1) args: 8, res: 0, upd: 8;</b></font></div><div><font face="monospace, monospace"><b> c3aX:</b></font></div><div><font face="monospace, monospace"><b> R1 = PicBaseReg + lvl_r39S_closure;</b></font></div><div><font face="monospace, monospace"><b> call (I64[R1])(R1) args: 8, res: 0, upd: 8;</b></font></div></div><div><br></div><div><br></div><div><br></div></div></div></div>
</blockquote></div>