<div dir="ltr">John,<div><br></div><div>Thank you very much! Pushing a cleanup via EHStack did the trick!</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018 at 3:02 PM John McCall <<a href="mailto:rjmccall@apple.com">rjmccall@apple.com</a>> wrote:<br></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"><div><br><blockquote type="cite"><div>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">cfe-dev@lists.llvm.org</a>> wrote:</div><br class="m_4264541401840967787Apple-interchange-newline"><div><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">Reid,<div><br></div><div>Thank you for the quick reply.</div><div><br></div><div>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><br></div><div>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><br></div><div>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><br></div><div>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><br></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div>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"><div><br></div><div>John.</div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div><div><blockquote type="cite"><div><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"><br><div class="gmail_quote"><div dir="ltr">On Thu, Jun 21, 2018 at 1:51 PM Reid Kleckner <<a href="mailto:rnk@google.com" target="_blank">rnk@google.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">Hi,<div><br></div><div>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>1. When a C++ exception is thrown?</div><div>2. When normal control flow leaves the scope (goto, break, return)?</div><div>3. When a hardware trap occurs (div by zero, access violation, segv)?</div><div><br></div><div>3 is not implementable with LLVM today. It's not implemented properly even for __try / __finally.</div><div><br></div><div>1 and 2 are simple and can be accomplished with regular C++ destructors. You can do things like:</div><div>struct Cleanup {</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>Cleanup(std::function<void()> f) : f(f) {}</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>~Cleanup() { f(); }</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>std::function<void()> f;<br>};</div><div>void foo() {</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>Cleanup c([&]() {</div><div>   <span class="m_4264541401840967787Apple-converted-space"> </span>.. // any finally code here</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>});</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>// any try body code here</div><div>}</div><div><br></div><div>Reid</div></div><br><div class="gmail_quote"></div><div class="gmail_quote"><div dir="ltr">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">cfe-dev@lists.llvm.org</a>> wrote:<br></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"><div>Hi,</div><div><br></div><div>I am working on a block annotation which should have a try-finally semantics. For example, given a code like this:</div><div><br></div><div><b>__speculate</b><span class="m_4264541401840967787Apple-converted-space"> </span><font color="#ff0000"><b>{</b></font></div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>if (x > 2) {</div><div>   <span class="m_4264541401840967787Apple-converted-space"> </span>y = 0;</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div><font color="#ff0000"><b>}</b></font></div><div><br></div><div>clang should produce something like</div><div><br></div><div>__exec_mode =<span class="m_4264541401840967787Apple-converted-space"> </span><b><font color="#ff0000">__begin_spec</font></b>();</div><div>if (__exec_mode == SW) {</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>if (__read_barrier(&x) > 2) {</div><div>   <span class="m_4264541401840967787Apple-converted-space"> </span>__write_barrier(&y, 0);</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span><font color="#ff0000"><b>__end_spec</b></font>();</div><div>} else {</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>if (x > 2) {</div><div>   <span class="m_4264541401840967787Apple-converted-space"> </span>y = 0;</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span>}</div><div> <span class="m_4264541401840967787Apple-converted-space"> </span><b><font color="#ff0000">__end_spec</font></b>();</div><div>}</div><div><br></div><div>such that __end_spec() is guaranteed to be called on every exit of the  __speculate {}  block.</div><div><br></div><div>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><br></div><div>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><div dir="ltr" class="m_4264541401840967787m_-2154533798381837922m_-7616334774840605940m_688693615537025702m_863750444281404145gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">João Paulo L. de Carvalho<br>Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">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>cfe-dev mailing list<br><a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br></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">--<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"><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"><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">João Paulo L. de Carvalho<br>Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">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">_______________________________________________</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"><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">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"><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">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"><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">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a></div></blockquote></div><br></div></blockquote></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px">João Paulo L. de Carvalho<br>Computer Science |  IC-UNICAMP | Campinas , SP - Brazil</div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:jaopaulolc@gmail.com" target="_blank">jaopaulolc@gmail.com</a></div><div style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px"><a href="mailto:joao.carvalho@ic.unicamp.br" target="_blank">joao.carvalho@ic.unicamp.br</a></div></div></div>