-
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.
Copy propagation in JuvixReg (#2828)
* Closes #1614 * Implements the copy propagation transformation in JuvixReg and adds tests for it. * For this optimization to give any improvement, we need to run dead code elimination afterwards (#2827).
- Loading branch information
Showing
16 changed files
with
302 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
module Juvix.Compiler.Reg.Transformation.CopyPropagation where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Data.HashSet qualified as HashSet | ||
import Juvix.Compiler.Reg.Extra | ||
import Juvix.Compiler.Reg.Transformation.Base | ||
|
||
type VarMap = HashMap VarRef VarRef | ||
|
||
copyPropagateFunction :: Code -> Code | ||
copyPropagateFunction = | ||
snd | ||
. runIdentity | ||
. recurseF | ||
ForwardRecursorSig | ||
{ _forwardFun = \i acc -> return (go i acc), | ||
_forwardCombine = combine | ||
} | ||
mempty | ||
where | ||
go :: Instruction -> VarMap -> (VarMap, Instruction) | ||
go instr mpv = case instr' of | ||
Assign InstrAssign {..} | ||
| VRef v <- _instrAssignValue -> | ||
(HashMap.insert _instrAssignResult v mpv', instr') | ||
_ -> | ||
(mpv', instr') | ||
where | ||
instr' = overValueRefs (adjustVarRef mpv) instr | ||
mpv' = maybe mpv (filterOutVars mpv) (getResultVar instr) | ||
|
||
filterOutVars :: VarMap -> VarRef -> VarMap | ||
filterOutVars mpv v = HashMap.delete v $ HashMap.filter (/= v) mpv | ||
|
||
adjustVarRef :: VarMap -> VarRef -> VarRef | ||
adjustVarRef mpv vref@VarRef {..} = case _varRefGroup of | ||
VarGroupArgs -> vref | ||
VarGroupLocal -> fromMaybe vref $ HashMap.lookup vref mpv | ||
|
||
combine :: Instruction -> NonEmpty VarMap -> (VarMap, Instruction) | ||
combine instr mpvs = (mpv, instr') | ||
where | ||
mpv' :| mpvs' = fmap HashMap.toList mpvs | ||
mpv = | ||
HashMap.fromList | ||
. HashSet.toList | ||
. foldr (HashSet.intersection . HashSet.fromList) (HashSet.fromList mpv') | ||
$ mpvs' | ||
|
||
instr' = case instr of | ||
Branch x -> Branch $ over instrBranchOutVar (fmap (adjustVarRef mpv)) x | ||
Case x -> Case $ over instrCaseOutVar (fmap (adjustVarRef mpv)) x | ||
_ -> impossible | ||
|
||
copyPropagate :: InfoTable -> InfoTable | ||
copyPropagate = mapT (const copyPropagateFunction) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Reg.Transformation.CopyPropagation where | ||
|
||
import Base | ||
import Juvix.Compiler.Reg.Transformation | ||
import Reg.Parse.Positive qualified as Parse | ||
import Reg.Transformation.Base | ||
|
||
allTests :: TestTree | ||
allTests = testGroup "Copy Propagation" (map liftTest Parse.tests) | ||
|
||
pipe :: [TransformationId] | ||
pipe = [CopyPropagation] | ||
|
||
liftTest :: Parse.PosTest -> TestTree | ||
liftTest _testRun = | ||
fromTest | ||
Test | ||
{ _testTransformations = pipe, | ||
_testAssertion = const (return ()), | ||
_testRun | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
82 |
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 @@ | ||
79 |
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,84 @@ | ||
-- Copy & constant propagation | ||
|
||
type either { | ||
left : integer -> either; | ||
right : bool -> either; | ||
} | ||
|
||
function main() : * { | ||
tmp[0] = 7; | ||
tmp[1] = tmp[0]; | ||
tmp[0] = tmp[1]; | ||
tmp[2] = tmp[0]; | ||
-- tmp[2] = 7 | ||
|
||
tmp[1] = tmp[0]; | ||
tmp[0] = add tmp[1] 1; | ||
tmp[2] = add tmp[2] tmp[1]; | ||
-- tmp[2] = 14 | ||
|
||
tmp[0] = 19; | ||
tmp[1] = tmp[0]; | ||
tmp[0] = add tmp[1] 1; | ||
tmp[3] = add tmp[0] tmp[1]; | ||
tmp[4] = tmp[3]; | ||
tmp[2] = add tmp[4] tmp[2]; | ||
-- tmp[2] = 53 | ||
|
||
tmp[1] = eq tmp[2] 54; | ||
tmp[0] = 4; | ||
tmp[3] = 3; | ||
tmp[4] = tmp[0]; | ||
tmp[5] = 4; | ||
tmp[6] = tmp[5]; | ||
br tmp[1] { | ||
true: { | ||
tmp[4] = 7; | ||
}; | ||
false: { | ||
tmp[3] = tmp[6]; | ||
}; | ||
}; | ||
tmp[2] = add tmp[2] tmp[4]; | ||
tmp[2] = add tmp[2] tmp[3]; | ||
-- tmp[2] = 61 | ||
|
||
tmp[0] = alloc left (3); | ||
tmp[1] = 17; | ||
tmp[3] = tmp[1]; | ||
case[either] tmp[0] { | ||
left: { | ||
tmp[4] = tmp[0].left[0]; | ||
tmp[1] = tmp[4]; | ||
tmp[3] = tmp[1]; | ||
}; | ||
right: { | ||
nop; | ||
}; | ||
}; | ||
tmp[2] = add tmp[2] tmp[3]; | ||
-- tmp[2] = 64 | ||
|
||
tmp[0] = alloc right (true); | ||
tmp[1] = 17; | ||
tmp[3] = tmp[1]; | ||
case[either] tmp[0] { | ||
left: { | ||
tmp[1] = tmp[0].left[0]; | ||
}; | ||
right: { | ||
br tmp[0].right[0] { | ||
true: { | ||
tmp[1] = add tmp[3] 1; | ||
}; | ||
false: { | ||
nop; | ||
}; | ||
}; | ||
}; | ||
}; | ||
tmp[2] = add tmp[2] tmp[1]; | ||
-- tmp[2] = 82 | ||
|
||
ret tmp[2]; | ||
} |
Oops, something went wrong.