Skip to content

Commit 87f2254

Browse files
Merge pull request #3037 from clash-lang/martijn/say-no-to-no-noops
Only run `collapseRHSNoops` after constant propagation
2 parents 8b927f6 + 4231114 commit 87f2254

File tree

15 files changed

+162
-114
lines changed

15 files changed

+162
-114
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: `collapseRHSNoops` now runs after constant folding, making Clash able to constant fold more expressions than before. See [#3036](https://github.com/clash-lang/clash-compiler/issues/3036).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: The compiler now tracks assignment types in more places, which can prevent "clash error call" errors in some specific cases. See [#3045](https://github.com/clash-lang/clash-compiler/issues/3045).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FIXED: Test bench primitives now assign the string they want to pass to Verilog's `$display` to a variable before printing. This works around a limitation in IVerilog. See [#3046](https://github.com/clash-lang/clash-compiler/issues/3046).

clash-ghc/src-ghc/Clash/GHC/Evaluator/Primitive.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2170,6 +2170,9 @@ ghcPrimStep tcm isSubj pInfo tys args mach = case primName pInfo of
21702170
"GHC.Internal.Real.^_$s$spowImpl2" -- :: Int# -> Integer -> Integer
21712171
| [intLiteral -> Just j, integerLiteral -> Just i] <- args
21722172
-> reduce (integerToIntLiteral $ i ^ j)
2173+
"GHC.Internal.Real.^_$s$spowImpl" -- :: Int -> Integer -> Integer
2174+
| [intLiteral -> Just j, integerLiteral -> Just i] <- args
2175+
-> reduce (integerToIntLiteral $ i ^ j)
21732176
"GHC.Internal.Real.$w$spowImpl" -- :: Integer -> Int# -> Integer
21742177
| [integerLiteral -> Just i, intLiteral -> Just j] <- args
21752178
-> reduce (integerToIntLiteral $ i ^ j)

clash-lib/prims/verilog/Clash_Explicit_Testbench.primitives.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
// assert begin
1616
// pragma translate_off
1717
always @(~IF~ACTIVEEDGE[Rising][0]~THENposedge~ELSEnegedge~FI ~ARG[3]) begin
18-
if (~ARG[6] !== ~ARG[7]) begin
19-
$display("@%0tns: %s, expected: %b, actual: %b", $time, ~LIT[5], ~ARG[7], ~ARG[6]);
18+
if (~VAR[checked][6] !== ~VAR[expected][7]) begin
19+
$display("@%0tns: %s, expected: %b, actual: %b", $time, ~LIT[5], ~VAR[expected][7], ~VAR[checked][6]);
2020
`ifdef VERILATOR
2121
$c("std::exit(1);");
2222
`endif
@@ -49,7 +49,7 @@
4949
5050
always @(~IF~ACTIVEEDGE[Rising][0]~THENposedge~ELSEnegedge~FI ~ARG[2]) begin
5151
if (~SYM[1] !== ~SYM[2]) begin
52-
$display("@%0tns: %s, expected: %b, actual: %b", $time, ~LIT[4], ~ARG[6], ~ARG[5]);
52+
$display("@%0tns: %s, expected: %b, actual: %b", $time, ~LIT[4], ~VAR[expected][6], ~VAR[checked][5]);
5353
`ifdef VERILATOR
5454
$c("std::exit(1);");
5555
`endif

clash-lib/src/Clash/Netlist.hs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ mkNetDecl (id_,tm) = preserveVarEnv $ do
363363
go pInfo (BlackBox {resultInits=nmDs, multiResult=True}) = withTicks ticks $ \_ -> do
364364
tcm <- Lens.view tcCache
365365
let (args1, res) = splitMultiPrimArgs (multiPrimInfo' tcm pInfo) args0
366-
(bbCtx, _) <- mkBlackBoxContext (primName pInfo) res args1
366+
(bbCtx, _) <- mkBlackBoxContext (primName pInfo) Concurrent res args1
367367
mapM (go' (primName pInfo) bbCtx) nmDs
368368
go pInfo (BlackBox {resultInits=nmDs}) = withTicks ticks $ \_ -> do
369-
(bbCtx, _) <- mkBlackBoxContext (primName pInfo) [i] args0
369+
(bbCtx, _) <- mkBlackBoxContext (primName pInfo) Concurrent [i] args0
370370
mapM (go' (primName pInfo) bbCtx) nmDs
371371
go _ _ = pure []
372372

@@ -413,8 +413,8 @@ mkDeclarations'
413413
-> Term
414414
-- ^ RHS of the let-binder
415415
-> NetlistMonad [Declaration]
416-
mkDeclarations' _declType bndr (collectTicks -> (Var v,ticks)) =
417-
withTicks ticks (mkFunApp (Id.unsafeFromCoreId bndr) v [])
416+
mkDeclarations' declType bndr (collectTicks -> (Var v,ticks)) =
417+
withTicks ticks (mkFunApp declType (Id.unsafeFromCoreId bndr) v [])
418418

419419
mkDeclarations' _declType _bndr e@(collectTicks -> (Case _ _ [],_)) = do
420420
(_,sp) <- Lens.use curCompNm
@@ -436,7 +436,7 @@ mkDeclarations' declType bndr app = do
436436
case appF of
437437
Var f
438438
| null tyArgs ->
439-
withTicks ticks (mkFunApp (Id.unsafeFromCoreId bndr) f args)
439+
withTicks ticks (mkFunApp declType (Id.unsafeFromCoreId bndr) f args)
440440
| otherwise -> do
441441
(_,sp) <- Lens.use curCompNm
442442
throw (ClashException sp ($(curLoc) ++ "Not in normal form: Var-application with Type arguments:\n\n" ++ showPpr app) Nothing)
@@ -633,12 +633,13 @@ patPos reprs pat@(DataPat dataCon _ _) =
633633
-- | Generate a list of Declarations for a let-binder where the RHS is a function application
634634
mkFunApp
635635
:: HasCallStack
636-
=> Identifier -- ^ LHS of the let-binder
636+
=> DeclarationType
637+
-> Identifier -- ^ LHS of the let-binder
637638
-> Id -- ^ Name of the applied function
638639
-> [Term] -- ^ Function arguments
639640
-> [Declaration] -- ^ Tick declarations
640641
-> NetlistMonad [Declaration]
641-
mkFunApp dstId fun args tickDecls = do
642+
mkFunApp declType dstId fun args tickDecls = do
642643
topAnns <- Lens.use topEntityAnns
643644
tcm <- Lens.view tcCache
644645
case (isGlobalId fun, lookupVarEnv fun topAnns) of
@@ -652,7 +653,7 @@ mkFunApp dstId fun args tickDecls = do
652653
-> do
653654
argHWTys <- mapM (unsafeCoreTypeToHWTypeM' $(curLoc)) fArgTys1
654655
(argExprs, concat -> argDecls) <- unzip <$>
655-
mapM (\(e,t) -> mkExpr False Concurrent (NetlistId dstId t) e)
656+
mapM (\(e,t) -> mkExpr False declType (NetlistId dstId t) e)
656657
(zip args fArgTys1)
657658

658659
-- Filter void arguments, but make sure to render their declarations:
@@ -724,7 +725,7 @@ mkFunApp dstId fun args tickDecls = do
724725
argHWTys <- mapM coreTypeToHWTypeM' argTys
725726

726727
(argExprs, concat -> argDecls) <- unzip <$>
727-
mapM (\(e,t) -> mkExpr False Concurrent (NetlistId dstId t) e)
728+
mapM (\(e,t) -> mkExpr False declType (NetlistId dstId t) e)
728729
(zip args argTys)
729730

730731
-- Filter void arguments, but make sure to render their declarations:
@@ -735,7 +736,7 @@ mkFunApp dstId fun args tickDecls = do
735736
let compOutp = (\(_,x,_) -> x) <$> listToMaybe co
736737
if length filteredTypeExprs == length compInps
737738
then do
738-
(argExprs',argDecls') <- (second concat . unzip) <$> mapM (toSimpleVar dstId) filteredTypeExprs
739+
(argExprs',argDecls') <- (second concat . unzip) <$> mapM (toSimpleVar declType dstId) filteredTypeExprs
739740
let inpAssigns = zipWith (\(i,t) e -> (Identifier i Nothing,In,t,e)) compInps argExprs'
740741
outpAssign = case compOutp of
741742
Nothing -> []
@@ -799,14 +800,16 @@ mkFunApp dstId fun args tickDecls = do
799800
"Maybe (Int -> Int)"
800801
|]
801802

802-
toSimpleVar :: Identifier
803+
toSimpleVar :: DeclarationType
804+
-> Identifier
803805
-> (Expr,Type)
804806
-> NetlistMonad (Expr,[Declaration])
805-
toSimpleVar _ (e@(Identifier _ Nothing),_) = return (e,[])
806-
toSimpleVar dstId (e,ty) = do
807+
toSimpleVar _ _ (e@(Identifier _ Nothing),_) = return (e,[])
808+
toSimpleVar declType dstId (e,ty) = do
807809
argNm <- Id.suffix dstId "fun_arg"
808810
hTy <- unsafeCoreTypeToHWTypeM' $(curLoc) ty
809-
argDecl <- mkInit Concurrent Cont argNm hTy e
811+
let assignTy = declTypeUsage declType
812+
argDecl <- mkInit declType assignTy argNm hTy e
810813
return (Identifier argNm Nothing, argDecl)
811814

812815
-- | Generate an expression for a term occurring on the RHS of a let-binder
@@ -843,14 +846,14 @@ mkExpr bbEasD declType bndr app =
843846
| not (null tyArgs) -> invalid "Var-application with type arguments"
844847
| otherwise -> do
845848
argNm <- Id.suffix (netlistId1 id Id.unsafeFromCoreId bndr) "fun_arg"
846-
decls <- mkFunApp argNm f tmArgs tickDecls
849+
decls <- mkFunApp declType argNm f tmArgs tickDecls
847850
if isVoid hwTyA then
848851
return (Noop, decls)
849852
else
850853
-- This net was already declared in the call to mkSelection.
851854
return ( Identifier argNm Nothing
852855
, NetDecl Nothing argNm hwTyA : decls)
853-
Case scrut ty' [alt] -> mkProjection bbEasD bndr scrut ty' alt
856+
Case scrut ty' [alt] -> mkProjection declType bbEasD bndr scrut ty' alt
854857
Case scrut tyA (alt:alts) -> do
855858
argNm <- Id.suffix (netlistId1 id Id.unsafeFromCoreId bndr) "sel_arg"
856859
decls <- mkSelection declType (NetlistId argNm (netlistTypes1 bndr))
@@ -880,7 +883,8 @@ mkExpr bbEasD declType bndr app =
880883
--
881884
-- Works for both product types, as sum-of-product types.
882885
mkProjection
883-
:: Bool
886+
:: DeclarationType
887+
-> Bool
884888
-- ^ Projection must bind to a simple variable
885889
-> NetlistId
886890
-- ^ Name hint for the signal to which the projection is (potentially) assigned
@@ -891,8 +895,9 @@ mkProjection
891895
-> Alt
892896
-- ^ The field to be projected
893897
-> NetlistMonad (Expr, [Declaration])
894-
mkProjection mkDec bndr scrut altTy alt@(pat,v) = do
898+
mkProjection declType mkDec bndr scrut altTy alt@(pat,v) = do
895899
tcm <- Lens.view tcCache
900+
let assignTy = declTypeUsage declType
896901
let scrutTy = inferCoreTypeOf tcm scrut
897902
e = Case scrut scrutTy [alt]
898903
(_,sp) <- Lens.use curCompNm
@@ -909,7 +914,7 @@ mkProjection mkDec bndr scrut altTy alt@(pat,v) = do
909914
Id.next
910915
(\b -> Id.suffix (Id.unsafeFromCoreId b) "projection")
911916
bndr
912-
(scrutExpr,newDecls) <- mkExpr False Concurrent (NetlistId scrutNm scrutTy) scrut
917+
(scrutExpr,newDecls) <- mkExpr False declType (NetlistId scrutNm scrutTy) scrut
913918
case scrutExpr of
914919
Identifier newId modM ->
915920
pure (Right (newId, modM, newDecls))
@@ -920,7 +925,7 @@ mkProjection mkDec bndr scrut altTy alt@(pat,v) = do
920925
-- TODO: seems useless?
921926
pure (Left newDecls)
922927
_ -> do
923-
scrutDecl <- mkInit Concurrent Cont scrutNm sHwTy scrutExpr
928+
scrutDecl <- mkInit declType assignTy scrutNm sHwTy scrutExpr
924929
pure (Right (scrutNm, Nothing, newDecls ++ scrutDecl))
925930

926931
case scrutRendered of
@@ -957,7 +962,7 @@ mkProjection mkDec bndr scrut altTy alt@(pat,v) = do
957962
case bndr of
958963
NetlistId scrutNm _ | mkDec -> do
959964
scrutNm' <- Id.next scrutNm
960-
scrutDecl <- mkInit Concurrent Cont scrutNm' vHwTy extractExpr
965+
scrutDecl <- mkInit declType assignTy scrutNm' vHwTy extractExpr
961966
return (Identifier scrutNm' Nothing, scrutDecl ++ decls)
962967
MultiId {} -> error "mkProjection: MultiId"
963968
_ -> return (extractExpr,decls)
@@ -985,7 +990,7 @@ mkDcApplication declType [dstHType] bndr dc args = do
985990
let dcNm = nameOcc (dcName dc)
986991
tcm <- Lens.view tcCache
987992
let argTys = map (inferCoreTypeOf tcm) args
988-
argNm <- netlistId1 return (\b -> Id.suffix (Id.unsafeFromCoreId b) "_dc_arg") bndr
993+
argNm <- netlistId1 return (\b -> Id.suffix (Id.unsafeFromCoreId b) "dc_arg") bndr
989994
argHWTys <- mapM coreTypeToHWTypeM' argTys
990995

991996
(argExprs, concat -> argDecls) <- unzip <$>

clash-lib/src/Clash/Netlist.hs-boot

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ mkDcApplication :: HasCallStack
5050
-> NetlistMonad (Expr,[Declaration])
5151

5252
mkProjection
53-
:: Bool
53+
:: DeclarationType
54+
-> Bool
5455
-> NetlistId
5556
-> Term
5657
-> Type
@@ -73,7 +74,8 @@ mkDeclarations' :: HasCallStack => DeclarationType -> Id -> Term -> NetlistMonad
7374

7475
mkFunApp
7576
:: HasCallStack
76-
=> Identifier -- ^ LHS of the let-binder
77+
=> DeclarationType
78+
-> Identifier -- ^ LHS of the let-binder
7779
-> Id -- ^ Name of the applied function
7880
-> [Term] -- ^ Function arguments
7981
-> [Declaration] -- ^ Tick declarations

0 commit comments

Comments
 (0)