Skip to content

Commit

Permalink
[SampleFDO] Stop letting findCalleeFunctionSamples return unrelated p…
Browse files Browse the repository at this point in the history
…rofiles

for invoke instructions.

We see a warning of "No debug information found in function foo: Function
profile not used" in a case. The function foo is called by an invoke
instruction. It has no debug information because it has attribute((nodebug))
in the definition. It shouldn't have profile instance in the sample profile
but compiler thinks it does, that turns out to be a compiler bug in
findCalleeFunctionSamples. The bug is exposed when sample-profile-merge-inlinee
is enabled recently.

Currently in findCalleeFunctionSamples, CalleeName is unset and is empty for
invoke instruction. For empty CalleeName, findFunctionSamplesAt will treat
the call as an indirect call and will return any inline instance profile at
the same location as the instruction. That leads to a wrong profile being
returned to function foo.

The patch set CalleeName when the instruction is an invoke.

Differential Revision: https://reviews.llvm.org/D85664
  • Loading branch information
wmi-11 committed Aug 10, 2020
1 parent 514445e commit 4cd8e9b
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
5 changes: 2 additions & 3 deletions llvm/lib/Transforms/IPO/SampleProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,8 @@ SampleProfileLoader::findCalleeFunctionSamples(const CallBase &Inst) const {
}

StringRef CalleeName;
if (const CallInst *CI = dyn_cast<CallInst>(&Inst))
if (Function *Callee = CI->getCalledFunction())
CalleeName = Callee->getName();
if (Function *Callee = Inst.getCalledFunction())
CalleeName = Callee->getName();

const FunctionSamples *FS = findFunctionSamples(Inst);
if (FS == nullptr)
Expand Down
60 changes: 60 additions & 0 deletions llvm/test/Transforms/SampleProfile/nodebug-error.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; Test the profile _Z3sumii won't be mistakenly annotated to foo
; when '-sample-profile-merge-inlinee' is enabled. When the mistake
; happens, there will be a "No debug information found" warning message.
; RUN: opt < %s -passes=sample-profile \
; RUN: -sample-profile-file=%S/Inputs/inline-mergeprof.prof \
; RUN: -sample-profile-merge-inlinee=true -S 2>&1| FileCheck %s

@.str = private unnamed_addr constant [11 x i8] c"sum is %d\0A\00", align 1
declare void @__cxa_call_unexpected(i8*)
declare i32 @__gxx_personality_v0(...)
declare i32 @_Z3subii(i32 %x, i32 %y)

define i32 @main() "use-sample-profile" nounwind uwtable ssp personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) !dbg !6 {
entry:
%retval = alloca i32, align 4
%s = alloca i32, align 4
%i = alloca i32, align 4
%tmp = load i32, i32* %i, align 4, !dbg !8
%tmp1 = load i32, i32* %s, align 4, !dbg !8
%call = invoke i32 @foo(i32 %tmp, i32 %tmp1)
to label %cont unwind label %lpad, !dbg !8
; CHECK-NOT: warning: No debug information found in function foo
; CHECK: invoke i32 @foo
cont:
store i32 %call, i32* %s, align 4, !dbg !8
ret i32 0, !dbg !11
lpad:
%lptmp0 = landingpad { i8*, i32 }
filter [0 x i8*] zeroinitializer
%lptmp1 = extractvalue { i8*, i32 } %lptmp0, 0
tail call void @__cxa_call_unexpected(i8* %lptmp1) noreturn nounwind
unreachable
}

define i32 @foo(i32 %x, i32 %y) #0 {
entry:
%add = add nsw i32 %x, %y
ret i32 %add
}

attributes #0 = { "use-sample-profile" }

declare i32 @printf(i8*, ...)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}
!llvm.ident = !{!5}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !1, producer: "clang version 3.5 ", isOptimized: false, runtimeVersion: 0, emissionKind: NoDebug, enums: !2, retainedTypes: !2, globals: !2, imports: !2)
!1 = !DIFile(filename: "calls.cc", directory: ".")
!2 = !{}
!3 = !{i32 2, !"Dwarf Version", i32 4}
!4 = !{i32 1, !"Debug Info Version", i32 3}
!5 = !{!"clang version 3.5 "}
!6 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 7, type: !7, scopeLine: 7, virtualIndex: 6, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0, retainedNodes: !2)
!7 = !DISubroutineType(types: !2)
!8 = !DILocation(line: 10, scope: !9)
!9 = !DILexicalBlockFile(scope: !10, file: !1, discriminator: 2)
!10 = distinct !DILexicalBlock(scope: !6, file: !1, line: 10)
!11 = !DILocation(line: 12, scope: !6)

0 comments on commit 4cd8e9b

Please sign in to comment.