Skip to content

Commit b644165

Browse files
committed
Use a faster implementation of checkHomeUnitsClosed
GHC had an implementation of this function, but it was horribly inefficient We should move back to the GHC implementation on compilers where https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12162 is included Fixes #4046
1 parent 9b0699d commit b644165

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Development.IDE.Core.RuleTypes
5252
import Development.IDE.Core.Shake hiding (Log, Priority,
5353
knownTargets, withHieDb)
5454
import qualified Development.IDE.GHC.Compat as Compat
55+
import qualified Development.IDE.GHC.Compat.Util as Compat
5556
import Development.IDE.GHC.Compat.Core hiding (Target,
5657
TargetFile, TargetModule,
5758
Var, Warning, getOptions)
@@ -122,10 +123,11 @@ import GHC.Data.Bag
122123
import GHC.Driver.Env (hsc_all_home_unit_ids)
123124
import GHC.Driver.Errors.Types
124125
import GHC.Driver.Make (checkHomeUnitsClosed)
125-
import GHC.Types.Error (errMsgDiagnostic)
126+
import GHC.Types.Error (errMsgDiagnostic, singleMessage)
126127
import GHC.Unit.State
127128
#endif
128129

130+
import GHC.Data.Graph.Directed
129131
import GHC.ResponseFile
130132

131133
data Log
@@ -810,6 +812,63 @@ setNameCache :: IORef NameCache -> HscEnv -> HscEnv
810812
#endif
811813
setNameCache nc hsc = hsc { hsc_NC = nc }
812814

815+
-- This function checks then important property that if both p and q are home units
816+
-- then any dependency of p, which transitively depends on q is also a home unit.
817+
-- GHC had an implementation of this function, but it was horribly inefficient
818+
-- We should move back to the GHC implementation on compilers where
819+
-- https://gitlab.haskell.org/ghc/ghc/-/merge_requests/12162 is included
820+
checkHomeUnitsClosed' :: UnitEnv -> OS.Set UnitId -> [DriverMessages]
821+
checkHomeUnitsClosed' ue home_id_set
822+
| OS.null bad_unit_ids = []
823+
| otherwise = [singleMessage $ GHC.mkPlainErrorMsgEnvelope rootLoc $ DriverHomePackagesNotClosed (OS.toList bad_unit_ids)]
824+
where
825+
bad_unit_ids = upwards_closure OS.\\ home_id_set
826+
rootLoc = mkGeneralSrcSpan (Compat.fsLit "<command line>")
827+
828+
graph :: Graph (Node UnitId UnitId)
829+
graph = graphFromEdgedVerticesUniq graphNodes
830+
831+
-- downwards closure of graph
832+
downwards_closure
833+
= graphFromEdgedVerticesUniq [ DigraphNode uid uid (OS.toList deps)
834+
| (uid, deps) <- Map.toList (allReachable graph node_key)]
835+
836+
inverse_closure = transposeG downwards_closure
837+
838+
upwards_closure = OS.fromList $ map node_key $ reachablesG inverse_closure [DigraphNode uid uid [] | uid <- OS.toList home_id_set]
839+
840+
all_unit_direct_deps :: UniqMap UnitId (OS.Set UnitId)
841+
all_unit_direct_deps
842+
= unitEnv_foldWithKey go emptyUniqMap $ ue_home_unit_graph ue
843+
where
844+
go rest this this_uis =
845+
plusUniqMap_C OS.union
846+
(addToUniqMap_C OS.union external_depends this (OS.fromList $ this_deps))
847+
rest
848+
where
849+
external_depends = mapUniqMap (OS.fromList . unitDepends)
850+
#if !MIN_VERSION_ghc(9,7,0)
851+
$ listToUniqMap $ Map.toList
852+
#endif
853+
854+
$ unitInfoMap this_units
855+
this_units = homeUnitEnv_units this_uis
856+
this_deps = [ Compat.toUnitId unit | (unit,Just _) <- explicitUnits this_units]
857+
858+
graphNodes :: [Node UnitId UnitId]
859+
graphNodes = go OS.empty home_id_set
860+
where
861+
go done todo
862+
= case OS.minView todo of
863+
Nothing -> []
864+
Just (uid, todo')
865+
| OS.member uid done -> go done todo'
866+
| otherwise -> case lookupUniqMap all_unit_direct_deps uid of
867+
Nothing -> pprPanic "uid not found" (Compat.ppr (uid, all_unit_direct_deps))
868+
Just depends ->
869+
let todo'' = (depends OS.\\ done) `OS.union` todo'
870+
in DigraphNode uid uid (OS.toList depends) : go (OS.insert uid done) todo''
871+
813872
-- | Create a mapping from FilePaths to HscEnvEqs
814873
-- This combines all the components we know about into
815874
-- an appropriate session, which is a multi component
@@ -838,11 +897,7 @@ newComponentCache recorder exts cradlePath _cfp hsc_env old_cis new_cis = do
838897
Compat.initUnits dfs hsc_env
839898

840899
#if MIN_VERSION_ghc(9,3,0)
841-
let closure_errs = checkHomeUnitsClosed (hsc_unit_env hscEnv') (hsc_all_home_unit_ids hscEnv') pkg_deps
842-
pkg_deps = do
843-
home_unit_id <- uids
844-
home_unit_env <- maybeToList $ unitEnv_lookup_maybe home_unit_id $ hsc_HUG hscEnv'
845-
map (home_unit_id,) (map (Compat.toUnitId . fst) $ explicitUnits $ homeUnitEnv_units home_unit_env)
900+
let closure_errs = checkHomeUnitsClosed' (hsc_unit_env hscEnv') (hsc_all_home_unit_ids hscEnv')
846901
multi_errs = map (ideErrorWithSource (Just "cradle") (Just DiagnosticSeverity_Warning) _cfp . T.pack . Compat.printWithoutUniques) closure_errs
847902
bad_units = OS.fromList $ concat $ do
848903
x <- bagToList $ mapBag errMsgDiagnostic $ unionManyBags $ map Compat.getMessages closure_errs

ghcide/src/Development/IDE/GHC/Compat/Core.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,12 @@ import GHC.Unit.Module hiding (ModLocation (..), UnitId,
491491
toUnitId)
492492
import qualified GHC.Unit.Module as Module
493493
import GHC.Unit.State (ModuleOrigin (..))
494-
import GHC.Utils.Error (Severity (..), emptyMessages)
494+
import GHC.Utils.Error (Severity (..), emptyMessages
495+
#if MIN_VERSION_ghc(9,3,0)
496+
, mkPlainErrorMsgEnvelope)
497+
#else
498+
)
499+
#endif
495500
import GHC.Utils.Panic hiding (try)
496501
import qualified GHC.Utils.Panic.Plain as Plain
497502

0 commit comments

Comments
 (0)