47

The documentation states:

An {-# INLINABLE f #-} pragma on a function f has the following behaviour:

  • While INLINE says "please inline me", the INLINABLE says "feel free to inline me; use your discretion". In other words the choice is left to GHC, which uses the same rules as for pragma-free functions. Unlike INLINE, that decision is made at the call site, and will therefore be affected by the inlining threshold, optimisation level etc.

  • Like INLINE, the INLINABLE pragma retains a copy of the original RHS for inlining purposes, and persists it in the interface file, regardless of the size of the RHS.

  • One way to use INLINABLE is in conjunction with the special function inline (Section 7.18, “Special built-in functions”). The call inline f tries very hard to inline f. To make sure that f can be inlined, it is a good idea to mark the definition of f as INLINABLE, so that GHC guarantees to expose an unfolding regardless of how big it is. Moreover, by annotating f as INLINABLE, you ensure that f's original RHS is inlined, rather than whatever random optimised version of f GHC's optimiser has produced.

  • The INLINABLE pragma also works with SPECIALISE: if you mark function f as INLINABLE, then you can subsequently SPECIALISE in another module (see Section 7.16.8, “SPECIALIZE pragma”).

  • Unlike INLINE, it is OK to use an INLINABLE pragma on a recursive function. The principal reason do to so to allow later use of SPECIALISE

What's the disadvantage of it?

Does it make interface files much, much bigger? Does it make compilation much slower?

Is there any reason I shouldn't put an INLINABLE pragma on every exported function I write? Is there any reason GHC doesn't put an INLINABLE pragma on every exported function I write?

1 Answer 1

51

There are three differences between using INLINABLE and not using a pragma at all:

  • Without INLINABLE, the definition that goes in the interface file is the code after optimisation, whereas with INLINABLE, it is the code you wrote (more or less). In particular, without INLINABLE, GHC might inline other functions into the function's definition.

  • Without INLINABLE, GHC will omit the definition from the interface file if it is too big. If some other function got inlined into the right-hand-side, this could easily push it over the limit.

  • INLINABLE also turns on some clever machinery that automatically specialises overloaded functions where they are used, and shares the specialised versions with other modules that transitively import the module in which the specialised version was created.

5
  • 11
    I'm trying to understand the practical ramifications. Does the first point mean that where the function isn't inlined, it'll use the unoptimized code and be slower? Or will it be slower if it does get inlined but RULES don't fire? Or will it sometimes be faster, sometimes slower on a case-by-case basis? Regarding the second point, is there any drawback to including it in the interface file other than disk space? Can they get so large that it's a problem (disks are big)? And re: the third point, is there any reason it's not an unvarnished good? Can it cause problematic amounts of code bloat?
    – glaebhoerl
    Mar 14, 2012 at 21:12
  • 6
    I deliberately avoided talking about the practical ramifications, because for the most part I'm not sure :-) I convinced Simon PJ to add INLINABLE because I wanted a variant of INLINE that did not cause so much code bloat, by using GHC's already quite sophisticated heuristics about when to inline. To answer your first question: no, there will also be an optimised version of the function compiled into the original module (as with INLINE), and this will be used for calls that were not inlined. Including lots of function definitions in the interface can slow things down. Mar 15, 2012 at 8:39
  • Hmm, okay. Would be nice to have some guidelines about when to use it and when not to use it, but I guess I'll have to experiment. :)
    – glaebhoerl
    Mar 15, 2012 at 11:59
  • 5
    I know this question is old, but I just wanted to ask one question to make sure I understood your answer: Does this mean that the only disadvantage of using INLINABLE, versus not using it, is slowing down compile times or increasing the size of the interface files? Oct 5, 2012 at 19:13
  • 21
    To make one more attempt: if we lived in a world where every function automatically got an INLINABLE pragma, what would be bad about it?
    – glaebhoerl
    Oct 14, 2012 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.