Live data from Hacker News

A Complete Guide to LLVM for Programming Language Creators

mukulrathi.co.uk

31–40 of 47 posts

Re: A Complete Guide to LLVM for Programming Language Creators

#31

As someone who writes a lot of toy languages, I made this scaffolding for a LLVM-based compiler: https://github.com/finiteloop/compiler It uses Bison and Flex for parsing and lexing unlike this post, but may be a useful starting point for those building their own toy languages.

Doesn’t the LLVM API go through breaking changes fairly often? How do you track all that and keep your sanity?

> Doesn’t the LLVM API go through breaking changes fairly often? How do you track all that and keep your sanity?

No, not very big ones. But they give no stability promises.

If you're using a language other than C++ via API bindings, you will experience some churn going from version to version as the bindings need to be updated too.

Re: A Complete Guide to LLVM for Programming Language Creators

#32
post #19

Earlier quoted context omitted.

I emit LLVM IR as text in my compiler. It’s painless, almost. Some points off the top of my head… - I started out just printing strings, but found I wanted a bit more structure so I wrote a dozen or so functions to do the printing. Things at the level of call(name,rags). All make a single line of IR. It helps keep the typos down. - just run your IR through clang to compile it. It has good enough error messages that y…

Can you print variables in lldb with the debug information? One compiler that I use which emits llvm ir added support for debug information recently and its now possible to set breakpoints in gdb but you can't print out any stack variables or anything so its not useful other than figuring out which code paths execute. I'd like to learn more about this. Maybe contribute to the compiler and fix this issue.

> I'd like to learn more about this. Maybe contribute to the compiler and fix this issue.

You need to create a call to the `llvm.dbg.declare` intrinsic that connects the stack variable alloca to a DILocalVariable metadata node, and place the call after the stack variable alloca. The rest of LLVM will handle tracing the stack location through the compiler to the output, including the alloca being promoted.

See: https://llvm.org/docs/SourceLevelDebugging.html#debugger-int...

Re: A Complete Guide to LLVM for Programming Language Creators

#33
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

Julia has an interesting split here, it does the lowering into SSA from in pure Julia and then has a codegen steps that translates the SSA from into LLVM IR, but for that second step we do use the C++ API. We have very robust bindings to the C-API, but it forever feels just a bit incomplete and less cared for. The C-API is very stable, whereas the C++ API does change quite a bit.

But you cannot use the C-API for symbols/methods. You need a C++ callback for that.

Re: A Complete Guide to LLVM for Programming Language Creators

#34
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

I'm currently on a compiler creation course with llvm in University.

Generating it in text form is really simple. Doing just that with Rust. Actually, writing it by hand is too.

You can use llvm-as to convert text form to bytecode and then lli to interpret it (or use one of the other tools to compile it).

Re: A Complete Guide to LLVM for Programming Language Creators

#35
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

Hey, author of the post here. Do I think the C++ API is important? For most languages no. The OCaml bindings in my case were almost sufficient, but I planned to do some memory fences and other operations in my language that the OCaml bindings didn't have.

In hindsight, it's probs better to choose OCaml bindings and then link in any special instructions you need from C++ if you need to.

Re: A Complete Guide to LLVM for Programming Language Creators

#36
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

[deleted]

Re: A Complete Guide to LLVM for Programming Language Creators

#37
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

Hey, author of the post here. Do I think the C++ API is important? For most languages no. The OCaml bindings in my case were almost sufficient, but I planned to do some memory fences and other operations in my language that the OCaml bindings didn't have. In hindsight, it's probs better to choose OCaml bindings and then link in any special instructions you need from C++ if you need to.

Regarding this post in particular, I chose to document everything in terms of the C++ API as that's the native API. You can use any of the other bindings, and just translate the syntax across to your language.

Re: A Complete Guide to LLVM for Programming Language Creators

#38
post #19
post #5

A question for the LLVM experts here: In the past when I've looked at LLVM from a distance, the biggest stumbling block I found were that it's written in C++ , which isn't the language I'm using for my frontend. How important is the C++ API in practice? Are the bindings for other languages usable? Is it possible to have my frontend emit LLVM IR in a text format, similarly to how you can feed assembly language source…

I emit LLVM IR as text in my compiler. It’s painless, almost. Some points off the top of my head… - I started out just printing strings, but found I wanted a bit more structure so I wrote a dozen or so functions to do the printing. Things at the level of call(name,rags). All make a single line of IR. It helps keep the typos down. - just run your IR through clang to compile it. It has good enough error messages that y…

> learn to write little C programs and use clang to emit the IR.

Highly recommend this. I used this to get an understanding of how to implement the IR for my language.

Re: A Complete Guide to LLVM for Programming Language Creators

#39
post #19

Earlier quoted context omitted.

I emit LLVM IR as text in my compiler. It’s painless, almost. Some points off the top of my head… - I started out just printing strings, but found I wanted a bit more structure so I wrote a dozen or so functions to do the printing. Things at the level of call(name,rags). All make a single line of IR. It helps keep the typos down. - just run your IR through clang to compile it. It has good enough error messages that y…

> learn to write little C programs and use clang to emit the IR. Highly recommend this. I used this to get an understanding of how to implement the IR for my language.

The command to use is `clang -S -emit-llvm -O1 foo.c`

It'll write it out to a foo.ll file.

(I use -O1 so it cleans up a bit of the messy parts of the IR).

Re: A Complete Guide to LLVM for Programming Language Creators

#40
post #4

> LLVM IR looks like a more readable form of assembly I think assembly is more readable than the dumped LLVM IR output of a real compiler… Anyway, that was a good overview of how to build simple LLVM IR. :)

The IR is pretty messy but if you can see through the clutter the IR is much easier to read the flow of. There's a reason why we use SSA (although prior to mem2reg running you don't quite have it, but still)

Yes, exactly. Clang emitted IR especially has a lot of (C/C++)-specific junk. If you look past that clutter, it's not too bad.

I think the best way to learn to read IR is to look at super-minimal examples, and then you'll be able to tell which parts of larger IR files are relevant.

Post reply on HN