TargetSelect.h and layering

So, I’m setting about trying to fix the layering of TargetSelect.h (& TargetRegistry.h, but haven’t really got to its issues yet).

The issues can be demonstrated fairly plainly by moving any/all of TargetSelect.h’s functions out of line. Pretty much all programs in LLVM fail to link because libSupport doesn’t actually depend on all the targets, quite the opposite in fact.

So, I set about creating a libLLVMAllTargets (for want of any better name/as a straw man) & wherever LLVM_TARGETS_TO_BUILD was listed in a CMakeLists.txt LLVM_LINK_COMPONENTS, I’d add in a dependence on AllTargets (& I moved all the function definitions in TargetSelect.h out of line to flush out/prove the dependencies were right).

A few problems:

It seems there are users of TargetSelect.h that want pretty much every granular split of the functionality possible. For example:

  • Some executables only depend on the asm parsers and only call InitializeAllAsmParsers (similarly for any other All*) - see the various AllTargetsAsmParsers and similar rules in cmake/modules/LLVM-Config.cmake
  • Some executables depend only on the native target support InitializeNativeTarget* functions - see the JIT tests and the “native” and “nativecodegen” rules in the above cmake file.

So… - any ideas? Should we just give up some of this fine grained dependency control & say you can only depend on all targets? (or maybe “all targets or the native target” and I’ll make two libraries - libLLVMAllTargets and libLLVMNativeTarget?)
Do we keep the fine grained dependency & have libLLVM{AllTargets,NativeTarget}{AsmPrinters,AsmParsers,Descs,Disassemblers,Infos} ?

Happy to make any of that happen, but curious what people might prefer.

Alternatively we can really say this header is a textual header - it’s included generally only once in a whole program, the functions are called only once, etc. Though that does seem a little unfortunate on principle but not much practical problem with it, I think. It’d be nice in theory to be able to depend on the right library, have that bring in the right dependencies, etc.

As designed, TargetSelect.h doesn't fit neatly into the normal way of
arranging libraries. I'd mark it textual and leave it alone.

Alternatively, we could make AllTargetsDescs and AllTargetsInfos and all
the other synthetic libraries in CMake into real libaries and sink the
bodies of these inline functions into each tiny little library. Doesn't
seem quite worth it, though.

Not sure about that - yeah, it’s a bit of the degenerate case, for sure.

But in a build system like Google’s, where a lib has other lib dependencies (whereas in the LLVM CMake build it seems libs don’t depend on other libs - so every executable has to explicitly list its transitive lib dependencies) it’s pretty nice to have these little libraries explicitly in the build graph - much like we have those synthetic library targets in the CMake rules, so it’s easy to depend on the right/full things.

(but because the CMake lib rules for LLVM don’t actually describe lib dependencies, I think even ‘fixing’ this in upstream LLVM wouldn’t make the dep situation better - the synthetic targets would just have to expand to the underlying libs + the wrapper/selector lib as well)

Yeah, maybe… just makes me a bit sad to have inline functions that can’t be trivially out-of-lined if/when desired, because they layering isn’t fully/correctly represented in the build system. Modular codegen’s been a good justification to flush out & fix several of these tricksy layering violations in LLVM already.

Ping - any further/other thoughts from folks? I’m not /too/ fussed, but generally like the idea of lib layering being simple/clear/obvious, but understand these are sort of the degenerate/worst case.

My only alternate ideas are:

a) To heck with this only a single target thing.
b) Autogenerate the entire file and library support as part of the build and have the various functions “defined” in the appropriate libraries.

I don’t really think a) is feasible, and b) is a bit of a stretch too. :\


FWIW, I think the end state we’ll end up wanting is what you describe in your email: fine grained dependencies and something like libLLVM{AllTargets,NativeTarget}{AsmPrinters,AsmParsers,Descs,Disassemblers,Infos}

I think the “Native” thing can be solved by having a CMake (and llvm-config) level alias that points to a specific single target library. Then I think you could actually build lib/Target/All/… directory tree that provides the “all” libraries and links everything together.

Last but not least, I think in this world we’d want each of the narrow, specific interfaces to be inside the individual target libraries rather than squeezed into a single header file.

But this is a lot of churn and work. So I’m not seeing a huge problem if it is ust too much churn and work and you make the header a textual header for now. I’d document this super clearly in the header and lift it up a directory to live alongside our other textual headers like LinkAllPasses.h

Could you guys clarify one thing for me? It sounds like the idea is that the current model of configuring the selection of targets would go away, to be replaced by an all-or-native-only switch. Sometimes I like to configure X86+ARM because that reduces rebuild time and still gets me the vast majority of debug-info tests.

Or maybe you’re using “all” as a shorthand for “all configured targets” which would be just fine.



Yes, “all” is “all configured” I think throughout this discussion. Sorry for that confusion.

Yep. Here too.