mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-24 19:56:05 +00:00

Reimplements MisExpect diagnostics from D66324 to reconstruct its original checking methodology only using MD_prof branch_weights metadata. New checks rely on 2 invariants: 1) For frontend instrumentation, MD_prof branch_weights will always be populated before llvm.expect intrinsics are lowered. 2) for IR and sample profiling, llvm.expect intrinsics will always be lowered before branch_weights are populated from the IR profiles. These invariants allow the checking to assume how the existing branch weights are populated depending on the profiling method used, and emit the correct diagnostics. If these invariants are ever invalidated, the MisExpect related checks would need to be updated, potentially by re-introducing MD_misexpect metadata, and ensuring it always will be transformed the same way as branch_weights in other optimization passes. Frontend based profiling is now enabled without using LLVM Args, by introducing a new CodeGen option, and checking if the -Wmisexpect flag has been passed on the command line. Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D115907
45 lines
1.0 KiB
C
45 lines
1.0 KiB
C
// 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
|
|
|
|
#define inner_loop 1000
|
|
#define outer_loop 20
|
|
#define arry_size 25
|
|
|
|
int sum(int *buff, int size);
|
|
int random_sample(int *buff, int size);
|
|
int rand();
|
|
void init_arry();
|
|
|
|
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;
|
|
}
|