Skip to content

Commit 44a8d38

Browse files
committed
[ctx_prof] Automatically convert available external linkage to local for modules with contextual roots
1 parent 783bac7 commit 44a8d38

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

llvm/lib/Transforms/IPO/ElimAvailExtern.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/Transforms/IPO/ElimAvailExtern.h"
1515
#include "llvm/ADT/STLExtras.h"
1616
#include "llvm/ADT/Statistic.h"
17+
#include "llvm/Analysis/CtxProfAnalysis.h"
1718
#include "llvm/IR/Constant.h"
1819
#include "llvm/IR/DebugInfoMetadata.h"
1920
#include "llvm/IR/Function.h"
@@ -88,7 +89,7 @@ static void convertToLocalCopy(Module &M, Function &F) {
8889
++NumConversions;
8990
}
9091

91-
static bool eliminateAvailableExternally(Module &M) {
92+
static bool eliminateAvailableExternally(Module &M, bool Convert) {
9293
bool Changed = false;
9394

9495
// Drop initializers of available externally global variables.
@@ -112,7 +113,7 @@ static bool eliminateAvailableExternally(Module &M) {
112113
if (F.isDeclaration() || !F.hasAvailableExternallyLinkage())
113114
continue;
114115

115-
if (ConvertToLocal)
116+
if (Convert || ConvertToLocal)
116117
convertToLocalCopy(M, F);
117118
else
118119
deleteFunction(F);
@@ -125,8 +126,10 @@ static bool eliminateAvailableExternally(Module &M) {
125126
}
126127

127128
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+
if (!eliminateAvailableExternally(M, (CtxProf && !!(*CtxProf))))
132+
;
133+
return PreservedAnalyses::all();
131134
return PreservedAnalyses::none();
132135
}

llvm/test/Transforms/EliminateAvailableExternally/transform-to-local.ll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
; REQUIRES: asserts
22
; RUN: opt -passes=elim-avail-extern -avail-extern-to-local -stats -S 2>&1 < %s | FileCheck %s
3+
; RUN: echo '[{"Guid":1234, "Counters": [1]}]' | llvm-ctxprof-util fromJSON --input=- --output=%t_profile.ctxprofdata
4+
; RUN: opt -passes='assign-guid,require<ctx-prof-analysis>,elim-avail-extern' -use-ctx-profile=%t_profile.ctxprofdata -stats -S 2>&1 < %s | FileCheck %s
35

6+
; If the profile doesn't apply to this module, nothing gets converted.
7+
; RUN: echo '[{"Guid":5678, "Counters": [1]}]' | llvm-ctxprof-util fromJSON --input=- --output=%t_profile_bad.ctxprofdata
8+
; RUN: opt -passes='assign-guid,require<ctx-prof-analysis>,elim-avail-extern' -use-ctx-profile=%t_profile_bad.ctxprofdata -stats -S 2>&1 < %s | FileCheck %s --check-prefix=NOOP
49

510
declare void @call_out(ptr %fct)
611

@@ -12,18 +17,22 @@ define available_externally hidden void @g() {
1217
ret void
1318
}
1419

15-
define void @hello(ptr %g) {
20+
define void @hello(ptr %g) !guid !0 {
1621
call void @f()
1722
%f = load ptr, ptr @f
1823
call void @call_out(ptr %f)
1924
ret void
2025
}
2126

27+
!0 = !{i64 1234}
28+
2229
; CHECK: define internal void @f.__uniq.{{[0-9|a-f]*}}()
2330
; CHECK: declare hidden void @g()
2431
; CHECK: call void @f.__uniq.{{[0-9|a-f]*}}()
2532
; CHECK-NEXT: load ptr, ptr @f
2633
; CHECK-NEXT: call void @call_out(ptr %f)
2734
; CHECK: Statistics Collected
2835
; CHECK: 1 elim-avail-extern - Number of functions converted
29-
; CHECK: 1 elim-avail-extern - Number of functions removed
36+
; CHECK: 1 elim-avail-extern - Number of functions removed
37+
38+
; NOOP: 2 elim-avail-extern - Number of functions removed

0 commit comments

Comments
 (0)