clash-lang / clash-compiler

Haskell to VHDL/Verilog/SystemVerilog compiler
https://clash-lang.org/
Other
1.4k stars 149 forks source link

BlackBoxHaskell blackboxes don't warn about potential primitive problems #2565

Open leonschoorl opened 11 months ago

leonschoorl commented 11 months ago
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module PrimWarn where

import Clash.Prelude hiding (Text)
import qualified Clash.Explicit.Prelude as E
import Clash.Annotations.Primitive
import Clash.Netlist.Types (BlackBox(BBTemplate))
import Clash.Netlist.BlackBox.Types (BlackBoxFunction, emptyBlackBoxMeta, Element(ArgGen,Text))
import Clash.Annotations.Primitive (Primitive(..), HDL(VHDL))
import Data.String.Interpolate (__i)

primitiveTF :: BlackBoxFunction
primitiveTF isD primName args ty = pure $
  Right ( emptyBlackBoxMeta, BBTemplate [Text "5 + ", ArgGen 0 0])

primitive :: Signal System Int -> Signal System Int
-- haskell ignores argument, while blackbox use the argument
primitive _ = pure 5

{-# NOINLINE primitive #-}  -- when using GHC >=9.4 clash wants this to be OPAQUE
{-# ANN primitive hasBlackBox #-}

#if 0
{-# ANN primitive (InlineYamlPrimitive [VHDL] $  [__i|
  BlackBoxHaskell:
    name: PrimWarn.primitive
    templateFunction: PrimWarn.primitiveTF
 |]) #-}
#else
{-# ANN primitive (InlineYamlPrimitive [VHDL] $  [__i|
  BlackBox:
    name: PrimWarn.primitive
    kind: Expression
    template: "5 + ~ARG[0]"
 |]) #-}
#endif

topEntity = primitive

When run as is clash warns about the potentially problems with the implementation of primitive:

PrimWarn.hs:20:1: Warning: primitive PrimWarn.primitive isn't marked OPAQUE.
This might make Clash ignore this primitive.

PrimWarn.hs:20:1: Warning: The Haskell implementation of primitive PrimWarn.primitive isn't using argument #0, but the corresponding primitive blackbox does.
This can lead to incorrect HDL output because GHC can replace these arguments by an undefined value.

But when changing the #if 0 to #if 1 thus switching from the BlackBox to an equivalent BlackBoxHaskell no such warnings are reported.