Skip to content

Commit 0d8b5d6

Browse files
committedJul 27, 2017
[ICP] Migrate to OptimizationRemarkEmitter
This is a module pass so for the old PM, we can't use ORE, the function analysis pass. Instead ORE is created on the fly. A few notes: - isPromotionLegal is folded in the caller since we want to emit the Function in the remark but we can only do that if the symbol table look-up succeeded. - There was good test coverage for remarks in this pass. - promoteIndirectCall uses ORE conditionally since it's also used from SampleProfile which does not use ORE yet. Fixes PR33792. Differential Revision: https://reviews.llvm.org/D35929 llvm-svn: 309294
1 parent 6374331 commit 0d8b5d6

File tree

5 files changed

+66
-43
lines changed

5 files changed

+66
-43
lines changed
 

‎llvm/include/llvm/Transforms/Instrumentation.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace llvm {
4040

4141
class FunctionPass;
4242
class ModulePass;
43+
class OptimizationRemarkEmitter;
4344

4445
/// Instrumentation passes often insert conditional checks into entry blocks.
4546
/// Call this function before splitting the entry block to move instructions
@@ -109,7 +110,8 @@ bool isLegalToPromote(Instruction *Inst, Function *F, const char **Reason);
109110
// Returns the promoted direct call instruction.
110111
Instruction *promoteIndirectCall(Instruction *Inst, Function *F, uint64_t Count,
111112
uint64_t TotalCount,
112-
bool AttachProfToDirectCall);
113+
bool AttachProfToDirectCall,
114+
OptimizationRemarkEmitter *ORE = nullptr);
113115

114116
/// Options for the frontend instrumentation based profiling pass.
115117
struct InstrProfOptions {

‎llvm/lib/Transforms/Instrumentation/IndirectCallPromotion.cpp

+53-35
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Analysis/GlobalsModRef.h"
2222
#include "llvm/Analysis/IndirectCallPromotionAnalysis.h"
2323
#include "llvm/Analysis/IndirectCallSiteVisitor.h"
24+
#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
2425
#include "llvm/IR/BasicBlock.h"
2526
#include "llvm/IR/CallSite.h"
2627
#include "llvm/IR/DerivedTypes.h"
@@ -160,9 +161,7 @@ class ICallPromotionFunc {
160161

161162
bool SamplePGO;
162163

163-
// Test if we can legally promote this direct-call of Target.
164-
bool isPromotionLegal(Instruction *Inst, uint64_t Target, Function *&F,
165-
const char **Reason = nullptr);
164+
OptimizationRemarkEmitter &ORE;
166165

167166
// A struct that records the direct target and it's call count.
168167
struct PromotionCandidate {
@@ -192,8 +191,8 @@ class ICallPromotionFunc {
192191

193192
public:
194193
ICallPromotionFunc(Function &Func, Module *Modu, InstrProfSymtab *Symtab,
195-
bool SamplePGO)
196-
: F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO) {}
194+
bool SamplePGO, OptimizationRemarkEmitter &ORE)
195+
: F(Func), M(Modu), Symtab(Symtab), SamplePGO(SamplePGO), ORE(ORE) {}
197196

198197
bool processFunction();
199198
};
@@ -242,17 +241,6 @@ bool llvm::isLegalToPromote(Instruction *Inst, Function *F,
242241
return true;
243242
}
244243

245-
bool ICallPromotionFunc::isPromotionLegal(Instruction *Inst, uint64_t Target,
246-
Function *&TargetFunction,
247-
const char **Reason) {
248-
TargetFunction = Symtab->getFunction(Target);
249-
if (TargetFunction == nullptr) {
250-
*Reason = "Cannot find the target";
251-
return false;
252-
}
253-
return isLegalToPromote(Inst, TargetFunction, Reason);
254-
}
255-
256244
// Indirect-call promotion heuristic. The direct targets are sorted based on
257245
// the count. Stop at the first target that is not promoted.
258246
std::vector<ICallPromotionFunc::PromotionCandidate>
@@ -279,28 +267,41 @@ ICallPromotionFunc::getPromotionCandidatesForCallSite(
279267

280268
if (ICPInvokeOnly && dyn_cast<CallInst>(Inst)) {
281269
DEBUG(dbgs() << " Not promote: User options.\n");
270+
ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
271+
<< " Not promote: User options");
282272
break;
283273
}
284274
if (ICPCallOnly && dyn_cast<InvokeInst>(Inst)) {
285275
DEBUG(dbgs() << " Not promote: User option.\n");
276+
ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UserOptions", Inst)
277+
<< " Not promote: User options");
286278
break;
287279
}
288280
if (ICPCutOff != 0 && NumOfPGOICallPromotion >= ICPCutOff) {
289281
DEBUG(dbgs() << " Not promote: Cutoff reached.\n");
282+
ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "CutOffReached", Inst)
283+
<< " Not promote: Cutoff reached");
284+
break;
285+
}
286+
287+
Function *TargetFunction = Symtab->getFunction(Target);
288+
if (TargetFunction == nullptr) {
289+
DEBUG(dbgs() << " Not promote: Cannot find the target\n");
290+
ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToFindTarget", Inst)
291+
<< "Cannot promote indirect call: target not found");
290292
break;
291293
}
292-
Function *TargetFunction = nullptr;
294+
293295
const char *Reason = nullptr;
294-
if (!isPromotionLegal(Inst, Target, TargetFunction, &Reason)) {
295-
StringRef TargetFuncName = Symtab->getFuncName(Target);
296-
DEBUG(dbgs() << " Not promote: " << Reason << "\n");
297-
emitOptimizationRemarkMissed(
298-
F.getContext(), "pgo-icall-prom", F, Inst->getDebugLoc(),
299-
Twine("Cannot promote indirect call to ") +
300-
(TargetFuncName.empty() ? Twine(Target) : Twine(TargetFuncName)) +
301-
Twine(" with count of ") + Twine(Count) + ": " + Reason);
296+
if (!isLegalToPromote(Inst, TargetFunction, &Reason)) {
297+
using namespace ore;
298+
ORE.emit(OptimizationRemarkMissed(DEBUG_TYPE, "UnableToPromote", Inst)
299+
<< "Cannot promote indirect call to "
300+
<< NV("TargetFunction", TargetFunction) << " with count of "
301+
<< NV("Count", Count) << ": " << Reason);
302302
break;
303303
}
304+
304305
Ret.push_back(PromotionCandidate(TargetFunction, Count));
305306
TotalCount -= Count;
306307
}
@@ -532,7 +533,8 @@ static void insertCallRetPHI(Instruction *Inst, Instruction *CallResult,
532533
Instruction *llvm::promoteIndirectCall(Instruction *Inst,
533534
Function *DirectCallee, uint64_t Count,
534535
uint64_t TotalCount,
535-
bool AttachProfToDirectCall) {
536+
bool AttachProfToDirectCall,
537+
OptimizationRemarkEmitter *ORE) {
536538
assert(DirectCallee != nullptr);
537539
BasicBlock *BB = Inst->getParent();
538540
// Just to suppress the non-debug build warning.
@@ -582,10 +584,12 @@ Instruction *llvm::promoteIndirectCall(Instruction *Inst,
582584
DEBUG(dbgs() << "\n== Basic Blocks After ==\n");
583585
DEBUG(dbgs() << *BB << *DirectCallBB << *IndirectCallBB << *MergeBB << "\n");
584586

585-
emitOptimizationRemark(
586-
BB->getContext(), "pgo-icall-prom", *BB->getParent(), Inst->getDebugLoc(),
587-
Twine("Promote indirect call to ") + DirectCallee->getName() +
588-
" with count " + Twine(Count) + " out of " + Twine(TotalCount));
587+
using namespace ore;
588+
if (ORE)
589+
ORE->emit(OptimizationRemark(DEBUG_TYPE, "Promoted", Inst)
590+
<< "Promote indirect call to " << NV("DirectCallee", DirectCallee)
591+
<< " with count " << NV("Count", Count) << " out of "
592+
<< NV("TotalCount", TotalCount));
589593
return NewInst;
590594
}
591595

@@ -597,7 +601,8 @@ uint32_t ICallPromotionFunc::tryToPromote(
597601

598602
for (auto &C : Candidates) {
599603
uint64_t Count = C.Count;
600-
promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO);
604+
promoteIndirectCall(Inst, C.TargetFunction, Count, TotalCount, SamplePGO,
605+
&ORE);
601606
assert(TotalCount >= Count);
602607
TotalCount -= Count;
603608
NumOfPGOICallPromotion++;
@@ -638,7 +643,8 @@ bool ICallPromotionFunc::processFunction() {
638643
}
639644

640645
// A wrapper function that does the actual work.
641-
static bool promoteIndirectCalls(Module &M, bool InLTO, bool SamplePGO) {
646+
static bool promoteIndirectCalls(Module &M, bool InLTO, bool SamplePGO,
647+
ModuleAnalysisManager *AM = nullptr) {
642648
if (DisableICP)
643649
return false;
644650
InstrProfSymtab Symtab;
@@ -654,7 +660,19 @@ static bool promoteIndirectCalls(Module &M, bool InLTO, bool SamplePGO) {
654660
continue;
655661
if (F.hasFnAttribute(Attribute::OptimizeNone))
656662
continue;
657-
ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO);
663+
664+
std::unique_ptr<OptimizationRemarkEmitter> OwnedORE;
665+
OptimizationRemarkEmitter *ORE;
666+
if (AM) {
667+
auto &FAM =
668+
AM->getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
669+
ORE = &FAM.getResult<OptimizationRemarkEmitterAnalysis>(F);
670+
} else {
671+
OwnedORE = make_unique<OptimizationRemarkEmitter>(&F);
672+
ORE = OwnedORE.get();
673+
}
674+
675+
ICallPromotionFunc ICallPromotion(F, &M, &Symtab, SamplePGO, *ORE);
658676
bool FuncChanged = ICallPromotion.processFunction();
659677
if (ICPDUMPAFTER && FuncChanged) {
660678
DEBUG(dbgs() << "\n== IR Dump After =="; F.print(dbgs()));
@@ -677,8 +695,8 @@ bool PGOIndirectCallPromotionLegacyPass::runOnModule(Module &M) {
677695

678696
PreservedAnalyses PGOIndirectCallPromotion::run(Module &M,
679697
ModuleAnalysisManager &AM) {
680-
if (!promoteIndirectCalls(M, InLTO | ICPLTOMode,
681-
SamplePGO | ICPSamplePGOMode))
698+
if (!promoteIndirectCalls(M, InLTO | ICPLTOMode, SamplePGO | ICPSamplePGOMode,
699+
&AM))
682700
return PreservedAnalyses::all();
683701

684702
return PreservedAnalyses::none();

‎llvm/test/Other/new-pm-lto-defaults.ll

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
; RUN: opt -disable-verify -debug-pass-manager \
66
; RUN: -passes='lto<O1>' -S %s 2>&1 \
7-
; RUN: | FileCheck %s --check-prefix=CHECK-O
7+
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O1
88
; RUN: opt -disable-verify -debug-pass-manager \
99
; RUN: -passes='lto<O2>' -S %s 2>&1 \
1010
; RUN: | FileCheck %s --check-prefix=CHECK-O --check-prefix=CHECK-O2
@@ -30,10 +30,12 @@
3030
; CHECK-O-NEXT: Running pass: InferFunctionAttrsPass
3131
; CHECK-O-NEXT: Running analysis: TargetLibraryAnalysis
3232
; CHECK-O2-NEXT: PGOIndirectCallPromotion
33+
; CHECK-O2-NEXT: Running analysis: InnerAnalysisManagerProxy<FunctionAnalysisManager
34+
; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
3335
; CHECK-O2-NEXT: Running pass: IPSCCPPass
3436
; CHECK-O-NEXT: Running pass: ModuleToPostOrderCGSCCPassAdaptor<{{.*}}PostOrderFunctionAttrsPass>
35-
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
36-
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
37+
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy<CGSCCAnalysisManager
38+
; CHECK-O1-NEXT: Running analysis: InnerAnalysisManagerProxy<FunctionAnalysisManager
3739
; CHECK-O-NEXT: Running analysis: LazyCallGraphAnalysis
3840
; CHECK-O-NEXT: Running analysis: FunctionAnalysisManagerCGSCCProxy
3941
; CHECK-O-NEXT: Running analysis: OuterAnalysisManagerProxy<{{.*}}LazyCallGraph{{.*}}>
@@ -52,7 +54,6 @@
5254
; CHECK-O2-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
5355
; CHECK-O2-NEXT: Starting llvm::Function pass manager run.
5456
; CHECK-O2-NEXT: Running pass: InstCombinePass
55-
; CHECK-O2-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
5657
; CHECK-EP-Peephole-NEXT: Running pass: NoOpFunctionPass
5758
; CHECK-O2-NEXT: Finished llvm::Function pass manager run.
5859
; CHECK-O2-NEXT: Running pass: ModuleToPostOrderCGSCCPassAdaptor<{{.*}}InlinerPass>

‎llvm/test/Other/new-pm-thinlto-defaults.ll

+4-2
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@
4646
; CHECK-O-NEXT: Starting llvm::Module pass manager run.
4747
; CHECK-O-NEXT: Running pass: ForceFunctionAttrsPass
4848
; CHECK-POSTLINK-O-NEXT: Running pass: PGOIndirectCallPromotion
49+
; CHECK-POSTLINK-O-NEXT: Running analysis: InnerAnalysisManagerProxy<FunctionAnalysisManager
50+
; CHECK-POSTLINK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
4951
; CHECK-O-NEXT: Running pass: PassManager<{{.*}}Module{{.*}}>
5052
; CHECK-O-NEXT: Starting llvm::Module pass manager run.
5153
; CHECK-O-NEXT: Running pass: InferFunctionAttrsPass
5254
; CHECK-O-NEXT: Running analysis: TargetLibraryAnalysis
5355
; CHECK-O-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
54-
; CHECK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
56+
; CHECK-PRELINK-O-NEXT: Running analysis: InnerAnalysisManagerProxy
5557
; CHECK-O-NEXT: Starting llvm::Function pass manager run.
5658
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
5759
; CHECK-O-NEXT: Running analysis: TargetIRAnalysis
@@ -69,7 +71,7 @@
6971
; CHECK-O-NEXT: Running pass: ModuleToFunctionPassAdaptor<{{.*}}PassManager{{.*}}>
7072
; CHECK-O-NEXT: Starting llvm::Function pass manager run.
7173
; CHECK-O-NEXT: Running pass: InstCombinePass
72-
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
74+
; CHECK-PRELINK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
7375
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
7476
; CHECK-O-NEXT: Finished llvm::Function pass manager run.
7577
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}GlobalsAA

‎llvm/test/Transforms/PGOProfile/icp_mismatch_msg.ll

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: opt < %s -passes=pgo-icall-prom -pass-remarks-missed=pgo-icall-prom -S 2>& 1 | FileCheck %s
33

44
; CHECK: remark: <unknown>:0:0: Cannot promote indirect call to func4 with count of 1234: The number of arguments mismatch
5-
; CHECK: remark: <unknown>:0:0: Cannot promote indirect call to 11517462787082255043 with count of 2345: Cannot find the target
5+
; CHECK: remark: <unknown>:0:0: Cannot promote indirect call: target not found
66
; CHECK: remark: <unknown>:0:0: Cannot promote indirect call to func2 with count of 7890: Return type mismatch
77

88
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"

0 commit comments

Comments
 (0)
Please sign in to comment.