Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reland "clang-misexpect: Profile Guided Validation of Performance Ann…
…otations in LLVM" This patch contains the basic functionality for reporting potentially incorrect usage of __builtin_expect() by comparing the developer's annotation against a collected PGO profile. A more detailed proposal and discussion appears on the CFE-dev mailing list (http://lists.llvm.org/pipermail/cfe-dev/2019-July/062971.html) and a prototype of the initial frontend changes appear here in D65300 We revised the work in D65300 by moving the misexpect check into the LLVM backend, and adding support for IR and sampling based profiles, in addition to frontend instrumentation. We add new misexpect metadata tags to those instructions directly influenced by the llvm.expect intrinsic (branch, switch, and select) when lowering the intrinsics. The misexpect metadata contains information about the expected target of the intrinsic so that we can check against the correct PGO counter when emitting diagnostics, and the compiler's values for the LikelyBranchWeight and UnlikelyBranchWeight. We use these branch weight values to determine when to emit the diagnostic to the user. A future patch should address the comment at the top of LowerExpectIntrisic.cpp to hoist the LikelyBranchWeight and UnlikelyBranchWeight values into a shared space that can be accessed outside of the LowerExpectIntrinsic pass. Once that is done, the misexpect metadata can be updated to be smaller. In the long term, it is possible to reconstruct portions of the misexpect metadata from the existing profile data. However, we have avoided this to keep the code simple, and because some kind of metadata tag will be required to identify which branch/switch/select instructions are influenced by the use of llvm.expect Patch By: paulkirth Differential Revision: https://reviews.llvm.org/D66324 llvm-svn: 371635
- Loading branch information
Showing
41 changed files
with
1,721 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
clang/test/Profile/Inputs/misexpect-branch-nonconst-expect-arg.proftext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
bar | ||
# Func Hash: | ||
11262309464 | ||
# Num Counters: | ||
2 | ||
# Counter Values: | ||
200000 | ||
2 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
bar | ||
# Func Hash: | ||
45795613684824 | ||
# Num Counters: | ||
2 | ||
# Counter Values: | ||
200000 | ||
0 | ||
|
12 changes: 12 additions & 0 deletions
12
clang/test/Profile/Inputs/misexpect-switch-default-only.proftext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
main | ||
# Func Hash: | ||
79676873694057560 | ||
# Num Counters: | ||
5 | ||
# Counter Values: | ||
1 | ||
20 | ||
20000 | ||
20000 | ||
20000 | ||
|
16 changes: 16 additions & 0 deletions
16
clang/test/Profile/Inputs/misexpect-switch-default.proftext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
main | ||
# Func Hash: | ||
8712453512413296413 | ||
# Num Counters: | ||
9 | ||
# Counter Values: | ||
1 | ||
20000 | ||
20000 | ||
4066 | ||
11889 | ||
0 | ||
0 | ||
4045 | ||
0 | ||
|
17 changes: 17 additions & 0 deletions
17
clang/test/Profile/Inputs/misexpect-switch-nonconst.proftext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
main | ||
# Func Hash: | ||
1965403898329309329 | ||
# Num Counters: | ||
10 | ||
# Counter Values: | ||
1 | ||
20 | ||
20000 | ||
20000 | ||
1 | ||
0 | ||
0 | ||
0 | ||
19999 | ||
0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
main | ||
# Func Hash: | ||
1965403898329309329 | ||
# Num Counters: | ||
9 | ||
# Counter Values: | ||
1 | ||
20 | ||
20000 | ||
20000 | ||
12 | ||
26 | ||
0 | ||
0 | ||
19962 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Test that misexpect emits no warning when prediction is correct | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-branch.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect | ||
|
||
// expected-no-diagnostics | ||
#define likely(x) __builtin_expect(!!(x), 1) | ||
#define unlikely(x) __builtin_expect(!!(x), 0) | ||
|
||
int foo(int); | ||
int baz(int); | ||
int buzz(); | ||
|
||
const int inner_loop = 100; | ||
const int outer_loop = 2000; | ||
|
||
int bar() { | ||
int rando = buzz(); | ||
int x = 0; | ||
if (unlikely(rando % (outer_loop * inner_loop) == 0)) { | ||
x = baz(rando); | ||
} else { | ||
x = foo(50); | ||
} | ||
return x; | ||
} |
23 changes: 23 additions & 0 deletions
23
clang/test/Profile/misexpect-branch-nonconst-expected-val.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Test that misexpect emits no warning when condition is not a compile-time constant | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-branch-nonconst-expect-arg.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect | ||
|
||
// expected-no-diagnostics | ||
int foo(int); | ||
int baz(int); | ||
int buzz(); | ||
|
||
const int inner_loop = 100; | ||
const int outer_loop = 2000; | ||
|
||
int bar() { | ||
int rando = buzz(); | ||
int x = 0; | ||
if (__builtin_expect(rando % (outer_loop * inner_loop) == 0, buzz())) { | ||
x = baz(rando); | ||
} else { | ||
x = foo(50); | ||
} | ||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Test that misexpect emits no warning when prediction is correct | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-branch.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect | ||
|
||
// expected-no-diagnostics | ||
#define unpredictable(x) __builtin_unpredictable(!!(x)) | ||
|
||
int foo(int); | ||
int baz(int); | ||
int buzz(); | ||
|
||
const int inner_loop = 100; | ||
const int outer_loop = 2000; | ||
|
||
int bar() { | ||
int rando = buzz(); | ||
int x = 0; | ||
if (unpredictable(rando % (outer_loop * inner_loop) == 0)) { | ||
x = baz(rando); | ||
} else { | ||
x = foo(50); | ||
} | ||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Test that misexpect detects mis-annotated branches | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-branch.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=imprecise -Wmisexpect | ||
// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=exact -Wmisexpect -debug-info-kind=line-tables-only | ||
// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify=foo | ||
|
||
// foo-no-diagnostics | ||
#define likely(x) __builtin_expect(!!(x), 1) | ||
#define unlikely(x) __builtin_expect(!!(x), 0) | ||
|
||
int foo(int); | ||
int baz(int); | ||
int buzz(); | ||
|
||
const int inner_loop = 100; | ||
const int outer_loop = 2000; | ||
|
||
int bar() { // imprecise-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} | ||
int rando = buzz(); | ||
int x = 0; | ||
if (likely(rando % (outer_loop * inner_loop) == 0)) { // exact-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} | ||
x = baz(rando); | ||
} else { | ||
x = foo(50); | ||
} | ||
return x; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Test that misexpect detects mis-annotated switch statements for default case | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-switch-default.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only | ||
|
||
int sum(int *buff, int size); | ||
int random_sample(int *buff, int size); | ||
int rand(); | ||
void init_arry(); | ||
|
||
const int inner_loop = 1000; | ||
const int outer_loop = 20; | ||
const int arry_size = 25; | ||
|
||
int arry[arry_size] = {0}; | ||
|
||
int main() { | ||
init_arry(); | ||
int val = 0; | ||
int j; | ||
for (j = 0; j < outer_loop * inner_loop; ++j) { | ||
unsigned condition = rand() % 5; | ||
switch (__builtin_expect(condition, 6)) { // expected-warning-re {{Potential performance regression from use of __builtin_expect(): Annotation was correct on {{.+}}% ({{[0-9]+ / [0-9]+}}) of profiled executions.}} | ||
case 0: | ||
val += sum(arry, arry_size); | ||
break; | ||
case 1: | ||
case 2: | ||
case 3: | ||
break; | ||
case 4: | ||
val += random_sample(arry, arry_size); | ||
break; | ||
default: | ||
__builtin_unreachable(); | ||
} // end switch | ||
} // end outer_loop | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Test that misexpect emits no warning when switch condition is non-const | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-switch-nonconst.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -disable-llvm-passes -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify | ||
|
||
// expected-no-diagnostics | ||
int sum(int *buff, int size); | ||
int random_sample(int *buff, int size); | ||
int rand(); | ||
void init_arry(); | ||
|
||
const int inner_loop = 1000; | ||
const int outer_loop = 20; | ||
const int arry_size = 25; | ||
|
||
int arry[arry_size] = {0}; | ||
|
||
int main() { | ||
init_arry(); | ||
int val = 0; | ||
|
||
int j, k; | ||
for (j = 0; j < outer_loop; ++j) { | ||
for (k = 0; k < inner_loop; ++k) { | ||
unsigned condition = rand() % 10000; | ||
switch (__builtin_expect(condition, rand())) { | ||
case 0: | ||
val += sum(arry, arry_size); | ||
break; | ||
case 1: | ||
case 2: | ||
case 3: | ||
case 4: | ||
val += random_sample(arry, arry_size); | ||
break; | ||
default: | ||
__builtin_unreachable(); | ||
} // end switch | ||
} // end inner_loop | ||
} // end outer_loop | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Test that misexpect emits no warning when there is only one switch case | ||
|
||
// RUN: llvm-profdata merge %S/Inputs/misexpect-switch-default-only.proftext -o %t.profdata | ||
// RUN: %clang_cc1 %s -O2 -o - -emit-llvm -fprofile-instrument-use-path=%t.profdata -verify -Wmisexpect -debug-info-kind=line-tables-only | ||
|
||
// expected-no-diagnostics | ||
int sum(int *buff, int size); | ||
int random_sample(int *buff, int size); | ||
int rand(); | ||
void init_arry(); | ||
|
||
const int inner_loop = 1000; | ||
const int outer_loop = 20; | ||
const int arry_size = 25; | ||
|
||
int arry[arry_size] = {0}; | ||
|
||
int main() { | ||
init_arry(); | ||
int val = 0; | ||
|
||
int j, k; | ||
for (j = 0; j < outer_loop; ++j) { | ||
for (k = 0; k < inner_loop; ++k) { | ||
unsigned condition = rand() % 10000; | ||
switch (__builtin_expect(condition, 0)) { | ||
default: | ||
val += random_sample(arry, arry_size); | ||
break; | ||
}; // end switch | ||
} // end inner_loop | ||
} // end outer_loop | ||
|
||
return 0; | ||
} |
Oops, something went wrong.