Hannes Siebenhandl pushed to branch wip/fendor/one-shot-no-eps at Glasgow Haskell Compiler / GHC Commits: 9b5b974d by Hannes Siebenhandl at 2026-11-30T15:42:20+01:00 Experiment with separate table types for inOneShot mode - - - - - 27 changed files: - compiler/GHC.hs - compiler/GHC/Core/Opt/Pipeline.hs - compiler/GHC/Core/Opt/Simplify.hs - compiler/GHC/Core/Rules.hs - compiler/GHC/Driver/Env.hs - compiler/GHC/Driver/Main/Compile.hs - compiler/GHC/Driver/Main/Hsc.hs - compiler/GHC/Iface/Load.hs - compiler/GHC/Iface/Recomp.hs - compiler/GHC/IfaceToCore.hs - compiler/GHC/Linker/Loader.hs - compiler/GHC/Tc/Instance/Family.hs - compiler/GHC/Tc/Module.hs - compiler/GHC/Tc/Utils/Env.hs - compiler/GHC/Tc/Utils/Instantiate.hs - compiler/GHC/Unit/Env.hs - compiler/GHC/Unit/External.hs - compiler/GHC/Unit/Home/Graph.hs - compiler/GHC/Unit/Home/PackageTable.hs - compiler/GHC/Unit/Module/Env.hs - ghc/GHCi/Leak.hs - testsuite/tests/bytecode/T25090/Makefile - testsuite/tests/driver/one-shot-boot/A.hs - testsuite/tests/driver/one-shot-boot/B.hs - testsuite/tests/driver/one-shot-boot/M.hs - testsuite/tests/driver/one-shot-boot/Makefile - testsuite/tests/driver/one-shot-boot/all.T Changes: ===================================== compiler/GHC.hs ===================================== @@ -442,13 +442,14 @@ import GHC.Types.PkgQual import GHC.Unit import GHC.Unit.Env as UnitEnv import GHC.Unit.Finder -import GHC.Unit.Module.ModIface -import GHC.Unit.Module.ModGuts +import GHC.Unit.Home.Graph qualified as HUG +import GHC.Unit.Home.ModInfo +import GHC.Unit.Home.PackageTable qualified as HPT +import GHC.Unit.Module.Graph import GHC.Unit.Module.ModDetails +import GHC.Unit.Module.ModGuts +import GHC.Unit.Module.ModIface import GHC.Unit.Module.ModSummary -import GHC.Unit.Module.Graph -import GHC.Unit.Home.ModInfo -import qualified GHC.Unit.Home.Graph as HUG import Control.Applicative ((<|>)) import Control.Monad @@ -693,14 +694,43 @@ setUnitDynFlagsNoCheck uid dflags1 = do (unit_state,home_unit,mconstants) <- liftIO $ initUnits logger dflags1 (hscUIC hsc_env) (hsc_all_home_unit_ids hsc_env) updated_dflags <- liftIO $ updatePlatformConstants dflags1 mconstants - let upd hue = - hue - { homeUnitEnv_units = unit_state - , homeUnitEnv_dflags = updated_dflags - , homeUnitEnv_home_unit = Just home_unit - } + let + upd hue = do + let + warnOnNonEmptyTable = + whenM (not <$> HPT.isHptEmpty (homeUnitEnv_hpt hue)) $ do + logInfo logger + ( text "Changed the ghc mode" <+> ppr (ghcMode $ homeUnitEnv_dflags hue) <+> text "to" <+> ppr (ghcMode updated_dflags) $$ + text "The home package table was not empty!" $$ + text "The table was cleared any way." + ) + the_hpt <- + case (HPT.isOneShotMode (homeUnitEnv_hpt hue), ghcMode updated_dflags) of + (True, OneShot) -> + pure $ homeUnitEnv_hpt hue + (True, CompManager) -> do + warnOnNonEmptyTable + HPT.emptyHomePackageTable + (True, MkDepend) -> do + warnOnNonEmptyTable + HPT.emptyHomePackageTable + (False, OneShot) -> do + warnOnNonEmptyTable + HPT.emptyOnDemandHomePackageTable + (False, CompManager) -> + pure $ homeUnitEnv_hpt hue + (False, MkDepend) -> + pure $ homeUnitEnv_hpt hue + pure hue + { homeUnitEnv_units = unit_state + , homeUnitEnv_dflags = updated_dflags + , homeUnitEnv_home_unit = Just home_unit + , homeUnitEnv_hpt = the_hpt + } + + - let unit_env = UnitEnv.ue_updateHomeUnitEnv upd uid (hsc_unit_env hsc_env) + unit_env <- liftIO $ UnitEnv.ue_adjustHomeUnitEnvA upd uid (hsc_unit_env hsc_env) let dflags = updated_dflags ===================================== 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_unit_env hsc_env) (moduleUnitId mod) (GWIB { gwib_mod = moduleName mod, gwib_isBoot = NotBoot }) name_ppr_ctx = mkNamePprCtx ptc unit_env rdr_env ptc = initPromotionTickContext dflags @@ -468,7 +468,7 @@ doCorePass pass guts = do p_fam_env <- getPackageFamInstEnv l_fam_env <- getLazyHomeFamInstEnv let platform = targetPlatform dflags - let fam_envs = (p_fam_env `unionFamInstEnv` l_fam_env, mg_fam_inst_env guts) + let fam_envs = (p_fam_env, mg_fam_inst_env guts `unionFamInstEnv` l_fam_env) let updateBinds f = return $ guts { mg_binds = f (mg_binds guts) } let updateBindsM f = f (mg_binds guts) >>= \b' -> return $ guts { mg_binds = b' } let updateBindsAndRulesM f = f (mg_binds guts) (mg_rules guts) >>= \(b',r') -> return $ guts { mg_binds = b', mg_rules = r' } ===================================== compiler/GHC/Core/Opt/Simplify.hs ===================================== @@ -44,7 +44,7 @@ import GHC.Types.Unique.FM import Control.Monad import Data.Foldable ( for_ ) -import GHC.Unit.Home.PackageTable (hptLoadedRuleBase, HomePackageTable) +import GHC.Unit.Home.PackageTable (hptLoadedRuleBase, hptLoadedFamInstEnv, HomePackageTable) {- ************************************************************************ @@ -74,8 +74,9 @@ simplifyExpr :: Logger simplifyExpr logger euc hpt opts expr = withTiming logger (text "Simplify [expr]") (const ()) $ do { eps <- eucEPS euc ; + ; home_fam_inst_env <- hptLoadedFamInstEnv hpt ; let fam_envs = ( eps_fam_inst_env eps - , extendFamInstEnvList emptyFamInstEnv $ se_fam_inst opts + , extendFamInstEnvList home_fam_inst_env $ se_fam_inst opts ) -- See Note [Seed the in-scope set for open expressions] simpl_env = setInScopeSet base_env @@ -278,6 +279,7 @@ simplifyPgm logger unit_env name_ppr_ctx opts -- value (via read_rule_env) and then combine it with the existing rule base. -- See `GHC.Core.Opt.Simplify.Monad.getSimplRules`. eps <- ueEPS unit_env ; + home_fam_inst_env <- hptLoadedFamInstEnv (ue_hpt unit_env) ; let { -- base_rule_env contains -- (a) home package rules, fixed across all iterations -- (b) local rules (substituted) from `local_rules` arg to do_iteration @@ -294,7 +296,7 @@ simplifyPgm logger unit_env name_ppr_ctx opts ; read_rule_env :: IO RuleEnv ; read_rule_env = (\ home_rules eps_rules -> updExternalPackageRules (updHomePackageRules base_rule_env home_rules) eps_rules) <$> read_home_rules <*> read_eps_rules - ; fam_envs = (eps_fam_inst_env eps, fam_inst_env) + ; fam_envs = (eps_fam_inst_env eps, fam_inst_env `unionFamInstEnv` home_fam_inst_env) ; simpl_env = mkSimplEnv mode fam_envs } ; -- Simplify the program ===================================== compiler/GHC/Core/Rules.hs ===================================== @@ -401,7 +401,7 @@ lookupRuleBase = lookupUFM unionRuleBase :: RuleBase -> RuleBase -> RuleBase unionRuleBase - = plusUFM + = plusUFM_C (++) pprRuleBase :: RuleBase -> SDoc pprRuleBase rules = pprUFM rules $ \rss -> ===================================== 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 :: UnitEnv -> UnitId -> ModuleNameWithIsBoot -> IO RuleBase +hugRulesBelow unit_env uid mn = foldr (flip extendRuleBaseList) emptyRuleBase <$> + hugSomeThingsBelowUs (md_rules . hm_details) False unit_env 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 :: UnitEnv -> UnitId -> ModuleNameWithIsBoot -> IO AnnEnv +hugAnnsBelow unit_env uid mn = foldr (flip extendAnnEnvList) emptyAnnEnv <$> + hugSomeThingsBelowUs (md_anns . hm_details) False unit_env 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 :: UnitEnv -> UnitId -> ModuleNameWithIsBoot -> IO CompleteMatches +hugCompleteSigsBelow unit_env uid mn = foldr (++) [] <$> + hugSomeThingsBelowUs (md_complete_matches . hm_details) False unit_env 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 :: UnitEnv -> UnitId -> ModuleNameWithIsBoot -> IO (InstEnv, [FamInst]) +hugInstancesBelow unit_env uid mnwib = do let mn = mkModule uid (gwib_mod mnwib) (insts, famInsts) <- unzip . concat <$> @@ -289,7 +290,7 @@ hugInstancesBelow hsc_env uid mnwib = do then [] else [(md_insts details, md_fam_insts details)]) True -- Include -hi-boot - hsc_env + unit_env uid mnwib return (foldl' unionInstEnv emptyInstEnv insts, concat famInsts) @@ -297,16 +298,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 -> UnitEnv -> 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 +hugSomeThingsBelowUs extract include_hi_boot unit_env uid mn + | HPT.isOneShotMode (ue_hpt unit_env) + = pure [] + | otherwise + = let hug = ue_home_unit_graph unit_env + mg = ue_module_graph unit_env in sequence [ things @@ -346,14 +349,15 @@ prepareAnnotations hsc_env mb_guts = do -- otherwise load annotations from all home package table -- 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) - . get_mod <$> mb_guts - more_home_pkg_anns <- HomePackageTable.hptLoadedAnnEnv $ hsc_HPT hsc_env + home_pkg_anns <- case mb_guts of + Nothing -> hugAllAnns (hsc_unit_env hsc_env) + Just guts -> plusAnnEnv + <$> uncurry (hugAnnsBelow (hsc_unit_env hsc_env)) (get_mod guts) + <*> HomePackageTable.hptLoadedAnnEnv (hsc_HPT hsc_env) let other_pkg_anns = eps_ann_env eps !ann_env = maybe id plusAnnEnv mb_this_module_anns $! - plusAnnEnv home_pkg_anns (plusAnnEnv more_home_pkg_anns other_pkg_anns) + plusAnnEnv home_pkg_anns other_pkg_anns return ann_env -- | Find the 'TyThing' for the given 'Name' by using all the resources ===================================== compiler/GHC/Driver/Main/Compile.hs ===================================== @@ -1,5 +1,6 @@ {-# LANGUAGE NondecreasingIndentation #-} {-# LANGUAGE MultiWayIf #-} +{-# LANGUAGE OverloadedRecordDot #-} {-# OPTIONS_GHC -fprof-auto-top #-} @@ -215,7 +216,7 @@ loadIfaceByteCode hsc_env iface location type_env = where compile decls = do bco <- compileWholeCoreBindings hsc_env type_env decls - return $! Linkable (gbc_hash bco) (mi_module iface) (pure (DotGBC bco)) + return $! Linkable (gbc_hash bco) (decls.wcb_module) (pure (DotGBC bco)) loadIfaceByteCodeLazy :: HscEnv -> ===================================== compiler/GHC/Driver/Main/Hsc.hs ===================================== @@ -85,6 +85,7 @@ import GHC.Types.Error import GHC.Types.Name.Cache ( newNameCache ) import GHC.Utils.Panic +import qualified GHC.Utils.Outputable as Outputable import GHC.Utils.Logger import GHC.Utils.TmpFs @@ -107,7 +108,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 @@ -271,8 +275,10 @@ dumpIfaceStats hsc_env = do logger = hsc_logger hsc_env dump_rn_stats = logHasDumpFlag logger Opt_D_dump_rn_stats dump_if_trace = logHasDumpFlag logger Opt_D_dump_if_trace - when (dump_if_trace || dump_rn_stats) $ - logDumpMsg logger "Interface statistics" (ifaceStats eps) + when (dump_if_trace || dump_rn_stats) $ do + mb_home_stats <- hptLoadedStats (hsc_HPT hsc_env) + logDumpMsg logger "Interface statistics" $ + ifaceStats eps Outputable.$$ maybe Outputable.empty pprHptStats mb_home_stats ===================================== 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 {- ********************************************************************* @@ -522,8 +521,8 @@ importDecl name -- Now look it up again; this time we should find it { eps <- getEps -- @fendor: DONE - ; loaderTypeEnv <- queryHpt HomePackageTable.hptLoadedTypeEnv - ; case lookupTypeEnv loaderTypeEnv name <|> lookupTypeEnv (eps_PTE eps) name of + ; mb_home_thing <- queryHpt (`HomePackageTable.hptLookupLoadedType` name) + ; case mb_home_thing <|> lookupTypeEnv (eps_PTE eps) name of Just thing -> return $ Succeeded thing Nothing -> return $ Failed $ Can'tFindNameInInterface name @@ -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 @@ -834,7 +833,7 @@ loadInterface doc_str mod from ; if fromMaybe False $ fmap (`isHomeModule` mod) mhome_unit -- TODO @fendor: horrible code then do -- TODO @fendor: document that we don't want/need 'dontLeakTheHUG' here 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)) + (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 loc iface tcParts else dontLeakTheHUG $ do @@ -858,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 gbl_env 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. @@ -868,37 +867,37 @@ 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 +addModIfacePartsToHpt gbl_env mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do hsc_env <- getTopEnv 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 @@ -907,87 +906,70 @@ addModIfacePartsToHpt gbl_env mod loc iface (new_eps_decls, new_eps_insts, new_e -- of one's own boot file! (one-shot only) -- See Note [Loading your own hi-boot file] + mb_bytecode = loadIfaceByteCode hsc_env iface loc (md_types details) + + final_iface = stripLoadedIface "home package table" iface + 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 - -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 + liftIO $ if bad_boot + then hptAddOwnBootTypes hpt new_decls + else addOnDemandHomeModule hpt (HomeModInfo final_iface details emptyHomeModInfoLinkable) mb_bytecode + +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 - final_iface = iface - & set_mi_decls (panic "No mi_decls in PIT") - & set_mi_insts (panic "No mi_insts in PIT") - & set_mi_defaults (panic "No mi_defaults in PIT") - & set_mi_fam_insts (panic "No mi_fam_insts in PIT") - & 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") - - bad_boot = mi_boot iface == IsBoot - && isJust (lookupKnotVars (if_rec_types gbl_env) mod) - -- Warn against an EPS-updating import - -- 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. - -- - -- See Note [Interface Files with Core Definitions] - add_bytecode old - | Just action <- loadIfaceByteCode purged_hsc_env iface loc (mkNameEnv new_eps_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 + final_iface = stripLoadedIface "PIT" iface mhome_unit = ue_homeUnit (hsc_unit_env purged_hsc_env) - warnPprTrace bad_boot "loadInterface" (ppr mod) $ - updateEps_ $ \ eps -> - if elemModuleEnv mod (eps_PIT eps) || is_external_sig mhome_unit iface - 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 } - else - eps { - eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface, - eps_PTE = addDeclsToPTE (eps_PTE eps) new_eps_decls, - eps_iface_bytecode = add_bytecode (eps_iface_bytecode eps), - eps_rule_base = extendRuleBaseList (eps_rule_base eps) - new_eps_rules, - eps_complete_matches - = eps_complete_matches eps ++ new_eps_complete_matches, - eps_inst_env = extendInstEnvList (eps_inst_env eps) - new_eps_insts, - eps_fam_inst_env = extendFamInstEnvList (eps_fam_inst_env eps) - new_eps_fam_insts, - eps_ann_env = extendAnnEnvList (eps_ann_env eps) - new_eps_anns, - eps_mod_fam_inst_env - = let - fam_inst_env = - extendFamInstEnvList emptyFamInstEnv - new_eps_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 - } - + updateEps_ $ \ eps -> + if elemModuleEnv mod (eps_PIT eps) || is_external_sig mhome_unit iface + then eps + else + eps { + eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface, + eps_PTE = addDeclsToPTE (eps_PTE eps) new_decls, + eps_rule_base = extendRuleBaseList (eps_rule_base eps) + new_rules, + eps_complete_matches + = eps_complete_matches eps ++ new_complete_matches, + eps_inst_env = extendInstEnvList (eps_inst_env eps) + new_insts, + eps_fam_inst_env = extendFamInstEnvList (eps_fam_inst_env eps) + new_fam_insts, + eps_ann_env = extendAnnEnvList (eps_ann_env eps) + new_anns, + eps_mod_fam_inst_env + = let + fam_inst_env = + extendFamInstEnvList emptyFamInstEnv + new_fam_insts + in + extendModuleEnv (eps_mod_fam_inst_env eps) + mod + fam_inst_env, + eps_stats = addEpsInStats (eps_stats eps) + (length new_decls) + (length new_insts) + (length new_rules), + eps_defaults = extendModuleEnv (eps_defaults eps) mod new_defaults + } + + +stripLoadedIface :: String -> ModIface -> ModIface +stripLoadedIface table iface = iface + & set_mi_decls (gone "mi_decls") + & set_mi_insts (gone "mi_insts") + & set_mi_defaults (gone "mi_defaults") + & set_mi_fam_insts (gone "mi_fam_insts") + & set_mi_rules (gone "mi_rules") + & set_mi_anns (gone "mi_anns") + & set_mi_simplified_core (gone "mi_simplified_core") + where + gone :: String -> a + gone field = panic ("No " ++ field ++ " in " ++ table) {- Note [Loading your own hi-boot file] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -1204,10 +1186,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 +1208,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 ===================================== @@ -138,6 +138,8 @@ import Language.Haskell.Syntax.BooleanFormula qualified as BF(BooleanFormula(..) import Language.Haskell.Syntax.Extension (NoExtField (NoExtField)) import GHC.Builtin.KnownKeys (ioTyConKey) import GHC.Unit.Home.PackageTable +import GHC.Unit.Home (isHomeModule) +import GHC.Driver.Env (hsc_HPT, hsc_home_unit_maybe) {- This module takes @@ -633,12 +635,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) @@ -1103,8 +1105,19 @@ tc_iface_decl_fingerprint ignore_prags (_version, decl) bumpDeclStats :: Name -> IfL () -- Record that one more declaration has actually been used bumpDeclStats name = do { traceIf (text "Loading decl for" <+> ppr name) - ; updateEps_ (\eps -> let stats = eps_stats eps - in eps { eps_stats = stats { n_decls_out = n_decls_out stats + 1 } }) + ; hsc_env <- getTopEnv + ; let hpt = hsc_HPT hsc_env + is_on_demand_home_decl + | isOneShotMode hpt + , Just home_unit <- hsc_home_unit_maybe hsc_env + , Just mod <- nameModule_maybe name + = isHomeModule home_unit mod + | otherwise + = False + ; if is_on_demand_home_decl + then liftIO $ hptBumpDeclsOut hpt + else updateEps_ (\eps -> let stats = eps_stats eps + in eps { eps_stats = stats { n_decls_out = n_decls_out stats + 1 } }) } tc_fd :: FunDep IfLclName -> IfL (FunDep TyVar) ===================================== 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 @@ -666,8 +666,8 @@ 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/Tc/Instance/Family.hs ===================================== @@ -409,9 +409,9 @@ getFamInsts hpt_fam_insts mod | Just env <- lookupModuleEnv hpt_fam_insts mod = return env | otherwise = do { _ <- initIfaceTcRn (loadSysInterface doc mod) ; eps <- getEps - ; hpt_extra_mod_fam_insts <- queryHpt HomePackageTable.hptLoadedModFamInstEnv + ; mb_home_fam_insts <- queryHpt (`HomePackageTable.hptLoadedModFamInsts` mod) ; return (expectJust $ - lookupModuleEnv hpt_extra_mod_fam_insts mod <|> lookupModuleEnv (eps_mod_fam_inst_env eps) mod) } + mb_home_fam_insts <|> lookupModuleEnv (eps_mod_fam_inst_env eps) mod) } where doc = ppr mod <+> text "is a family-instance module" @@ -619,7 +619,7 @@ addLocalFamInst (home_fie, my_fis) fam_inst -- we've ever seen. Fixing this is part of #13102. ; eps <- getEps ; extra <- queryHpt HomePackageTable.hptLoadedFamInstEnv - ; let inst_envs = (eps_fam_inst_env eps `unionFamInstEnv` extra, home_fie) + ; let inst_envs = (eps_fam_inst_env eps, home_fie `unionFamInstEnv` extra) home_fie' = extendFamInstEnv home_fie fam_inst -- Check for conflicting instance decls and injectivity violations ===================================== 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, isOneShotMode) {- ************************************************************************ @@ -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_unit_env 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_unit_env 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 -> @@ -2177,7 +2175,10 @@ runTcInteractive tcm_plugin_handling hsc_env thing_inside (renameRawPkgQual (hsc_unit_env hsc_env) (hsModuleName $ unLoc $ ideclName i) (ideclPkgQual i)) - ; (home_insts, home_fam_insts) <- liftIO $ UnitEnv.hugAllInstances (hsc_unit_env hsc_env) + ; (home_insts, home_fam_insts) <- + if isOneShotMode (hsc_HPT hsc_env) + then pure (emptyInstEnv, []) + else liftIO $ UnitEnv.hugAllInstances (hsc_unit_env hsc_env) ; let imports = emptyImportAvails { imp_orphs = orphs } ===================================== compiler/GHC/Tc/Utils/Env.hs ===================================== @@ -386,8 +386,7 @@ tcGetClsDefaults :: [Module] -> TcM [DefaultEnv] tcGetClsDefaults mods = do hug <- hsc_HUG <$> getTopEnv module_env_defaults <- eps_defaults <$> getEps - more_env_defaults <- queryHpt HomePackageTable.hptLoadedDefaults - liftIO $ mapMaybeM (lookupClsDefault hug (module_env_defaults `plusModuleEnv` more_env_defaults)) mods + liftIO $ mapMaybeM (lookupClsDefault hug module_env_defaults) mods lookupClsDefault :: HomeUnitGraph -> ModuleEnv DefaultEnv -> Module -> IO (Maybe DefaultEnv) lookupClsDefault hug module_env_defaults mod = ===================================== compiler/GHC/Tc/Utils/Instantiate.hs ===================================== @@ -947,13 +947,6 @@ addLocalInst (home_ie, my_insts) ispec ; let (_tvs, cls, tys) = instanceHead ispec (matches, _, _) = lookupInstEnv False inst_envs cls tys dups = filter (identicalClsInstHead ispec) (map fst matches) - ; traceRn "addLocalInst 0" (ppr ispec) - ; traceRn "addLocalInst 1" (ppr home_ie) - ; traceRn "addLocalInst 2" (ppr my_insts) - ; traceRn "addLocalInst 3" (ppr matches) - ; traceRn "addLocalInst 4" (ppr dups) - ; traceRn "addLocalInst 5" (ppr global_ie) - ; traceRn "addLocalInst 6" (ppr extra_inst_env) ; unless (null dups) $ dupInstErr ispec (head dups) ===================================== compiler/GHC/Unit/Env.hs ===================================== @@ -52,6 +52,7 @@ module GHC.Unit.Env , ue_unitHomeUnit , ue_unitHomeUnit_maybe , ue_updateHomeUnitEnv + , ue_adjustHomeUnitEnvA , ue_all_home_unit_ids , ue_unsafeHomeUnit @@ -360,6 +361,13 @@ ue_updateHomeUnitEnv f uid e = e { ue_home_unit_graph = HUG.unitEnv_adjust f uid $ ue_home_unit_graph e } +ue_adjustHomeUnitEnvA :: Applicative f => (HomeUnitEnv -> f HomeUnitEnv) -> UnitId -> UnitEnv -> f UnitEnv +ue_adjustHomeUnitEnvA f uid e = + let + hugA = HUG.unitEnv_adjustA f uid $ ue_home_unit_graph e + in + (\ hug -> e { ue_home_unit_graph = hug }) <$> hugA + ue_all_home_unit_ids :: UnitEnv -> Set.Set UnitId ue_all_home_unit_ids = HUG.allUnits . ue_home_unit_graph ===================================== 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/Graph.hs ===================================== @@ -61,6 +61,8 @@ module GHC.Unit.Home.Graph , unitEnv_foldWithKey , unitEnv_singleton , unitEnv_adjust + , unitEnv_adjustA + , unitEnv_alterF , unitEnv_keys , unitEnv_insert , unitEnv_new @@ -320,6 +322,17 @@ unitEnv_adjust f uid unitEnv = unitEnv { unitEnv_graph = Map.adjust f uid (unitEnv_graph unitEnv) } +unitEnv_adjustA :: Applicative f => (v -> f v) -> UnitEnvGraphKey -> UnitEnvGraph v -> f (UnitEnvGraph v) +unitEnv_adjustA f uid unitEnv = + unitEnv_alterF go uid unitEnv + where + go Nothing = pure Nothing + go (Just v) = Just <$> f v + +unitEnv_alterF :: Functor f => (Maybe v -> f (Maybe v)) -> UnitEnvGraphKey -> UnitEnvGraph v -> f (UnitEnvGraph v) +unitEnv_alterF f uid unitEnv = + UnitEnvGraph <$> Map.alterF f uid (unitEnv_graph unitEnv) + unitEnv_singleton :: UnitEnvGraphKey -> v -> UnitEnvGraph v unitEnv_singleton active m = UnitEnvGraph { unitEnv_graph = Map.singleton active m ===================================== compiler/GHC/Unit/Home/PackageTable.hs ===================================== @@ -21,13 +21,18 @@ -- 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(..) + HomePackageTable , emptyHomePackageTable + , emptyOnDemandHomePackageTable - , HomeLoaderState -- TODO: should this be opaque? Probably - , emptyHomeLoaderState + , isOneShotMode + , isHptEmpty + + -- , HomeLoaderState -- TODO: should this be opaque? Probably + -- , emptyHomeLoaderState -- * Lookups in the HPT , lookupHpt @@ -37,27 +42,31 @@ module GHC.Unit.Home.PackageTable , addHomeModInfoToHpt , addHomeModInfosToHpt , addOnDemandHomeModule + , hptAddOwnBootTypes -- * Restrict the HPT , restrictHpt -- * Queries about home modules + , listHptIfaces , hptCompleteSigs , hptAllInstances , hptAllFamInstances , hptAllAnnotations - , hlsIsModuleNameWithBoot - , hlsIsBoot - , hlsSetBootInfo + , hptSetBootInfo , hptLoadedIsBoot - , hptLoadedTypeEnv + , hptLookupLoadedType , hptLoadedInstEnv , hptLoadedFamInstEnv - , hptLoadedModFamInstEnv + , hptLoadedModFamInsts , hptLoadedRuleBase , hptLoadedAnnEnv , hptLoadedCompleteMatches - , hptLoadedDefaults + , hptLoadedByteCode + , HptStats(..) + , hptLoadedStats + , hptBumpDeclsOut + , pprHptStats -- ** More Traversal-based queries , hptCollectDependencies @@ -78,9 +87,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. @@ -113,28 +121,21 @@ import GHC.Unit.Module.ModIface import GHC.Utils.Outputable import GHC.Types.Name import GHC.Types.Name.Env (extendNameEnvList) -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 + +isHptEmpty :: HomePackageTable -> IO Bool +isHptEmpty = \ case + HPT_OneShot{loader} -> null . hls_table <$> readIORef loader + HPT_Make{table} -> isNullUDFM <$> readIORef table -- | Create a new 'HomePackageTable'. -- @@ -145,85 +146,195 @@ 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_indices :: !LoaderIndices , hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot - , hls_type_env :: !TypeEnv + , hls_boot_type_env :: !TypeEnv , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) - , hls_inst_env :: !InstEnv - , hls_fam_inst_env :: !FamInstEnv - , hls_mod_fam_inst_env :: !(ModuleEnv FamInstEnv) - , hls_rule_base :: !RuleBase - , hls_ann_env :: !AnnEnv - , hls_complete_matches :: !CompleteMatches - , hls_defaults :: ModuleEnv DefaultEnv + , hls_stats :: !HptStats + } + +data LoaderIndices = LoaderIndices + { li_inst_env :: !InstEnv + , li_fam_inst_env :: !FamInstEnv + , li_rule_base :: !RuleBase + , li_ann_env :: !AnnEnv + , li_complete_matches :: !CompleteMatches } +emptyLoaderIndices :: LoaderIndices +emptyLoaderIndices = LoaderIndices + { li_inst_env = emptyInstEnv + , li_fam_inst_env = emptyFamInstEnv + , li_rule_base = emptyRuleBase + , li_ann_env = emptyAnnEnv + , li_complete_matches = [] + } + +extendLoaderIndices :: LoaderIndices -> HomeModInfo -> LoaderIndices +extendLoaderIndices li hmi = LoaderIndices + { li_inst_env = extendInstEnvList (li_inst_env li) (instEnvElts (md_insts details)) + , li_fam_inst_env = extendFamInstEnvList (li_fam_inst_env li) (md_fam_insts details) + , li_rule_base = extendRuleBaseList (li_rule_base li) (md_rules details) + , li_ann_env = extendAnnEnvList (li_ann_env li) (md_anns details) + , li_complete_matches = li_complete_matches li ++ md_complete_matches details + } + where + details = hm_details hmi + emptyHomeLoaderState :: HomeLoaderState emptyHomeLoaderState = HomeLoaderState - { hls_modules = emptyModuleEnv + { hls_table = emptyUDFM + , hls_indices = emptyLoaderIndices , hls_is_boot = emptyInstalledModuleEnv - , hls_type_env = emptyTypeEnv + , hls_boot_type_env = emptyTypeEnv , hls_iface_bytecode = emptyModuleEnv - , hls_inst_env = emptyInstEnv - , hls_fam_inst_env = emptyFamInstEnv - , hls_mod_fam_inst_env = emptyModuleEnv - , hls_rule_base = emptyRuleBase - , hls_ann_env = emptyAnnEnv - , hls_complete_matches = [] - , hls_defaults = emptyModuleEnv + , hls_stats = emptyHptStats } +insertLoadedHomeModInfo :: ModuleName -> HomeModInfo -> HomeLoaderState -> HomeLoaderState +insertLoadedHomeModInfo mn hmi hls + | mn `elemUDFM` hls_table hls + = hls { hls_table = table' + , hls_indices = foldl' extendLoaderIndices emptyLoaderIndices (eltsUDFM table') + , hls_stats = stats' + } + | otherwise + = hls { hls_table = table' + , hls_indices = extendLoaderIndices (hls_indices hls) hmi + , hls_stats = stats' + } + where + table' = addToUDFM (hls_table hls) mn hmi + details = hm_details hmi + stats' = addHptInStats (hls_stats hls) + (length (typeEnvElts (md_types details))) + (length (instEnvElts (md_insts details))) + (length (md_rules details)) + -------------------------------------------------------------------------------- -- * Lookups in the HPT -------------------------------------------------------------------------------- -- | 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) - -hptLoadedTypeEnv :: HomePackageTable -> IO TypeEnv -hptLoadedTypeEnv hpt = hls_type_env <$> readIORef (loader hpt) +hptLoadedIsBoot = queryOnDemandHpt emptyInstalledModuleEnv hls_is_boot + +hptLookupLoadedType :: HomePackageTable -> Name -> IO (Maybe TyThing) +hptLookupLoadedType HPT_Make{} _ = pure Nothing +hptLookupLoadedType hpt@HPT_OneShot{loader} name = do + mb_hmi <- case nameModule_maybe name of + Just modl -> lookupHptByModule hpt modl + Nothing -> pure Nothing + case mb_hmi of + Just hmi -> pure $! lookupTypeEnv (md_types (hm_details hmi)) name + Nothing -> (`lookupTypeEnv` name) . hls_boot_type_env <$> readIORef loader + +hptLoadedModFamInsts :: HomePackageTable -> Module -> IO (Maybe FamInstEnv) +hptLoadedModFamInsts HPT_Make{} _ = pure Nothing +hptLoadedModFamInsts hpt@HPT_OneShot{} modl = + fmap (extendFamInstEnvList emptyFamInstEnv . md_fam_insts . hm_details) + <$> lookupHptByModule hpt modl hptLoadedInstEnv :: HomePackageTable -> IO InstEnv -hptLoadedInstEnv hpt = hls_inst_env <$> readIORef (loader hpt) +hptLoadedInstEnv = queryOnDemandHpt emptyInstEnv (li_inst_env . hls_indices) hptLoadedFamInstEnv :: HomePackageTable -> IO FamInstEnv -hptLoadedFamInstEnv hpt = hls_fam_inst_env <$> readIORef (loader hpt) - -hptLoadedModFamInstEnv :: HomePackageTable -> IO (ModuleEnv FamInstEnv) -hptLoadedModFamInstEnv hpt = hls_mod_fam_inst_env <$> readIORef (loader hpt) +hptLoadedFamInstEnv = queryOnDemandHpt emptyFamInstEnv (li_fam_inst_env . hls_indices) hptLoadedRuleBase :: HomePackageTable -> IO RuleBase -hptLoadedRuleBase hpt = hls_rule_base <$> readIORef (loader hpt) +hptLoadedRuleBase = queryOnDemandHpt emptyRuleBase (li_rule_base . hls_indices) hptLoadedAnnEnv :: HomePackageTable -> IO AnnEnv -hptLoadedAnnEnv hpt = hls_ann_env <$> readIORef (loader hpt) +hptLoadedAnnEnv = queryOnDemandHpt emptyAnnEnv (li_ann_env . hls_indices) hptLoadedCompleteMatches :: HomePackageTable -> IO CompleteMatches -hptLoadedCompleteMatches hpt = hls_complete_matches <$> readIORef (loader hpt) +hptLoadedCompleteMatches = queryOnDemandHpt [] (li_complete_matches . hls_indices) + +hptLoadedByteCode :: HomePackageTable -> Module -> IO (Maybe (IO Linkable)) +hptLoadedByteCode hpt modl = + queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) modl) hpt + +hptLoadedStats :: HomePackageTable -> IO (Maybe HptStats) +hptLoadedStats = queryOnDemandHpt Nothing (Just . hls_stats) + +hptBumpDeclsOut :: HomePackageTable -> IO () +hptBumpDeclsOut HPT_Make{} = pure () +hptBumpDeclsOut HPT_OneShot{loader} = + atomicModifyIORef' loader $ \ hls -> + let stats = hls_stats hls + in (hls { hls_stats = stats { hpt_n_decls_out = hpt_n_decls_out stats + 1 } }, ()) + +data HptStats = HptStats + { hpt_n_ifaces_in :: !Int + , hpt_n_decls_in :: !Int + , hpt_n_decls_out :: !Int + , hpt_n_insts_in :: !Int + , hpt_n_rules_in :: !Int + } + +emptyHptStats :: HptStats +emptyHptStats = HptStats + { hpt_n_ifaces_in = 0 + , hpt_n_decls_in = 0 + , hpt_n_decls_out = 0 + , hpt_n_insts_in = 0 + , hpt_n_rules_in = 0 + } -hptLoadedDefaults :: HomePackageTable -> IO (ModuleEnv DefaultEnv) -hptLoadedDefaults hpt = hls_defaults <$> readIORef (loader hpt) +addHptInStats :: HptStats -> Int -> Int -> Int -> HptStats +addHptInStats stats n_decls n_insts n_rules + = stats { hpt_n_ifaces_in = hpt_n_ifaces_in stats + 1 + , hpt_n_decls_in = hpt_n_decls_in stats + n_decls + , hpt_n_insts_in = hpt_n_insts_in stats + n_insts + , hpt_n_rules_in = hpt_n_rules_in stats + n_rules } + +pprHptStats :: HptStats -> SDoc +pprHptStats stats + = hcat [text "Home interface stats: ", msg] + where + msg = vcat + [int (hpt_n_ifaces_in stats) <+> text "interfaces read", + hsep [ int (hpt_n_decls_out stats), text "type/class/variable imported, out of", + int (hpt_n_decls_in stats), text "read"], + hsep [ int (hpt_n_insts_in stats), text "instance decls read"], + hsep [ int (hpt_n_rules_in stats), text "rule decls read"] + ] + +listHptIfaces :: HomePackageTable -> IO (DModuleNameEnv HomeModInfo) +listHptIfaces = \ case + HPT_Make{table} -> readIORef table + HPT_OneShot{loader} -> hls_table <$> readIORef loader -------------------------------------------------------------------------------- -- * Extending the HPT @@ -244,10 +355,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} mn hmi = + atomicModifyIORef' loader (\hls -> (insertLoadedHomeModInfo mn hmi hls, ())) -- | 'addHomeModInfoToHpt' for multiple module infos. addHomeModInfosToHpt :: HomePackageTable -> [HomeModInfo] -> IO () @@ -262,64 +375,39 @@ 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 -> - ( if mn `elemModuleEnv` hls_modules hls - then hls -- If already loaded, don't add twice - else - if bad_boot - then addHomeModTypeEnv hls - else addHomeModData hls - , () - ) +addOnDemandHomeModule :: HomePackageTable -> HomeModInfo -> Maybe (IO Linkable) -> IO () +addOnDemandHomeModule hpt@HPT_Make{} hmi _ = addHomeModInfoToHpt hmi hpt +addOnDemandHomeModule HPT_OneShot{loader} hmi mb_bytecode = + atomicModifyIORef' loader $ \hls -> + let hls' = insertLoadedHomeModInfo (moduleName modl) hmi hls + in ( case mb_bytecode of + Nothing -> hls' + Just action -> hls' { hls_iface_bytecode = extendModuleEnv (hls_iface_bytecode hls') modl action } + , () ) where - addHomeModTypeEnv hls = hls - { hls_type_env = extendNameEnvList (hls_type_env hls) new_hls_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_rule_base = extendRuleBaseList (hls_rule_base hls) - new_hls_rules, - hls_complete_matches - = hls_complete_matches hls ++ new_hls_complete_matches, - hls_inst_env = extendInstEnvList (hls_inst_env hls) - new_hls_insts, - hls_fam_inst_env = extendFamInstEnvList (hls_fam_inst_env hls) - new_hls_fam_insts, - hls_ann_env = extendAnnEnvList (hls_ann_env hls) - new_hls_anns, - hls_mod_fam_inst_env - = - let - fam_inst_env = - extendFamInstEnvList emptyFamInstEnv - new_hls_fam_insts - in - extendModuleEnv (hls_mod_fam_inst_env hls) - mn - fam_inst_env, - hls_defaults = extendModuleEnv (hls_defaults hls) mn new_hls_defaults - } - -hlsSetBootInfo :: HomePackageTable -> InstalledModuleEnv ModuleNameWithIsBoot -> IO () -hlsSetBootInfo hpt is_boot_env = - atomicModifyIORef' (loader hpt) $ \ hls -> + modl = mi_module (hm_iface hmi) + +hptAddOwnBootTypes :: HomePackageTable -> [(Name, TyThing)] -> IO () +hptAddOwnBootTypes HPT_Make{} _ = pure () +hptAddOwnBootTypes HPT_OneShot{loader} decls = + atomicModifyIORef' loader $ \hls -> + (hls { hls_boot_type_env = extendNameEnvList (hls_boot_type_env hls) decls }, ()) + +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 }, ()) ---------------------------------------------------------------------------------- @@ -329,11 +417,15 @@ hlsSetBootInfo hpt is_boot_env = -- | Get all 'CompleteMatches' (arising from COMPLETE pragmas) present in all -- modules from this unit's HPT. hptCompleteSigs :: HomePackageTable -> IO CompleteMatches -hptCompleteSigs = concatHpt (md_complete_matches . hm_details) +hptCompleteSigs HPT_OneShot{loader} = li_complete_matches . hls_indices <$> readIORef loader +hptCompleteSigs hpt@HPT_Make{} = concatHpt (md_complete_matches . hm_details) hpt -- | Find all the instance declarations (of classes and families) from this Home Package Table hptAllInstances :: HomePackageTable -> IO (InstEnv, [FamInst]) -hptAllInstances hpt = do +hptAllInstances HPT_OneShot{loader} = do + li <- hls_indices <$> readIORef loader + return (li_inst_env li, famInstEnvElts (li_fam_inst_env li)) +hptAllInstances hpt@HPT_Make{} = do hits <- flip concatHpt hpt $ \mod_info -> do let details = hm_details mod_info return (md_insts details, md_fam_insts details) @@ -350,13 +442,8 @@ hptAllFamInstances = fmap mkModuleEnv . concatHpt (\hmi -> [(hmiModule hmi, hmiF -- | All annotations from the HPT hptAllAnnotations :: HomePackageTable -> IO AnnEnv -hptAllAnnotations = fmap mkAnnEnv . concatHpt (md_anns . hm_details) - -hlsIsBoot :: HomeLoaderState -> InstalledModule -> Maybe IsBootInterface -hlsIsBoot hls mn = gwib_isBoot <$> hlsIsModuleNameWithBoot hls mn - -hlsIsModuleNameWithBoot :: HomeLoaderState -> InstalledModule -> Maybe ModuleNameWithIsBoot -hlsIsModuleNameWithBoot hls mn = lookupInstalledModuleEnv (hls_is_boot hls) mn +hptAllAnnotations HPT_OneShot{loader} = li_ann_env . hls_indices <$> readIORef loader +hptAllAnnotations hpt@HPT_Make{} = mkAnnEnv <$> concatHpt (md_anns . hm_details) hpt -------------------------------------------------------------------------------- -- * Traversal-based queries @@ -370,7 +457,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 +471,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 +493,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 +538,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 +555,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 } ===================================== compiler/GHC/Unit/Module/Env.hs ===================================== @@ -249,7 +249,7 @@ lookupModuleNameEnv :: ModuleNameEnv elt -> ModuleName -> Maybe elt lookupModuleNameEnv = lookupUFM unionModuleNameEnv :: ModuleNameEnv elt -> ModuleNameEnv elt -> ModuleNameEnv elt -unionModuleNameEnv = unionModuleNameEnv +unionModuleNameEnv = plusUFM -- | A map keyed off of 'ModuleName's (actually, their 'Unique's) -- Has deterministic folds and can be deterministically converted to a list ===================================== 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 ===================================== testsuite/tests/bytecode/T25090/Makefile ===================================== @@ -16,9 +16,11 @@ T25090a: '$(TEST_HC)' -fbyte-code-and-object-code -fprefer-byte-code A.o -o exe ./exe -# Like T25090a, but without .gbc files: the bytecode for the splice has to be -# compiled from the Core stored in the interface files -# (-fwrite-if-simplified-core). See Note [Interface Files with Core Definitions]. +T25090b: + '$(TEST_HC)' -fbyte-code-and-object-code -fprefer-byte-code A -o exe -v0 + ./exe + +# Like T25090a, but get bytecode for splicing from simplified core in the interface files. T25090c: '$(TEST_HC)' -c -fwrite-if-simplified-core C.hs-boot '$(TEST_HC)' -c -fwrite-if-simplified-core B.hs @@ -31,7 +33,3 @@ T25090c: '$(TEST_HC)' -c -fwrite-if-simplified-core -fprefer-byte-code A.hs '$(TEST_HC)' A.o -o exe ./exe - -T25090b: - '$(TEST_HC)' -fbyte-code-and-object-code -fprefer-byte-code A -o exe -v0 - ./exe ===================================== testsuite/tests/driver/one-shot-boot/A.hs ===================================== @@ -4,5 +4,4 @@ module A where import Fam import {-# SOURCE #-} M (T) --- This instance mentions 'T' from M's boot file. type instance F T = Int ===================================== testsuite/tests/driver/one-shot-boot/B.hs ===================================== @@ -3,8 +3,4 @@ module B where import Fam --- A second family-instance module for 'F', so that compiling M has to check --- the consistency of A's and B's instances. That check forces 'T', which --- makes GHC load M.hi-boot while compiling M itself. --- See Note [Loading your own hi-boot file] in GHC.Iface.Load. type instance F Bool = Char ===================================== testsuite/tests/driver/one-shot-boot/M.hs ===================================== @@ -7,11 +7,8 @@ import B () data T = MkT Int --- Also declared in M.hs-boot: loading M.hi-boot while compiling M must not --- cause a duplicate instance error. instance Show T where show (MkT n) = "MkT " ++ show n --- Not part of M.hs-boot. Needs A's instance of 'F' to typecheck. onlyInM :: F T onlyInM = 42 ===================================== testsuite/tests/driver/one-shot-boot/Makefile ===================================== @@ -2,9 +2,6 @@ TOP=../../.. include $(TOP)/mk/boilerplate.mk include $(TOP)/mk/test.mk -# One-shot compilation of a SOURCE-import cycle where type family instances -# mention the type from the boot file. Every module is compiled by its own -# invocation of GHC. OneShotBootFamInst: '$(TEST_HC)' $(TEST_HC_OPTS) -c Fam.hs '$(TEST_HC)' $(TEST_HC_OPTS) -c M.hs-boot @@ -15,9 +12,6 @@ OneShotBootFamInst: '$(TEST_HC)' $(TEST_HC_OPTS) Fam.o A.o B.o M.o Main.o -o main ./main -# Like OneShotBootFamInst, but M and Main are compiled by the same invocation. -# Compiling M loads M.hi-boot (see Note [Loading your own hi-boot file]); that -# must not make Main see the boot interface of M instead of the real one. OneShotBootSameInvocation: '$(TEST_HC)' $(TEST_HC_OPTS) -c Fam.hs '$(TEST_HC)' $(TEST_HC_OPTS) -c M.hs-boot ===================================== testsuite/tests/driver/one-shot-boot/all.T ===================================== @@ -1,5 +1,3 @@ -# Compiling M loads M.hi-boot, which triggers a warnPprTrace in debug builds. -# See Note [Loading your own hi-boot file] in GHC.Iface.Load. def test_one_shot_boot(name): return test(name, [extra_files(['Fam.hs', 'M.hs-boot', 'A.hs', 'B.hs', 'M.hs', 'Main.hs']), View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9b5b974da50c7e2aee4528c18bcea072... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/9b5b974da50c7e2aee4528c18bcea072... 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