Add support for executing TH splices on the host platform during cross compilation.
Summary
Currently when using TH we require any splices to be executed on the target platform.
While this simplifies things for GHC in a lot of ways it can make building for different platforms difficult as it requires either access to target platform hardware or simulation thereof.
I believe for most uses of TH it is possible (although not straight forward) to lift this restriction.
Ideal workflow for the feature.
Here is how I imagine this could work if I could simply snap my fingers to make it happen:
- GHC would support targeting different platforms at runtime.
- When cross-compiling a project that uses TH we would pass to the build tool both the target platform and
-fsplices-on-host
. - The build tool compiles all dependencies first for the host (potentially only to bytecode) as one would do today.
- Then GHC is invoked with a cross platform target, passing
-fsplices-on-host
. This would result in splices being executed on the host using the libraries built in the second step.
Current roadblocks
Libraries need to be available for both target and host platform.
In order to run TH splices on the host all the dependencies of the splice need to be available.
With build tool support this could be achieved on top of #11470 / #23682
In practice this would mean compiling most code twice, obviously once for the target platform, but also once to ensure the dependencies are available for the splices.
Splices which give different results for different platforms.
Code which relies on things like Int bit-width or other platform dependent behavior can give different results for different platforms. This makes running on the host by default very dangerous as it might introduce subtle breakage.
I believe it's acceptable to allow users to opt-into executing splices on the host implicitly accepting such breakage by doing so.
A more extreme approach would be to define a virtual TH target platform which can be emulated independent of the actual host platform. This would mean TH splices would give the same results independent of the host they are run on.
One example of such an approach in a different project is the External STG Interpreter project, which implemented semantics for most of GHCs primops.