<div dir="ltr">Prototyping the idea of "isDefinitionExact" returning false for optnone (whether or not we split it out into noipo or not) I've tripped over something it seems I created 5 years ago:<br><br>I added some IPC support for optnone to GlobalsModRef: <a href="https://github.com/llvm/llvm-project/commit/c662b501508200076e581beb9345a7631173a1d8#diff-55664e96a7ce3533b46f12c6906acecb2bd9a599e2b79c97506af4b1b4873fa1">https://github.com/llvm/llvm-project/commit/c662b501508200076e581beb9345a7631173a1d8#diff-55664e96a7ce3533b46f12c6906acecb2bd9a599e2b79c97506af4b1b4873fa1</a> - so it wouldn't conclude properties of an optnone function.<br><br>But I then made a follow-up commit (without a lot of context as to why, unfortunately :/ ) that allowed GlobasModRef to use existing attributes on an optnone function: <a href="https://github.com/llvm/llvm-project/commit/7a9b788830da0a426fb0ff0a4cec6d592bb026e9#diff-55664e96a7ce3533b46f12c6906acecb2bd9a599e2b79c97506af4b1b4873fa1">https://github.com/llvm/llvm-project/commit/7a9b788830da0a426fb0ff0a4cec6d592bb026e9#diff-55664e96a7ce3533b46f12c6906acecb2bd9a599e2b79c97506af4b1b4873fa1</a><br><br>But it seems making the function definition inexact, breaks the unit testing added in the latter commit. I suppose then it's an open question whether existing attributes on an inexact definition should be used at all? (I don't know what motivated me to support them for optnone)<br><br>Oh, and here's a change from Chandler around the same time similarly blocking some ipo for optnone: <a href="https://github.com/llvm/llvm-project/commit/0fb998110abcf3d67495d12f854a1576b182d811#diff-cc618a9485181a9246c4e0367dc9f1a19d3cb6811d1e488713f53a753d3da60c">https://github.com/llvm/llvm-project/commit/0fb998110abcf3d67495d12f854a1576b182d811#diff-cc618a9485181a9246c4e0367dc9f1a19d3cb6811d1e488713f53a753d3da60c</a> - in this case preventing FunctionAttrs from deriving the attributes for an optnone function. That functionality looks like it can be subsumed by the inexact approach - applying inexact to optnone and removing the change in Chandler's patch still passes the tests. (hmm, tested - not quite, but more work to do there)</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Apr 18, 2021 at 10:06 AM David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr">On Sun, Apr 18, 2021 at 9:43 AM Roman Lebedev <<a href="mailto:lebedev.ri@gmail.com" target="_blank">lebedev.ri@gmail.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">There's 'noipa' attribute in GCC, currently it is not supported by clang.<br>
Theoretically, how would one implement it?<br></blockquote><div><br>If we wanted to do this really robustly, I guess we might have to introduce some sort of "here's the usual way to check if this is a definition/get the body of the function" (which for noipa it says "there is no body/don't look here") and "no, really, I need the definition" (for actual code generation).<br><br>Though I'm not advocating for that - I'm OK with a more ad-hoc/best-effort implementation targeting the -O0/debugging assistant __attribute__((optnone)) kind of use case - happy to fix cases as they come up to improve the user experience for these situations.<br><br>Maybe we could get away with generalizing this by having an optnone (or noipa) function appear "interposable" even though it doesn't have a real interposable linkage? That should hinder/disable any IPA.<br><br>Hmm, looks like GlobalValue::isDefinitionExact would be best to return false in this case (whatever we end up naming it) /maybe/ mayBeDerefined should return false too.<br><br>Yeah, I guess if we can implement such a robust generalization, then it'd probably be OK/easy enough to implement both noipa and optnone implies noipa the same as it implies noinline (well, I guess noipa would subsume the noinline implication - if the function isn't exact, the inliner won't inline it so there wouldn't be any need for the explicit noinline)<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
With your proposal, clang `noipa` attribute could be lowered<br>
to `optnone` on the whole function, To me that seems like<br>
too much of a hammer, should that be the path forward.<br></blockquote><div><br>I agree that lowering noipa to optnone would be a very aggressive form of noipa - likely if we want to support noipa it would be to support it separately and maybe either lower -O0 (& maybe __attribute__((optnone))) to both optnone+noipa+noinline (since optnone already implies noinline) or make optnone imply ipa/be a superset of it implicitly (if we do have noipa it's probably best to have "optnone requires noipa" the same way "optnone requires noinline" rather than an implicit superset sort of thing).<br><br>I think that'd certainly be appropriate for -O0, and I'd argue it'd be appropriate for __attribute__((optnone)) because I think it'd be what people expect/is consistent with the motivation for the attribute (for debuggability - so you wouldn't want a caller to not fill in parameters/pass in garbage because it knows the implementation doesn't matter, or not use the result because it knows what the result should be).<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Would it not be best to not conflate the two,<br>
and just introduce the `noipa` attribute?<br></blockquote><div><br>I think we'd still want to conflate them for user-facing functionality, even if they were separable at the IR level.<br><br>- Dave<br> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Roman<br>
<br>
On Sun, Apr 18, 2021 at 7:37 PM David Blaikie <<a href="mailto:dblaikie@gmail.com" target="_blank">dblaikie@gmail.com</a>> wrote:<br>
><br>
> While trying to reproduce some debug info thing (I don't have the exact example at the moment - but I think it was more aggressive than the example I have now, but something like this:<br>
><br>
> __attribute__((optnone)) int f1() {<br>
>   return 3;<br>
> }<br>
> int main() {<br>
>   return f1();<br>
> }<br>
><br>
><br>
> (actually I think in my case I had a variable to hold the return value from f1, with the intent that this variable's location couldn't use a constant - a load from a volatile variable would probably have provided similar functionality in this case)<br>
><br>
> LLVM (& specifically Sparse Conditional Constant Propagation, llvm/lib/Transforms/Scalar/SCCP.cpp) optimizes this code noting that f1 always returns 3, so rather than using the return value from the call to f1, it ends up hardcoding the return value:<br>
><br>
> define dso_local i32 @main() local_unnamed_addr #1 {<br>
><br>
> entry:<br>
><br>
>   %call = tail call i32 @_Z2f1v()<br>
><br>
>   ret i32 3<br>
><br>
> }<br>
><br>
><br>
> I consider this a bug - in that optnone is used to implement -O0 for LTO, so it seemed to me that the correct behavior is for an optnone function to behave as though it were compiled in another object file outside the purview of optimizations - interprocedural or intraprocedural.<br>
><br>
> So I sent <a href="https://reviews.llvm.org/D100353" rel="noreferrer" target="_blank">https://reviews.llvm.org/D100353</a> to fix that.<br>
><br>
> Florian pointed out that this wasn't quite specified in the LangRef, which says this about optnone:<br>
><br>
> This function attribute indicates that most optimization passes will skip this function, with the exception of interprocedural optimization passes. Code generation defaults to the “fast” instruction selector. This attribute cannot be used together with the alwaysinline attribute; this attribute is also incompatible with the minsize attribute and the optsize attribute.<br>
><br>
> This attribute requires the noinline attribute to be specified on the function as well, so the function is never inlined into any caller. Only functions with the alwaysinline attribute are valid candidates for inlining into the body of this function.<br>
><br>
><br>
> So the spec of optnone is unclear (or arguably explicitly disallows) whether interprocedural optimizations should treat optnone functions in any particular way.<br>
><br>
> So I was going to update the wording to rephrase this to say "Interprocedural optimizations should treat this function as though it were defined in an isolated module/object." (perhaps "interprocedural optimizations should treat optnone functions as opaque" or "as though they were only declarations")<br>
><br>
> The choice of this direction was based on my (possibly incorrect or debatable) understanding of optnone, that it was equivalent to the function being in a separate/non-lto object. (this seems consistent with the way optnone is used to implement -O0 under lto - you could imagine a user debugging a binary, using -O0 for the code they're interested in debugging, and potentially using an interactive debugger to change some state in the function causing it to return a different value - which would get quite confusing if the return value was effectively hardcoded into the caller)<br>
><br>
> What're folks thoughts on this?<br>
><br>
> - Dave<br>
</blockquote></div></div>
</blockquote></div>