[llvm-dev] allocsize: change from 3.9 to 4.0

George Burgess IV via llvm-dev llvm-dev at lists.llvm.org
Wed May 3 11:04:21 PDT 2017


Hello!

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: https://www.mail-arch
ive.com/cfe-commits at lists.llvm.org/msg45128.html . In particular, this
visibly broke `realloc`, which has the side effect of potentially freeing
memory.

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.

> (if the call is to "malloc", it is removed because LLVM recognizes the
function name)

FWIW, the prior behavior was that we'd pretend any `allocsize` function was
present in http://llvm.org/docs/doxygen/html/MemoryBuiltins_8cpp_sou
rce.html#l00054 and was "MallocLike". Looks like our friends in the Rust
world keep https://github.com/rust-lang/llvm/commit/cca16c06fbec67
2b7d1dd9753491783a006bdfe5 for just this purpose. :)

Sorry for any confusion,
George

On Sun, Apr 30, 2017 at 4:56 AM, Johan Engelen via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Hi all,
>   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.
>
> For example:
> ```
> declare i8* @my_malloc(i32) allocsize(0)
>
> define void @test_malloc() {
>   %1 = call i8* @my_malloc(i32 100)
>   ret void
> }
> ```
> 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.
>
> I can't find out why this was changed.
> (if the call is to "malloc", it is removed because LLVM recognizes the
> function name)
>
> Thanks for the explanation,
> Kind regards,
>   Johan
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170503/2a9fee62/attachment.html>


More information about the llvm-dev mailing list