peepmatic: Be generic over the operator type
This lets us avoid the cost of `cranelift_codegen::ir::Opcode` to `peepmatic_runtime::Operator` conversion overhead, and paves the way for allowing Peepmatic to support non-clif optimizations (e.g. vcode optimizations). Rather than defining our own `peepmatic::Operator` type like we used to, now the whole `peepmatic` crate is effectively generic over a `TOperator` type parameter. For the Cranelift integration, we use `cranelift_codegen::ir::Opcode` as the concrete type for our `TOperator` type parameter. For testing, we also define a `TestOperator` type, so that we can test Peepmatic code without building all of Cranelift, and we can keep them somewhat isolated from each other. The methods that `peepmatic::Operator` had are now translated into trait bounds on the `TOperator` type. These traits need to be shared between all of `peepmatic`, `peepmatic-runtime`, and `cranelift-codegen`'s Peepmatic integration. Therefore, these new traits live in a new crate: `peepmatic-traits`. This crate acts as a header file of sorts for shared trait/type/macro definitions. Additionally, the `peepmatic-runtime` crate no longer depends on the `peepmatic-macro` procedural macro crate, which should lead to faster build times for Cranelift when it is using pre-built peephole optimizers.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
//! Interfacing with actual instructions.
|
||||
|
||||
use crate::operator::Operator;
|
||||
use crate::part::{Constant, Part};
|
||||
use crate::paths::Path;
|
||||
use crate::r#type::Type;
|
||||
use std::fmt::Debug;
|
||||
use std::hash::Hash;
|
||||
use std::num::NonZeroU32;
|
||||
|
||||
/// A trait for interfacing with actual instruction sequences.
|
||||
///
|
||||
@@ -32,6 +33,9 @@ pub unsafe trait InstructionSet<'a> {
|
||||
/// implementation.
|
||||
type Context;
|
||||
|
||||
/// An operator.
|
||||
type Operator: 'static + Copy + Debug + Eq + Hash + Into<NonZeroU32>;
|
||||
|
||||
/// An instruction (or identifier for an instruction).
|
||||
type Instruction: Copy + Debug + Eq;
|
||||
|
||||
@@ -64,10 +68,12 @@ pub unsafe trait InstructionSet<'a> {
|
||||
|
||||
/// Get the given instruction's operator.
|
||||
///
|
||||
/// If the instruction's opcode does not have an associated
|
||||
/// `peepmatic_runtime::operator::Operator` variant (i.e. that instruction
|
||||
/// isn't supported by `peepmatic` yet) then `None` should be returned.
|
||||
fn operator(&self, context: &mut Self::Context, instr: Self::Instruction) -> Option<Operator>;
|
||||
/// If the instruction isn't supported, then `None` should be returned.
|
||||
fn operator(
|
||||
&self,
|
||||
context: &mut Self::Context,
|
||||
instr: Self::Instruction,
|
||||
) -> Option<Self::Operator>;
|
||||
|
||||
/// Make a unary instruction.
|
||||
///
|
||||
@@ -76,7 +82,7 @@ pub unsafe trait InstructionSet<'a> {
|
||||
&self,
|
||||
context: &mut Self::Context,
|
||||
root: Self::Instruction,
|
||||
operator: Operator,
|
||||
operator: Self::Operator,
|
||||
r#type: Type,
|
||||
a: Part<Self::Instruction>,
|
||||
) -> Self::Instruction;
|
||||
@@ -92,7 +98,7 @@ pub unsafe trait InstructionSet<'a> {
|
||||
&self,
|
||||
context: &mut Self::Context,
|
||||
root: Self::Instruction,
|
||||
operator: Operator,
|
||||
operator: Self::Operator,
|
||||
r#type: Type,
|
||||
a: Part<Self::Instruction>,
|
||||
b: Part<Self::Instruction>,
|
||||
@@ -108,7 +114,7 @@ pub unsafe trait InstructionSet<'a> {
|
||||
&self,
|
||||
context: &mut Self::Context,
|
||||
root: Self::Instruction,
|
||||
operator: Operator,
|
||||
operator: Self::Operator,
|
||||
r#type: Type,
|
||||
a: Part<Self::Instruction>,
|
||||
b: Part<Self::Instruction>,
|
||||
|
||||
Reference in New Issue
Block a user