Skip to content

Behaves differently: zipWith3 f a b c / f <$> a <*> b <*> c #2723

Open
@martijnbastiaan

Description

@martijnbastiaan

That is, the following causes Clash to generate one module for bbWrapper:

topEntity :: Vec 4 (Signal System Bit)
topEntity = zipWith3 bbWrapper indicesI indicesI indicesI
{-# CLASH_OPAQUE topEntity #-}

But the following generates tries to generate 4:

topEntity :: Vec 4 (Signal System Bit)
topEntity = bbWrapper <$> indicesI <*> indicesI <*> indicesI
{-# CLASH_OPAQUE topEntity #-}

Reproducer (note that this reproducer will fail to error if #2722 gets fixed):

{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Test @zipWith3 f a b c@ behaves the same as @f <$> a <*> b <*> c@ when it
-- comes to specialization caches.
module T2723 where

import Clash.Explicit.Prelude

import Clash.Annotations.Primitive (Primitive(..))
import Clash.Backend (blockDecl)
import Data.Monoid (Ap(getAp))

import qualified Clash.Netlist.Types as N
import qualified Clash.Netlist.Id as Id

bbTF :: N.TemplateFunction
bbTF = N.TemplateFunction used valid $ \_bbCtx -> do
  x <- Id.make "x"

  () <- case Id.toText x of
    "x" -> pure ()
    xName -> error $ "Unexpected name: " <> show xName <> ". Expected: x."

  getAp $ blockDecl x [N.NetDecl Nothing x N.Bit]
 where
  used    = [0,1]
  valid _ = True

{-# ANN bb (InlinePrimitive [minBound..] "[ { \"BlackBox\" : { \"name\" : \"T2723.bb\", \"kind\": \"Declaration\", \"workInfo\": \"Always\", \"format\": \"Haskell\", \"templateFunction\": \"T2723.bbTF\"}} ]") #-}
bb :: Signal System Bit
bb = pure low
{-# CLASH_OPAQUE bb #-}

bbWrapper :: Index n -> Index n -> Index n -> Signal System Bit
bbWrapper !_ !_ !_ = bb
{-# CLASH_OPAQUE bbWrapper #-}

-- FAILS:
topEntity :: Vec 4 (Signal System Bit)
topEntity = bbWrapper <$> indicesI <*> indicesI <*> indicesI
{-# CLASH_OPAQUE topEntity #-}

-- OK:
-- topEntity :: Vec 4 (Signal System Bit)
-- topEntity v = zipWith3 bbWrapper v indicesI indicesI
-- {-# CLASH_OPAQUE topEntity #-}

Gives:

$ rm -rf vhdl/ && cabal run clash -- -itests/shouldwork/Issues T2723 --vhdl -Wall -Werror -DCLASH_OPAQUE=OPAQUE
Resolving dependencies...
Loaded package environment from /home/martijn/code/clash-compiler/.ghc.environment.x86_64-linux-9.4.8
GHC: Setting up GHC took: 0.250s
GHC: Compiling and loading modules took: 1.229s
Hint: Interpreting T2723.bbTF
Clash: Parsing and compiling primitives took 1.550s
GHC+Clash: Loading modules cumulatively took 3.170s
Clash: Compiling T2723.topEntity
Clash: Normalization took 0.005s
Clash: Netlist generation took 0.000s

<no location info>: error:
    Clash error call:
    Unexpected name: "x_0". Expected: x.
    CallStack (from HasCallStack):
      error, called at tests/shouldwork/Issues/T2723.hs:23:14 in main:T2723

I'm somewhat undecided whether this is a bug or an enhancement, so I've labelled them as both.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions