-Cllvm-args usability improvement by ldm0 · Pull Request #115638 · rust-lang/rust (original) (raw)

fixes: #26338
fixes: #115564

Two problems were found during playing with -Cllvm-args

  1. When llvm.link-shared is set to false in config.toml, output of rustc -C llvm-args='--help-list-hidden' doesn't contain --emit-dwarf-unwind and --emulated-tls. When it is set to true, rustc -C llvm-args='--help-list-hidden' emits --emit-dwarf-unwind, but --emulated-tls is still missing.
  2. Setting -Cllvm-args=--emit-dwarf-unwind=always doesn't take any effect, but -Cllvm-args=-machine-outliner-reruns=3 does work.

1

Adding RegisterCodeGenFlags to register codegen flags fixed the first problem. rustc -C llvm-args='--help-list-hidden' emits full codegen flags including --emit-dwarf-unwind and --emulated-tls.

2

Constructing TargetOptions from InitTargetOptionsFromCodeGenFlags in LLVMRustCreateTargetMachine fixed the second problem. The LLVMRustSetLLVMOptions calls ParseCommandLineOptions which parses given llvm-args. For options like machine-outliner-reruns, it just works, since the codegen logic directly consumes the parsing result:

machine-outliner-reruns register
machine-outliner-reruns consumption

But for flags defined in TargetOptions and MCTargetOptions to take effect, constructing them with InitTargetOptionsFromCodeGenFlags is essential, or the parsing result is just not consumed. Similar patterns can be observed in lli, llc, etc.