ClangIR#

ClangIR (CIR) is a high-level representation in Clang that reflects aspects of the C/C++ languages and their extensions. It is implemented using MLIR and occupies a position between Clang’s AST and LLVM IR.

Follow and contribute to upstream development through the ClangIR-labeled issues and pull requests on llvm-project. See the ClangIR Design Documents for in-depth coverage of CIR’s design.

Using ClangIR#

ClangIR is an MLIR dialect tightly integrated with Clang: it lives in the Clang project rather than as a standalone in-tree dialect.

Pass -fclangir to the Clang driver to compile through CIR (AST to CIR to CIR passes to LLVM IR and the backend). In order to inspect CIR output, use -emit-cir, the equivalent of -emit-llvm for CIR.

Note

ClangIR can be disabled from the build of Clang via the -DCLANG_ENABLE_CIR=OFF, see CMake flag. Using this flag can reduce the compile time and binary size of Clang, however it will make the -fclangir flag unavaiable.

See this Compiler Explorer example, which shows the flow of compilation through CIR, from higher-level CIR constructs to lower-level ones, the pipeline viewer is convenient for following along.

ClangIR Design Documents#

RFCs#

Talks and posts#

History#

ClangIR started in October 2021. In June 2022, an RFC proposing to incorporate the project — then in very early stages — into llvm-project led to ClangIR becoming part of the LLVM incubator, where development happened for the following years. In January 2024, the upstreaming RFC was accepted as the graduation step from the incubator (now archived), and ClangIR has been developed directly upstream ever since, superseding the incubator in most functionality.