14
14
#include " llvm/Transforms/IPO/ElimAvailExtern.h"
15
15
#include " llvm/ADT/STLExtras.h"
16
16
#include " llvm/ADT/Statistic.h"
17
+ #include " llvm/Analysis/CtxProfAnalysis.h"
17
18
#include " llvm/IR/Constant.h"
18
19
#include " llvm/IR/DebugInfoMetadata.h"
19
20
#include " llvm/IR/Function.h"
@@ -88,7 +89,7 @@ static void convertToLocalCopy(Module &M, Function &F) {
88
89
++NumConversions;
89
90
}
90
91
91
- static bool eliminateAvailableExternally (Module &M) {
92
+ static bool eliminateAvailableExternally (Module &M, bool Convert ) {
92
93
bool Changed = false ;
93
94
94
95
// Drop initializers of available externally global variables.
@@ -112,7 +113,7 @@ static bool eliminateAvailableExternally(Module &M) {
112
113
if (F.isDeclaration () || !F.hasAvailableExternallyLinkage ())
113
114
continue ;
114
115
115
- if (ConvertToLocal)
116
+ if (Convert || ConvertToLocal)
116
117
convertToLocalCopy (M, F);
117
118
else
118
119
deleteFunction (F);
@@ -125,8 +126,16 @@ static bool eliminateAvailableExternally(Module &M) {
125
126
}
126
127
127
128
PreservedAnalyses
128
- EliminateAvailableExternallyPass::run (Module &M, ModuleAnalysisManager &) {
129
- if (!eliminateAvailableExternally (M))
130
- return PreservedAnalyses::all ();
129
+ EliminateAvailableExternallyPass::run (Module &M, ModuleAnalysisManager &MAM) {
130
+ auto *CtxProf = MAM.getCachedResult <CtxProfAnalysis>(M);
131
+ // Convert to local instead of eliding if we use contextual profiling in this
132
+ // module. This is because the IPO decisions performed with contextual
133
+ // information will likely differ from decisions made without. For a function
134
+ // that's imported, its optimizations will, thus, differ, and be specialized
135
+ // for this contextual information. Eliding it in favor of the original would
136
+ // undo these optimizations.
137
+ if (!eliminateAvailableExternally (M, /* Convert=*/ (CtxProf && !!(*CtxProf))))
138
+ ;
139
+ return PreservedAnalyses::all ();
131
140
return PreservedAnalyses::none ();
132
141
}
0 commit comments