[GHC] #14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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: -------------------------------------+------------------------------------- 64 bit GHC has 64 bit Unique values, but they don't fit in a `template- haskell` `NameU` when cross-compiling to a 32 bit target. In theory we can support Template Haskell in this situation with `-fexternal-interpreter` (and the earlier GHCJS implementation), but when the target cannot represent GHC's uniques in an `Int`, things go wrong: from `Language.Haskell.TH.Syntax`: {{{#!hs type Uniq = Int }}} I think we can fix this for now by changing the synonym to `type Uniq = Int64` (or even `type Uniq = Integer`), making the `template-haskell` `Uniq` at least as big as the GHC `Unique` in practice. This requires a change in `DsMeta`. the uses of `Int` below should really be changed to `Uniq` {{{#!hs data NameFlavour = NameS -- ^ An unqualified name; dynamically bound | NameQ ModName -- ^ A qualified name; dynamically bound | NameU !Int -- ^ A unique local name | NameL !Int -- ^ Local name bound outside of the TH AST | NameG NameSpace PkgName ModName -- ^ Global name bound outside of the TH AST: -- An original name (occurrences only, not binders) -- Need the namespace too to be sure which -- thing we are naming deriving ( Data, Eq, Ord, Show, Generic ) }}} {{{#!hs -- Global variable to generate unique symbols counter :: IORef Int {-# NOINLINE counter #-} counter = unsafePerformIO (newIORef 0) }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 RyanGlScott): That seems like a reasonable proposal. Out of curiosity, when you say: Replying to [ticket:14574 luite]:
but when the target cannot represent GHC's uniques in an `Int`, things go wrong:
Do you mean "things go wrong" when trying to build a program that uses Template Haskell? And if so, do you have an example demonstrating what goes wrong? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 luite): Replying to [comment:1 RyanGlScott]:
Do you mean "things go wrong" when trying to build a program that uses Template Haskell? And if so, do you have an example demonstrating what goes wrong?
Yes, when building a program that uses Template Haskell, GHC complains that a name is not in scope, when the real reason is that the `Unique` has been truncated (when using a 64 bit GHC) The following test case was reported on the GHCJS tracker as giving a "not in scope" error with 64 bit GHCJS, but working ok with 32 bit GHCJS ( https://github.com/ghcjs/ghcjs/issues/617 ): {{{#!hs {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE QuasiQuotes #-} module QQBug ( test ) where import qualified NeatInterpolation import Data.Text -- txt = "test1" test :: Text test = [NeatInterpolation.text|$txt|] where txt = "test" }}} {{{ QQBug.hs:13:8: error: • The exact Name ‘txt_XN’ is not in scope Probable cause: you used a unique Template Haskell name (NameU), perhaps via newName, but did not bind it If that's it, then -ddump-splices might be useful • In the quasi-quotation: [NeatInterpolation.text|$txt|] }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 RyanGlScott): Thanks. Have you confirmed that your proposed patch fixes this issue? If so, I propose just going ahead and submitting the patch. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 luite): I don't have a complete patch yet, but I'll make one soon and submit (or report back here if things turn out to be more complicated) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 luite): I now have a patch that validates on `ghc-8.4` and `master`. Changing it to `type Uniq = Integer` turned out to be easiest because GHC doesn't know the data constructor for `Int64`. And I think `Integer` is actually more appropriate here. Some of the other types in the TH library are still machine sized, like operator precedence, but I think those don't lead to many problems in practice, since they tend to be user supplied. I still need to do a bit more testing with GHCJS to make sure that I haven't missed anything. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:5 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 RyanGlScott): Replying to [comment:5 luite]:
Changing it to `type Uniq = Integer` turned out to be easiest because GHC doesn't know the data constructor for `Int64`.
What do you mean by this?
And I think `Integer` is actually more appropriate here.
I'm sure `Integer` technically works, but I am worried that we'd lose some efficiency by doing this, given how widely used `Name`s are in Template Haskell. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:6 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 luite): Replying to [comment:6 RyanGlScott]:
Replying to [comment:5 luite]:
Changing it to `type Uniq = Integer` turned out to be easiest because GHC doesn't know the data constructor for `Int64`.
What do you mean by this?
GHC doesn't have the `GHC.Int.I64#` data constructor as a known name in `PrelNames`
And I think `Integer` is actually more appropriate here.
I'm sure `Integer` technically works, but I am worried that we'd lose some efficiency by doing this, given how widely used `Name`s are in Template Haskell.
I think in practice it would not make much of a difference in situations that were already supported (i.e. target word size is the same as compiler word size). Only the `S#` constructor would be used (the library isn't compiled with `-funbox-strict-fields`). Maybe serialization would add an extra tag byte and there would be some additional unused branches in the code? Any suggestions for a good test case for the performance impact of this change? -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:7 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.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 luite): I've tested the change with the upcoming GHCJS release and it fixes the problem. I'll submit the patch tomorrow after some sleep. Also I now have a way to build GHCJS against a customized GHC library (`ghc-api-ghcjs`) and template haskell (`template-haskell-ghcjs`) package, so GHCJS on GHC 8.2 can get a backported fix and working Template Haskell. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:8 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#14574: Template Haskell Uniq ~ Int leads to external interpreter cross compilation trouble -------------------------------------+------------------------------------- Reporter: luite | Owner: (none) Type: bug | Status: new Priority: normal | Milestone: Component: Compiler | Version: 8.3 Resolution: | Keywords: RemoteGHCi 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 simonmar): * cc: simonmar (added) * keywords: => RemoteGHCi -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/14574#comment:9 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC