[GHC] #11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore)

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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: -------------------------------------+------------------------------------- Allow intermediate code inline. == Inline ''Core'' == Typeable/WithTypeable:
{{{#!hs withTypeable :: TTypeRep a -> (Typeable a => r) -> r }}}
Currently, `withTypeable` cannot be written in Haskell, but it is straightforward to write in Core. Can we fix this?
We cannot implement ''withTypeable'' in Haskell source. But we ''can'' implement it in GHC's statically-typed intermediate language, System FC. The definition is simple, roughly like this:
{{{#!hs withTypeable tr k = k tr -- Not quite right }}}
Its second argument ''k'' expects a ''Typeable'' dictionary as its value argument. But since a ''Typeable'' dictionary is represented by a ''TypeRep'', we can simply
[http://www.seas.upenn.edu/~sweirich/papers/wadlerfest2016.pdf A reflection on types] (''2016''): pass ''tr'' to ''k''.
When written in System FC there is a type-safe coercion to move from ''TypeRep a'' to ''Typeable a'', but that coercion is erased at runtime. Since the definition can be statically type checked, ''withTypeable'' does not form part of the trusted code base.
== Inline ''Cmm'' == Edkward Kmett writes his own `.cmm` [https://github.com/ekmett/concurrent/blob/master/cbits/hashmap.cmm hither], [https://github.com/ekmett/concurrent/blob/master/cbits/array.cmm yon]. == Inline ''Strict Core'' == Should [http://research.microsoft.com/en-us/um/people/simonpj/papers /strict-core/tacc-hs09.pdf Types are calling conventions] (''2009'') be implemented:
=== '''4.4 The ''`seq`'' function''' ===
A nice feature of Strict Core,,ANF,, is that it is possible to give a straightforward definition of the primitive ''seq'' function of Haskell: {{{#!hs seq : {a:* -> b:* -> {a} -> {b} -> b} = { \a:*. \b:*. \x:{a}. \y:{b}. let _:a = x <> in y <> } }}}
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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 goldfire): I think this would be quite useful. And quite a lot of work. Are you volunteering to do it? :) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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: | -------------------------------------+------------------------------------- Changes (by thomie): * type: bug => feature request -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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 Iceland_jack): Another example of Core from [http://osdir.com/ml/general/2016-01/msg29309.html ghc-devs]:
It’d probably need a built-in function {{{#!hs setCallStack :: CallStack -> (AppendsCallStack => a) -> a }}} Correct. This is easy to write in Core but not in Haskell. We also need something similar for Typeable, when we get a type-indexed version of TypeRep {{{#!hs withTypeable :: TypeRep a -> (Typeable a => r) => r }}}
-- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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 Iceland_jack): Quick coinage: === Verb === to '''push one's own applecart''' * (''idiomatic'') To strive or push for efforts that realise one's own ideas or vision. === See also === * [https://en.wiktionary.org/wiki/actions_speak_louder_than_words#English actions speak louder than words] ([https://en.wiktionary.org/wiki/res,_non_verba#Latin res, non verba]) * to [https://en.wiktionary.org/wiki/put_one's_money_where_one's_mouth_is#English put one's money where one's mouth is] ---- Replying to [comment:1 goldfire]:
I think this would be quite useful. And quite a lot of work. Are you volunteering to do it? :)
If there is support for the idea I can push my own applecart (`...`) and take it on, but I would need feedback from those more familiar with GHC internals to begin forming the idea. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.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 Iceland_jack): Focusing on Core for now, we can reuse Haskell's syntax (some intersection between Haskell and Core syntax), see #11350, with minor sugar for functions: {{{#!hs {-# CORE id #-} id :: forall a. a -> a id @a (x :: a) = x {-# CORE add #-} double :: forall num. Num num => num -> num double (@ num) (numDict :: Num num) (x :: num) = (+) (@ num) numDict x x }}} or we could cheekily reuse Template Haskell. Like [https://ghc.haskell.org/trac/ghc/wiki/Records/OverloadedRecordFields/Overloa... this suggestions] to define a magic module for implicit values we can have define a magic module of quoters (`QuasiQuoter`s for sublanguages that are treated specially: {{{#!hs import GHC.Magic (core, stg) [core| id :: forall a. a -> a id = \ (@ a) (x :: a) -> x double :: forall num. Num num => num -> num double = \(@ num) (numDict :: Num num) (x :: num) -> + @ num numDict x x |] }}} or a combinator library where users code directly in Core AST, `data Expr b = Var Id | ...`. Some questions: * Dictionaries and their scope. * Writing function metadata. * Only top-level definitions? * ... Interested in other uses. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): For posteriority: [http://blog.haskell-exists.com/yuras/posts/turn-ghc- into-frontend-for-ministg.html Turn GHC into a frontend for miniSTG] ([https://www.reddit.com/r/haskell/comments/4akub2/turn_ghc_into_a_frontend_fo... reddit post]). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): ticket:11715#comment:9 covers some of the same ground -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by Iceland_jack): [https://www.youtube.com/watch?v=5Py0l7CfQTo&index=24&list=PLMTm6Ln7vQZZv6sQ0I4R7iaIjvSVhHXod#t=69.164308 Daniel Patterson - FunTAL: Reasonably Mixing a Functional Language with Assembly] -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#11441: RFC: Inline intermediate languages (Core, STG, Cmm, even StrictCore) -------------------------------------+------------------------------------- Reporter: Iceland_jack | Owner: (none) Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.10.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: Runtime | Unknown/Multiple performance bug | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Comment (by recursion-ninja): I would find this capability extremely useful for writing optimized string alignment code that is still under the purview of the GHC memory manager instead of an FFI binding to C or C++ where the memory models are disjoint and sharing is much more difficult. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/11441#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC