mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-27 13:16:08 +00:00

This implements a global function merging pass. Unlike traditional function merging passes that use IR comparators, this pass employs a structurally stable hash to identify similar functions while ignoring certain constant operands. These ignored constants are tracked and encoded into a stable function summary. When merging, instead of explicitly folding similar functions and their call sites, we form a merging instance by supplying different parameters via thunks. The actual size reduction occurs when identically created merging instances are folded by the linker. Currently, this pass is wired to a pre-codegen pass, enabled by the `-enable-global-merge-func` flag. In a local merging mode, the analysis and merging steps occur sequentially within a module: - `analyze`: Collects stable function hashes and tracks locations of ignored constant operands. - `finalize`: Identifies merge candidates with matching hashes and computes the set of parameters that point to different constants. - `merge`: Uses the stable function map to optimistically create a merged function. We can enable a global merging mode similar to the global function outliner (https://discourse.llvm.org/t/rfc-enhanced-machine-outliner-part-2-thinlto-nolto/78753/), which will perform the above steps separately. - `-codegen-data-generate`: During the first round of code generation, we analyze local merging instances and publish their summaries. - Offline using `llvm-cgdata` or at link-time, we can finalize all these merging summaries that are combined to determine parameters. - `-codegen-data-use`: During the second round of code generation, we optimistically create merging instances within each module, and finally, the linker folds identically created merging instances. Depends on #112664 This is a patch for https://discourse.llvm.org/t/rfc-global-function-merging/82608.
80 lines
2.7 KiB
Plaintext
80 lines
2.7 KiB
Plaintext
# REQUIRES: shell, aarch64-registered-target
|
|
# UNSUPPORTED: system-windows
|
|
|
|
# Merge two object files having cgdata (__llvm_merge)
|
|
|
|
RUN: split-file %s %t
|
|
|
|
# Synthesize raw cgdata without the header (32 byte) from the indexed cgdata.
|
|
RUN: llvm-cgdata --convert --format binary %t/raw-1.cgtext -o %t/raw-1.cgdata
|
|
RUN: od -t x1 -j 32 -An %t/raw-1.cgdata | tr -d '\n\r\t' | sed 's/[ ]*$//' | sed 's/[ ][ ]*/\\\\/g' > %t/raw-1-bytes.txt
|
|
RUN: sed "s/<RAW_1_BYTES>/$(cat %t/raw-1-bytes.txt)/g" %t/merge-1-template.ll > %t/merge-1.ll
|
|
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-1.ll -o %t/merge-1.o
|
|
|
|
# Synthesize raw cgdata without the header (32 byte) from the indexed cgdata.
|
|
RUN: llvm-cgdata --convert --format binary %t/raw-2.cgtext -o %t/raw-2.cgdata
|
|
RUN: od -t x1 -j 32 -An %t/raw-2.cgdata | tr -d '\n\r\t' | sed 's/[ ]*$//' | sed 's/[ ][ ]*/\\\\/g' > %t/raw-2-bytes.txt
|
|
RUN: sed "s/<RAW_2_BYTES>/$(cat %t/raw-2-bytes.txt)/g" %t/merge-2-template.ll > %t/merge-2.ll
|
|
RUN: llc -filetype=obj -mtriple arm64-apple-darwin %t/merge-2.ll -o %t/merge-2.o
|
|
|
|
# Merge two object files into the codegen data file.
|
|
RUN: llvm-cgdata --merge --skip-trim %t/merge-1.o %t/merge-2.o -o %t/merge.cgdata
|
|
|
|
RUN: llvm-cgdata --show %t/merge.cgdata | FileCheck %s
|
|
CHECK: Stable function map:
|
|
CHECK-NEXT: Unique hash Count: 1
|
|
CHECK-NEXT: Total function Count: 2
|
|
CHECK-NEXT: Mergeable function Count: 2
|
|
|
|
RUN: llvm-cgdata --convert %t/merge.cgdata | FileCheck %s --check-prefix=MAP
|
|
MAP: # Stable function map
|
|
MAP-NEXT: :stable_function_map
|
|
MAP-NEXT: ---
|
|
MAP-NEXT: - Hash: 1
|
|
MAP-NEXT: FunctionName: Func1
|
|
MAP-NEXT: ModuleName: Mod1
|
|
MAP-NEXT: InstCount: 2
|
|
MAP-NEXT: IndexOperandHashes:
|
|
MAP-NEXT: - InstIndex: 0
|
|
MAP-NEXT: OpndIndex: 1
|
|
MAP-NEXT: OpndHash: 3
|
|
MAP-NEXT: - Hash: 1
|
|
MAP-NEXT: FunctionName: Func2
|
|
MAP-NEXT: ModuleName: Mod1
|
|
MAP-NEXT: InstCount: 2
|
|
MAP-NEXT: IndexOperandHashes:
|
|
MAP-NEXT: - InstIndex: 0
|
|
MAP-NEXT: OpndIndex: 1
|
|
MAP-NEXT: OpndHash: 4
|
|
MAP-NEXT: ...
|
|
|
|
;--- raw-1.cgtext
|
|
:stable_function_map
|
|
- Hash: 1
|
|
FunctionName: Func2
|
|
ModuleName: Mod1
|
|
InstCount: 2
|
|
IndexOperandHashes:
|
|
- InstIndex: 0
|
|
OpndIndex: 1
|
|
OpndHash: 4
|
|
...
|
|
|
|
;--- merge-1-template.ll
|
|
@.data = private unnamed_addr constant [60 x i8] c"<RAW_1_BYTES>", section "__DATA,__llvm_merge"
|
|
|
|
;--- raw-2.cgtext
|
|
:stable_function_map
|
|
- Hash: 1
|
|
FunctionName: Func1
|
|
ModuleName: Mod1
|
|
InstCount: 2
|
|
IndexOperandHashes:
|
|
- InstIndex: 0
|
|
OpndIndex: 1
|
|
OpndHash: 3
|
|
...
|
|
|
|
;--- merge-2-template.ll
|
|
@.data = private unnamed_addr constant [60 x i8] c"<RAW_2_BYTES>", section "__DATA,__llvm_merge"
|