Skip to content

Mypyc Implementation Overview

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

Mypyc compiles a Python module (or a set of modules) to C, and compiles the generated C to a Python C extension module (or modules). You can compile only a subset of your program to C -- compiled and interpreted code can freely and transparently interact. You can also freely use any Python libraries (including C extensions) in compiled code.

Mypyc will only make compiled code faster. To see a significant speedup, you must make sure that most of the time is spent in compiled code -- and not in libraries, for example.

Overview of Passes

Mypyc has these compilation passes:

  • Type check the code using mypy and infer types for variables and expressions. This produces a mypy AST (defined in mypy.nodes) and a type map that describes the inferred types (mypy.types.Type) of all expressions (as PEP 484 types).

  • Translate the mypy AST into a mypyc-specific intermediate representation (IR).

    • The IR is defined in mypyc.ir (see here for an explanation of the IR).
    • Various primitive operations used in the IR are defined in mypyc.primitives.
    • The translation to IR happens in mypyc.irbuild. The top-level logic is in mypyc.irbuild.main.
  • Insert checks for uses of potentially uninitialized variables (mypyc.transform.uninit).

  • Insert exception handling (mypyc.transform.exceptions).

  • Insert explicit reference count inc/dec opcodes (mypyc.transform.refcount).

  • Infer always defined attributes that don't require checking for a missing value on read (mypyc.analysis.attrdefined).

  • Translate the IR into C (mypyc.codegen).

  • Compile the generated C code using a C compiler (mypyc.build).

Contents

Details of the mypyc implementation are described in these pages: