VMKit: msil optimization

Hello all.

is it possible to use LLVM to optimize existing .NET assemblies?

Basically doing the following:

  • read in MSIL and convert into LLVM internal representation
  • perform global optimizations on LLVM internal representation
  • write out optimized MSIL using the existing LLVM backend

I presume that the capability to convert MSIL into LLVM internal representation exists somewhere in VMKit.

The reason I need this is the following:

I have a very large C# application that is in desperate need of some optimization. The problem is that the microsoft C# compiler does almost no optimization but relies on the MS JIT compiler to do it. But the MS JIT compiler does not do any agressive optimization, especially inlining and subsequent optimizations. So the generated machine code is just atrocious and performance is much lower than expected.

I know that optimizing on a MSIL level will somewhat alleviate the problem since the microsoft C++/CLR compiler does MSIL level optimizations and produces much faster code in some tests I have written. But rewriting the application in question in C++/CLR is not an option.

best regards,

RĂ¼diger Klaehn

Dear Rudiger,

RĂ¼diger Klaehn wrote:

Hello all.

is it possible to use LLVM to optimize existing .NET assemblies?

It's in theory possible. LLVM+VMKit already does it for Java classes.

Basically doing the following:

- read in MSIL and convert into LLVM internal representation
- perform global optimizations on LLVM internal representation - write out optimized MSIL using the existing LLVM backend

I presume that the capability to convert MSIL into LLVM internal representation exists somewhere in VMKit.

Although I haven't worked on VMKit's .NET for a long time, and it must surely be broken for larger programs than HelloWorld, adding the capability should not be too hard. It was really straightforward to add it to VMKit's JVM.

As for the state of the MSIL backend of LLVM, I don't think it's for production use yet. Assemblies have lots of information in them and maintaining them during the MSIL -> LLVM -> MSIL translations should be the hard part.

Cheers,
Nicolas

Hello,

As for the state of the MSIL backend of LLVM, I don't think it's for
production use yet. Assemblies have lots of information in them and
maintaining them during the MSIL -> LLVM -> MSIL translations should be
the hard part.

This should be pretty hard. MSIL is definitely much more high level
than LLVM, that's why significant portion of information will be lost
during MSIL => LLVM (=lowered to vmkit-specific stuff), so I doubt
that sane back reconstruction will ever be possible (consider e.g.
callvirt instruction as an example).

OK. That is quite unfortunate.

Does that mean that the existing LLVM MSIL backend does not work for complex LLVM programs?

Hello,

Does that mean that the existing LLVM MSIL backend does not work for complex
LLVM programs?

No (modulo bugs). It should work, however it will "map" to some
(C-like) subset of MSIL.