blob: 622beddce0d2911d01544fb9577b94e0b72c2fbe [file]
/* Check that offloaded simd collapse(n) loops that are not composite get a
SIMT region and compute the right result. The whole nest runs in every
lane, and each row is split over the lanes. Covered: inner trip counts
below, equal to and above the vector length, runtime bounds, collapse(3),
a non-unit outer and inner step, a negative inner step, unsigned
iteration variables, runtime inner steps with iteration variables of
different widths, and safelen and simdlen clauses.
Each loop declares its iteration variables in the for-init, so that they
are private, not implicitly lastprivate. The result array has guard
elements on both sides, which catch writes outside the iteration space. */
/* { dg-do run } */
/* { dg-additional-options "-O2" } */
/* { 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 } } */
/* { dg-additional-options "-foffload-options=-fdump-tree-optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } */
#include <stdlib.h>
#define PAD 256
#define MAXN 8192
int buf[MAXN + 2 * PAD];
static void
clear (void)
{
for (int x = 0; x < MAXN + 2 * PAD; x++)
buf[x] = -1;
}
/* BUF[PAD + I] must be EXPECT (I) for the I in [0, N), and -1 elsewhere. */
static void
check (int n, int (*expect) (int))
{
for (int x = 0; x < MAXN + 2 * PAD; x++)
{
int i = x - PAD;
int want = (i >= 0 && i < n) ? expect (i) : -1;
if (buf[x] != want)
abort ();
}
}
static int
ident (int i)
{
return i * 7 + 3;
}
static void
t2_default (void)
{
#pragma omp target simd collapse(2) map(tofrom: buf)
for (int i = 0; i < 61; i++)
for (int j = 0; j < 67; j++)
buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3;
check (61 * 67, ident);
}
/* Runtime bounds, so one loop covers all shapes. */
static void
t2_gen (int n, int m)
{
#pragma omp target simd collapse(2) map(tofrom: buf) firstprivate(n, m)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
buf[PAD + i * m + j] = (i * m + j) * 7 + 3;
check (n * m, ident);
}
static void
t3_default (void)
{
#pragma omp target simd collapse(3) map(tofrom: buf)
for (int i = 0; i < 9; i++)
for (int j = 0; j < 13; j++)
for (int k = 0; k < 19; k++)
buf[PAD + (i * 13 + j) * 19 + k] = ((i * 13 + j) * 19 + k) * 7 + 3;
check (9 * 13 * 19, ident);
}
/* Inner step 3: the lanes must be offset by 3 * lane. The index has no
division, which would take the one-lane fallback, so the rows have
holes. */
static int
step3_expect (int i)
{
return i % 3 == 0 ? ident (i) : -1;
}
static void
t2_step3 (void)
{
#pragma omp target simd collapse(2) map(tofrom: buf)
for (int i = 0; i < 19; i++)
for (int j = 0; j < 200; j += 3)
buf[PAD + i * 201 + j] = (i * 201 + j) * 7 + 3;
check (19 * 201, step3_expect);
}
/* Outer loop with a non-unit start and step. As above, the index has no
division, so the rows have holes. */
static int
outer_step_expect (int i)
{
return (i / 70) % 5 == 0 ? ident (i) : -1;
}
static void
t2_outer_step (void)
{
#pragma omp target simd collapse(2) map(tofrom: buf)
for (int i = 5; i < 100; i += 5)
for (int j = 0; j < 70; j++)
buf[PAD + (i - 5) * 70 + j] = ((i - 5) * 70 + j) * 7 + 3;
check (91 * 70, outer_step_expect);
}
static void
t2_down (void)
{
#pragma omp target simd collapse(2) map(tofrom: buf)
for (int i = 0; i < 61; i++)
for (int j = 66; j >= 0; j--)
buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3;
check (61 * 67, ident);
}
static void
t2_unsigned (void)
{
#pragma omp target simd collapse(2) map(tofrom: buf)
for (unsigned i = 0; i < 61; i++)
for (unsigned j = 0; j < 67; j++)
buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3;
check (61 * 67, ident);
}
/* Runtime inner step: the rows are iterated by an alternate IV, which
steps by the vector length under SIMT. Its type must hold the row trip
count, also when the outer IV is narrower than the inner one (an
unsigned char outer IV with 300 iterations per row). An unsigned char
inner IV makes the loop take the one-lane fallback instead (see
simt-collapse-fallback-1.c); t2_rstep_uchar_inner checks the result. */
static int rstep_m, rstep_s;
static int
rstep_expect (int i)
{
return (i % rstep_m) % rstep_s == 0 ? ident (i) : -1;
}
static void
t2_rstep (int n, int m, int s)
{
rstep_m = m;
rstep_s = s;
#pragma omp target simd collapse(2) map(tofrom: buf) firstprivate(n, m, s)
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j += s)
buf[PAD + i * m + j] = (i * m + j) * 7 + 3;
check (n * m, rstep_expect);
}
static void
t2_rstep_uchar_outer (int n, int m, int s)
{
rstep_m = m;
rstep_s = s;
#pragma omp target simd collapse(2) map(tofrom: buf) firstprivate(n, m, s)
for (unsigned char i = 0; i < n; i++)
for (int j = 0; j < m; j += s)
buf[PAD + i * m + j] = (i * m + j) * 7 + 3;
check (n * m, rstep_expect);
}
static void
t2_rstep_uchar_inner (int n, int m, int s)
{
rstep_m = m;
rstep_s = s;
#pragma omp target simd collapse(2) map(tofrom: buf) firstprivate(n, m, s)
for (int i = 0; i < n; i++)
for (unsigned char j = 0; j < m; j += s)
buf[PAD + i * m + j] = (i * m + j) * 7 + 3;
check (n * m, rstep_expect);
}
/* safelen and simdlen limit the number of active lanes. */
#define DO_PRAGMA(X) _Pragma (#X)
#define T2_CLAUSES(NAME, ...) \
static void \
NAME (void) \
{ \
DO_PRAGMA (omp target simd collapse(2) map(tofrom: buf) __VA_ARGS__) \
for (int i = 0; i < 61; i++) \
for (int j = 0; j < 67; j++) \
buf[PAD + i * 67 + j] = (i * 67 + j) * 7 + 3; \
check (61 * 67, ident); \
}
T2_CLAUSES (t2_safelen_2, safelen(2))
T2_CLAUSES (t2_safelen_8, safelen(8))
T2_CLAUSES (t2_safelen_33, safelen(33))
T2_CLAUSES (t2_simdlen_4, simdlen(4))
#define RUN(F) do { clear (); F; } while (0)
int
main (void)
{
RUN (t2_default ());
RUN (t2_gen (1, 1));
RUN (t2_gen (5, 3));
RUN (t2_gen (3, 64));
RUN (t2_gen (7, 65));
RUN (t2_gen (2, 200));
RUN (t2_gen (100, 1));
RUN (t2_gen (0, 10));
RUN (t2_gen (10, 0));
RUN (t3_default ());
RUN (t2_step3 ());
RUN (t2_outer_step ());
RUN (t2_down ());
RUN (t2_unsigned ());
RUN (t2_rstep (7, 65, 1));
RUN (t2_rstep (7, 200, 3));
RUN (t2_rstep_uchar_outer (5, 200, 1));
RUN (t2_rstep_uchar_outer (5, 300, 1));
RUN (t2_rstep_uchar_inner (5, 200, 1));
RUN (t2_rstep_uchar_inner (5, 255, 1));
RUN (t2_safelen_2 ());
RUN (t2_safelen_8 ());
RUN (t2_safelen_33 ());
RUN (t2_simdlen_4 ());
return 0;
}
/* { dg-final { scan-offload-tree-dump "GOMP_SIMT_ENTER" "optimized" { target { offload_target_amdgcn || offload_target_nvptx } } } } */