Skip to content

Mypyc Intermediate Representation (IR)

Jukka Lehtosalo edited this page Oct 24, 2022 · 2 revisions

The mypyc IR is defined in mypyc.ir. It covers several key concepts that are essential to understand by all mypyc contributors:

  • mypyc.ir.ops.Op is an Abstract Base Class for all IR operations. These are low-level and generally map to simple fragments of C each. Mypy expressions are translated to linear sequences of these ops.

  • mypyc.ir.ops.BasicBlock is a container of a sequence of ops with a branch/goto/return at the end, and no branch/goto/return ops in the middle. Each function is compiled to a bunch of basic blocks.

  • mypyc.ir.rtypes.RType and its subclasses are the types used for everything in the IR. These are lower-level and simpler than mypy or PEP 484 types. For example, there are no general-purpose generic types types here. Each List[X] type (for any X) is represented by a single list type, for example.

  • Primitive types are special RTypes of which mypyc has some special understanding, and there are typically some specialized ops. Examples include int (referred to as int_rprimitive in the code) and list (list_rprimitive). Python types for which there is no specific RType type will be represented by the catch-all object_rprimitive type.

  • Instances of compiled classes are generally represented using the RInstance type. Classes are compiled to C extension classes and contain vtables for fast method calls and fast attribute access.

  • IR representations of functions and classes live in mypyc.ir.func_ir and mypyc.ir.class_ir, respectively.

Look at the docstrings and comments in mypyc.ir for additional information. See the test cases in mypyc/test-data/irbuild-basic.test for examples of what the IR looks like in a pretty-printed form.

Clone this wiki locally