Opened 4 months ago

Last modified 3 months ago

#14391 new task

Make the simplifier independent of the typechecker

Reported by: nomeata Owned by:
Priority: normal Milestone:
Component: Compiler Version: 8.3
Keywords: newcomer Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: None/Unknown Test Case:
Blocked By: Blocking:
Related Tickets: Differential Rev(s):
Wiki Page:


I noticed that the simplifier module depends on all of the type checker, and HsSyn stuff, and renamer stuff, which I found strange.

After a little investigation, it seems that the simplifier depends on CoreMonad, and that pulls some very few type-checker related things:

  1. import TcRnMonad        ( initTcForLookup )
    import {-# SOURCE #-} TcSplice ( lookupThName_maybe )


thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
thNameToGhcName th_name = do
    hsc_env <- getHscEnv
    liftIO $ initTcForLookup hsc_env (lookupThName_maybe th_name)

which is not even used in GHC, but only in GHC Plugins, so this could probably be moved to a separate module pulled in by GhcPlugins.hs


import TcEnv            ( lookupGlobal )


instance MonadThings CoreM where
    lookupThing name = do { hsc_env <- getHscEnv
                          ; liftIO $ lookupGlobal hsc_env name }

This might be a bit harder to disentangle. But if successful, it would probably make building GHC in parallel quite a bit faster. And it just seems strange to me that the Core-to-Core code should depend on the type checker…

Simon says:

Both of these code paths go through initTcForLookup which is massive overkill, as the comment with TcEnv.lookupGlobal says. There's clearly a ToDo here to strip off the redundant stuff and do a minimal lookup.

I am optimistically marking this as newcomer because it is a refactoring task, and a good way of learning a bit about various pieces, with a reasonably clear notion of “success”.

Change History (6)

comment:1 Changed 3 months ago by jkiefer

Hello. Am newcomer. I'd love to take a stab at this! Will start hacking away.

comment:2 Changed 3 months ago by bgamari

Great! Do let us know if you encounter trouble.

comment:3 Changed 3 months ago by simonpj


I suggest you start with TcEnv.lookupGlobal.

  • It invokes initTcForLookup (massive overkill) in order to call tcLookupGlobal. We need a versionn of this function that operates in the IO monad, not the TcM monad.
  • What does tcLookupGlobal get from the TcM monad? It'll need to get these things as explicit arguments instead, I guess. For example: consults the tcg_type_env, which was initialised by initTcForLookup. And it uses tcg_semantic_mod likewise.
  • Then it hands off to LoadIface.tcLookupImported_maybe. That does a bit more IO-ish things before finally deciding to load a new interface decl in tcImportDecl_maybe.
  • tcImportDecl_maybe uses initIfaceTcRn to make an IfM moand in which to do the loading work. But instesad you can make an IfM from scratch, by writing a variant of initIfaceTcRn.

Nothing really hard here, but you'll need to carefully tease out what dependencies are where. (The lack of explicit dependencies is, of course, both the blessing and the curse of monadic progrmaming.)

Happy to help.

comment:4 Changed 3 months ago by jkiefer

Alrighty! First question while I investigate more into performing a minimal lookup instead of using initTcForLookup.

(Bear with me, I'm writing some of this out as I understand it myself)

We want to refactor thNameToGhcName to no longer be a part of CoreMonad and instead exist in a module to be pulled by GhcPlugins.hs.

Do correct me if I'm wrong, but creating a singleton module with just thNameToGhcName in it seems like a naive approach. Is there an existing module that is pulled in by GhcPlugins.hsthat would be an appropriate home for thNameToGhcName? Or should a new one simply be created?

comment:5 Changed 3 months ago by simonpj

I suggest you start with TcEnv.lookupGlobal.

Doing this will avoid you getting sucked into template haskell stuff right away. We can come back to your question when this is done.

comment:6 in reply to:  5 Changed 3 months ago by jkiefer

Gotcha. Thank you for the guidance!

Note: See TracTickets for help on using tickets.