clash-lang / clash-compiler

Haskell to VHDL/Verilog/SystemVerilog compiler
https://clash-lang.org/
Other
1.42k stars 150 forks source link

Template Haskell code is run twice on GHC 9.4 and higher #2522

Closed christiaanb closed 1 year ago

christiaanb commented 1 year ago

Given:

module Test where

import Clash.Prelude
import Language.Haskell.TH
import Language.Haskell.TH.Syntax

topEntity :: Integer
topEntity =
  $(do qRunIO (putStrLn "Hello!")
       litE (integerL 42))

on GHC 9.2.8 we get:

$ cabal run clash -- --vhdl -fclash-no-cache Test.hs 
Up to date
Loaded package environment from /home/christiaan/devel/clash-compiler/.ghc.environment.x86_64-linux-9.2.8
GHC: Setting up GHC took: 0.189s
Hello!
GHC: Compiling and loading modules took: 0.114s
Clash: Parsing and compiling primitives took 0.212s
GHC+Clash: Loading modules cumulatively took 0.575s
Clash: Ignoring previously made caches
Clash: Compiling Test.topEntity
Clash: Normalization took 0.000s
Clash: Netlist generation took 0.000s
Clash: Compiling Test.topEntity took 0.006s
Clash: Total compilation took 0.582s

but on GHC 9.4.5 we get:

$ cabal run clash -- --vhdl -fclash-no-cache Test.hs 
Up to date
Loaded package environment from /home/christiaan/devel/clash-compiler/.ghc.environment.x86_64-linux-9.4.5
GHC: Setting up GHC took: 0.392s
Hello!
Hello!
GHC: Compiling and loading modules took: 0.275s
Clash: Parsing and compiling primitives took 0.199s
GHC+Clash: Loading modules cumulatively took 0.930s
Clash: Ignoring previously made caches
Clash: Compiling Test.topEntity
Clash: Normalization took 0.000s
Clash: Netlist generation took 0.000s
Clash: Compiling Test.topEntity took 0.005s
Clash: Total compilation took 0.936s

The culprit is that when upgrading to GHC 9.4 I couldn't figure out how to deal with the loss of GHC.loadModule: https://github.com/clash-lang/clash-compiler/blob/3db6cf04c3bfcea777f45da0a4ff85519f5a14c6/clash-ghc/src-ghc/Clash/GHC/LoadModules.hs#L410-L422

This might also be the underlying reason for #2504

martijnbastiaan commented 1 year ago

As a workaround for the slowness: we can put affected modules in the LoadModules corner.