Incremental Compilation (IC)

The nim ic command provides incremental compilation support for Nim projects, allowing faster rebuilds by reusing previously compiled intermediate representations of modules that haven't changed.

Overview

Incremental compilation works by decomposing the compilation process into several stages:

  1. Parsing - Source files are parsed into an abstract syntax tree (AST)
  2. Semantic Analysis - Symbols are resolved and type checking is performed
  3. Code Generation - Platform-specific code is generated from the analyzed AST
  4. Linking - The generated code is linked into an executable

The IC mechanism caches the results of earlier stages in NIF files (Nim intermediate format): .p.nif (parsed), .deps.nif (dependencies), and .nif (semantically analyzed). When recompiling, only modules that have changed need to be reprocessed through the semantic analysis and code generation stages, significantly reducing compilation time for large projects.

NIF File Format

NIF (Nim Intermediate Format) files are text-based files that use a Lisp-like syntax. They employ a hybrid format where byte offsets into the text are used for efficient access, making them simultaneously human-readable and machine-efficient. The text representation is particularly valuable for debugging and introspection.

Each .nim module produces its own .nif file during compilation. The NIF format contains:

The NIF format is designed specifically for Nim and allows efficient serialization and deserialization of the compiler's intermediate representation while remaining readable and debuggable by tools and developers.

The nim ic Switch

The nim ic command initiates incremental compilation for a project. It automatically manages the build process by:

  1. Parsing all source files into .nif format (using the nifler tool)
  2. Performing semantic analysis on modified modules
  3. Generating code only for modules with changes or dependencies on changed modules
  4. Generating a build file (in NIFMake format) that orchestrates the compilation
  5. Executing the build file through nifmake

Prerequisites

If these tools are not available, nim ic will display instructions on how to obtain them.

Key Modules for IC Logic

The primary modules in the compiler that handle incremental compilation logic are:

Code, Logic & Debugging

This section focuses on the compiler-side code paths, the logic you will inspect while debugging IC, and a pragmatic manual workflow for bug hunting using local invocations such as nim m --nimcache:nifcache.

Core places to inspect

Understanding the NIF text

Manual bug-hunting workflow

Tips for efficient debugging

Where to change behavior

See also