1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-02 23:43:01 +03:00

Reachability analysis in Core (#2097)

* Closes #2080
This commit is contained in:
Łukasz Czajka 2023-05-16 13:15:36 +02:00 committed by GitHub
parent 336a934d18
commit 185937fc7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 8 deletions

View File

@ -1,6 +1,5 @@
module Juvix.Compiler.Core.Data.IdentDependencyInfo where
import Data.HashMap.Strict qualified as HashMap
import Data.HashSet qualified as HashSet
import Juvix.Compiler.Core.Data.InfoTable
import Juvix.Compiler.Core.Extra.Utils
@ -24,7 +23,7 @@ createIdentDependencyInfo tab = createDependencyInfo graph startVertices
startVertices = HashSet.fromList syms
syms :: [Symbol]
syms = map (^. identifierSymbol) (HashMap.elems (tab ^. infoIdentifiers))
syms = maybe [] singleton (tab ^. infoMain)
recursiveIdents :: InfoTable -> HashSet Symbol
recursiveIdents = nodesOnCycles . createIdentDependencyInfo

View File

@ -0,0 +1,14 @@
module Juvix.Compiler.Core.Transformation.Optimize.FilterUnreachable where
import Data.HashMap.Strict qualified as HashMap
import Juvix.Compiler.Core.Data.IdentDependencyInfo
import Juvix.Compiler.Core.Transformation.Base
filterUnreachable :: InfoTable -> InfoTable
filterUnreachable tab = pruneInfoTable $ over infoIdentifiers goFilter tab
where
depInfo = createIdentDependencyInfo tab
goFilter :: HashMap Symbol IdentifierInfo -> HashMap Symbol IdentifierInfo
goFilter idents =
HashMap.filterWithKey (\sym _ -> isReachable depInfo sym) idents

View File

@ -3,18 +3,20 @@ module Juvix.Compiler.Core.Transformation.Optimize.Phase.Main where
import Juvix.Compiler.Core.Data.IdentDependencyInfo
import Juvix.Compiler.Core.Options
import Juvix.Compiler.Core.Transformation.Base
import Juvix.Compiler.Core.Transformation.Optimize.FilterUnreachable
import Juvix.Compiler.Core.Transformation.Optimize.Inlining
import Juvix.Compiler.Core.Transformation.Optimize.LambdaFolding
import Juvix.Compiler.Core.Transformation.Optimize.LetFolding
optimize' :: CoreOptions -> InfoTable -> InfoTable
optimize' CoreOptions {..} tab =
compose
(4 * _optOptimizationLevel)
( compose 2 (letFolding' (isInlineableLambda _optInliningDepth))
. lambdaFolding
. inlining' _optInliningDepth (recursiveIdents tab)
)
filterUnreachable
. compose
(4 * _optOptimizationLevel)
( compose 2 (letFolding' (isInlineableLambda _optInliningDepth))
. lambdaFolding
. inlining' _optInliningDepth (recursiveIdents tab)
)
. letFolding
$ tab