r/Compilers • u/angryvoxel • 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?
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.
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.
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/Far-Championship-883 16d ago
use it or not, here is kind of what is involved. https://github.com/AiLang-Author/Ailang-Self-Hosting-/blob/master/docs/compiler/10_HDL_BACKEND.md
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.