[PATCH] D137165: [JumpThreading] Copy profile metadata on select unfolding

Roman Paukner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 08:28:06 PDT 2022


rp created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
rp updated this revision to Diff 472309.
rp edited the summary of this revision.
rp added a comment.
rp added a comment.
rp published this revision for review.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

comment text


rp added a comment.

This patch addresses losing of the profile attribute during jump-threading, later causing not ideal block merge due to missing profile in shouldFoldCondBranchesToCommonDestination() where blocks are merged in case branch probability is unknown (and it is unknown as it was not propagated from select to br).


When select is unfolded, copy profile metadata attached to the select to the newly created branch


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137165

Files:
  llvm/lib/Transforms/Scalar/JumpThreading.cpp
  llvm/test/Transforms/JumpThreading/select.ll


Index: llvm/test/Transforms/JumpThreading/select.ll
===================================================================
--- llvm/test/Transforms/JumpThreading/select.ll
+++ llvm/test/Transforms/JumpThreading/select.ll
@@ -274,7 +274,7 @@
 ; CHECK:       cond.false:
 ; CHECK-NEXT:    [[ADD:%.*]] = fadd double [[X]], [[Y]]
 ; CHECK-NEXT:    [[CMP1:%.*]] = fcmp ogt double [[ADD]], 1.000000e+01
-; CHECK-NEXT:    br i1 [[CMP1]], label [[COND_END4]], label [[IF_THEN:%.*]]
+; CHECK-NEXT:    br i1 [[CMP1]], label [[COND_END4]], label [[IF_THEN:%.*]], !prof !0
 ; CHECK:       cond.end4:
 ; CHECK-NEXT:    [[COND5:%.*]] = phi double [ [[SUB]], [[ENTRY:%.*]] ], [ [[ADD]], [[COND_FALSE]] ]
 ; CHECK-NEXT:    [[CMP6:%.*]] = fcmp oeq double [[COND5]], 0.000000e+00
@@ -293,7 +293,7 @@
 cond.false:                                       ; preds = %entry
   %add = fadd double %x, %y
   %cmp1 = fcmp ogt double %add, 1.000000e+01
-  %add. = select i1 %cmp1, double %add, double 0.000000e+00
+  %add. = select i1 %cmp1, double %add, double 0.000000e+00, !prof !0
   br label %cond.end4
 
 cond.end4:                                        ; preds = %entry, %cond.false
@@ -320,7 +320,7 @@
 ; CHECK:       cond.false:
 ; CHECK-NEXT:    [[ADD:%.*]] = add nsw i32 [[X]], [[Y]]
 ; CHECK-NEXT:    [[CMP1:%.*]] = icmp sgt i32 [[ADD]], 10
-; CHECK-NEXT:    br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[COND_END4:%.*]]
+; CHECK-NEXT:    br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[COND_END4:%.*]], !prof !0
 ; CHECK:       cond.end4:
 ; CHECK-NEXT:    [[COND5:%.*]] = phi i32 [ [[ADD]], [[COND_FALSE]] ]
 ; CHECK-NEXT:    [[CMP6:%.*]] = icmp eq i32 [[COND5]], 0
@@ -339,7 +339,7 @@
 cond.false:                                       ; preds = %entry
   %add = add nsw i32 %x, %y
   %cmp1 = icmp sgt i32 %add, 10
-  %add. = select i1 %cmp1, i32 0, i32 %add
+  %add. = select i1 %cmp1, i32 0, i32 %add, !prof !0
   br label %cond.end4
 
 cond.end4:                                        ; preds = %entry, %cond.false
@@ -652,3 +652,5 @@
   %v1 = select i1 %v, i32 %s, i32 42
   ret i32 %v1
 }
+
+!0 = !{!"branch_weights", i32 2146410443, i32 1073205}
\ No newline at end of file
Index: llvm/lib/Transforms/Scalar/JumpThreading.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -2759,6 +2759,7 @@
   // Create a conditional branch and update PHI nodes.
   auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred);
   BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc());
+  BI->copyMetadata(*SI, {LLVMContext::MD_prof});
   SIUse->setIncomingValue(Idx, SI->getFalseValue());
   SIUse->addIncoming(SI->getTrueValue(), NewBB);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137165.472309.patch
Type: text/x-patch
Size: 2761 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221101/5fc03a3c/attachment.bin>


More information about the llvm-commits mailing list