Skip to content

Commit 141df71

Browse files
Use data for Signed, Unsigned, Index, and BiSignalOut
GHC may insert coercions making Clash see underlying types. For the types mentioned in the commit title, this means Clash will see either a `Natural` or an `Integer`. These types are (for better or for worse) represented as machine words, potentially truncating any operations. As a concrete example, take: top :: Unsinged 68 -> Unsigned 68 -> Vec 1 (Unsigned 64) top x y = let res = x + y in res `seq` res :> Nil Translated to core, we get: λ(x :: Unsigned 68) -> λ(y :: Unsigned 68) -> let nt :: Natural = +# @68 topEntity2[GlobalId] x[LocalId] y[LocalId] in case nt[LocalId] of _ -> Cons @(+ 0 1) @(Unsigned 68) @0 (_CO_ @(~# Natural Natural (+ 0 1) (+ 0 1))) nt[LocalId] ((Λb -> Nil @0 @b (_CO_ @(~# Natural Natural 0 0))) @(Unsigned 68)) I.e., the result of `x + y` gets assinged to a binder of type `Natural`, making the calculation truncate.
1 parent 6bfa864 commit 141df71

File tree

4 files changed

+0
-26
lines changed

4 files changed

+0
-26
lines changed

clash-prelude/src/Clash/Signal/BiSignal.hs

-5
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,8 @@ type role BiSignalOut nominal nominal nominal
206206
--
207207
-- as it is not safe to coerce the default behaviour, synthesis domain or width
208208
-- of the data in the signal.
209-
#if MIN_VERSION_base(4,15,0) && !MIN_VERSION_base(4,17,0)
210209
data BiSignalOut (ds :: BiSignalDefault) (dom :: Domain) (n :: Nat)
211210
= BiSignalOut ![Signal dom (Maybe (BitVector n))]
212-
#else
213-
newtype BiSignalOut (ds :: BiSignalDefault) (dom :: Domain) (n :: Nat)
214-
= BiSignalOut [Signal dom (Maybe (BitVector n))]
215-
#endif
216211

217212
type instance HasDomain dom1 (BiSignalOut ds dom2 n) = DomEq dom1 dom2
218213
type instance TryDomain t (BiSignalOut ds dom n) = 'Found dom

clash-prelude/src/Clash/Sized/Internal/Index.hs

-7
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,10 @@ type role Index nominal
158158
--
159159
-- as it is not safe to coerce between 'Index'es with different ranges. To
160160
-- change the size, use the functions in the 'Resize' class.
161-
#if MIN_VERSION_base(4,15,0) && !MIN_VERSION_base(4,17,0)
162161
data Index (n :: Nat) =
163162
-- | The constructor, 'I', and the field, 'unsafeToInteger', are not
164163
-- synthesizable.
165164
I { unsafeToInteger :: !Integer }
166-
#else
167-
newtype Index (n :: Nat) =
168-
-- | The constructor, 'I', and the field, 'unsafeToInteger', are not
169-
-- synthesizable.
170-
I { unsafeToInteger :: Integer }
171-
#endif
172165
deriving (Data, Generic)
173166

174167
{-# ANN I hasBlackBox #-}

clash-prelude/src/Clash/Sized/Internal/Signed.hs

-7
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,10 @@ type role Signed nominal
184184
--
185185
-- as it is not safe to coerce between different width Signed. To change the
186186
-- width, use the functions in the 'Clash.Class.Resize.Resize' class.
187-
#if (__GLASGOW_HASKELL__ >= 900 && __GLASGOW_HASKELL__ < 904) || __GLASGOW_HASKELL__ >= 912
188187
data Signed (n :: Nat) =
189188
-- | The constructor, 'S', and the field, 'unsafeToInteger', are not
190189
-- synthesizable.
191190
S { unsafeToInteger :: !Integer}
192-
#else
193-
newtype Signed (n :: Nat) =
194-
-- | The constructor, 'S', and the field, 'unsafeToInteger', are not
195-
-- synthesizable.
196-
S { unsafeToInteger :: Integer}
197-
#endif
198191
deriving (Data, Generic)
199192

200193
{-# ANN S hasBlackBox #-}

clash-prelude/src/Clash/Sized/Internal/Unsigned.hs

-7
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,10 @@ type role Unsigned nominal
200200
--
201201
-- as it is not safe to coerce between different width Unsigned. To change the
202202
-- width, use the functions in the 'Clash.Class.Resize.Resize' class.
203-
#if (__GLASGOW_HASKELL__ >= 900 && __GLASGOW_HASKELL__ < 904) || __GLASGOW_HASKELL__ >= 912
204203
data Unsigned (n :: Nat) =
205204
-- | The constructor, 'U', and the field, 'unsafeToNatural', are not
206205
-- synthesizable.
207206
U { unsafeToNatural :: !Natural }
208-
#else
209-
newtype Unsigned (n :: Nat) =
210-
-- | The constructor, 'U', and the field, 'unsafeToNatural', are not
211-
-- synthesizable.
212-
U { unsafeToNatural :: Natural }
213-
#endif
214207
deriving (Data, Generic)
215208

216209
{-# ANN U hasBlackBox #-}

0 commit comments

Comments
 (0)