Quantcast

[GHC] #5075: CPR optimisation for sum types if only one constructor is used

classic Classic list List threaded Threaded
14 messages Options
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

[GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:              
        Type:  feature request   |      Status:  new        
    Priority:  normal            |   Component:  Compiler    
     Version:  7.0.3             |    Keywords:              
    Testcase:                    |   Blockedby:              
          Os:  Unknown/Multiple  |    Blocking:              
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown
---------------------------------+------------------------------------------
 Inspired by #3138, it might be useful for StrAnal to detect functions such
 as the following where only one of the data constructors for a sum type
 are CPRed:

 {{{
 loop x = case x < 10 of True -> Left x; False -> loop (x*2)
 }}}

 We can usefully transform to:

 {{{
 $wloop x = case (case x < 10 of True -> Left x; False -> loop (x*2)) of
 Left y -> (# y #)
 loop x = case loop x of (# y #) -> Left y
 }}}

 Attached patch implements this behaviour. Most of the complication in the
 new code occurs because adding a DataCon field to the Demand data type
 means that we have to define a separate IfaceDemand type for storage in
 interface files.

 The patch validates but I haven't done any tests on nofib. I have
 confirmed that the new optimisation hits on some examples, though.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:              
        Type:  feature request   |      Status:  patch      
    Priority:  normal            |   Component:  Compiler    
     Version:  7.0.3             |    Keywords:              
    Testcase:                    |   Blockedby:              
          Os:  Unknown/Multiple  |    Blocking:              
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown
---------------------------------+------------------------------------------
Changes (by batterseapower):

  * status:  new => patch


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:1>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |        Owner:  simonpj    
        Type:  feature request   |       Status:  patch      
    Priority:  normal            |    Milestone:              
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------
Changes (by simonpj):

  * owner:  => simonpj


Comment:

 I'm working on this

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |        Owner:  simonpj    
        Type:  feature request   |       Status:  patch      
    Priority:  normal            |    Milestone:  7.4.1      
   Component:  Compiler          |      Version:  7.0.3      
    Keywords:                    |     Testcase:              
   Blockedby:                    |   Difficulty:              
          Os:  Unknown/Multiple  |     Blocking:              
Architecture:  Unknown/Multiple  |      Failure:  None/Unknown
---------------------------------+------------------------------------------
Changes (by igloo):

  * milestone:  => 7.4.1


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:3>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.1          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by simonpj):

  * difficulty:  => Unknown


Comment:

 All this is on the `cpr-sum-types` branch.  Awaiting attention from Simon!

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:5>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by akio):

 * cc: tkn.akio@… (added)


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by simonpj@…):

 commit d3b8991be3875302ca6d1a4ef6e72891e9567dd5
 {{{
 Author: Simon Peyton Jones <[hidden email]>
 Date:   Thu Jan 24 14:50:50 2013 +0000

     Introduce CPR for sum types (Trac #5075)

     The main payload of this patch is to extend CPR so that it
     detects when a function always returns a result constructed
     with the *same* constructor, even if the constructor comes from
     a sum type.  This doesn't matter very often, but it does improve
     some things (results below).

     Binary sizes increase a little bit, I think because there are more
     wrappers.  This with -split-objs.  Without split-ojbs binary sizes
     increased by 6% even for HelloWorld.hs.  It's hard to see exactly why,
     but I think it was because System.Posix.Types.o got included in the
     linked binary, whereas it didn't before.

             Program           Size    Allocs   Runtime   Elapsed  TotalMem
               fluid          +1.8%     -0.3%      0.01      0.01     +0.0%
                 tak          +2.2%     -0.2%      0.02      0.02     +0.0%
                ansi          +1.7%     -0.3%      0.00      0.00     +0.0%
           cacheprof          +1.6%     -0.3%     +0.6%     +0.5%     +1.4%
             parstof          +1.4%     -4.4%      0.00      0.00     +0.0%
             reptile          +2.0%     +0.3%      0.02      0.02     +0.0%
     ----------------------------------------------------------------------
                 Min          +1.1%     -4.4%     -4.7%     -4.7%    -15.0%
                 Max          +2.3%     +0.3%     +8.3%     +9.4%    +50.0%
      Geometric Mean          +1.9%     -0.1%     +0.6%     +0.7%     +0.3%

     Other things in this commit
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
     * Got rid of the Lattice class in Demand

     * Refactored the way that products and newtypes are
       decomposed (no change in functionality)

  compiler/basicTypes/BasicTypes.lhs  |   17 ++
  compiler/basicTypes/DataCon.lhs     |   60 -----
  compiler/basicTypes/Demand.lhs      |  429
 ++++++++++++++++++-----------------
  compiler/basicTypes/MkId.lhs        |   11 +-
  compiler/cmm/CLabel.hs              |    1 -
  compiler/coreSyn/CoreLint.lhs       |   12 +-
  compiler/coreSyn/CoreSyn.lhs        |    7 +-
  compiler/deSugar/DsCCall.lhs        |   45 ++++-
  compiler/deSugar/DsForeign.lhs      |    2 +-
  compiler/prelude/PrelRules.lhs      |    2 +-
  compiler/simplCore/Simplify.lhs     |    5 +-
  compiler/specialise/SpecConstr.lhs  |    2 +-
  compiler/stranal/DmdAnal.lhs        |   49 +++-
  compiler/stranal/WwLib.lhs          |  210 ++++++++----------
  compiler/types/Coercion.lhs         |   62 ++++--
  compiler/types/FamInstEnv.lhs       |   26 +--
  compiler/types/TyCon.lhs            |   25 ++-
  compiler/types/Type.lhs             |   26 +--
  compiler/vectorise/Vectorise/Exp.hs |   10 +-
  19 files changed, 515 insertions(+), 486 deletions(-)
 }}}

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:8>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by simonpj):

 Still to come: the sum-CPR stuff is switched off for '''nested'''
 functions because it turns some let-no-escape functions into non-let-no-
 escape ones, which increases allocation. I'm hopeful that Nick F's work on
 late-lambda-lifting may solve this, in which case we can switch it on more
 vigorously.  Hence leaving open for now.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:9>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by danielv):

 * cc: danielv@… (added)


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:10>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by parcs):

 I noticed that sum CPR does not trigger for this function, when it seems
 like it should:

 {{{
 loop :: Int -> Maybe Int -> Maybe Int
 loop n x = case n of
     0 -> x
     _ -> loop (n-1) (fmap (+1) x)
 }}}

 The specializations that the SpecConstr pass creates for this function
 seem to be perfect candidates for sum CPR:

 {{{
 Rec {
 loop_$s$wloop
 loop_$s$wloop =
   \ sc_sop sc1_sor ->
     case sc_sop of ds_Xmj {
       __DEFAULT ->
         loop_$s$wloop
           (-# ds_Xmj 1) (case sc1_sor of _ { I# x_amE -> I# (+# x_amE 1)
 });
       0 -> Just sc1_sor
     }
 end Rec }

 Rec {
 loop_$s$wloop1
 loop_$s$wloop1 =
   \ sc_soq ->
     case sc_soq of ds_Xmj {
       __DEFAULT -> loop_$s$wloop1 (-# ds_Xmj 1);
       0 -> Nothing
     }
 end Rec }
 }}}

 Or am I mistaken?

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:11>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------
Changes (by simonpj):

 * cc: nfrisby@… (added)


Comment:

 Indeed.  But the opportunity only arises after `SpecConstr`, and we don't
 currently run the demand/CPR analyser after `SpecConstr`, we run it
 before.

 Several other tickets (#6087, #5302, #6070) identify other opportunities
 that could be exploited by running the demand analyser again, late in the
 optimisation pipeline.

 Nick Frisby is planning to experiment with this.  Thanks for a new
 example.

 Simon

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:12>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
---------------------------------+------------------------------------------
    Reporter:  batterseapower    |       Owner:  simonpj        
        Type:  feature request   |      Status:  patch          
    Priority:  normal            |   Milestone:  7.6.2          
   Component:  Compiler          |     Version:  7.0.3          
    Keywords:                    |          Os:  Unknown/Multiple
Architecture:  Unknown/Multiple  |     Failure:  None/Unknown    
  Difficulty:  Unknown           |    Testcase:                  
   Blockedby:                    |    Blocking:                  
     Related:                    |  
---------------------------------+------------------------------------------

Comment(by nfrisby):

 Confirmed: the Just gets ww'd-away with this core2core pipeline:
 SpecConstr, simpl, stranal, wwsplit, simpl.

 {{{
 Rec {
 $w$s$wloop
 $w$s$wloop =
   \ w_sqX w1_sqY ->
     case w_sqX of ds_Xng {
       __DEFAULT ->
         $w$s$wloop
           (-# ds_Xng 1) (case w1_sqY of _ { I# x_anM -> I# (+# x_anM 1)
 });
       0 -> (# w1_sqY #)
     }
 end Rec }

 loop_$s$wloop [InlPrag=INLINE[0]]
   :: ...
 [...
  Str=DmdType <S,U><L,U(U)>m2,
  Unf=Unf{Src=Worker=$w$s$wloop,...}]
 loop_$s$wloop =
   \ w_sqX w1_sqY ->
     case $w$s$wloop w_sqX w1_sqY of _ { (# ww1_sr4 #) -> Just ww1_sr4 }

 Rec {
 loop_$s$wloop1
 loop_$s$wloop1 =
   \ sc_sqe ->
     case sc_sqe of ds_Xng {
       __DEFAULT -> loop_$s$wloop1 (-# ds_Xng 1);
       0 -> Nothing
     }
 end Rec }
 }}}

 That's an impressive prefix, "$w$s$w".

 In case any reader is wondering, the second argument to the loop gets
 unboxed if a strict variant of Maybe is used.

 Thanks again for the compact example.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:13>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
-------------------------------+--------------------------------------------
  Reporter:  batterseapower    |          Owner:                  
      Type:  feature request   |         Status:  new            
  Priority:  normal            |      Milestone:  7.6.2          
 Component:  Compiler          |        Version:  7.0.3          
Resolution:                    |       Keywords:                  
        Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple
   Failure:  None/Unknown      |     Difficulty:  Unknown        
  Testcase:                    |      Blockedby:                  
  Blocking:                    |        Related:                  
-------------------------------+--------------------------------------------
Changes (by igloo):

  * owner:  simonpj =>
  * status:  patch => new


Comment:

 It looks like the patch has been applied to the cpr-sum-types branch.

--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:14>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
GHC
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: [GHC] #5075: CPR optimisation for sum types if only one constructor is used

GHC
In reply to this post by GHC
#5075: CPR optimisation for sum types if only one constructor is used
-------------------------------+--------------------------------------------
  Reporter:  batterseapower    |          Owner:  simonpj        
      Type:  feature request   |         Status:  new            
  Priority:  normal            |      Milestone:  7.6.2          
 Component:  Compiler          |        Version:  7.0.3          
Resolution:                    |       Keywords:                  
        Os:  Unknown/Multiple  |   Architecture:  Unknown/Multiple
   Failure:  None/Unknown      |     Difficulty:  Unknown        
  Testcase:                    |      Blockedby:                  
  Blocking:                    |        Related:                  
-------------------------------+--------------------------------------------
Changes (by igloo):

  * owner:  => simonpj


--
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/5075#comment:15>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
ghc-tickets mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/ghc-tickets
Loading...