How to register a new LLVM backend?

Viewed 3101

I'm developing a very basic new LLVM backend for a RISC machine (named Risco), based on the existing Sparc backend and this tutorial. To register the backend, I've used the following.

  • At RiscoTargetMachine.cpp:

    extern "C" void LLVMInitializeRiscoTarget()
    {
        // Register the target.
        RegisterTargetMachine<RiscoSimulatorTargetMachine> X(TheRiscoTarget);
        RegisterAsmInfo<RiscoMCAsmInfo> Y(TheRiscoTarget);
    }
    
  • At Risco.td:

    def : Processor<"simulator", NoItineraries, [FeatureA]>;
    
    def Risco : Target {
            // Pull in Instruction Info:
            let InstructionSet = RiscoInstrInfo;
    }
    
  • At TargetInfo/RiscoTargetInfo.cpp:

    Target llvm::TheRiscoTarget;
    
    extern "C" void LLVMInitializeRiscoTargetInfo() {
            RegisterTarget<> X(TheRiscoTarget, "risco", "Risco");
    }
    
  • At the top level LLVM configure script:

    # Added Risco to the TARGETS_TO_BUILD variable at line 4965 (from svn trunk):
    all) TARGETS_TO_BUILD="X86 Sparc PowerPC Alpha ARM Mips CellSPU PIC16 XCore MSP430 SystemZ Blackfin CBackend CppBackend MBlaze PTX Risco" ;;
    

After build, llc -version doesn't show the new target. Even llc -march=risco test.ll says it's an invalid target. What am I missing?

PS: Currently, I'm including the new target as a folder inside llvm/lib/Target. How can I change that so I can build the target separately, and load it dynamic with llc -load?

2 Answers

You have to edit at least 16 files in the root directory of LLVM:

1) In CMakeLists.txt add our target to: set(LLVM_ALL_TARGETS AArch64 ARM ... )

2) Add your target to Triple.h

3) Add HI/LO to llvm_root_dir/include/llvm/MC/MCExpr.h

...

16) ...

The complete steps can be found in LLVMCookbook. Page 228 to 238. Sorry I could not copy/paste 10 pages of tutorial here.

After editing all those 16 files, then build the LLVM using cmake: $cmake ~/llvm/src/ -DLLVM_TARGETS_TO_BUILD=YourTargetName and then $make

If you are lucky enough then your build will be successful, and you can see your target added to llc tools by issuing: $llc –version

Related