<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Jun 21, 2018, at 3:37 PM, João Paulo Labegalini de Carvalho <<a href="mailto:jaopaulolc@gmail.com" class="">jaopaulolc@gmail.com</a>> wrote:</div><div class=""><div dir="ltr" class="">John,<div class=""><br class=""></div><div class="">Thank you very much! Pushing a cleanup via EHStack did the trick!</div></div></div></blockquote><div><br class=""></div>Note that if you're interested in making this user-facing, you should probably add something to the JumpDiagnostics checker to make sure you can't jump *into* this thing.</div><div><br class=""></div><div>I think the analysis part is fairly easy to find examples of how to modify; the more subtle thing is that you need to call `getCurFunction()->setHasBranchProtectedScope()` to ensure that the analysis is run.</div><div><br class=""></div><div>John.</div><div><br class=""><blockquote type="cite" class=""><div class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Jun 21, 2018 at 3:02 PM John McCall <<a href="mailto:rjmccall@apple.com" class="">rjmccall@apple.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Jun 21, 2018, at 1:26 PM, João Paulo Labegalini de Carvalho via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_4264541401840967787Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class="">Reid,<div class=""><br class=""></div><div class="">Thank you for the quick reply.</div><div class=""><br class=""></div><div class="">I am not worried about exceptions, I am more interested in the concept of finally, a block of code which is guaranteed to execute "no matter what" happens inside the try block. But you are right, my example is a little crude.</div><div class=""><br class=""></div><div class="">My goal is to guarantee that __end_spec() is called on every exit of __speculate blocks. Even if __speculate is within a for/while-loop and a there is a break/return inside of it. The __speculate block annotates a block that will be transformed into a two-path code: (i) instrumented with software load/store barriers to detect conflicts and (ii) instrumentation-free that will either execute under mutual exclusion or using hardware support for speculative execution.</div><div class=""><br class=""></div><div class="">I am already generating correct code when the compound statement does not have any statements which cause control flow to "escape" the block within which __speculate resides.</div><div class=""><br class=""></div><div class="">My question was motivated more by my worry to no generate an ugly solution (e.g. fixing up every exit of the block) or (re)coding something that is(might have been) implemented.</div></div></div></blockquote><div class=""><br class=""></div></div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class="">You're hacking the compiler, right?  In the IRGen for your new declaration (statement?), push a cleanup.</div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""></div><div class="">John.</div></div><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Thu, Jun 21, 2018 at 1:51 PM Reid Kleckner <<a href="mailto:rnk@google.com" target="_blank" class="">rnk@google.com</a>> wrote:<br class=""></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" class="">Hi,<div class=""><br class=""></div><div class="">From your example, it's not clear to me what you want this feature to do. Do you want the finally block to execute:</div><div class="">1. When a C++ exception is thrown?</div><div class="">2. When normal control flow leaves the scope (goto, break, return)?</div><div class="">3. When a hardware trap occurs (div by zero, access violation, segv)?</div><div class=""><br class=""></div><div class="">3 is not implementable with LLVM today. It's not implemented properly even for __try / __finally.</div><div class=""><br class=""></div><div class="">1 and 2 are simple and can be accomplished with regular C++ destructors. You can do things like:</div><div class="">struct Cleanup {</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>Cleanup(std::function<void()> f) : f(f) {}</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>~Cleanup() { f(); }</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>std::function<void()> f;<br class="">};</div><div class="">void foo() {</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>Cleanup c([&]() {</div><div class="">   <span class="m_4264541401840967787Apple-converted-space"> </span>.. // any finally code here</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>});</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>// any try body code here</div><div class="">}</div><div class=""><br class=""></div><div class="">Reid</div></div><br class=""><div class="gmail_quote"></div><div class="gmail_quote"><div dir="ltr" class="">On Thu, Jun 21, 2018 at 9:26 AM João Paulo Labegalini de Carvalho via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a>> wrote:<br class=""></div></div><div class="gmail_quote"><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" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">I am working on a block annotation which should have a try-finally semantics. For example, given a code like this:</div><div class=""><br class=""></div><div class=""><b class="">__speculate</b><span class="m_4264541401840967787Apple-converted-space"> </span><font color="#ff0000" class=""><b class="">{</b></font></div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>if (x > 2) {</div><div class="">   <span class="m_4264541401840967787Apple-converted-space"> </span>y = 0;</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div class=""><font color="#ff0000" class=""><b class="">}</b></font></div><div class=""><br class=""></div><div class="">clang should produce something like</div><div class=""><br class=""></div><div class="">__exec_mode =<span class="m_4264541401840967787Apple-converted-space"> </span><b class=""><font color="#ff0000" class="">__begin_spec</font></b>();</div><div class="">if (__exec_mode == SW) {</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>if (__read_barrier(&x) > 2) {</div><div class="">   <span class="m_4264541401840967787Apple-converted-space"> </span>__write_barrier(&y, 0);</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span><font color="#ff0000" class=""><b class="">__end_spec</b></font>();</div><div class="">} else {</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>if (x > 2) {</div><div class="">   <span class="m_4264541401840967787Apple-converted-space"> </span>y = 0;</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div class=""> <span class="m_4264541401840967787Apple-converted-space"> </span><b class=""><font color="#ff0000" class="">__end_spec</font></b>();</div><div class="">}</div><div class=""><br class=""></div><div class="">such that __end_spec() is guaranteed to be called on every exit of the  __speculate {}  block.</div><div class=""><br class=""></div><div class="">It is my understanding that Clang implements try-finally statements as a Microsoft Specific Extension. However, I would not like to have such restriction, since such extension is only available for a reduced number of targets.</div><div class=""><br class=""></div><div class="">My question is, how should I implement try-finally semantics? Through an IR pass (fixing up every exit with a call to __end_spec()) or it is possible to extend from CXXTryStmt (reusing some CodeGen code)?</div>--<span class="m_4264541401840967787Apple-converted-space"> </span><br class=""><div dir="ltr" class="m_4264541401840967787m_-2154533798381837922m_-7616334774840605940m_688693615537025702m_863750444281404145gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class="">João Paulo L. de Carvalho<br class="">Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:jaopaulolc@gmail.com" target="_blank" class="">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank" class="">joao.carvalho@ic.unicamp.br</a></div></div></div></div></blockquote></div><div class="gmail_quote"><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">_______________________________________________<br class="">cfe-dev mailing list<br class=""><a href="mailto:cfe-dev@lists.llvm.org" target="_blank" class="">cfe-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br class=""></blockquote></div></blockquote></div></div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important" class="">--<span class="m_4264541401840967787Apple-converted-space"> </span></span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><div dir="ltr" class="m_4264541401840967787gmail_signature" data-smartmail="gmail_signature" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none"><div dir="ltr" class=""><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class="">João Paulo L. de Carvalho<br class="">Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:jaopaulolc@gmail.com" target="_blank" class="">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank" class="">joao.carvalho@ic.unicamp.br</a></div></div></div><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important" class="">_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline!important" class="">cfe-dev mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><a href="mailto:cfe-dev@lists.llvm.org" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank" class="">cfe-dev@lists.llvm.org</a><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none" class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px" target="_blank" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></div></blockquote></div><br class=""></div></blockquote></div>-- <br class=""><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr" class=""><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class="">João Paulo L. de Carvalho<br class="">Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:jaopaulolc@gmail.com" target="_blank" class="">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class=""><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank" class="">joao.carvalho@ic.unicamp.br</a></div></div></div>
</div></blockquote></div><br class=""></body></html>