|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +from kirin import ir, interp |
| 4 | +from kirin.lattice import EmptyLattice |
| 5 | +from kirin.analysis import Forward |
| 6 | +from kirin.dialects import scf |
| 7 | +from kirin.validation import ValidationPass |
| 8 | +from kirin.analysis.forward import ForwardFrame |
| 9 | + |
| 10 | +from bloqade.qasm2.passes.unroll_if import DontLiftType |
| 11 | + |
| 12 | + |
| 13 | +class _QASM2ValidationAnalysis(Forward[EmptyLattice]): |
| 14 | + keys = ["qasm2.main.validation"] |
| 15 | + |
| 16 | + lattice = EmptyLattice |
| 17 | + |
| 18 | + def method_self(self, method: ir.Method) -> EmptyLattice: |
| 19 | + return self.lattice.bottom() |
| 20 | + |
| 21 | + def eval_fallback( |
| 22 | + self, frame: ForwardFrame[EmptyLattice], node: ir.Statement |
| 23 | + ) -> tuple[EmptyLattice, ...]: |
| 24 | + return tuple(self.lattice.bottom() for _ in range(len(node.results))) |
| 25 | + |
| 26 | + |
| 27 | +@scf.dialect.register(key="qasm2.main.validation") |
| 28 | +class __ScfMethods(interp.MethodTable): |
| 29 | + |
| 30 | + @interp.impl(scf.IfElse) |
| 31 | + def if_else( |
| 32 | + self, |
| 33 | + interp_: _QASM2ValidationAnalysis, |
| 34 | + frame: ForwardFrame[EmptyLattice], |
| 35 | + stmt: scf.IfElse, |
| 36 | + ): |
| 37 | + |
| 38 | + # TODO: stmt.condition has to be based off a measurement |
| 39 | + |
| 40 | + if len(stmt.then_body.blocks) > 1: |
| 41 | + interp_.add_validation_error( |
| 42 | + stmt, |
| 43 | + ir.ValidationError( |
| 44 | + stmt, |
| 45 | + "Only single block is allowed in the then-body of an if-else statement!", |
| 46 | + ), |
| 47 | + ) |
| 48 | + |
| 49 | + then_stmts = list(stmt.then_body.stmts()) |
| 50 | + if len(then_stmts) > 2: |
| 51 | + interp_.add_validation_error( |
| 52 | + stmt, |
| 53 | + ir.ValidationError( |
| 54 | + stmt, |
| 55 | + "Only single statements are allowed inside the then-body of an if-else statement!", |
| 56 | + ), |
| 57 | + ) |
| 58 | + |
| 59 | + if not isinstance(then_stmts[0], DontLiftType): |
| 60 | + interp_.add_validation_error( |
| 61 | + stmt, |
| 62 | + ir.ValidationError( |
| 63 | + stmt, f"Statement {then_stmts[0]} not allowed inside if clause!" |
| 64 | + ), |
| 65 | + ) |
| 66 | + |
| 67 | + self.__validate_empty_yield(interp_, then_stmts[-1]) |
| 68 | + |
| 69 | + if len(stmt.else_body.blocks) > 1: |
| 70 | + interp_.add_validation_error( |
| 71 | + stmt, |
| 72 | + ir.ValidationError( |
| 73 | + stmt, |
| 74 | + "Only single block is allowed in the else-body of an if-else statement!", |
| 75 | + ), |
| 76 | + ) |
| 77 | + |
| 78 | + else_stmts = list(stmt.else_body.stmts()) |
| 79 | + if len(else_stmts) > 1: |
| 80 | + interp_.add_validation_error( |
| 81 | + stmt, |
| 82 | + ir.ValidationError(stmt, "Non-empty else is not allowed in QASM2!"), |
| 83 | + ) |
| 84 | + |
| 85 | + self.__validate_empty_yield(interp_, else_stmts[-1]) |
| 86 | + |
| 87 | + def __validate_empty_yield( |
| 88 | + self, interp_: _QASM2ValidationAnalysis, stmt: ir.Statement |
| 89 | + ): |
| 90 | + if not isinstance(stmt, scf.Yield): |
| 91 | + interp_.add_validation_error( |
| 92 | + stmt, |
| 93 | + ir.ValidationError( |
| 94 | + stmt, f"Expected scf.Yield terminator in if clause, got {stmt}" |
| 95 | + ), |
| 96 | + ) |
| 97 | + elif len(stmt.values) > 0: |
| 98 | + interp_.add_validation_error( |
| 99 | + stmt, ir.ValidationError(stmt, "Cannot yield values from if statement!") |
| 100 | + ) |
| 101 | + |
| 102 | + @interp.impl(scf.For) |
| 103 | + def for_loop( |
| 104 | + self, |
| 105 | + interp_: _QASM2ValidationAnalysis, |
| 106 | + frame: ForwardFrame[EmptyLattice], |
| 107 | + stmt: scf.For, |
| 108 | + ): |
| 109 | + interp_.add_validation_error( |
| 110 | + stmt, ir.ValidationError(stmt, "Loops not supported in QASM2!") |
| 111 | + ) |
| 112 | + |
| 113 | + |
| 114 | +class QASM2Validation(ValidationPass): |
| 115 | + def name(self) -> str: |
| 116 | + return "QASM2 validation" |
| 117 | + |
| 118 | + def run(self, method: ir.Method) -> tuple[Any, list[ir.ValidationError]]: |
| 119 | + analysis = _QASM2ValidationAnalysis(method.dialects) |
| 120 | + frame, _ = analysis.run(method) |
| 121 | + return frame, analysis.get_validation_errors() |
0 commit comments