Hannes Siebenhandl pushed to branch wip/fendor/one-shot-no-eps at Glasgow Haskell Compiler / GHC
Commits:
-
ce01df3c
by fendor at 2026-11-30T14:04:05+01:00
5 changed files:
- compiler/GHC/Iface/Load.hs
- compiler/GHC/Linker/Loader.hs
- compiler/GHC/Unit/External.hs
- compiler/GHC/Unit/Home/PackageTable.hs
- compiler/GHC/Unit/Module/WholeCoreBindings.hs
Changes:
| ... | ... | @@ -835,7 +835,7 @@ loadInterface doc_str mod from |
| 835 | 835 | massertPpr (moduleUnitId mod `Set.member` hsc_all_home_unit_ids hsc_env)
|
| 836 | 836 | (text "Attempting to load non-home package interface into the Home Loader State" $$ ppr (HUG.allUnits $ hsc_HUG hsc_env) $$ doc_str $$ ppr mod $$ ppr (moduleUnitId mod))
|
| 837 | 837 | tcParts <- tcPartsOfTheIface mod iface
|
| 838 | - addModIfacePartsToHpt gbl_env mod iface tcParts
|
|
| 838 | + addModIfacePartsToHpt gbl_env mod loc iface tcParts
|
|
| 839 | 839 | else dontLeakTheHUG $ do
|
| 840 | 840 | -- Load the new ModIface into the External Package State
|
| 841 | 841 | --
|
| ... | ... | @@ -857,7 +857,7 @@ loadInterface doc_str mod from |
| 857 | 857 | (moduleUnitId mod `Set.notMember` hsc_all_home_unit_ids hsc_env)
|
| 858 | 858 | (text "Attempting to load home package interface into the EPS" $$ ppr (HUG.allUnits $ hsc_HUG hsc_env) $$ doc_str $$ ppr mod $$ ppr (moduleUnitId mod))
|
| 859 | 859 | tcParts <- tcPartsOfTheIface mod iface
|
| 860 | - addModIfacePartsToEps mod loc iface tcParts
|
|
| 860 | + addModIfacePartsToEps mod iface tcParts
|
|
| 861 | 861 | |
| 862 | 862 | ; -- invoke plugins with *full* interface, not final_iface, to ensure
|
| 863 | 863 | -- that plugins have access to declarations, etc.
|
| ... | ... | @@ -885,8 +885,12 @@ tcPartsOfTheIface mod iface = do |
| 885 | 885 | , new_complete_matches
|
| 886 | 886 | )
|
| 887 | 887 | |
| 888 | -addModIfacePartsToHpt :: IfGblEnv -> Module -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL ()
|
|
| 889 | -addModIfacePartsToHpt gbl_env mod iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do
|
|
| 888 | +addModIfacePartsToHpt :: IfGblEnv -> Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL ()
|
|
| 889 | +addModIfacePartsToHpt gbl_env mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do
|
|
| 890 | + -- NB: this is the full 'HscEnv', not one purged by 'dontLeakTheHUG':
|
|
| 891 | + -- compiling the Core bindings to bytecode typechecks them, which may need
|
|
| 892 | + -- to look up other home modules, and those live in the HPT.
|
|
| 893 | + hsc_env <- getTopEnv
|
|
| 890 | 894 | let
|
| 891 | 895 | details = ModDetails
|
| 892 | 896 | { md_types = mkNameEnv new_decls
|
| ... | ... | @@ -905,14 +909,20 @@ addModIfacePartsToHpt gbl_env mod iface (new_decls, new_insts, new_defaults, new |
| 905 | 909 | -- of one's own boot file! (one-shot only)
|
| 906 | 910 | -- See Note [Loading your own hi-boot file]
|
| 907 | 911 | |
| 912 | + -- Create an IO action that loads and compiles bytecode from Core
|
|
| 913 | + -- bindings, if the interface has any.
|
|
| 914 | + --
|
|
| 915 | + -- See Note [Interface Files with Core Definitions]
|
|
| 916 | + mb_bytecode = loadIfaceByteCode hsc_env iface loc (md_types details)
|
|
| 917 | + |
|
| 908 | 918 | warnPprTrace bad_boot "loadInterface" (ppr mod) $ do
|
| 909 | 919 | hpt <- getHpt
|
| 910 | 920 | when (not bad_boot) $ do
|
| 911 | 921 | liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt
|
| 912 | - liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches)
|
|
| 922 | + liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode
|
|
| 913 | 923 | |
| 914 | -addModIfacePartsToEps :: Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL ()
|
|
| 915 | -addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do
|
|
| 924 | +addModIfacePartsToEps :: Module -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL ()
|
|
| 925 | +addModIfacePartsToEps mod iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do
|
|
| 916 | 926 | purged_hsc_env <- getTopEnv
|
| 917 | 927 | |
| 918 | 928 | let
|
| ... | ... | @@ -924,17 +934,6 @@ addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam |
| 924 | 934 | & set_mi_rules (panic "No mi_rules in PIT")
|
| 925 | 935 | & set_mi_anns (panic "No mi_anns in PIT")
|
| 926 | 936 | & set_mi_simplified_core (panic "No mi_simplified_core in PIT")
|
| 927 | - -- Create an IO action that loads and compiles bytecode from Core
|
|
| 928 | - -- bindings.
|
|
| 929 | - --
|
|
| 930 | - -- See Note [Interface Files with Core Definitions]
|
|
| 931 | - add_bytecode old
|
|
| 932 | - | Just action <- loadIfaceByteCode purged_hsc_env iface loc (mkNameEnv new_decls)
|
|
| 933 | - = extendModuleEnv old mod action
|
|
| 934 | - -- Don't add an entry if the iface doesn't have 'extra_decls'
|
|
| 935 | - -- so 'get_link_deps' knows that it should load object code.
|
|
| 936 | - | otherwise
|
|
| 937 | - = old
|
|
| 938 | 937 | |
| 939 | 938 | mhome_unit = ue_homeUnit (hsc_unit_env purged_hsc_env)
|
| 940 | 939 | updateEps_ $ \ eps ->
|
| ... | ... | @@ -944,7 +943,6 @@ addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam |
| 944 | 943 | eps {
|
| 945 | 944 | eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface,
|
| 946 | 945 | eps_PTE = addDeclsToPTE (eps_PTE eps) new_decls,
|
| 947 | - eps_iface_bytecode = add_bytecode (eps_iface_bytecode eps),
|
|
| 948 | 946 | eps_rule_base = extendRuleBaseList (eps_rule_base eps)
|
| 949 | 947 | new_rules,
|
| 950 | 948 | eps_complete_matches
|
| ... | ... | @@ -90,7 +90,7 @@ import GHC.Utils.TmpFs |
| 90 | 90 | |
| 91 | 91 | import GHC.Unit.Env
|
| 92 | 92 | import GHC.Unit.Home.ModInfo
|
| 93 | -import GHC.Unit.External (ExternalPackageState (..))
|
|
| 93 | +import GHC.Unit.Home.PackageTable (hptLoadedByteCode)
|
|
| 94 | 94 | import GHC.Unit.Module
|
| 95 | 95 | import GHC.Unit.Module.ModNodeKey
|
| 96 | 96 | import GHC.Unit.Module.Graph
|
| ... | ... | @@ -661,13 +661,18 @@ initLinkDepsOpts hsc_env = opts |
| 661 | 661 | Nothing -> findWholeCoreBindings hsc_env mod
|
| 662 | 662 | Just bco -> return (Just bco)
|
| 663 | 663 | |
| 664 | +-- | Compile the Core bindings stored in the interface of a home module to
|
|
| 665 | +-- bytecode, if the interface has any. Only used in one-shot mode, where home
|
|
| 666 | +-- modules are loaded on demand into the 'HomePackageTable'.
|
|
| 667 | +--
|
|
| 668 | +-- See Note [Interface Files with Core Definitions]
|
|
| 664 | 669 | findWholeCoreBindings :: HscEnv -> Module -> IO (Maybe Linkable)
|
| 665 | 670 | findWholeCoreBindings hsc_env mod = do
|
| 666 | 671 | _ <- initIfaceLoad hsc_env $
|
| 667 | 672 | loadInterface (text "get_reachable_nodes" <+> parens (ppr mod))
|
| 668 | 673 | mod ImportBySystem
|
| 669 | - EPS {eps_iface_bytecode} <- hscEPS hsc_env
|
|
| 670 | - sequence (lookupModuleEnv eps_iface_bytecode mod)
|
|
| 674 | + mb_action <- hptLoadedByteCode (hsc_HPT hsc_env) mod
|
|
| 675 | + sequence mb_action
|
|
| 671 | 676 | |
| 672 | 677 | |
| 673 | 678 | findBytecodeLinkableMaybe :: HscEnv -> ModLocation -> IO (Maybe Linkable)
|
| ... | ... | @@ -36,8 +36,6 @@ import GHC.Types.Unique.DSet |
| 36 | 36 | |
| 37 | 37 | import GHC.Utils.Fingerprint ( Fingerprint )
|
| 38 | 38 | |
| 39 | -import GHC.Linker.Types (Linkable)
|
|
| 40 | - |
|
| 41 | 39 | import Data.IORef
|
| 42 | 40 | |
| 43 | 41 | |
| ... | ... | @@ -74,7 +72,6 @@ initExternalPackageState = EPS |
| 74 | 72 | { eps_PIT = emptyPackageIfaceTable
|
| 75 | 73 | , eps_free_holes = emptyInstalledModuleEnv
|
| 76 | 74 | , eps_PTE = emptyTypeEnv
|
| 77 | - , eps_iface_bytecode = emptyModuleEnv
|
|
| 78 | 75 | , eps_inst_env = emptyInstEnv
|
| 79 | 76 | , eps_fam_inst_env = emptyFamInstEnv
|
| 80 | 77 | , eps_rule_base = mkRuleBase builtinRules
|
| ... | ... | @@ -135,12 +132,6 @@ data ExternalPackageState |
| 135 | 132 | -- interface files we have sucked in. The domain of
|
| 136 | 133 | -- the mapping is external-package modules
|
| 137 | 134 | |
| 138 | - -- | If an interface was written with @-fwrite-if-simplified-core@, this
|
|
| 139 | - -- will contain an IO action that compiles bytecode from core bindings.
|
|
| 140 | - --
|
|
| 141 | - -- See Note [Interface Files with Core Definitions]
|
|
| 142 | - eps_iface_bytecode :: !(ModuleEnv (IO Linkable)),
|
|
| 143 | - |
|
| 144 | 135 | eps_known_keys :: ModuleEnv (Fingerprint, KnownKeyNameMaps),
|
| 145 | 136 | -- ^ Cache of the known-entity maps of all loaded 'GHC.Essentials'
|
| 146 | 137 | -- modules, keyed by module and the corresponding interface ABI hash.
|
| ... | ... | @@ -62,6 +62,7 @@ module GHC.Unit.Home.PackageTable |
| 62 | 62 | , hptLoadedAnnEnv
|
| 63 | 63 | , hptLoadedCompleteMatches
|
| 64 | 64 | , hptLoadedDefaults
|
| 65 | + , hptLoadedByteCode
|
|
| 65 | 66 | |
| 66 | 67 | -- ** More Traversal-based queries
|
| 67 | 68 | , hptCollectDependencies
|
| ... | ... | @@ -101,6 +102,7 @@ import GHC.Core.FamInstEnv |
| 101 | 102 | import GHC.Core.InstEnv
|
| 102 | 103 | import GHC.Core.Rules (RuleBase, extendRuleBaseList, emptyRuleBase)
|
| 103 | 104 | import GHC.Data.Word64Set qualified as W64
|
| 105 | +import GHC.Linker.Types (Linkable)
|
|
| 104 | 106 | import GHC.Types.Annotations
|
| 105 | 107 | import GHC.Types.CompleteMatch
|
| 106 | 108 | import GHC.Types.TypeEnv
|
| ... | ... | @@ -154,7 +156,10 @@ data HomeLoaderState = HomeLoaderState |
| 154 | 156 | , hls_modules :: ModuleEnv ()
|
| 155 | 157 | , hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot
|
| 156 | 158 | , hls_type_env :: !TypeEnv
|
| 157 | - -- , hls_iface_bytecode :: !(ModuleEnv (IO Linkable))
|
|
| 159 | + , hls_iface_bytecode :: !(ModuleEnv (IO Linkable))
|
|
| 160 | + -- ^ Actions that compile the Core bindings stored in a loaded interface
|
|
| 161 | + -- to bytecode, for modules whose interface has Core bindings.
|
|
| 162 | + -- See Note [Interface Files with Core Definitions]
|
|
| 158 | 163 | , hls_inst_env :: !InstEnv
|
| 159 | 164 | , hls_fam_inst_env :: !FamInstEnv
|
| 160 | 165 | , hls_mod_fam_inst_env :: !(ModuleEnv FamInstEnv)
|
| ... | ... | @@ -171,7 +176,7 @@ emptyHomeLoaderState = |
| 171 | 176 | , hls_modules = emptyModuleEnv
|
| 172 | 177 | , hls_is_boot = emptyInstalledModuleEnv
|
| 173 | 178 | , hls_type_env = emptyTypeEnv
|
| 174 | - -- , hls_iface_bytecode = emptyModuleEnv
|
|
| 179 | + , hls_iface_bytecode = emptyModuleEnv
|
|
| 175 | 180 | , hls_inst_env = emptyInstEnv
|
| 176 | 181 | , hls_fam_inst_env = emptyFamInstEnv
|
| 177 | 182 | , hls_mod_fam_inst_env = emptyModuleEnv
|
| ... | ... | @@ -234,6 +239,15 @@ hptLoadedCompleteMatches = queryOnDemandHpt [] hls_complete_matches |
| 234 | 239 | hptLoadedDefaults :: HomePackageTable -> IO (ModuleEnv DefaultEnv)
|
| 235 | 240 | hptLoadedDefaults = queryOnDemandHpt emptyModuleEnv hls_defaults
|
| 236 | 241 | |
| 242 | +-- | The action that compiles the Core bindings of an on-demand loaded home
|
|
| 243 | +-- module to bytecode, if its interface contains Core bindings.
|
|
| 244 | +-- Always 'Nothing' for a 'HPT_Make' table.
|
|
| 245 | +--
|
|
| 246 | +-- See Note [Interface Files with Core Definitions]
|
|
| 247 | +hptLoadedByteCode :: HomePackageTable -> Module -> IO (Maybe (IO Linkable))
|
|
| 248 | +hptLoadedByteCode hpt mod =
|
|
| 249 | + queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) mod) hpt
|
|
| 250 | + |
|
| 237 | 251 | listHptIfaces :: HomePackageTable -> IO (DModuleNameEnv HomeModInfo)
|
| 238 | 252 | listHptIfaces = \ case
|
| 239 | 253 | HPT_Make{table} -> readIORef table
|
| ... | ... | @@ -289,9 +303,9 @@ restrictHpt HPT_OneShot{} _ = pure () |
| 289 | 303 | addListToHpt :: HomePackageTable -> [(ModuleName, HomeModInfo)] -> IO ()
|
| 290 | 304 | addListToHpt hpt = mapM_ (uncurry (addToHpt hpt))
|
| 291 | 305 | |
| 292 | -addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IO ()
|
|
| 293 | -addOnDemandHomeModule HPT_Make{} _ _ _ = pure ()
|
|
| 294 | -addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do
|
|
| 306 | +addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> Maybe (IO Linkable) -> IO ()
|
|
| 307 | +addOnDemandHomeModule HPT_Make{} _ _ _ _ = pure ()
|
|
| 308 | +addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode = do
|
|
| 295 | 309 | atomicModifyIORef loader $ \hls ->
|
| 296 | 310 | ( if mn `elemModuleEnv` hls_modules hls
|
| 297 | 311 | then hls -- If already loaded, don't add twice
|
| ... | ... | @@ -309,6 +323,11 @@ addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new |
| 309 | 323 | addHomeModData hls = hls {
|
| 310 | 324 | hls_modules = extendModuleEnv (hls_modules hls) mn (),
|
| 311 | 325 | hls_type_env = extendNameEnvList (hls_type_env hls) new_decls,
|
| 326 | + -- Don't add an entry if the iface doesn't have Core bindings, so that
|
|
| 327 | + -- 'get_link_deps' knows that it should load object code.
|
|
| 328 | + hls_iface_bytecode = case mb_bytecode of
|
|
| 329 | + Nothing -> hls_iface_bytecode hls
|
|
| 330 | + Just action -> extendModuleEnv (hls_iface_bytecode hls) mn action,
|
|
| 312 | 331 | hls_rule_base = extendRuleBaseList (hls_rule_base hls)
|
| 313 | 332 | new_rules,
|
| 314 | 333 | hls_complete_matches
|
| ... | ... | @@ -84,21 +84,18 @@ settings. |
| 84 | 84 | 2. In oneshot mode, which compiles individual modules without a shared home unit
|
| 85 | 85 | graph, a previously compiled module is not reprocessed as described for make
|
| 86 | 86 | mode above.
|
| 87 | + Instead, home modules are loaded on demand by 'loadInterface' into the
|
|
| 88 | + one-shot 'HomePackageTable' (its 'HomeLoaderState').
|
|
| 87 | 89 | When 'get_link_deps' encounters a dependency on a local module, it requests
|
| 88 | - its bytecode from the External Package State, who loads the interface
|
|
| 89 | - on-demand.
|
|
| 90 | + its bytecode from there via 'findWholeCoreBindings', which loads the
|
|
| 91 | + interface on-demand.
|
|
| 90 | 92 | |
| 91 | - Since the EPS stores interfaces for all package dependencies in addition to
|
|
| 92 | - local modules in oneshot mode, it has a substantial memory footprint.
|
|
| 93 | - We try to curtail that by extracting important data into specialized fields
|
|
| 94 | - in the EPS, and retaining only a few fields of 'ModIface' by overwriting the
|
|
| 95 | - others with bottom values.
|
|
| 96 | - |
|
| 97 | - In order to avoid keeping around all of the interface's components needed for
|
|
| 98 | - compiling bytecode, we instead store an IO action in 'eps_iface_bytecode'.
|
|
| 93 | + When a home interface with Core bindings is loaded, we store an IO action
|
|
| 94 | + that compiles them to bytecode in 'hls_iface_bytecode', rather than
|
|
| 95 | + compiling eagerly.
|
|
| 99 | 96 | When 'get_link_deps' evaluates this action, the result is not retained in the
|
| 100 | - EPS, but stored in 'LoaderState', where it may eventually get evicted to free
|
|
| 101 | - up the memory.
|
|
| 97 | + home package table, but stored in 'LoaderState', where it may eventually get
|
|
| 98 | + evicted to free up the memory.
|
|
| 102 | 99 | This IO action retains the dehydrated Core bindings from the interface in its
|
| 103 | 100 | closure.
|
| 104 | 101 | Like the bytecode 'Linkable' stored in 'LoaderState', this is preferable to
|