Torsten Schmits pushed to branch wip/torsten.schmits/mwb-26-01/abstract-linkables at Glasgow Haskell Compiler / GHC
Commits:
-
e1ce573a
by Torsten Schmits at 2026-07-08T14:44:48+02:00
-
184990db
by Torsten Schmits at 2026-07-08T17:55:04+02:00
5 changed files:
- compiler/GHC/Driver/Env/Types.hs
- compiler/GHC/Driver/Main.hs
- compiler/GHC/Linker/Deps.hs
- compiler/GHC/Linker/Loader.hs
- compiler/GHC/Unit/Module/ModIface.hs
Changes:
| ... | ... | @@ -3,6 +3,8 @@ |
| 3 | 3 | module GHC.Driver.Env.Types
|
| 4 | 4 | ( Hsc(..)
|
| 5 | 5 | , HscEnv(..)
|
| 6 | + , LinkDeps (..)
|
|
| 7 | + , Linkables (..)
|
|
| 6 | 8 | ) where
|
| 7 | 9 | |
| 8 | 10 | import GHC.Driver.Errors.Types ( GhcMessage )
|
| ... | ... | @@ -29,6 +31,10 @@ import Control.Monad.Trans.Reader |
| 29 | 31 | import Control.Monad.Trans.State
|
| 30 | 32 | import Data.IORef
|
| 31 | 33 | import GHC.Driver.Env.KnotVars
|
| 34 | +import GHC.Types.SrcLoc (SrcSpan)
|
|
| 35 | +import GHC.Linker.Types (Linkable, LoaderState)
|
|
| 36 | +import GHC.Unit.Types (UnitId, Module)
|
|
| 37 | +import GHC.Types.Unique.DSet (UniqDSet)
|
|
| 32 | 38 | |
| 33 | 39 | -- | The Hsc monad: Passing an environment and diagnostic state
|
| 34 | 40 | newtype Hsc a = Hsc (HscEnv -> Messages GhcMessage -> IO (a, Messages GhcMessage))
|
| ... | ... | @@ -44,6 +50,18 @@ instance ContainsDynFlags HscEnv where |
| 44 | 50 | instance HasLogger Hsc where
|
| 45 | 51 | getLogger = Hsc $ \e w -> return (hsc_logger e, w)
|
| 46 | 52 | |
| 53 | +data LinkDeps = LinkDeps
|
|
| 54 | + { ldNeededLinkables :: [Linkable]
|
|
| 55 | + , ldAllLinkables :: [Linkable]
|
|
| 56 | + , ldNeededUnits :: [UnitId]
|
|
| 57 | + , ldAllUnits :: UniqDSet UnitId
|
|
| 58 | + }
|
|
| 59 | + |
|
| 60 | +data Linkables where
|
|
| 61 | + Linkables :: {
|
|
| 62 | + linkablesResolve :: SrcSpan -> [Module] -> IO a,
|
|
| 63 | + linkablesSelect :: a -> IO LinkDeps
|
|
| 64 | + } -> Linkables
|
|
| 47 | 65 | |
| 48 | 66 | -- | HscEnv is like 'GHC.Driver.Monad.Session', except that some of the fields are immutable.
|
| 49 | 67 | -- An HscEnv is used to compile a single module from plain Haskell source
|
| ... | ... | @@ -87,6 +105,8 @@ data HscEnv |
| 87 | 105 | -- ^ target code interpreter (if any) to use for TH and GHCi.
|
| 88 | 106 | -- See Note [Target code interpreter]
|
| 89 | 107 | |
| 108 | + , hsc_linkables :: HscEnv -> LoaderState -> IO Linkables
|
|
| 109 | + |
|
| 90 | 110 | , hsc_plugins :: !Plugins
|
| 91 | 111 | -- ^ Plugins
|
| 92 | 112 |
| ... | ... | @@ -304,7 +304,6 @@ import GHC.StgToCmm.Utils (IPEStats) |
| 304 | 304 | import GHC.Types.Unique.FM
|
| 305 | 305 | import GHC.Types.Unique.DFM
|
| 306 | 306 | import GHC.Cmm.Config (CmmConfig)
|
| 307 | -import Data.Bifunctor
|
|
| 308 | 307 | import qualified GHC.Unit.Home.Graph as HUG
|
| 309 | 308 | import GHC.Unit.Home.PackageTable
|
| 310 | 309 | |
| ... | ... | @@ -341,6 +340,7 @@ newHscEnvWithHUG top_dir top_dynflags cur_unit home_unit_graph = do |
| 341 | 340 | , hsc_FC = fc_var
|
| 342 | 341 | , hsc_type_env_vars = emptyKnotVars
|
| 343 | 342 | , hsc_interp = Nothing
|
| 343 | + , hsc_linkables = linkablesDefault
|
|
| 344 | 344 | , hsc_unit_env = unit_env
|
| 345 | 345 | , hsc_plugins = emptyPlugins
|
| 346 | 346 | , hsc_hooks = emptyHooks
|
| ... | ... | @@ -2870,10 +2870,10 @@ jsCodeGen hsc_env srcspan i this_mod stg_binds_with_deps binding_id = do |
| 2870 | 2870 | |
| 2871 | 2871 | -- Take lock for the actual work.
|
| 2872 | 2872 | (dep_linkables, dep_units) <- modifyLoaderState interp $ \pls -> do
|
| 2873 | - let link_opts = initLinkDepsOpts hsc_env
|
|
| 2874 | 2873 | |
| 2875 | 2874 | -- Find what packages and linkables are required
|
| 2876 | - deps <- getLinkDeps link_opts interp pls srcspan needed_mods
|
|
| 2875 | + linkables <- hsc_linkables hsc_env hsc_env pls
|
|
| 2876 | + deps <- linkablesGet linkables srcspan needed_mods
|
|
| 2877 | 2877 | -- We update the LinkerState even if the JS interpreter maintains its linker
|
| 2878 | 2878 | -- state independently to load new objects here.
|
| 2879 | 2879 |
| ... | ... | @@ -12,6 +12,8 @@ module GHC.Linker.Deps |
| 12 | 12 | ( LinkDepsOpts (..)
|
| 13 | 13 | , LinkDeps (..)
|
| 14 | 14 | , getLinkDeps
|
| 15 | + , Linkables (..)
|
|
| 16 | + , linkablesGet
|
|
| 15 | 17 | )
|
| 16 | 18 | where
|
| 17 | 19 | |
| ... | ... | @@ -46,7 +48,6 @@ import GHC.Iface.Errors.Ppr |
| 46 | 48 | |
| 47 | 49 | import GHC.Utils.Misc
|
| 48 | 50 | import GHC.Unit.Home
|
| 49 | -import qualified GHC.Unit.Home.Graph as HUG
|
|
| 50 | 51 | import GHC.Data.Maybe
|
| 51 | 52 | |
| 52 | 53 | import Control.Applicative
|
| ... | ... | @@ -61,10 +62,8 @@ import System.FilePath |
| 61 | 62 | import System.Directory
|
| 62 | 63 | import GHC.Utils.Logger (Logger)
|
| 63 | 64 | import Control.Monad ((<$!>))
|
| 64 | -import GHC.Driver.Env
|
|
| 65 | -import {-# SOURCE #-} GHC.Driver.Main
|
|
| 66 | -import Data.Time.Clock
|
|
| 67 | 65 | import GHC.Unit.Home.Graph
|
| 66 | +import GHC.Driver.Env.Types (Linkables (..), LinkDeps (..))
|
|
| 68 | 67 | |
| 69 | 68 | |
| 70 | 69 | data LinkDepsOpts = LinkDepsOpts
|
| ... | ... | @@ -86,12 +85,9 @@ data LinkDepsOpts = LinkDepsOpts |
| 86 | 85 | , ldLogger :: !Logger
|
| 87 | 86 | }
|
| 88 | 87 | |
| 89 | -data LinkDeps = LinkDeps
|
|
| 90 | - { ldNeededLinkables :: [Linkable]
|
|
| 91 | - , ldAllLinkables :: [Linkable]
|
|
| 92 | - , ldNeededUnits :: [UnitId]
|
|
| 93 | - , ldAllUnits :: UniqDSet UnitId
|
|
| 94 | - }
|
|
| 88 | +linkablesGet :: Linkables -> SrcSpan -> [Module] -> IO LinkDeps
|
|
| 89 | +linkablesGet Linkables {..} span mods =
|
|
| 90 | + linkablesSelect =<< linkablesResolve span mods
|
|
| 95 | 91 | |
| 96 | 92 | -- | Find all the packages and linkables that a set of modules depends on
|
| 97 | 93 | --
|
| ... | ... | @@ -32,6 +32,7 @@ module GHC.Linker.Loader |
| 32 | 32 | , rmDupLinkables
|
| 33 | 33 | , modifyLoaderState
|
| 34 | 34 | , initLinkDepsOpts
|
| 35 | + , linkablesDefault
|
|
| 35 | 36 | )
|
| 36 | 37 | where
|
| 37 | 38 | |
| ... | ... | @@ -97,7 +98,6 @@ import Control.Monad |
| 97 | 98 | import qualified Data.Set as Set
|
| 98 | 99 | import Data.Char (isSpace)
|
| 99 | 100 | import Data.Functor ((<&>))
|
| 100 | -import qualified Data.Foldable as Foldable
|
|
| 101 | 101 | import Data.IORef
|
| 102 | 102 | import Data.List (intercalate, isPrefixOf, nub, partition)
|
| 103 | 103 | import Data.Maybe
|
| ... | ... | @@ -115,7 +115,7 @@ import System.Win32.Info (getSystemDirectory) |
| 115 | 115 | #endif
|
| 116 | 116 | |
| 117 | 117 | import GHC.Utils.Exception
|
| 118 | -import GHC.Unit.Home.Graph (lookupHug, unitEnv_foldWithKey)
|
|
| 118 | +import GHC.Unit.Home.Graph (unitEnv_foldWithKey)
|
|
| 119 | 119 | |
| 120 | 120 | -- Note [Linkers and loaders]
|
| 121 | 121 | -- ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -231,10 +231,10 @@ loadDependencies |
| 231 | 231 | -> IO (LoaderState, SuccessFlag, [Linkable], PkgsLoaded) -- ^ returns the set of linkables required
|
| 232 | 232 | -- When called, the loader state must have been initialized (see `initLoaderState`)
|
| 233 | 233 | loadDependencies interp hsc_env pls span needed_mods = do
|
| 234 | - let opts = initLinkDepsOpts hsc_env
|
|
| 234 | + linkables <- hsc_linkables hsc_env hsc_env pls
|
|
| 235 | 235 | |
| 236 | 236 | -- Find what packages and linkables are required
|
| 237 | - deps <- getLinkDeps opts interp pls span needed_mods
|
|
| 237 | + deps <- linkablesGet linkables span needed_mods
|
|
| 238 | 238 | |
| 239 | 239 | let this_pkgs_needed = ldAllUnits deps
|
| 240 | 240 | |
| ... | ... | @@ -683,6 +683,15 @@ initLinkDepsOpts hsc_env = opts |
| 683 | 683 | EPS {eps_iface_bytecode} <- hscEPS hsc_env
|
| 684 | 684 | pure (lookupModuleEnv eps_iface_bytecode mod)
|
| 685 | 685 | |
| 686 | +linkablesDefault ::
|
|
| 687 | + HscEnv ->
|
|
| 688 | + LoaderState ->
|
|
| 689 | + IO Linkables
|
|
| 690 | +linkablesDefault hsc_env pls =
|
|
| 691 | + pure Linkables {
|
|
| 692 | + linkablesResolve = getLinkDeps (initLinkDepsOpts hsc_env) (hscInterp hsc_env) pls,
|
|
| 693 | + linkablesSelect = pure
|
|
| 694 | + }
|
|
| 686 | 695 | |
| 687 | 696 | |
| 688 | 697 | {- **********************************************************************
|
| ... | ... | @@ -503,7 +503,7 @@ instance Binary ModIface where |
| 503 | 503 | lazyPut bh warns
|
| 504 | 504 | lazyPut bh anns
|
| 505 | 505 | put_ bh decls
|
| 506 | - put_ bh extra_decls
|
|
| 506 | + lazyPut bh extra_decls
|
|
| 507 | 507 | put_ bh foreign_
|
| 508 | 508 | put_ bh insts
|
| 509 | 509 | put_ bh fam_insts
|
| ... | ... | @@ -536,7 +536,7 @@ instance Binary ModIface where |
| 536 | 536 | warns <- {-# SCC "bin_warns" #-} lazyGet bh
|
| 537 | 537 | anns <- {-# SCC "bin_anns" #-} lazyGet bh
|
| 538 | 538 | decls <- {-# SCC "bin_tycldecls" #-} get bh
|
| 539 | - extra_decls <- get bh
|
|
| 539 | + extra_decls <- lazyGet bh
|
|
| 540 | 540 | foreign_ <- get bh
|
| 541 | 541 | insts <- {-# SCC "bin_insts" #-} get bh
|
| 542 | 542 | fam_insts <- {-# SCC "bin_fam_insts" #-} get bh
|