[PATCH] D31143: Set the prof weight correctly for call instructions in DeadArgumentElimination.

Dehao Chen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 20 11:08:54 PDT 2017


danielcdh created this revision.

In DeadArgumentElimination, the call instructions will be replaced. We also need to set the prof weights so that function inlining can find the correct profile.


https://reviews.llvm.org/D31143

Files:
  include/llvm/IR/Instruction.h
  lib/IR/Instruction.cpp
  lib/Transforms/IPO/DeadArgumentElimination.cpp
  test/Transforms/DeadArgElim/prof.ll


Index: test/Transforms/DeadArgElim/prof.ll
===================================================================
--- /dev/null
+++ test/Transforms/DeadArgElim/prof.ll
@@ -0,0 +1,18 @@
+; RUN: opt -deadargelim -S < %s | FileCheck %s
+
+; Checks if !prof metadata is reserved in deadargelim.
+
+; Function Attrs: uwtable
+define void @_Z2f2v() #0 {
+; CHECK: call void @_ZL2f1iz(), !prof ![[PROF:[0-9]]]
+  call void (i32, ...) @_ZL2f1iz(i32 1), !prof !0
+  ret void
+}
+
+; Function Attrs: nounwind uwtable
+define internal void @_ZL2f1iz(i32, ...) #1 {
+  ret void
+}
+
+; CHECK:![[PROF]] = !{!"branch_weights", i32 30}
+!0 = !{!"branch_weights", i32 30}
Index: lib/Transforms/IPO/DeadArgumentElimination.cpp
===================================================================
--- lib/Transforms/IPO/DeadArgumentElimination.cpp
+++ lib/Transforms/IPO/DeadArgumentElimination.cpp
@@ -194,6 +194,9 @@
           cast<CallInst>(Call)->getTailCallKind());
     }
     New->setDebugLoc(Call->getDebugLoc());
+    uint64_t W;
+    if (Call->extractProfTotalWeight(W))
+      New->setProfWeight(W);
 
     Args.clear();
 
@@ -900,6 +903,9 @@
           cast<CallInst>(Call)->getTailCallKind());
     }
     New->setDebugLoc(Call->getDebugLoc());
+    uint64_t W;
+    if (Call->extractProfTotalWeight(W))
+      New->setProfWeight(W);
 
     Args.clear();
 
Index: lib/IR/Instruction.cpp
===================================================================
--- lib/IR/Instruction.cpp
+++ lib/IR/Instruction.cpp
@@ -652,3 +652,14 @@
   MDBuilder MDB(getContext());
   setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
 }
+
+void Instruction::setProfWeight(uint64_t W) {
+  assert((isa<CallInst>(this) || isa<InvokeInst>(this)) &&
+         "Can only set weights for call instruciton");
+  if (W == 0)
+    return;
+  SmallVector<uint32_t, 1> Weights;
+  Weights.push_back(W);
+  MDBuilder MDB(getContext());
+  setMetadata(LLVMContext::MD_prof, MDB.createBranchWeights(Weights));
+}
Index: include/llvm/IR/Instruction.h
===================================================================
--- include/llvm/IR/Instruction.h
+++ include/llvm/IR/Instruction.h
@@ -255,6 +255,9 @@
   /// Updates branch_weights metadata by scaling it by \p S / \p T.
   void updateProfWeight(uint64_t S, uint64_t T);
 
+  /// Sets the branch_weights metadata to \p W for CallInst.
+  void setProfWeight(uint64_t W);
+
   /// Set the debug location information for this instruction.
   void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31143.92362.patch
Type: text/x-patch
Size: 2533 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170320/6275f256/attachment-0001.bin>


More information about the llvm-commits mailing list