FractalFir / rustc_codegen_clr

This rust compiler backend(module) emmits valid CIL (.NET IR), enabling you to use Rust in .NET projects.
MIT License
1.39k stars 30 forks source link

Enhancement - implement the bgt and bgt.un instructions, and use them for optimizations. #44

Closed FractalFir closed 3 months ago

FractalFir commented 3 months ago

Currently, the project uses long CIL sequences to compare values and branch.

    ldarg.0
    ldarg.1
   cgt // 2 bytes long, compare greater than
brtrue // 1 byte + offset, branch if true

Those sequences can be shortened, using the blt instructions:

   ldarg.0
   ldarg.1
bgt // 1 byte + offset, branch if greater than

This more optimal sequence is not only more space efficient, it is also better for the JIT compiler.

Steps needed:

[ ] Add this instruction, by adding a new variant of the CILRoot enum [ ] Add support for exporting this instruction. [ ] Add optimizations, changing the sequence cgt brtrue into just bgt [ ] Add some basic unit tests

This only requires changes to the cilly crate, so no Rust-specific experience is needed.

Helpful sources: List of CIL instructions CIL Root optimization function