Skip to content

[clang] Register all LLVM targets in AllClangUnitTest main #144428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

rnk
Copy link
Collaborator

@rnk rnk commented Jun 16, 2025

Addresses feedback in #134196 (comment)

Makes the tests less sensitive to target registration from unrelated test fixtures by registering everything up front.

Addresses feedback in llvm#134196 (comment)

Makes the tests less sensitive to target registration from unrelated
test fixtures by registering everything up front.
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jun 16, 2025
@rnk rnk requested a review from kadircet June 16, 2025 20:11
@llvmbot
Copy link
Member

llvmbot commented Jun 16, 2025

@llvm/pr-subscribers-clang

Author: Reid Kleckner (rnk)

Changes

Addresses feedback in #134196 (comment)

Makes the tests less sensitive to target registration from unrelated test fixtures by registering everything up front.


Full diff: https://github.com/llvm/llvm-project/pull/144428.diff

2 Files Affected:

  • (added) clang/unittests/AllClangUnitTests.cpp (+24)
  • (modified) clang/unittests/CMakeLists.txt (+1)
diff --git a/clang/unittests/AllClangUnitTests.cpp b/clang/unittests/AllClangUnitTests.cpp
new file mode 100644
index 0000000000000..1726cabc8107c
--- /dev/null
+++ b/clang/unittests/AllClangUnitTests.cpp
@@ -0,0 +1,24 @@
+//===- clang/unittests/AllClangUnitTests.cpp ------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "gtest/gtest.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/TargetSelect.h"
+
+// This custom main entry point for the AllClangUnitTests binary registers all
+// tests on startup, so the tests don't become sensitive to target registration
+// within the test suite.
+int main(int argc, char **argv) {
+  ::testing::InitGoogleTest(&argc, argv);
+  llvm::cl::ParseCommandLineOptions(argc, argv);
+
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+
+  return RUN_ALL_TESTS();
+}
diff --git a/clang/unittests/CMakeLists.txt b/clang/unittests/CMakeLists.txt
index aef28f914b640..54c781a35c20c 100644
--- a/clang/unittests/CMakeLists.txt
+++ b/clang/unittests/CMakeLists.txt
@@ -117,6 +117,7 @@ get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
 get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
 add_distinct_clang_unittest(AllClangUnitTests
   ${SRCS}
+  AllClangUnitTests.cpp
   CLANG_LIBS
   ${CLANG_LIBS}
   LINK_LIBS

Copy link

github-actions bot commented Jun 16, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@@ -117,6 +117,7 @@ get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
add_distinct_clang_unittest(AllClangUnitTests
${SRCS}
AllClangUnitTests.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we're still linking against llvm_gtest_main, https://github.com/llvm/llvm-project/blob/main/third-party/unittest/UnitTestMain/TestMain.cpp. we probably need to do some fiddling in the build rules as well to make sure, we only link against llvm_gtest. (not sure if you want to have other msvc related magic in here as well)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spot checked two other unit tests that have custom mains, and they don't do anything special:

  1. llvm-cfi-verify main , cmake
  2. LiveIntervalTest main, cmake

I think archive semantics are pretty portable between platforms, so I think this just works. The sources used to build the unit test executable override the main provided by the gtest library.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't feel great about relying on linking order and linker not loading llvm_gtest_main solely because it wasn't referenced, but I guess that's a theoretical concern that won't matter much in practice (since llvm_gtest_main doesn't really define symbols apart from TestMainArgv0, at least yet). so leaving this up to you.

but can we at least leave a comment here, mentioning that we're also linking against llvm_gtest_main to make it easier for people to investigate if they encounter any such issues?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, on second thought, there's a lot more going on in UnitTestMain than I thought. I rewrote this to use a dynamic initializer to register the targets instead. Those will reliably run before test fixtures. The only risk here is that we encounter intialization-order-fiasco issues, but I spot-checked x86, and it seems to be resilient to that... I will test with ASan locally to build confidence, but PTAL at the new approach.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot, this is really neat! and yeah i think initialization-order-fiasco should be less of a concern here.

Copy link
Member

@kadircet kadircet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the quick fix!

@@ -117,6 +117,7 @@ get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
add_distinct_clang_unittest(AllClangUnitTests
${SRCS}
AllClangUnitTests.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't feel great about relying on linking order and linker not loading llvm_gtest_main solely because it wasn't referenced, but I guess that's a theoretical concern that won't matter much in practice (since llvm_gtest_main doesn't really define symbols apart from TestMainArgv0, at least yet). so leaving this up to you.

but can we at least leave a comment here, mentioning that we're also linking against llvm_gtest_main to make it easier for people to investigate if they encounter any such issues?

…rs on startup instead of overriding regular unit test main
@@ -117,6 +117,7 @@ get_property(LINK_LIBS GLOBAL PROPERTY CLANG_UNITTEST_LINK_LIBS)
get_property(LLVM_COMPONENTS GLOBAL PROPERTY CLANG_UNITTEST_LLVM_COMPONENTS)
add_distinct_clang_unittest(AllClangUnitTests
${SRCS}
AllClangUnitTests.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks a lot, this is really neat! and yeah i think initialization-order-fiasco should be less of a concern here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants