Skip to content

Commit

Permalink
Look through annotations in tyfam solving (#2677)
Browse files Browse the repository at this point in the history
1. Annotations are implemented as type synonyms.
2. GHC looks through type synonyms when matching type families
3. So Clash should do the same.

Fixes #2593

(cherry picked from commit da6ee06)

Co-authored-by: Christiaan Baaij <[email protected]>
  • Loading branch information
mergify[bot] and christiaanb authored Feb 24, 2024
1 parent 77ded2e commit 1f4c73f
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/2024-02-16T16_42_52+01_00_fix2593
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIXED: HDL generation fails when using multiple-hidden feature in combination with synthesis attributes [#2593](https://github.com/clash-lang/clash-compiler/issues/2593)
20 changes: 13 additions & 7 deletions clash-lib/src/Clash/Core/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright : (C) 2012-2016, University of Twente,
2016 , Myrtle Software Ltd,
2017 , Google Inc.
2021 , QBayLogic B.V.
2021-2024, QBayLogic B.V.
License : BSD2 (see the file LICENSE)
Maintainer : QBayLogic B.V. <[email protected]>
Expand Down Expand Up @@ -452,6 +452,10 @@ funSubst
funSubst _ Nothing = const Nothing
funSubst tcm (Just s) = uncurry go
where
-- AnnType cannot be matched in type-families within regular GHC (as they
-- are type synonyms) so it is fine to skip over them here.
go (AnnType _ t1) t2 = go t1 t2

go (VarTy nmF) ty = case lookup nmF s of
Nothing -> Just ((nmF,ty):s)
-- Given, for example, the type family definition:
Expand All @@ -470,6 +474,10 @@ funSubst tcm (Just s) = uncurry go
Just ty' | ty' `aeqType` ty -> Just s
_ -> Nothing

-- Only look through annotations in RHS after the VarTy case, so we can
-- preserve annotations in the substitution created by the VarTy case above
go t1 (AnnType _ t2) = go t1 t2

-- [Note] funSubst FunTy
--
-- Whenever type classes have associated types whose instances 'map' to
Expand All @@ -495,13 +503,11 @@ funSubst tcm (Just s) = uncurry go
, argView tcm r2 -- See [Note: Eager type families]
)

go ty1@(ConstTy _) ty2 =
-- Looks through AnnType
if ty1 `aeqType` ty2 then Just s else Nothing
go (ConstTy c1) (ConstTy c2)
| c1 == c2 = Just s

go ty1@(LitTy _) ty2 =
-- Looks through AnnType
if ty1 `aeqType` ty2 then Just s else Nothing
go (LitTy l1) (LitTy l2)
| l1 == l2 = Just s

go _ _ = Nothing

Expand Down
1 change: 1 addition & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ runClashTest = defaultMain $ clashTestRoot
, outputTest "T2510" def{hdlTargets=[VHDL], clashFlags=["-DNOINLINE=OPAQUE"]}
#endif
, outputTest "T2542" def{hdlTargets=[VHDL]}
, runTest "T2593" def{hdlSim=[]}
] <>
if compiledWith == Cabal then
-- This tests fails without environment files present, which are only
Expand Down
14 changes: 14 additions & 0 deletions tests/shouldwork/Issues/T2593.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module T2593 where

import Clash.Prelude
import Clash.Annotations.SynthesisAttributes

topEntity ::
Signal System Bit ->
Signal System Bit `Annotate` 'StringAttr "breaka" "me"
topEntity dIn =
exposeClockResetEnable (register 0) clk rst en dIn
where
clk = clockGen
rst = resetGen
en = enableGen

0 comments on commit 1f4c73f

Please sign in to comment.