blob: f4910b1708345923ade415e31524a70681f3f3db [file]
/* Check that offloaded simd collapse(2) loops of the forms the SIMT path
does not handle take the one-lane fallback, and still compute the right
result. Each loop declares its iteration variables in the for-init, so
that they are private, not implicitly lastprivate, and only the one
construct under test can trigger the fallback. */
/* { dg-do run } */
/* { dg-additional-options "-O2 -fdump-tree-omplower-details" } */
/* { dg-xfail-if "--param=offload-gpu-mode=simt-directed needs the amdgcn SIMT series" { offload_target_amdgcn } } */
/* { dg-additional-options "--param=offload-gpu-mode=simt-directed" { target offload_target_amdgcn } } */
#include <stdlib.h>
#define N 37
#define M 23
int a[N * M];
#pragma omp begin declare target
__attribute__((noinline)) static int
f (int x)
{
return 3 * x + 1;
}
#pragma omp end declare target
static void
clear (void)
{
for (int x = 0; x < N * M; x++)
a[x] = -1;
}
/* A non-rectangular composite distribute/for simd collapse(2) ICEs in the
amdgcn offload compiler (expand_fn_using_insn), in mainline too, so use
a plain simd in a target region. */
static void
non_rect (void)
{
#pragma omp target map(tofrom: a)
#pragma omp simd collapse(2)
for (int i = 0; i < M; i++)
for (int j = i; j < M; j++)
a[i * M + j] = i + j;
}
/* Start from a pointer parameter: initializing P from the mapped global
array A itself ICEs in scan_omp_1_op, in mainline too. */
static void
pointer_iv (int *b)
{
#pragma omp target teams distribute parallel for simd collapse(2) \
map(tofrom: b[:N * M])
for (int *p = b; p < b + N * M; p += M)
for (int j = 0; j < M; j++)
p[j] = j;
}
static int
lastprivate_clause (void)
{
int x = -1;
#pragma omp target teams distribute parallel for simd collapse(2) \
map(tofrom: a) lastprivate(x)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
x = i * M + j;
a[x] = x;
}
return x;
}
/* A non-iterator linear clause is not allowed on a construct combined
with distribute, so use a plain simd in a target region. K is stepped
in the body: without that, GCC leaves it at its initial value in a
collapsed loop, in mainline too. */
static void
linear_clause (void)
{
int k = 0;
#pragma omp target map(tofrom: a)
#pragma omp simd collapse(2) linear(k: 1)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
a[i * M + j] = k;
k++;
}
}
/* A reduction on a non-composite loop takes the fallback. The simd loop
is nested in a loop construct, the shape on which a SIMT reduction hangs
on GCN. Reductions on composite loops take the SIMT path, see
simt-collapse-reduction-1.c. */
static int
reduction_non_composite (void)
{
int s = 0;
#pragma omp target parallel for reduction(+: s)
for (int o = 0; o < 4; o++)
{
int t = 0;
#pragma omp simd collapse(2) reduction(+: t)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
t += i * M + j + o;
s += t;
}
return s;
}
static void
call (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: a)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
a[i * M + j] = f (i * M + j);
}
static void
addressable_local (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: a)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int t = i * M + j;
int *q = &t;
a[i * M + j] = *q + 1;
}
}
static void
int_div (int d)
{
#pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: a)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
a[i * M + j] = (i * M + j) / d;
}
static void
int_mod (int d)
{
#pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: a)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
a[i * M + j] = (i * M + j) % d;
}
/* A pointer subtraction divides by the element size (EXACT_DIV_EXPR). */
static void
ptr_diff (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) map(tofrom: a)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int *p = &a[i * M + j];
a[i * M + j] = p - a;
}
}
/* The value of an iteration variable stored through a loop-invariant
pointer, then read back: under SIMT, the lanes would all store to the
same location and read one lane's value. The store to A may alias *P, so
*P is read back from memory. The store to *P comes first in the body, so
that compiling with -g puts a debug marker before it (see
simt-collapse-fallback-4.c). */
static void
iv_store (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
*p = j;
a[i * M + j] = -2;
a[i * M + *p] = i * M + *p;
}
}
/* The same store inside a GIMPLE_BIND: a declaration in the loop body puts
the body in a bind after the iteration variable copies. */
static void
iv_store_bind (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
*p = j;
int t = i * M;
a[t + j] = -2;
a[t + *p] = t + *p;
}
}
/* The same store two binds deep: the inner bind is the first statement of
the outer one, because a declaration without an initializer gives no
statement. */
static void
iv_store_nested_bind (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int t;
{
int u = i * M;
*p = j;
t = u;
a[t + j] = -2;
a[t + *p] = t + *p;
}
}
}
/* The loop construct maps to simd here, and the gimplifier wraps its body
in a bind even without a declaration. */
static void
iv_store_loop (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp loop bind(thread) collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
*p = j;
a[i * M + j] = -2;
a[i * M + *p] = i * M + *p;
}
}
/* The store follows an inner block, so the walk must carry on with the
enclosing sequence after leaving the block. First, a block with
IV-derived assignments. */
static void
iv_store_after_bind (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int t;
{
int u = i * M;
t = u;
}
*p = j;
a[t + j] = -2;
a[t + *p] = t + *p;
}
}
/* A block with only a declaration has an empty body. */
static void
iv_store_after_empty_bind (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int t = i * M;
{
int u;
}
*p = j;
a[t + j] = -2;
a[t + *p] = t + *p;
}
}
/* The same as iv_store_after_bind, through the loop construct. */
static void
iv_store_after_bind_loop (int *p)
{
#pragma omp target map(tofrom: a, p[:1])
#pragma omp loop bind(thread) collapse(2)
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
{
int t;
{
int u = i * M;
t = u;
}
*p = j;
a[t + j] = -2;
a[t + *p] = t + *p;
}
}
/* In a non-composite loop, each lane steps the innermost variable by the
step times the SIMT width and tests it against its bound, so an unsigned
variable counting down can wrap past 0 and keep running. Check each
spelling of the condition: > (the front ends also turn != 0 into it),
>= and != on a non-zero bound. */
static void
unsigned_down_gt (void)
{
#pragma omp target map(tofrom: a)
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (unsigned j = M; j > 0; j--)
a[i * M + j - 1] = i * M + j - 1;
}
static void
unsigned_down_ge (void)
{
#pragma omp target map(tofrom: a)
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (unsigned j = M; j >= 1; j--)
a[i * M + j - 1] = i * M + j - 1;
}
static void
unsigned_down_ne (void)
{
#pragma omp target map(tofrom: a)
#pragma omp simd collapse(2)
for (int i = 0; i < N; i++)
for (unsigned j = M + 5; j != 5; j--)
a[i * M + j - 6] = i * M + j - 6;
}
/* In a non-composite loop, the lane offset and the SIMT step can wrap the
innermost variable when its bound is close (namely less than the step times
the SIMT width) to its type's maximum. If the variable is narrower than int,
even small loops can cause wrapping. B is indexed by the variable itself, so
that an iteration that doesn't exist shows up as an element that should be 0.
*/
#define NI 3
unsigned char b[NI][65536];
static void
narrow_uchar (void)
{
#pragma omp target map(tofrom: b)
#pragma omp simd collapse(2)
for (int i = 0; i < NI; i++)
for (unsigned char j = 0; j < 200; j += 5)
b[i][j] = i + 1;
}
static void
narrow_uchar_runtime_step (int s)
{
#pragma omp target map(tofrom: b)
#pragma omp simd collapse(2)
for (int i = 0; i < NI; i++)
for (unsigned char j = 0; j < 200; j += s)
b[i][j] = i + 1;
}
static void
narrow_uchar_high (void)
{
#pragma omp target map(tofrom: b)
#pragma omp simd collapse(2)
for (int i = 0; i < NI; i++)
for (unsigned char j = 200; j < 250; j++)
b[i][j] = i + 1;
}
static void
narrow_ushort_runtime_step (int s)
{
#pragma omp target map(tofrom: b)
#pragma omp simd collapse(2)
for (int i = 0; i < NI; i++)
for (unsigned short j = 0; j < 60000; j += s)
b[i][j] = i + 1;
}
/* In a composite loop, the lane offset and the SIMT step can wrap the
logical iteration variable (which has the precision of the widest
iteration variable) when it gets close (namely less than SIMT width) to its
type's maximum. If every variable is narrower than int, this is likely to
happen even for small loops. Even when the widest variable is not narrower
than int, a narrow innermost variable still has the lane offset and the SIMT
step computed in its own type, so the loop takes the fallback too. */
static void
narrow_composite_uchar (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) \
map(tofrom: b)
for (unsigned char i = 0; i < 15; i++)
for (unsigned char j = 0; j < 15; j++)
b[0][i * 15 + j]++;
}
static void
narrow_composite_ushort (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) \
map(tofrom: b)
for (unsigned short i = 0; i < 257; i++)
for (unsigned short j = 0; j < 255; j++)
b[0][i * 255 + j]++;
}
static void
narrow_composite_inner_uchar (void)
{
#pragma omp target teams distribute parallel for simd collapse(2) \
map(tofrom: b)
for (int i = 0; i < 3; i++)
for (unsigned char j = 0; j < 100; j++)
b[0][i * 100 + j]++;
}
/* Check that B[0][J] is 1 for J below N, and that B is 0 elsewhere, then
clear B. */
static void
check_narrow_composite (int n)
{
for (int i = 0; i < NI; i++)
for (int j = 0; j < 65536; j++)
{
if (b[i][j] != (i == 0 && j < n))
abort ();
b[i][j] = 0;
}
}
/* Check that B[I][J] is I + 1 for J in N1, N1 + STEP, ... below N2, and 0
elsewhere, then clear B. */
static void
check_narrow (int n1, int n2, int step)
{
for (int i = 0; i < NI; i++)
for (int j = 0; j < 65536; j++)
{
int in = j >= n1 && j < n2 && (j - n1) % step == 0;
if (b[i][j] != (in ? i + 1 : 0))
abort ();
b[i][j] = 0;
}
}
volatile int v5 = 5, v1100 = 1100;
int
main (void)
{
clear ();
non_rect ();
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
if (a[i * M + j] != (i < M && j >= i ? i + j : -1))
abort ();
clear ();
pointer_iv (a);
for (int x = 0; x < N * M; x++)
if (a[x] != x % M)
abort ();
clear ();
if (lastprivate_clause () != N * M - 1)
abort ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
linear_clause ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
if (reduction_non_composite ()
!= 4 * (N * M * (N * M - 1) / 2) + N * M * 6)
abort ();
clear ();
call ();
for (int x = 0; x < N * M; x++)
if (a[x] != 3 * x + 1)
abort ();
clear ();
addressable_local ();
for (int x = 0; x < N * M; x++)
if (a[x] != x + 1)
abort ();
clear ();
int_div (3);
for (int x = 0; x < N * M; x++)
if (a[x] != x / 3)
abort ();
clear ();
int_mod (3);
for (int x = 0; x < N * M; x++)
if (a[x] != x % 3)
abort ();
clear ();
ptr_diff ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
int t = -1;
iv_store (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_bind (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_nested_bind (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_loop (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_after_bind (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_after_empty_bind (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
iv_store_after_bind_loop (&t);
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
unsigned_down_gt ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
unsigned_down_ge ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
clear ();
unsigned_down_ne ();
for (int x = 0; x < N * M; x++)
if (a[x] != x)
abort ();
narrow_uchar ();
check_narrow (0, 200, 5);
narrow_uchar_runtime_step (v5);
check_narrow (0, 200, 5);
narrow_uchar_high ();
check_narrow (200, 250, 1);
narrow_ushort_runtime_step (v1100);
check_narrow (0, 60000, 1100);
narrow_composite_uchar ();
check_narrow_composite (15 * 15);
narrow_composite_ushort ();
check_narrow_composite (257 * 255);
narrow_composite_inner_uchar ();
check_narrow_composite (3 * 100);
return 0;
}
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: non-rectangular loop" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: pointer iterator" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: lastprivate clause" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: linear clause" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: reduction clause on non-composite loop" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: external function call" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: address-taken local variable" 1 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: integer division or modulo" 3 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: non-composite loop with unsigned innermost IV counting down" 3 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: loop with innermost IV narrower than int" 7 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path: iteration variable value stored to memory" 7 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-times "collapsed loop cannot take the SIMT path" 27 "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */
/* { dg-final { scan-tree-dump-not "_simt_" "omplower" { target { offload_target_amdgcn || offload_target_nvptx } } } } */