Skip to content

Commit

Permalink
Make applyDebug's free variable check take the context into account
Browse files Browse the repository at this point in the history
Fixes #2623
  • Loading branch information
leonschoorl committed Dec 12, 2023
1 parent 930641c commit 56eda6b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
29 changes: 23 additions & 6 deletions clash-lib/src/Clash/Rewrite/Util.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Copyright : (C) 2012-2016, University of Twente,
2016 , Myrtle Software Ltd,
2017 , Google Inc.,
2021-2022, QBayLogic B.V.
2021-2023, QBayLogic B.V.
License : BSD2 (see the file LICENSE)
Maintainer : QBayLogic B.V. <devops@qbaylogic.com>
Expand Down Expand Up @@ -77,7 +77,7 @@ import Clash.Core.Type (Type (..), normalizeType)
import Clash.Core.Var
(Id, IdScope (..), TyVar, Var (..), mkGlobalId, mkLocalId, mkTyVar)
import Clash.Core.VarEnv
(InScopeSet, extendInScopeSet, extendInScopeSetList, mkInScopeSet,
(InScopeSet, extendInScopeSet, extendInScopeSetList, mkInScopeSet, notElemInScopeSet,
uniqAway, uniqAway', mapVarEnv, eltsVarEnv, unitVarSet, emptyVarEnv,
mkVarEnv, eltsVarSet, elemVarEnv, lookupVarEnv, extendVarEnv, elemVarSet,
differenceVarEnv)
Expand Down Expand Up @@ -173,12 +173,13 @@ apply = \s rewrite ctx expr0 -> do
return ()

if isDebugging opts
then applyDebug s expr0 hasChanged expr1
then applyDebug ctx s expr0 hasChanged expr1
else return expr1
{-# INLINE apply #-}

applyDebug
:: String
:: TransformContext
-> String
-- ^ Name of the transformation
-> Term
-- ^ Original expression
Expand All @@ -187,7 +188,7 @@ applyDebug
-> Term
-- ^ New expression
-> RewriteMonad extra Term
applyDebug name exprOld hasChanged exprNew = do
applyDebug ctx name exprOld hasChanged exprNew = do
nTrans <- Lens.use transformCounter
opts <- Lens.view debugOpts

Expand All @@ -212,9 +213,14 @@ applyDebug name exprOld hasChanged exprNew = do
let beforeTy = inferCoreTypeOf tcm exprOld
beforeFV = Lens.setOf freeLocalVars exprOld
afterTy = inferCoreTypeOf tcm exprNew
afterFV = Lens.setOf freeLocalVars exprNew
afterFV = filterFVs (Lens.setOf freeLocalVars exprNew)
newFV = not (afterFV `Set.isSubsetOf` beforeFV)
accidentalShadows = findAccidentialShadows exprNew
-- see NOTE [Filter free variables]
allowNewFVsFromCtx = name == "caseCon"
filterFVs | allowNewFVsFromCtx = Set.filter notInCtx
| otherwise = id
notInCtx v = notElemInScopeSet v (tfInScope ctx)

Monad.when newFV $
error ( concat [ $(curLoc)
Expand Down Expand Up @@ -266,6 +272,17 @@ applyDebug name exprOld hasChanged exprNew = do
before = showPpr exprOld
after = showPpr exprNew

-- NOTE [Filter free variables]
-- Since [Give evaluator acces to inscope let-bindings #2571](https://github.com/clash-lang/clash-compiler/pull/2571)
-- the evaluator can rewrite expressions using let bindings from the 'TransformContext',
-- these bindings may reference other things bound in the context which weren't
-- in the expression before, and in doing so introduces new free variables and
-- fails this check for new free variables.
-- To prevent this we filter out all variables from bound in the context.
-- But only during a caseCon transformation, to not weaken this check unnecessarily.



-- | Perform a transformation on a Term
runRewrite
:: String
Expand Down
1 change: 1 addition & 0 deletions tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,7 @@ runClashTest = defaultMain $ clashTestRoot
, outputTest "T2510" def{hdlTargets=[VHDL], clashFlags=["-DNOINLINE=OPAQUE"]}
#endif
, outputTest "T2542" def{hdlTargets=[VHDL]}
, runTest "T2623CaseConFVs" def{hdlLoad=[],hdlSim=[],hdlTargets=[VHDL]}
] <>
if compiledWith == Cabal then
-- This tests fails without environment files present, which are only
Expand Down
19 changes: 19 additions & 0 deletions tests/shouldwork/Issues/T2623CaseConFVs.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module T2623CaseConFVs where
import Clash.Prelude

topEntity = foo @System

foo :: forall dom. Signal dom (Vec 1 (Signed 2)) -> Signal dom Bool
foo = \input ->
let
scs :: Signal dom (Vec 1 Bool)
scs = bundle $ map f $ unbundle input
in
fmap bar scs


bar :: (KnownNat n) => Vec (n+1) Bool -> Bool
bar = fold (&&) . map (id)

f :: Signal dom a -> Signal dom Bool
f = const $ pure $ True

0 comments on commit 56eda6b

Please sign in to comment.