xref: /llvm-project/compiler-rt/test/profile/Windows/coverage-weak-lld.cpp (revision 8822eaae0f459c478cf7c08ea662b2cbdfca34f3)
1 // REQUIRES: lld-available
2 
3 // REQUIRES: target={{.*windows-msvc.*}}
4 // RUN: %clang_profgen -fcoverage-mapping -c %s -o %t0.o
5 // RUN: %clang_profgen -fcoverage-mapping -c %s -DOBJ_1 -o %t1.o
6 // RUN: %clang_profgen -fcoverage-mapping -c %s -DOBJ_2 -o %t2.o
7 
8 /// An external symbol can override a weak external symbol.
9 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-opt:noref %t0.o %t1.o -o %t1
10 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t1 | FileCheck %s --check-prefix=CHECK1
11 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE1
12 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-opt:ref %t0.o %t1.o -o %t1
13 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t1 | FileCheck %s --check-prefix=CHECK1
14 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE1
15 
16 /// link.exe does not support weak overridding weak.
17 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-lld-allow-duplicate-weak,-opt:ref %t0.o %t2.o -o %t2
18 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t2 | FileCheck %s --check-prefix=CHECK2
19 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE2
20 
21 /// Repeat the above tests with -ffunction-sections (associative comdat).
22 // RUN: %clang_profgen -fcoverage-mapping -ffunction-sections -c %s -o %t0.o
23 // RUN: %clang_profgen -fcoverage-mapping -ffunction-sections -c %s -DOBJ_1 -o %t1.o
24 // RUN: %clang_profgen -fcoverage-mapping -ffunction-sections -c %s -DOBJ_2 -o %t2.o
25 
26 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-opt:noref %t0.o %t1.o -o %t1
27 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t1 | FileCheck %s --check-prefix=CHECK1
28 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE1
29 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-opt:ref %t0.o %t1.o -o %t1
30 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t1 | FileCheck %s --check-prefix=CHECK1
31 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE1
32 
33 // RUN: %clang_profgen -fcoverage-mapping -fuse-ld=lld -Wl,-lld-allow-duplicate-weak,-opt:ref %t0.o %t2.o -o %t2
34 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t2 | FileCheck %s --check-prefix=CHECK2
35 // RUN: llvm-profdata show %t.profraw --all-functions | FileCheck %s --check-prefix=PROFILE2
36 
37 // CHECK1: strong
38 // CHECK1: strong
39 
40 /// Document the current behavior:
41 ///  __profc_?weak@@YAXXZ in %t1.o is local and has a zero value.
42 /// Without GC it takes a duplicate entry.
43 // PROFILE1:      ?weak@@YAXXZ:
44 // PROFILE1-NEXT:    Hash:
45 // PROFILE1-NEXT:    Counters: 1
46 // PROFILE1-NEXT:    Function count: 0
47 // PROFILE1:      ?weak@@YAXXZ:
48 // PROFILE1-NEXT:    Hash:
49 // PROFILE1-NEXT:    Counters: 1
50 // PROFILE1-NEXT:    Function count: 2
51 
52 // CHECK2: weak
53 // CHECK2: weak
54 
55 /// __profc__Z4weakv in %t2.o is weak and resolves to the value of %t0.o's copy.
56 /// Without GC it takes a duplicate entry.
57 // PROFILE2:      ?weak@@YAXXZ:
58 // PROFILE2-NEXT:    Hash:
59 // PROFILE2-NEXT:    Counters: 1
60 // PROFILE2-NEXT:    Function count: 2
61 // PROFILE2:      ?weak@@YAXXZ:
62 // PROFILE2-NEXT:    Hash:
63 // PROFILE2-NEXT:    Counters: 1
64 // PROFILE2-NEXT:    Function count: 2
65 
66 #ifdef OBJ_1
67 #include <stdio.h>
68 
weak()69 void weak() { puts("strong"); }
foo()70 void foo() { weak(); }
71 
72 #elif defined(OBJ_2)
73 #include <stdio.h>
74 
weak()75 __attribute__((weak)) void weak() { puts("unreachable"); }
foo()76 void foo() { weak(); }
77 
78 #else
79 #include <stdio.h>
80 
weak()81 __attribute__((weak)) void weak() { puts("weak"); }
82 void foo();
83 
main()84 int main() {
85   foo();
86   weak();
87 }
88 #endif
89