llvm.org GIT mirror llvm / 5edba1d
[PGO] Value profile for size of memory intrinsic calls This patch annotates the valuesites profile to memory intrinsics. Differential Revision: http://reviews.llvm.org/D31002 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298110 91177308-0d34-0410-b5e6-96231b3b80d8 Rong Xu 2 years ago
3 changed file(s) with 108 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
120120 cl::desc("Max number of annotations for a single indirect "
121121 "call callsite"));
122122
123 // Command line option to set the maximum number of value annotations
124 // to write to the metadata for a single memop intrinsic.
125 static cl::opt MaxNumMemOPAnnotations(
126 "memop-max-annotations", cl::init(4), cl::Hidden, cl::ZeroOrMore,
127 cl::desc("Max number of preicise value annotations for a single memop"
128 "intrinsic"));
129
123130 // Command line option to control appending FunctionHash to the name of a COMDAT
124131 // function. This is to avoid the hash mismatch caused by the preinliner.
125132 static cl::opt DoComdatRenaming(
249256 GlobalVariable *FuncNameVar = nullptr;
250257 uint64_t FuncHash = 0;
251258 PGOUseFunc *UseFunc = nullptr;
259 std::vector Candidates;
252260
253261 MemIntrinsicVisitor(Function &Func) : F(Func) {}
254262
257265 Mode = VM_counting;
258266 visit(Func);
259267 }
268
260269 void instrumentMemIntrinsics(Function &Func, unsigned TotalNC,
261270 GlobalVariable *FNV, uint64_t FHash) {
262271 Mode = VM_instrument;
264273 FuncHash = FHash;
265274 FuncNameVar = FNV;
266275 visit(Func);
276 }
277
278 std::vector findMemIntrinsics(Function &Func) {
279 Candidates.clear();
280 Mode = VM_annotate;
281 visit(Func);
282 return Candidates;
267283 }
268284
269285 // Visit the IR stream and annotate all mem intrinsic call instructions.
431447 NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
432448 NumOfPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics();
433449 ValueSites[IPVK_IndirectCallTarget] = findIndirectCallSites(Func);
450 ValueSites[IPVK_MemOPSize] = MIVisitor.findMemIntrinsics(Func);
434451
435452 FuncName = getPGOFuncName(F);
436453 computeCFGHash();
11521169 instrumentOneMemIntrinsic(MI);
11531170 return;
11541171 case VM_annotate:
1155 break;
1172 Candidates.push_back(&MI);
1173 return;
11561174 }
11571175 llvm_unreachable("Unknown visiting mode");
11581176 }
11661184 createPGOFuncNameMetadata(F, FuncInfo.FuncName);
11671185
11681186 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
1169 // TBD: Only handle IPVK_IndirectCallTarget for now.
1170 if (Kind == IPVK_IndirectCallTarget)
1171 annotateValueSites(Kind);
1187 annotateValueSites(Kind);
11721188 }
11731189
11741190 // Annotate the instructions for a specific value kind.
11921208 << NumValueSites << "\n");
11931209 annotateValueSite(*M, *I, ProfileRecord,
11941210 static_cast(Kind), ValueSiteIndex,
1195 MaxNumAnnotations);
1211 Kind == IPVK_MemOPSize ? MaxNumMemOPAnnotations
1212 : MaxNumAnnotations);
11961213 ValueSiteIndex++;
11971214 }
11981215 }
0 # IR level Instrumentation Flag
1 :ir
2 foo
3 # Func Hash:
4 53929068288
5 # Num Counters:
6 3
7 # Counter Values:
8 556
9 20
10 1
11 # Num Value Kinds:
12 1
13 # ValueKind = IPVK_MemOPSize:
14 1
15 # NumValueSites:
16 1
17 9
18 7:33
19 2:88
20 9:72
21 4:66
22 1:99
23 5:55
24 6:44
25 3:77
26 8:22
0 ; RUN: llvm-profdata merge %S/Inputs/memop_size_annotation.proftext -o %t.profdata
1 ; RUN: opt < %s -pgo-instr-use -memop-max-annotations=9 -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION9
2 ; RUN: opt < %s -passes=pgo-instr-use -memop-max-annotations=9 -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION9
3 ; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION4
4 ; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION4
5
6 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
7 target triple = "x86_64-unknown-linux-gnu"
8
9 define void @foo(i8* %dst, i8* %src, i32* %a, i32 %n) {
10 entry:
11 br label %for.cond
12
13 for.cond:
14 %i.0 = phi i32 [ 0, %entry ], [ %inc5, %for.inc4 ]
15 %cmp = icmp slt i32 %i.0, %n
16 br i1 %cmp, label %for.body, label %for.end6
17
18 for.body:
19 br label %for.cond1
20
21 for.cond1:
22 %j.0 = phi i32 [ 0, %for.body ], [ %inc, %for.inc ]
23 %idx.ext = sext i32 %i.0 to i64
24 %add.ptr = getelementptr inbounds i32, i32* %a, i64 %idx.ext
25 %0 = load i32, i32* %add.ptr, align 4
26 %cmp2 = icmp slt i32 %j.0, %0
27 br i1 %cmp2, label %for.body3, label %for.end
28
29 for.body3:
30 %add = add nsw i32 %i.0, 1
31 %conv = sext i32 %add to i64
32 call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %conv, i32 1, i1 false)
33 ; MEMOP_ANNOTATION: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %conv, i32 1, i1 false)
34 ; MEMOP_ANNOTATION-SAME: !prof ![[MEMOP_VALUESITE:[0-9]+]]
35 ; MEMOP_ANNOTATION9: ![[MEMOP_VALUESITE]] = !{!"VP", i32 1, i64 556, i64 1, i64 99, i64 2, i64 88, i64 3, i64 77, i64 9, i64 72, i64 4, i64 66, i64 5, i64 55, i64 6, i64 44, i64 7, i64 33, i64 8, i64 22}
36 ; MEMOP_ANNOTATION4: ![[MEMOP_VALUESITE]] = !{!"VP", i32 1, i64 556, i64 1, i64 99, i64 2, i64 88, i64 3, i64 77, i64 9, i64 72}
37 br label %for.inc
38
39 for.inc:
40 %inc = add nsw i32 %j.0, 1
41 br label %for.cond1
42
43 for.end:
44 br label %for.inc4
45
46 for.inc4:
47 %inc5 = add nsw i32 %i.0, 1
48 br label %for.cond
49
50 for.end6:
51 ret void
52 }
53
54 declare void @llvm.lifetime.start(i64, i8* nocapture)
55
56 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i32, i1)
57
58 declare void @llvm.lifetime.end(i64, i8* nocapture)