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 Add lazy bytecode loading back - - - - - 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: ===================================== compiler/GHC/Iface/Load.hs ===================================== @@ -835,7 +835,7 @@ loadInterface doc_str mod from massertPpr (moduleUnitId mod `Set.member` hsc_all_home_unit_ids hsc_env) (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)) tcParts <- tcPartsOfTheIface mod iface - addModIfacePartsToHpt gbl_env mod iface tcParts + addModIfacePartsToHpt gbl_env mod loc iface tcParts else dontLeakTheHUG $ do -- Load the new ModIface into the External Package State -- @@ -857,7 +857,7 @@ loadInterface doc_str mod from (moduleUnitId mod `Set.notMember` hsc_all_home_unit_ids hsc_env) (text "Attempting to load home package interface into the EPS" $$ ppr (HUG.allUnits $ hsc_HUG hsc_env) $$ doc_str $$ ppr mod $$ ppr (moduleUnitId mod)) tcParts <- tcPartsOfTheIface mod iface - addModIfacePartsToEps mod loc iface tcParts + addModIfacePartsToEps mod iface tcParts ; -- invoke plugins with *full* interface, not final_iface, to ensure -- that plugins have access to declarations, etc. @@ -885,8 +885,12 @@ tcPartsOfTheIface mod iface = do , new_complete_matches ) -addModIfacePartsToHpt :: IfGblEnv -> Module -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () -addModIfacePartsToHpt gbl_env mod iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do +addModIfacePartsToHpt :: IfGblEnv -> Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () +addModIfacePartsToHpt gbl_env mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do + -- NB: this is the full 'HscEnv', not one purged by 'dontLeakTheHUG': + -- compiling the Core bindings to bytecode typechecks them, which may need + -- to look up other home modules, and those live in the HPT. + hsc_env <- getTopEnv let details = ModDetails { md_types = mkNameEnv new_decls @@ -905,14 +909,20 @@ addModIfacePartsToHpt gbl_env mod iface (new_decls, new_insts, new_defaults, new -- of one's own boot file! (one-shot only) -- See Note [Loading your own hi-boot file] + -- Create an IO action that loads and compiles bytecode from Core + -- bindings, if the interface has any. + -- + -- See Note [Interface Files with Core Definitions] + mb_bytecode = loadIfaceByteCode hsc_env iface loc (md_types details) + warnPprTrace bad_boot "loadInterface" (ppr mod) $ do hpt <- getHpt when (not bad_boot) $ do liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt - liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) + liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode -addModIfacePartsToEps :: Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () -addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do +addModIfacePartsToEps :: Module -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () +addModIfacePartsToEps mod iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do purged_hsc_env <- getTopEnv let @@ -924,17 +934,6 @@ addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam & set_mi_rules (panic "No mi_rules in PIT") & set_mi_anns (panic "No mi_anns in PIT") & set_mi_simplified_core (panic "No mi_simplified_core in PIT") - -- Create an IO action that loads and compiles bytecode from Core - -- bindings. - -- - -- See Note [Interface Files with Core Definitions] - add_bytecode old - | Just action <- loadIfaceByteCode purged_hsc_env iface loc (mkNameEnv new_decls) - = extendModuleEnv old mod action - -- Don't add an entry if the iface doesn't have 'extra_decls' - -- so 'get_link_deps' knows that it should load object code. - | otherwise - = old mhome_unit = ue_homeUnit (hsc_unit_env purged_hsc_env) updateEps_ $ \ eps -> @@ -944,7 +943,6 @@ addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam eps { eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface, eps_PTE = addDeclsToPTE (eps_PTE eps) new_decls, - eps_iface_bytecode = add_bytecode (eps_iface_bytecode eps), eps_rule_base = extendRuleBaseList (eps_rule_base eps) new_rules, eps_complete_matches ===================================== compiler/GHC/Linker/Loader.hs ===================================== @@ -90,7 +90,7 @@ import GHC.Utils.TmpFs import GHC.Unit.Env import GHC.Unit.Home.ModInfo -import GHC.Unit.External (ExternalPackageState (..)) +import GHC.Unit.Home.PackageTable (hptLoadedByteCode) import GHC.Unit.Module import GHC.Unit.Module.ModNodeKey import GHC.Unit.Module.Graph @@ -661,13 +661,18 @@ initLinkDepsOpts hsc_env = opts Nothing -> findWholeCoreBindings hsc_env mod Just bco -> return (Just bco) +-- | Compile the Core bindings stored in the interface of a home module to +-- bytecode, if the interface has any. Only used in one-shot mode, where home +-- modules are loaded on demand into the 'HomePackageTable'. +-- +-- See Note [Interface Files with Core Definitions] findWholeCoreBindings :: HscEnv -> Module -> IO (Maybe Linkable) findWholeCoreBindings hsc_env mod = do _ <- initIfaceLoad hsc_env $ loadInterface (text "get_reachable_nodes" <+> parens (ppr mod)) mod ImportBySystem - EPS {eps_iface_bytecode} <- hscEPS hsc_env - sequence (lookupModuleEnv eps_iface_bytecode mod) + mb_action <- hptLoadedByteCode (hsc_HPT hsc_env) mod + sequence mb_action findBytecodeLinkableMaybe :: HscEnv -> ModLocation -> IO (Maybe Linkable) ===================================== compiler/GHC/Unit/External.hs ===================================== @@ -36,8 +36,6 @@ import GHC.Types.Unique.DSet import GHC.Utils.Fingerprint ( Fingerprint ) -import GHC.Linker.Types (Linkable) - import Data.IORef @@ -74,7 +72,6 @@ initExternalPackageState = EPS { eps_PIT = emptyPackageIfaceTable , eps_free_holes = emptyInstalledModuleEnv , eps_PTE = emptyTypeEnv - , eps_iface_bytecode = emptyModuleEnv , eps_inst_env = emptyInstEnv , eps_fam_inst_env = emptyFamInstEnv , eps_rule_base = mkRuleBase builtinRules @@ -135,12 +132,6 @@ data ExternalPackageState -- interface files we have sucked in. The domain of -- the mapping is external-package modules - -- | If an interface was written with @-fwrite-if-simplified-core@, this - -- will contain an IO action that compiles bytecode from core bindings. - -- - -- See Note [Interface Files with Core Definitions] - eps_iface_bytecode :: !(ModuleEnv (IO Linkable)), - eps_known_keys :: ModuleEnv (Fingerprint, KnownKeyNameMaps), -- ^ Cache of the known-entity maps of all loaded 'GHC.Essentials' -- modules, keyed by module and the corresponding interface ABI hash. ===================================== compiler/GHC/Unit/Home/PackageTable.hs ===================================== @@ -62,6 +62,7 @@ module GHC.Unit.Home.PackageTable , hptLoadedAnnEnv , hptLoadedCompleteMatches , hptLoadedDefaults + , hptLoadedByteCode -- ** More Traversal-based queries , hptCollectDependencies @@ -101,6 +102,7 @@ import GHC.Core.FamInstEnv import GHC.Core.InstEnv import GHC.Core.Rules (RuleBase, extendRuleBaseList, emptyRuleBase) import GHC.Data.Word64Set qualified as W64 +import GHC.Linker.Types (Linkable) import GHC.Types.Annotations import GHC.Types.CompleteMatch import GHC.Types.TypeEnv @@ -154,7 +156,10 @@ data HomeLoaderState = HomeLoaderState , hls_modules :: ModuleEnv () , hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot , hls_type_env :: !TypeEnv - -- , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) + , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) + -- ^ Actions that compile the Core bindings stored in a loaded interface + -- to bytecode, for modules whose interface has Core bindings. + -- See Note [Interface Files with Core Definitions] , hls_inst_env :: !InstEnv , hls_fam_inst_env :: !FamInstEnv , hls_mod_fam_inst_env :: !(ModuleEnv FamInstEnv) @@ -171,7 +176,7 @@ emptyHomeLoaderState = , hls_modules = emptyModuleEnv , hls_is_boot = emptyInstalledModuleEnv , hls_type_env = emptyTypeEnv - -- , hls_iface_bytecode = emptyModuleEnv + , hls_iface_bytecode = emptyModuleEnv , hls_inst_env = emptyInstEnv , hls_fam_inst_env = emptyFamInstEnv , hls_mod_fam_inst_env = emptyModuleEnv @@ -234,6 +239,15 @@ hptLoadedCompleteMatches = queryOnDemandHpt [] hls_complete_matches hptLoadedDefaults :: HomePackageTable -> IO (ModuleEnv DefaultEnv) hptLoadedDefaults = queryOnDemandHpt emptyModuleEnv hls_defaults +-- | The action that compiles the Core bindings of an on-demand loaded home +-- module to bytecode, if its interface contains Core bindings. +-- Always 'Nothing' for a 'HPT_Make' table. +-- +-- See Note [Interface Files with Core Definitions] +hptLoadedByteCode :: HomePackageTable -> Module -> IO (Maybe (IO Linkable)) +hptLoadedByteCode hpt mod = + queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) mod) hpt + listHptIfaces :: HomePackageTable -> IO (DModuleNameEnv HomeModInfo) listHptIfaces = \ case HPT_Make{table} -> readIORef table @@ -289,9 +303,9 @@ restrictHpt HPT_OneShot{} _ = pure () addListToHpt :: HomePackageTable -> [(ModuleName, HomeModInfo)] -> IO () addListToHpt hpt = mapM_ (uncurry (addToHpt hpt)) -addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IO () -addOnDemandHomeModule HPT_Make{} _ _ _ = pure () -addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do +addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> Maybe (IO Linkable) -> IO () +addOnDemandHomeModule HPT_Make{} _ _ _ _ = pure () +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 atomicModifyIORef loader $ \hls -> ( if mn `elemModuleEnv` hls_modules hls 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 addHomeModData hls = hls { hls_modules = extendModuleEnv (hls_modules hls) mn (), hls_type_env = extendNameEnvList (hls_type_env hls) new_decls, + -- Don't add an entry if the iface doesn't have Core bindings, so that + -- 'get_link_deps' knows that it should load object code. + hls_iface_bytecode = case mb_bytecode of + Nothing -> hls_iface_bytecode hls + Just action -> extendModuleEnv (hls_iface_bytecode hls) mn action, hls_rule_base = extendRuleBaseList (hls_rule_base hls) new_rules, hls_complete_matches ===================================== compiler/GHC/Unit/Module/WholeCoreBindings.hs ===================================== @@ -84,21 +84,18 @@ settings. 2. In oneshot mode, which compiles individual modules without a shared home unit graph, a previously compiled module is not reprocessed as described for make mode above. + Instead, home modules are loaded on demand by 'loadInterface' into the + one-shot 'HomePackageTable' (its 'HomeLoaderState'). When 'get_link_deps' encounters a dependency on a local module, it requests - its bytecode from the External Package State, who loads the interface - on-demand. + its bytecode from there via 'findWholeCoreBindings', which loads the + interface on-demand. - Since the EPS stores interfaces for all package dependencies in addition to - local modules in oneshot mode, it has a substantial memory footprint. - We try to curtail that by extracting important data into specialized fields - in the EPS, and retaining only a few fields of 'ModIface' by overwriting the - others with bottom values. - - In order to avoid keeping around all of the interface's components needed for - compiling bytecode, we instead store an IO action in 'eps_iface_bytecode'. + When a home interface with Core bindings is loaded, we store an IO action + that compiles them to bytecode in 'hls_iface_bytecode', rather than + compiling eagerly. When 'get_link_deps' evaluates this action, the result is not retained in the - EPS, but stored in 'LoaderState', where it may eventually get evicted to free - up the memory. + home package table, but stored in 'LoaderState', where it may eventually get + evicted to free up the memory. This IO action retains the dehydrated Core bindings from the interface in its closure. Like the bytecode 'Linkable' stored in 'LoaderState', this is preferable to View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ce01df3c694ce6addc428d208158c1ba... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/ce01df3c694ce6addc428d208158c1ba... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help