-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize single-argument unknown function invocations with known non-…
…function target types (#3270) * Closes #2935 * Implements the `ConvertUnaryCalls` optimization which replaces generic calls to unknown unary functions with known non-function target types by direct closure calls * Adds an optimization phase in JuvixTree * Adds options to the JuvixTree pipeline * Fixes a bug in Rust code generation for direct closure calls
- Loading branch information
Showing
27 changed files
with
233 additions
and
29 deletions.
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
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
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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
src/Juvix/Compiler/Tree/Transformation/Optimize/ConvertUnaryCalls.hs
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,53 @@ | ||
module Juvix.Compiler.Tree.Transformation.Optimize.ConvertUnaryCalls where | ||
|
||
import Juvix.Compiler.Core.Data.BinderList qualified as BL | ||
import Juvix.Compiler.Tree.Data.InfoTable | ||
import Juvix.Compiler.Tree.Extra.Recursors | ||
import Juvix.Compiler.Tree.Extra.Type | ||
import Juvix.Compiler.Tree.Transformation.Base | ||
|
||
-- | Replaces generic calls (with CallClosures) to unknown unary functions with | ||
-- known non-function target types by direct closure calls (with Call) | ||
convertUnaryCalls :: InfoTable -> InfoTable | ||
convertUnaryCalls tab = mapT convert tab | ||
where | ||
convert :: Symbol -> Node -> Node | ||
convert sym = umapL (go argtys) | ||
where | ||
funInfo = lookupFunInfo tab sym | ||
argtys | ||
| funInfo ^. functionArgsNum == 0 = [] | ||
| otherwise = typeArgs (funInfo ^. functionType) | ||
|
||
go :: [Type] -> BinderList TempVar -> Node -> Node | ||
go argtys tmps node = case node of | ||
CallClosures ncl@NodeCallClosures {..} | ||
| length _nodeCallClosuresArgs == 1 -> | ||
case _nodeCallClosuresFun of | ||
MemRef NodeMemRef {..} | ||
| DRef (ArgRef OffsetRef {..}) <- _nodeMemRef, | ||
isUnaryWithAtomicTarget (argtys !! _offsetRefOffset) -> | ||
mkClosureCall ncl | ||
| DRef (TempRef (RefTemp OffsetRef {..})) <- _nodeMemRef, | ||
isUnaryWithAtomicTarget (BL.lookupLevel _offsetRefOffset tmps ^. tempVarType) -> | ||
mkClosureCall ncl | ||
| ConstrRef (Field {..}) <- _nodeMemRef, | ||
constrInfo <- lookupConstrInfo tab _fieldTag, | ||
isUnaryWithAtomicTarget (typeArgs (constrInfo ^. constructorType) !! _fieldOffset) -> | ||
mkClosureCall ncl | ||
_ -> node | ||
_ -> node | ||
|
||
mkClosureCall :: NodeCallClosures -> Node | ||
mkClosureCall NodeCallClosures {..} = | ||
Call | ||
NodeCall | ||
{ _nodeCallInfo = _nodeCallClosuresInfo, | ||
_nodeCallType = CallClosure _nodeCallClosuresFun, | ||
_nodeCallArgs = toList _nodeCallClosuresArgs | ||
} | ||
|
||
isUnaryWithAtomicTarget :: Type -> Bool | ||
isUnaryWithAtomicTarget ty = | ||
length (typeArgs ty) == 1 | ||
&& isConcreteAtomType (typeTarget ty) |
9 changes: 9 additions & 0 deletions
9
src/Juvix/Compiler/Tree/Transformation/Optimize/Phase/Main.hs
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,9 @@ | ||
module Juvix.Compiler.Tree.Transformation.Optimize.Phase.Main where | ||
|
||
import Juvix.Compiler.Tree.Transformation.Base | ||
import Juvix.Compiler.Tree.Transformation.Optimize.ConvertUnaryCalls | ||
|
||
optimize :: (Member (Reader Options) r) => InfoTable -> Sem r InfoTable | ||
optimize = | ||
withOptimizationLevel 1 $ | ||
return . convertUnaryCalls |
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
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
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
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
Oops, something went wrong.