Custom LLVM backend: error: unable to interface with target machine

Background

I am writing a custom, globalISel only, backend for a currently unsupported processor. I have progressed to the point where I can run

> clang -target xxx -emit-llvm file.c

> llc -march=xxx -global-isel -stop-after=irtranslator file.ll -o -

> llc -march=xxx -global-isel -debug-only=instruction-select,xxx-gisel file.ll -o -

and it will produce valid target assembly.

Problem

However, when I try to run this through the clang driver:

> clang -fglobal-isel --target=xxx -S file.c

I get:

error: unable to interface with target machine

Debugging

I am not sure how to resolve this. The error comes directly from BackendUtils.cpp, where my target does not seem to implement TargetMachine→addPassesToEmitFile(). And as expected, when I manually override this method, the error goes away, however the assembly file is empty (as no passes have been added). However, other backends do not seem to override this, as I believe the passes (IRTranslator, RegBankSelect, etc…) get called automatically.

One thing I have not done is implement xxx.cpp in clang/lib/CodeGen/Targets/. Am am not confident I understand the purpose of the logic in here. It seems to be defining the ABI and calling conventions. My issue is I think I have already done that in llvm/Target/XXX/XXXCallingConvention.td and llvm/Target/XXX/GlobalISel/XXXCallLowering.cpp

I am not sure if this is a relevant problem, nor if it is necessary for me to do. Is this truly duplicate logic or is it necessary information for both clang and llvm.

Conclusion

I understand this is quite a specific question, and I have far-from included all the relevant details. I am happy to provide anything that would be helpful. Any assistance would be greatly appreciated, thank you.

I don’t have a proper answer for you but if you’re going to trace addPassesToEmitFile and see what fails, be aware that it returns true on failure (for unknown reasons, despite the caller flipping that for its own return value).

So you’d be looking for a point in that function where it returns true not false. Then follow the previous call that it made to see why it did that.

addPassesToEmit file is appears in two places, in clang and llvm.

The first is in clang/TargetMachine.h, where it is a virtual method which returns true by default. My issue is that I do not override this method, thus the assertion trip. No other backends seem to override this except for DirectX.

llvm/CodeGenTargetMachineImpl.cpp contains the logic that I would like to see; it basically creates an AsmPrinter. That is what led me to the target specific files in clang/lib/CodeGen/Targets/