HACKER Q&A
📣 tester756

Should I use “raw” LLVM IR or some wrapper over it?


Hi!

I feel like HN seems to be good place to ask this question because compilers seems to be quite popular topic here, so I'm going to ask

I want to write yet another programming lang which uses LLVM to generate WebAssembly

My perception of it is that I have to write parser of my lang and then some "translation layer" which will translate abstraction over my language to LLVM IR

But I'm not sure whether I should write "raw IR" or use some library like https://github.com/microsoft/LLVMSharp

I feel like "raw IR" is better / more reliable way, but I've no experience with low lvl stuff (asm and so on)

Also I'm not sure about how good LLVMSharp actually is

PS: I've heard about other LLVM IR wrappers in other languages, I'd rather stick to C# because I have some kind of deadline on it

Thanks in advance


  👤 tlb Accepted Answer ✓
I built Yoga (a language for real-time robot control) using LLVM as the code generator in ORCJIT (on request just in time) mode. I'm pretty happy with LLVM.

LLVM is well documented, and generates extremely fast code. Robot control used a lot of floating point ops, and LLVM turns it into a surprisingly small number of SSE4 instructions.

I found it easier to call the LLVM API than to generate the IR as text. But it's extremely convenient to be able to dump the result as IR. Like 80% of my debugging involved looking at IR dumps. And for really hard-core debug sessions you can hand-edit the IR, compile it, and call it from C programs.

I'm calling LLVM directly from C++, but LLVMSharp is probably the way to go if you want to work in C#.

If you use LLVM's JIT system, beware that orc::jit is about the 3rd different API and there are tutorials on the web for the previous APIs. You should probably start with LLVM 9.0.1 and ignore any tutorial not for orc::jit.