Skip to content
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

Precompute debug operations info in linear time #3038

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Core/Extra/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ isDebugOp = \case
OpTrace -> True
OpFail -> True
OpSeq -> True
OpAssert -> False
OpAssert -> True
OpAnomaByteArrayFromAnomaContents -> False
OpAnomaByteArrayToAnomaContents -> False
OpAnomaDecode -> False
Expand Down
38 changes: 38 additions & 0 deletions src/Juvix/Compiler/Core/Info/DebugOpsInfo.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Juvix.Compiler.Core.Info.DebugOpsInfo where

import Juvix.Compiler.Core.Extra
import Juvix.Compiler.Core.Info qualified as Info

newtype DebugOpsInfo = DebugOpsInfo
{ _infoHasDebugOps :: Bool
}

instance IsInfo DebugOpsInfo

kDebugOpsInfo :: Key DebugOpsInfo
kDebugOpsInfo = Proxy

makeLenses ''DebugOpsInfo

-- | Computes debug operations info for each subnode.
computeDebugOpsInfo :: Node -> Node
computeDebugOpsInfo = umap go
where
go :: Node -> Node
go node
| isDebugOp node =
modifyInfo (Info.insert (DebugOpsInfo True)) node
| otherwise =
modifyInfo (Info.insert dbi) node
where
dbi =
DebugOpsInfo
. or
. map (hasDebugOps . (^. childNode))
$ children node

getDebugOpsInfo :: Node -> DebugOpsInfo
getDebugOpsInfo = fromJust . Info.lookup kDebugOpsInfo . getInfo

hasDebugOps :: Node -> Bool
hasDebugOps = (^. infoHasDebugOps) . getDebugOpsInfo
9 changes: 6 additions & 3 deletions src/Juvix/Compiler/Core/Transformation/Optimize/LetFolding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Juvix.Compiler.Core.Transformation.Optimize.LetFolding (letFolding, letFo

import Juvix.Compiler.Core.Data.BinderList qualified as BL
import Juvix.Compiler.Core.Extra
import Juvix.Compiler.Core.Info.DebugOpsInfo as Info
import Juvix.Compiler.Core.Info.FreeVarsInfo as Info
import Juvix.Compiler.Core.Transformation.Base

Expand All @@ -28,7 +29,7 @@ convertNode isFoldable md = rmapL go
|| Info.freeVarOccurrences 0 _letBody <= 1
|| isFoldable md bl (_letItem ^. letItemValue)
)
&& not (containsDebugOps _letBody) ->
&& not (Info.hasDebugOps _letBody) ->
go (recur . (mkBCRemove b val' :)) (BL.cons b bl) _letBody
where
val' = go recur bl (_letItem ^. letItemValue)
Expand All @@ -39,13 +40,15 @@ convertNode isFoldable md = rmapL go
letFolding' :: (Module -> BinderList Binder -> Node -> Bool) -> Module -> Module
letFolding' isFoldable tab =
mapAllNodes
( removeInfo kFreeVarsInfo
( removeInfo kDebugOpsInfo
. removeInfo kFreeVarsInfo
. convertNode isFoldable tab
. computeFreeVarsInfo' 2
-- 2 is the lambda multiplier factor which guarantees that every free
-- variable under a lambda is counted at least twice, preventing let
-- folding for let-bound variables (with non-immediate values) that
-- occur under lambdas
. computeFreeVarsInfo' 2
. computeDebugOpsInfo
)
tab

Expand Down
Loading