tpt-gpu-compiler 0.1.0

Rust port of TPTIR compiler stack
Documentation
//! TPTIR semantic validator pass.
//!
//! Implements the `ValidatePass` that checks TPTIR IR for semantic correctness:
//! - All values used are defined (no use-before-def)
//! - Type consistency of operands and results
//! - No cyclic control flow
//! - Block terminators are valid
//! - Operations have correct operand counts

use crate::ir::{OpKind, Region};

/// Validation error type.
#[derive(Debug, Clone, PartialEq)]
pub enum ValidationError {
    /// A value is used before it is defined.
    UseBeforeDef { value_id: u64, block: String },
    /// Type mismatch between operand and expected type.
    TypeMismatch {
        expected: String,
        found: String,
        block: String,
    },
    /// A block is missing a terminator.
    MissingTerminator { block: String },
    /// An operation has the wrong number of operands.
    WrongOperandCount {
        op: String,
        expected: usize,
        found: usize,
        block: String,
    },
    /// A cyclic control flow was detected.
    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)
            }
        }
    }
}

/// Result of a validation pass.
#[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()
    }
}

/// Validate a TPTIR region.
pub fn validate_region(region: &Region) -> ValidationResult {
    let mut errors = Vec::new();

    for block in &region.blocks {
        // Check that the block has a terminator
        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(),
                });
            }
        }

        // Check operand counts for each operation
        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,           // A, B, C
                OpKind::Quantize => 1,       // input tensor
                OpKind::Dequantize => 1,     // quantized tensor
                OpKind::QuantGemm => 3,      // A_int, B_int, C_f32
                OpKind::QuantAttention => 4, // Q, K, V, mask
                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 }
}

/// The ValidatePass struct for use in pass pipelines.
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() {
            // In a real implementation, we would report errors.
            // For now, return the number of errors as the change count.
            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(&region);
        // An empty region has no blocks, so no errors
        assert!(result.is_valid());
    }

    #[test]
    fn test_missing_terminator() {
        let mut region = Region::new();
        let mut block = Block::new("entry");
        // Add an operation (with a correct operand count, to isolate the
        // missing-terminator check) but no terminator
        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(&region);
        assert!(!result.is_valid());
        assert_eq!(result.errors.len(), 1);
    }
}