use crate::ir::{OpKind, Region};
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
UseBeforeDef { value_id: u64, block: String },
TypeMismatch {
expected: String,
found: String,
block: String,
},
MissingTerminator { block: String },
WrongOperandCount {
op: String,
expected: usize,
found: usize,
block: String,
},
CyclicControlFlow { block: String },
}
impl std::fmt::Display for ValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
ValidationError::UseBeforeDef { value_id, block } => {
write!(
f,
"value %{} used before definition in block {}",
value_id, block
)
}
ValidationError::TypeMismatch {
expected,
found,
block,
} => {
write!(
f,
"type mismatch in block {}: expected {}, found {}",
block, expected, found
)
}
ValidationError::MissingTerminator { block } => {
write!(f, "block {} is missing a terminator", block)
}
ValidationError::WrongOperandCount {
op,
expected,
found,
block,
} => {
write!(
f,
"operation {} in block {} expects {} operands, found {}",
op, block, expected, found
)
}
ValidationError::CyclicControlFlow { block } => {
write!(f, "cyclic control flow detected involving block {}", block)
}
}
}
}
#[derive(Debug, Clone)]
pub struct ValidationResult {
pub errors: Vec<ValidationError>,
}
impl ValidationResult {
pub fn is_valid(&self) -> bool {
self.errors.is_empty()
}
pub fn error_count(&self) -> usize {
self.errors.len()
}
}
pub fn validate_region(region: &Region) -> ValidationResult {
let mut errors = Vec::new();
for block in ®ion.blocks {
match block.operations.last() {
Some(op) => {
let kind = &op.kind;
if !matches!(kind, OpKind::Return | OpKind::Branch) {
errors.push(ValidationError::MissingTerminator {
block: block.label.clone(),
});
}
}
None => {
errors.push(ValidationError::MissingTerminator {
block: block.label.clone(),
});
}
}
for op in &block.operations {
let expected = match op.kind {
OpKind::Addi
| OpKind::Subi
| OpKind::Muli
| OpKind::Addf
| OpKind::Subf
| OpKind::Mulf
| OpKind::And
| OpKind::Or
| OpKind::Xor
| OpKind::CmpEq
| OpKind::CmpLt => 2,
OpKind::Load => 1,
OpKind::Store => 2,
OpKind::Branch => 0,
OpKind::Return => 0,
OpKind::Constant(_) => 0,
OpKind::Gemm => 3, OpKind::Quantize => 1, OpKind::Dequantize => 1, OpKind::QuantGemm => 3, OpKind::QuantAttention => 4, OpKind::Custom(_) => 1,
};
if op.operands.len() != expected && !matches!(op.kind, OpKind::Custom(_)) {
errors.push(ValidationError::WrongOperandCount {
op: op.kind.to_string(),
expected,
found: op.operands.len(),
block: block.label.clone(),
});
}
}
}
ValidationResult { errors }
}
pub struct ValidatePass;
impl super::passes::Pass for ValidatePass {
fn name(&self) -> &str {
"validate"
}
fn run(&self, region: &Region) -> usize {
let result = validate_region(region);
if !result.is_valid() {
result.error_count()
} else {
0
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::{Block, OpKind, Operation, Region, Type, Value};
#[test]
fn test_valid_region() {
let region = Region::new();
let result = validate_region(®ion);
assert!(result.is_valid());
}
#[test]
fn test_missing_terminator() {
let mut region = Region::new();
let mut block = Block::new("entry");
let mut op = Operation::new(OpKind::Addi);
op.operands.push(Value::new(0, Type::primitive("i32")));
op.operands.push(Value::new(1, Type::primitive("i32")));
block.operations.push(op);
region.blocks.push(block);
let result = validate_region(®ion);
assert!(!result.is_valid());
assert_eq!(result.errors.len(), 1);
}
}