static void
process_asm (FILE *in, FILE *out, FILE *cfile)
{
- int fn_count = 0, var_count = 0, dims_count = 0;
- struct obstack fns_os, vars_os, varsizes_os, dims_os;
+ int fn_count = 0, var_count = 0, dims_count = 0, regcount_count = 0;
+ struct obstack fns_os, vars_os, varsizes_os, dims_os, regcounts_os;
obstack_init (&fns_os);
obstack_init (&vars_os);
obstack_init (&varsizes_os);
obstack_init (&dims_os);
+ obstack_init (®counts_os);
struct oaccdims
{
char *name;
} dim;
+ struct regcount
+ {
+ int sgpr_count;
+ int vgpr_count;
+ char *kernel_name;
+ } regcount;
+
/* Always add _init_array and _fini_array as kernels. */
obstack_ptr_grow (&fns_os, xstrdup ("_init_array"));
obstack_ptr_grow (&fns_os, xstrdup ("_fini_array"));
fn_count += 2;
char buf[1000];
- enum { IN_CODE, IN_VARS, IN_FUNCS } state = IN_CODE;
+ enum { IN_CODE, IN_AMD_KERNEL_CODE_T, IN_VARS, IN_FUNCS } state = IN_CODE;
while (fgets (buf, sizeof (buf), in))
{
switch (state)
obstack_grow (&dims_os, &dim, sizeof (dim));
dims_count++;
}
+ else if (sscanf (buf, " .amdgpu_hsa_kernel %ms\n",
+ ®count.kernel_name) == 1)
+ break;
+
+ break;
+ }
+ case IN_AMD_KERNEL_CODE_T:
+ {
+ gcc_assert (regcount.kernel_name);
+ if (sscanf (buf, " wavefront_sgpr_count = %d\n",
+ ®count.sgpr_count) == 1)
+ break;
+ else if (sscanf (buf, " workitem_vgpr_count = %d\n",
+ ®count.vgpr_count) == 1)
+ break;
+
break;
}
case IN_VARS:
state = IN_VARS;
else if (sscanf (buf, " .section .gnu.offload_funcs%c", &dummy) > 0)
state = IN_FUNCS;
+ else if (sscanf (buf, " .amd_kernel_code_%c", &dummy) > 0)
+ {
+ state = IN_AMD_KERNEL_CODE_T;
+ regcount.sgpr_count = regcount.vgpr_count = -1;
+ }
else if (sscanf (buf, " .section %c", &dummy) > 0
|| sscanf (buf, " .text%c", &dummy) > 0
|| sscanf (buf, " .bss%c", &dummy) > 0
|| sscanf (buf, " .data%c", &dummy) > 0
|| sscanf (buf, " .ident %c", &dummy) > 0)
state = IN_CODE;
+ else if (sscanf (buf, " .end_amd_kernel_code_%c", &dummy) > 0)
+ {
+ state = IN_CODE;
+ gcc_assert (regcount.kernel_name != NULL
+ && regcount.sgpr_count >= 0
+ && regcount.vgpr_count >= 0);
+ obstack_grow (®counts_os, ®count, sizeof (regcount));
+ regcount_count++;
+ regcount.kernel_name = NULL;
+ regcount.sgpr_count = regcount.vgpr_count = -1;
+ }
- if (state == IN_CODE)
+ if (state == IN_CODE || state == IN_AMD_KERNEL_CODE_T)
fputs (buf, out);
}
char **fns = XOBFINISH (&fns_os, char **);
struct oaccdims *dims = XOBFINISH (&dims_os, struct oaccdims *);
+ struct regcount *regcounts = XOBFINISH (®counts_os, struct regcount *);
fprintf (cfile, "#include <stdlib.h>\n");
fprintf (cfile, "#include <stdbool.h>\n\n");
fprintf (cfile, "static const struct hsa_kernel_description {\n"
" const char *name;\n"
" int oacc_dims[3];\n"
+ " int sgpr_count;\n"
+ " int vgpr_count;\n"
"} gcn_kernels[] = {\n ");
dim.d[0] = dim.d[1] = dim.d[2] = 0;
const char *comma;
{
/* Find if we recorded dimensions for this function. */
int *d = dim.d; /* Previously zeroed. */
+ int sgpr_count = 0;
+ int vgpr_count = 0;
for (int j = 0; j < dims_count; j++)
if (strcmp (fns[i], dims[j].name) == 0)
{
d = dims[j].d;
break;
}
+ for (int j = 0; j < regcount_count; j++)
+ if (strcmp (fns[i], regcounts[j].kernel_name) == 0)
+ {
+ sgpr_count = regcounts[j].sgpr_count;
+ vgpr_count = regcounts[j].vgpr_count;
+ break;
+ }
- fprintf (cfile, "%s{\"%s\", {%d, %d, %d}}", comma,
- fns[i], d[0], d[1], d[2]);
+ fprintf (cfile, "%s{\"%s\", {%d, %d, %d}, %d, %d}", comma,
+ fns[i], d[0], d[1], d[2], sgpr_count, vgpr_count);
free (fns[i]);
}
obstack_free (&fns_os, NULL);
for (i = 0; i < dims_count; i++)
free (dims[i].name);
+ for (i = 0; i < regcount_count; i++)
+ free (regcounts[i].kernel_name);
obstack_free (&dims_os, NULL);
+ obstack_free (®counts_os, NULL);
}
/* Embed an object file into a C source file. */
{
const char *name;
int oacc_dims[3]; /* Only present for GCN kernels. */
+ int sgpr_count;
+ int vpgr_count;
};
/* Mkoffload uses this structure to describe an offload variable. */
struct agent_info *agent;
/* The specific module where the kernel takes place. */
struct module_info *module;
+ /* Information provided by mkoffload associated with the kernel. */
+ struct hsa_kernel_description *description;
/* Mutex enforcing that at most once thread ever initializes a kernel for
use. A thread should have locked agent->module_rwlock for reading before
acquiring it. */
struct GOMP_kernel_launch_attributes *kla,
struct goacc_asyncqueue *aq, bool module_locked)
{
+ GCN_DEBUG ("SGPRs: %d, VGPRs: %d\n", kernel->description->sgpr_count,
+ kernel->description->vpgr_count);
+
+ /* Reduce the number of threads/workers if there are insufficient
+ VGPRs available to run the kernels together. */
+ if (kla->ndim == 3 && kernel->description->vpgr_count > 0)
+ {
+ int granulated_vgprs = (kernel->description->vpgr_count + 3) & ~3;
+ int max_threads = (256 / granulated_vgprs) * 4;
+ if (kla->gdims[2] > max_threads)
+ {
+ GCN_WARNING ("Too many VGPRs required to support %d threads/workers"
+ " per team/gang - reducing to %d threads/workers.\n",
+ kla->gdims[2], max_threads);
+ kla->gdims[2] = max_threads;
+ }
+ }
+
GCN_DEBUG ("GCN launch on queue: %d:%d\n", kernel->agent->device_id,
(aq ? aq->id : 0));
GCN_DEBUG ("GCN launch attribs: gdims:[");
kernel->agent = agent;
kernel->module = module;
kernel->name = d->name;
+ kernel->description = d;
if (pthread_mutex_init (&kernel->init_mutex, NULL))
{
GOMP_PLUGIN_error ("Failed to initialize a GCN kernel mutex");