[flang-commits] [flang] [flang, unittest] Initialize targets before creating a CompilerInstance. NFC (PR #233117)

Fangrui Song via flang-commits flang-commits at lists.llvm.org
Sat Oct 17 10:34:05 PDT 2026


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

Initialize targets first to register library options before creating a
CompilerInstance.

>From 57e0dac9dc0c9f98de18c2c73bc25731668cf5d2 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 17 Oct 2026 10:28:49 -0700
Subject: [PATCH] [flang,unittest] Initialize targets before creating a
 CompilerInstance. NFC

Initialize targets first to register library options before creating a
CompilerInstance.
---
 flang/unittests/Frontend/CompilerInstanceTest.cpp |  6 +++---
 flang/unittests/Frontend/FrontendActionTest.cpp   | 13 +++++++++----
 flang/unittests/Semantics/OpenMPUtils.cpp         | 13 +++++++++----
 3 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/flang/unittests/Frontend/CompilerInstanceTest.cpp b/flang/unittests/Frontend/CompilerInstanceTest.cpp
index 60ef29c53c28d6..5f95d2f3edeb7f 100644
--- a/flang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/flang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -101,14 +101,14 @@ TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
 
 TEST(CompilerInstance,
     OpenAccDefaultNoneScalarsStrictDisableOptionUsesDriverTable) {
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+
   CompilerInstance compInst;
   compInst.createDiagnostics();
   compInst.getInvocation().getTargetOpts().triple =
       llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
 
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllTargetMCs();
-
   ASSERT_TRUE(compInst.setUpTargetMachine());
   auto &context = compInst.createNewSemanticsContext();
 
diff --git a/flang/unittests/Frontend/FrontendActionTest.cpp b/flang/unittests/Frontend/FrontendActionTest.cpp
index ec9682e27b3121..10211c5ea2fae0 100644
--- a/flang/unittests/Frontend/FrontendActionTest.cpp
+++ b/flang/unittests/Frontend/FrontendActionTest.cpp
@@ -39,6 +39,13 @@ class FrontendActionTest : public ::testing::Test {
 
   CompilerInstance compInst;
 
+  // Targets register their library options, which compInst holds from its
+  // construction.
+  static void SetUpTestSuite() {
+    llvm::InitializeAllTargets();
+    llvm::InitializeAllTargetMCs();
+  }
+
   void SetUp() override {
     // Generate a unique test file name.
     const testing::TestInfo *const testInfo =
@@ -59,12 +66,10 @@ class FrontendActionTest : public ::testing::Test {
     inputFilePath = cwd.c_str();
     inputFilePath += "/" + inputFileName;
 
-    // Set-up default target triple and initialize LLVM Targets so that the
-    // target data layout can be passed to the frontend.
+    // Set-up default target triple so that the target data layout can be
+    // passed to the frontend.
     compInst.getInvocation().getTargetOpts().triple =
         llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
-    llvm::InitializeAllTargets();
-    llvm::InitializeAllTargetMCs();
 
     compInst.createDiagnostics();
     compInst.getFrontendOpts().inputs.push_back(
diff --git a/flang/unittests/Semantics/OpenMPUtils.cpp b/flang/unittests/Semantics/OpenMPUtils.cpp
index 3bdbb7f3f6f030..45197f48a9859a 100644
--- a/flang/unittests/Semantics/OpenMPUtils.cpp
+++ b/flang/unittests/Semantics/OpenMPUtils.cpp
@@ -47,6 +47,13 @@ class OpenMPUtilsTest : public ::testing::Test {
 
   CompilerInstance compInst;
 
+  // Targets register their library options, which compInst holds from its
+  // construction.
+  static void SetUpTestSuite() {
+    llvm::InitializeAllTargets();
+    llvm::InitializeAllTargetMCs();
+  }
+
   void SetUp() override {
     // Generate a unique test file name.
     const testing::TestInfo *const testInfo =
@@ -67,13 +74,11 @@ class OpenMPUtilsTest : public ::testing::Test {
     inputFilePath = cwd.c_str();
     inputFilePath += "/" + inputFileName;
 
-    // Set-up default target triple and initialize LLVM Targets so that the
-    // target data layout can be passed to the frontend.
+    // Set-up default target triple so that the target data layout can be
+    // passed to the frontend.
     compInst.getInvocation().getTargetOpts().triple =
         llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
     compInst.getInvocation().getLangOpts().OpenMPVersion = 60;
-    llvm::InitializeAllTargets();
-    llvm::InitializeAllTargetMCs();
 
     compInst.createDiagnostics();
     compInst.getFrontendOpts().inputs.push_back(



More information about the flang-commits mailing list