<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">On 10 Jun 2020, at 11:31, Arthur O'Dwyer wrote:</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">On Wed, Jun 10, 2020 at 11:14 AM Johannes Doerfert via cfe-dev <<br>
cfe-dev@lists.llvm.org> wrote:<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">On 6/8/20 8:13 PM, Richard Smith via cfe-dev wrote</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">You wouldn’t be the first person to be surprised by the result of this</p>
</blockquote></blockquote><p dir="auto">sort</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">of analysis, but I’m afraid it’s what we’re working with.<br>
<br>
Unfortunately, there’s really no way to eliminate this one without</p>
</blockquote></blockquote><p dir="auto">either</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">interprocedural information or language changes. trivial_abi eliminates<br>
the other one because it changes the convention for passing by value,</p>
</blockquote></blockquote><p dir="auto">but</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">to pass an “immutably borrowed” value in C++ we have to pass by</p>
</blockquote></blockquote><p dir="auto">reference,</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">which allows the reference to be escaped and accessed (and even</p>
</blockquote></blockquote><p dir="auto">mutated, if the</p>
<blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">original object wasn’t declared const) as long as those accesses happen<br>
before destruction.<br>
</p>
</blockquote><p dir="auto">Perhaps we should expose LLVM's nocapture attribute to the source level?</p>
</blockquote><p dir="auto">Yes please ;)<br>
</p>
</blockquote><p dir="auto">As someone (John?) has said in this thread, the nocapture attribute is<br>
already exposed to C/C++ via __attribute__((noescape)).<br>
<br>
Here is an example of __attribute__((noescape)) doing its thing:<br>
<a href="https://godbolt.org/z/rJbEmZ" style="color:#777">https://godbolt.org/z/rJbEmZ</a><br>
In function `one`, the call to `noargs` is assumed to trash the value of<br>
`i`.<br>
In function `two`, the call to `noargs` is *not* assumed to trash the value<br>
of `i`.<br>
<br>
However, notice that the store corresponding to the assignment `i = 42;`<br>
cannot be removed in either case! We initialize `i` to 42, then pass it by<br>
const reference to `dont_escape`, and yet *cannot assume that its value is<br>
unchanged by that call*. This is required by C++'s loose rules around<br>
`const`. But it doesn't match up with human intuition very well.<br>
<br>
I mentioned to Zoe offline that I'd like to see Clang add an opt-in<br>
non-conforming optimization mode, on the lines of `-ffast-math`, which<br>
would *assume* things like "things passed by const reference don't change"<br>
and "things passed by reference don't escape," and then measure to what<br>
degree codegen is improved on real codebases by that non-conforming<br>
optimization mode.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">This is quite an interesting idea. Having a mode that assumes that<br>
<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">const</code> references don’t get cast back might be quite interesting. I<br>
suspect that enforcing stricter escape rules would break some specific<br>
idioms and types.</p>
<p dir="auto">John.</p>
</div>
</div>
</body>
</html>