ClangIR#

Warning

ClangIR is not included in a default clang build yet. The documentation may be incomplete and out-of-date.

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#

Note

Until #221291 lands, you need a build of Clang with CIR enabled. Configure LLVM with -DLLVM_ENABLE_PROJECTS="clang;mlir" and -DCLANG_ENABLE_CIR=ON.

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.

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.