xref: /llvm-project/lld/test/MachO/cgdata-generate-merge.s (revision ab27253ad395881c0798ac5c8efc2f6fc2922399)
1*ab27253aSKyungwoo Lee# UNSUPPORTED: system-windows
2*ab27253aSKyungwoo Lee# REQUIRES: aarch64
3*ab27253aSKyungwoo Lee
4*ab27253aSKyungwoo Lee# RUN: rm -rf %t; split-file %s %t
5*ab27253aSKyungwoo Lee
6*ab27253aSKyungwoo Lee# Synthesize raw cgdata without the header (32 byte) from the indexed cgdata.
7*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --convert --format binary %t/raw-1.cgtext -o %t/raw-1.cgdata
8*ab27253aSKyungwoo Lee# 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*ab27253aSKyungwoo Lee# RUN: sed "s/<RAW_BYTES>/$(cat %t/raw-1-bytes.txt)/g" %t/merge-template.s > %t/merge-1.s
10*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --convert --format binary %t/raw-2.cgtext -o %t/raw-2.cgdata
11*ab27253aSKyungwoo Lee# 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*ab27253aSKyungwoo Lee# RUN: sed "s/<RAW_BYTES>/$(cat %t/raw-2-bytes.txt)/g" %t/merge-template.s > %t/merge-2.s
13*ab27253aSKyungwoo Lee
14*ab27253aSKyungwoo Lee# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/merge-1.s -o %t/merge-1.o
15*ab27253aSKyungwoo Lee# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/merge-2.s -o %t/merge-2.o
16*ab27253aSKyungwoo Lee# RUN: llvm-mc -filetype obj -triple arm64-apple-darwin %t/main.s -o %t/main.o
17*ab27253aSKyungwoo Lee
18*ab27253aSKyungwoo Lee# This checks if the codegen data from the linker is identical to the merged codegen data
19*ab27253aSKyungwoo Lee# from each object file, which is obtained using the llvm-cgdata tool.
20*ab27253aSKyungwoo Lee# RUN: %no-arg-lld -dylib -arch arm64 -platform_version ios 14.0 15.0 -o %t/out \
21*ab27253aSKyungwoo Lee# RUN: %t/merge-1.o %t/merge-2.o %t/main.o --codegen-data-generate-path=%t/out-cgdata
22*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --merge %t/merge-1.o %t/merge-2.o %t/main.o -o %t/merge-cgdata
23*ab27253aSKyungwoo Lee# RUN: diff %t/out-cgdata %t/merge-cgdata
24*ab27253aSKyungwoo Lee
25*ab27253aSKyungwoo Lee# Merge order doesn't matter in the yaml format. `main.o` is dropped due to missing __llvm_merge.
26*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --merge %t/merge-2.o %t/merge-1.o -o %t/merge-cgdata-shuffle
27*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --convert %t/out-cgdata -o %t/out-cgdata.yaml
28*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --convert %t/merge-cgdata-shuffle -o %t/merge-cgdata-shuffle.yaml
29*ab27253aSKyungwoo Lee# RUN: diff %t/out-cgdata.yaml %t/merge-cgdata-shuffle.yaml
30*ab27253aSKyungwoo Lee
31*ab27253aSKyungwoo Lee# We can also generate the merged codegen data from the executable that is not dead-stripped.
32*ab27253aSKyungwoo Lee# RUN: llvm-objdump -h %t/out| FileCheck %s
33*ab27253aSKyungwoo Lee# CHECK: __llvm_merge
34*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --merge %t/out -o %t/merge-cgdata-exe
35*ab27253aSKyungwoo Lee# RUN: diff %t/merge-cgdata-exe %t/merge-cgdata
36*ab27253aSKyungwoo Lee
37*ab27253aSKyungwoo Lee# Dead-strip will remove __llvm_merge sections from the final executable.
38*ab27253aSKyungwoo Lee# But the codeden data is still correctly produced from the linker.
39*ab27253aSKyungwoo Lee# RUN: %no-arg-lld -dylib -arch arm64 -platform_version ios 14.0 15.0 -o %t/out-strip \
40*ab27253aSKyungwoo Lee# RUN: %t/merge-1.o %t/merge-2.o %t/main.o -dead_strip --codegen-data-generate-path=%t/out-cgdata-strip
41*ab27253aSKyungwoo Lee# RUN: llvm-cgdata --merge %t/merge-1.o %t/merge-2.o %t/main.o -o %t/merge-cgdata-strip
42*ab27253aSKyungwoo Lee# RUN: diff %t/out-cgdata-strip %t/merge-cgdata-strip
43*ab27253aSKyungwoo Lee# RUN: diff %t/out-cgdata-strip %t/merge-cgdata
44*ab27253aSKyungwoo Lee
45*ab27253aSKyungwoo Lee# Ensure no __llvm_merge section remains in the executable.
46*ab27253aSKyungwoo Lee# RUN: llvm-objdump -h %t/out-strip | FileCheck %s --check-prefix=STRIP
47*ab27253aSKyungwoo Lee# STRIP-NOT: __llvm_merge
48*ab27253aSKyungwoo Lee
49*ab27253aSKyungwoo Lee#--- raw-1.cgtext
50*ab27253aSKyungwoo Lee:stable_function_map
51*ab27253aSKyungwoo Lee---
52*ab27253aSKyungwoo Lee- Hash:            123
53*ab27253aSKyungwoo Lee  FunctionName:    f1
54*ab27253aSKyungwoo Lee  ModuleName:      'foo.bc'
55*ab27253aSKyungwoo Lee  InstCount:       7
56*ab27253aSKyungwoo Lee  IndexOperandHashes:
57*ab27253aSKyungwoo Lee    - InstIndex:       3
58*ab27253aSKyungwoo Lee      OpndIndex:       0
59*ab27253aSKyungwoo Lee      OpndHash:        456
60*ab27253aSKyungwoo Lee...
61*ab27253aSKyungwoo Lee
62*ab27253aSKyungwoo Lee#--- raw-2.cgtext
63*ab27253aSKyungwoo Lee:stable_function_map
64*ab27253aSKyungwoo Lee---
65*ab27253aSKyungwoo Lee- Hash:            123
66*ab27253aSKyungwoo Lee  FunctionName:    f2
67*ab27253aSKyungwoo Lee  ModuleName:      'goo.bc'
68*ab27253aSKyungwoo Lee  InstCount:       7
69*ab27253aSKyungwoo Lee  IndexOperandHashes:
70*ab27253aSKyungwoo Lee    - InstIndex:       3
71*ab27253aSKyungwoo Lee      OpndIndex:       0
72*ab27253aSKyungwoo Lee      OpndHash:        789
73*ab27253aSKyungwoo Lee...
74*ab27253aSKyungwoo Lee
75*ab27253aSKyungwoo Lee#--- merge-template.s
76*ab27253aSKyungwoo Lee.section __DATA,__llvm_merge
77*ab27253aSKyungwoo Lee_data:
78*ab27253aSKyungwoo Lee.byte <RAW_BYTES>
79*ab27253aSKyungwoo Lee
80*ab27253aSKyungwoo Lee#--- main.s
81*ab27253aSKyungwoo Lee.globl _main
82*ab27253aSKyungwoo Lee
83*ab27253aSKyungwoo Lee.text
84*ab27253aSKyungwoo Lee_main:
85*ab27253aSKyungwoo Lee  ret
86