[llvm] 2032d7a - [llvm][test][CGPluginTest] Avoid __attribute__((constructor)) for MSVC (#227310)

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 30 05:06:49 PDT 2026


Author: Stefan Gränitz
Date: 2026-09-30T14:06:37+02:00
New Revision: 2032d7a2f64eb37a24a997605f60713e2815cc7d

URL: https://github.com/llvm/llvm-project/commit/2032d7a2f64eb37a24a997605f60713e2815cc7d
DIFF: https://github.com/llvm/llvm-project/commit/2032d7a2f64eb37a24a997605f60713e2815cc7d.diff

LOG: [llvm][test][CGPluginTest] Avoid __attribute__((constructor)) for MSVC (#227310)

`__attribute__((constructor))` isn't supported by MSVC. This patch
replaces the initializer function with a static object whose constructor
performs the pass registration.

Added: 
    

Modified: 
    llvm/docs/WritingAnLLVMPass.md
    llvm/unittests/CodeGen/CGPluginTest/Plugin/Plugin.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/docs/WritingAnLLVMPass.md b/llvm/docs/WritingAnLLVMPass.md
index 7d56a546a4dd7..c1b8ad5f125fc 100644
--- a/llvm/docs/WritingAnLLVMPass.md
+++ b/llvm/docs/WritingAnLLVMPass.md
@@ -424,12 +424,16 @@ RegisterTargetPassConfigCallback X{[](auto &TM, auto &PM, auto *TPC) {
 }};
 ```
 
-Note that passes still have to be registered:
+Note that passes still have to be registered. This can be done using a
+static object whose constructor runs when the plugin is loaded:
 
 ```cpp
-__attribute__((constructor)) static void initCodeGenPlugin() {
-  initializeExamplePass(*PassRegistry::getPassRegistry());
-}
+struct InitCodeGenPlugin {
+  InitCodeGenPlugin() {
+    initializeExamplePass(*PassRegistry::getPassRegistry());
+  }
+};
+static InitCodeGenPlugin InitCodeGenPluginInstance;
 ```
 
 (writing-an-llvm-pass-interaction)=

diff  --git a/llvm/unittests/CodeGen/CGPluginTest/Plugin/Plugin.cpp b/llvm/unittests/CodeGen/CGPluginTest/Plugin/Plugin.cpp
index 76889a727976a..14e8e3290c336 100644
--- a/llvm/unittests/CodeGen/CGPluginTest/Plugin/Plugin.cpp
+++ b/llvm/unittests/CodeGen/CGPluginTest/Plugin/Plugin.cpp
@@ -20,11 +20,14 @@ namespace {
     [](auto &TM, auto &PM, auto *TPC) {
       TPC->insertPass(&GCLoweringID, &CodeGenTest::ID);
     }};
-} // namespace
 
-__attribute__((constructor)) static void initCodeGenPlugin() {
-  initializeCodeGenTestPass(*PassRegistry::getPassRegistry());
-}
+struct InitCodeGenPlugin {
+  InitCodeGenPlugin() {
+    initializeCodeGenTestPass(*PassRegistry::getPassRegistry());
+  }
+};
+[[maybe_unused]] InitCodeGenPlugin InitCodeGenPluginInstance;
+} // namespace
 
 // We use -load-pass-plugin to specify and load the plugin shared-lib, but not
 // the actual pass-plugin interface. This isn't available in the codegen


        


More information about the llvm-commits mailing list