Skip to content

Commit 43d704c

Browse files
committed
Properly recognize workfree enable signals (#1936)
isWorkFreeClockOrResetOrEnable tried to detect constant enable signals, but we forgot to update this when we changed the representation of Enable in #1368. This fixes the register duplication issue seen in #1935 (cherry picked from commit 0415bcf)
1 parent 3e6f36c commit 43d704c

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: Recognize enableGen as workfree and don't duplicate registers [#1935](https://github.com/clash-lang/clash-compiler/issues/1935)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ isWorkFreeClockOrResetOrEnable tcm e =
150150
case collectArgs e of
151151
(Prim p,_) -> Just (primName p == "Clash.Transformations.removedArg")
152152
(Var _, []) -> Just True
153-
(Data _, []) -> Just True -- For Enable True/False
153+
(Data _, [_dom, Left (stripTicks -> Data _)]) -> Just True -- For Enable True/False
154154
(Literal _,_) -> Just True
155155
_ -> Just False
156156
else

tests/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ runClashTest = defaultMain $ clashTestRoot
137137
[ clashTestGroup "netlist"
138138
[ clashLibTest ("tests" </> "shouldwork" </> "Netlist") allTargets [] "Identity" "main"
139139
, NEEDS_PRIMS(clashLibTest ("tests" </> "shouldwork" </> "Netlist") [VHDL] [] "NoDeDup" "main")
140+
, clashLibTest ("tests" </> "shouldwork" </> "Netlist") allTargets [] "T1935" "main"
140141
]
141142
, clashTestGroup "examples"
142143
[ runTest "ALU" def{hdlSim=False}

tests/shouldwork/Netlist/T1935.hs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{-# LANGUAGE OverloadedStrings #-}
2+
module T1935 where
3+
4+
import qualified Prelude as P
5+
6+
import Clash.Prelude
7+
8+
import Clash.Netlist.Types
9+
import Clash.Backend (Backend)
10+
11+
import Test.Tasty.Clash
12+
import Test.Tasty.Clash.NetlistTest
13+
14+
import Control.Monad (when)
15+
16+
topEntity
17+
:: Clock System
18+
-> Reset System
19+
-> Signal System (Unsigned 8)
20+
topEntity clk rst = withClockResetEnable clk rst enableGen x
21+
where
22+
x :: SystemClockResetEnable => Signal System (Unsigned 8)
23+
x = register 4 (x+1)
24+
25+
testPath :: FilePath
26+
testPath = "tests/shouldwork/Netlist/T1935.hs"
27+
28+
countRegisters :: Component -> Int
29+
countRegisters (Component _nm _inps _outs ds) =
30+
let regs = filter isRegister ds
31+
in P.length regs
32+
where
33+
isRegister (BlackBoxD nm _ _ _ _ _)
34+
| nm == "Clash.Signal.Internal.register#" = True
35+
isRegister _ = False
36+
37+
mainGeneric :: Backend (TargetToState target) => SBuildTarget target -> IO ()
38+
mainGeneric hdl = do
39+
netlist <- runToNetlistStage hdl id testPath
40+
let regs = sum $ fmap (countRegisters . snd) netlist
41+
when (regs /= 1) $ error ("Expected 1 register, but found: " <> show regs)
42+
43+
mainVHDL :: IO ()
44+
mainVHDL = mainGeneric SVHDL
45+
46+
mainVerilog :: IO ()
47+
mainVerilog = mainGeneric SVerilog
48+
49+
mainSystemVerilog :: IO ()
50+
mainSystemVerilog = mainGeneric SSystemVerilog

tests/src/Test/Tasty/Clash/NetlistTest.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
--
1717
module Test.Tasty.Clash.NetlistTest
1818
( runToNetlistStage
19+
, TargetToState
1920
) where
2021

2122
import qualified Prelude as P

0 commit comments

Comments
 (0)