https://gcc.gnu.org/g:b5ef770791fa8007bd1c396e5172ef8e302a643f
commit b5ef770791fa8007bd1c396e5172ef8e302a643f Author: Andrew Stubbs <[email protected]> Date: Mon Nov 2 11:33:41 2026 +0000 amdgcn, libgomp: increase default stack size for SIMT SIMT is a bit more stack-hungry than SIMD (every stack variable and function parameter is multiplied 64x), so the default 32KiB is insufficient for many programs (the motivating example is the minibude benchmark). This patch sets a new default, for SIMT kernels only, of 96KiB. I don't want to go too big because that will cause problems for devices with less memory, but this figure is more than big enough for the testcases I have tested. Note: the stack size setting remains *per wave*, even in SIMT mode. Assisted-by: Claude Opus 5.5 Claude assisted with choosing a suitable size and testing the changes. libgomp/ChangeLog: * config/gcn/libgomp-gcn.h (DEFAULT_GCN_SIMT_STACK_SIZE): New. * libgomp.texi (AMD Radeon (GCN)): Document the "simt" mode default stack size. * plugin/plugin-gcn.c (struct hsa_kernel_description): Add simt_mode. (GCN_KERNEL_SIMD_MODE, GCN_KERNEL_SIMT_DIRECTED_MODE) (GCN_KERNEL_SIMT_MODE): New enum values. (simt_stack_size): New variable. (round_stack_size): New function, split out from ... (init_environment_variables): ... here. Set simt_stack_size from GCN_STACK_SIZE too, and round it. (configure_ephemeral_memories): Use simt_stack_size for "simt" mode kernels. Diff: --- libgomp/config/gcn/libgomp-gcn.h | 3 +++ libgomp/libgomp.texi | 10 +++++--- libgomp/plugin/plugin-gcn.c | 55 +++++++++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/libgomp/config/gcn/libgomp-gcn.h b/libgomp/config/gcn/libgomp-gcn.h index fca6e278d2d4..7475be774d25 100644 --- a/libgomp/config/gcn/libgomp-gcn.h +++ b/libgomp/config/gcn/libgomp-gcn.h @@ -31,6 +31,9 @@ #define LIBGOMP_GCN_H 1 #define DEFAULT_GCN_STACK_SIZE (32*1024) +/* Kernels compiled in "simt" mode keep a copy of each stack variable for + every lane of the wavefront, so they need a larger default stack. */ +#define DEFAULT_GCN_SIMT_STACK_SIZE (96*1024) #define DEFAULT_TEAM_ARENA_SIZE (64*1024) /* These define the LDS location of data needed by OpenMP. */ diff --git a/libgomp/libgomp.texi b/libgomp/libgomp.texi index 8babce663f35..c74c07ce9751 100644 --- a/libgomp/libgomp.texi +++ b/libgomp/libgomp.texi @@ -7730,10 +7730,12 @@ The implementation remark: @item The MI100 (gfx908) and gfx902 are not supported with the @code{GOMP_RUNTIME_USM} environment variable. @item The available stack size can be changed using the @code{GCN_STACK_SIZE} - environment variable; the default is 32 kiB per thread. The size is - rounded up to an odd multiple of 4 kiB (so the default becomes 36 kiB) - because placing the stacks at a large power-of-two stride harms memory - performance. + environment variable; the default is 32 kiB per thread, or 96 kiB for + kernels compiled with @option{--param=offload-gpu-mode=simt}, which + keep a copy of each stack variable for every lane. The size is + rounded up to an odd multiple of 4 kiB (so the defaults become 36 kiB + and 100 kiB) because placing the stacks at a large power-of-two stride + harms memory performance. @item Low-latency memory (@code{omp_low_lat_mem_space}) is supported when the the @code{access} trait is set to @code{cgroup}. The default pool size is automatically scaled to share the 64 kiB LDS memory between the number diff --git a/libgomp/plugin/plugin-gcn.c b/libgomp/plugin/plugin-gcn.c index 15951340bcf6..de133d0a178a 100644 --- a/libgomp/plugin/plugin-gcn.c +++ b/libgomp/plugin/plugin-gcn.c @@ -413,6 +413,17 @@ struct hsa_kernel_description int oacc_dims[3]; /* Only present for GCN kernels. */ int sgpr_count; int vpgr_count; + int simt_mode; /* See GCN_KERNEL_SIMT_MODE. */ +}; + +/* The values of simt_mode in struct hsa_kernel_description, which must match + mkoffload. */ + +enum +{ + GCN_KERNEL_SIMD_MODE, + GCN_KERNEL_SIMT_DIRECTED_MODE, + GCN_KERNEL_SIMT_MODE }; /* Mkoffload uses this structure to describe an offload variable. */ @@ -626,8 +637,27 @@ static size_t gcn_kernel_heap_size = DEFAULT_GCN_HEAP_SIZE; static int team_arena_size = DEFAULT_TEAM_ARENA_SIZE; static int stack_size = DEFAULT_GCN_STACK_SIZE; +static int simt_stack_size = DEFAULT_GCN_SIMT_STACK_SIZE; static int lowlat_size = -1; +/* When the stride between the stacks of consecutive threads is a multiple + of a large power of two, the same stack offset in every thread maps to + the same memory channel, which can considerably harm the performance of + kernels that use the stack heavily, such as vectorized reductions. Round + SIZE up to an odd multiple of 4 kiB to avoid this. */ + +static int +round_stack_size (int size) +{ + if (size > 0) + { + size = (size + 4095) & ~4095; + if ((size & 4096) == 0) + size += 4096; + } + return size; +} + /* Flag to decide whether print to stderr information about what is going on. Set in init_debug depending on environment variables. */ @@ -1187,25 +1217,17 @@ init_environment_variables (void) team_arena_size = tmp;; } + /* GCN_STACK_SIZE applies to "simt" mode kernels too. */ const char *stack = secure_getenv ("GCN_STACK_SIZE"); if (stack) { int tmp = atoi (stack); if (tmp) - stack_size = tmp;; + stack_size = simt_stack_size = tmp; } - /* When the stride between the stacks of consecutive threads is a multiple - of a large power of two, the same stack offset in every thread maps to - the same memory channel, which can considerably harm the performance of - kernels that use the stack heavily, such as vectorized reductions. Round - the stack size up to an odd multiple of 4 kiB to avoid this. */ - if (stack_size > 0) - { - stack_size = (stack_size + 4095) & ~4095; - if ((stack_size & 4096) == 0) - stack_size += 4096; - } + stack_size = round_stack_size (stack_size); + simt_stack_size = round_stack_size (simt_stack_size); const char *lowlat = secure_getenv ("GOMP_GCN_LOWLAT_POOL"); if (lowlat) @@ -1991,9 +2013,12 @@ configure_ephemeral_memories (struct kernel_info *kernel, int actual_arena_size = (kernel->kind == KIND_OPENMP ? team_arena_size : 0); - int actual_arena_total_size = actual_arena_size * num_teams; + size_t actual_arena_total_size = (size_t) actual_arena_size * num_teams; + size_t actual_stack_size = (kernel->description->simt_mode + == GCN_KERNEL_SIMT_MODE + ? simt_stack_size : stack_size); size_t size = (actual_arena_total_size - + num_teams * num_threads * stack_size); + + (size_t) num_teams * num_threads * actual_stack_size); for (item = *next_ptr; item; next_ptr = &item->next, item = item->next) { @@ -2052,7 +2077,7 @@ configure_ephemeral_memories (struct kernel_info *kernel, : 0); kernargs->stack_ptr = (uint64_t)item->address + actual_arena_total_size; kernargs->arena_size_per_team = actual_arena_size; - kernargs->stack_size_per_thread = stack_size; + kernargs->stack_size_per_thread = actual_stack_size; } /* Mark an ephemeral memory space available for reuse. */
