Skip to content

Commit 98595cf

Browse files
authored
[llvm] prepare explicit template instantiations in llvm/CodeGen for DLL export annotations (#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 `LLVM_ABI` annotations will be added in a subsequent patch. These changes are required to build with visibility annotations using Clang and gcc on Linux/Darwin/etc; Windows DLL can build fine without them. ## Overview This PR does four things in preparation for adding `LLVM_ABI` annotations to llvm/CodeGen: 1. Explicitly include `Machine.h` and `Function.h` headers from `MachinePassManager.cpp` so that `Function` and `Machine` types are available for the instantiations of `InnerAnalysisManagerProxy`. Without this change, Clang only will only export one of the templates after visibility annotations are added to them. Unclear if this is a Clang bug or expected behavior, but this change avoids the issue and should be harmless. 2. 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 `-Wattribute` warnings. 3. Remove the redundant specialization of the `DominatorTreeBase<MachineBasicBlock, false>::addRoot` method. The specialization is the same as implemented in `DominatorTreeBase` so should be unnecessary. Without this change, it is not possible to annotate the subsequent instantiations of `DominatorTreeBase` in the header file without gcc producing `-Wattribute` warnings. Mark unspecialized `addRoot` as `inline` to match the removed specialized version. 4. Move the explicit instantiations of the `GenericDomTreeUpdater` template earlier in the header file. These need to appear before being used in the `MachineDomTreeUpdater` class definition or gcc will produce warnings once visibility annotations are added. ## 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
1 parent e25abd0 commit 98595cf

7 files changed

+48
-28
lines changed

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/SparseBitVector.h"
1919
#include "llvm/ADT/ilist.h"
2020
#include "llvm/ADT/iterator_range.h"
21+
#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
2122
#include "llvm/CodeGen/MachineInstr.h"
2223
#include "llvm/CodeGen/MachineInstrBundleIterator.h"
2324
#include "llvm/IR/DebugLoc.h"
@@ -46,8 +47,6 @@ class LiveIntervals;
4647
class LiveVariables;
4748
class TargetRegisterClass;
4849
class TargetRegisterInfo;
49-
template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
50-
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
5150

5251
// This structure uniquely identifies a basic block section.
5352
// Possible values are

llvm/include/llvm/CodeGen/MachineDomTreeUpdater.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,22 @@
2020
namespace llvm {
2121

2222
class MachinePostDominatorTree;
23+
class MachineDomTreeUpdater;
24+
25+
extern template class GenericDomTreeUpdater<
26+
MachineDomTreeUpdater, MachineDominatorTree, MachinePostDominatorTree>;
27+
28+
extern template void
29+
GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
30+
MachinePostDominatorTree>::recalculate(MachineFunction
31+
&MF);
32+
33+
extern template void GenericDomTreeUpdater<
34+
MachineDomTreeUpdater, MachineDominatorTree,
35+
MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/true>();
36+
extern template void GenericDomTreeUpdater<
37+
MachineDomTreeUpdater, MachineDominatorTree,
38+
MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/false>();
2339

2440
class MachineDomTreeUpdater
2541
: public GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
@@ -61,20 +77,5 @@ class MachineDomTreeUpdater
6177
/// Returns true if at least one MachineBasicBlock is deleted.
6278
bool forceFlushDeletedBB();
6379
};
64-
65-
extern template class GenericDomTreeUpdater<
66-
MachineDomTreeUpdater, MachineDominatorTree, MachinePostDominatorTree>;
67-
68-
extern template void
69-
GenericDomTreeUpdater<MachineDomTreeUpdater, MachineDominatorTree,
70-
MachinePostDominatorTree>::recalculate(MachineFunction
71-
&MF);
72-
73-
extern template void GenericDomTreeUpdater<
74-
MachineDomTreeUpdater, MachineDominatorTree,
75-
MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/true>();
76-
extern template void GenericDomTreeUpdater<
77-
MachineDomTreeUpdater, MachineDominatorTree,
78-
MachinePostDominatorTree>::applyUpdatesImpl</*IsForward=*/false>();
7980
} // namespace llvm
8081
#endif // LLVM_CODEGEN_MACHINEDOMTREEUPDATER_H

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ class MachineFunction;
3232
class Module;
3333
class raw_ostream;
3434

35-
template <>
36-
inline void DominatorTreeBase<MachineBasicBlock, false>::addRoot(
37-
MachineBasicBlock *MBB) {
38-
this->Roots.push_back(MBB);
39-
}
40-
4135
extern template class DomTreeNodeBase<MachineBasicBlock>;
4236
extern template class DominatorTreeBase<MachineBasicBlock, false>; // DomTree
4337

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===- llvm/CodeGen/MachineFunctionAnalysisManager.h ------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Typedef for MachineFunctionAnalysisManager as an explicit instantiation of
10+
// AnalysisManager<MachineFunction>.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
#ifndef LLVM_CODEGEN_MACHINEFUNCTIONANALYSISMANAGER
14+
#define LLVM_CODEGEN_MACHINEFUNCTIONANALYSISMANAGER
15+
16+
#include "llvm/IR/PassManager.h"
17+
18+
namespace llvm {
19+
20+
class MachineFunction;
21+
22+
extern template class AnalysisManager<MachineFunction>;
23+
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
24+
25+
} // namespace llvm
26+
27+
#endif

llvm/include/llvm/CodeGen/MachinePassManager.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,14 @@
2424
#include "llvm/ADT/FunctionExtras.h"
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/CodeGen/MachineFunction.h"
27+
#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
2728
#include "llvm/IR/PassManager.h"
2829
#include "llvm/IR/PassManagerInternal.h"
2930
#include "llvm/Support/Error.h"
3031

3132
namespace llvm {
3233
class Module;
3334
class Function;
34-
class MachineFunction;
35-
36-
extern template class AnalysisManager<MachineFunction>;
37-
using MachineFunctionAnalysisManager = AnalysisManager<MachineFunction>;
3835

3936
/// An RAII based helper class to modify MachineFunctionProperties when running
4037
/// pass. Define a MFPropsModifier in PassT::run to set

llvm/include/llvm/Support/GenericDomTree.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ class DominatorTreeBase {
918918
}
919919

920920
protected:
921-
void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
921+
inline void addRoot(NodeT *BB) { this->Roots.push_back(BB); }
922922

923923
DomTreeNodeBase<NodeT> *createNode(NodeT *BB,
924924
DomTreeNodeBase<NodeT> *IDom = nullptr) {

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include "llvm/CodeGen/MachineFunction.h"
1515
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
1616
#include "llvm/CodeGen/MachineModuleInfo.h"
17+
#include "llvm/IR/Function.h"
18+
#include "llvm/IR/Module.h"
1719
#include "llvm/IR/PassManagerImpl.h"
1820

1921
using namespace llvm;

0 commit comments

Comments
 (0)