Changes between Initial Version and Version 1 of Inlining


Ignore:
Timestamp:
Mar 22, 2009 6:28:04 PM (11 years ago)
Author:
claus
Comment:

start on INLINE PEEL/UNROLL spec

Legend:

Unmodified
Added
Removed
Modified
  • Inlining

    v1 v1  
     1= Inlining =
     2
     3Inlining refers to the unfolding of definitions, ie replacing uses of identifiers with the definitions bound to them. Doing this at compile time can expose potential for other optimizations. As described in the [http://www.haskell.org/ghc/docs/latest/html/users_guide/pragmas.html#inline-noinline-pragma User Guide], this is currently limited to non-recursive definitions, to avoid non-terminating recursion in the inliner.
     4
     5== Unfolding Recursions ==
     6
     7Since many definitions in non-trivial programs are recursive, leaving them out alltogether is a serious limitation, especially in view of the encoding of loops via tail recursion. In conventional languages, loop transformations such as loop unrolling are at the heart of optimizing high performance code (for a useful overview, see [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.4885 Compiler Transformations for High-Performance Computing, ACM Computing Surveys, 1994]). As a consequence, many performance-critical Haskell programs contain hand-unrolled recursions, which is error-prone and obscures declarative contents.
     8
     9There is a tension wrt to the stage in the compilation pipeline that should handle loop unrolling: if we are looking only at removing loop administration overhead and code size, then the applicability of unrolling depends on information that is only available in the backend (such as register and cache sizes); if we are looking at enabling other optimizations, then the applicability of unrolling depends on interactions with code that is located in the Core to Core simplifier (such as rewrite rules for array fusion and recycling). We assume that loop transformations should be considered at both stages, for their respective benefits and drawbacks. This page is concerned with Core-level unfolding of recursive definitions, closing the gap in GHC's inliner.
     10
     11== An Informal Specification ==
     12
     13For the purpose of unfolding/inlining definitions, look at groups of mutually recursive definitions as a whole, rather than trying to think about individual definitions. Compare the existing documentation for [http://www.haskell.org/ghc/docs/latest/html/users_guide/pragmas.html#inline-noinline-pragma `INLINE/NOINLINE`] pragmas.
     14
     15In the following, let `REC({f g ..})` denote the set of all identifiers belonging to the recursion involving `f`, `g`, .. (`f`, `g`, .. in `REC({f g ..})` or in `{-# INLINE f g .. #-}` are required to belong to the same recursion).
     16
     17`{-# NOINLINE f #-}`
     18   as now: no unfolding of `f`
     19
     20`{-# INLINE f #-}`
     21   as now: for non-recursive `f` only, unfold definition of `f` at call sites of `f` (might in future be taken as go-ahead for analysis-based recursion unfolding)
     22
     23`{-# INLINE f g .. PEEL n #-}`
     24   _new_: unfold definitions of the named identifiers at their call sites *outside* their recursion group `REC({f g ..})`. In other words, *entries into* `REC({f g ..})` via `f`, `g`, .. are unfolded.
     25   
     26   (for the special case of loops this corresponds to loop peeling)
     27
     28`{-# INLINE f g .. UNROLL m #-}`
     29   _new_: unfold definitions of the named identifiers at their call sites *inside* their recursion group `REC({f g ..})`. In other words, *cross-references inside* `REC({f g ..})` via `f`, `g`, .. are unfolded.
     30   
     31   (for the special case of loops this corresponds to loop unrolling)
     32
     33`{-# INLINE f g .. PEEL n UNROLL m #-}`
     34   combine the previous two
     35
     36   The numeric parameters are to be interpreted as if each call to `f`, `g`, .. was annotated with both `PEEL` and `UNROLL` limits for the whole recursion group `REC({f g ..})`, starting with the limits from the pragmas (write `f_n_m` for a call to `f` with `PEEL` limit `n` and `UNROLL` limit `m`), to be decreased for every `PEEL` or `UNROLL` action, as follows (`REC({f g})` = {`f` `g` `h`}, in these examples):
     37
     381.
     39{{{
     40   let {-# INLINE f g PEEL n UNROLL m #-}
     41       f .. = .. f_?_? .. g_?_? .. h_0_0 ..
     42       g .. = .. f_?_? .. g_?_? .. h_0_0 ..
     43       h .. = .. f_?_? .. g_?_? .. h_0_0 ..
     44   in ..|f_n_m|..
     45
     46   --PEEL-->
     47
     48   let {-# INLINE f g PEEL n UNROLL m #-}
     49       f .. = .. f_?_? .. g_?_? .. h_0_0 ..
     50       g .. = .. f_?_? .. g_?_? .. h_0_0 ..
     51       h .. = .. f_?_? .. g_?_? .. h_0_0 ..
     52   in ..|.. f_(n-1)_0 .. g_(n-1)_0 .. h_0_0 ..|..
     53}}}
     54
     55   Notes:
     56   - unfolding produces copies of definition bodies
     57   - the `PEEL` limit at the call site decides the `PEEL` limit for all calls to `REC({f g})` in the inlined copy; this limit decreases with each `PEEL` step
     58   - since peeling unfolds code into call sites from outside the recursion, the `UNROLL` limits of calls to `REC({f g})` are effectively `0` in the inlined copy
     59   - only calls to identifiers named in the `INLINE` pragma can be peeled (`f` and `g` here), calls to other members of the same recursion remain unaffected (`h` here), having effective limits of `0`
     60
     612.
     62{{{
     63   let {-# INLINE f g PEEL n UNROLL m #-}
     64       f .. = .. f_0_m .. g_?_? .. h_0_0 ..
     65       g .. = .. f_?_? .. g_?_? .. h_0_0 ..
     66       h .. = .. f_?_? .. g_?_? .. h_0_0 ..
     67   in ..
     68
     69   --UNROLL-->
     70
     71   let {-# INLINE f g PEEL n UNROLL m #-}
     72       f .. = .. .. f_0_(m-1) .. g_0_(m-1) .. h_0_0 .. .. g_?_? .. h_0_0 ..
     73       g .. = .. f_?_? .. g_?_? .. h_0_0 ..
     74       h .. = .. f_?_? .. g_?_? .. h_0_0 ..
     75   in ..
     76}}}
     77   Notes:
     78   - unfolding produces copies of definition bodies
     79   - the `UNROLL` limit at the call site decides the `UNROLL` limit for all calls to `REC({f g})` in the inlined copy; this limit decreases with each `UNROLL` step
     80   - peeling conceptually precedes unrolling (`PEEL` limit needs to reach `0` before unrolling commences), to avoid peeling unrolled definitions (this corresponds to an existing restriction of no inlining into definitions to be inlined)
     81   - unrolling unfolds copies of the original definitions, not the already unrolled ones, again corresponding to the existing inlining restriction (TODO: how to specify this avoidance of unrolling unrolled defs in this form of local rule spec?)
     82   - only calls to identifiers named in the `INLINE` pragma can be unrolled (`f` and `g` here), calls to other members of the same recursion remain unaffected (`h` here), having effective limits of `0`
     83
     84   Peeling and unrolling stop when the respective count annotation has reached `0`. Peeling precedes unrolling, to avoid ambiguities in the size of the peeled definitions. Note that calls into mutual recursion groups is the domain of `PEEL`, while `UNROLL` only applies to calls within mutual recursion groups.
     85
     86   `{-# INLINE f PEEL n #-}`, for `n>0`, corresponds to worker/ wrapper transforms (previously done manually) + inline wrapper, and should therefore also be taken as a hint for the compiler to try the static argument transformation for `f` (the "worker").
     87
     88   Non-supporting implementations should treat these as `INLINE` pragmas (same warning/ignore or automatic unfold behaviour).  This might be easier to accomplish if `INLINE PEEL/UNROLL` were implemented as separate pragmas, even though they are refinements of `INLINE` conceptually.
     89
     90   About the current side-conditions for `INLINE` pragmas:
     91
     92    - no functions inlined into `f`:
     93
     94      still makes sense for `PEEL`, needs to be adapted with an exception for `UNROLL`, in that we want to be able to unroll into the function being unrolled, but we want to use the original body for the unrolling, not an already unrolled one (else unrolling would be exponential rather than linear); this appears to be in line with existing work on `INLINE`
     95
     96    - no float-in/float-out/cse:
     97
     98        similar to existing `INLINE`
     99
     100    - no worker/wrapper transform in strictness analyser:
     101
     102        similar to existing `INLINE`
     103
     104    - loop breakers:
     105
     106       `PEEL/UNROLL` have their own limits, applicable to the whole recursion group, creating intrinsic loop breakers when the counters run out. Every `PEEL` or `UNROLL` action creates calls with smaller counters in the inlined copies, if the calls go into the same recursion.
     107
     108== Further References ==
     109
     110[0] GHC mailing list threads, with examples and discussion
     111  - [http://www.haskell.org/pipermail/glasgow-haskell-users/2009-February/016695.html optimization and rewrite rules questions]
     112  - [http://www.haskell.org/pipermail/glasgow-haskell-users/2009-February/016729.html Loop unrolling + fusion ?]
     113    [http://www.haskell.org/pipermail/glasgow-haskell-users/2009-March/016732.html continued in March]
     114
     115[1] [http://citeseer.ist.psu.edu/old/620489.html An Aggressive Approach to Loop Unrolling], 1995
     116
     117[2] [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.41.4885 Compiler Transformations for High-Performance Computing], ACM Computing Surveys, 1994
     118
     119[3] [http://en.wikipedia.org/wiki/Loop_transformation]
     120
     121[4] [http://www.intel.com/software/products/compilers/flin/docs/main_for/mergedprojects/optaps_for/common/optaps_hlo_unrl.htm loop unrolling vs hardware]
     122
     123[5] [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.5.9813 Unrolling and simplifying expressions with Template Haskell], Ian Lynagh, 2003