[cfe-dev] [RFC] Per-callsite inline intrinsics

John McCall via cfe-dev cfe-dev at lists.llvm.org
Tue Sep 4 18:12:51 PDT 2018


> On Sep 4, 2018, at 2:32 PM, Jakub (Kuba) Kuderski via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> Hi folks,
> 
> TL;DR: I propose to add 3 new C/C++ intrinsics for controlling inlining at callsite:
> *  __builtin_no_inline(Foo()) -- prevents the call to Foo() from being inlined at that particular callsite.
> *  __builtin_always_inline(Foo()) -- inlines this call to Foo(), if possible.
> *  __builtin_flatten_inline(Foo()) -- inlines this call to Foo() and (transitively) everything called within Foo’s body.
> These intrinsics apply to the outermost call-like expression and it will be possible to use them with: function calls, member function calls, operator calls, constructor calls, indirect calls (with function pointers, member function pointers, virtual calls).

Would it make sense to pre-emptively generalize this as a call-annotation builtin that takes the annotations as a second, string-literal argument?  Or maybe even has some special parsing rules so that we can e.g. parse attributes in the later operands?  Call-related features tend to end up with a million different variations; you already have three different builtins for three different inlining behaviors, and once this infrastructure is up and working, I expect we'll want to add more for all sorts of other call-related mechanisms.  To me, that means it makes sense to plan this as a more heavyweight language feature, including things like preparing for call sites that the user wants to apply multiple annotations to.

For `__builtin_flatten_inline`, there are sometimes a lot of implicit calls done on behalf of emitting the primary call: `operator->`, default arguments, copy constructors for arguments, destructors for arguments, that kind of thing.  Are these covered?  Not covered because they're emitted with the caller?  Is it okay for the semantics to differ by target for things like argument destructors?

> One thing I’m not sure about is what to do when the expression inside inline intrinsic doesn’t happen to be any kind of call. It doesn’t make much sense to be able to write something like:
> __builtin_always_inline(1 + 3), but what may happen in generic context (e.g.,
> __builtin_always_inline(t + u)), is that it’s not known if expressions will end up operating on primitive types or user-defined ones that actually make function calls. In my opinion, it will make life easier if inline intrinsics over non-call-like expressions will be treated as no-ops, in any context, as the compiler can already reason about them and won’t perform any function calls. One option is to silently not inline when the compiler resolves the call to an operation, which would be consistent with the behavior of silently not inlining calls it cannot resolve.  Alternatively we may emit warnings, which would make maintaining code with these intrinsics easier.
> I’d really like to get feedback on this issue.

This should definitely be a parse-time error.  In a template, you can recognize and decline to diagnose dependent expressions that might be instantiated to a call, which I believe just means the allowing BinaryOperator and UnaryOperator when one of the operands has a dependent type.

John.

> 
> Implementation:
> I have already partially implemented the first two intrinsics (__builtin_no_inline and __builtin_always_inline) here: https://reviews.llvm.org/D51200 <https://reviews.llvm.org/D51200>. Calls wrapped with the inline intrinsics are annotated with appropriate attributes during code generation. LLVM seems to already take care of callsites attributed with alwaysinline and noinline. I think it should also be possible to implement some appropriate attribute for flattening, as there’s already gnu::flatten attribute for function declarations.
> 
> Let me know what you think,
> Kuba
> 
> -- 
> Jakub Kuderski
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180904/5fe498f1/attachment.html>


More information about the cfe-dev mailing list