-
Notifications
You must be signed in to change notification settings - Fork 156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make applyDebug's free variable check take the context into account #2624
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. <[email protected]> | ||
|
||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reformatted / broke up sentences to make it easier to read.
I'm not sure about this though:
Is this because the evaluator is only called in
caseCon
? In any case, I think we should defend why we think we only need to do it forcaseCon
in the note.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@christiaanb maybe you could comment on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The evaluator is called in multiple places, including
caseCon
. But as @leonschoorl points out, the "introduce free variables" check is quite useful to catch bugs in the Clash compiler, so we want to leave it enabled in as many places as possible. So it's only disabled incaseCon
as we have a confirmed "false positive" for that transformation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, this sounds like: we're aware this is an issue, and we're going to wait until another transformation calling the evaluator is going to blow up..
What we should do is check the other transformations calling the evaluator, check whether they suffer from the same issue, and add a comment explaining why we do (not) need a similar approach as introduced in this PR.