<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto" role="textbox" aria-label="Message Body"><div dir="ltr">I’d like to work on improving this, and I’ve got a few ideas thanks to your pointers. However there’s one issue that I can’t seem to work out.</div><div dir="ltr"><br></div><div dir="ltr">The funclets are treated as save and restore blocks for the associated function, which means that they’ll push/pop every callee saved register that the associated function uses, even if the funclets themselves don’t use them. I tried fixing this with some custom logic in X86FrameLowering::[spill/restore]CalleeSavedRegisters, but I couldn’t find a good way to determine which registers the block for the funclet actually use (without iterating over each instruction).</div><div dir="ltr"><br></div><div dir="ltr">Is there a better way to approach this?</div><div dir="ltr"><br><blockquote type="cite">On 26 Jun 2019, at 21:17, Reid Kleckner <rnk@google.com> wrote:<br><br></blockquote></div><blockquote type="cite"><div dir="ltr"><div dir="ltr">Yes, not much effort has been applied to optimizing Windows exception handling. We were primarily concerned with making it correct, and improving it hasn't been a priority. You can follow the code path through X86FrameLowering::emitPrologue with IsFunclet=true and see that it mechanically emits all the extra instructions mentioned above without any logic to skip such steps when not necessary.<div><br></div><div>However, while the mid-level representation we chose makes it hard to write these types of micro-level code quality optimizations, it allows the optimizers to do a variety of fancy things like heap to stack promotion on unique_ptr in the presence of exceptional control flow.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 25, 2019 at 4:08 AM Hamza Sood via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<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 style="overflow-wrap: break-word;">I’ve been experimenting with SEH handling in LLVM, and it seems like the unwind funclets generated by LLVM are much larger than those generated by Microsoft’s CL compiler.<div><br></div><div>I used the following code as a test:</div><div><br></div><div>void test() {</div><div> MyClass x;</div><div> externalFunction();</div>}<div><br></div><div>Compiling with CL, the unwind funclet that destroys ‘x’ is just two lines of asm:</div><div><br></div><div><font color="#000000" face="Menlo"><span style="font-size:11px">lea</span><span style="font-size:11px"> rcx, QWORD PTR x$[rdx]</span></font></div><div><font color="#000000" face="Menlo"><span style="font-size:11px">jmp </span></font><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">??1MyClass@@QEAA@XZ</span></div><div><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px"><br></span></div><div><font color="#000000" face="Menlo"><span style="font-size:11px">However when compiling with clang-cl, it seems like it sets up an entire function frame</span><span style="font-size:11px"> just for the destructor call:</span></font></div><div><font color="#000000" face="Menlo"><span style="font-size:11px"><br></span></font></div><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">mov qword ptr [rsp + 16], rdx</span></div></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">push rbp</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">.seh_pushreg 5</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">sub rsp, 32</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">.seh_stackalloc 32</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">Lea rbp, [rdx + 48]</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">.seh_endprologue</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">Lea rcx, [rbp - 16]</span></div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><span style="font-variant-ligatures:no-common-ligatures">call </span>"??1MyClass@@QEAA@XZ”</div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)">nop</div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(0,0,0)"><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">add rsp, 32</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">pop rbp</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ret</span></div></div><div><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px"><br></span></div><div><font color="#000000" face="Menlo"><span style="font-size:11px">Both were compiled with </span><span style="font-size:11px">“/c /O2 /MD /EHsc”</span></font></div><div><font color="#000000" face="Menlo"><span style="font-size:11px"><br></span></font></div><div><font color="#000000" face="Menlo"><span style="font-size:11px">Is LLVM missing a major optimisation here?</span></font></div></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</div></blockquote></body></html>