Skip to content

Commit e4520b3

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 e4520b3

File tree

14 files changed

+441
-255
lines changed

14 files changed

+441
-255
lines changed

Clash.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ doHDL b src = do
6363

6464
generateHDL (buildCustomReprs reprs) domainConfs bindingsMap (Just b) primMap tcm tupTcm
6565
(ghcTypeToHWType WORD_SIZE_IN_BITS True) evaluator topEntities Nothing
66-
defClashOpts{opt_cachehdl = False, opt_dbgLevel = DebugSilent, opt_clear = True}
66+
defClashOpts{opt_cachehdl = False, opt_debug = debugSilent, opt_clear = True}
6767
(startTime,prepTime)
6868

6969
main :: IO ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CHANGED: Clash now supports more expressive debug options at the command line [#1800](https://github.com/clash-lang/clash-compiler/issues/1800).
2+
3+
With the old `DebugLevel` type for setting debug options, it was not possible to set certain debug options without implying others, i.e. counting transformations was not possible without also printing at least the final normalized core for a term. It is now possible to set options individually with new flags:
4+
5+
* -fclash-debug-invariants to check invariants and print warnings / errors
6+
* -fclash-debug-info to choose how much information to show about individual transformations
7+
* -fclash-debug-count-transformations to print a tally of each transformation applied
8+
9+
The old -fclash-debug flag is still available for backwards compatibility, and each `DebugLevel` is now a synonym for setting these options together.
10+

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

+92-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{-|
22
Copyright : (C) 2015-2016, University of Twente,
3-
2016-2017, Myrtle Software Ltd
3+
2016-2017, Myrtle Software Ltd,
4+
2021, QBayLogic B.V.
45
License : BSD2 (see the file LICENSE)
5-
Maintainer : Christiaan Baaij <christiaan.baaij@gmail.com>
6+
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
67
-}
78

89
{-# LANGUAGE CPP #-}
@@ -54,9 +55,12 @@ parseClashFlagsFull flagsAvialable args = do
5455
flagsClash :: IORef ClashOpts -> [Flag IO]
5556
flagsClash r = [
5657
defFlag "fclash-debug" $ SepArg (setDebugLevel r)
58+
, defFlag "fclash-debug-info" $ SepArg (setDebugInfo r)
59+
, defFlag "fclash-debug-invariants" $ NoArg (liftEwM (setDebugInvariants r))
60+
, defFlag "fclash-debug-count-transformations" $ NoArg (liftEwM (setDebugCountTransformations r))
5761
, defFlag "fclash-debug-transformations" $ SepArg (setDebugTransformations r)
58-
, defFlag "fclash-debug-transformations-from" $ OptIntSuffix (setDebugTransformationsFrom r)
59-
, defFlag "fclash-debug-transformations-limit" $ OptIntSuffix (setDebugTransformationsLimit r)
62+
, defFlag "fclash-debug-transformations-from" $ IntSuffix (setDebugTransformationsFrom r)
63+
, defFlag "fclash-debug-transformations-limit" $ IntSuffix (setDebugTransformationsLimit r)
6064
, defFlag "fclash-debug-history" $ AnySuffix (liftEwM . (setRewriteHistoryFile r))
6165
, defFlag "fclash-hdldir" $ SepArg (setHdlDir r)
6266
, defFlag "fclash-hdlsyn" $ SepArg (setHdlSyn r)
@@ -136,31 +140,92 @@ setSpecLimit :: IORef ClashOpts
136140
-> IO ()
137141
setSpecLimit r n = modifyIORef r (\c -> c {opt_specLimit = n})
138142

143+
setDebugInvariants :: IORef ClashOpts -> IO ()
144+
setDebugInvariants r =
145+
modifyIORef r $ \c ->
146+
c { opt_debug = (opt_debug c) { dbg_invariants = True } }
147+
148+
setDebugCountTransformations :: IORef ClashOpts -> IO ()
149+
setDebugCountTransformations r =
150+
modifyIORef r $ \c ->
151+
c { opt_debug = (opt_debug c) { dbg_countTransformations = True } }
152+
139153
setDebugTransformations :: IORef ClashOpts -> String -> EwM IO ()
140154
setDebugTransformations r s =
141-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformations = transformations}))
155+
liftEwM (modifyIORef r (setTransformations transformations))
142156
where
143157
transformations = Set.fromList (filter (not . null) (map trim (splitOn "," s)))
144158
trim = dropWhileEnd isSpace . dropWhile isSpace
145159

146-
setDebugTransformationsFrom :: IORef ClashOpts -> Maybe Int -> EwM IO ()
147-
setDebugTransformationsFrom r (Just n) =
148-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformationsFrom = n}))
149-
setDebugTransformationsFrom _r Nothing = pure ()
150-
151-
setDebugTransformationsLimit :: IORef ClashOpts -> Maybe Int -> EwM IO ()
152-
setDebugTransformationsLimit r (Just n) =
153-
liftEwM (modifyIORef r (\c -> c {opt_dbgTransformationsLimit = n}))
154-
setDebugTransformationsLimit _r Nothing = pure ()
155-
156-
setDebugLevel :: IORef ClashOpts
157-
-> String
158-
-> EwM IO ()
159-
setDebugLevel r s = case readMaybe s of
160-
Just dbgLvl -> liftEwM $ do
161-
modifyIORef r (\c -> c {opt_dbgLevel = dbgLvl})
162-
when (dbgLvl > DebugNone) $ setNoCache r -- when debugging disable cache
163-
Nothing -> addWarn (s ++ " is an invalid debug level")
160+
setTransformations xs opts =
161+
opts { opt_debug = (opt_debug opts) { dbg_transformations = xs } }
162+
163+
setDebugTransformationsFrom :: IORef ClashOpts -> Int -> EwM IO ()
164+
setDebugTransformationsFrom r n =
165+
liftEwM (modifyIORef r (setFrom (fromIntegral n)))
166+
where
167+
setFrom from opts =
168+
opts { opt_debug = (opt_debug opts) { dbg_transformationsFrom = Just from } }
169+
170+
setDebugTransformationsLimit :: IORef ClashOpts -> Int -> EwM IO ()
171+
setDebugTransformationsLimit r n =
172+
liftEwM (modifyIORef r (setLimit (fromIntegral n)))
173+
where
174+
setLimit limit opts =
175+
opts { opt_debug = (opt_debug opts) { dbg_transformationsLimit = Just limit } }
176+
177+
setDebugLevel :: IORef ClashOpts -> String -> EwM IO ()
178+
setDebugLevel r s =
179+
case s of
180+
"DebugNone" ->
181+
liftEwM $ modifyIORef r (setLevel debugNone)
182+
"DebugSilent" ->
183+
liftEwM $ do
184+
modifyIORef r (setLevel debugSilent)
185+
setNoCache r
186+
"DebugFinal" ->
187+
liftEwM $ do
188+
modifyIORef r (setLevel debugFinal)
189+
setNoCache r
190+
"DebugCount" ->
191+
liftEwM $ do
192+
modifyIORef r (setLevel debugCount)
193+
setNoCache r
194+
"DebugName" ->
195+
liftEwM $ do
196+
modifyIORef r (setLevel debugName)
197+
setNoCache r
198+
"DebugTry" ->
199+
liftEwM $ do
200+
modifyIORef r (setLevel debugTry)
201+
setNoCache r
202+
"DebugApplied" ->
203+
liftEwM $ do
204+
modifyIORef r (setLevel debugApplied)
205+
setNoCache r
206+
"DebugAll" ->
207+
liftEwM $ do
208+
modifyIORef r (setLevel debugAll)
209+
setNoCache r
210+
_ ->
211+
addWarn (s ++ " is an invalid debug level")
212+
where
213+
setLevel lvl opts =
214+
opts { opt_debug = lvl }
215+
216+
setDebugInfo :: IORef ClashOpts -> String -> EwM IO ()
217+
setDebugInfo r s =
218+
case readMaybe s of
219+
Just info ->
220+
liftEwM $ do
221+
modifyIORef r (setInfo info)
222+
when (info /= None) (setNoCache r)
223+
224+
Nothing ->
225+
addWarn (s ++ " is an invalid debug info")
226+
where
227+
setInfo info opts =
228+
opts { opt_debug = (opt_debug opts) { dbg_transformationInfo = info } }
164229

165230
setNoCache :: IORef ClashOpts -> IO ()
166231
setNoCache r = modifyIORef r (\c -> c {opt_cachehdl = False})
@@ -251,4 +316,7 @@ setRewriteHistoryFile r arg = do
251316
let fileNm = case drop (length "-fclash-debug-history=") arg of
252317
[] -> "history.dat"
253318
str -> str
254-
modifyIORef r (\c -> c {opt_dbgRewriteHistoryFile = Just fileNm})
319+
modifyIORef r (setFile fileNm)
320+
where
321+
setFile file opts =
322+
opts { opt_debug = (opt_debug opts) { dbg_historyFile = Just file } }

clash-lib/src/Clash/Driver.hs

+1-1
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 dbg_historyFile (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

+5-3
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+
{ dbg_invariants = False
376+
, dbg_transformations = Set.empty
377+
, dbg_historyFile = Nothing
378+
}
377379

378380
-- 2. Caching
379381
, opt_cachehdl = True

0 commit comments

Comments
 (0)