Skip to content

Commit 2afff59

Browse files
kasaurovspatrang
authored andcommitted
Update some gdb.rocm tests to support optimized code
Adapt several gdb.rocm tests so they pass under -O1/-O2/-O3: - Apply __attribute__((optnone)) to device functions whose locals or call structure the tests rely on, and to host main() where its store-then-exit pattern would otherwise be tail-optimised. - Make breakpoint-anchor helper functions ("done", "foo", "bar") non-static and noinline, with an asm volatile body, so the symbol survives the optimiser and the breakpoint can be inserted. - Mark local variables volatile where the test reads them via convenience variables or takes their address. - Restore "__global__ void" qualifier on device-attach kernel, and mark its spin-loop guard volatile so the loop survives optimisation. - Relax regex/line-number expectations under optimisation in deep-stack, hip-builtin-variables, snapshot-objfile-on- load, and step-schedlock-spurious-waves. - Brace expr-substitutions in deep-stack and until-tests (tclint hygiene). Co-Authored-By: Luis Machado <luis.machado@amd.com> Change-Id: I750d64e842413429e281bb8f7d85c4f46319a627 Signed-off-by: Sarang Patrange <spatrang@amd.com>
1 parent 7abe298 commit 2afff59

30 files changed

Lines changed: 160 additions & 76 deletions

gdb/testsuite/gdb.rocm/alu-exceptions.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,62 +26,63 @@ enable_alu_exceptions ()
2626
{
2727
/* By default, ALU exceptions are not enabled. Break here and use the
2828
debugger to enable exceptions. */
29+
volatile int i = 0;
2930
}
3031

3132
__global__ void
3233
raise_invalid ()
3334
{
3435
enable_alu_exceptions ();
3536
volatile float a = -1;
36-
float b = sqrt (a); /* Break here for invalid. */
37+
volatile float b = sqrt (a); /* Break here for invalid. */
3738
}
3839

3940
__global__ void
4041
raise_denorm ()
4142
{
4243
enable_alu_exceptions ();
4344
volatile float inp = std::numeric_limits<float>::denorm_min ();
44-
float b = inp * inp; /* Break here for denorm. */
45+
volatile float b = inp * inp; /* Break here for denorm. */
4546
}
4647

4748
__global__ void
4849
raise_float_div0 ()
4950
{
5051
enable_alu_exceptions ();
5152
volatile float a = 1, b = 0;
52-
float c = a / b; /* Break here for float_div0. */
53+
volatile float c = a / b; /* Break here for float_div0. */
5354
}
5455

5556
__global__ void
5657
raise_overflow ()
5758
{
5859
enable_alu_exceptions ();
5960
volatile float max = std::numeric_limits<float>::max ();
60-
float b = max * 2.0f; /* Break here for overflow. */
61+
volatile float b = max * 2.0f; /* Break here for overflow. */
6162
}
6263

6364
__global__ void
6465
raise_underflow ()
6566
{
6667
enable_alu_exceptions ();
6768
volatile float min = std::numeric_limits<float>::min ();
68-
float c = min * min; /* Break here for underflow. */
69+
volatile float c = min * min; /* Break here for underflow. */
6970
}
7071

7172
__global__ void
7273
raise_inexact ()
7374
{
7475
enable_alu_exceptions ();
7576
volatile float f = 1.1f;
76-
float r = f * f; /* Break here for inexact. */
77+
volatile float r = f * f; /* Break here for inexact. */
7778
}
7879

7980
__global__ void
8081
raise_int_div0 ()
8182
{
8283
enable_alu_exceptions ();
8384
volatile int a = 1, b = 0;
84-
int c = a / b; /* Break here for int_div0. */
85+
volatile int c = a / b; /* Break here for int_div0. */
8586
}
8687

8788
int

gdb/testsuite/gdb.rocm/corefile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
} while (0)
3535

3636
__device__ int
37-
bar ()
37+
bar () __attribute__((optnone))
3838
{
3939
return threadIdx.x;
4040
}
4141

42-
__device__ int
42+
__device__ int __attribute__((optnone))
4343
baz ()
4444
{
4545
return 0;

gdb/testsuite/gdb.rocm/deep-stack.exp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ with_rocm_gpu_lock {
3838
}
3939

4040
set line [gdb_get_line_number "break here"]
41+
if {[check_for_any_flags {-O1 -O2 -O3}]} {
42+
# return is optimized out, stop one statement before.
43+
set line [expr {$line - 1}]
44+
}
4145
gdb_breakpoint "$line" allow-pending
4246

4347
gdb_test "continue" "Thread $decimal \"hip_deep\" hit Breakpoint $decimal.*"
@@ -53,7 +57,7 @@ with_rocm_gpu_lock {
5357
"#7 .* deep<6u> .*" \
5458
"#8 .* deep<7u> .*" \
5559
"#9 .* deep<8u> .*" \
56-
"#10 .* deep<9u> .*" \
57-
"#11 .* deep<10u> .*" \
58-
"#12 .* hip_deep .*"]
60+
"#10 .*deep<9u> .*" \
61+
"#11 .*deep<10u> .*" \
62+
"#12 .*hip_deep .*"]
5963
}

gdb/testsuite/gdb.rocm/deref-scoped-pointer.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@
3232
} \
3333
}
3434

35-
__device__ static void
35+
/* Non-static + noinline so a line-breakpoint at the "done ()" call
36+
site below has a stable instruction address under -O1/-O2/-O3.
37+
Without these, the empty body is inlined into its caller, so
38+
there is no instruction at the call-site line for GDB to insert
39+
the breakpoint at. */
40+
41+
__device__ void __attribute__((noinline))
3642
done ()
3743
{
44+
asm volatile ("" : : : "memory");
3845
}
3946

4047
/* This is initialized by the kernel function below. */
@@ -43,10 +50,16 @@ __device__ size_t *global_ptr = nullptr;
4350
/* This is initialized by GDB. */
4451
__device__ size_t *global_ptr2 = nullptr;
4552

46-
__global__ void
53+
/* kernel() is marked __attribute__((optnone)) and "local_var" is
54+
declared "volatile" so the variable remains addressable from the
55+
"done" call site through the end of the function under
56+
-O1/-O2/-O3. The test takes the address of local_var via GDB
57+
expressions, which requires it to have an lvalue/storage. */
58+
59+
__global__ void __attribute__((optnone))
4760
kernel ()
4861
{
49-
size_t local_var = blockIdx.x * blockDim.x + threadIdx.x;
62+
volatile size_t local_var = blockIdx.x * blockDim.x + threadIdx.x;
5063

5164
/* This will be zero for the first HIP thread, or lane. */
5265
if (local_var == 0)

gdb/testsuite/gdb.rocm/device-attach.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bit_extract_kernel (uint32_t *C_d, const uint32_t *A_d, size_t N)
3737
{
3838
size_t offset = (blockIdx.x * blockDim.x + threadIdx.x);
3939
size_t stride = blockDim.x * gridDim.x;
40-
int loop_forever = 1;
40+
volatile int loop_forever = 1;
4141
int a = 9;
4242
while (loop_forever)
4343
a = 8;

gdb/testsuite/gdb.rocm/finish.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ struct WithStaticFields
104104
};
105105
__device__ int WithStaticFields::c = 12;
106106

107-
__device__ static Empty
107+
__device__ __attribute__((noinline)) Empty
108108
returnEmpty ()
109109
{
110110
return {};
111111
}
112112

113113
template<int N>
114-
__device__ static Custom<char, N>
114+
__device__ __attribute__((noinline)) Custom<char, N>
115115
returnSized ()
116116
{
117117
Custom<char, N> ret;
@@ -122,7 +122,7 @@ returnSized ()
122122
}
123123

124124
template<int N>
125-
__device__ static Custom<Union, N>
125+
__device__ __attribute__((noinline)) Custom<Union, N>
126126
returnCustomWithUnion ()
127127
{
128128
Custom<Union, N> ret;
@@ -131,15 +131,15 @@ returnCustomWithUnion ()
131131
return ret;
132132
}
133133

134-
__device__ static FlexibleArrayMember
134+
__device__ __attribute__((noinline)) FlexibleArrayMember
135135
returnFlexibleArrayMember ()
136136
{
137137
return {
138138
.non_flexible = static_cast<int> (threadIdx.x)
139139
};
140140
}
141141

142-
__device__ static NotPackedWithBitField
142+
__device__ __attribute__((noinline)) NotPackedWithBitField
143143
returnNotPackedWithBitField ()
144144
{
145145
const int val = static_cast<int> (threadIdx.x);
@@ -157,7 +157,7 @@ returnNotPackedWithBitField ()
157157
};
158158
}
159159

160-
__device__ static PackedWithBitField
160+
__device__ __attribute__((noinline)) PackedWithBitField
161161
returnPackedWithBitField ()
162162
{
163163
const int val = static_cast<int> (threadIdx.x);
@@ -168,7 +168,7 @@ returnPackedWithBitField ()
168168
};
169169
}
170170

171-
__device__ static const Custom<char, 8> *
171+
__device__ __attribute__((noinline)) const Custom<char, 8> *
172172
returnPtr ()
173173
{
174174
static Custom<char, 8> vv {
@@ -177,13 +177,13 @@ returnPtr ()
177177
return &vv;
178178
}
179179

180-
__device__ static int *
180+
__device__ __attribute__((noinline)) int *
181181
returnPtr2 (int *a)
182182
{
183183
return a;
184184
}
185185

186-
__device__ static const Custom<char, 8> &
186+
__device__ __attribute__((noinline)) const Custom<char, 8> &
187187
returnRef ()
188188
{
189189
static Custom<char, 8> vv {
@@ -192,13 +192,13 @@ returnRef ()
192192
return vv;
193193
};
194194

195-
__device__ static int &
195+
__device__ __attribute__((noinline)) int &
196196
returnRef2 (int &a)
197197
{
198198
return a;
199199
}
200200

201-
__device__ static WithStaticFields
201+
__device__ __attribute__((noinline)) WithStaticFields
202202
returnWithStatic ()
203203
{
204204
OnlyStatic::something = 12;
@@ -210,7 +210,7 @@ returnWithStatic ()
210210
};
211211
}
212212

213-
__device__ static OnlyStatic
213+
__device__ __attribute__((noinline)) OnlyStatic
214214
returnEmptyWithStatic ()
215215
{
216216
OnlyStatic::something = 56;
@@ -230,7 +230,7 @@ returnMixedSmallSruct ()
230230

231231
__device__ int someGlobal = 42;
232232

233-
__global__ void
233+
__global__ void __attribute__((optnone))
234234
kernel ()
235235
{
236236
returnEmpty ();

gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu-execee.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
static void
1919
break_here_execee (void)
20-
{}
20+
{
21+
volatile int i = 0;
22+
}
2123

2224
int
2325
main (void)

gdb/testsuite/gdb.rocm/fork-exec-gpu-to-non-gpu-execer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ kernel1 ()
2929
__device__ static void
3030
break_here_execer ()
3131
{
32+
volatile int j = 0;
3233
}
3334

3435
__global__ static void

gdb/testsuite/gdb.rocm/hip-builtin-variables.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,41 +34,41 @@
3434
/* A function to be called by "kern ()", without any references to
3535
threadIdx, blockIdx, blockDim, gridDim, or warpSize variables. */
3636

37-
__device__ void
37+
__device__ void __attribute__((optnone))
3838
inner_func1 ()
3939
{
4040
return;
4141
}
4242

4343
/* A function to be called by "kern ()", with its own "blockIdx". */
4444

45-
__device__ void
45+
__device__ void __attribute__((optnone))
4646
inner_func2 ()
4747
{
4848
int blockIdx = 0x42424242;
4949
return;
5050
}
5151

52-
__global__ void
52+
__global__ void __attribute__((optnone))
5353
kern ()
5454
{
55-
const int thread_idx_x = threadIdx.x;
56-
const int thread_idx_y = threadIdx.y;
57-
const int thread_idx_z = threadIdx.z;
55+
volatile int thread_idx_x = threadIdx.x;
56+
volatile int thread_idx_y = threadIdx.y;
57+
volatile int thread_idx_z = threadIdx.z;
5858

59-
const int group_idx_x = blockIdx.x;
60-
const int group_idx_y = blockIdx.y;
61-
const int group_idx_z = blockIdx.z;
59+
volatile int group_idx_x = blockIdx.x;
60+
volatile int group_idx_y = blockIdx.y;
61+
volatile int group_idx_z = blockIdx.z;
6262

63-
const int group_size_x = blockDim.x;
64-
const int group_size_y = blockDim.y;
65-
const int group_size_z = blockDim.z;
63+
volatile int group_size_x = blockDim.x;
64+
volatile int group_size_y = blockDim.y;
65+
volatile int group_size_z = blockDim.z;
6666

67-
const int grid_size_x = gridDim.x;
68-
const int grid_size_y = gridDim.y;
69-
const int grid_size_z = gridDim.z;
67+
volatile int grid_size_x = gridDim.x;
68+
volatile int grid_size_y = gridDim.y;
69+
volatile int grid_size_z = gridDim.z;
7070

71-
const int wave_size = warpSize;
71+
volatile int wave_size = warpSize;
7272

7373
/* Break here. */
7474
inner_func1 ();

gdb/testsuite/gdb.rocm/hip-builtin-variables.exp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,16 @@ with_rocm_gpu_lock {
226226
# Get into inner_func2() with its own local blockIdx variable.
227227
gdb_test "step" "inner_func2 \\(\\) at.*" \
228228
"step into inner_func2() with its own blockIdx"
229-
gdb_test "next" "$::decimal\[ \t\]+return;" \
229+
# Accept landing on either the "return;" statement or the
230+
# closing brace. At -O0 Clang emits a separate line entry
231+
# for the explicit "return;", so "next" lands there. Under
232+
# __attribute__((optnone)) (which we use to keep the local
233+
# blockIdx observable at -O1/-O2/-O3) Clang merges the line
234+
# entry of the "return;" with the function's closing brace,
235+
# so "next" lands on the brace instead. Either is fine for
236+
# this test - the point is just to step past the local
237+
# initialisation before reading blockIdx.
238+
gdb_test "next" "$::decimal\[ \t\]+(return;|\})" \
230239
"let the initialization happen"
231240
gdb_test "p/x blockIdx" "0x42424242" \
232241
"local blockIdx shadowing the built-in"

0 commit comments

Comments
 (0)