18

I am trying to write an LLVM backend for my custom processor that I have designed recently.

I tried to follow the official tutorial at http://llvm.org/docs/WritingAnLLVMBackend.html

But it is so vague, so incomplete and blurry that I failed to follow it.

I then started to search for other tutorials online and all of them suffer from the same symptoms of the original tutorial. It seems the authors assume lots of pre-readings and write their tutorials without necessary preliminary explanations.

How can I find a tutorial or anything that can set me up to write a functional backend in LLVM?

-- Update: I see two down votes and two up votes. The down votes says my question is not related to programming. I wonder how the moderators have concluded that writing a compiler is not related to programming.

Dr. Ehsan Ali
  • 4,735
  • 4
  • 23
  • 37
  • 4
    Most of the backends for LLVM were started by copying an existing backend and then tweaking it. The most suitable (read: simple) backend at the moment is Lanai (if you're implementing a simple RISC) or Hexagon if you're implementing a VLIW CPU. Otherwise you'll spend too much time setting up the infrastructure for your backend. I wish we had a "template" basic backend, but there is nobody to maintain such a thing. – SK-logic Oct 10 '16 at 09:36
  • 1
    Also, things to look at first: IR to DAG lowering is a central point of your backend (see *ISelLowering.cpp), start from there and then go on to defining the isel patterns and pre- and post- passes if necessary. Also, SO is unlikely to be helpful. Feel free to ask your questions in the llvm-dev list. – SK-logic Oct 10 '16 at 11:21
  • 2
    I am trying to make sense out of things, but the progress is very slow. yes I copied the SPARC folder into a new folder under /lib/target and trying to understand things there. For example there is not even a single example on how to invoke the \emph{tablegen} command. Anyway I am a PhD student and got used to these dirt roads and obstacles. If I manage to get my backend working then I will write a proper tutorial and will contribute. – Dr. Ehsan Ali Oct 10 '16 at 11:48
  • 2
    Just rely on CMake to invoke tablegen for you, no need to do it manually, even for debugging. And the problem with all the tutorials (and stale backends not included in the upstream) is that things change too fast, this stuff is becoming obsolete very quickly. – SK-logic Oct 10 '16 at 13:18
  • 3
    There have been talks at the previous llvm dev meetings, you may want to start with this: http://llvm.org/devmtg/ – Joky Oct 15 '16 at 23:44
  • To add to above suggestions - in general you should expect to do a lot of debugging and hacking around existing code. LLVM is relatively easy to extend (i.e. easier than GCC or Open64) but still hard in absolute terms. – yugr Nov 30 '16 at 09:32
  • @SK-logic Maybe you could make this an answer? – yugr Mar 28 '18 at 12:53
  • @Ehsan are you still interested in writing a tutorial? (or have already written?) – Yashas Jul 15 '18 at 14:00
  • 1
    I have published a paper on it. The tile is "A guideline for rapid development of assembler to target tailor-made microprocessors" by Ehsan Ali and Wanchalerm Pora. It will appear in IEEE website soon. – Dr. Ehsan Ali Jul 19 '18 at 22:35
  • 2
    It seems the paper cannot be found by through search engines; As I have received couple of emails asking for the paper, here is the link to the paper saved in my personal Dropbox: https://www.dropbox.com/s/onap4rkd7wyf09w/PID5357107.pdf – Dr. Ehsan Ali Oct 11 '18 at 05:53
  • I also struggled to create the custom backend initially. But spending few weeks on other backends helped me. Now I started created my own backend for 8 bit cpu, and added support for few instructions. What I will suggest is , go through the available resources. You can copy other backends , and modify the patterns and instructions initially. After that you can surely able to understand the lifecycle of instructions. Learning different techniques of instruction selection, and ways to expand one instr to many will help you lot. (Frame management, Custom inserters..etc) – Mannar Amuthan Apr 30 '22 at 03:21

1 Answers1

1

I've came across the same problem and there is no good tutorials indeed. But hope some of them I've found should help you if it is still actual or anyone else who read this.

First of all there is a common advice to copy-paste an existing backend and modify it for your CPU. But this is not a good practice because you don't understand what you are doing. So I've found a book with short description of llvm architecture. It can help you much. Also there is a step-by-step tutorial for implementing backend for CPU0 processor. Other sources are miscelantious slides that could be easyly found by yourself.

alexanius
  • 402
  • 2
  • 11