Skip to content

Commit 14be3f0

Browse files
committed
[debuginfo-tests] Add some optnone tests
Add dexter tests using the optnone attribute in various scenarios. Our users have found optnone useful when debugging optimised code. We have these tests downstream (and one upstream already: D89873) and we would like to contribute them if there is any interest. The tests are fairly self explanatory. Testing optnone with: * optnone-fastmath.cpp: floats and -ffast-math, * optnone-simple-functions: simple functions and integer arithmetic, * optnone-struct-and-methods: a struct with methods, * optnone-vectors-and-functions: templates and integer vector arithmetic. optnone-vectors-and-functions contains two FIXMEs. The first problem is that lldb seems to struggle with evaluating expressions with the templates used here (example below). Perhaps this is PR42920? (lldb) p TypeTraits<int __attribute__((ext_vector_type(4)))>::NumElements error: <user expression 0>:1:1: no template named 'TypeTraits' TypeTraits<int __attribute__((ext_vector_type(4)))>::NumElements ^ The second is that while lldb cannot evaluate the following expression, gdb can, but it reports that the variable has been optimzed away. It does this when compiling at O0 too. llvm-dwarfdump shows that MysteryNumber does have a location. I don't know whether the DIE is bad or if both debuggers just don't support it. TypeTraits<int __attribute__((ext_vector_type(4)))>::MysteryNumber DW_TAG_variable DW_AT_specification (0x0000006b "MysteryNumber") DW_AT_location (DW_OP_addr 0x601028) DW_AT_linkage_name ("_ZN10TypeTraitsIDv4_iE13MysteryNumberE") Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D97668
1 parent d6ba8ec commit 14be3f0

File tree

4 files changed

+448
-0
lines changed

4 files changed

+448
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// RUN: %dexter --fail-lt 1.0 -w \
2+
// RUN: --builder 'clang' --debugger 'lldb' \
3+
// RUN: --cflags "-ffast-math -O2 -g" -- %s
4+
// RUN: %dexter --fail-lt 1.0 -w \
5+
// RUN: --builder 'clang' --debugger 'lldb' \
6+
// RUN: --cflags "-ffast-math -O0 -g" -- %s
7+
8+
// REQUIRES: lldb
9+
// UNSUPPORTED: system-windows
10+
11+
//// Check that the debugging experience with __attribute__((optnone)) at O2
12+
//// matches O0. Test scalar floating point arithmetic with -ffast-math.
13+
14+
//// Example of strength reduction.
15+
//// The division by 10.0f can be rewritten as a multiply by 0.1f.
16+
//// A / 10.f ==> A * 0.1f
17+
//// This is safe with fastmath since we treat the two operations
18+
//// as equally precise. However we don't want this to happen
19+
//// with optnone.
20+
__attribute__((optnone))
21+
float test_fdiv(float A) {
22+
float result;
23+
result = A / 10.f; // DexLabel('fdiv_assign')
24+
return result; // DexLabel('fdiv_ret')
25+
}
26+
// DexExpectWatchValue('A', 4, on_line='fdiv_assign')
27+
// DexExpectWatchValue('result', '0.400000006', on_line='fdiv_ret')
28+
29+
//// (A * B) - (A * C) ==> A * (B - C)
30+
__attribute__((optnone))
31+
float test_distributivity(float A, float B, float C) {
32+
float result;
33+
float op1 = A * B;
34+
float op2 = A * C; // DexLabel('distributivity_op2')
35+
result = op1 - op2; // DexLabel('distributivity_result')
36+
return result; // DexLabel('distributivity_ret')
37+
}
38+
// DexExpectWatchValue('op1', '20', on_line='distributivity_op2')
39+
// DexExpectWatchValue('op2', '24', on_line='distributivity_result')
40+
// DexExpectWatchValue('result', '-4', on_line='distributivity_ret')
41+
42+
//// (A + B) + C == A + (B + C)
43+
//// therefore, ((A + B) + C) + (A + (B + C)))
44+
//// can be rewritten as
45+
//// 2.0f * ((A + B) + C)
46+
//// Clang is currently unable to spot this optimization
47+
//// opportunity with fastmath.
48+
__attribute__((optnone))
49+
float test_associativity(float A, float B, float C) {
50+
float result;
51+
float op1 = A + B;
52+
float op2 = B + C;
53+
op1 += C; // DexLabel('associativity_op1')
54+
op2 += A;
55+
result = op1 + op2; // DexLabel('associativity_result')
56+
return result; // DexLabel('associativity_ret')
57+
}
58+
// DexExpectWatchValue('op1', '9', '15', from_line='associativity_op1', to_line='associativity_result')
59+
// DexExpectWatchValue('op2', '11', '15', from_line='associativity_op1', to_line='associativity_result')
60+
// DexExpectWatchValue('result', '30', on_line='associativity_ret')
61+
62+
//// With fastmath, the ordering of instructions doesn't matter
63+
//// since we work under the assumption that there is no loss
64+
//// in precision. This simplifies things for the optimizer which
65+
//// can then decide to reorder instructions and fold
66+
//// redundant operations like this:
67+
//// A += 5.0f
68+
//// A -= 5.0f
69+
//// -->
70+
//// A
71+
//// This function can be simplified to a return A + B.
72+
__attribute__((optnone))
73+
float test_simplify_fp_operations(float A, float B) {
74+
float result = A + 10.0f; // DexLabel('fp_operations_result')
75+
result += B; // DexLabel('fp_operations_add')
76+
result -= 10.0f;
77+
return result; // DexLabel('fp_operations_ret')
78+
}
79+
// DexExpectWatchValue('A', '8.25', on_line='fp_operations_result')
80+
// DexExpectWatchValue('B', '26.3999996', on_line='fp_operations_result')
81+
// DexExpectWatchValue('result', '18.25', '44.6500015', '34.6500015', from_line='fp_operations_add', to_line='fp_operations_ret')
82+
83+
//// Again, this is a simple return A + B.
84+
//// Clang is unable to spot the opportunity to fold the code sequence.
85+
__attribute__((optnone))
86+
float test_simplify_fp_operations_2(float A, float B, float C) {
87+
float result = A + C; // DexLabel('fp_operations_2_result')
88+
result += B;
89+
result -= C; // DexLabel('fp_operations_2_subtract')
90+
return result; // DexLabel('fp_operations_2_ret')
91+
}
92+
// DexExpectWatchValue('A', '9.11999988', on_line='fp_operations_2_result')
93+
// DexExpectWatchValue('B', '61.050003', on_line='fp_operations_2_result')
94+
// DexExpectWatchValue('C', '1002.11102', on_line='fp_operations_2_result')
95+
// DexExpectWatchValue('result', '1072.28101', '70.1699829', from_line='fp_operations_2_subtract', to_line='fp_operations_2_ret')
96+
97+
int main() {
98+
float result = test_fdiv(4.0f);
99+
result += test_distributivity(4.0f, 5.0f, 6.0f);
100+
result += test_associativity(4.0f, 5.0f, 6.0f);
101+
result += test_simplify_fp_operations(8.25, result);
102+
result += test_simplify_fp_operations_2(9.12, result, 1002.111);
103+
return static_cast<int>(result);
104+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// RUN: %dexter --fail-lt 1.0 -w \
2+
// RUN: --builder 'clang' --debugger 'lldb' \
3+
// RUN: --cflags "-O2 -g" -- %s
4+
// RUN: %dexter --fail-lt 1.0 -w \
5+
// RUN: --builder 'clang' --debugger 'lldb' \
6+
// RUN: --cflags "-O0 -g" -- %s
7+
8+
// REQUIRES: lldb
9+
// UNSUPPORTED: system-windows
10+
11+
//// Check that the debugging experience with __attribute__((optnone)) at O2
12+
//// matches O0. Test simple functions performing simple arithmetic
13+
//// operations and small loops.
14+
15+
__attribute__((optnone))
16+
int test1(int test1_a, int test1_b) {
17+
int test1_result = 0;
18+
// DexLabel('test1_start')
19+
test1_result = test1_a + test1_b; // DexExpectStepOrder(1)
20+
return test1_result; // DexExpectStepOrder(2)
21+
// DexLabel('test1_end')
22+
}
23+
// DexExpectWatchValue('test1_a', 3, from_line='test1_start', to_line='test1_end')
24+
// DexExpectWatchValue('test1_b', 4, from_line='test1_start', to_line='test1_end')
25+
// DexExpectWatchValue('test1_result', 0, 7, from_line='test1_start', to_line='test1_end')
26+
27+
__attribute__((optnone))
28+
int test2(int test2_a, int test2_b) {
29+
int test2_result = test2_a + test2_a + test2_a + test2_a; // DexExpectStepOrder(3)
30+
// DexLabel('test2_start')
31+
return test2_a << 2; // DexExpectStepOrder(4)
32+
// DexLabel('test2_end')
33+
}
34+
// DexExpectWatchValue('test2_a', 1, from_line='test2_start', to_line='test2_end')
35+
// DexExpectWatchValue('test2_b', 2, from_line='test2_start', to_line='test2_end')
36+
// DexExpectWatchValue('test2_result', 4, from_line='test2_start', to_line='test2_end')
37+
38+
__attribute__((optnone))
39+
int test3(int test3_a, int test3_b) {
40+
int test3_temp1 = 0, test3_temp2 = 0;
41+
// DexLabel('test3_start')
42+
test3_temp1 = test3_a + 5; // DexExpectStepOrder(5)
43+
test3_temp2 = test3_b + 5; // DexExpectStepOrder(6)
44+
if (test3_temp1 > test3_temp2) { // DexExpectStepOrder(7)
45+
test3_temp1 *= test3_temp2; // DexUnreachable()
46+
}
47+
return test3_temp1; // DexExpectStepOrder(8)
48+
// DexLabel('test3_end')
49+
}
50+
// DexExpectWatchValue('test3_a', 5, from_line='test3_start', to_line='test3_end')
51+
// DexExpectWatchValue('test3_b', 6, from_line='test3_start', to_line='test3_end')
52+
// DexExpectWatchValue('test3_temp1', 0, 10, from_line='test3_start', to_line='test3_end')
53+
// DexExpectWatchValue('test3_temp2', 0, 11, from_line='test3_start', to_line='test3_end')
54+
55+
unsigned num_iterations = 4;
56+
57+
__attribute__((optnone))
58+
int test4(int test4_a, int test4_b) {
59+
int val1 = 0, val2 = 0;
60+
// DexLabel('test4_start')
61+
62+
val1 = (test4_a > test4_b) ? test4_a : test4_b; // DexExpectStepOrder(9)
63+
val2 = val1;
64+
val2 += val1; // DexExpectStepOrder(10)
65+
66+
for (unsigned i=0; i != num_iterations; ++i) { // DexExpectStepOrder(11, 13, 15, 17, 19)
67+
val1--;
68+
val2 += i;
69+
if (val2 % 2 == 0) // DexExpectStepOrder(12, 14, 16, 18)
70+
val2 /= 2;
71+
}
72+
73+
return (val1 > val2) ? val2 : val1; // DexExpectStepOrder(20)
74+
// DexLabel('test4_end')
75+
}
76+
// DexExpectWatchValue('test4_a', 1, from_line='test4_start', to_line='test4_end')
77+
// DexExpectWatchValue('test4_b', 9, from_line='test4_start', to_line='test4_end')
78+
// DexExpectWatchValue('val1', 0, 9, 8, 7, 6, 5, from_line='test4_start', to_line='test4_end')
79+
// DexExpectWatchValue('val2', 0, 9, 18, 9, 10, 5, 7, 10, 5, 9, from_line='test4_start', to_line='test4_end')
80+
81+
__attribute__((optnone))
82+
int test5(int test5_val) {
83+
int c = 1; // DexExpectStepOrder(21)
84+
// DexLabel('test5_start')
85+
if (test5_val) // DexExpectStepOrder(22)
86+
c = 5; // DexExpectStepOrder(23)
87+
return c ? test5_val : test5_val; // DexExpectStepOrder(24)
88+
// DexLabel('test5_end')
89+
}
90+
// DexExpectWatchValue('test5_val', 7, from_line='test5_start', to_line='test5_end')
91+
// DexExpectWatchValue('c', 1, 5, from_line='test5_start', to_line='test5_end')
92+
93+
int main() {
94+
int main_result = 0;
95+
// DexLabel('main_start')
96+
main_result = test1(3,4);
97+
main_result += test2(1,2);
98+
main_result += test3(5,6);
99+
main_result += test4(1,9);
100+
main_result += test5(7);
101+
return main_result;
102+
// DexLabel('main_end')
103+
}
104+
// DexExpectWatchValue('main_result', 0, 7, 11, 21, 26, 33, from_line='main_start', to_line='main_end')
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// RUN: %dexter --fail-lt 1.0 -w \
2+
// RUN: --builder 'clang' --debugger 'lldb' \
3+
// RUN: --cflags "-g -O2" -v -- %s
4+
// RUN: %dexter --fail-lt 1.0 -w \
5+
// RUN: --builder 'clang' --debugger 'lldb' \
6+
// RUN: --cflags "-g -O0" -- %s
7+
8+
// REQUIRES: lldb
9+
// UNSUPPORTED: system-windows
10+
11+
//// Check that the debugging experience with __attribute__((optnone)) at O2
12+
//// matches O0. Test simple structs and methods.
13+
14+
long a_global_ptr[] = { 0xCAFEBABEL, 0xFEEDBEEFL };
15+
16+
namespace {
17+
18+
struct A {
19+
int a;
20+
float b;
21+
22+
enum B {
23+
A_VALUE = 0x1,
24+
B_VALUE = 0x2
25+
};
26+
27+
struct some_data {
28+
enum B other_b;
29+
enum B other_other_b;
30+
};
31+
32+
struct other_data {
33+
union {
34+
void *raw_ptr;
35+
long *long_ptr;
36+
float *float_ptr;
37+
} a;
38+
struct some_data b;
39+
struct some_data c;
40+
};
41+
private:
42+
struct other_data _data;
43+
44+
public:
45+
struct other_data *getOtherData() { return &_data; }
46+
47+
__attribute__((always_inline,nodebug))
48+
void setSomeData1(A::B value, A::B other_value) {
49+
struct other_data *data = getOtherData();
50+
data->b.other_b = value;
51+
data->b.other_other_b = other_value;
52+
}
53+
54+
__attribute__((always_inline))
55+
void setSomeData2(A::B value, A::B other_value) {
56+
struct other_data *data = getOtherData();
57+
data->c.other_b = value;
58+
data->c.other_other_b = other_value;
59+
}
60+
61+
void setOtherData() {
62+
setSomeData2(A_VALUE, B_VALUE);
63+
getOtherData()->a.long_ptr = &a_global_ptr[0];
64+
}
65+
66+
__attribute__((optnone))
67+
A() {
68+
__builtin_memset(this, 0xFF, sizeof(*this));
69+
} //DexLabel('break_0')
70+
// DexExpectWatchValue('a', '-1', on_line='break_0')
71+
//// Check b is NaN by comparing it to itself.
72+
// DexExpectWatchValue('this->b == this->b', 'false', on_line='break_0')
73+
// DexExpectWatchValue('_data.a.raw_ptr == -1', 'true', on_line='break_0')
74+
// DexExpectWatchValue('_data.a.float_ptr == -1', 'true', on_line='break_0')
75+
// DexExpectWatchValue('_data.a.float_ptr == -1', 'true', on_line='break_0')
76+
// DexExpectWatchValue('a_global_ptr[0]', 0xcafebabe, on_line='break_0')
77+
// DexExpectWatchValue('a_global_ptr[1]', 0xfeedbeef, on_line='break_0')
78+
79+
__attribute__((optnone))
80+
~A() {
81+
*getOtherData()->a.long_ptr = 0xADDF00DL;
82+
} //DexLabel('break_1')
83+
// DexExpectWatchValue('_data.a.raw_ptr == a_global_ptr', 'true', on_line='break_1')
84+
// DexExpectWatchValue('a_global_ptr[0]', 0xaddf00d, on_line='break_1')
85+
86+
__attribute__((optnone))
87+
long getData() {
88+
setSomeData1(B_VALUE, A_VALUE);
89+
setOtherData();
90+
return getOtherData()->a.long_ptr[1]; //DexLabel('break_2')
91+
}
92+
// DexExpectWatchValue('_data.b.other_b', 'B_VALUE', on_line='break_2')
93+
// DexExpectWatchValue('_data.b.other_other_b', 'A_VALUE', on_line='break_2')
94+
};
95+
96+
} // anonymous namespace
97+
98+
int main() {
99+
int result = 0;
100+
{
101+
A a;
102+
result = a.getData();
103+
}
104+
return result;
105+
}

0 commit comments

Comments
 (0)