Skip to content

Commit 688fa35

Browse files
authored
[Profile] Dump binary id to raw profiles on Windows. (#75618)
#74652 adds `__buildid` symbol which allows us to dump it at runtime.
1 parent 11141bc commit 688fa35

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

compiler-rt/lib/profile/InstrProfilingPlatformWindows.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,17 @@ ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
7474
ValueProfNode *CurrentVNode = &VNodesStart + 1;
7575
ValueProfNode *EndVNode = &VNodesEnd;
7676

77+
/* lld-link provides __buildid symbol which ponits to the 16 bytes build id when
78+
* using /build-id flag. https://lld.llvm.org/windows_support.html#lld-flags */
79+
#define BUILD_ID_LEN 16
80+
COMPILER_RT_WEAK extern uint8_t __buildid[BUILD_ID_LEN];
7781
COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
82+
if (*__buildid) {
83+
if (Writer &&
84+
lprofWriteOneBinaryId(Writer, BUILD_ID_LEN, __buildid, 0) == -1)
85+
return -1;
86+
return sizeof(uint64_t) + BUILD_ID_LEN;
87+
}
7888
return 0;
7989
}
8090

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// REQUIRES: target={{.*windows-msvc.*}}
2+
// REQUIRES: lld-available
3+
4+
// RUN: %clang_profgen -O2 -o %t %s
5+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
6+
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.out
7+
// RUN: FileCheck %s --check-prefix=NO-BINARY-ID < %t.out
8+
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
9+
10+
// RUN: %clang_profgen -fuse-ld=lld -Wl,-build-id -O2 -o %t %s
11+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
12+
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.profraw.out
13+
// RUN: FileCheck %s --check-prefix=BINARY-ID-RAW-PROF < %t.profraw.out
14+
15+
// RUN: rm -rf %t.profdir
16+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
17+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
18+
// RUN: env LLVM_PROFILE_FILE=%t.profdir/default_%m.profraw %run %t
19+
// RUN: llvm-profdata show --binary-ids %t.profdir/default_*.profraw > %t.profraw.out
20+
// RUN: FileCheck %s --check-prefix=ONE-BINARY-ID < %t.profraw.out
21+
22+
// RUN: llvm-profdata merge -o %t.profdata %t.profdir/default_*.profraw
23+
// RUN: llvm-profdata show --binary-ids %t.profdata > %t.profdata.out
24+
// RUN: FileCheck %s --check-prefix=ONE-BINARY-ID < %t.profdata.out
25+
26+
// Test raw profiles with DLLs.
27+
// RUN: rm -rf %t.dir && split-file %s %t.dir
28+
// RUN: %clang_profgen -O2 %t.dir/foo.c -fuse-ld=lld -Wl,-build-id -Wl,-dll -o %t.dir/foo.dll
29+
// RUN: %clang_profgen -O2 %t.dir/bar.c -fuse-ld=lld -Wl,-build-id -Wl,-dll -o %t.dir/bar.dll
30+
// RUN: %clang_profgen -O2 %t.dir/main.c -fuse-ld=lld -Wl,-build-id %t.dir/foo.lib %t.dir/bar.lib -o %t.dir/main.exe
31+
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.dir/main.exe
32+
// RUN: llvm-profdata show --binary-ids %t.profraw > %t.profraw.out
33+
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
34+
// RUN: FileCheck %s --check-prefix=MULTI-BINARY-ID < %t.profraw.out
35+
36+
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
37+
// RUN: llvm-profdata show --binary-ids %t.profdata > %t.profdata.out
38+
// RUN: FileCheck %s --check-prefix=MULTI-BINARY-ID < %t.profraw.out
39+
40+
//--- foo.c
41+
__declspec(dllexport) void foo() {}
42+
43+
//--- bar.c
44+
__declspec(dllexport) void bar() {}
45+
46+
//--- main.c
47+
__declspec(dllimport) void foo();
48+
__declspec(dllimport) void bar();
49+
int main() {
50+
foo();
51+
bar();
52+
return 0;
53+
}
54+
55+
// NO-BINARY-ID: Instrumentation level: Front-end
56+
// NO-BINARY-ID-NEXT: Total functions: 3
57+
// NO-BINARY-ID-NEXT: Maximum function count: 1
58+
// NO-BINARY-ID-NEXT: Maximum internal block count: 0
59+
// NO-BINARY-ID-NOT: Binary IDs:
60+
61+
// BINARY-ID-RAW-PROF: Instrumentation level: Front-end
62+
// BINARY-ID-RAW-PROF-NEXT: Total functions: 3
63+
// BINARY-ID-RAW-PROF-NEXT: Maximum function count: 1
64+
// BINARY-ID-RAW-PROF-NEXT: Maximum internal block count: 0
65+
// BINARY-ID-RAW-PROF-NEXT: Binary IDs:
66+
// BINARY-ID-RAW-PROF-NEXT: {{[0-9a-f]+}}
67+
68+
// ONE-BINARY-ID: Instrumentation level: Front-end
69+
// ONE-BINARY-ID-NEXT: Total functions: 3
70+
// ONE-BINARY-ID-NEXT: Maximum function count: 3
71+
// ONE-BINARY-ID-NEXT: Maximum internal block count: 0
72+
// ONE-BINARY-ID-NEXT: Binary IDs:
73+
// ONE-BINARY-ID-NEXT: {{[0-9a-f]+}}
74+
75+
// MULTI-BINARY-ID: Instrumentation level: Front-end
76+
// MULTI-BINARY-ID-NEXT: Total functions: 3
77+
// MULTI-BINARY-ID-NEXT: Maximum function count: 1
78+
// MULTI-BINARY-ID-NEXT: Maximum internal block count: 0
79+
// MULTI-BINARY-ID-NEXT: Binary IDs:
80+
// MULTI-BINARY-ID-NEXT: {{[0-9a-f]+}}
81+
// MULTI-BINARY-ID-NEXT: {{[0-9a-f]+}}
82+
// MULTI-BINARY-ID-NEXT: {{[0-9a-f]+}}

0 commit comments

Comments
 (0)