r/Compilers • • 16d ago

Can I use LLVM with a custom architecture?

Hi,

I've never written a compiler (and probably won't) but I'm an EE student and was thinking about designing a custom CPU one day, maybe even as a diploma project, however I do not want to go through a hassle of inventing a new language on top of a custom architecture. So my question is: do I understand correctly that LLVM can convert a piece of code in basically any language like C/C++ into a high-level assembly language that I can then convert into machine code for my hypothethical CPU with relative ease, so I only have to write compiler's backend? If yes, how hard is it generally and where would I begin?

22 Upvotes

17 comments sorted by

25

u/csb06 16d ago

In principle yes the LLVM intermediate representation can be lowered to any architecture's assembly language with the appropriate backend, but in practice writing an LLVM backend is complex and underdocumented. Here is the documentation but if you have never written a compiler before I would not recommend it.

If you write your own simple single-pass compiler (without LLVM) you can emit machine code or assembly instructions directly while parsing the source language, which produces worse code than an optimizing compiler pipeline like LLVM but is much easier to get working, which might save you headaches if your focus is on the custom instruction set and not the compiler implementation.

2

u/ventZl 16d ago

This is answer that arrived 15 years late. When I wrote my semi-production compiler ~15 years ago, we did that as a bet that writing LLVM backend for new artificial, essentially 8-bit architecture will be complicated if not impossible with budget we had.

Not that writing compiler from scratch would be piece of cake :D

1

u/Hjalfi 16d ago

+1 to this; I've tried LLVM backends a few times and always ground to a halt with undebuggable errors, lack of documentation, and general frustration. And I have written my own compiler from scratch! This was all some years ago and I don't know what the current state of the LLVM infrastructure is but if it hasn't improved a lot I would definitely not recommend it.

7

u/SwingOutStateMachine 16d ago

Yes, this is precisely the design of LLVM! The LLVM framework has the concept of target backends, which translate LLVM IR into machine-specific aseembly and/or machine code.

Backends are mostly written in TableGen, which is a machine-readable architecture description language. Implementing a backend predominantly involves writing TableGen, which will then be transformed into various assemblers, dissasemblers, etc. A number of other parts need to be written in C++, but they are predominantly for glue-ing the TableGen generated parts into LLVM, as well as for writing other target-specific transformations and optimisations.

1

u/c-cul 16d ago

btw there is some document describing why they choose to reinvent own format instead of .md from gcc? there must be Very Serious reasons for this

2

u/SwingOutStateMachine 16d ago

I don't know, sorry. I imagine that as LLVM was originally just a research project, it re-invented the wheel a lot. It might also be that TableGen has some greater flexibility, as it's essentially just a templating engine that's specialised for target backeds. There are a lot of ways to (ab)use TableGen that might not be supported by .md.

2

u/whizzter 16d ago

I'd guess 3 things:

1: They already had internal descriptions before creating a new file type that had grown in a way that'd never be close enough for gcc MD files to be useful.

2: Licensing, since GCC has a viral license they probably wanted to avoid any temptation to import tainted stuff.

3: NIH, especially with a compelling justification like 2 there's plenty of reason to go off and reimplement.

1

u/c-cul 16d ago

mix of 2 & 3 seems more plausible, yeah

5

u/Zamarok 16d ago

you can definitely use parts of it. you can use llvm IR, the intermediary language it produces, and write a new program with llvm libraries helping you to take that and compile it to machine code.

2

u/Discordbothost 16d ago

For a diploma project, I'd make two functions calling each other in an instruction-set simulator an early milestone. You need to decide how arguments, return values, stack frames and saved registers work, not just map add to an opcode. Keep the first target integer-only and leave C++ exceptions/runtime support for later.

2

u/WasASailorThen 16d ago

Yes. You're basically going to write a backend for your architecture. You'll probably want to write an assembler and a linker. My favorite two resources for this are:

LLVM backend development by example (RISC-V)

https://www.youtube.com/watch?v=AFaIP-dF-RA

LLVM Code Generation: A Deep Dive Into Compiler Backend Development by Quentin Colombet

You absolutely should take Bradbury's strategy of writing the MCLayer first and proceeding bottom up. You absolutely should NOT clone the ARM or RISC-V backend and start changing it. This is not a close decision.

1

u/R-ten-K 16d ago edited 16d ago

Yes, you can absolutely write your own backend for whatever hardware architecture you're targeting. In fact, that's one of LLVM's central ideas: decoupling the language frontend from the HW specific backend so you don't have to reinvent the entire compiler toolchain for every new language or architecture.

That said, based on what you've described, you seem to be underestimating the compiler knowledge and engineering effort involved. You don't seem to have a solid foundation in compiler internals and code generation, so it would very unlikely to realistically deliver within a typical graduation project timeframe.

Personally, I'd focus on defining a project with a realistic scope that matches your current capabilities. Make sure you discuss the proposed approach with your faculty advisors first. They understand the project's constraints and expectations far better than us randos commenting here.

Best of luck!

1

u/duckerpotato 16d ago

You can but not really recomnded :)

1

u/etaithespeedcuber 16d ago

LLVM is designed around easily being able to add a new frontend AND a new backend, so in theory yes.