unfoldrI doesn't quite (un)fold constants

Skip to first unread message

Chen-Mou Cheng

Aug 20, 2021, 2:57:19 AM8/20/21
to Clash - Hardware Description Language

It seems that unfoldrI doesn't completely (un)fold constants; instead, Clash would unfold it once and generate a circuit to do the rest. A minimal example looks like this:
module Example.Project (topEntity, plus) where

import Clash.Prelude
import Data.Tuple (swap)

plus :: Vec 3 (Signed 8) -> Vec 3 (Signed 8) -> Vec 3 (Signed 8)
plus = liftA2 (+)

topEntity :: Vec 3 (Signed 8) -> Vec 3 (Signed 8)
topEntity = plus $ map fromInteger $ unfoldrI (swap . flip divMod 128) 100000

We found that in the generated verilog, the constant 100000 is folded once as follows:
assign \Example.Project.topEntity1  = {{64'sd32, 64'sd781},   \Example.Project.topEntity2 };

And it seems that the rest of the work is done by a divModInteger circuit.  Is this the expected behavior?  Is it possible to completely (un)fold the constant at compile time and not generate any division circuits?  Any comments or suggestions would be greatly appreciated.

All the Best,
Chen-Mou Cheng

Alex McKenna

Aug 20, 2021, 11:17:06 AM8/20/21
to Clash - Hardware Description Language
Hi Chen-Mou Cheng,

Unfortunately unconcatI is a function which Clash currently has a hard time constant folding. You can get around this with TemplateHaskell for the time being though. If you use

topEntity = plus $ map resize $ $(lift (unfoldrI @3 (swap . flip divMod 128) (100000 :: Signed 17)))

then it constant folds down to the vector (32 :> 13 :> 126 :> Nil).

One note: the 100000 in your code currently is an Integer, which means divMod acts on Integer. In order to have the TemplateHaskell work correctly I had to make this a Signed 17. The lift instance for Signed adds type signatures, which are needed for inference to work correctly when expanding the splice.

Hope that helps,

  - Alex

Chen-Mou Cheng

Aug 21, 2021, 2:20:18 AM8/21/21
to clash-l...@googlegroups.com
Hi Alex,

Thank you very much for your prompt response.  I confirm that the TH workaround works for me.  Thanks!

All the Best,

You received this message because you are subscribed to a topic in the Google Groups "Clash - Hardware Description Language" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clash-language/BFQVxZUgj5g/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clash-languag...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clash-language/683dab0f-e1d8-4df4-8203-03cbf60721aen%40googlegroups.com.
Reply all
Reply to author
0 new messages