[llvm] 4cd8e9b - [SampleFDO] Stop letting findCalleeFunctionSamples return unrelated profiles

Wei Mi via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 10 12:42:20 PDT 2020


Author: Wei Mi
Date: 2020-08-10T12:41:09-07:00
New Revision: 4cd8e9b169f4dc5dde19807585c86f6d6113d3ff

URL: https://github.com/llvm/llvm-project/commit/4cd8e9b169f4dc5dde19807585c86f6d6113d3ff
DIFF: https://github.com/llvm/llvm-project/commit/4cd8e9b169f4dc5dde19807585c86f6d6113d3ff.diff

LOG: [SampleFDO] Stop letting findCalleeFunctionSamples return unrelated profiles
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

Added: 
    llvm/test/Transforms/SampleProfile/nodebug-error.ll

Modified: 
    llvm/lib/Transforms/IPO/SampleProfile.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp
index 5978f2dc6d1d..95c4f1ca9b7f 100644
--- a/llvm/lib/Transforms/IPO/SampleProfile.cpp
+++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp
@@ -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)

diff  --git a/llvm/test/Transforms/SampleProfile/nodebug-error.ll b/llvm/test/Transforms/SampleProfile/nodebug-error.ll
new file mode 100644
index 000000000000..746846572aee
--- /dev/null
+++ b/llvm/test/Transforms/SampleProfile/nodebug-error.ll
@@ -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
+
+ at .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)


        


More information about the llvm-commits mailing list