1 // REQUIRES: target={{.*windows-msvc.*}}
2 // REQUIRES: lld-available
4 // Fails on Windows on Arm for unknown reasons.
5 // UNSUPPORTED: target=aarch64-pc-windows-msvc
7 // Test the online merging mode (%m) along with continuous mode (%c).
9 // Split files & cd into a temporary directory.
10 // RUN: rm -rf %t.dir && split-file %s %t.dir && cd %t.dir
12 // Create two DLLs and a driver program that uses them.
13 // RUN: %clang_pgogen foo.c -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld -Wl,-dll -o %t.dir/foo.dll
14 // RUN: %clang_pgogen bar.c -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld -Wl,-dll -o %t.dir/bar.dll
15 // RUN: %clang_pgogen main.c -o main.exe %t.dir/foo.lib %t.dir/bar.lib -mllvm -instrprof-atomic-counter-update-all=1 -mllvm -runtime-counter-relocation=true -fuse-ld=lld
18 // Test merging+continuous mode without any file contention.
20 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe nospawn
21 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
22 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND1
24 // ROUND1-LABEL: Counters:
26 // ROUND1-DAG: Hash: 0x{{.*}}
27 // ROUND1-DAG: Counters: 1
28 // ROUND1-DAG: Block counts: [1]
30 // ROUND1-DAG: Hash: 0x{{.*}}
31 // ROUND1-DAG: Counters: 1
32 // ROUND1-DAG: Block counts: [1]
34 // ROUND1-DAG: Hash: 0x{{.*}}
35 // ROUND1-LABEL: Instrumentation level: IR
38 // Test merging+continuous mode with some file contention.
40 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe spawn
41 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
42 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND2
44 // ROUND2-LABEL: Counters:
46 // ROUND2-DAG: Hash: 0x{{.*}}
47 // ROUND2-DAG: Counters: 1
48 // ROUND2-DAG: Block counts: [97]
50 // ROUND2-DAG: Hash: 0x{{.*}}
51 // ROUND2-DAG: Counters: 1
52 // ROUND2-DAG: Block counts: [97]
54 // ROUND2-DAG: Hash: 0x{{.*}}
55 // ROUND2-LABEL: Instrumentation level: IR
58 __declspec(dllexport
) void foo(void) {}
61 __declspec(dllexport
) void bar(void) {}
69 const int num_child_procs_to_spawn
= 32;
71 extern int __llvm_profile_is_continuous_mode_enabled(void);
72 extern char *__llvm_profile_get_filename(void);
74 __declspec(dllimport
) void foo(void);
75 __declspec(dllimport
) void bar(void);
77 // Change to "#define" for debug output.
81 # define DEBUG(...) fprintf(stderr, __VA_ARGS__);
86 int main(int argc
, char *const argv
[]) {
88 DEBUG("Requires at least one argument.\n");
91 if (strcmp(argv
[1], "nospawn") == 0) {
93 "Hello from child (pid = %lu, cont-mode-enabled = %d, profile = %s).\n",
94 GetCurrentProcessId(), __llvm_profile_is_continuous_mode_enabled(),
95 __llvm_profile_get_filename());
100 } else if (strcmp(argv
[1], "spawn") == 0) {
101 // This is the start of Round 2.
102 // Expect Counts[dsoX] = 1, as this was the state at the end of Round 1.
104 HANDLE child_pids
[num_child_procs_to_spawn
];
105 for (I
= 0; I
< num_child_procs_to_spawn
; ++I
) {
106 foo(); // Counts[dsoX] += 2 * num_child_procs_to_spawn
109 DEBUG("Spawning child with argv = {%s, %s, NULL} and envp = {%s, NULL}\n",
110 child_argv
[0], child_argv
[1], child_envp
[0]);
112 // Start the child process.
114 ZeroMemory(&si
, sizeof(si
));
115 PROCESS_INFORMATION pi
;
116 ZeroMemory(&pi
, sizeof(pi
));
117 if (!CreateProcess(NULL
, // No module name (use command line)
118 "main.exe nospawn", // Command line
119 NULL
, // Process handle not inheritable
120 NULL
, // Thread handle not inheritable
121 FALSE
, // Set handle inheritance to FALSE
122 0, // No creation flags
123 NULL
, // Use parent's environment block
124 NULL
, // Use parent's starting directory
125 &si
, // Pointer to STARTUPINFO structure
126 &pi
) // Pointer to PROCESS_INFORMATION structure
128 fprintf(stderr
, "Child %d could not be spawned: %lu\n", I
,
132 child_pids
[I
] = pi
.hProcess
;
134 DEBUG("Spawned child %d (pid = %zu).\n", I
, pi
.dwProcessId
);
136 for (I
= 0; I
< num_child_procs_to_spawn
; ++I
) {
137 foo(); // Counts[dsoX] += num_child_procs_to_spawn
141 WaitForSingleObject(child_pids
[I
], INFINITE
);
142 if (!GetExitCodeProcess(child_pids
[I
], &exit_code
)) {
143 fprintf(stderr
, "Failed to get exit code of child %d.\n", I
);
146 if (exit_code
!= 0) {
147 fprintf(stderr
, "Child %d did not exit with code 0.\n", I
);
152 // At the end of Round 2, we have:
153 // Counts[dsoX] = 1 + (2 * num_child_procs_to_spawn) + num_child_procs_to_spawn