<div dir="ltr">Hello!<div><br></div><div>The short story is that this optimization we were performing was invalid. allocsize is speced to only tell us how many bytes exist at a returned pointer, but we were assuming it also meant the function with allocsize was malloc-like (e.g. had no other side effects, ...). I realized this mistake when I added alloc_size -- which lowers to LLVM's allocsize -- to clang, and broke things in the land of FreeBSD: <a href="https://www.mail-archive.com/cfe-commits@lists.llvm.org/msg45128.html" target="_blank">https://www.mail-arch<wbr>ive.com/cfe-commits@lists.llvm<wbr>.org/msg45128.html</a> . In particular, this visibly broke `realloc`, which has the side effect of potentially freeing memory.</div><div><br></div><div>The commit that fixed this behavior is r290397. There's a FIXME in there that says we might be able to tag allocsize functions as allocation functions, but I'm unsure if "allocation function" will let us be as aggressive as you'd like.</div><div><div><br></div><div>> (<span style="font-size:12.8px">if the call is to "malloc", it is removed because LLVM recognizes the function name)</span></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">FWIW, the prior behavior was that we'd pretend any `allocsize` function was present in </span><span style="font-size:12.8px"><a href="http://llvm.org/docs/doxygen/html/MemoryBuiltins_8cpp_source.html#l00054" target="_blank">http://llvm.org/docs/doxyge<wbr>n/html/MemoryBuiltins_8cpp_sou<wbr>rce.html#l00054</a> and was "MallocLike". Looks like our friends in the Rust world keep <a href="https://github.com/rust-lang/llvm/commit/cca16c06fbec672b7d1dd9753491783a006bdfe5" target="_blank">https://github.com/rust-l<wbr>ang/llvm/commit/cca16c06fbec67<wbr>2b7d1dd9753491783a006bdfe5</a> for just this purpose. :)</span></div></div><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Sorry for any confusion,</span></div><div><span style="font-size:12.8px">George</span></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Apr 30, 2017 at 4:56 AM, Johan Engelen via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi all,<div> I added support for the allocsize function attribute to our compiler (LDC), thinking that that would enable removal of function calls when the allocated memory is not used.</div><div><br></div><div>For example:</div><div><div>```</div><div>declare i8* @my_malloc(i32) allocsize(0)</div><div><br></div><div>define void @test_malloc() {<br></div><div> %1 = call i8* @my_malloc(i32 100)</div><div> ret void<br></div><div>}</div></div><div>```</div><div>I thought the my_alloc call in test_malloc would be removed, but `opt -O3` doesn't do that (LLVM 4.0 and trunk). However, LLVM3.9's `opt` _does_ remove the call.</div><div><br></div><div>I can't find out why this was changed.</div><div>(if the call is to "malloc", it is removed because LLVM recognizes the function name)</div><div><br></div><div>Thanks for the explanation,</div><div>Kind regards,</div><div> Johan</div><div><br></div></div>
<br>______________________________<wbr>_________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div></div>