dotnet / LLVMSharp

LLVM bindings for .NET Standard written in C# using ClangSharp
MIT License
844 stars 97 forks source link

Can you create a custom backend with this binding? #207

Open furesoft opened 1 year ago

furesoft commented 1 year ago

Hi,

I am wondering if I can write my own llvm backend with this binding in c#?

tannergooding commented 1 year ago

To the extent that the official LLVM-C bindings allow you to, yes.

LLVM-C (and therefore LLVMSharp) cover most of the core APIs you need. However, it is ultimately not as verbose as the LLVM C++ API surface.

ClangSharp works around this limitation by providing libClangSharp to fill the gap around any missing API surface. There are plans for LLVMSharp to eventually do the same, but I haven't had enough time to do that yet (nor have there been enough contributors for the work to be done by someone else).

furesoft commented 1 year ago

I cannot find LLVMTargetMachine on ClangSharp as well to write a custom backend

tannergooding commented 1 year ago

LLVM-C exports it as LLVMGetExecutionEngineTargetMachine and corresponding APIs that take LLVMTargetMachineRef

LLVMSharp exposes this as a "raw API" LLVM.GetExecutionEngineTargetMachine and as helper method on the LLVMExecutionEngineRef type (in turn, APIs that take LLVMTargetMachineRef are exposed as helper methods on that type).

There is a WIP higher level wrapper that exposes all of this as managed types and tries to mirror the C++ API surface, but it isn't complete and there is functionality available in the "raw API" that isn't exposed yet.

-- As an additional note, I would recommend using LLVMSharp 8.0.0-beta or later (ideally 15.0.0-beta, as that's the latest). The older bindings have many known bugs and are actually overall less stable/correct.

The only reason a stable version of the new bindings hasn't been published yet is because I haven't had the time to finish validating the packages are being properly code signed (it only recently became enabled for the project).