Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / test / profile / ContinuousSyncMode / online-merging-windows.c
blob0a36d82dac6472c0c87f82a4d9622f7439b79f05
1 // REQUIRES: target={{.*windows-msvc.*}}
2 // REQUIRES: lld-available
4 // Test the online merging mode (%m) along with continuous mode (%c).
5 //
6 // Split files & cd into a temporary directory.
7 // RUN: rm -rf %t.dir && split-file %s %t.dir && cd %t.dir
8 //
9 // Create two DLLs and a driver program that uses them.
10 // 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
11 // 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
12 // 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
14 // === Round 1 ===
15 // Test merging+continuous mode without any file contention.
17 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe nospawn
18 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
19 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND1
21 // ROUND1-LABEL: Counters:
22 // ROUND1-DAG: foo:
23 // ROUND1-DAG: Hash: 0x{{.*}}
24 // ROUND1-DAG: Counters: 1
25 // ROUND1-DAG: Block counts: [1]
26 // ROUND1-DAG: bar:
27 // ROUND1-DAG: Hash: 0x{{.*}}
28 // ROUND1-DAG: Counters: 1
29 // ROUND1-DAG: Block counts: [1]
30 // ROUND1-DAG: main:
31 // ROUND1-DAG: Hash: 0x{{.*}}
32 // ROUND1-LABEL: Instrumentation level: IR
34 // === Round 2 ===
35 // Test merging+continuous mode with some file contention.
37 // RUN: env LLVM_PROFILE_FILE="%t.dir/profdir/%m%c.profraw" %run %t.dir/main.exe spawn
38 // RUN: llvm-profdata merge -o %t.profdata %t.dir/profdir
39 // RUN: llvm-profdata show --counts --all-functions %t.profdata | FileCheck %s -check-prefix=ROUND2
41 // ROUND2-LABEL: Counters:
42 // ROUND2-DAG: foo:
43 // ROUND2-DAG: Hash: 0x{{.*}}
44 // ROUND2-DAG: Counters: 1
45 // ROUND2-DAG: Block counts: [97]
46 // ROUND2-DAG: bar:
47 // ROUND2-DAG: Hash: 0x{{.*}}
48 // ROUND2-DAG: Counters: 1
49 // ROUND2-DAG: Block counts: [97]
50 // ROUND2-DAG: main:
51 // ROUND2-DAG: Hash: 0x{{.*}}
52 // ROUND2-LABEL: Instrumentation level: IR
54 //--- foo.c
55 __declspec(dllexport) void foo(void) {}
57 //--- bar.c
58 __declspec(dllexport) void bar(void) {}
60 //--- main.c
61 #include <stdio.h>
62 #include <string.h>
63 #include <windows.h>
66 const int num_child_procs_to_spawn = 32;
68 extern int __llvm_profile_is_continuous_mode_enabled(void);
69 extern char *__llvm_profile_get_filename(void);
71 __declspec(dllimport) void foo(void);
72 __declspec(dllimport) void bar(void);
74 // Change to "#define" for debug output.
75 #undef DEBUG_TEST
77 #ifdef DEBUG_TEST
78 # define DEBUG(...) fprintf(stderr, __VA_ARGS__);
79 #else
80 # define DEBUG(...)
81 #endif
83 int main(int argc, char *const argv[]) {
84 if (argc < 2) {
85 DEBUG("Requires at least one argument.\n");
86 return 1;
88 if (strcmp(argv[1], "nospawn") == 0) {
89 DEBUG(
90 "Hello from child (pid = %lu, cont-mode-enabled = %d, profile = %s).\n",
91 GetCurrentProcessId(), __llvm_profile_is_continuous_mode_enabled(),
92 __llvm_profile_get_filename());
94 foo();
95 bar();
96 return 0;
97 } else if (strcmp(argv[1], "spawn") == 0) {
98 // This is the start of Round 2.
99 // Expect Counts[dsoX] = 1, as this was the state at the end of Round 1.
100 int I;
101 HANDLE child_pids[num_child_procs_to_spawn];
102 for (I = 0; I < num_child_procs_to_spawn; ++I) {
103 foo(); // Counts[dsoX] += 2 * num_child_procs_to_spawn
104 bar();
106 DEBUG("Spawning child with argv = {%s, %s, NULL} and envp = {%s, NULL}\n",
107 child_argv[0], child_argv[1], child_envp[0]);
109 // Start the child process.
110 STARTUPINFO si;
111 ZeroMemory(&si, sizeof(si));
112 PROCESS_INFORMATION pi;
113 ZeroMemory(&pi, sizeof(pi));
114 if (!CreateProcess(NULL, // No module name (use command line)
115 "main.exe nospawn", // Command line
116 NULL, // Process handle not inheritable
117 NULL, // Thread handle not inheritable
118 FALSE, // Set handle inheritance to FALSE
119 0, // No creation flags
120 NULL, // Use parent's environment block
121 NULL, // Use parent's starting directory
122 &si, // Pointer to STARTUPINFO structure
123 &pi) // Pointer to PROCESS_INFORMATION structure
125 fprintf(stderr, "Child %d could not be spawned: %lu\n", I,
126 GetLastError());
127 return 1;
129 child_pids[I] = pi.hProcess;
131 DEBUG("Spawned child %d (pid = %zu).\n", I, pi.dwProcessId);
133 for (I = 0; I < num_child_procs_to_spawn; ++I) {
134 foo(); // Counts[dsoX] += num_child_procs_to_spawn
135 bar();
137 DWORD exit_code;
138 WaitForSingleObject(child_pids[I], INFINITE);
139 if (!GetExitCodeProcess(child_pids[I], &exit_code)) {
140 fprintf(stderr, "Failed to get exit code of child %d.\n", I);
141 return 1;
143 if (exit_code != 0) {
144 fprintf(stderr, "Child %d did not exit with code 0.\n", I);
145 return 1;
149 // At the end of Round 2, we have:
150 // Counts[dsoX] = 1 + (2 * num_child_procs_to_spawn) + num_child_procs_to_spawn
151 // = 97
153 return 0;
156 return 1;