Skip to content

Commit f7d9044

Browse files
author
Alex McKenna
committed
Move debug options to new DebugOpts type
The debugging options for the compiler are often used together, yet are passed individually to the rewrite system. Keeping debugging options together helps expose less unnecessary detail.
1 parent ee738e8 commit f7d9044

File tree

11 files changed

+142
-115
lines changed

11 files changed

+142
-115
lines changed

clash-ghc/src-ghc/Clash/GHC/ClashFlags.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,29 +138,43 @@ setSpecLimit r n = modifyIORef r (\c -> c {opt_specLimit = n})
138138

139139
setDebugTransformations :: IORef ClashOpts -> String -> EwM IO ()
140140
setDebugTransformations r s =
141-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformations = transformations}))
141+
liftEwM (modifyIORef r (setTransformations transformations))
142142
where
143143
transformations = Set.fromList (filter (not . null) (map trim (splitOn "," s)))
144144
trim = dropWhileEnd isSpace . dropWhile isSpace
145145

146+
setTransformations xs opts =
147+
opts { opt_debug = (opt_debug opts) { opt_transformations = xs } }
148+
146149
setDebugTransformationsFrom :: IORef ClashOpts -> Maybe Int -> EwM IO ()
147150
setDebugTransformationsFrom r (Just n) =
148-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformationsFrom = n}))
151+
liftEwM (modifyIORef r (setFrom (fromIntegral n)))
152+
where
153+
setFrom from opts =
154+
opts { opt_debug = (opt_debug opts) { opt_transformationsFrom = from } }
155+
149156
setDebugTransformationsFrom _r Nothing = pure ()
150157

151158
setDebugTransformationsLimit :: IORef ClashOpts -> Maybe Int -> EwM IO ()
152159
setDebugTransformationsLimit r (Just n) =
153-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformationsLimit = n}))
160+
liftEwM (modifyIORef r (setLimit (fromIntegral n)))
161+
where
162+
setLimit limit opts =
163+
opts { opt_debug = (opt_debug opts) { opt_transformationsLimit = limit } }
164+
154165
setDebugTransformationsLimit _r Nothing = pure ()
155166

156167
setDebugLevel :: IORef ClashOpts
157168
-> String
158169
-> EwM IO ()
159170
setDebugLevel r s = case readMaybe s of
160171
Just dbgLvl -> liftEwM $ do
161-
modifyIORef r (\c -> c {opt_dbgLevel = dbgLvl})
172+
modifyIORef r (setLevel dbgLvl)
162173
when (dbgLvl > DebugNone) $ setNoCache r -- when debugging disable cache
163174
Nothing -> addWarn (s ++ " is an invalid debug level")
175+
where
176+
setLevel lvl opts =
177+
opts { opt_debug = (opt_debug opts) { opt_level = lvl } }
164178

165179
setNoCache :: IORef ClashOpts -> IO ()
166180
setNoCache r = modifyIORef r (\c -> c {opt_cachehdl = False})
@@ -251,4 +265,7 @@ setRewriteHistoryFile r arg = do
251265
let fileNm = case drop (length "-fclash-debug-history=") arg of
252266
[] -> "history.dat"
253267
str -> str
254-
modifyIORef r (\c -> c {opt_dbgRewriteHistoryFile = Just fileNm})
268+
modifyIORef r (setFile fileNm)
269+
where
270+
setFile file opts =
271+
opts { opt_debug = (opt_debug opts) { opt_rewriteHistoryFile = Just file } }

clash-lib/src/Clash/Driver.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ generateHDL
292292
-> IO ()
293293
generateHDL reprs domainConfs bindingsMap hdlState primMap tcm tupTcm typeTrans eval
294294
topEntities0 mainTopEntity opts (startTime,prepTime) = do
295-
case opt_dbgRewriteHistoryFile opts of
295+
case opt_rewriteHistoryFile (opt_debug opts) of
296296
Nothing -> pure ()
297297
Just histFile -> whenM (Directory.doesFileExist histFile) (Directory.removeFile histFile)
298298
let (tes, deps) = sortTop bindingsMap topEntities1

clash-lib/src/Clash/Driver/Manifest.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,11 @@ readFreshManifest tops (bindingsMap, topId) primMap opts@(ClashOpts{..}) clashMo
371371
-- Ignore the following settings, they don't affect the generated HDL:
372372

373373
-- 1. Debug
374-
opt_dbgLevel = DebugNone
375-
, opt_dbgTransformations = Set.empty
376-
, opt_dbgRewriteHistoryFile = Nothing
374+
opt_debug = opt_debug
375+
{ opt_level = DebugNone
376+
, opt_transformations = Set.empty
377+
, opt_rewriteHistoryFile = Nothing
378+
}
377379

378380
-- 2. Caching
379381
, opt_cachehdl = True

clash-lib/src/Clash/Driver/Types.hs

Lines changed: 51 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,53 @@ data DebugLevel
9898
-- ^ Show all sub-expressions on which a rewrite is attempted
9999
deriving (Eq,Ord,Read,Enum,Generic,Hashable)
100100

101+
-- | Options related to debugging. See 'ClashOpts'
102+
data DebugOpts = DebugOpts
103+
{ opt_level :: DebugLevel
104+
-- ^ Set the debugging mode for the compiler, exposing additional output. See
105+
-- "DebugLevel" for the available options.
106+
--
107+
-- Command line flag: -fclash-debug
108+
, opt_transformations :: Set.Set String
109+
-- ^ List the transformations that are to be debugged.
110+
--
111+
-- Command line flag: -fclash-debug-transformations
112+
, opt_transformationsFrom :: Word
113+
-- ^ Only output debug information from (applied) transformation n
114+
--
115+
-- Command line flag: -fclash-debug-transformations-from
116+
, opt_transformationsLimit :: Word
117+
-- ^ Only output debug information for n (applied) transformations. If this
118+
-- limit is exceeded, Clash will stop normalizing.
119+
--
120+
-- Command line flag: -fclash-debug-transformations-limit
121+
, opt_rewriteHistoryFile :: Maybe FilePath
122+
-- ^ Save all applied rewrites to a file
123+
--
124+
-- Command line flag: -fclash-debug-history
125+
}
126+
127+
instance Hashable DebugOpts where
128+
hashWithSalt s DebugOpts{..} =
129+
s `hashWithSalt`
130+
opt_level `hashSet`
131+
opt_transformations `hashWithSalt`
132+
opt_transformationsFrom `hashWithSalt`
133+
opt_transformationsLimit `hashWithSalt`
134+
opt_rewriteHistoryFile
135+
where
136+
hashSet = Set.foldl' hashWithSalt
137+
infixl 0 `hashSet`
138+
139+
defDebugOpts :: DebugOpts
140+
defDebugOpts = DebugOpts
141+
{ opt_level = DebugNone
142+
, opt_transformations = Set.empty
143+
, opt_transformationsFrom = 0
144+
, opt_transformationsLimit = maxBound
145+
, opt_rewriteHistoryFile = Nothing
146+
}
147+
101148
-- | Options passed to Clash compiler
102149
data ClashOpts = ClashOpts
103150
{ opt_inlineLimit :: Int
@@ -124,30 +171,8 @@ data ClashOpts = ClashOpts
124171
-- of zero means no potentially non-terminating binding is unfolded.
125172
--
126173
-- Command line flag: -fclash-evaluator-fuel-limit
127-
, opt_dbgLevel :: DebugLevel
128-
-- ^ Set the debugging mode for the compiler, exposing additional output. See
129-
-- "DebugLevel" for the available options.
130-
--
131-
-- Command line flag: -fclash-debug
132-
, opt_dbgTransformations :: Set.Set String
133-
-- ^ List the transformations that are to be debugged.
134-
--
135-
-- Command line flag: -fclash-debug-transformations
136-
, opt_dbgTransformationsFrom :: Word
137-
-- ^ Only output debug information from (applied) transformation n
138-
--
139-
-- Command line flag: -fclash-debug-transformations-from
140-
, opt_dbgTransformationsLimit :: Word
141-
-- ^ Only output debug information for n (applied) transformations. If this
142-
-- limit is exceeded, Clash will stop normalizing.
143-
--
144-
-- Command line flag: -fclash-debug-transformations-limit
145-
146-
, opt_dbgRewriteHistoryFile :: Maybe FilePath
147-
-- ^ Save all applied rewrites to a file
148-
--
149-
-- Command line flag: -fclash-debug-history
150-
174+
, opt_debug :: DebugOpts
175+
-- ^ Options which control debugging. See 'DebugOpts'.
151176
, opt_cachehdl :: Bool
152177
-- ^ Reuse previously generated output from Clash. Only caches topentities.
153178
--
@@ -231,11 +256,6 @@ instance Hashable ClashOpts where
231256
opt_inlineFunctionLimit `hashWithSalt`
232257
opt_inlineConstantLimit `hashWithSalt`
233258
opt_evaluatorFuelLimit `hashWithSalt`
234-
opt_dbgLevel `hashSet`
235-
opt_dbgTransformations `hashWithSalt`
236-
opt_dbgTransformationsFrom `hashWithSalt`
237-
opt_dbgTransformationsLimit `hashWithSalt`
238-
opt_dbgRewriteHistoryFile `hashWithSalt`
239259
opt_cachehdl `hashWithSalt`
240260
opt_clear `hashWithSalt`
241261
opt_primWarn `hashOverridingBool`
@@ -264,23 +284,15 @@ instance Hashable ClashOpts where
264284
hashOverridingBool s1 Never = hashWithSalt s1 (2 :: Int)
265285
infixl 0 `hashOverridingBool`
266286

267-
hashSet :: Hashable a => Int -> Set.Set a -> Int
268-
hashSet = Set.foldl' hashWithSalt
269-
infixl 0 `hashSet`
270-
271287
defClashOpts :: ClashOpts
272288
defClashOpts
273289
= ClashOpts
274-
{ opt_dbgLevel = DebugNone
275-
, opt_dbgRewriteHistoryFile = Nothing
276-
, opt_dbgTransformations = Set.empty
277-
, opt_dbgTransformationsFrom = 0
278-
, opt_dbgTransformationsLimit = maxBound
279-
, opt_inlineLimit = 20
290+
{ opt_inlineLimit = 20
280291
, opt_specLimit = 20
281292
, opt_inlineFunctionLimit = 15
282293
, opt_inlineConstantLimit = 0
283294
, opt_evaluatorFuelLimit = 20
295+
, opt_debug = defDebugOpts
284296
, opt_cachehdl = True
285297
, opt_clear = False
286298
, opt_primWarn = True

clash-lib/src/Clash/Normalize.hs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import Clash.Core.VarEnv
6565
mkVarEnv, mkVarSet, notElemVarEnv, notElemVarSet, nullVarEnv, unionVarEnv)
6666
import Clash.Debug (traceIf)
6767
import Clash.Driver.Types
68-
(BindingMap, Binding(..), ClashOpts (..), DebugLevel (..))
68+
(BindingMap, Binding(..), ClashOpts (..), DebugLevel (..), DebugOpts(..))
6969
import Clash.Netlist.Types
7070
(HWMap, FilteredHWType(..))
7171
import Clash.Netlist.Util
@@ -77,7 +77,7 @@ import Clash.Normalize.Util
7777
import Clash.Primitives.Types (CompiledPrimMap)
7878
import Clash.Rewrite.Combinators ((>->),(!->),repeatR,topdownR)
7979
import Clash.Rewrite.Types
80-
(RewriteEnv (..), RewriteState (..), bindings, dbgLevel, dbgRewriteHistoryFile, extra,
80+
(RewriteEnv (..), RewriteState (..), bindings, debugOpts, extra,
8181
tcCache, topEntities)
8282
import Clash.Rewrite.Util
8383
(apply, isUntranslatableType, runRewriteSession)
@@ -123,11 +123,7 @@ runNormalization opts supply globals typeTrans reprs tcm tupTcm eval primMap rcs
123123
= runRewriteSession rwEnv rwState
124124
where
125125
rwEnv = RewriteEnv
126-
(opt_dbgLevel opts)
127-
(opt_dbgTransformations opts)
128-
(opt_dbgTransformationsFrom opts)
129-
(opt_dbgTransformationsLimit opts)
130-
(opt_dbgRewriteHistoryFile opts)
126+
(opt_debug opts)
131127
(opt_aggressiveXOpt opts)
132128
typeTrans
133129
tcm
@@ -238,7 +234,8 @@ normalize' nm = do
238234
-- for the ByteArray# inside of a Natural constant.
239235
-- (GHC-8.4 does this with tests/shouldwork/Numbers/Exp.hs)
240236
-- It will later be inlined by flattenCallTree.
241-
lvl <- Lens.view dbgLevel
237+
opts <- Lens.view debugOpts
238+
let lvl = opt_level opts
242239
traceIf (lvl > DebugNone)
243240
(concat [$(curLoc), "Expr belonging to bndr: ", nmS, " (:: "
244241
, showPpr (varType nm')
@@ -377,7 +374,8 @@ flattenCallTree (CBranch (nm,(Binding nm' sp inl pr tm)) used) = do
377374
let tm1 = substTm "flattenCallTree.flattenExpr" subst tm
378375

379376
-- NB: When -fclash-debug-history is on, emit binary data holding the recorded rewrite steps
380-
rewriteHistFile <- Lens.view dbgRewriteHistoryFile
377+
opts <- Lens.view debugOpts
378+
let rewriteHistFile = opt_rewriteHistoryFile opts
381379
when (Maybe.isJust rewriteHistFile) $
382380
let !_ = unsafePerformIO
383381
$ BS.appendFile (Maybe.fromJust rewriteHistFile)

clash-lib/src/Clash/Normalize/Transformations/Case.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ import Clash.Core.VarEnv
6767
( InScopeSet, elemVarSet, extendInScopeSet, extendInScopeSetList, mkVarSet
6868
, unitVarSet, uniqAway)
6969
import Clash.Debug (traceIf)
70-
import Clash.Driver.Types (DebugLevel(..))
70+
import Clash.Driver.Types (DebugLevel(..), DebugOpts(opt_level))
7171
import Clash.Netlist.Types (FilteredHWType(..), HWType(..))
7272
import Clash.Netlist.Util (coreTypeToHWType, representableType)
7373
import qualified Clash.Normalize.Primitives as NP (undefined)
7474
import Clash.Normalize.Types (NormRewrite, NormalizeSession)
7575
import Clash.Rewrite.Combinators ((>-!))
7676
import Clash.Rewrite.Types
77-
( TransformContext(..), bindings, customReprs, dbgLevel, tcCache
77+
( TransformContext(..), bindings, customReprs, debugOpts, tcCache
7878
, typeTranslator, workFreeBinders)
7979
import Clash.Rewrite.Util (apply, changed, isFromInt, whnfRW)
8080
import Clash.Rewrite.WorkFree
@@ -286,7 +286,8 @@ caseCon' ctx@(TransformContext is0 _) e@(Case subj ty alts) = do
286286
let ret = caseOneAlt e
287287
-- Otherwise check whether the entire case-expression has a single
288288
-- alternative, and pick that one.
289-
lvl <- Lens.view dbgLevel
289+
opts <- Lens.view debugOpts
290+
let lvl = opt_level opts
290291
if lvl > DebugNone then do
291292
let subjIsConst = isConstant subj
292293
-- In debug mode we always report missing evaluation rules for the

clash-lib/src/Clash/Normalize/Transformations/Inline.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ import Clash.Core.VarEnv
7373
, foldlWithUniqueVarEnv', lookupVarEnv, lookupVarEnvDirectly, mkVarEnv
7474
, notElemVarSet, unionVarEnv, unionVarEnvWith, unitVarSet)
7575
import Clash.Debug (trace, traceIf)
76-
import Clash.Driver.Types (Binding(..), DebugLevel(..))
76+
import Clash.Driver.Types (Binding(..), DebugLevel(..), DebugOpts(opt_level))
7777
import Clash.Netlist.Util (representableType)
7878
import Clash.Primitives.Types
7979
(CompiledPrimMap, Primitive(..), TemplateKind(..))
8080
import Clash.Rewrite.Combinators (allR)
8181
import Clash.Rewrite.Types
82-
( TransformContext(..), bindings, curFun, customReprs, dbgLevel, extra
82+
( TransformContext(..), bindings, curFun, customReprs, debugOpts, extra
8383
, tcCache, topEntities, typeTranslator)
8484
import Clash.Rewrite.Util
8585
( changed, inlineBinders, inlineOrLiftBinders, isJoinPointIn
@@ -392,7 +392,8 @@ inlineHO _ e@(App _ _)
392392
limit <- Lens.use (extra.inlineLimit)
393393
if (Maybe.fromMaybe 0 isInlined) > limit
394394
then do
395-
lvl <- Lens.view dbgLevel
395+
opts <- Lens.view debugOpts
396+
let lvl = opt_level opts
396397
traceIf (lvl > DebugNone) ($(curLoc) ++ "InlineHO: " ++ show f ++ " already inlined " ++ show limit ++ " times in:" ++ show cf) (return e)
397398
else do
398399
bodyMaybe <- lookupVarEnv f <$> Lens.use bindings

clash-lib/src/Clash/Normalize/Util.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ import Clash.Core.VarEnv
7777
(VarEnv, emptyInScopeSet, emptyVarEnv, extendVarEnv, extendVarEnvWith,
7878
lookupVarEnv, unionVarEnvWith, unitVarEnv, extendInScopeSetList)
7979
import Clash.Debug (traceIf)
80-
import Clash.Driver.Types (BindingMap, Binding(..), DebugLevel (..))
80+
import Clash.Driver.Types (BindingMap, Binding(..), DebugLevel (..), DebugOpts(..))
8181
import Clash.Normalize.Primitives (removedArg)
8282
import {-# SOURCE #-} Clash.Normalize.Strategy (normalization)
8383
import Clash.Normalize.Types
8484
import Clash.Primitives.Util (constantArgs)
8585
import Clash.Rewrite.Types
86-
(RewriteMonad, TransformContext(..), bindings, curFun, dbgLevel, extra,
86+
(RewriteMonad, TransformContext(..), bindings, curFun, debugOpts, extra,
8787
tcCache)
8888
import Clash.Rewrite.Util
8989
(runRewrite, specialise, mkTmBinderFor, mkDerivedName)
@@ -511,7 +511,8 @@ rewriteExpr :: (String,NormRewrite) -- ^ Transformation to apply
511511
-> NormalizeSession Term
512512
rewriteExpr (nrwS,nrw) (bndrS,expr) (nm, sp) = do
513513
curFun .= (nm, sp)
514-
lvl <- Lens.view dbgLevel
514+
opts <- Lens.view debugOpts
515+
let lvl = opt_level opts
515516
let before = showPpr expr
516517
let expr' = traceIf (lvl >= DebugFinal)
517518
(bndrS ++ " before " ++ nrwS ++ ":\n\n" ++ before ++ "\n")

clash-lib/src/Clash/Rewrite/Types.hs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import Data.Hashable (Hashable)
3636
import Data.HashMap.Strict (HashMap)
3737
import Data.IntMap.Strict (IntMap)
3838
import Data.Monoid (Any)
39-
import qualified Data.Set as Set
4039
import Data.Text (Text)
4140
import GHC.Generics
4241

@@ -52,7 +51,7 @@ import Clash.Core.Type (Type)
5251
import Clash.Core.TyCon (TyConName, TyConMap)
5352
import Clash.Core.Var (Id)
5453
import Clash.Core.VarEnv (InScopeSet, VarSet, VarEnv)
55-
import Clash.Driver.Types (BindingMap, DebugLevel)
54+
import Clash.Driver.Types (BindingMap, DebugOpts)
5655
import Clash.Netlist.Types (FilteredHWType, HWMap)
5756
import Clash.Rewrite.WorkFree (isWorkFree)
5857
import Clash.Util
@@ -111,16 +110,8 @@ Lens.makeLenses ''RewriteState
111110
-- | Read-only environment of a rewriting session
112111
data RewriteEnv
113112
= RewriteEnv
114-
{ _dbgLevel :: DebugLevel
115-
-- ^ Level at which we print debugging messages
116-
, _dbgTransformations :: Set.Set String
117-
-- ^ See ClashOpts.dbgTransformations
118-
, _dbgTransformationsFrom :: Word
119-
-- ^ See ClashOpts.opt_dbgTransformationsFrom
120-
, _dbgTransformationsLimit :: Word
121-
-- ^ See ClashOpts.opt_dbgTransformationsLimit
122-
, _dbgRewriteHistoryFile :: Maybe FilePath
123-
-- ^ See ClashOpts.opt_dbgRewriteHistory
113+
{ _debugOpts :: DebugOpts
114+
-- ^ Options for debugging during rewriting
124115
, _aggressiveXOpt :: Bool
125116
-- ^ Transformations to print debugging info for
126117
, _typeTranslator :: CustomReprs

0 commit comments

Comments
 (0)