<div dir="ltr">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><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:0 0 0 .8ex;border-left:1px #ccc solid;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>  Cleanup(std::function<void()> f) : f(f) {}</div><div>  ~Cleanup() { f(); }</div><div>  std::function<void()> f;<br>};</div><div>void foo() {</div><div>  Cleanup c([&]() {</div><div>    .. // any finally code here</div><div>  });</div><div>  // 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:0 0 0 .8ex;border-left:1px #ccc solid;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> <font color="#ff0000"><b>{</b></font></div><div>  if (x > 2) {</div><div>    y = 0;</div><div>  }</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 = <b><font color="#ff0000">__begin_spec</font></b>();</div><div>if (__exec_mode == SW) {</div><div>  if (__read_barrier(&x) > 2) {</div><div>    __write_barrier(&y, 0);</div><div>  }</div><div>  <font color="#ff0000"><b>__end_spec</b></font>();</div><div>} else {</div><div>  if (x > 2) {</div><div>    y = 0;</div><div>  }</div><div>  <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>-- <br><div dir="ltr" class="m_-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:0 0 0 .8ex;border-left:1px #ccc solid;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>-- <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>