Skip to content

Commit ab27253

Browse files
authored
[CGData][lld-macho] Merge CG Data by LLD (#112674)
LLD now processes raw CG data for stable functions, similar to how it handles raw CG data for the outliner's hash tree. This data is encoded in the custom section (`__llvm_merge`) within object files. LLD merges this information into the indexed CG data file specified by the `-codegen-data-generate-path={path}` option. For the linker that does not support this feature, we could use `llvm-cgdata` tool -- https://github.com/llvm/llvm-project/blob/main/llvm/docs/CommandGuide/llvm-cgdata.rst. Depends on #115750. This is a patch for https://discourse.llvm.org/t/rfc-global-function-merging/82608.
1 parent 6a0905d commit ab27253

File tree

3 files changed

+107
-3
lines changed

3 files changed

+107
-3
lines changed

lld/MachO/Driver.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,21 +1326,39 @@ static void codegenDataGenerate() {
13261326
TimeTraceScope timeScope("Generating codegen data");
13271327

13281328
OutlinedHashTreeRecord globalOutlineRecord;
1329-
for (ConcatInputSection *isec : inputSections)
1330-
if (isec->getSegName() == segment_names::data &&
1331-
isec->getName() == section_names::outlinedHashTree) {
1329+
StableFunctionMapRecord globalMergeRecord;
1330+
for (ConcatInputSection *isec : inputSections) {
1331+
if (isec->getSegName() != segment_names::data)
1332+
continue;
1333+
if (isec->getName() == section_names::outlinedHashTree) {
13321334
// Read outlined hash tree from each section.
13331335
OutlinedHashTreeRecord localOutlineRecord;
1336+
// Use a pointer to allow modification by the function.
13341337
auto *data = isec->data.data();
13351338
localOutlineRecord.deserialize(data);
13361339

13371340
// Merge it to the global hash tree.
13381341
globalOutlineRecord.merge(localOutlineRecord);
13391342
}
1343+
if (isec->getName() == section_names::functionMap) {
1344+
// Read stable functions from each section.
1345+
StableFunctionMapRecord localMergeRecord;
1346+
// Use a pointer to allow modification by the function.
1347+
auto *data = isec->data.data();
1348+
localMergeRecord.deserialize(data);
1349+
1350+
// Merge it to the global function map.
1351+
globalMergeRecord.merge(localMergeRecord);
1352+
}
1353+
}
1354+
1355+
globalMergeRecord.finalize();
13401356

13411357
CodeGenDataWriter Writer;
13421358
if (!globalOutlineRecord.empty())
13431359
Writer.addRecord(globalOutlineRecord);
1360+
if (!globalMergeRecord.empty())
1361+
Writer.addRecord(globalMergeRecord);
13441362

13451363
std::error_code EC;
13461364
auto fileName = config->codegenDataGeneratePath;

lld/MachO/InputSection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ constexpr const char const_[] = "__const";
339339
constexpr const char lazySymbolPtr[] = "__la_symbol_ptr";
340340
constexpr const char lazyBinding[] = "__lazy_binding";
341341
constexpr const char literals[] = "__literals";
342+
constexpr const char functionMap[] = "__llvm_merge";
342343
constexpr const char moduleInitFunc[] = "__mod_init_func";
343344
constexpr const char moduleTermFunc[] = "__mod_term_func";
344345
constexpr const char nonLazySymbolPtr[] = "__nl_symbol_ptr";
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# UNSUPPORTED: system-windows
2+
# REQUIRES: aarch64
3+
4+
# RUN: rm -rf %t; split-file %s %t
5+
6+
# Synthesize raw cgdata without the header (32 byte) from the indexed cgdata.
7+
# RUN: llvm-cgdata --convert --format binary %t/raw-1.cgtext -o %t/raw-1.cgdata
8+
# RUN: od -t x1 -j 32 -An %t/raw-1.cgdata | tr -d '\n\r\t' | sed 's/[ ][ ]*/ /g; s/^[ ]*//; s/[ ]*$//; s/[ ]/,0x/g; s/^/0x/' > %t/raw-1-bytes.txt
9+
# RUN: sed "s/<RAW_BYTES>/$(cat %t/raw-1-bytes.txt)/g" %t/merge-template.s > %t/merge-1.s
10+
# RUN: llvm-cgdata --convert --format binary %t/raw-2.cgtext -o %t/raw-2.cgdata
11+
# RUN: od -t x1 -j 32 -An %t/raw-2.cgdata | tr -d '\n\r\t' | sed 's/[ ][ ]*/ /g; s/^[ ]*//; s/[ ]*$//; s/[ ]/,0x/g; s/^/0x/' > %t/raw-2-bytes.txt
12+
# RUN: sed "s/<RAW_BYTES>/$(cat %t/raw-2-bytes.txt)/g" %t/merge-template.s > %t/merge-2.s
13+
14+
# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/merge-1.s -o %t/merge-1.o
15+
# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/merge-2.s -o %t/merge-2.o
16+
# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/main.s -o %t/main.o
17+
18+
# This checks if the codegen data from the linker is identical to the merged codegen data
19+
# from each object file, which is obtained using the llvm-cgdata tool.
20+
# RUN: %no-arg-lld -dylib -arch arm64 -platform_version ios 14.0 15.0 -o %t/out \
21+
# RUN: %t/merge-1.o %t/merge-2.o %t/main.o --codegen-data-generate-path=%t/out-cgdata
22+
# RUN: llvm-cgdata --merge %t/merge-1.o %t/merge-2.o %t/main.o -o %t/merge-cgdata
23+
# RUN: diff %t/out-cgdata %t/merge-cgdata
24+
25+
# Merge order doesn't matter in the yaml format. `main.o` is dropped due to missing __llvm_merge.
26+
# RUN: llvm-cgdata --merge %t/merge-2.o %t/merge-1.o -o %t/merge-cgdata-shuffle
27+
# RUN: llvm-cgdata --convert %t/out-cgdata -o %t/out-cgdata.yaml
28+
# RUN: llvm-cgdata --convert %t/merge-cgdata-shuffle -o %t/merge-cgdata-shuffle.yaml
29+
# RUN: diff %t/out-cgdata.yaml %t/merge-cgdata-shuffle.yaml
30+
31+
# We can also generate the merged codegen data from the executable that is not dead-stripped.
32+
# RUN: llvm-objdump -h %t/out| FileCheck %s
33+
# CHECK: __llvm_merge
34+
# RUN: llvm-cgdata --merge %t/out -o %t/merge-cgdata-exe
35+
# RUN: diff %t/merge-cgdata-exe %t/merge-cgdata
36+
37+
# Dead-strip will remove __llvm_merge sections from the final executable.
38+
# But the codeden data is still correctly produced from the linker.
39+
# RUN: %no-arg-lld -dylib -arch arm64 -platform_version ios 14.0 15.0 -o %t/out-strip \
40+
# RUN: %t/merge-1.o %t/merge-2.o %t/main.o -dead_strip --codegen-data-generate-path=%t/out-cgdata-strip
41+
# RUN: llvm-cgdata --merge %t/merge-1.o %t/merge-2.o %t/main.o -o %t/merge-cgdata-strip
42+
# RUN: diff %t/out-cgdata-strip %t/merge-cgdata-strip
43+
# RUN: diff %t/out-cgdata-strip %t/merge-cgdata
44+
45+
# Ensure no __llvm_merge section remains in the executable.
46+
# RUN: llvm-objdump -h %t/out-strip | FileCheck %s --check-prefix=STRIP
47+
# STRIP-NOT: __llvm_merge
48+
49+
#--- raw-1.cgtext
50+
:stable_function_map
51+
---
52+
- Hash: 123
53+
FunctionName: f1
54+
ModuleName: 'foo.bc'
55+
InstCount: 7
56+
IndexOperandHashes:
57+
- InstIndex: 3
58+
OpndIndex: 0
59+
OpndHash: 456
60+
...
61+
62+
#--- raw-2.cgtext
63+
:stable_function_map
64+
---
65+
- Hash: 123
66+
FunctionName: f2
67+
ModuleName: 'goo.bc'
68+
InstCount: 7
69+
IndexOperandHashes:
70+
- InstIndex: 3
71+
OpndIndex: 0
72+
OpndHash: 789
73+
...
74+
75+
#--- merge-template.s
76+
.section __DATA,__llvm_merge
77+
_data:
78+
.byte <RAW_BYTES>
79+
80+
#--- main.s
81+
.globl _main
82+
83+
.text
84+
_main:
85+
ret

0 commit comments

Comments
 (0)