Skip to content

Encode

BytecodeEncoder -- stack_move ir.Method → syntactic Program.

The encoder assumes the input ir.Method is stack-consistent: every SSA value is used exactly once and the defining statements appear in the order that satisfies the bytecode stack discipline (deepest arg defined first, top-of-stack arg defined last). load_program (decode.py) always produces stack-consistent IR, so round-trips work unconditionally. IR produced by compiler rewrites (e.g. RewriteMoveToStackMove) may violate the ordering invariant and must be normalised by stackify before encoding.

Implemented as a kirin EmitABC pass: each dialect registers its own MethodTable under the key "emit.bytecode", and the encoder dispatches via the standard kirin interpreter machinery. The encoded instructions accumulate in BytecodeEncoder.instructions; call dump_program for the one-shot public API.

BytecodeEncoder dataclass

BytecodeEncoder(instructions: list[Instruction] = list())

Bases: EmitABC[EmitFrame, Program]


              flowchart TD
              bloqade.lanes.bytecode.encode.BytecodeEncoder[BytecodeEncoder]

              

              click bloqade.lanes.bytecode.encode.BytecodeEncoder href "" "bloqade.lanes.bytecode.encode.BytecodeEncoder"
            

Turn a stack_move ir.Method into a bytecode Program.

Constructed with the method's dialect group (BytecodeEncoder(dialects=method.dialects)). Call run(method) to populate self.instructions, then build the Program from them. Prefer dump_program for the one-shot public API.

EncodingError

EncodingError(stmt: Statement, reason: str | None = None)

Bases: Exception


              flowchart TD
              bloqade.lanes.bytecode.encode.EncodingError[EncodingError]

              

              click bloqade.lanes.bytecode.encode.EncodingError href "" "bloqade.lanes.bytecode.encode.EncodingError"
            

Raised when the encoder encounters an unrecognized statement.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/bytecode/encode.py
35
36
37
def __init__(self, stmt: ir.Statement, reason: str | None = None) -> None:
    default = f"unrecognized statement type {type(stmt).__name__!r}"
    super().__init__(reason or default)

dump_program

dump_program(
    method: Method, version: tuple[int, int] = (1, 0)
) -> Program

Encode a stack_move ir.Method into a bytecode Program.

Inverse of load_program in decode.py: load_program(dump_program(method)) round-trips through bytecode and back to an equivalent ir.Method.

The method must be stack-consistent (each SSA value used exactly once, defining statements in stack order). load_program always produces stack-consistent output; IR from compiler rewrites must be normalised by stackify before calling this function.

Source code in .venv/lib/python3.12/site-packages/bloqade/lanes/bytecode/encode.py
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def dump_program(method: ir.Method, version: tuple[int, int] = (1, 0)) -> Program:
    """Encode a stack_move ir.Method into a bytecode Program.

    Inverse of ``load_program`` in ``decode.py``:
    ``load_program(dump_program(method))`` round-trips through bytecode
    and back to an equivalent ir.Method.

    The *method* must be stack-consistent (each SSA value used exactly
    once, defining statements in stack order).  ``load_program`` always
    produces stack-consistent output; IR from compiler rewrites must be
    normalised by ``stackify`` before calling this function.
    """
    encoder = BytecodeEncoder(dialects=method.dialects)
    encoder.run(method)
    return Program(version=version, instructions=encoder.instructions)