Hannes Siebenhandl pushed to branch wip/fendor/one-shot-no-eps at Glasgow Haskell Compiler / GHC Commits: 1a30ac00 by Hannes Siebenhandl at 2026-11-30T13:49:04+01:00 Experiment with separate table types for inOneShot mode - - - - - 8 changed files: - compiler/GHC.hs - compiler/GHC/Core/Rules.hs - compiler/GHC/Iface/Load.hs - compiler/GHC/Tc/Utils/Instantiate.hs - compiler/GHC/Unit/Env.hs - compiler/GHC/Unit/Home/Graph.hs - compiler/GHC/Unit/Home/PackageTable.hs - compiler/GHC/Unit/Module/Env.hs 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,41 @@ 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 = 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!" + ) + 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/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/Iface/Load.hs ===================================== @@ -833,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 iface tcParts else dontLeakTheHUG $ do @@ -857,7 +857,7 @@ loadInterface doc_str mod from (moduleUnitId mod `Set.notMember` hsc_all_home_unit_ids hsc_env) (text "Attempting to load home package interface into the EPS" $$ ppr (HUG.allUnits $ hsc_HUG hsc_env) $$ doc_str $$ ppr mod $$ ppr (moduleUnitId mod)) tcParts <- tcPartsOfTheIface mod iface - addModIfacePartsToEps gbl_env mod loc iface tcParts + addModIfacePartsToEps mod loc iface tcParts ; -- invoke plugins with *full* interface, not final_iface, to ensure -- that plugins have access to declarations, etc. @@ -907,11 +907,12 @@ addModIfacePartsToHpt gbl_env mod iface (new_decls, new_insts, new_defaults, new warnPprTrace bad_boot "loadInterface" (ppr mod) $ do hpt <- getHpt - liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt + when (not bad_boot) $ do + liftIO $ addHomeModInfoToHpt (HomeModInfo iface details emptyHomeModInfoLinkable) hpt liftIO $ addOnDemandHomeModule hpt mod bad_boot (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) -addModIfacePartsToEps :: IfGblEnv -> Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () -addModIfacePartsToEps gbl_env mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do +addModIfacePartsToEps :: Module -> ModLocation -> ModIface -> ([(Name, TyThing)], [ClsInst], DefaultEnv, [FamInst], [CoreRule], [Annotation], [CompleteMatch]) -> IfL () +addModIfacePartsToEps mod loc iface (new_decls, new_insts, new_defaults, new_fam_insts, new_rules, new_anns, new_complete_matches) = do purged_hsc_env <- getTopEnv let @@ -923,13 +924,6 @@ addModIfacePartsToEps gbl_env mod loc iface (new_decls, new_insts, new_defaults, & 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. -- @@ -943,43 +937,39 @@ addModIfacePartsToEps gbl_env mod loc iface (new_decls, new_insts, new_defaults, = old 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_decls } - else - eps { - eps_PIT = extendModuleEnv (eps_PIT eps) mod final_iface, - eps_PTE = addDeclsToPTE (eps_PTE eps) new_decls, - eps_iface_bytecode = add_bytecode (eps_iface_bytecode eps), - eps_rule_base = extendRuleBaseList (eps_rule_base eps) - new_rules, - eps_complete_matches - = 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 - } + 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_iface_bytecode = add_bytecode (eps_iface_bytecode eps), + 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 + } {- Note [Loading your own hi-boot file] ===================================== 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/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 ===================================== @@ -24,11 +24,12 @@ {-# OPTIONS_GHC -Wall -Wincomplete-record-selectors #-} module GHC.Unit.Home.PackageTable ( - HomePackageTable(..) + HomePackageTable , emptyHomePackageTable , emptyOnDemandHomePackageTable , isOneShotMode + , isHptEmpty -- , HomeLoaderState -- TODO: should this be opaque? Probably -- , emptyHomeLoaderState @@ -51,8 +52,6 @@ module GHC.Unit.Home.PackageTable , hptAllInstances , hptAllFamInstances , hptAllAnnotations - , hlsIsModuleNameWithBoot - , hlsIsBoot , hptSetBootInfo , hptLoadedIsBoot , hptLoadedTypeEnv @@ -129,6 +128,11 @@ 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'. -- -- Be careful not to share it across e.g. different units, since it uses a @@ -364,12 +368,6 @@ hptAllFamInstances = fmap mkModuleEnv . concatHpt (\hmi -> [(hmiModule hmi, hmiF 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 - -------------------------------------------------------------------------------- -- * Traversal-based queries -------------------------------------------------------------------------------- ===================================== 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 View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1a30ac00178d94bb55f7dfd32e396e51... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/1a30ac00178d94bb55f7dfd32e396e51... 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)