-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Constant propagation in JuvixReg (#2833)
* Closes #2702 * For this to give any improvement, we need to run dead code elimination afterwards (#2827). Depends on: * #2828
- Loading branch information
Showing
18 changed files
with
268 additions
and
28 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
65 changes: 65 additions & 0 deletions
65
src/Juvix/Compiler/Reg/Transformation/ConstantPropagation.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,65 @@ | ||
module Juvix.Compiler.Reg.Transformation.ConstantPropagation where | ||
|
||
import Data.HashMap.Strict qualified as HashMap | ||
import Juvix.Compiler.Reg.Extra | ||
import Juvix.Compiler.Reg.Transformation.Base | ||
import Juvix.Compiler.Tree.Evaluator.Builtins | ||
|
||
type VarMap = HashMap VarRef Constant | ||
|
||
constantPropagateFunction :: Code -> Code | ||
constantPropagateFunction = | ||
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 {..} | ||
| ValConst c <- _instrAssignValue -> | ||
(HashMap.insert _instrAssignResult c mpv', instr') | ||
Binop InstrBinop {..} | ||
| ValConst c1 <- _instrBinopArg1, | ||
ValConst c2 <- _instrBinopArg2 -> | ||
case evalBinop' _instrBinopOpcode c1 c2 of | ||
Left _ -> (mpv', instr') | ||
Right c -> | ||
( HashMap.insert _instrBinopResult c mpv', | ||
Assign | ||
InstrAssign | ||
{ _instrAssignResult = _instrBinopResult, | ||
_instrAssignValue = ValConst c | ||
} | ||
) | ||
_ -> | ||
(mpv', instr') | ||
where | ||
instr' = overValueRefs' (adjustVarRef mpv) instr | ||
mpv' = maybe mpv (`HashMap.delete` mpv) (getResultVar instr) | ||
|
||
adjustVarRef :: VarMap -> VarRef -> Value | ||
adjustVarRef mpv vref@VarRef {..} = case _varRefGroup of | ||
VarGroupArgs -> VRef vref | ||
VarGroupLocal -> maybe (VRef vref) ValConst (HashMap.lookup vref mpv) | ||
|
||
combine :: Instruction -> NonEmpty VarMap -> (VarMap, Instruction) | ||
combine instr mpvs = case instr of | ||
Branch InstrBranch {..} | ||
| ValConst (ConstBool True) <- _instrBranchValue -> | ||
(mpv1, Block $ InstrBlock _instrBranchTrue) | ||
| ValConst (ConstBool False) <- _instrBranchValue -> | ||
(mpv2, Block $ InstrBlock _instrBranchFalse) | ||
where | ||
(mpv1, mpv2) = case mpvs of | ||
mpv1' :| [mpv2'] -> (mpv1', mpv2') | ||
_ -> impossible | ||
_ -> | ||
(combineMaps mpvs, instr) | ||
|
||
constantPropagate :: InfoTable -> InfoTable | ||
constantPropagate = mapT (const constantPropagateFunction) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Reg.Transformation.ConstantPropagation where | ||
|
||
import Base | ||
import Juvix.Compiler.Reg.Transformation | ||
import Reg.Parse.Positive qualified as Parse | ||
import Reg.Transformation.Base | ||
|
||
allTests :: TestTree | ||
allTests = testGroup "JuvixReg Constant Propagation" (map liftTest Parse.tests) | ||
|
||
pipe :: [TransformationId] | ||
pipe = [ConstantPropagation] | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
79 |
Oops, something went wrong.