Hannes Siebenhandl pushed to branch wip/fendor/one-shot-no-eps at Glasgow Haskell Compiler / GHC Commits: bc53c2eb by fendor at 2026-11-30T14:27:20+01:00 Add eps stats, fix retainer leak, and make fam inst more consistent - - - - - 7 changed files: - compiler/GHC/Core/Opt/Pipeline.hs - compiler/GHC/Driver/Main/Compile.hs - compiler/GHC/Driver/Main/Hsc.hs - compiler/GHC/Iface/Load.hs - compiler/GHC/IfaceToCore.hs - compiler/GHC/Tc/Instance/Family.hs - compiler/GHC/Unit/Home/PackageTable.hs Changes: ===================================== compiler/GHC/Core/Opt/Pipeline.hs ===================================== @@ -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/Driver/Main/Compile.hs ===================================== @@ -215,7 +215,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) (wcb_module decls) (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 @@ -274,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 ===================================== @@ -918,7 +918,8 @@ addModIfacePartsToHpt gbl_env mod loc iface (new_decls, new_insts, new_defaults, warnPprTrace bad_boot "loadInterface" (ppr mod) $ do hpt <- getHpt when (not bad_boot) $ do - liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt + let final_iface = stripLoadedIface "home package table" iface + liftIO $ addHomeModInfoToHpt (HomeModInfo final_iface details emptyHomeModInfoLinkable) hpt liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode addModIfacePartsToEps :: Module -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () @@ -926,14 +927,7 @@ addModIfacePartsToEps mod iface (new_decls, new_insts, new_defaults, new_fam_ins 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") + final_iface = stripLoadedIface "PIT" iface mhome_unit = ue_homeUnit (hsc_unit_env purged_hsc_env) updateEps_ $ \ eps -> @@ -970,6 +964,19 @@ addModIfacePartsToEps mod iface (new_decls, new_insts, new_defaults, new_fam_ins } +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] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Generally speaking, when compiling module M, we should not ===================================== 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 @@ -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/Tc/Instance/Family.hs ===================================== @@ -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/Unit/Home/PackageTable.hs ===================================== @@ -63,6 +63,10 @@ module GHC.Unit.Home.PackageTable , hptLoadedCompleteMatches , hptLoadedDefaults , hptLoadedByteCode + , HptStats(..) + , hptLoadedStats + , hptBumpDeclsOut + , pprHptStats -- ** More Traversal-based queries , hptCollectDependencies @@ -153,7 +157,6 @@ emptyOnDemandHomePackageTable = do data HomeLoaderState = HomeLoaderState { hls_table :: DModuleNameEnv HomeModInfo - , hls_modules :: ModuleEnv () , hls_is_boot :: InstalledModuleEnv ModuleNameWithIsBoot , hls_type_env :: !TypeEnv , hls_iface_bytecode :: !(ModuleEnv (IO Linkable)) @@ -167,13 +170,13 @@ data HomeLoaderState = HomeLoaderState , hls_ann_env :: !AnnEnv , hls_complete_matches :: !CompleteMatches , hls_defaults :: ModuleEnv DefaultEnv + , hls_stats :: !HptStats } emptyHomeLoaderState :: HomeLoaderState emptyHomeLoaderState = HomeLoaderState { hls_table = emptyUDFM - , hls_modules = emptyModuleEnv , hls_is_boot = emptyInstalledModuleEnv , hls_type_env = emptyTypeEnv , hls_iface_bytecode = emptyModuleEnv @@ -184,6 +187,7 @@ emptyHomeLoaderState = , hls_ann_env = emptyAnnEnv , hls_complete_matches = [] , hls_defaults = emptyModuleEnv + , hls_stats = emptyHptStats } -------------------------------------------------------------------------------- @@ -248,6 +252,52 @@ hptLoadedByteCode :: HomePackageTable -> Module -> IO (Maybe (IO Linkable)) hptLoadedByteCode hpt mod = queryOnDemandHpt Nothing (\ hls -> lookupModuleEnv (hls_iface_bytecode hls) mod) 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 + } + +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 @@ -306,13 +356,10 @@ addListToHpt hpt = mapM_ (uncurry (addToHpt hpt)) addOnDemandHomeModule :: HomePackageTable -> Module -> Bool -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> Maybe (IO Linkable) -> IO () addOnDemandHomeModule HPT_Make{} _ _ _ _ = pure () addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) mb_bytecode = do - atomicModifyIORef loader $ \hls -> - ( if mn `elemModuleEnv` hls_modules hls - then hls -- If already loaded, don't add twice - else - if bad_boot - then addHomeModTypeEnv hls - else addHomeModData hls + atomicModifyIORef' loader $ \hls -> + ( if bad_boot + then addHomeModTypeEnv hls + else addHomeModData hls , () ) where @@ -321,7 +368,6 @@ addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new } addHomeModData hls = hls { - hls_modules = extendModuleEnv (hls_modules hls) mn (), hls_type_env = extendNameEnvList (hls_type_env hls) new_decls, -- Don't add an entry if the iface doesn't have Core bindings, so that -- 'get_link_deps' knows that it should load object code. @@ -348,7 +394,11 @@ addOnDemandHomeModule HPT_OneShot{loader} mn bad_boot (new_decls, new_insts, new extendModuleEnv (hls_mod_fam_inst_env hls) mn fam_inst_env, - hls_defaults = extendModuleEnv (hls_defaults hls) mn new_defaults + hls_defaults = extendModuleEnv (hls_defaults hls) mn new_defaults, + hls_stats = addHptInStats (hls_stats hls) + (length new_decls) + (length new_insts) + (length new_rules) } hptSetBootInfo :: HomePackageTable -> InstalledModuleEnv ModuleNameWithIsBoot -> IO () View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bc53c2eb6da117767a4710cf212e1664... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/bc53c2eb6da117767a4710cf212e1664... 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