<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:PMingLiU;
panose-1:2 2 5 0 0 0 0 0 0 0;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Segoe UI Emoji";
panose-1:2 11 5 2 4 2 4 2 2 3;}
@font-face
{font-family:"\@PMingLiU";
panose-1:2 1 6 1 0 1 1 1 1 1;}
@font-face
{font-family:"Consolas\,";}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.EmailStyle20
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">Hi, Eli,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Why are you under the impression that <span style="font-family:"Consolas\,";color:black">
threw_exception() </span>will not be called if optimizations are enabled? I don’t know if the -EHa Spec is clearly described in MSFT Webs. At least this proposal has described the rules for both C & C++ code.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The very first rule clearly said that “<b>no exception can move in or out of _try region., i.e., no potential faulty instruction can be moved across _try boundary</b>”. As such the dereference of statement
<span style="font-family:"Consolas\,";color:blue">return</span><span style="font-family:"Consolas\,";color:black"> *(C*)</span><span style="font-family:"Consolas\,";color:#09885A">0
</span> must be kept in _try scope and the access-violation fault will be caught in _except handler where
<span style="font-family:"Consolas\,";color:black">threw_exception() </span>will be called.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I don’t see why Register allocation plays a part in this topic. I do see a serious problem in LLVM SJLJ today (All tests in MSVC’s Setjmp suite fail with -O2 that I will look into it soon). But I failed to see why HW exception is corelated
to setjmp/longjmp. These are two totally different features and the approaches employed are also totally different.
<o:p></o:p></p>
<p class="MsoNormal">It would be helpful if you can give one example why this proposal need to care about how registers are allocated.
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Again what we intend to do in this feature is to achieve these two points below. Please take a moment to read through it. Let me know if there is anything unclear.
<o:p></o:p></p>
<p class="MsoNormal">Thanks<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">--Ten<o:p></o:p></p>
<p class="MsoNormal"><o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">**** The rules for C code: ****<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> For C-code, one way (MSVC approach) to achieve SEH -EHa semantic is to<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> follow three rules. <span style="background:yellow;mso-highlight:yellow">
First, no exception can move in or out of _try<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="background:yellow;mso-highlight:yellow"> region., i.e., no "potential faulty instruction can be moved across _try<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="background:yellow;mso-highlight:yellow"> boundary</span>. Second, the order of exceptions for instructions 'directly'<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> under a _try must be preserved (not applied to those in callees).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Finally, global states (local/global/heap variables) that can be read<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> outside of _try region must be updated in memory (not just in register)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> before the subsequent exception occurs.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> **** The impact to C++ code: ****<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Although SEH is a feature for C code, -EHa does have a profound effect<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> on C++ side. When a C++ function (in the same compilation unit with<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> option -EHa ) is called by a SEH C function, a hardware exception occurs<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> in C++ code can also be handled properly by an upstream SEH _try-handler<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> or a C++ catch(...). As such, when that happens in the middle of an<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> object's life scope, the dtor must be invoked the same way as C++<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Synchronous Exception during unwinding process.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> Eli Friedman <efriedma@quicinc.com> <br>
<b>Sent:</b> Wednesday, April 15, 2020 4:29 PM<br>
<b>To:</b> Ten Tzen <tentzen@microsoft.com>; llvm-dev@lists.llvm.org<br>
<b>Cc:</b> rnk@google.com; Aaron Smith <aaron.smith@microsoft.com>; Joseph Tremoulet <jotrem@microsoft.com><br>
<b>Subject:</b> [EXTERNAL] RE: [RFC] [Windows SEH][-EHa] Support Hardware Exception Handling<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I still have basically the same concerns. I’ll try to give more concrete examples for what I’m concerned about.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Suppose I have something like the following:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:blue">typedef</span><span style="font-family:"Consolas\,";color:black"> </span><span style="font-family:"Consolas\,";color:blue">struct</span><span style="font-family:"Consolas\,";color:black"> C { </span><span style="font-family:"Consolas\,";color:blue">int</span><span style="font-family:"Consolas\,";color:black"> x[</span><span style="font-family:"Consolas\,";color:#09885A">2</span><span style="font-family:"Consolas\,";color:black">]; } C;<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:blue">void</span><span style="font-family:"Consolas\,";color:black"> threw_exception();<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:blue">void</span><span style="font-family:"Consolas\,";color:black"> z();<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black">C f() {<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> __try {<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> z();<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> </span><span style="font-family:"Consolas\,";color:blue">return</span><span style="font-family:"Consolas\,";color:black"> *(C*)</span><span style="font-family:"Consolas\,";color:#09885A">0</span><span style="font-family:"Consolas\,";color:black">;<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> } __except(</span><span style="font-family:"Consolas\,";color:#09885A">1</span><span style="font-family:"Consolas\,";color:black">) {<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> threw_exception();<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> }<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> C c = {</span><span style="font-family:"Consolas\,";color:#09885A">0</span><span style="font-family:"Consolas\,";color:black">};<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"> </span><span style="font-family:"Consolas\,";color:blue">return</span><span style="font-family:"Consolas\,";color:black"> c;<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black">}<o:p></o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"><o:p> </o:p></span></p>
<p class="MsoNormal" style="background:#FFFFFE"><span style="font-family:"Consolas\,";color:black"><o:p> </o:p></span></p>
<p class="MsoNormal">Currently, under your proposal, this won’t call threw_exception() if optimization is enabled, as far as I can tell. I have no idea if this is intentional: your proposal and your patch don’t contain or point to any documentation, and I
can’t find any documentation that describes this on Microsoft’s website. (I don’t really care what the answer is here; I care that there’s some documented answer to this question, and other questions like it.)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Constructing a testcase for the register allocation issues I mentioned before is hard because it’s sort of “random” based on the register allocation heuristics, but see
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Freviews.llvm.org%2FD77767&data=02%7C01%7Ctentzen%40microsoft.com%7C334ed759562941d3f2ba08d7e194d0d6%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637225901600853320&sdata=hTvtxO9OVy3fwzqX2EfLRb76Qb%2ByDlmdTHHLvuIrupQ%3D&reserved=0">
https://reviews.llvm.org/D77767</a> for the sort of issues that come up. Note that we mark setjmp returns_twice, which turns off certain optimizations. I don’t really like extending the usage of this sort of construct further, but if we are going to, we should
at least mark the new intrinsics returns_twice, so they get the same protection as setjmp.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Eli<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b>From:</b> Ten Tzen <<a href="mailto:tentzen@microsoft.com">tentzen@microsoft.com</a>>
<br>
<b>Sent:</b> Wednesday, April 15, 2020 1:51 PM<br>
<b>To:</b> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<b>Cc:</b> <a href="mailto:rnk@google.com">rnk@google.com</a>; Eli Friedman <<a href="mailto:efriedma@quicinc.com">efriedma@quicinc.com</a>>;
<a href="mailto:aaron.smith@microsoft.com">aaron.smith@microsoft.com</a>; Joseph Tremoulet <<a href="mailto:jotrem@microsoft.com">jotrem@microsoft.com</a>><br>
<b>Subject:</b> [EXT] [RFC] [Windows SEH][-EHa] Support Hardware Exception Handling<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Hi, <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">This is a spin-off of previous Windows SEH RFC below. This RFC only focus on supporting HW Exception Handling.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">A detailed implementation can be seen in here:
<a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ftentzen%2Fllvm-project%2Fcommit%2F8a2421c274b683051e456cbe12c177e3b934fb5e&data=02%7C01%7Ctentzen%40microsoft.com%7C334ed759562941d3f2ba08d7e194d0d6%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637225901600853320&sdata=Dx710jfERAD7dDrNvsuEFOYVOrYgeYcRLAlt3mmW8es%3D&reserved=0">
https://github.com/tentzen/llvm-project/commit/8a2421c274b683051e456cbe12c177e3b934fb5e</a><o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">It passes all MSVC SEH suite (excluding those with “Jumping out of _finally” ( _Local_Unwind)).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Thanks,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">--Ten<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> **** The rules for C code: ****<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> For C-code, one way (MSVC approach) to achieve SEH -EHa semantic is to<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> follow three rules. First, no exception can move in or out of _try<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> region., i.e., no "potential faulty instruction can be moved across _try<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> boundary. Second, the order of exceptions for instructions 'directly'<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> under a _try must be preserved (not applied to those in callees).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Finally, global states (local/global/heap variables) that can be read<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> outside of _try region must be updated in memory (not just in register)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> before the subsequent exception occurs.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> **** The impact to C++ code: ****<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Although SEH is a feature for C code, -EHa does have a profound effect<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> on C++ side. When a C++ function (in the same compilation unit with<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> option -EHa ) is called by a SEH C function, a hardware exception occurs<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> in C++ code can also be handled properly by an upstream SEH _try-handler<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> or a C++ catch(...). As such, when that happens in the middle of an<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> object's life scope, the dtor must be invoked the same way as C++<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Synchronous Exception during unwinding process.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> **** Design and Implementation: ****<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> A natural way to achieve the rules above in LLVM today is to allow an EH<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> edge added on memory/computation instruction (previous iload/istore<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> idea) so that exception path is modeled in Flow graph preciously.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> However, tracking every single memory instruction and potential faulty<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> instruction can create many Invokes, complicate flow graph and possibly<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> result in negative performance impact for downstream optimization and<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> code generation. Making all optimizations be aware of the new semantic<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> is also substantial.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> This design does not intend to model exception path at instruction<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> level. Instead, the proposed design tracks and reports EH state at<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> BLOCK-level to reduce the complexity of flow graph and minimize the<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> performance-impact on CPP code under -EHa option. Detailed<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> implementation described below.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- Two intrinsic are created to track CPP object scopes;<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> eha_scope_begin() and eha_scope_end(). _scope_begin() is immediately<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> added after ctor() is called and EHStack is pushed. So it must be an<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> invoke, not a call. With that it's also guaranteed an EH-cleanup-pad is<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> created regardless whether there exists a call in this scope. _scope_end<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> is added before dtor(). These two intrinsics make the computation of<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Block-State possible in downstream code gen pass, even in the presence<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> of ctor/dtor inlining.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- Two intrinsic, seh_try_begin() and seh_try_end(), are added for<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> C-code to mark _try boundary and to prevent from exceptions being moved<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> across _try boundary.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- All memory instructions inside a _try are considered as 'volatile' to<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> assure 2nd and 3rd rules for C-code above. This is a little<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> sub-optimized. But it's acceptable as the amount of code directly under<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> _try is very small.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- For both C++ & C-code, the state of each block is computed at the<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> same place in BE (WinEHPreparing pass) where all other EH tables/maps<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> are calculated. In addition to _scope_begin & _scope_end, the<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> computation of block state also rely on the existing State tracking code<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> (UnwindMap and InvokeStateMap).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- For both C++ & C-code, the state of each block with potential trap<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> instruction is marked and reported in DAG Instruction Selection pass,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> the same place where the state for -EHsc (synchronous exceptions) is<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> done.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- If the first instruction in a reported block scope can trap, a Nop is<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> injected before this instruction. This nop is needed to accommodate LLVM<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> Windows EH implementation, in which the address in IPToState table is<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> offset by +1. (note the purpose of that is to ensure the return address<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> of a call is in the same scope as the call address.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> -- The handler for catch(...) for -EHa must handle HW exception. So it<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> is 'adjective' flag is reset (it cannot be IsStdDotDot (0x40) that only<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> catches C++ exceptions).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b>From:</b> Ten Tzen <<a href="mailto:tentzen@microsoft.com">tentzen@microsoft.com</a>>
<br>
<b>Sent:</b> Friday, April 3, 2020 9:43 PM<br>
<b>To:</b> <a href="mailto:rnk@google.com">rnk@google.com</a><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>; Aaron Smith <<a href="mailto:aaron.smith@microsoft.com">aaron.smith@microsoft.com</a>><br>
<b>Subject:</b> RE: [EXTERNAL] Re: [llvm-dev] [RFC] [Windows SEH] Local_Unwind (Jumping out of a _finally) and -EHa (Hardware Exception Handling)<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Hi, Reid,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Nice to finally meet you<span style="font-family:"Segoe UI Emoji",sans-serif">😊</span>. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Thank you for reading through the doc and providing insightful feedbacks.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Yes I definitely can separate these two features if it’s more convenient for everyone.
<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">For now, the local_unwind specific changes can be separated and reviewed between these two commits:<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> git diff 9b48ea90f4c9ae7ef030719d6c0b49b00861cdde 06c81a4b6262445432a4166627b87bf595f5291b
<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">the -EHa changes can be read :<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"> git diff e943329ba00772f96fbc1fe5dec836cfd0707a38 9b48ea90f4c9ae7ef030719d6c0b49b00861cdde<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">My reply inline below in [Ten] lines.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">--Ten<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b>From:</b> Reid Kleckner <<a href="mailto:rnk@google.com">rnk@google.com</a>>
<br>
<b>Sent:</b> Friday, April 3, 2020 3:36 PM<br>
<b>To:</b> Ten Tzen <<a href="mailto:tentzen@microsoft.com">tentzen@microsoft.com</a>><br>
<b>Cc:</b> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>; Aaron Smith <<a href="mailto:aaron.smith@microsoft.com">aaron.smith@microsoft.com</a>><br>
<b>Subject:</b> [EXTERNAL] Re: [llvm-dev] [RFC] [Windows SEH] Local_Unwind (Jumping out of a _finally) and -EHa (Hardware Exception Handling)<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">UHi Ten,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><br>
Thanks for the writeup and implementation, nice to meet you.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I wonder if it would be best to try to discuss the features separately. My view is that catching hardware exceptions (/EHa) is critical functionality, but it's not clear to me if local unwind is truly worth implementing.
Having looked at the code briefly, it seemed like a large portion of the complexity comes from local unwind. Today, clang crashes on this small example that jumps out of a __finally block, but the intention was to reject the code and avoid implementing the
functionality. Clang does, in fact, emit a warning:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">$ clang -c t.cpp<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">t.cpp:7:7: warning: jump out of __finally block has undefined behavior [-Wjump-seh-finally]<br>
goto lu1;<br>
^<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Local unwind, in my view, is the user saying, "I wrote __finally, but actually I decided I wanted to catch the exception, so let's transfer to normal control flow now." It seems to me that the user already has a
way to express this: __except. I know the mapping isn't trivial and it's not exactly the same, but it seems feasible to rewrite most uses of local unwind this way.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] Right, I agree that to some degree a local_unwind can be viewed as another type of _except handler in the middle of unwinding. And true that some usage patterns can be worked around by rewriting SEH hierarchy.
But I believe the work can be substantial and risky, especially in an OS Kernel. Furthermore, to broaden the interpretation, local_unwind can also serve as a _filter (or even rethrow-like handler in C++ EH), and the target block is the final handler. See
the multi-local-unwind example in the doc. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Can you estimate the prevalence of local unwind? What percent of __finally blocks in your experience use non-local control flow? I see a lot of value in supporting catching hardware exceptions, but if we can avoid
carrying over the complexity of this local unwind feature, it seems to me that future generations of compiler engineers will thank us.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] I don’t have this data in hand. But what I know is that local_unwind is an essential feature to build Windows Kernel. One most important SEH test (the infamous xcpt4u.c) is composed of 88 tests; among them
there are 25 jumping-out-of-finally occurrences. Of course this does not translate to a percentage of local_unwind, but it does show us the significance of this feature to Windows. FYI Passing xcpt4u.c is the very first fundamental requirement before building
Windows Kernel.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">---<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Regarding trap / non-call / hardware exception handling, I guess I am a bit more blase about precisely modeling the control flow. As Eli mentions, we already have setjmp, and we already don't model it. Users file
bugs about problems with setjmp, and we essentially close them as "wontfix" and tell them to put more "volatile" on the problem until it stops hurting.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">One thing that I am very concerned about is the implications for basic block layout. Right now, machine basic block layout is very free-handed. Today, CodeGen puts labels around every potentially-throwing call,
does block layout without considering try regions, and then collapses adjacent label regions with the same landingpad during AsmPrinting. For MSVC C++ EH, state number stores and the ip2state table achieve the same goal.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] Yes, I saw that (pretty nice implementation actually). This design and implementation completely inherits current mechanism except that now it’s allowed to report EH state ranges that only contain memory/computation
instructions (for obvious reason). I’m not sure which part of that concerns you. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I think we need rules about how LLVM is allowed to transform the following code:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">void foo(volatile int *pv) {<br>
__try {<br>
if (cond()) {<br>
++*pv;<br>
__builtin_unreachable();<br>
}<br>
} __except(1) { }<br>
__try {<br>
if (cond()) {<br>
++*pv;<br>
__builtin_unreachable();<br>
}<br>
} __except(1) { }<br>
}<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">In this case, the *pv operation may throw, but I believe it would be semantics preserving to merge the two identical if-then blocks. The call.setup proposal I sent not long ago runs into the same issue. I have written
a patch to tail merge such similar blocks, but I have not landed it:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Freviews.llvm.org%2FD29428&data=02%7C01%7Ctentzen%40microsoft.com%7C334ed759562941d3f2ba08d7e194d0d6%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637225901600863316&sdata=C2rPxOIKLoLFbFgpRlT%2F4E4aaazpphHhTiL3HNF%2BI9Y%3D&reserved=0" target="_blank">https://reviews.llvm.org/D29428</a> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Even though it's not yet landed, I think we need to know if the transform is valid. If it is, then we need to do more than volatilize the try region to make EHa work.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] The merging should not happen. Per C-standard, a volatile must be read (or write) ONCE and only once (as long as it’s naturally aligned and can be accessed in one operation by HW). So merging two volatiles
violates the standard. I’m sure it’s currently well-protected in LLVM today. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">For a long time I've wanted regions of some kind in LLVM IR, and this use case has made me want to pick it up again. However, assuming that you want to land support for hardware exceptions without some kind of generalized
region support in the IR, I think we do need to do something about these blocks ending in unreachable in __try regions. The simplest thing that could possibly work is to make clang end the try region before unreachable. This would mean ending the block and
adding `invoke void @seh_try_end` after every unreachable. It would be redundant for noreturn calls, since those will already have an unwind edge, ensuring they remain in the try region.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] it’s interesting you mentioned this “blocks ending in unreachable in __try regions" here. With these two features supported, two remaining bugs in my ToDo list are; one setjmp() and one nested EH throw.
The second one seems caused by a _try_block ended with an unreachable. Yes, this is on my list. Will discuss with you guys further when I look into it. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">---<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Another interesting aspect of /EHa is how it affects C++ destructor cleanups. I am personally comfortable with the requirement that LLVM avoid moving around volatile instructions in __try blocks. LLVM is already
required to leave volatile operations in order. But I *am* concerned about C++ destructor scopes, which are much more frequent than __try. As you have described it, clang would invoke eha_scope_begin() / eha_scope_end() around the object lifetime, but are
you proposing to volatilize all memory operations in the region? If not, I see nothing that would prevent LLVM from moving potentially faulting operations in or out of this scope. We cannot require passes to look for non-local EH regions before doing code
motion. Would that be acceptable behavior? It could lead to some strange behavior, where a load is sunk to the point of use outside the cleanup region, but maybe users don't care about this in practice.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">[Ten] No, memory operations in C++ need not be volatilized. The order of exception in C++ code does not matter for -EHa. Potential trap instructions are free to move in/out of any EH region. The only criteria
is that when a HW exception is caught and handled, local live objects must be dtored gracefully, the same manner as C++ Synchronous exception. By reporting the EH state of those trap instructions, this is automatically done in LLVM today.
<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">---<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">To summarize, my feedback would be:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">1. Focus on __try and hardware exceptions first, the value proposition is clear and large. In particular, something has to be done about unreachable. Clang should already thread other abnormal control flow through
the region exit.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">2. Please gather some data on prevalence of local unwind to motivate the feature<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">3. Please elaborate on the design for /EHa C++ destructor cleanups and code motion<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">I hope that helps, and I'm sorry if I'm slow to respond, this is a tricky problem, and it's not my first priority.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Reid<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">On Wed, Apr 1, 2020 at 8:22 AM Ten Tzen via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-top:5.0pt;margin-right:0in;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Hi, all,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
The intend of this thread is to complete the support for Windows SEH. <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Currently there are two major missing features: Jumping out of a _finally and Hardware exception handling. <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
The document below is my proposed design and implementation to fully support SEH on LLVM.
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
I have completely implemented this design on a branch in repo: <a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ftentzen%2Fllvm-project&data=02%7C01%7Ctentzen%40microsoft.com%7C334ed759562941d3f2ba08d7e194d0d6%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637225901600863316&sdata=bDssTTeo13Fw1qheHB%2BX%2FG1rcJty0l%2FYFeDUdtZYpO4%3D&reserved=0" target="_blank">https://github.com/tentzen/llvm-project</a>.
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
It now passes MSVC’s in-house SEH suite. <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Sorry for this long write-up. For better readability, please read it on <a href="https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Ftentzen%2Fllvm-project%2Fwiki&data=02%7C01%7Ctentzen%40microsoft.com%7C334ed759562941d3f2ba08d7e194d0d6%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637225901600873310&sdata=YdaHyRNh4JigYkzQQPx7COVJXYz2WTjaqr2E18RHo6Y%3D&reserved=0" target="_blank">
https://github.com/tentzen/llvm-project/wiki</a><o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Special thanks to Joseph Tremoulet for his earlier comments and suggestions.<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Note: I just subscribed llvm-dev, probably not in the list yet. So please reply with my email address (<a href="mailto:tentzen@microsoft.com" target="_blank">tentzen@microsoft.com</a>) explicitly in To-list. <o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
Thanks,<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
<o:p></o:p></p>
<p class="MsoNormal" style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:.5in">
--Ten<o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
</div>
</div>
</body>
</html>