[llvm] 9641b9b - [Inliner] Preserve !prof metadata when converting call to invoke.

Hongtao Yu via llvm-commits llvm-commits at lists.llvm.org
Mon May 9 15:08:19 PDT 2022


Author: Hongtao Yu
Date: 2022-05-09T15:08:09-07:00
New Revision: 9641b9be9dfc599cbb6a812c1e587eff2ddd8707

URL: https://github.com/llvm/llvm-project/commit/9641b9be9dfc599cbb6a812c1e587eff2ddd8707
DIFF: https://github.com/llvm/llvm-project/commit/9641b9be9dfc599cbb6a812c1e587eff2ddd8707.diff

LOG: [Inliner] Preserve !prof metadata when converting call to invoke.

When a callee function is inlined via an invoke instruction, every function call inside the callee, if not an invoke,  will be converted to an invoke after cloned to the caller body. I found that during the conversion the !prof metadata was dropped. This in turned caused a cloned indirect call not properly promoted in subsequent passes.

The particular scenario I was investigating was with AutoFDO and thinLTO. In prelink, no ICP was triggered (neither by the sample loader nor PGO ICP), no indirect call was promoted. This is because 1) the particular indirect call did not have inlined samples;  and 2) PGO ICP was intentionally disabled.  After inlining, the prof metadata was dropped. Then in postlink, PGO ICP jumped in but didn't do anything. Thus the opportunity was missed.

I'm making a simple fix to preserve !prof metadata when converting call to invoke.

Reviewed By: davidxl

Differential Revision: https://reviews.llvm.org/D125249

Added: 
    llvm/test/Transforms/Inline/profile_meta_invoke.ll

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index e72e3cee1d4b..5e37f387c19b 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2233,6 +2233,7 @@ BasicBlock *llvm::changeToInvokeAndSplitBasicBlock(CallInst *CI,
   II->setDebugLoc(CI->getDebugLoc());
   II->setCallingConv(CI->getCallingConv());
   II->setAttributes(CI->getAttributes());
+  II->setMetadata(LLVMContext::MD_prof, CI->getMetadata(LLVMContext::MD_prof));
 
   if (DTU)
     DTU->applyUpdates({{DominatorTree::Insert, BB, UnwindEdge}});

diff  --git a/llvm/test/Transforms/Inline/profile_meta_invoke.ll b/llvm/test/Transforms/Inline/profile_meta_invoke.ll
new file mode 100644
index 000000000000..b89868b94561
--- /dev/null
+++ b/llvm/test/Transforms/Inline/profile_meta_invoke.ll
@@ -0,0 +1,46 @@
+; Make sure that profile metadata is preserved when cloning a call.
+; RUN: opt < %s -passes='require<profile-summary>,cgscc(inline)' -S | FileCheck %s
+
+declare i32 @__gxx_personality_v0(...)
+
+define void @callee(void ()* %func) !prof !15 {
+  call void %func(), !prof !16
+  ret void
+}
+
+define void @caller(void ()* %func) personality i32 (...)* @__gxx_personality_v0 {
+  invoke void @callee(void ()* %func)
+          to label %ret unwind label %lpad, !prof !17
+
+ret:
+  ret void
+
+lpad:
+  %exn = landingpad {i8*, i32}
+          cleanup
+  unreachable
+}
+
+!llvm.module.flags = !{!1}
+!1 = !{i32 1, !"ProfileSummary", !2}
+!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
+!3 = !{!"ProfileFormat", !"SampleProfile"}
+!4 = !{!"TotalCount", i64 10000}
+!5 = !{!"MaxCount", i64 10}
+!6 = !{!"MaxInternalCount", i64 1}
+!7 = !{!"MaxFunctionCount", i64 2000}
+!8 = !{!"NumCounts", i64 2}
+!9 = !{!"NumFunctions", i64 2}
+!10 = !{!"DetailedSummary", !11}
+!11 = !{!12, !13, !14}
+!12 = !{i32 10000, i64 100, i32 1}
+!13 = !{i32 999000, i64 100, i32 1}
+!14 = !{i32 999999, i64 1, i32 2}
+!15 = !{!"function_entry_count", i64 1000}
+!16 = !{!"VP", i32 0, i64 1000, i64 9191153033785521275, i64 400, i64 -1069303473483922844, i64 600}
+!17 = !{!"branch_weights", i32 500}
+
+; CHECK-LABEL: @caller(
+; CHECK:  invoke void %func()
+; CHECK-NEXT: {{.*}} !prof ![[PROF:[0-9]+]]
+; CHECK: ![[PROF]] = !{!"VP", i32 0, i64 500, i64 9191153033785521275, i64 200, i64 -1069303473483922844, i64 300}


        


More information about the llvm-commits mailing list