[Git][ghc/ghc][wip/fendor/one-shot-no-eps] Experiment with separate table types for inOneShot mode
Hannes Siebenhandl pushed to branch wip/fendor/one-shot-no-eps at Glasgow Haskell Compiler / GHC Commits: c00e2758 by Hannes Siebenhandl at 2026-11-30T12:42:34+01:00 Experiment with separate table types for inOneShot mode - - - - - 9 changed files: - compiler/GHC/Core/Opt/Pipeline.hs - compiler/GHC/Driver/Env.hs - compiler/GHC/Driver/Main/Hsc.hs - compiler/GHC/Iface/Load.hs - compiler/GHC/Iface/Recomp.hs - compiler/GHC/IfaceToCore.hs - compiler/GHC/Tc/Module.hs - compiler/GHC/Unit/Home/PackageTable.hs - ghc/GHCi/Leak.hs Changes: ===================================== compiler/GHC/Core/Opt/Pipeline.hs ===================================== @@ -102,7 +102,7 @@ core2core hsc_env guts@(ModGuts { mg_module = mod logger = hsc_logger hsc_env unit_env = hsc_unit_env hsc_env extra_vars = interactiveInScope (hsc_IC hsc_env) - home_pkg_rules = hugRulesBelow hsc_env (moduleUnitId mod) + home_pkg_rules = hugRulesBelow (hsc_mod_graph hsc_env) (hsc_HUG hsc_env) (moduleUnitId mod) (GWIB { gwib_mod = moduleName mod, gwib_isBoot = NotBoot }) name_ppr_ctx = mkNamePprCtx ptc unit_env rdr_env ptc = initPromotionTickContext dflags ===================================== compiler/GHC/Driver/Env.hs ===================================== @@ -99,6 +99,7 @@ import Data.IORef import qualified Data.Set as Set import GHC.Types.Name.Reader (ExactRdrName(..)) import qualified GHC.Unit.Home.PackageTable as HomePackageTable +import qualified GHC.Unit.Home.PackageTable as HPT runHsc :: HscEnv -> Hsc a -> IO a runHsc hsc_env hsc = do @@ -259,26 +260,26 @@ hscUIC hsc_env = ue_uic (hsc_unit_env hsc_env) -- | Find all rules in modules that are in the transitive closure of the given -- module. -hugRulesBelow :: HscEnv -> UnitId -> ModuleNameWithIsBoot -> IO RuleBase -hugRulesBelow hsc_env uid mn = foldr (flip extendRuleBaseList) emptyRuleBase <$> - hugSomeThingsBelowUs (md_rules . hm_details) False hsc_env uid mn +hugRulesBelow :: ModuleGraph -> HomeUnitGraph -> UnitId -> ModuleNameWithIsBoot -> IO RuleBase +hugRulesBelow mg hug uid mn = foldr (flip extendRuleBaseList) emptyRuleBase <$> + hugSomeThingsBelowUs (md_rules . hm_details) False mg hug uid mn -- | Get annotations from all modules "below" this one (in the dependency -- sense) within the home units. If the module is @Nothing@, returns /all/ -- annotations in the home units. -hugAnnsBelow :: HscEnv -> UnitId -> ModuleNameWithIsBoot -> IO AnnEnv -hugAnnsBelow hsc_env uid mn = foldr (flip extendAnnEnvList) emptyAnnEnv <$> - hugSomeThingsBelowUs (md_anns . hm_details) False hsc_env uid mn +hugAnnsBelow :: ModuleGraph -> HomeUnitGraph -> UnitId -> ModuleNameWithIsBoot -> IO AnnEnv +hugAnnsBelow mg hug uid mn = foldr (flip extendAnnEnvList) emptyAnnEnv <$> + hugSomeThingsBelowUs (md_anns . hm_details) False mg hug uid mn -- | Find all COMPLETE pragmas in modules that are in the transitive closure of the -- given module. -hugCompleteSigsBelow :: HscEnv -> UnitId -> ModuleNameWithIsBoot -> IO CompleteMatches -hugCompleteSigsBelow hsc uid mn = foldr (++) [] <$> - hugSomeThingsBelowUs (md_complete_matches . hm_details) False hsc uid mn +hugCompleteSigsBelow :: ModuleGraph -> HomeUnitGraph -> UnitId -> ModuleNameWithIsBoot -> IO CompleteMatches +hugCompleteSigsBelow mg hug uid mn = foldr (++) [] <$> + hugSomeThingsBelowUs (md_complete_matches . hm_details) False mg hug uid mn -- | Find instances visible from the given set of imports -hugInstancesBelow :: HscEnv -> UnitId -> ModuleNameWithIsBoot -> IO (InstEnv, [FamInst]) -hugInstancesBelow hsc_env uid mnwib = do +hugInstancesBelow :: ModuleGraph -> HomeUnitGraph -> UnitId -> ModuleNameWithIsBoot -> IO (InstEnv, [FamInst]) +hugInstancesBelow mg hug uid mnwib = do let mn = mkModule uid (gwib_mod mnwib) (insts, famInsts) <- unzip . concat <$> @@ -289,7 +290,8 @@ hugInstancesBelow hsc_env uid mnwib = do then [] else [(md_insts details, md_fam_insts details)]) True -- Include -hi-boot - hsc_env + mg + hug uid mnwib return (foldl' unionInstEnv emptyInstEnv insts, concat famInsts) @@ -297,17 +299,18 @@ hugInstancesBelow hsc_env uid mnwib = do -- | Get things from modules in the transitive closure of the given module. -- -- Note: Don't expose this function. This is a footgun if exposed! -hugSomeThingsBelowUs :: (HomeModInfo -> [a]) -> Bool -> HscEnv -> UnitId -> ModuleNameWithIsBoot -> IO [[a]] +hugSomeThingsBelowUs :: (HomeModInfo -> [a]) -> Bool -> ModuleGraph -> HomeUnitGraph -> UnitId -> ModuleNameWithIsBoot -> IO [[a]] -- An explicit check to see if we are in one-shot mode to avoid poking the ModuleGraph thunk -- These things are currently stored in the EPS for home packages. (See #25795 for -- progress in removing these kind of checks; and making these functions of -- `UnitEnv` rather than `HscEnv`) -- See Note [The ModuleGraph] -hugSomeThingsBelowUs _ _ hsc_env _ _ | isOneShot (ghcMode (hsc_dflags hsc_env)) = return [] -hugSomeThingsBelowUs extract include_hi_boot hsc_env uid mn - = let hug = hsc_HUG hsc_env - mg = hsc_mod_graph hsc_env - in +hugSomeThingsBelowUs extract include_hi_boot mg hug uid mn = + -- If the first hpt is a one-shot table, we don't recurse into it. + -- TODO @fendor: I guess that's nonsense to a certain degree + if HPT.isOneShotMode $ homeUnitEnv_hpt $ unitEnv_lookup uid hug + then pure [] + else sequence [ things -- "Finding each non-hi-boot module below me" maybe could be cached (well, @@ -347,7 +350,7 @@ prepareAnnotations hsc_env mb_guts = do -- entries regardless of dependency ordering. get_mod mg = (moduleUnitId (mg_module mg), GWIB (moduleName (mg_module mg)) NotBoot) home_pkg_anns <- fromMaybe (hugAllAnns (hsc_unit_env hsc_env)) - $ uncurry (hugAnnsBelow hsc_env) + $ uncurry (hugAnnsBelow (hsc_mod_graph hsc_env) (hsc_HUG hsc_env)) . get_mod <$> mb_guts more_home_pkg_anns <- HomePackageTable.hptLoadedAnnEnv $ hsc_HPT hsc_env let ===================================== compiler/GHC/Driver/Main/Hsc.hs ===================================== @@ -107,7 +107,10 @@ import System.IO newHscEnv :: FilePath -> DynFlags -> IO HscEnv newHscEnv top_dir dflags = do - hpt <- emptyHomePackageTable + hpt <- case ghcMode dflags of + OneShot -> emptyOnDemandHomePackageTable + CompManager -> emptyHomePackageTable + MkDepend -> emptyHomePackageTable newHscEnvWithHUG top_dir dflags (homeUnitId_ dflags) (home_unit_graph hpt) where home_unit_graph hpt = HUG.unitEnv_singleton ===================================== compiler/GHC/Iface/Load.hs ===================================== @@ -100,66 +100,65 @@ import GHC.Builtin.PrimOps.Ids import GHC.Builtin.WiredIn.Prim import GHC.Builtin.WiredIn.Ids( seqId ) +import GHC.Core (CoreRule) +import GHC.Core.FamInstEnv +import GHC.Core.InstEnv import GHC.Core.Rules import GHC.Core.TyCon -import GHC.Core.InstEnv -import GHC.Core.FamInstEnv -import GHC.Parser.Annotation( noLocA, noAnn ) +import GHC.Parser.Annotation (noAnn, noLocA) import GHC.Types.Annotations +import GHC.Types.Avail +import GHC.Types.DefaultEnv (DefaultEnv) +import GHC.Types.Fixity +import GHC.Types.Fixity.Env import GHC.Types.Id import GHC.Types.Name import GHC.Types.Name.Cache import GHC.Types.Name.Env import GHC.Types.Name.Reader -import GHC.Types.Avail -import GHC.Types.Fixity -import GHC.Types.Fixity.Env +import GHC.Types.PkgQual +import GHC.Types.SafeHaskell import GHC.Types.SourceError import GHC.Types.SourceFile -import GHC.Types.SafeHaskell -import GHC.Types.TypeEnv -import GHC.Types.Unique.DSet -import GHC.Types.Unique.Map( listToUniqMap ) -import GHC.Types.Unique.FM( UniqFM, listToUFM, lookupUFM, elemUFM ) +import GHC.Types.SourceText import GHC.Types.SrcLoc import GHC.Types.TyThing -import GHC.Types.PkgQual -import GHC.Types.SourceText +import GHC.Types.TypeEnv +import GHC.Types.Unique.DSet +import GHC.Types.Unique.FM (UniqFM, elemUFM, listToUFM, lookupUFM) +import GHC.Types.Unique.Map (listToUniqMap) +import GHC.Unit.Env import GHC.Unit.External +import GHC.Unit.Finder +import GHC.Unit.Home +import GHC.Unit.Home.Graph qualified as HUG +import GHC.Unit.Home.ModInfo (HomeModInfo (..), emptyHomeModInfoLinkable) +import GHC.Unit.Home.PackageTable +import GHC.Unit.Home.PackageTable qualified as HomePackageTable import GHC.Unit.Module +import GHC.Unit.Module.Deps import GHC.Unit.Module.Graph -import GHC.Unit.Module.Warnings +import GHC.Unit.Module.ModDetails (ModDetails (..)) import GHC.Unit.Module.ModIface -import GHC.Unit.Module.Deps +import GHC.Unit.Module.Warnings import GHC.Unit.State -import GHC.Unit.Home -import qualified GHC.Unit.Home.Graph as HUG -import GHC.Unit.Home.PackageTable -import GHC.Unit.Finder -import GHC.Unit.Env import GHC.Stack( callStack ) import GHC.Data.Maybe import Language.Haskell.Syntax.Lit (StringLiteral(..)) +import Control.Applicative ((<|>)) import Control.Monad -import qualified Data.Foldable as Foldable +import Data.Foldable qualified as Foldable import Data.Function ((&)) -import Data.Map ( toList ) -import qualified Data.Set as Set -import System.FilePath +import Data.Map (toList) +import Data.Set qualified as Set import System.Directory -import GHC.Types.DefaultEnv (DefaultEnv) -import GHC.Core (CoreRule) -import GHC.Unit.Module.ModDetails (ModDetails(..)) -import GHC.Unit.Home.ModInfo (HomeModInfo(..), emptyHomeModInfoLinkable) -import qualified Data.Set as Set -import qualified GHC.Unit.Home.PackageTable as HomePackageTable -import Control.Applicative ((<|>)) +import System.FilePath {- ********************************************************************* @@ -799,8 +798,8 @@ loadInterface doc_str mod from -- READ THE MODULE IN ; hpt <- getHpt - ; hls <- liftIO (hptLoaderState hpt) - ; read_result <- case wantHiBootFile mhome_unit hls mod from of + ; is_boot_map <- liftIO (hptLoadedIsBoot hpt) + ; read_result <- case wantHiBootFile mhome_unit is_boot_map mod from of Failed err -> return (Failed err) Succeeded hi_boot_file -> do hsc_env <- getTopEnv @@ -836,7 +835,7 @@ loadInterface doc_str mod from massertPpr (moduleUnitId mod `Set.member` hsc_all_home_unit_ids hsc_env) (text "Attempting to 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 loc iface tcParts + addModIfacePartsToHpt gbl_env mod iface tcParts else dontLeakTheHUG $ do -- Load the new ModIface into the External Package State -- @@ -868,37 +867,36 @@ loadInterface doc_str mod from tcPartsOfTheIface :: Module -> ModIface -> IfL ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], CompleteMatches) tcPartsOfTheIface mod iface = do - ignore_prags <- goptM Opt_IgnoreInterfacePragmas - new_eps_decls <- tcIfaceDecls ignore_prags (mi_decls iface) - new_eps_insts <- mapM tcIfaceInst (mi_insts iface) - new_eps_defaults <- tcIfaceDefaults mod (mi_defaults iface) - new_eps_fam_insts <- mapM tcIfaceFamInst (mi_fam_insts iface) - new_eps_rules <- tcIfaceRules ignore_prags (mi_rules iface) - new_eps_anns <- tcIfaceAnnotations (mi_anns iface) - new_eps_complete_matches <- tcIfaceCompleteMatches (mi_complete_matches iface) + ignore_prags <- goptM Opt_IgnoreInterfacePragmas + new_decls <- tcIfaceDecls ignore_prags (mi_decls iface) + new_insts <- mapM tcIfaceInst (mi_insts iface) + new_defaults <- tcIfaceDefaults mod (mi_defaults iface) + new_fam_insts <- mapM tcIfaceFamInst (mi_fam_insts iface) + new_rules <- tcIfaceRules ignore_prags (mi_rules iface) + new_anns <- tcIfaceAnnotations (mi_anns iface) + new_complete_matches <- tcIfaceCompleteMatches (mi_complete_matches iface) pure - ( new_eps_decls - , new_eps_insts - , new_eps_defaults - , new_eps_fam_insts - , new_eps_rules - , new_eps_anns - , new_eps_complete_matches + ( new_decls + , new_insts + , new_defaults + , new_fam_insts + , new_rules + , new_anns + , new_complete_matches ) -addModIfacePartsToHpt :: IfGblEnv -> Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () -addModIfacePartsToHpt gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_eps_defaults, new_eps_fam_insts, new_eps_rules, new_eps_anns, new_eps_complete_matches) = do - hsc_env <- getTopEnv +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 let details = ModDetails - { md_types = mkNameEnv new_eps_decls + { md_types = mkNameEnv new_decls , md_exports = mi_exports iface - , md_defaults = new_eps_defaults - , md_insts = mkInstEnv new_eps_insts - , md_fam_insts = new_eps_fam_insts - , md_rules = new_eps_rules - , md_anns = new_eps_anns - , md_complete_matches = new_eps_complete_matches + , md_defaults = new_defaults + , md_insts = mkInstEnv new_insts + , md_fam_insts = new_fam_insts + , md_rules = new_rules + , md_anns = new_anns + , md_complete_matches = new_complete_matches } bad_boot = mi_boot iface == IsBoot @@ -909,16 +907,11 @@ addModIfacePartsToHpt gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_e warnPprTrace bad_boot "loadInterface" (ppr mod) $ do hpt <- getHpt - -- Create an IO action that loads and compiles bytecode from Core - -- bindings. - -- - -- See Note [Interface Files with Core Definitions] - let lazy_bytecode = loadIfaceByteCode hsc_env iface loc (mkNameEnv new_eps_decls) liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt - liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_eps_decls, new_eps_insts, new_eps_defaults, new_eps_fam_insts, new_eps_rules, new_eps_anns, new_eps_complete_matches) lazy_bytecode + liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) addModIfacePartsToEps :: IfGblEnv -> Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () -addModIfacePartsToEps gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_eps_defaults, new_eps_fam_insts, new_eps_rules, new_eps_anns, new_eps_complete_matches) = do +addModIfacePartsToEps gbl_env mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do purged_hsc_env <- getTopEnv let @@ -942,7 +935,7 @@ addModIfacePartsToEps gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_e -- -- See Note [Interface Files with Core Definitions] add_bytecode old - | Just action <- loadIfaceByteCode purged_hsc_env iface loc (mkNameEnv new_eps_decls) + | 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. @@ -956,36 +949,36 @@ addModIfacePartsToEps gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_e then eps else if bad_boot -- See Note [Loading your own hi-boot file] - then eps { eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls } + then eps { eps_PTE = addDeclsToPTE (eps_PTE eps) new_decls } else eps { eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface, - eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls, + 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_eps_rules, + new_rules, eps_complete_matches - = eps_complete_matches eps ++ new_eps_complete_matches, + = eps_complete_matches eps ++ new_complete_matches, eps_inst_env = extendInstEnvList (eps_inst_env eps) - new_eps_insts, + new_insts, eps_fam_inst_env = extendFamInstEnvList (eps_fam_inst_env eps) - new_eps_fam_insts, + new_fam_insts, eps_ann_env = extendAnnEnvList (eps_ann_env eps) - new_eps_anns, + new_anns, eps_mod_fam_inst_env = let fam_inst_env = extendFamInstEnvList emptyFamInstEnv - new_eps_fam_insts + new_fam_insts in extendModuleEnv (eps_mod_fam_inst_env eps) mod fam_inst_env, eps_stats = addEpsInStats (eps_stats eps) - (length new_eps_decls) - (length new_eps_insts) - (length new_eps_rules), - eps_defaults = extendModuleEnv (eps_defaults eps) mod new_eps_defaults + (length new_decls) + (length new_insts) + (length new_rules), + eps_defaults = extendModuleEnv (eps_defaults eps) mod new_defaults } @@ -1204,10 +1197,10 @@ moduleFreeHolesPrecise doc_str mod return (Succeeded (renameFreeHoles ifhs insts)) Failed err -> return (Failed err) -wantHiBootFile :: Maybe HomeUnit -> HomeLoaderState -> Module -> WhereFrom +wantHiBootFile :: Maybe HomeUnit -> InstalledModuleEnv ModuleNameWithIsBoot -> Module -> WhereFrom -> MaybeErr MissingInterfaceError IsBootInterface -- Figure out whether we want Foo.hi or Foo.hi-boot -wantHiBootFile mhome_unit hls mod from +wantHiBootFile mhome_unit is_boot_map mod from = case from of ImportByUser usr_boot | usr_boot == IsBoot && notHomeModuleMaybe mhome_unit mod @@ -1226,9 +1219,9 @@ wantHiBootFile mhome_unit hls mod from -- We never import boot modules from other packages! | otherwise - -> case hlsIsBoot hls (toUnitId <$> mod) of - Just is_boot -> - Succeeded is_boot + -> case lookupInstalledModuleEnv is_boot_map (toUnitId <$> mod) of + Just mnib -> + Succeeded $ gwib_isBoot mnib Nothing -> Succeeded NotBoot -- The boot-ness of the requested interface, ===================================== compiler/GHC/Iface/Recomp.hs ===================================== @@ -427,10 +427,9 @@ checkVersions hsc_env mod_summary iface self_recomp -- (in which case we are done with this module) or it'll fail (in which -- case we'll compile the module from scratch anyhow). - when (isOneShot (ghcMode (hsc_dflags hsc_env))) $ do { - ; hpt <- getHpt - ; liftIO $ hlsSetBootInfo hpt (mkModDeps $ dep_boot_mods (mi_deps iface)) - } + ; hpt <- getHpt + ; liftIO $ hptSetBootInfo hpt (mkModDeps $ dep_boot_mods (mi_deps iface)) + ; recomp <- checkList [checkModUsage (hsc_FC hsc_env) u | u <- mi_sr_usages self_recomp] ; case recomp of (NeedsRecompile reason) -> return $ OutOfDateItem reason (Just iface) ; _ -> do { ===================================== compiler/GHC/IfaceToCore.hs ===================================== @@ -633,12 +633,12 @@ tcHiBootIface hsc_src mod -- a SOURCE import) or that our hi-boot file has mysteriously -- disappeared. do { hpt <- getHpt - ; hls <- liftIO $ hptLoaderState hpt - ; case hlsIsBoot hls (toUnitId <$> mod) of + ; is_boot_map <- liftIO $ hptLoadedIsBoot hpt + ; case lookupInstalledModuleEnv is_boot_map (toUnitId <$> mod) of -- The typical case Nothing -> return NoSelfBoot -- error cases - Just is_boot -> case is_boot of + Just mnib -> case gwib_isBoot mnib of IsBoot -> let diag = Can'tFindInterface err (LookingForHiBoot mod) ===================================== compiler/GHC/Tc/Module.hs ===================================== @@ -191,7 +191,7 @@ import qualified Data.Map as M import Data.Foldable ( for_ ) import Data.Traversable ( for ) import Data.IORef( newIORef ) -import GHC.Unit.Home.PackageTable (hlsSetBootInfo) +import GHC.Unit.Home.PackageTable (hptSetBootInfo) {- ************************************************************************ @@ -501,21 +501,19 @@ tcRnImports hsc_env import_decls -- modules batch (@--make@) compiled before this one, but -- which are not below this one. ; (home_insts, home_fam_insts) <- liftIO $ - hugInstancesBelow hsc_env unitId mnwib + hugInstancesBelow (hsc_mod_graph hsc_env) (hsc_HUG hsc_env) unitId mnwib -- We use 'unsafeInterleaveIO' to avoid redundant memory allocations -- See Note [Lazily loading COMPLETE pragmas] from GHC.HsToCore.Monad -- and see https://gitlab.haskell.org/ghc/ghc/-/merge_requests/14274#note_620545 ; completeSigsBelow <- liftIO $ unsafeInterleaveIO $ - hugCompleteSigsBelow hsc_env unitId mnwib + hugCompleteSigsBelow (hsc_mod_graph hsc_env) (hsc_HUG hsc_env) unitId mnwib - -- Record boot-file info in the EPS, so that it's + -- Record boot-file info in the HPT, so that it's -- visible to loadHiBootInterface in tcRnSrcDecls, -- and any other incrementally-performed imports - ; when (isOneShot (ghcMode (hsc_dflags hsc_env))) $ do { - ; hpt <- getHpt - ; liftIO $ hlsSetBootInfo hpt (imp_boot_mods imports) - } + ; hpt <- getHpt + ; liftIO $ hptSetBootInfo hpt (imp_boot_mods imports) -- Update the gbl env ; updGblEnv ( \ gbl -> ===================================== compiler/GHC/Unit/Home/PackageTable.hs ===================================== @@ -21,13 +21,17 @@ -- Or, do you really need a function to traverse all modules in the HPT? It is -- often better to keep the computation internal to this module, such as in -- 'hptCollectObjects'... +{-# OPTIONS_GHC -Wall -Wincomplete-record-selectors #-} module GHC.Unit.Home.PackageTable ( HomePackageTable(..) , emptyHomePackageTable + , emptyOnDemandHomePackageTable - , HomeLoaderState -- TODO: should this be opaque? Probably - , emptyHomeLoaderState + , isOneShotMode + + -- , HomeLoaderState -- TODO: should this be opaque? Probably + -- , emptyHomeLoaderState -- * Lookups in the HPT , lookupHpt @@ -42,13 +46,14 @@ module GHC.Unit.Home.PackageTable , restrictHpt -- * Queries about home modules + , listHptIfaces , hptCompleteSigs , hptAllInstances , hptAllFamInstances , hptAllAnnotations , hlsIsModuleNameWithBoot , hlsIsBoot - , hlsSetBootInfo + , hptSetBootInfo , hptLoadedIsBoot , hptLoadedTypeEnv , hptLoadedInstEnv @@ -78,9 +83,8 @@ module GHC.Unit.Home.PackageTable -- -- In GHC itself these should be avoided, and other uses should justify why -- it is not sufficient to go through the intended insert-only API. - , hptInternalTableRef + -- , hptInternalTableRef , hptInternalTableFromRef - , hptLoaderState -- * Legacy API -- -- | This API is deprecated and meant to be removed. @@ -98,7 +102,6 @@ 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 @@ -117,24 +120,14 @@ import GHC.Types.DefaultEnv (DefaultEnv) import GHC.Core -- | Helps us find information about modules in the home package -data HomePackageTable = HPT { +data HomePackageTable + = HPT_Make { table :: {-# UNPACK #-} !(IORef (DModuleNameEnv HomeModInfo)) } + | HPT_OneShot { loader :: {-# UNPACK #-} !(IORef HomeLoaderState) } - table :: IORef (DModuleNameEnv HomeModInfo), - -- ^ Domain = modules in this home unit - -- - -- This is an IORef because we want to avoid leaking HPTs (see the particularly bad #25511). - -- Moreover, the HPT invariant allows mutability in this table without compromising thread safety or soundness. - -- To recall: - -- A query to the HPT should depend only on data relevant to that query, such that - -- there being more or less unrelated entries in the HPT does not influence the result in any way. - -- - -- Note that the HPT increases monotonically, except at certain barrier - -- points like when 'restrictHpt' is called. At these barriers, it is safe - -- to temporarily violate the HPT monotonicity. - -- - -- The elements of this table may be updated (e.g. on rehydration). - loader :: IORef HomeLoaderState - } +isOneShotMode :: HomePackageTable -> Bool +isOneShotMode = \ case + HPT_OneShot{} -> True + HPT_Make{} -> False -- | Create a new 'HomePackageTable'. -- @@ -145,14 +138,19 @@ emptyHomePackageTable :: IO HomePackageTable -- romes:todo: use a MutableArray directly? emptyHomePackageTable = do table <- newIORef emptyUDFM + return HPT_Make{table} + +emptyOnDemandHomePackageTable :: IO HomePackageTable +emptyOnDemandHomePackageTable = do loader <- newIORef emptyHomeLoaderState - return HPT{table, loader} + return HPT_OneShot{loader} data HomeLoaderState = HomeLoaderState - { hls_modules :: ModuleEnv () + { hls_table :: DModuleNameEnv HomeModInfo + , hls_modules :: ModuleEnv () , hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot , hls_type_env :: !TypeEnv - , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) + -- , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) , hls_inst_env :: !InstEnv , hls_fam_inst_env :: !FamInstEnv , hls_mod_fam_inst_env :: !(ModuleEnv FamInstEnv) @@ -165,10 +163,11 @@ data HomeLoaderState = HomeLoaderState emptyHomeLoaderState :: HomeLoaderState emptyHomeLoaderState = HomeLoaderState - { hls_modules = emptyModuleEnv + { hls_table = emptyUDFM + , 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 @@ -184,46 +183,57 @@ emptyHomeLoaderState = -- | Lookup the 'HomeModInfo' of a module in the HPT, given its name. lookupHpt :: HomePackageTable -> ModuleName -> IO (Maybe HomeModInfo) -lookupHpt HPT{table=hpt} mn = (`lookupUDFM` mn) <$!> readIORef hpt +lookupHpt HPT_Make{table=hpt} mn = (`lookupUDFM` mn) <$!> readIORef hpt +lookupHpt HPT_OneShot{loader=hpt} mn = (`lookupUDFM` mn) . hls_table <$!> readIORef hpt -- | Lookup the 'HomeModInfo' of a 'Module' in the HPT. lookupHptByModule :: HomePackageTable -> Module -> IO (Maybe HomeModInfo) -lookupHptByModule hpt mod +lookupHptByModule hpt modl = -- The HPT is indexed by ModuleName, not Module, -- we must check for a hit on the right Module - lookupHpt hpt (moduleName mod) >>= pure . \case - Just hm | mi_module (hm_iface hm) == mod -> Just hm + lookupHpt hpt (moduleName modl) >>= pure . \case + Just hm | mi_module (hm_iface hm) == modl -> Just hm _otherwise -> Nothing -hptLoaderState :: HomePackageTable -> IO HomeLoaderState -hptLoaderState = readIORef . loader +queryOnDemandHpt :: a -> (HomeLoaderState -> a) -> HomePackageTable -> IO a +queryOnDemandHpt a f hpt = + case hpt of + HPT_Make{} -> + pure a + HPT_OneShot{loader} -> + f <$> readIORef loader hptLoadedIsBoot :: HomePackageTable -> IO (InstalledModuleEnv ModuleNameWithIsBoot) -hptLoadedIsBoot hpt = hls_is_boot <$> readIORef (loader hpt) +hptLoadedIsBoot = queryOnDemandHpt emptyInstalledModuleEnv hls_is_boot hptLoadedTypeEnv :: HomePackageTable -> IO TypeEnv -hptLoadedTypeEnv hpt = hls_type_env <$> readIORef (loader hpt) +hptLoadedTypeEnv = queryOnDemandHpt emptyTypeEnv hls_type_env hptLoadedInstEnv :: HomePackageTable -> IO InstEnv -hptLoadedInstEnv hpt = hls_inst_env <$> readIORef (loader hpt) +hptLoadedInstEnv = queryOnDemandHpt emptyInstEnv hls_inst_env hptLoadedFamInstEnv :: HomePackageTable -> IO FamInstEnv -hptLoadedFamInstEnv hpt = hls_fam_inst_env <$> readIORef (loader hpt) +hptLoadedFamInstEnv = queryOnDemandHpt emptyFamInstEnv hls_fam_inst_env hptLoadedModFamInstEnv :: HomePackageTable -> IO (ModuleEnv FamInstEnv) -hptLoadedModFamInstEnv hpt = hls_mod_fam_inst_env <$> readIORef (loader hpt) +hptLoadedModFamInstEnv = queryOnDemandHpt emptyModuleEnv hls_mod_fam_inst_env hptLoadedRuleBase :: HomePackageTable -> IO RuleBase -hptLoadedRuleBase hpt = hls_rule_base <$> readIORef (loader hpt) +hptLoadedRuleBase = queryOnDemandHpt emptyRuleBase hls_rule_base hptLoadedAnnEnv :: HomePackageTable -> IO AnnEnv -hptLoadedAnnEnv hpt = hls_ann_env <$> readIORef (loader hpt) +hptLoadedAnnEnv = queryOnDemandHpt emptyAnnEnv hls_ann_env hptLoadedCompleteMatches :: HomePackageTable -> IO CompleteMatches -hptLoadedCompleteMatches hpt = hls_complete_matches <$> readIORef (loader hpt) +hptLoadedCompleteMatches = queryOnDemandHpt [] hls_complete_matches hptLoadedDefaults :: HomePackageTable -> IO (ModuleEnv DefaultEnv) -hptLoadedDefaults hpt = hls_defaults <$> readIORef (loader hpt) +hptLoadedDefaults = queryOnDemandHpt emptyModuleEnv hls_defaults + +listHptIfaces :: HomePackageTable -> IO (DModuleNameEnv HomeModInfo) +listHptIfaces = \ case + HPT_Make{table} -> readIORef table + HPT_OneShot{loader} -> hls_table <$> readIORef loader -------------------------------------------------------------------------------- -- * Extending the HPT @@ -244,10 +254,12 @@ addHomeModInfoToHpt hmi hpt = addToHpt hpt (moduleName (mi_module (hm_iface hmi) {-# DEPRECATED addToHpt "Deprecated in favour of 'addHomeModInfoToHpt', as the module at which a 'HomeModInfo' is inserted should always be derived from the 'HomeModInfo' itself." #-} -- After deprecation cycle, move `addToHpt` to a `where` clause inside `addHomeModInfoToHpt`. addToHpt :: HomePackageTable -> ModuleName -> HomeModInfo -> IO () -addToHpt HPT{table=hptr} mn hmi = do +addToHpt HPT_Make{table=hptr} mn hmi = do atomicModifyIORef' hptr (\hpt -> (addToUDFM hpt mn hmi, ())) -- If the key already existed in the map, this insertion is overwriting -- the HMI of a previously loaded module (likely in rehydration). +addToHpt HPT_OneShot{loader=hptr} mn hmi = do + atomicModifyIORef' hptr (\hpt -> (hpt { hls_table = addToUDFM (hls_table hpt) mn hmi }, ())) -- | 'addHomeModInfoToHpt' for multiple module infos. addHomeModInfosToHpt :: HomePackageTable -> [HomeModInfo] -> IO () @@ -262,19 +274,21 @@ addHomeModInfosToHpt hpt = mapM_ (flip addHomeModInfoToHpt hpt) -- called at barrier points, such as the end of upsweep, when all threads are -- done and we want to clean up failed entries. restrictHpt :: HomePackageTable -> [HomeModInfo] -> IO () -restrictHpt HPT{table=hptr} hmis = +restrictHpt HPT_Make{table=hptr} hmis = let key_set = map (getKey . getUnique . hmi_mod) hmis hmi_mod hmi = moduleName (mi_module (hm_iface hmi)) in atomicModifyIORef' hptr (\hpt -> (udfmRestrictKeysSet hpt (W64.fromList key_set), ())) +restrictHpt HPT_OneShot{} _ = pure () {-# DEPRECATED addListToHpt "Deprecated in favour of 'addHomeModInfosToHpt', as the module at which a 'HomeModInfo' is inserted should always be derived from the 'HomeModInfo' itself." #-} -- After deprecation cycle, remove. addListToHpt :: HomePackageTable -> [(ModuleName, HomeModInfo)] -> IO () addListToHpt hpt = mapM_ (uncurry (addToHpt hpt)) -addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> Maybe (IO Linkable) -> IO () -addOnDemandHomeModule hpt mn bad_boot (new_hls_decls, new_hls_insts, new_hls_defaults, new_hls_fam_insts, new_hls_rules, new_hls_anns, new_hls_complete_matches) bytecode = do - atomicModifyIORef (loader hpt) $ \hls -> +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 + atomicModifyIORef loader $ \hls -> ( if mn `elemModuleEnv` hls_modules hls then hls -- If already loaded, don't add twice else @@ -285,41 +299,39 @@ addOnDemandHomeModule hpt mn bad_boot (new_hls_decls, new_hls_insts, new_hls_def ) where addHomeModTypeEnv hls = hls - { hls_type_env = extendNameEnvList (hls_type_env hls) new_hls_decls + { hls_type_env = extendNameEnvList (hls_type_env hls) new_decls } addHomeModData hls = hls { - hls_modules = extendModuleEnv (hls_modules hls) mn (), - hls_type_env = extendNameEnvList (hls_type_env hls) new_hls_decls, - hls_iface_bytecode = case bytecode of - Nothing -> hls_iface_bytecode hls - Just bc -> extendModuleEnv (hls_iface_bytecode hls) mn bc, + hls_modules = extendModuleEnv (hls_modules hls) mn (), + hls_type_env = extendNameEnvList (hls_type_env hls) new_decls, hls_rule_base = extendRuleBaseList (hls_rule_base hls) - new_hls_rules, + new_rules, hls_complete_matches - = hls_complete_matches hls ++ new_hls_complete_matches, + = hls_complete_matches hls ++ new_complete_matches, hls_inst_env = extendInstEnvList (hls_inst_env hls) - new_hls_insts, + new_insts, hls_fam_inst_env = extendFamInstEnvList (hls_fam_inst_env hls) - new_hls_fam_insts, + new_fam_insts, hls_ann_env = extendAnnEnvList (hls_ann_env hls) - new_hls_anns, + new_anns, hls_mod_fam_inst_env = let fam_inst_env = extendFamInstEnvList emptyFamInstEnv - new_hls_fam_insts + new_fam_insts in extendModuleEnv (hls_mod_fam_inst_env hls) mn fam_inst_env, - hls_defaults = extendModuleEnv (hls_defaults hls) mn new_hls_defaults + hls_defaults = extendModuleEnv (hls_defaults hls) mn new_defaults } -hlsSetBootInfo :: HomePackageTable -> InstalledModuleEnv ModuleNameWithIsBoot -> IO () -hlsSetBootInfo hpt is_boot_env = - atomicModifyIORef' (loader hpt) $ \ hls -> +hptSetBootInfo :: HomePackageTable -> InstalledModuleEnv ModuleNameWithIsBoot -> IO () +hptSetBootInfo HPT_Make{} _ = pure () +hptSetBootInfo HPT_OneShot{loader} is_boot_env = + atomicModifyIORef' loader $ \ hls -> (hls { hls_is_boot = is_boot_env }, ()) ---------------------------------------------------------------------------------- @@ -370,7 +382,11 @@ hlsIsModuleNameWithBoot hls mn = lookupInstalledModuleEnv (hls_is_boot hls) mn -- currently takes all dependencies only to then filter them with an ad-hoc transitive closure check. -- See #25639 hptCollectDependencies :: HomePackageTable -> IO (Set.Set (IfaceImportLevel, UnitId)) -hptCollectDependencies HPT{table} = do +hptCollectDependencies HPT_OneShot{loader} = do + hpt <- readIORef loader + return $ + foldr (Set.union . dep_direct_pkgs . mi_deps . hm_iface) Set.empty (hls_table hpt) +hptCollectDependencies HPT_Make{table} = do hpt <- readIORef table return $ foldr (Set.union . dep_direct_pkgs . mi_deps . hm_iface) Set.empty hpt @@ -380,7 +396,11 @@ hptCollectDependencies HPT{table} = do -- -- $O(n)$ in the number of modules in the HPT. hptCollectHomeModInfo :: HomePackageTable -> IO [HomeModInfo] -hptCollectHomeModInfo HPT{table} = do +hptCollectHomeModInfo HPT_OneShot{loader} = do + hpt <- readIORef loader + return $ + foldr (:) [] (hls_table hpt) +hptCollectHomeModInfo HPT_Make{table} = do hpt <- readIORef table return $ foldr (:) [] hpt @@ -398,7 +418,13 @@ hptCollectHomeModInfo HPT{table} = do -- unit map in failed lookups. pprHPT :: HomePackageTable -> IO SDoc -- A bit arbitrary for now -pprHPT HPT{table=hptr} = do +pprHPT HPT_OneShot{loader=hptr} = do + hpt <- readIORef hptr + return $! + pprUDFM (hls_table hpt) $ \hms -> + vcat [ ppr (mi_module (hm_iface hm)) + | hm <- hms ] +pprHPT HPT_Make{table=hptr} = do hpt <- readIORef hptr return $! pprUDFM hpt $ \hms -> @@ -437,7 +463,12 @@ pprHPT HPT{table=hptr} = do -- If this function is ever exposed from the HPT module, make sure the -- argument function doesn't introduce leaks. concatHpt :: (HomeModInfo -> [a]) -> HomePackageTable -> IO [a] -concatHpt f HPT{table} = do +concatHpt f HPT_OneShot{loader} = do + hpt <- readIORef loader + return $ concat . eltsUDFM . mapMaybeUDFM g $ hls_table hpt + where + g hmi = case f hmi of { [] -> Nothing; as -> Just as } +concatHpt f HPT_Make{table} = do hpt <- readIORef table return $ concat . eltsUDFM . mapMaybeUDFM g $ hpt where @@ -449,15 +480,15 @@ concatHpt f HPT{table} = do -- | Gets the internal 'IORef' which holds the 'HomeModInfo's of this HPT. -- Use with care. -hptInternalTableRef :: HomePackageTable -> IORef (DModuleNameEnv HomeModInfo) -hptInternalTableRef = table +-- hptInternalTableRef :: HomePackageTable -> IORef (DModuleNameEnv HomeModInfo) +-- hptInternalTableRef = \ case +-- HPT_Make{table} -> table +-- HPT_OneShot{} -> -- TODO: @fendor how does this make sense? -- | Construct a HomePackageTable from the IORef. -- Use with care, only if you can really justify going around the intended insert-only API. hptInternalTableFromRef :: IORef (DModuleNameEnv HomeModInfo) -> IO HomePackageTable hptInternalTableFromRef ref = do - loader <- newIORef emptyHomeLoaderState - return HPT { - table = ref, - loader + return HPT_Make { + table = ref } ===================================== ghc/GHCi/Leak.hs ===================================== @@ -7,7 +7,6 @@ module GHCi.Leak import Control.Monad import Data.Bits -import Data.IORef import Foreign.Ptr (ptrToIntPtr, intPtrToPtr) import GHC import GHC.Ptr (Ptr (..)) @@ -43,7 +42,7 @@ data LeakModIndicators = LeakModIndicators getLeakIndicators :: HscEnv -> IO LeakIndicators getLeakIndicators hsc_env = fmap LeakIndicators $ do - hpt <- readIORef $ hptInternalTableRef $ hsc_HPT hsc_env + hpt <- listHptIfaces $ hsc_HPT hsc_env forM (eltsUDFM hpt) $ \hmi@HomeModInfo{..} -> do leakMod <- mkWeakPtr hmi Nothing leakIface <- mkWeakPtr hm_iface Nothing View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c00e2758112af1126cdcc1d51cae8705... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/c00e2758112af1126cdcc1d51cae8705... 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
participants (1)
-
Hannes Siebenhandl (@fendor)