Skip to content

Commit

Permalink
[PGO] Value profile for size of memory intrinsic calls
Browse files Browse the repository at this point in the history
This patch annotates the valuesites profile to memory intrinsics.

Differential Revision: http://reviews.llvm.org/D31002

llvm-svn: 298110
  • Loading branch information
xur-llvm committed Mar 17, 2017
1 parent c2a250c commit e60343d
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
27 changes: 22 additions & 5 deletions llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Expand Up @@ -121,6 +121,13 @@ static cl::opt<unsigned> MaxNumAnnotations(
cl::desc("Max number of annotations for a single indirect "
"call callsite"));

// Command line option to set the maximum number of value annotations
// to write to the metadata for a single memop intrinsic.
static cl::opt<unsigned> MaxNumMemOPAnnotations(
"memop-max-annotations", cl::init(4), cl::Hidden, cl::ZeroOrMore,
cl::desc("Max number of preicise value annotations for a single memop"
"intrinsic"));

// Command line option to control appending FunctionHash to the name of a COMDAT
// function. This is to avoid the hash mismatch caused by the preinliner.
static cl::opt<bool> DoComdatRenaming(
Expand Down Expand Up @@ -250,6 +257,7 @@ struct MemIntrinsicVisitor : public InstVisitor<MemIntrinsicVisitor> {
GlobalVariable *FuncNameVar = nullptr;
uint64_t FuncHash = 0;
PGOUseFunc *UseFunc = nullptr;
std::vector<Instruction *> Candidates;

MemIntrinsicVisitor(Function &Func) : F(Func) {}

Expand All @@ -258,6 +266,7 @@ struct MemIntrinsicVisitor : public InstVisitor<MemIntrinsicVisitor> {
Mode = VM_counting;
visit(Func);
}

void instrumentMemIntrinsics(Function &Func, unsigned TotalNC,
GlobalVariable *FNV, uint64_t FHash) {
Mode = VM_instrument;
Expand All @@ -267,6 +276,13 @@ struct MemIntrinsicVisitor : public InstVisitor<MemIntrinsicVisitor> {
visit(Func);
}

std::vector<Instruction *> findMemIntrinsics(Function &Func) {
Candidates.clear();
Mode = VM_annotate;
visit(Func);
return Candidates;
}

// Visit the IR stream and annotate all mem intrinsic call instructions.
void instrumentOneMemIntrinsic(MemIntrinsic &MI);
// Visit \p MI instruction and perform tasks according to visit mode.
Expand Down Expand Up @@ -432,6 +448,7 @@ template <class Edge, class BBInfo> class FuncPGOInstrumentation {
NumOfPGOSelectInsts += SIVisitor.getNumOfSelectInsts();
NumOfPGOMemIntrinsics += MIVisitor.getNumOfMemIntrinsics();
ValueSites[IPVK_IndirectCallTarget] = findIndirectCallSites(Func);
ValueSites[IPVK_MemOPSize] = MIVisitor.findMemIntrinsics(Func);

FuncName = getPGOFuncName(F);
computeCFGHash();
Expand Down Expand Up @@ -1153,7 +1170,8 @@ void MemIntrinsicVisitor::visitMemIntrinsic(MemIntrinsic &MI) {
instrumentOneMemIntrinsic(MI);
return;
case VM_annotate:
break;
Candidates.push_back(&MI);
return;
}
llvm_unreachable("Unknown visiting mode");
}
Expand All @@ -1167,9 +1185,7 @@ void PGOUseFunc::annotateValueSites() {
createPGOFuncNameMetadata(F, FuncInfo.FuncName);

for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
// TBD: Only handle IPVK_IndirectCallTarget for now.
if (Kind == IPVK_IndirectCallTarget)
annotateValueSites(Kind);
annotateValueSites(Kind);
}

// Annotate the instructions for a specific value kind.
Expand All @@ -1193,7 +1209,8 @@ void PGOUseFunc::annotateValueSites(uint32_t Kind) {
<< NumValueSites << "\n");
annotateValueSite(*M, *I, ProfileRecord,
static_cast<InstrProfValueKind>(Kind), ValueSiteIndex,
MaxNumAnnotations);
Kind == IPVK_MemOPSize ? MaxNumMemOPAnnotations
: MaxNumAnnotations);
ValueSiteIndex++;
}
}
Expand Down
@@ -0,0 +1,27 @@
# IR level Instrumentation Flag
:ir
foo
# Func Hash:
53929068288
# Num Counters:
3
# Counter Values:
556
20
1
# Num Value Kinds:
1
# ValueKind = IPVK_MemOPSize:
1
# NumValueSites:
1
9
7:33
2:88
9:72
4:66
1:99
5:55
6:44
3:77
8:22
59 changes: 59 additions & 0 deletions llvm/test/Transforms/PGOProfile/memop_size_annotation.ll
@@ -0,0 +1,59 @@
; RUN: llvm-profdata merge %S/Inputs/memop_size_annotation.proftext -o %t.profdata
; 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
; 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
; RUN: opt < %s -pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION4
; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s --check-prefixes=MEMOP_ANNOTATION,MEMOP_ANNOTATION4

target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @foo(i8* %dst, i8* %src, i32* %a, i32 %n) {
entry:
br label %for.cond

for.cond:
%i.0 = phi i32 [ 0, %entry ], [ %inc5, %for.inc4 ]
%cmp = icmp slt i32 %i.0, %n
br i1 %cmp, label %for.body, label %for.end6

for.body:
br label %for.cond1

for.cond1:
%j.0 = phi i32 [ 0, %for.body ], [ %inc, %for.inc ]
%idx.ext = sext i32 %i.0 to i64
%add.ptr = getelementptr inbounds i32, i32* %a, i64 %idx.ext
%0 = load i32, i32* %add.ptr, align 4
%cmp2 = icmp slt i32 %j.0, %0
br i1 %cmp2, label %for.body3, label %for.end

for.body3:
%add = add nsw i32 %i.0, 1
%conv = sext i32 %add to i64
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %conv, i32 1, i1 false)
; MEMOP_ANNOTATION: call void @llvm.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %conv, i32 1, i1 false)
; MEMOP_ANNOTATION-SAME: !prof ![[MEMOP_VALUESITE:[0-9]+]]
; 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}
; 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}
br label %for.inc

for.inc:
%inc = add nsw i32 %j.0, 1
br label %for.cond1

for.end:
br label %for.inc4

for.inc4:
%inc5 = add nsw i32 %i.0, 1
br label %for.cond

for.end6:
ret void
}

declare void @llvm.lifetime.start(i64, i8* nocapture)

declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i32, i1)

declare void @llvm.lifetime.end(i64, i8* nocapture)

0 comments on commit e60343d

Please sign in to comment.