peepmatic: Introduce the main peepmatic crate
Peepmatic is a DSL for peephole optimizations and compiler for generating peephole optimizers from them. The user writes a set of optimizations in the DSL, and then `peepmatic` compiles the set of optimizations into an efficient peephole optimizer: ``` DSL ----peepmatic----> Peephole Optimizer ``` The generated peephole optimizer has all of its optimizations' left-hand sides collapsed into a compact automata 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, `peepmatic` should 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/alive2
This commit is contained in:
31
cranelift/peepmatic/src/automatize.rs
Normal file
31
cranelift/peepmatic/src/automatize.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
//! Compile a set of linear optimizations into an automaton.
|
||||
|
||||
use peepmatic_automata::{Automaton, Builder};
|
||||
use peepmatic_runtime::linear;
|
||||
|
||||
/// Construct an automaton from a set of linear optimizations.
|
||||
pub fn automatize(
|
||||
opts: &linear::Optimizations,
|
||||
) -> Automaton<Option<u32>, linear::MatchOp, Vec<linear::Action>> {
|
||||
debug_assert!(crate::linear_passes::is_sorted_lexicographically(opts));
|
||||
|
||||
let mut builder = Builder::<Option<u32>, linear::MatchOp, Vec<linear::Action>>::new();
|
||||
|
||||
for opt in &opts.optimizations {
|
||||
let mut insertion = builder.insert();
|
||||
for inc in &opt.increments {
|
||||
// Ensure that this state's associated data is this increment's
|
||||
// match operation.
|
||||
if let Some(op) = insertion.get_state_data() {
|
||||
assert_eq!(*op, inc.operation);
|
||||
} else {
|
||||
insertion.set_state_data(inc.operation);
|
||||
}
|
||||
|
||||
insertion.next(inc.expected, inc.actions.clone());
|
||||
}
|
||||
insertion.finish();
|
||||
}
|
||||
|
||||
builder.finish()
|
||||
}
|
||||
Reference in New Issue
Block a user