[GHC] #12791: Superclass methods could be more aggressively specialised.

GHC ghc-devs at haskell.org
Tue Jan 31 23:00:20 UTC 2017


#12791: Superclass methods could be more aggressively specialised.
-------------------------------------+-------------------------------------
        Reporter:  mpickering        |                Owner:  danharaj
            Type:  bug               |               Status:  new
        Priority:  normal            |            Milestone:
       Component:  Compiler          |              Version:  8.0.1
      Resolution:                    |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:  #5835             |  Differential Rev(s):  Phab:D2714
       Wiki Page:                    |
-------------------------------------+-------------------------------------

Comment (by Matthew Pickering <matthewtpickering@…>):

 In [changeset:"748b79741652028827b6225c36b8ab55d22bdeb0/ghc"
 748b7974/ghc]:
 {{{
 #!CommitTicketReference repository="ghc"
 revision="748b79741652028827b6225c36b8ab55d22bdeb0"
 Use top-level instances to solve superclasses where possible

 This patch introduces a new flag `-fsolve-constant-dicts` which makes the
 constraint solver solve super class constraints with available
 dictionaries if
 possible. The flag is enabled by `-O1`.

 The motivation of this patch is that the compiler can produce more
 efficient
 code if the constraint solver used top-level instance declarations to
 solve
 constraints that are currently solved givens and their superclasses. In
 particular, as it currently stands, the compiler imposes a performance
 penalty
 on the common use-case where superclasses are bundled together for user
 convenience. The performance penalty applies to constraint synonyms as
 well. This example illustrates the issue:

 ```
 {-# LANGUAGE ConstraintKinds, MultiParamTypeClasses, FlexibleContexts #-}
 module B where

 class M a b where m :: a -> b

 type C a b = (Num a, M a b)

 f :: C Int b => b -> Int -> Int
 f _ x = x + 1
 ```

 Output without the patch, notice that we get the instance for `Num Int` by
 using the class selector `p1`.

 ```
 f :: forall b_arz. C Int b_arz => b_arz -> Int -> Int
 f =
   \ (@ b_a1EB) ($d(%,%)_a1EC :: C Int b_a1EB) _ (eta1_B1 :: Int) ->
     + @ Int
       (GHC.Classes.$p1(%,%) @ (Num Int) @ (M Int b_a1EB) $d(%,%)_a1EC)
       eta1_B1
       B.f1
 ```

 Output with the patch, nicely optimised code!

 ```
 f :: forall b. C Int b => b -> Int -> Int
 f =
   \ (@ b) _ _ (x_azg :: Int) ->
     case x_azg of { GHC.Types.I# x1_a1DP ->
     GHC.Types.I# (GHC.Prim.+# x1_a1DP 1#)
     }
 ```

 Reviewers: simonpj, bgamari, austin

 Reviewed By: simonpj

 Subscribers: mpickering, rwbarton, thomie

 Differential Revision: https://phabricator.haskell.org/D2714

 GHC Trac Issues: #12791, #5835
 }}}

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


More information about the ghc-tickets mailing list