How llvm/polygeist works
Project type and purpose
• This is a compiler-toolchain project built as an MLIR/Clang plugin (“Polygeist”) that
• Defines a new MLIR dialect for C/C++ constructs (“polygeist” dialect),
• Provides passes to “raise” C to MLIR, optimize (affine/polyhedral/Ops transforms), and lower back to LLVM IR or CUDA/HIP,
• Includes optional integration with the “Polymer” polyhedral optimizer (Pluto/ISL backends),
• Ships command-line tools (cgeist, polygeist-opt, polymer), and a test suite driven by LLVM’s lit harness.
File structure
• Top‐level CMake files (+ cmake/modules) orchestrate building:
• /polygeist & lib/polygeist: dialect definitions (.td, .h) and implementation of passes, runtime wrappers, ExecutionEngine support for CUDA/ROCm, etc.
• llvm-project: submodule providing MLIR, Clang, LLVM core.
• tools/cgeist, tools/polygeist-opt, tools/polymer: distinct executables that invoke the plugin passes.
• test/: lit configuration and .mlir test cases for polygeist-opt and cgeist.
• tools/polymer: an optional, heavier polyhedral subsystem with its own includes, libs, tools, tests, and examples.
README for architecture hints
• Shows that Polygeist plugs into MLIR/Clang via CMake options, registers dialect + passes, and can be built standalone or as an external LLVM project.
• Mentions GPU backends, Polymer (Pluto/ISL), and test targets (check-polygeist-opt, check-cgeist, check-polymer).
a. External Dependencies
• LLVM Core (llvm‐project/llvm)
• Clang Frontend (llvm‐project/clang)
• MLIR Core (llvm‐project/mlir)
• Optional: Polly (llvm subproject) for ISL, or Pluto libraries for Polymer
b. Polygeist Core
• Dialect Registration (include/polygeist/Dialect.td → generated code)
• Passes (include/polygeist/Passes & lib/polygeist/Passes/*.cpp)
• ExecutionEngine Extensions (CudaRuntimeWrappers, RocmRuntimeWrappers)
c. Polymer Subsystem (tools/polymer)
• Its own dialect/transforms/plugins built via CMake hooks when POLYMER enabled
• Pluto/ISL backends under the hood
d. Tools / CLI
• cgeist → C/C++ → MLIR translator (uses ClangAST → MLIR polygeist dialect)
• polygeist-opt → MLIR optimizer: loads MLIR, runs polygeist passes, lowers to LLVM/DialectOps
• polymer-opt (or polymer-translate) → invokes polymer transformations
e. Code Generation & Runtime
• MLIR → LLVM IR lowering passes (ConvertPolygeistToLLVM, ConvertToOpaquePtr, etc.)
• JIT / ExecutionEngine (CPU or GPU backends)
• CUDA/ROCm runtime wrappers for barrier sync, memory, kernels
f. Tests / CI
• lit.cfg.py, lit.site.cfg.py.in drive test cases under test/ and tools/polymer/test/.
Architectural patterns & design principles
• Plugin architecture: dialect & pass registration via MLIR’s PassManager
• Layered compilation pipeline:
1. Frontend (ClangAST → MLIR polygeist dialect)
2. Raising/Canonicalization passes (polygeist)
3. Polyhedral transformations (optional polymer plugin)
4. Lowering passes (MLIR → LLVM dialect → LLVM IR)
5. Codegen & JIT/Runtime
• Modularity: separate CMake targets, clear dependency isolation (include vs lib vs tools)
• Extensibility: optional GPU and polymer features turned on via CMake flags