[llvm] [FuncSpec] Update function specialization to handle phi-chains (PR #71442)

Kiran Chandramohan via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 8 02:33:28 PST 2023


================
@@ -262,30 +267,124 @@ Cost InstCostVisitor::estimateBranchInst(BranchInst &I) {
   return estimateBasicBlocks(WorkList);
 }
 
+// This function is determining if a PHINode is part of a strongly
+// connected component - that is a chain or graph of PHINodes that all
+// link to each other. That means, if the original input to the chain
+// is a constant, all the other values are also that constant.
+//
+// For example:
+//
+// L1:
+// %a = load %0
+//
+// L4:
+// %c = phi [%a, %L1], [%d, %L2]
+//
+// L2:
+// %d = phi [%e, L3] [%c, L4]
+// L3:
+// %e = phi [%c, L4],[%f, L5]
+// L5:
+// %f = phi [%j, L6], [%h, L7]
+// L6:
+// %j = phi [%h:L7, %j, L6]
+// L7:
+// %h = phi [%g:L6, %c:L4]
+//
+// This is only showing the labels and PHINodes, not the branches that
+// choose the different paths.
+// If some input is not part of the graph, then it's not a strongly
+// connected component.
+//
+// A depth limit is used to avoid extreme recurusion.
+// A max number of incoming phi values ensures that expensive searches
+// are avoided.
+void InstCostVisitor::discoverStronglyConnectedComponent(PHINode *PN,
+                                                         unsigned Depth) {
+  if (Depth > MaxDiscoveryDepth)
+    return;
+
+  if (PN->getNumIncomingValues() > MaxIncomingPhiValues)
+    return;
----------------
kiranchandramohan wrote:

Should we distinguish these two conditions. As in, if these cases are reached, do we want to bail out completely instead of proceeding with the restricted set of nodes that we have already collected and performing `CanConstantFoldPhi` on them?

https://github.com/llvm/llvm-project/pull/71442


More information about the llvm-commits mailing list