[flang-commits] [flang] [llvm] Initialize targets before creating a LibraryOptionsContext. NFC (PR #233041)

Fangrui Song via flang-commits flang-commits at lists.llvm.org
Sat Oct 17 00:27:56 PDT 2026


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

Initialize the targets to register their library options, before parsing
command line arguments.

This prepares for removing the T::Global fallback in
`LibraryOptionsContext::get<T>()`.

(The -mtriple change of llvm-split was originally added for
AMDGPU --lto-partitions #83128)

>From d476f0fbc47e4c89f05744079b952a416b674803 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Fri, 16 Oct 2026 23:47:52 -0700
Subject: [PATCH] Initialize targets before creating a LibraryOptionsContext.
 NFC

Initialize the targets to register their library options, before parsing
command line arguments.

This prepares for removing the T::Global fallback in
`LibraryOptionsContext::get<T>()`.

(The -mtriple change of llvm-split was originally added for
AMDGPU --lto-partitions #83128)
---
 flang/tools/flang-driver/fc1_main.cpp | 11 ++++++-----
 llvm/tools/llvm-split/llvm-split.cpp  |  5 ++---
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/flang/tools/flang-driver/fc1_main.cpp b/flang/tools/flang-driver/fc1_main.cpp
index a8ad38b0152137..6d5b713ed39b40 100644
--- a/flang/tools/flang-driver/fc1_main.cpp
+++ b/flang/tools/flang-driver/fc1_main.cpp
@@ -52,6 +52,12 @@ static int printSupportedCPUs(llvm::StringRef triple) {
 }
 
 int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
+  // Initialize targets first, so that --version shows registered targets and
+  // CompilerInstance's library options include the targets'.
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+  llvm::InitializeAllAsmPrinters();
+
   // Create CompilerInstance
   std::unique_ptr<CompilerInstance> flang(new CompilerInstance());
 
@@ -72,11 +78,6 @@ int fc1_main(llvm::ArrayRef<const char *> argv, const char *argv0) {
   bool success = CompilerInvocation::createFromArgs(flang->getInvocation(),
                                                     argv, diags, argv0);
 
-  // Initialize targets first, so that --version shows registered targets.
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmPrinters();
-
   // --print-supported-cpus takes priority over the actual compilation.
   if (flang->getFrontendOpts().printSupportedCPUs)
     return printSupportedCPUs(flang->getInvocation().getTargetOpts().triple);
diff --git a/llvm/tools/llvm-split/llvm-split.cpp b/llvm/tools/llvm-split/llvm-split.cpp
index 2aa5207a4f1227..bf78764186b4e3 100644
--- a/llvm/tools/llvm-split/llvm-split.cpp
+++ b/llvm/tools/llvm-split/llvm-split.cpp
@@ -244,6 +244,8 @@ Error runSplitModuleByCategory(std::unique_ptr<Module> M) {
 
 int main(int argc, char **argv) {
   InitLLVM X(argc, argv);
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
 
   LLVMContext Context;
   SMDiagnostic Err;
@@ -255,9 +257,6 @@ int main(int argc, char **argv) {
 
   std::unique_ptr<TargetMachine> TM;
   if (!MTriple.empty()) {
-    InitializeAllTargets();
-    InitializeAllTargetMCs();
-
     std::string Error;
     const Target *T = TargetRegistry::lookupTarget(TT, Error);
     if (!T) {



More information about the flang-commits mailing list