[lld] [ELF] Initialize LLVM targets before parsing -mllvm (PR #228314)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 20:54:37 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/228314

readConfigs parses -mllvm before initLLVM initializes the targets, so an
option that a target registers at initialization (currently only Sparc #227977)
is rejected as unknown.
Call initLLVM first, as the COFF, MachO, and wasm ports do. This will be
covered once a -mllvm used by lld/test/ELF/lto tests migrates.

>From 3857892980a9227e0b75238fd36d402b44e73398 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Thu, 1 Oct 2026 20:49:44 -0700
Subject: [PATCH] [ELF] Initialize LLVM targets before parsing -mllvm

readConfigs parses -mllvm before initLLVM initializes the targets, so an
option that a target registers at initialization (currently only Sparc #227977)
is rejected as unknown.
Call initLLVM first, as the COFF, MachO, and wasm ports do. This will be
covered once a -mllvm used by lld/test/ELF/lto tests migrates.
---
 lld/ELF/Driver.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 4e5a5eb8664d7..00842f8f08bfd 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -699,6 +699,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
     }
   }
 
+  initLLVM();
   readConfigs(ctx, args);
   checkZOptions(ctx, args);
 
@@ -716,7 +717,6 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
   {
     llvm::TimeTraceScope timeScope("ExecuteLinker");
 
-    initLLVM();
     createFiles(args);
     if (errCount(ctx))
       return;
@@ -1368,6 +1368,8 @@ static void parseClangOption(Ctx &ctx, StringRef opt, const Twine &msg) {
   raw_string_ostream os(err);
 
   const char *argv[] = {ctx.arg.progName.data(), opt.data()};
+  // Called after initLLVM() to ensure target-specific RegisterLibraryOptions
+  // have been called.
   if (cl::ParseCommandLineOptions(2, argv, "", &os))
     return;
   ErrAlways(ctx) << msg << ": " << StringRef(err).trim();



More information about the llvm-commits mailing list