[llvm] [CGData] Global Merge Functions (PR #112671)

Zhaoxuan Jiang via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 17 18:12:22 PST 2024


================
@@ -117,7 +149,38 @@ static void removeIdenticalIndexPair(
       SF->IndexOperandHashMap->erase(Pair);
 }
 
-void StableFunctionMap::finalize() {
+static bool isProfitable(
+    const SmallVector<std::unique_ptr<StableFunctionMap::StableFunctionEntry>>
+        &SFS) {
+  unsigned StableFunctionCount = SFS.size();
+  if (StableFunctionCount < GlobalMergingMinMerges)
+    return false;
+
+  unsigned InstCount = SFS[0]->InstCount;
+  if (InstCount < GlobalMergingMinInstrs)
+    return false;
+
+  unsigned ParamCount = SFS[0]->IndexOperandHashMap->size();
+  if (ParamCount > GlobalMergingMaxParams)
+    return false;
+
+  unsigned Benefit = InstCount * (StableFunctionCount - 1);
+  unsigned Cost =
+      (GlobalMergingParamOverhead * ParamCount + GlobalMergingCallOverhead) *
----------------
nocchijiang wrote:

> I'm curious about what the existing Swift merge can accomplish that this new pass cannot.

These are the behavioral differences I have found by running `merge_func*.ll` from Swift repo, some of which I believe we should consider implementing as well:

|Test|Differences|Remark|
|----|-----------|--------|
|`merge_func_preserves_vfe.ll`|`merge_candidate_c` merged or not|The new pass seems unaware of metadata differences.|
|`merge_func_ptrauth.ll`|presence of ptrauth info|I believe the new pass does not implement ptrauth for simplicity.|
|`merge_func_return_type_cast.ll`|`return_0` merged with `return_null` or not|The 2 functions have different stable hash values.|
|`merge_func.ll`|`func*_merged_with*` merging strategy|Apple implementation tends to avoid too many parameters.|
|`merge_func.ll`|`caller*_*` merging strategy|Apple implementation does not parameterize the calls on the call chain.|
|`merge_func.ll`|`first` merged with `second` or not|Apple implementation is aware of the order of incoming blocks to a phi.|
|`merge_func.ll`|`not_really_recursive` eliminated or not|Apple implementation replaces the call to the original function with the merged one.|

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


More information about the llvm-commits mailing list