[llvm] [llvm] prepare explicit template instantiations in llvm/CodeGen for DLL export annotations (PR #140653)

Andrew Rogers via llvm-commits llvm-commits at lists.llvm.org
Mon May 19 17:35:27 PDT 2025


https://github.com/andrurogerz created https://github.com/llvm/llvm-project/pull/140653

## Purpose

This patch prepares the llvm/CodeGen library for public interface annotations in support of an LLVM Windows DLL (shared library) build, tracked in #109483. The purpose of this patch is to make the upcoming codemod of this library more straight-forward. It is not expected to impact any functionality.

The actual export/visibility annotations (e.g. `LLVM_ABI`) will be added in a subsequent patch. None of these changes are technically required for the Windows MSVC DLL build. They are required to build with visibility annotations using Clang and gcc on Linux/Darwin/etc.

## Overview
This PR does three things in preparation for adding `LLVM_ABI` annotations to llvm/CodeGen:
1. Refactor the definition of `MachineFunctionAnalysisManager` to its own header file. Without this change, it is not possible to add visibility annotations to the declaration with causing gcc to produce warnings.
2. Move the explicit instantiations of the `GenericDomTreeUpdater` template earlier in the header file. They need to appear before they are used in the `MachineDomTreeUpdater` class definition or gcc will produce warnings once visibility annotations are added to them.
3. Explicitly include `Machine.h` and `Function.h` from `MachinePassManager.cpp` so that `Function` and `Machine` types are available for the explicit instantiations of `InnerAnalysisManagerProxy`. Without this change, Clang only exports one of the templates after visibility annotations are added to both of them. Unclear if this is a Clang bug or expected behavior, but this change avoids the issue and should be harmless.

## Background

The LLVM Windows DLL effort is tracked in #109483. Additional context is provided in [this discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307). 

Clang and gcc handle visibility attributes on explicit template instantiations a bit differently; gcc is pickier and generates `-Wattribute` warnings when an explicit instantiation with a visibility annotation appears after the type has already appeared in the translation unit. These warnings can be avoided by moving explicit template instantiations so they always appear first.

## Validation

Local builds and tests to validate cross-platform compatibility. This included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang

>From 857b9cc55c88b1952a5e0ba778f8c51e500406eb Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 19 May 2025 15:32:59 -0700
Subject: [PATCH 1/4] [llvm] include headers to define Function and Module
 types used in template instantiations

---
 llvm/lib/CodeGen/MachinePassManager.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/llvm/lib/CodeGen/MachinePassManager.cpp b/llvm/lib/CodeGen/MachinePassManager.cpp
index 3eed874903f7c..bbe386507fcd2 100644
--- a/llvm/lib/CodeGen/MachinePassManager.cpp
+++ b/llvm/lib/CodeGen/MachinePassManager.cpp
@@ -14,6 +14,8 @@
 #include "llvm/CodeGen/MachineFunction.h"
 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Module.h"
 #include "llvm/IR/PassManagerImpl.h"
 
 using namespace llvm;

>From 45ad80f3a157c8f886e158daec0c0ec25ae88ea4 Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 19 May 2025 15:41:02 -0700
Subject: [PATCH 2/4] [llvm] refactor MachineFunctionAnalysisManager definition
 to its own header

---
 llvm/include/llvm/CodeGen/MachineBasicBlock.h |  3 +--
 .../CodeGen/MachineFunctionAnalysisManager.h  | 27 +++++++++++++++++++
 .../include/llvm/CodeGen/MachinePassManager.h |  5 +---
 3 files changed, 29 insertions(+), 6 deletions(-)
 create mode 100644 llvm/include/llvm/CodeGen/MachineFunctionAnalysisManager.h

diff --git a/llvm/include/llvm/CodeGen/MachineBasicBlock.h b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
index 9c563d761c1d9..201e35d30cee2 100644
--- a/llvm/include/llvm/CodeGen/MachineBasicBlock.h
+++ b/llvm/include/llvm/CodeGen/MachineBasicBlock.h
@@ -18,6 +18,7 @@
 #include "llvm/ADT/SparseBitVector.h"
 #include "llvm/ADT/ilist.h"
 #include "llvm/ADT/iterator_range.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/CodeGen/MachineInstr.h"
 #include "llvm/CodeGen/MachineInstrBundleIterator.h"
 #include "llvm/IR/DebugLoc.h"
@@ -46,8 +47,6 @@ class LiveIntervals;
 class LiveVariables;
 class TargetRegisterClass;
 class TargetRegisterInfo;
-template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
-using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
 
 // This structure uniquely identifies a basic block section.
 // Possible values are
diff --git a/llvm/include/llvm/CodeGen/MachineFunctionAnalysisManager.h b/llvm/include/llvm/CodeGen/MachineFunctionAnalysisManager.h
new file mode 100644
index 0000000000000..ccf72d996dc4b
--- /dev/null
+++ b/llvm/include/llvm/CodeGen/MachineFunctionAnalysisManager.h
@@ -0,0 +1,27 @@
+//===- llvm/CodeGen/MachineFunctionAnalysisManager.h ------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Typedef for MachineFunctionAnalysisManager as an explicit instantiation of
+// AnalysisManager<MachineFunction>.
+//
+//===----------------------------------------------------------------------===//
+#ifndef LLVM_CODEGEN_MACHINEFUNCTIONANALYSISMANAGER
+#define LLVM_CODEGEN_MACHINEFUNCTIONANALYSISMANAGER
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class MachineFunction;
+
+extern template class AnalysisManager<MachineFunction>;
+using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
+
+} // namespace llvm
+
+#endif
diff --git a/llvm/include/llvm/CodeGen/MachinePassManager.h b/llvm/include/llvm/CodeGen/MachinePassManager.h
index 69b5f6e92940c..57a6c1a65be46 100644
--- a/llvm/include/llvm/CodeGen/MachinePassManager.h
+++ b/llvm/include/llvm/CodeGen/MachinePassManager.h
@@ -24,6 +24,7 @@
 #include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/CodeGen/MachineFunction.h"
+#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/PassManagerInternal.h"
 #include "llvm/Support/Error.h"
@@ -31,10 +32,6 @@
 namespace llvm {
 class Module;
 class Function;
-class MachineFunction;
-
-extern template class AnalysisManager<MachineFunction>;
-using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
 
 /// An RAII based helper class to modify MachineFunctionProperties when running
 /// pass. Define a MFPropsModifier in PassT::run to set

>From 3c848b41420b4f7a702731835be73674c904821d Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 19 May 2025 15:53:30 -0700
Subject: [PATCH 3/4] [llvm] remove redundant DominatorTreeBase::addRoot method
 specialization

---
 llvm/include/llvm/CodeGen/MachineDominators.h | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineDominators.h b/llvm/include/llvm/CodeGen/MachineDominators.h
index 61635ff64502d..d373be6263f64 100644
--- a/llvm/include/llvm/CodeGen/MachineDominators.h
+++ b/llvm/include/llvm/CodeGen/MachineDominators.h
@@ -32,12 +32,6 @@ class MachineFunction;
 class Module;
 class raw_ostream;
 
-template <>
-inline void DominatorTreeBase<MachineBasicBlock, false>::addRoot(
-    MachineBasicBlock *MBB) {
-  this->Roots.push_back(MBB);
-}
-
 extern template class DomTreeNodeBase<MachineBasicBlock>;
 extern template class DominatorTreeBase<MachineBasicBlock, false>; // DomTree
 

>From 4956ca3ec4046dfc575d3d25f8acbe4ec09761c0 Mon Sep 17 00:00:00 2001
From: Andrew Rogers <andrurogerz at gmail.com>
Date: Mon, 19 May 2025 17:05:27 -0700
Subject: [PATCH 4/4] [llvm] move GenericDomTreeUpdater template instantiations

---
 .../llvm/CodeGen/MachineDomTreeUpdater.h      | 31 ++++++++++---------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/MachineDomTreeUpdater.h b/llvm/include/llvm/CodeGen/MachineDomTreeUpdater.h
index fcdc0becf31c1..ff9f743bd1276 100644
--- a/llvm/include/llvm/CodeGen/MachineDomTreeUpdater.h
+++ b/llvm/include/llvm/CodeGen/MachineDomTreeUpdater.h
@@ -20,6 +20,22 @@
 namespace llvm {
 
 class MachinePostDominatorTree;
+class MachineDomTreeUpdater;
+
+extern template class GenericDomTreeUpdater<
+    MachineDomTreeUpdater, MachineDominatorTree, MachinePostDominatorTree>;
+
+extern template void
+GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
+                      MachinePostDominatorTree>::recalculate(MachineFunction
+                                                                 &MF);
+
+extern template void GenericDomTreeUpdater<
+    MachineDomTreeUpdater, MachineDominatorTree,
+    MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/true>();
+extern template void GenericDomTreeUpdater<
+    MachineDomTreeUpdater, MachineDominatorTree,
+    MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/false>();
 
 class MachineDomTreeUpdater
     : public GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
@@ -61,20 +77,5 @@ class MachineDomTreeUpdater
   /// Returns true if at least one MachineBasicBlock is deleted.
   bool forceFlushDeletedBB();
 };
-
-extern template class GenericDomTreeUpdater<
-    MachineDomTreeUpdater, MachineDominatorTree, MachinePostDominatorTree>;
-
-extern template void
-GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
-                      MachinePostDominatorTree>::recalculate(MachineFunction
-                                                                 &MF);
-
-extern template void GenericDomTreeUpdater<
-    MachineDomTreeUpdater, MachineDominatorTree,
-    MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/true>();
-extern template void GenericDomTreeUpdater<
-    MachineDomTreeUpdater, MachineDominatorTree,
-    MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/false>();
 } // namespace llvm
 #endif // LLVM_CODEGEN_MACHINEDOMTREEUPDATER_H



More information about the llvm-commits mailing list