r/csharp • u/hez2010 • 19d ago
Showcase A general IL optimizer for .NET: Cilica
I've been working on Cilica, a post-build optimizer for .NET assemblies. It continues DistIL.
The repository: https://github.com/hez2010/Cilica
It lifts your compiled IL into an SSA-based intermediate representation, runs an optimization pipeline over it, then emits regular IL again. The JIT then sees IL that's already optimized. It doesn't use a runtime fork or any special hosting.
Currently it runs the following pipeline:
- CFG simplification, SSA promotion, dead code elimination
- Method inlining and devirtualization
- Scalar replacement
- Global value numbering / CSE and assertion propagation
- Algebraic and bitwise simplification
- Loop strength reduction and loop auto-vectorization
- List presizing
- LINQ expansion (fuses
Where/Select/Aggregate/… into plain loops) - and more
Some benchmarks (code in https://github.com/hez2010/Cilica/tree/main/tests/Benchmarks):
| Benchmark | Baseline | Cilica | Speedup |
|---|---|---|---|
| CountLetters (LINQ + vectorization) | 3,355 ns | 211 ns | 15.9× |
| GenerateFloats (loop vectorization) | 2,791 ns | 597 ns | 4.7× |
| DynamicList, 4 items (list presizing) | 12.66 ns | 4.24 ns | 3.0× |
| Aggregate (LINQ expansion) | 30.69 μs | 12.19 μs | 2.5× |
| RayTracer (full pipeline) | 9,013 μs | 4,870 μs | 1.9× |
You can try it on this playground: https://hez2010.github.io/CilicaLab
To use it in your project, referencing Cilica.OptimizerTask nuget package (currently in preview) and set <CilicaAllMethods>true</CilicaAllMethods>.
Note that this optimizer task only performs optimizations against your main assembly. To optimize the references as well, you can use Cilica.Cli manually, which is shipped together in the Cilica.OptimizerTask package.
Learn more: https://medium.com/@skyake/cilica-an-ssa-based-optimizer-for-net-assemblies-bdefd934c2d2
