Skip to content

Commit

Permalink
inline before specialize
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcz committed Nov 25, 2024
1 parent 25e4b57 commit 2cec4a3
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
11 changes: 10 additions & 1 deletion src/Juvix/Compiler/Core/Data/IdentDependencyInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,20 @@ recursiveIdentsClosure tab =
chlds = fromJust $ HashMap.lookup sym graph

-- | Complement of recursiveIdentsClosure
nonRecursiveReachableIdents' :: InfoTable -> HashSet Symbol
nonRecursiveReachableIdents' tab =
HashSet.difference
(HashSet.fromList (HashMap.keys (tab ^. infoIdentifiers)))
(recursiveIdentsClosure tab)

nonRecursiveReachableIdents :: Module -> HashSet Symbol
nonRecursiveReachableIdents = nonRecursiveReachableIdents' . computeCombinedInfoTable

nonRecursiveIdents' :: InfoTable -> HashSet Symbol
nonRecursiveIdents' tab =
HashSet.difference
(HashSet.fromList (HashMap.keys (tab ^. infoIdentifiers)))
(recursiveIdentsClosure tab)
(recursiveIdents' tab)

nonRecursiveIdents :: Module -> HashSet Symbol
nonRecursiveIdents = nonRecursiveIdents' . computeCombinedInfoTable
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ constantFolding' opts nonRecSyms tab md =
-- zero-order. For example, `3 + 4` is evaluated to `7`, and `id 3` is evaluated
-- to `3`, but `id id` is not evaluated because the target type is not
-- zero-order (it's a function type). This optimization is only applied to
-- non-recursive symbols.
-- symbols from which no recursive symbols can be reached.
--
-- References:
-- - https://github.com/anoma/juvix/pull/2450
-- - https://github.com/anoma/juvix/issues/2154
constantFolding :: (Member (Reader CoreOptions) r) => Module -> Sem r Module
constantFolding md = do
opts <- ask
return $ constantFolding' opts (nonRecursiveIdents' tab) tab md
return $ constantFolding' opts (nonRecursiveReachableIdents' tab) tab md
where
tab = computeCombinedInfoTable md
11 changes: 7 additions & 4 deletions src/Juvix/Compiler/Core/Transformation/Optimize/Phase/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ optimize' opts@CoreOptions {..} md =
. compose
(6 * _optOptimizationLevel)
( doConstantFolding
. doSimplification 2
. doInlining
. doSimplification 1
. specializeArgs
. doSimplification 2
. doInlining
)
. doConstantFolding
. letFolding
Expand All @@ -36,13 +36,16 @@ optimize' opts@CoreOptions {..} md =
nonRecs :: HashSet Symbol
nonRecs = nonRecursiveIdents' tab

nonRecsReachable :: HashSet Symbol
nonRecsReachable = nonRecursiveReachableIdents' tab

doConstantFolding :: Module -> Module
doConstantFolding md' = constantFolding' opts nonRecs' tab' md'
where
tab' = computeCombinedInfoTable md'
nonRecs'
| _optOptimizationLevel > 1 = nonRecursiveIdents' tab'
| otherwise = nonRecs
| _optOptimizationLevel > 1 = nonRecursiveReachableIdents' tab'
| otherwise = nonRecsReachable

doInlining :: Module -> Module
doInlining md' = inlining' _optInliningDepth nonRecs' md'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ convertNode = dmapLRM go
fun = reLambdas lams' body''
letitem =
mkLetItem
(ii ^. identifierName)
("spec_" <> ii ^. identifierName)
-- the type is not in the scope of the binder
(shift (-1) ty')
fun
Expand Down
2 changes: 1 addition & 1 deletion src/Juvix/Compiler/Internal/Translation/FromConcrete.hs
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ deriveEq DerivingArgs {..} = do
indInfo <- getIndInfo
let argty = getArgType indInfo
argsInfo <- goArgsInfo _derivingInstanceName
lamName <- Internal.freshFunVar (getLoc _derivingInstanceName) ("__eq__" <> _derivingInstanceName ^. Internal.nameText)
lamName <- Internal.freshFunVar (getLoc _derivingInstanceName) ("eq__" <> _derivingInstanceName ^. Internal.nameText)
let lam = Internal.ExpressionIden (Internal.IdenFunction lamName)
lamFun <- eqLambda lam indInfo argty
lamTy <- Internal.ExpressionHole <$> Internal.freshHole (getLoc _derivingInstanceName)
Expand Down

0 comments on commit 2cec4a3

Please sign in to comment.