fitzgen opened PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
fitzgen requested sunfishcode for a review on PR #1647.
bjorn3 submitted PR Review.
bjorn3 submitted PR Review.
bjorn3 created PR Review Comment:
Maybe construct this map on the fly from
valueswhen deserializing?
bjorn3 created PR Review Comment:
False,
bjorn3 created PR Review Comment:
Is there any use for this function as opposed to using
intern?
bjorn3 created PR Review Comment:
It would be nice to start this doc comment with a single sentence describing this type in short, followed by a blank line. Rustdoc will then show that sentence and only that sentence when visiting the documentation of the parent module.
bjorn3 created PR Review Comment:
/// Implicitly define the n^th RHS as a condition code.
bjorn3 created PR Review Comment:
Is there any use for peephole optimization of
adjust_sp_down{,_imm}?
bjorn3 created PR Review Comment:
"implicitly" is repeated for all variants.
bjorn3 created PR Review Comment:
/// A map from a path (whose owned data is inside `arena`) to the canonical /// `PathId` we assigned it when interning it. map: HashMap<UnsafePath, PathId>, /// A map from a `PathId` index to an unsafe, self-borrowed path pointing /// into `arena`. It is safe to given these out as safe `Path`s, as long as /// the lifetime is not longer than this `PathInterner`'s lifetime. paths: Vec<UnsafePath>, /// Bump allocation arena for path data. The bump arena ensures that these /// allocations never move, and are therefore safe for self-references.These show up when using
--document-private-items.
bjorn3 submitted PR Review.
bjorn3 submitted PR Review.
bjorn3 created PR Review Comment:
You can already match on
ValueDefyourself.
bjorn3 created PR Review Comment:
Maybe require that
srcis already not in the layout?
bjorn3 created PR Review Comment:
Please add a CI check that
preopt.serializedis up to date.
bjorn3 created PR Review Comment:
Is 1 unreachable?
bjorn3 created PR Review Comment:
Same here re size of 1.
bjorn3 created PR Review Comment:
If this is for a signed operation, the immediate needs to be sign extended.
bjorn3 created PR Review Comment:
Why are the constants here at the beginning, instead of at the end like in the real clif ir?
bjorn3 created PR Review Comment:
To be honest reading through this file was tiresome. Maybe a more rust like syntax would help with this?
bjorn3 created PR Review Comment:
Maybe avoid duplication by moving the
linear::Optimizationconstruction into the macro, leaving this closure to just return a Vec. Also you can try to cast the closure tofn(&mut dyn FnMut(&[u8]) -> PathId, &mut dyn FnMut(u64) -> IntegerId) -> Vec<linear::Increment>before calling it. This may help type inference enough to not need explicit type annotations here.
bjorn3 submitted PR Review.
bjorn3 created PR Review Comment:
This doesn't check if the peephole optimizers were up to date. Only that they can be rebuild.
bjorn3 deleted PR Review Comment.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Yes, it is used when evaluating
MatchOp::IntegerValue-- seepeepmatic/crates/runtime/src/optimizer.rs.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Nopis closer toTruethanFalse, but I think renaming it really clarifies anything here.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Yes, to convert
adjust_sp_downwith a constant argument into anadjust_sp_down_imm.
fitzgen created PR Review Comment:
In clif, some immediates are at the beginning (e.g. condition codes) and some are at the end (e.g. constant values). For simplicity, I implemented peepmatic such that all immediates come first, followed by all operands. There is no inherent reason it needs to be this way.
fitzgen submitted PR Review.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
S-expressions are simple and easy to parse. Plus we have nice infrastructure for them in the
wastcrate. Syntax and parsing is largely busy work, and not the interesting bits of this work. If the cranelift team feels strongly, we can change this, but it isn't worth debating about before landing this PR.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
This is true, but it is nice having a convenience method, same as why
Result<T, E>has anokmethod.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
This would require that we remove it ourselves before calling this method, which would be a bunch of duplication.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
This is something we could do in the future with a custom serialization and deserialization implementation.
bjorn3 submitted PR Review.
bjorn3 created PR Review Comment:
This order is confusing for
isub_imm.
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Nice idea!
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
I haven't been able to create a test case where I can observe the lack of sign extension, and the fact that the existing code wasn't doing this either makes me think it isn't necessary. Can you provide a test case that shows this?
fitzgen updated PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
fitzgen updated PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
fitzgen updated PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
fitzgen updated PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
bjorn3 submitted PR Review.
bjorn3 created PR Review Comment:
See https://github.com/bytecodealliance/wasmtime/issues/1095
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Thanks. I'll work on fixing #1095 at the
InstructionBuilderlevel, as suggested in that issue, rather than in this PR.
sunfishcode submitted PR Review.
sunfishcode submitted PR Review.
sunfishcode created PR Review Comment:
I don't see the
funcorisalifetimes being used outside of this declaration, so it's not clear whydo_preopt's signature changed here. Does theoptimizerhave any state that lives longer than thedo_preoptcall?
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
This might be left over from some bits that ultimately got thrown away; I'll try and remove it and on the off chance that I can't, then I'll have an explanation for why not ;)
fitzgen submitted PR Review.
fitzgen created PR Review Comment:
Ok, these are removed.
fitzgen updated PR #1647 from integrate-peepmatic to master:
This PR introduces
peepmatic, a peephole optimizations DSL and peephole optimizer compiler.Developers write a set of optimizations in the DSL, and then
peepmaticcompiles the set of optimizations into an efficient peephole optimizer:DSL ----peepmatic----> Peephole OptimizerThe generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact transducer automaton that makes matching candidate instruction sequences fast.
The DSL's optimizations may be written by hand or discovered mechanically with a superoptimizer like [Souper][]. Eventually,
peepmaticshould have a verifier that ensures that the DSL's optimizations are sound, similar to what [Alive][] does for LLVM optimizations.[Souper]: https://github.com/google/souper
[Alive]: https://github.com/AliveToolkit/alive2Learn More
cranelift/peepmatic/README.mdhas an overview of the DSL and implementationHere is a slide deck I am presenting at the 2020-05-04 Cranelift meeting
Current Status
I've ported most of
simple_preopt.rstopeepmatic's DSLAll tests are passing
I've been doing lots and lots of fuzzing
Next Steps
This work is not complete, but I think it is at a good point to merge into Cranelift and then evolve in-tree.
The next steps after landing this PR are:
Port the rest of
simple_preopt.rsover topeepmaticPort
postopt.rsover topeepmaticOptimize the runtime that interprets the generated peephole optimizations transducers and applies them
Extend
peepmaticto work with the new backend'sMachInstand vcodeFor even further future directions, see the discussion in the slides, linked above.
fitzgen merged PR #1647.
Last updated: Dec 06 2025 at 06:05 UTC