Per-Target LLVM Module Flags

From LLVM 11.0, the RISC-V targets will need to add some target-specific module flags when generating LLVM IR inside Rustc (this is a change we're requiring of all RISC-V LLVM frontends, and is needed to support LTO).

I had a quick look through librustc_codegen_llvm and it doesn't seem like any other targets need to do this.

I presume the correct approach is to start all the way from the llvmrust C++ layer (because there's no way of adding target flags which can error on conflicts, otherwise), and then try to thread the information down from somewhere target-specific.

I note there are two places the information could potentially be added:

  • The target-specific abi lowering code (src/librustc_target/abi)
  • The target definitions. Guidance on which might turn out to be better would be helfpful.

Adding the module metadata seems simple, since:

  • adding the module metadata on other targets should not cause problems (it would just be ignored).
  • target specs in rustc already have the llvm_abiname field, and I believe for RISC-V its contents match what's expected in the target-abi metadata

Could we just unconditionally (i.e., target-independent) add the metadata whenever we create a module during codegen?

Is anything else needed aside from module metadata? I vaguely recall that TargetMachine construction during LTO might need changes too, something like: TM constructed before loading module, but TM should take the ABI from the module metadata. As rustc has some custom code for driving LTO, changes in this area might need to be ported to rustc as well.