-
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.
Peephole optimization of Cairo assembly (#2858)
* Closes #2703 * Adds [peephole optimization](https://en.wikipedia.org/wiki/Peephole_optimization) of Cairo assembly. * Adds a transformation framework for the CASM IR. * Adds `--transforms`, `--run` and `--no-print` options to the `dev casm read` command.
- Loading branch information
Showing
18 changed files
with
327 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
module Commands.Dev.Casm.Read.Options where | ||
|
||
import CommonOptions | ||
import Juvix.Compiler.Casm.Data.TransformationId | ||
|
||
newtype CasmReadOptions = CasmReadOptions | ||
{ _casmReadInputFile :: AppPath File | ||
data CasmReadOptions = CasmReadOptions | ||
{ _casmReadTransformations :: [TransformationId], | ||
_casmReadRun :: Bool, | ||
_casmReadNoPrint :: Bool, | ||
_casmReadInputFile :: AppPath File | ||
} | ||
deriving stock (Data) | ||
|
||
makeLenses ''CasmReadOptions | ||
|
||
parseCasmReadOptions :: Parser CasmReadOptions | ||
parseCasmReadOptions = do | ||
_casmReadNoPrint <- optReadNoPrint | ||
_casmReadRun <- optReadRun | ||
_casmReadTransformations <- optCasmTransformationIds | ||
_casmReadInputFile <- parseInputFile FileExtCasm | ||
pure CasmReadOptions {..} |
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,34 @@ | ||
module Juvix.Compiler.Casm.Data.TransformationId where | ||
|
||
import Juvix.Compiler.Casm.Data.TransformationId.Strings | ||
import Juvix.Compiler.Core.Data.TransformationId.Base | ||
import Juvix.Prelude | ||
|
||
data TransformationId | ||
= IdentityTrans | ||
| Peephole | ||
deriving stock (Data, Bounded, Enum, Show) | ||
|
||
data PipelineId | ||
= PipelineCairo | ||
deriving stock (Data, Bounded, Enum) | ||
|
||
type TransformationLikeId = TransformationLikeId' TransformationId PipelineId | ||
|
||
toCairoTransformations :: [TransformationId] | ||
toCairoTransformations = [Peephole] | ||
|
||
instance TransformationId' TransformationId where | ||
transformationText :: TransformationId -> Text | ||
transformationText = \case | ||
IdentityTrans -> strIdentity | ||
Peephole -> strPeephole | ||
|
||
instance PipelineId' TransformationId PipelineId where | ||
pipelineText :: PipelineId -> Text | ||
pipelineText = \case | ||
PipelineCairo -> strCairoPipeline | ||
|
||
pipeline :: PipelineId -> [TransformationId] | ||
pipeline = \case | ||
PipelineCairo -> toCairoTransformations |
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,14 @@ | ||
module Juvix.Compiler.Casm.Data.TransformationId.Parser (parseTransformations, TransformationId (..), completions, completionsString) where | ||
|
||
import Juvix.Compiler.Casm.Data.TransformationId | ||
import Juvix.Compiler.Core.Data.TransformationId.Parser.Base | ||
import Juvix.Prelude | ||
|
||
parseTransformations :: Text -> Either Text [TransformationId] | ||
parseTransformations = parseTransformations' @TransformationId @PipelineId | ||
|
||
completionsString :: String -> [String] | ||
completionsString = completionsString' @TransformationId @PipelineId | ||
|
||
completions :: Text -> [Text] | ||
completions = completions' @TransformationId @PipelineId |
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,12 @@ | ||
module Juvix.Compiler.Casm.Data.TransformationId.Strings where | ||
|
||
import Juvix.Prelude | ||
|
||
strCairoPipeline :: Text | ||
strCairoPipeline = "pipeline-cairo" | ||
|
||
strIdentity :: Text | ||
strIdentity = "identity" | ||
|
||
strPeephole :: Text | ||
strPeephole = "peephole" |
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,17 @@ | ||
module Juvix.Compiler.Casm.Pipeline | ||
( module Juvix.Compiler.Casm.Pipeline, | ||
Options, | ||
Code, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Casm.Transformation | ||
import Juvix.Compiler.Pipeline.EntryPoint (EntryPoint) | ||
|
||
-- | Perform transformations on CASM necessary before the translation to Cairo | ||
-- bytecode | ||
toCairo' :: Code -> Sem r Code | ||
toCairo' = applyTransformations toCairoTransformations | ||
|
||
toCairo :: (Member (Reader EntryPoint) r) => Code -> Sem r Code | ||
toCairo = mapReader fromEntryPoint . toCairo' |
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,18 @@ | ||
module Juvix.Compiler.Casm.Transformation | ||
( module Juvix.Compiler.Casm.Transformation.Base, | ||
module Juvix.Compiler.Casm.Transformation, | ||
module Juvix.Compiler.Casm.Data.TransformationId, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Casm.Data.TransformationId | ||
import Juvix.Compiler.Casm.Transformation.Base | ||
import Juvix.Compiler.Casm.Transformation.Optimize.Peephole | ||
|
||
applyTransformations :: forall r. [TransformationId] -> Code -> Sem r Code | ||
applyTransformations ts tbl = foldM (flip appTrans) tbl ts | ||
where | ||
appTrans :: TransformationId -> Code -> Sem r Code | ||
appTrans = \case | ||
IdentityTrans -> return | ||
Peephole -> return . peephole |
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,17 @@ | ||
module Juvix.Compiler.Casm.Transformation.Base | ||
( module Juvix.Compiler.Casm.Transformation.Base, | ||
module Juvix.Compiler.Casm.Language, | ||
module Juvix.Compiler.Tree.Options, | ||
) | ||
where | ||
|
||
import Juvix.Compiler.Casm.Language | ||
import Juvix.Compiler.Tree.Options | ||
|
||
mapT :: ([Instruction] -> [Instruction]) -> [Instruction] -> [Instruction] | ||
mapT f = go | ||
where | ||
go :: [Instruction] -> [Instruction] | ||
go = \case | ||
i : is -> f (i : go is) | ||
[] -> f [] |
78 changes: 78 additions & 0 deletions
78
src/Juvix/Compiler/Casm/Transformation/Optimize/Peephole.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,78 @@ | ||
module Juvix.Compiler.Casm.Transformation.Optimize.Peephole where | ||
|
||
import Juvix.Compiler.Casm.Extra.Base | ||
import Juvix.Compiler.Casm.Language | ||
import Juvix.Compiler.Casm.Transformation.Base | ||
|
||
peephole :: [Instruction] -> [Instruction] | ||
peephole = mapT go | ||
where | ||
go :: [Instruction] -> [Instruction] | ||
go = \case | ||
Nop : is -> is | ||
Jump InstrJump {..} : lab@(Label LabelRef {..}) : is | ||
| not _instrJumpIncAp, | ||
Val (Lab (LabelRef sym _)) <- _instrJumpTarget, | ||
sym == _labelRefSymbol -> | ||
lab : is | ||
Call InstrCall {..} : Return : Assign a1 : Return : is | ||
| _instrCallRel, | ||
Imm 3 <- _instrCallTarget, | ||
Just k1 <- getAssignApFp a1 -> | ||
fixAssignAp $ | ||
mkAssignAp (Val (Ref (MemRef Ap k1))) | ||
: Return | ||
: is | ||
Call InstrCall {..} : Return : Assign a1 : Assign a2 : Return : is | ||
| _instrCallRel, | ||
Imm 3 <- _instrCallTarget, | ||
Just k1 <- getAssignApFp a1, | ||
Just k2 <- getAssignApFp a2 -> | ||
fixAssignAp $ | ||
mkAssignAp (Val (Ref (MemRef Ap k1))) | ||
: mkAssignAp (Val (Ref (MemRef Ap (k2 - 1)))) | ||
: Return | ||
: is | ||
Call InstrCall {..} : Return : Jump InstrJump {..} : is | ||
| _instrCallRel, | ||
Imm 3 <- _instrCallTarget, | ||
Val tgt@(Lab {}) <- _instrJumpTarget, | ||
not _instrJumpIncAp -> | ||
let call = | ||
InstrCall | ||
{ _instrCallTarget = tgt, | ||
_instrCallRel = _instrJumpRel | ||
} | ||
in Call call : Return : is | ||
is -> is | ||
|
||
fixAssignAp :: [Instruction] -> [Instruction] | ||
fixAssignAp = \case | ||
Assign a : Return : is | ||
| Just (-1) <- getAssignAp Ap a -> | ||
Return : is | ||
Assign a1 : Assign a2 : Return : is | ||
| Just (-2) <- getAssignAp Ap a1, | ||
Just (-2) <- getAssignAp Ap a2 -> | ||
Return : is | ||
Assign a1 : Assign a2 : Return : is | ||
| Just (-1) <- getAssignAp Ap a1, | ||
Just (-3) <- getAssignAp Ap a2 -> | ||
mkAssignAp (Val (Ref (MemRef Ap (-2)))) : Return : is | ||
is -> is | ||
|
||
getAssignAp :: Reg -> InstrAssign -> Maybe Offset | ||
getAssignAp reg InstrAssign {..} | ||
| MemRef Ap 0 <- _instrAssignResult, | ||
Val (Ref (MemRef r k)) <- _instrAssignValue, | ||
r == reg, | ||
_instrAssignIncAp = | ||
Just k | ||
| otherwise = | ||
Nothing | ||
|
||
getAssignApFp :: InstrAssign -> Maybe Offset | ||
getAssignApFp instr = case getAssignAp Fp instr of | ||
Just k | ||
| k <= -3 -> Just (k + 2) | ||
_ -> Nothing |
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.