Skip to content

Commit 47fb21d

Browse files
ZhiZhuangMTKErich Keane
authored andcommitted
fix test failure for clang/test/CodeGen/builtin-expect-with-probability.cpp
Fix test case added by D79830 Rewrite the test case, which did similar thing as builtin-expect.c does(test generated llvm intrinsic instead of test branch weights). Currently pass by "-disable-llvm-passes" option. Differential Revision: https://reviews.llvm.org/D82403
1 parent 6904c71 commit 47fb21d

File tree

1 file changed

+67
-13
lines changed

1 file changed

+67
-13
lines changed
Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
// RUN: %clang_cc1 -emit-llvm -o - %s -O1 | FileCheck %s
2-
// RUN: %clang_cc1 -emit-llvm -o - -fexperimental-new-pass-manager %s -O1 | FileCheck %s
1+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O1 -disable-llvm-passes | FileCheck %s --check-prefix=ALL --check-prefix=O1
2+
// RUN: %clang_cc1 -triple x86_64-unknown-unknown -emit-llvm -o - %s -O0 | FileCheck %s --check-prefix=ALL --check-prefix=O0
33
extern int global;
44

5+
int expect_taken(int x) {
6+
// ALL-LABEL: expect_taken
7+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 9.000000e-01)
8+
// O0-NOT: @llvm.expect.with.probability
9+
10+
if (__builtin_expect_with_probability(x == 100, 1, 0.9)) {
11+
return 0;
12+
}
13+
return x;
14+
}
15+
16+
int expect_not_taken(int x) {
17+
// ALL-LABEL: expect_not_taken
18+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 0, double 9.000000e-01)
19+
// O0-NOT: @llvm.expect.with.probability
20+
21+
if (__builtin_expect_with_probability(x == 100, 0, 0.9)) {
22+
return 0;
23+
}
24+
return x;
25+
}
26+
527
struct S {
628
static constexpr int prob = 1;
729
};
830

931
template<typename T>
10-
int expect_taken(int x) {
11-
// CHECK: !{{[0-9]+}} = !{!"branch_weights", i32 2147483647, i32 1}
32+
int expect_taken_template(int x) {
33+
// ALL-LABEL: expect_taken_template
34+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 1.000000e+00)
35+
// O0-NOT: @llvm.expect.with.probability
1236

1337
if (__builtin_expect_with_probability (x == 100, 1, T::prob)) {
1438
return 0;
@@ -17,20 +41,31 @@ int expect_taken(int x) {
1741
}
1842

1943
int f() {
20-
return expect_taken<S>(global);
44+
return expect_taken_template<S>(global);
2145
}
2246

23-
int expect_taken2(int x) {
24-
// CHECK: !{{[0-9]+}} = !{!"branch_weights", i32 1932735283, i32 214748366}
47+
int x;
48+
extern "C" {
49+
int y(void);
50+
}
51+
void foo();
2552

26-
if (__builtin_expect_with_probability(x == 100, 1, 0.9)) {
27-
return 0;
28-
}
29-
return x;
53+
void expect_value_side_effects() {
54+
// ALL-LABEL: expect_value_side_effects
55+
// ALL: [[CALL:%.*]] = call i32 @y
56+
// O1: [[SEXT:%.*]] = sext i32 [[CALL]] to i64
57+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 [[SEXT]], double 6.000000e-01)
58+
// O0-NOT: @llvm.expect.with.probability
59+
60+
if (__builtin_expect_with_probability(x, y(), 0.6))
61+
foo();
3062
}
3163

32-
int expect_taken3(int x) {
33-
// CHECK: !{{[0-9]+}} = !{!"branch_weights", i32 107374184, i32 107374184, i32 1717986918, i32 107374184, i32 107374184}
64+
int switch_cond(int x) {
65+
// ALL-LABEL: switch_cond
66+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 1, double 8.000000e-01)
67+
// O0-NOT: @llvm.expect.with.probability
68+
3469
switch (__builtin_expect_with_probability(x, 1, 0.8)) {
3570
case 0:
3671
x = x + 0;
@@ -45,3 +80,22 @@ int expect_taken3(int x) {
4580
}
4681
return x;
4782
}
83+
84+
constexpr double prob = 0.8;
85+
86+
int variable_expected(int stuff) {
87+
// ALL-LABEL: variable_expected
88+
// O1: call i64 @llvm.expect.with.probability.i64(i64 {{%.*}}, i64 {{%.*}}, double 8.000000e-01)
89+
// O0-NOT: @llvm.expect.with.probability
90+
91+
int res = 0;
92+
93+
switch(__builtin_expect_with_probability(stuff, stuff, prob)) {
94+
case 0:
95+
res = 1;
96+
break;
97+
default:
98+
break;
99+
}
100+
return res;
101+
}

0 commit comments

Comments
 (0)