Changes between Version 39 and Version 40 of Commentary/Compiler/HscMain


Ignore:
Timestamp:
Jan 23, 2011 12:30:28 AM (3 years ago)
Author:
ezyang
Comment:

Add some useful flags for looking at the stages

Legend:

Unmodified
Added
Removed
Modified
  • Commentary/Compiler/HscMain

    v39 v40  
    88There is a picture that goes with this description, which appears at the bottom of this page, but you'll probably find it easier to open [wiki:Commentary/Compiler/HscPipe this link] in another window, so you can see it at the same time as reading the text. 
    99 
    10 Look at the picture first.  The yellow boxes are compiler passes, while the blue stuff on the left gives the data type that moves from one phase to the next.  The entire pipeline for a single module is run by a module called !HscMain (in [[GhcFile(compiler/main/HscMain.lhs)]]).  Here are the steps it goes through: 
     10Look at the picture first.  The yellow boxes are compiler passes, while the blue stuff on the left gives the data type that moves from one phase to the next.  The entire pipeline for a single module is run by a module called !HscMain (in [[GhcFile(compiler/main/HscMain.lhs)]]).  Each data type's representation can be dumped for further inspection using a `-ddump-*` flag.  Here are the steps it goes through: 
    1111 
    1212 * The '''Front End''' processes the program in the [wiki:Commentary/Compiler/HsSynType big HsSyn type]. {{{HsSyn}}} is parameterised over the types of the term variables it contains.  The first three passes (the front end) of the compiler work like this:[[BR]][[BR]] 
    13    * The '''Parser''' produces {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/RdrNameType RdrName]'''.  To a first approximation, a {{{RdrName}}} is just a string.[[BR]][[BR]] 
    14    * The '''[wiki:Commentary/Compiler/Renamer Renamer]''' transforms this to {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/NameType Name]'''.  To a first appoximation, a {{{Name}}} is a string plus a {{{Unique}}} (number) that uniquely identifies it.  In particular, the renamer associates each identifier with its binding instance and ensures that all occurrences which associate to the same binding instance share a single {{{Unique}}}.[[BR]][[BR]] 
    15    * The '''Typechecker''' transforms this further, to {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/EntityTypes Id]'''.  To a first approximation, an {{{Id}}} is a {{{Name}}} plus a type. In addition, the type-checker converts class declarations to {{{Class}}}es, and type declarations to {{{TyCon}}}s and {{{DataCon}}}s.  And of course, the type-checker deals in {{{Type}}}s and {{{TyVar}}}s. The [wiki:Commentary/Compiler/EntityTypes data types for these entities] ({{{Type}}}, {{{TyCon}}}, {{{Class}}}, {{{Id}}}, {{{TyVar}}}) are pervasive throughout the rest of the compiler. 
     13   * The '''Parser''' produces {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/RdrNameType RdrName]'''.  To a first approximation, a {{{RdrName}}} is just a string. (`-ddump-parsed`) [[BR]][[BR]] 
     14   * The '''[wiki:Commentary/Compiler/Renamer Renamer]''' transforms this to {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/NameType Name]'''.  To a first appoximation, a {{{Name}}} is a string plus a {{{Unique}}} (number) that uniquely identifies it.  In particular, the renamer associates each identifier with its binding instance and ensures that all occurrences which associate to the same binding instance share a single {{{Unique}}}. (`-ddump-rn`)  [[BR]][[BR]] 
     15   * The '''Typechecker''' transforms this further, to {{{HsSyn}}} parameterised by '''[wiki:Commentary/Compiler/EntityTypes Id]'''.  To a first approximation, an {{{Id}}} is a {{{Name}}} plus a type. In addition, the type-checker converts class declarations to {{{Class}}}es, and type declarations to {{{TyCon}}}s and {{{DataCon}}}s.  And of course, the type-checker deals in {{{Type}}}s and {{{TyVar}}}s. The [wiki:Commentary/Compiler/EntityTypes data types for these entities] ({{{Type}}}, {{{TyCon}}}, {{{Class}}}, {{{Id}}}, {{{TyVar}}}) are pervasive throughout the rest of the compiler. (`-ddump-tc`) 
    1616 
    1717 These three passes can all discover programmer errors, which are sorted and reported to the user. 
    1818  
    19  * The '''Desugarer''' ([[GhcFile(compiler/deSugar/Desugar)]]) converts from the massive {{{HsSyn}}} type to [wiki:Commentary/Compiler/CoreSynType GHC's intermediate language, CoreSyn].  This Core-language data type is unusually tiny: just eight constructors.[[BR]][[BR]] 
     19 * The '''Desugarer''' ([[GhcFile(compiler/deSugar/Desugar)]]) converts from the massive {{{HsSyn}}} type to [wiki:Commentary/Compiler/CoreSynType GHC's intermediate language, CoreSyn].  This Core-language data type is unusually tiny: just eight constructors.) (`-ddump-ds`)[[BR]][[BR]] 
    2020   Generally speaking, the desugarer produces few user errors or warnings. But it does produce ''some''.  In particular, (a) pattern-match overlap warnings are produced here; and (b) when desugaring Template Haskell code quotations, the desugarer may find that `THSyntax` is not expressive enough.  In that case, we must produce an error ([[GhcFile(compiler/deSugar/DsMeta)]]).[[BR]][[BR]] 
    2121   This late desugaring is somewhat unusual.  It is much more common to desugar the program before typechecking, or renaming, because that presents the renamer and typechecker with a much smaller language to deal with.  However, GHC's organisation means that 
     
    2525 
    2626 * The '''!SimplCore''' pass ([[GhcFile(simplCore/SimplCore.lhs)]]) is a bunch of Core-to-Core passes that optimise the program; see [http://research.microsoft.com/%7Esimonpj/Papers/comp-by-trans-scp.ps.gz A transformation-based optimiser for Haskell (SCP'98)] for a more-or-less accurate overview.  The main passes are:[[BR]][[BR]] 
    27     * The '''Simplifier''', which applies lots of small, local optimisations to the program.  The simplifier is big and complicated, because it implements a ''lot'' of transformations; and tries to make them cascade nicely.  The transformation-based optimiser paper gives lots of details, but two other papers are particularly relevant: [http://research.microsoft.com/%7Esimonpj/Papers/inlining/index.htm Secrets of the Glasgow Haskell Compiler inliner (JFP'02)] and [http://research.microsoft.com/%7Esimonpj/Papers/rules.htm Playing by the rules: rewriting as a practical optimisation technique in GHC (Haskell workshop 2001)].[[BR]][[BR]] 
     27    * The '''Simplifier''', which applies lots of small, local optimisations to the program.  The simplifier is big and complicated, because it implements a ''lot'' of transformations; and tries to make them cascade nicely.  The transformation-based optimiser paper gives lots of details, but two other papers are particularly relevant: [http://research.microsoft.com/%7Esimonpj/Papers/inlining/index.htm Secrets of the Glasgow Haskell Compiler inliner (JFP'02)] and [http://research.microsoft.com/%7Esimonpj/Papers/rules.htm Playing by the rules: rewriting as a practical optimisation technique in GHC (Haskell workshop 2001)].  (`-ddump-simpl`)[[BR]][[BR]] 
    2828    * The '''float-out''' and '''float-in''' transformations, which move let-bindings outwards and inwards respectively.  See [http://research.microsoft.com/%7Esimonpj/papers/float.ps.gz Let-floating: moving bindings to give faster programs (ICFP '96)].[[BR]][[BR]] 
    29     * The '''strictness analyser'''.  This actually comprises two passes: the '''analyser''' itself and the '''worker/wrapper''' transformation that uses the results of the analysis to transform the program. (Further described in [wiki:Commentary/Compiler/StrictnessAnalysis Demand analysis].) The same analyser also does [http://research.microsoft.com/%7Esimonpj/Papers/cpr/index.htm Constructed Product Result analysis].[[BR]][[BR]] 
     29    * The '''strictness analyser'''.  This actually comprises two passes: the '''analyser''' itself and the '''worker/wrapper''' transformation that uses the results of the analysis to transform the program. (Further described in [wiki:Commentary/Compiler/StrictnessAnalysis Demand analysis].) The same analyser also does [http://research.microsoft.com/%7Esimonpj/Papers/cpr/index.htm Constructed Product Result analysis]. (`-ddump-stranal`)[[BR]][[BR]] 
    3030    * The '''liberate-case''' transformation.[[BR]][[BR]] 
    3131    * The '''constructor-specialialisation''' transformation.[[BR]][[BR]] 
    32     * The '''common sub-expression eliminiation''' (CSE) transformation. 
     32    * The '''common sub-expression eliminiation''' (CSE) transformation. (`-ddump-cse`) 
    3333 
    3434 * Then the '''!CoreTidy pass''' gets the code into a form in which it can be imported into subsequent modules (when using {{{--make}}}) and/or put into an interface file.