Skip to content

Commit c52cf26

Browse files
committed
FIXED: HO blackboxes not propagating usage metadata for generated results
1 parent b1b1477 commit c52cf26

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: HO blackboxes not propagating usage metadata for generated results [#3147](https://github.com/clash-lang/clash-compiler/issues/3141)

clash-lib/src/Clash/Primitives/DSL.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{-|
22
Copyright : (C) 2019, Myrtle Software Ltd.
3-
2020-2024, QBayLogic B.V.
3+
2020-2026, QBayLogic B.V.
44
2021, Myrtle.ai
55
2022-2023, Google Inc
66
License : BSD2 (see the file LICENSE)
@@ -102,7 +102,7 @@ import Data.IntMap (IntMap)
102102
import qualified Data.IntMap as IntMap
103103
import Data.List (intersperse)
104104
import Data.List.Extra (zipEqual)
105-
import Data.Maybe (fromMaybe)
105+
import Data.Maybe (fromMaybe, listToMaybe)
106106
import Data.Monoid (Ap(getAp))
107107
import Data.Semigroup hiding (Product)
108108
import Data.String
@@ -835,6 +835,10 @@ instHO bbCtx fPos (resTy, bbResTy) argsWithTypes = do
835835

836836
resName <- declare' (ctxName <> "_" <> "ho" <> showt fPos <> "_"
837837
<> showt fSubPos <> "_res") resTy
838+
-- Pick the fSubPos-th HO instantiation (if present) and reuse its usage.
839+
case IntMap.lookup fPos (bbFunctions bbCtx) >>= listToMaybe . drop fSubPos of
840+
Just (_, usage, _, _, _, _) -> declareUseOnce usage resName
841+
Nothing -> pure ()
838842
let res = ([Text (Id.toLazyText resName)], bbResTy)
839843

840844
-- Render HO argument to plain text

tests/Main.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ runClashTest = defaultMain
669669
, let _opts = def { hdlTargets = [VHDL], hdlLoad = [], hdlSim = []}
670670
in runTest "T3084" _opts
671671
, runTest "T3141" def{hdlSim=[], hdlLoad=[], clashFlags=["-itests/shouldwork/Issues/T3141", "-itests/shouldwork/Issues/T3141"]}
672+
, outputTest "T3147" def{hdlTargets=[Verilog], hdlSim=[]}
672673
] <>
673674
if compiledWith == Cabal then
674675
-- This tests fails without environment files present, which are only

tests/shouldwork/Issues/T3147.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module T3147 where
2+
3+
import Clash.Prelude
4+
import Data.List (isInfixOf)
5+
import System.Environment (getArgs)
6+
import System.FilePath ((</>))
7+
import qualified Prelude as P
8+
9+
delayedCounter ::
10+
forall dom d n.
11+
(HiddenClockResetEnable dom, KnownNat d, KnownNat n) =>
12+
SNat d ->
13+
Signal dom (Unsigned n)
14+
delayedCounter d = last $ iterate (succSNat d) dflipflop cnt
15+
where
16+
cnt = register 0 (cnt + 1)
17+
18+
topEntity :: Clock System -> Reset System -> Enable System -> Signal System (Unsigned 8)
19+
topEntity = exposeClockResetEnable $ delayedCounter d3
20+
21+
assertIn :: String -> String -> IO ()
22+
assertIn needle haystack
23+
| needle `isInfixOf` haystack = return ()
24+
| otherwise =
25+
P.error $ P.concat ["Expected:\n\n ", needle, "\n\nIn:\n\n", haystack]
26+
27+
mainVerilog :: IO ()
28+
mainVerilog = do
29+
[topDir] <- getArgs
30+
content <- readFile (topDir </> show 'topEntity </> "topEntity.v")
31+
assertIn "reg [7:0] c$bb_res_res" content
32+
assertIn "reg [7:0] c$bb_res_res_0" content
33+
assertIn "reg [7:0] c$bb_res_res_1" content
34+
assertIn "assign iterateI_ho1_0_res = c$bb_res_res3;" content
35+
assertIn "assign iterateI_ho1_1_res = c$bb_res_res_0;" content
36+
assertIn "assign iterateI_ho1_2_res = c$bb_res_res_1;" content

0 commit comments

Comments
 (0)