<div dir="ltr"><div><div><div>Hi Hal,<br><br>>malloc hooks are an interesting point, but those are not standard, not commonly used<br></div>I very much agree with this.<br><br>>readonly is a more-interesting question, because, in practice, this will currently work. It works, however, for the wrong reason<br></div>Rather than read-only, could we mark malloc/free etc with onlyAccessesArgMem()? GlobalsAA would just need a simple check to ignore such functions (along with read-only which it already is checking for) during propagation along the call graph. As a reference, I'm attaching a prototype patch (This patch is on release37 unfortunately, but is applicable verbatim to the latest svn version). <br><br></div>Thanks,<br><div><div><br></div></div></div><div class="gmail_extra"><br clear="all"><div><div class="gmail_signature"><div dir="ltr"> - Vaivaswatha<br></div></div></div>
<br><div class="gmail_quote">On Thu, Dec 3, 2015 at 5:53 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">----- Original Message -----<br>
> From: "James Molloy via llvm-dev" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
> To: "Vaivaswatha Nagaraj" <<a href="mailto:vn@compilertree.com">vn@compilertree.com</a>><br>
> Cc: "LLVM Dev" <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
> Sent: Thursday, December 3, 2015 4:41:46 AM<br>
> Subject: Re: [llvm-dev] Function attributes for LibFunc and its impact on GlobalsAA<br>
><br>
> Hi,<br>
><br>
> I think that might be difficult to detect. If you wanted to force<br>
> this behaviour in your own toolchain, you could just use "-mllvm<br>
> -force-attribute=malloc:readnone" on the clang command line?<br>
<br>
</span>This is unlikely to be desirable. A readnone function is one whose output is a function only of its inputs, and if you have this:<br>
<br>
int *x = malloc(4);<br>
*x = 2;<br>
int *y = malloc(4);<br>
*y = 4;<br>
<br>
you certainly don't want EarlyCSE to replace the second call to malloc with the result of the first (which it will happily do if you mark malloc as readnone).<br>
<br>
readonly is a more-interesting question, because, in practice, this will currently work. It works, however, for the wrong reason (as I recall, we currently don't CSE readonly calls because we need to assume that they might have infinite loops, which is a problem we need to otherwise fix). Thus, marking it readonly is probably not a good long-term plan.<br>
<br>
Given that malloc is an important special case, however, giving it special handling is potentially reasonable (we have isMallocLikeFn and isOperatorNewLikeFn in MemoryBuiltins.h). One might argue that tagging malloc() as readonly might break an LTO build, but doing so already potentially has problems because of our aliasing assumptions. malloc hooks are an interesting point, but those are not standard, not commonly used, can already cause violations of our aliasing assumptions, and the problem that hooking libc functions that we assume are readonly in a way that changes state visible to the caller might break things is not unique to malloc. Users always have the option of turning off these kinds of assumptions by compiling with -fno-builtin-malloc.<br>
<br>
-Hal<br>
<div class="HOEnZb"><div class="h5"><br>
><br>
> James<br>
><br>
><br>
> On Thu, 3 Dec 2015 at 10:21 Vaivaswatha Nagaraj < <a href="mailto:vn@compilertree.com">vn@compilertree.com</a><br>
> > wrote:<br>
><br>
> Hi James,<br>
><br>
> Thank you for the response. I understand the concern about<br>
> malloc/free hooks. Could we detect that a program has setup malloc<br>
> hooks (assuming we're in a whole program compilation) and make<br>
> assumptions (such as onlyAccessesArgMem()) when the program hasn't<br>
> setup malloc hooks? Using a command line flag could be one option<br>
> too.<br>
><br>
> I'm currently working on a program where having these attributes<br>
> could help GlobalsAA give significantly more precise results.<br>
> Considering that this info is propagated to the caller, its caller<br>
> and so on, this may have a wider impact than the program I'm<br>
> currently looking at.<br>
><br>
> Thanks,<br>
><br>
> - Vaivaswatha<br>
><br>
> On Thu, Dec 3, 2015 at 2:57 PM, James Molloy <<br>
> <a href="mailto:james@jamesmolloy.co.uk">james@jamesmolloy.co.uk</a> > wrote:<br>
><br>
><br>
><br>
> Hi Vaivaswatha,<br>
><br>
><br>
> I think not adding readnone/readonly to malloc/realloc is correct.<br>
> malloc/free hooks can be added to most implementations (for leak<br>
> checking and so on), so calling malloc could in fact call any other<br>
> arbitrary code that could write to memory.<br>
><br>
><br>
> Cheers,<br>
><br>
><br>
> James<br>
><br>
><br>
><br>
><br>
> On Wed, 2 Dec 2015 at 14:07 Vaivaswatha Nagaraj via llvm-dev <<br>
> <a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a> > wrote:<br>
><br>
><br>
><br>
><br>
><br>
><br>
> Hi,<br>
><br>
> GlobalsAA, during propagation of mod-ref behavior in the call graph,<br>
> looks at library functions (in GlobalsAAResult::AnalyzeCallGraph:<br>
> F->isDeclaration() check), for attributes, and if the function does<br>
> not have the onlyReadsMemory attribute set, forgets it.<br>
><br>
><br>
><br>
> I noticed that library functions such as malloc/realloc do not have<br>
> the attributes doesNotAccessMemory or onlyReadsMemory respectively<br>
> set (FunctionAttrs.cpp). This leads to a loss of GlobalsAA<br>
> information in the caller (and its caller and so on). Aren't these<br>
> attributes stricter than necessary currently? I do not see why the<br>
> presence of malloc/realloc in a function needs to invalidate all<br>
> mod-ref info gathered for that function so far.<br>
><br>
><br>
><br>
> Please let me know if the question is not clear. I'll try to extract<br>
> out a simple test case from the program I'm looking at and post it,<br>
> so as to have a concrete example.<br>
><br>
><br>
> Thanks,<br>
><br>
><br>
><br>
><br>
><br>
><br>
> - Vaivaswatha<br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org">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/mailman/listinfo/llvm-dev</a><br>
><br>
><br>
> _______________________________________________<br>
> LLVM Developers mailing list<br>
> <a href="mailto:llvm-dev@lists.llvm.org">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/mailman/listinfo/llvm-dev</a><br>
><br>
<br>
</div></div><span class="HOEnZb"><font color="#888888">--<br>
Hal Finkel<br>
Assistant Computational Scientist<br>
Leadership Computing Facility<br>
Argonne National Laboratory<br>
</font></span></blockquote></div><br></div>