[llvm] 6429471 - [IR] Convert profile metadata in createCallMatchingInvoke()

Yevgeny Rouban via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 22:14:44 PDT 2020


Author: Yevgeny Rouban
Date: 2020-06-20T12:10:31+07:00
New Revision: 6429471e8b766b841644f224b5c6525a9b3227c9

URL: https://github.com/llvm/llvm-project/commit/6429471e8b766b841644f224b5c6525a9b3227c9
DIFF: https://github.com/llvm/llvm-project/commit/6429471e8b766b841644f224b5c6525a9b3227c9.diff

LOG: [IR] Convert profile metadata in createCallMatchingInvoke()

When an invoke instruction is converted to a call its
profile metadata is dropped because it has incompatible
format (see commit 16ad6eeb94ff).
This patch adds an attempt to convert profile data to
format of the call instruction. This used to work well
before the commit dcfa78a4ccec.

Reviewers: reames
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D82071

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/Local.cpp
    llvm/test/Transforms/SimplifyCFG/invoke_unwind.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index d0f0c8f1565c..da40c342af3a 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1986,9 +1986,16 @@ CallInst *llvm::createCallMatchingInvoke(InvokeInst *II) {
   NewCall->setDebugLoc(II->getDebugLoc());
   NewCall->copyMetadata(*II);
 
-  // If the invoke had profile metadata, drop it.
-  if (NewCall->hasMetadata(LLVMContext::MD_prof))
-    NewCall->setMetadata(LLVMContext::MD_prof, nullptr);
+  // If the invoke had profile metadata, try converting them for CallInst.
+  uint64_t TotalWeight;
+  if (NewCall->extractProfTotalWeight(TotalWeight)) {
+    // Set the total weight if it fits into i32, otherwise reset.
+    MDBuilder MDB(NewCall->getContext());
+    auto NewWeights = uint32_t(TotalWeight) != TotalWeight
+                          ? nullptr
+                          : MDB.createBranchWeights({uint32_t(TotalWeight)});
+    NewCall->setMetadata(LLVMContext::MD_prof, NewWeights);
+  }
 
   return NewCall;
 }

diff  --git a/llvm/test/Transforms/SimplifyCFG/invoke_unwind.ll b/llvm/test/Transforms/SimplifyCFG/invoke_unwind.ll
index 5758b67a55b5..8559cda24358 100644
--- a/llvm/test/Transforms/SimplifyCFG/invoke_unwind.ll
+++ b/llvm/test/Transforms/SimplifyCFG/invoke_unwind.ll
@@ -6,8 +6,7 @@ declare void @bar()
 ; instructions to call instructions if the handler just rethrows the exception.
 define i32 @test1() personality i32 (...)* @__gxx_personality_v0 {
 ; CHECK-LABEL: @test1(
-; CHECK-NEXT: call void @bar()
-; CHECK-NOT: !prof
+; CHECK-NEXT: call void @bar(), !prof ![[PROF:[0-9]+]]
 ; CHECK-NEXT: ret i32 0
         invoke void @bar( )
                         to label %1 unwind label %Rethrow, !prof !0
@@ -18,7 +17,7 @@ Rethrow:
         resume { i8*, i32 } %exn
 }
 
-!0 = !{!"branch_weights", i32 369, i32 0}
+!0 = !{!"branch_weights", i32 369, i32 2}
 
 define i32 @test2() personality i32 (...)* @__gxx_personality_v0 {
 ; CHECK-LABEL: @test2(
@@ -76,3 +75,5 @@ lpad2:
 }
 
 declare i32 @__gxx_personality_v0(...)
+
+; CHECK: ![[PROF]] = !{!"branch_weights", i32 371}


        


More information about the llvm-commits mailing list