Increase compiler modularity
(See also the wiki page https://gitlab.haskell.org/ghc/ghc/-/wikis/Make-GHC-codebase-more-modular)
This is a meta-ticket about the avoidance of direct access to compiler state (DynFlags
, HscEnv
, etc.). Many functions implicitly access DynFlags
to query command line options, package state, linker state, etc. making the code not modular.
As a stronger motivation: I want to fix #14335 (support for compiler plugins with cross-compilation). It should have been pretty straightforward to use two package states instead of one (one for the host compiler and another for the target code). Sadly this isn't currently possible because the package state stored in DynFlags
is accessed very indirectly (e.g. to get the effective wired-in unit ids on disk when pretty-printing messages for the user, etc.). There are a lot of other examples of this kind.
All the work done in this direction will be beneficial to make GHC multi-target, multi-session and to improve cross-compilation support (cf https://github.com/hsyl20/ghc-cross-compilation for what we would like to improve).
Three different but related goals regarding DynFlags:
- Make it clearer which bits of DynFlags are used where. Eg pass smaller record to SDoc. Ameliorates the symptoms of not having (2); and is generally a good idea.
- Stop making DynFlags depend on TcM etc. It should just be an abstract syntax tree. Move mutable state, caches &c, to Session (= HscEnv).
- Remove mutable state from DynFlags, and keep it somewhere else. But where? HscEnv?
Old Tasks:
-
Remove DynFlags
from pretty-printSDocContext
: #10143 (closed) -
Remove unsafeGlobalDynFlags
#14597 -
Add SDocContext instead of directly accessing DynFlags
inSDoc
-
Don't access the (target) package state when pretty-printing UnitId
,Module
, etc. -
Use Platform
instead ofDynFlags
in code generators -
Fix handling of -dppr-debug
-
Remove Outputable
instances for things that need some context to be printed -
Fix binary blob handling ( GHC.CmmToAsm.Ppr.pprBytes
) -
Don't store state into DynFlags. Use HscEnv or another env instead. -
Remove DumpFlags
fromLogger
(c.f.logHasDumpFlag
). They must be passed in each subsystem configuration explicitly instead. [Sylvain: it was easier to keep DumpFlags into the Logger to untangleLogger
and the rest ofDynFlags
first. But now we should complete this.]
DynFlags
and HscEnv
:
Current tasks, making subsystems not use A crude test is
git grep -l -E '(Driver.Session|DynFlags|dflags|HscEnv|hsc_env)' ':(exclude)compiler/GHC/Driver' compiler/
to find module in need of being fixed up.
Isolate DynFlags
module by module, phase by phase in the compiler. we'll try to proceed in this ordering (deepest part of the compiler up) based on the log file in @doyougnu's previous comments in #20730.
C-- & STG
-
CmmToLlvm
: merged: !7158 (closed) -
Cmm
: merged: !7199 (closed) -
Cmm.Parser
-
No more DynFlags
!8160 (merged) -
No more StgToCmmConfig
!8237
-
-
StgToCmm
: merged: !7325 (closed) -
Stg
: merged: !7479 (closed)
Core optimizations
Green modules
-
Core.Opt.Arity
: !8177 (closed)-
!8291 (closed) Follow up to make GHC.Driver.Config
module.
-
-
Core.Opt.WorkWrap
: !8294 (closed) -
Core.Opt.LiberateCase
: !8296 (closed) -
GHC.Core.Opt
remove someHscEnv
!7515 (merged)
Purple modules
Can do punting on the monad, perhaps:
-
Core.Lint
: !8293 (merged) -
CoreToStg.Prep
!7498 (merged) -
CoreToStg
The monad issue:
Better to do after:
-
Make Core.Lint.Interactive
: !8470 first commit -
Core.Opt
(a good many modules)-
No HscEnv
: !7845 (closed)
-
- ... re-evaluate once here ...
-
Iface.Tidy !7836 (closed) -
HsToCore
-
Coverage
noHscEnv
: !7467 (closed) -
Usage
noHscEnv
: !8231 (closed) -
No HscEnv
: !7845 (closed) -
Coverage
noDynFlags
: !7508 (merged) -
Usage
noHscEnv
!8231 (closed) -
No direct use of DynFlags in HsToCore: !12841
-
Misc
Linker modules except for GHC.Linker.Loader
seem like perhaps good "leaf" modules to tackle.
-
GHC.Linker
-
GHC.Linker.Dynamic
-
GHC.Linker.ExtraObj
-
GHC.Linker.Loader
Probably best to skip -
GHC.Linker.MacOS
-
GHC.Linker.Static
-
GHC.Linker.Static.Utils
-
GHC.Linker.Types
-
GHC.Linker.Unit
: !8370 (closed) -
GHC.Linker.Windows
-
GHC.SysTools.Process
: !11437 (closed)