.. _compilation-options: =================== Compilation Options =================== Curry code runs the way Python code does: import it and it runs. Nothing on this page needs to be configured for that. The page is for the reader who wants to know what happens behind the import, how to choose between the interpreter, the just-in-time compiler and compilation ahead of time, what each platform supports and how to watch the choice at work. Three ways to run a function ============================ Every Curry function reaches the runtime as ICurry, the intermediate language of the compiler (:ref:`compilation-pipeline`). The C++ runtime can run it in three ways, with the same scheduler, the same rewrite steps and the same answers: Interpreted The runtime interprets a bytecode made from the ICurry when the module loads. Nothing is compiled and no compiler is needed. This is how a freshly written module starts. Compiled just in time A tier of the runtime compiles interpreted functions to machine code while the program runs and swaps the compiled code in at a safepoint of the scheduler. Two backends do this: LLVM, through the Python package ``llvmlite``, compiles single functions in the process; the C++ backend writes the generated C++ of a module and compiles it with the C++ compiler (g++, clang++ or clang-cl on Windows) in a child process, then adopts the object. The C++ backend compiles the modules of the program; the modules of the Curry library run from the objects of the installation. A compile still running when the program ends is canceled, so a short program never waits for a compiler and leaves no object behind; ``sprite-make --so`` writes one. LLVM keeps one native object per function in ``M.jit`` beside the module image. On the next import, only the index is read. Cached code links as its functions run, under both ``interpret:new`` and ``interpret:all``; ``jit:eager`` resolves all cached functions at load. A miss follows the selected eager or hot tier. ``jit:off`` reads no JIT cache. A read-only product directory uses ``SPRITE_JIT_CACHE`` when set to a writable directory; otherwise compilation stays in memory. ``make stage`` fills the library cache when llvmlite is available; ``SPRITE_STAGE_JIT_CACHE=0`` skips that fill. Wheels exclude these machine-specific objects; a hot run fills the cache with the functions that become hot, while an eager run fills the whole module. Compiled ahead of time ``sprite-make --so M.curry`` compiles a module to a shared object beside its source and ``make stage`` compiles the Curry library into the installation. A current object loads under ``interpret:new`` and ``interpret:off``, whatever the JIT settings say. ``curry.save`` writes such an object, or a program around it, for use elsewhere (:doc:`PythonAPI/UsingTheAPI`). The default combines them: current objects load, everything else starts interpreted and the functions that run often are compiled behind the scenes. The front end runs once per change of a module and not at all when its products are current. The flags ========= Three interpreter flags select the behavior. Set them for a process in ``SPRITE_INTERPRETER_FLAGS`` or for a session with ``curry.reset`` (:ref:`interpreter-flags`):: SPRITE_INTERPRETER_FLAGS=jit:off python -m curry Peano.curry For a Python session: >>> import curry >>> curry.reset(jit='eager', jit_backend='llvm') =================== ===================== ================================================ Flag Values Meaning =================== ===================== ================================================ ``interpret`` ``new`` (default), ``new`` loads current ahead-of-time objects and ``all``, ``off`` interprets the rest. ``all`` interprets every module and opens no ahead-of-time object. ``off`` compiles a module that has no current object with the C++ compiler before it runs. ``jit`` ``hot`` (default), ``hot`` compiles an interpreted function after ``eager``, ``off`` it has run ``SPRITE_JIT_THRESHOLD`` times (100). ``eager`` compiles every function of a module when the module loads. ``off`` leaves the interpreter alone. ``jit_backend`` ``auto`` (default), ``auto`` takes LLVM when ``llvmlite`` imports, ``llvm``, ``cxx`` else the C++ compiler when the installation has one, else no backend. A forced backend that is not available is an error at ``curry.reset`` or at the first import. =================== ===================== ================================================ With no backend available and ``jit_backend:auto``, the process runs the interpreter alone and says so once in its log, at INFO. The program still runs; only its speed changes. What is installed decides ========================= The backends are optional. What you install selects them: - ``pip install --pre "curry[jit]"`` adds ``llvmlite`` and with it the LLVM backend, on every platform that has a wheel for it. - A C++ compiler on the machine (g++, clang++ from Xcode or clang-cl of Visual Studio on Windows) adds the C++ compiler backend and compilation ahead of time. A source build records its compiler at ``tools/cxx``; a wheel uses ``CXX`` when set; otherwise ``g++`` from PATH, or on Windows the clang-cl of Visual Studio, then a clang-cl on PATH. - Neither is needed to run Curry code: :doc:`Installation/WithoutCompiler` describes an installation with the interpreter alone. ================== =========== =========== ========== ====================== Platform Interpreter JIT: LLVM JIT: C++ Ahead of time ================== =========== =========== ========== ====================== Linux x86-64 yes yes yes yes Linux aarch64 yes yes yes yes macOS arm64 yes yes yes yes macOS x86-64 yes conda [1]_ yes yes Windows x64 yes yes yes [2]_ yes [2]_ ================== =========== =========== ========== ====================== .. [1] ``llvmlite`` ships no wheel for Intel macOS on PyPI; the conda-forge package provides it. Without it, ``auto`` takes the C++ compiler. .. [2] Sprite finds Visual Studio with ``vswhere`` and captures the developer environment of ``vcvars64.bat`` for clang-cl; no developer prompt is needed. ``sprite-make --so`` writes a DLL; the library ships compiled, and ``interpret`` defaults to ``new`` on every platform. Microsoft cl is not supported; clang-cl is the Windows compiler. Watching the choice =================== Two environment variables turn on the log of the package (:doc:`CommandLineInterface/EnvironmentVariables`). At ``INFO`` the package says what it decided for each module and for the process:: SPRITE_LOG_LEVEL=INFO python -m curry Peano.curry The lines name the backend chosen and why: ``jit: backend llvm ...`` or ``jit: no backend, interpreter only ...``. Each module reports how it loads: ``module Peano: compiled object loaded ...`` or ``module Peano: interpreted ...`` with the reason. Hot compiles report their functions and time: ``jit hot: compiled ...`` or ``cxx hot: module ... compiled ... swapped``. The log also records product-cache hits, misses and runs of the front end. LLVM reports ``jit cache: module M, miss``, ``stored N functions`` and ``indexed N functions`` at the attach, and ``resolved M.f, T ms`` at the first call of a cached function; resolved functions contribute to ``swapped`` in ``--stats``, the index alone contributes none. A failed resolution logs once per function and restores its interpreter step. ``SPRITE_LOG_FILE`` sends the records to a file instead; child processes write to the same file with their process id. Other variables serve the JIT itself: ``SPRITE_JIT_THRESHOLD`` sets the number of calls that makes a function hot and ``SPRITE_JIT_DUMP`` names a directory into which the LLVM backend writes the optimized IR and the assembly of each batch. From Python, ``curry.flags`` shows the interpreter flags of the session.