How to register a new LLVM backend?
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_开发者_JS百科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
?
The default first template parameter for RegisterTarget
is Triple::InvalidArch
. Try this:
extern "C" void LLVMInitializeRiscoTargetInfo() {
RegisterTarget<Triple::UnknownArch> X(TheRiscoTarget, "risco", "Risco");
}
You might also need to register an assembly printer for your backend in RiscoAsmPrinter.cpp:
extern "C" void LLVMInitializeRiscoAsmPrinter() {
RegisterAsmPrinter<RiscoAsmPrinter> X(TheRiscoTarget);
}
I'm not quite sure what you mean by the last bit. My Makefile has LOADABLE_MODULE=1
and builds the target as a shared object in the lib folder. In order to see the Risco target in the list of registered targets, I would run something like ./bin/llc -load ./lib/libLLVMRisco.so -version
assuming you are on Linux.
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
精彩评论