How to register a new LLVM backend

Hi,

I’m developing a very basic new LLVM backend for a RISC machine (named Risco), based on the existing Sparc and Mips backends and the main tutorial [1]. I’m having trouble registering the backend so the main tools can see it.

My project code is outside the source tree, and I’ve altered the Makefile to generate a shared library for the backend (libLLVMRiscoCodeGen.so). I’ve tried running: llc -load ./libLLVMRiscoCodeGen.so -march=risco, but the target isn’t recognized (it doesn’t even appear in the llc -version output).

The main steps I did for registering the backend were:

  • At RiscoTargetMachine.cpp:

extern “C” void LLVMInitializeRiscoTarget() {
// Register the target.
RegisterTargetMachine X(TheRiscoTarget);
RegisterAsmInfo A(TheRiscoTarget);
}

  • At Risco.td:

def Risco : Target {
let InstructionSet = RiscoInstrInfo;
}

  • At RiscoTargetInfo.cpp:

Target llvm::TheRiscoTarget;

extern “C” void LLVMInitializeRiscoTargetInfo() {
RegisterTarget<> X(TheRiscoTarget, “risco”, “Risco”);
}

What I found suspicious was that in the last file (RiscoTargetInfo.cpp), the original RegisterTarget template parameter was Triple::mips. I found it odd because, if I understood it right, this attaches backend information to a LLVM core file. What am I supposed to put there for a new backend? Do I leave the default parameter?

Is this the problem or am I missing something else?

[1] http://llvm.org/docs/WritingAnLLVMBackend.html

Hi Giuliano,

Have you modified the configure script to know to enable the target? Look for the bit that handles “–enable-targets” and you’ll see a list of the default targets (all of them) that are enabled in a build and a case statement that parses the options to “–enable-targets”. Make sure your new target is added in both places.

include/llvm/ADT/Triple.h contains a enumeration for the targets. Make sure you’ve added “risco” there, too.

-Jim

My project code is outside the source tree, and I've altered the Makefile to
generate a shared library for the backend (libLLVMRiscoCodeGen.so).

You cannot do this anymore. You need to alter the build system (add
stuff to configure, etc.)

Hi,

I think you need to modify these files, at a minimum, for it to build and register the target.

/configure
/lib/Support/Triple.cpp
/include/llvm/ADT/Triple.h

–John

Thanks for the info.

> My project code is outside the source tree, and I've altered the Makefile to
> generate a shared library for the backend (libLLVMRiscoCodeGen.so).
You cannot do this anymore. You need to alter the build system (add
stuff to configure, etc.)

That's unfortunate. In order to test the backend, currently, I have to
build it and then link llc again: the linking process in freezing my
machine pretty bad for a while. Is there a easier way to debug changes
to a backend? Maybe speed up llc linking?

Giuliano Vilela <giulianoxt@gmail.com> writes:

That's unfortunate. In order to test the backend, currently, I have to
build it and then link llc again: the linking process in freezing my
machine pretty bad for a while. Is there a easier way to debug changes
to a backend? Maybe speed up llc linking?

What's you setup? Linking llc takes less than 2 seconds on my two years
old Linux desktop, with all LLVM targets enabled.

That's unfortunate. In order to test the backend, currently, I have to
build it and then link llc again: the linking process in freezing my
machine pretty bad for a while. Is there a easier way to debug changes
to a backend? Maybe speed up llc linking?

I found gold definitely 'a must' on linux for llvm development: it
makes significant decrease of the whole link time

Hello Giuliano,

If you're running into trouble linking things, it will help to have more than 1
GiB of available memory when linking. I ran into this before when I was trying
to build Clang from source on a Linux VM under VirtualBox on my Mac.

--Sam