[GHC] #15490: Can Template Haskell and RULES be combined?

#15490: Can Template Haskell and RULES be combined? -------------------------------------+------------------------------------- Reporter: ChaiTRex | Owner: (none) Type: feature | Status: new request | Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- It would be nice if there was something like this: {{{#!hs {-# RULES "(literal*)" forall n. (n*) = $( makeOptimizedMultiplication [|| n ||] ) #-} }}} where `makeOptimizedMultiplication` is a Template Haskell function that, if `n` is an integer literal, produces an optimized multiplication function for that literal. However, that code above gives a `VarE` corresponding to the `n` in the quoter rather than the AST of the `n` that's actually being matched by the rewrite rule, so I can't get a `LitE` that way. This sort of thing would come in very handy for a large fixed-width integer type, because I don't think GHC and LLVM will know that standard fixed-width integer optimizations apply to it. For example, with a `Word65536` type, multiplying blindly at runtime by the literal `1099511627777` requires a lot of extra work. At the very least, all 65536 bits of that literal have to be read and dealt with pretty blindly. However, at compile time, the compiler can take the time to figure out that `(1099511627777*)` can be replaced with the much, much lighter `(\x -> shiftL x 40 + x)` (and not only that, but if `Word65536`s are stored in a byte array, 1 byte evenly divides the shift amount of 40 bits, which can be exploited to speed things up greatly). With appropriate `RULES` and Template Haskell functions, the compiler gains the ability to apply lots of fixed-width integer optimizations. Since the above sort of code doesn't work, is there some other way to do this that I don't know about? If not, can the feature be added? ---- The only downside I can think of is that the user of the `Word65536` type might not knowingly be expecting Template Haskell to be at work since they didn't use any splices or quoters and might not have even included `{-# LANGUAGE TemplateHaskell #-}` in their code, so perhaps some easy method of consenting to the application of TH-based rewrites might be in order. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15490 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15490: Can Template Haskell and RULES be combined? -------------------------------------+------------------------------------- Reporter: ChaiTRex | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by nomeata): Template Haskell is the wrong tool for this, since it deals with Haskell (the surface language), but rules actually apply to Core. But I suppose you can write a GHC plugin that installs a `BuiltInRule` (see https://downloads.haskell.org/~ghc/latest/docs/html/libraries/ghc-8.4.3/Core...) which can have arbitrary functions to decide whether the rule fires, and what to replace it with. Or write a GHC Plugin with a Core2Core pass that simply rewrites the code as desired. In all cases, the plugin will have to be loaded explicitly, i.e. these kind of rules are not exported. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15490#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15490: Can Template Haskell and RULES be combined? -------------------------------------+------------------------------------- Reporter: ChaiTRex | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by ChaiTRex): OK. Thanks for the pointer to GHC plugins. I'll look into that. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15490#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#15490: Can Template Haskell and RULES be combined? -------------------------------------+------------------------------------- Reporter: ChaiTRex | Owner: (none) Type: feature request | Status: closed Priority: normal | Milestone: 8.6.1 Component: Compiler | Version: 8.4.3 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by ChaiTRex): * status: new => closed * resolution: => invalid -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/15490#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC