Kernel entry functions may use more registers than normal functions, which
can limit how many wavefronts fit on a compute unit. This can be useful,
but can also be counter-productive, so the new option allows the user to
tune it.
For example, a kernel that needs more than 64 VGPRs+AVGPRs on gfx90a cannot fit
two 16-wave teams on one CU, and miniBUDE in SIMT mode (not yet upstream) runs
~1.85x slower at 208 teams than at 104 for that reason.
The new option derives per-wave SGPR, VGPR and AVGPR budgets for kernels from a
requested occupancy. The default of 4 imposes no limit on any device.
While investigating this, I've also discovered that the existing register
calculations were both a little buggy, and also out of date. In particular,
the "normal" register usage was still tuned for GCN3, GCN5, and CDNA1 devices.
These values can be safely increased for CDNA2+ and RDNA devices.
This patch moves the occupancy parameters of each device into gcn-devices.def,
so that the compiler and the libgomp plugin share them; this replaces
TARGET_VGPR_GRANULARITY. The kernel metadata's .sgpr_count now includes VCC,
the XNACK mask and architected flat scratch, as the AMDGPU metadata
specification requires, and the plugin's check that a team's wavefronts fit on
one CU now applies the same rules as the compiler: VGPR granularity, the
maximum number of waves per SIMD, SGPR usage, and the number of SIMDs per CU.
This fixes the plugin's check for RDNA devices. It assumed four SIMDs per
CU, but in CU mode an RDNA workgroup is confined to the CU's two SIMDs, so
on gfx1100 a 16-wave team needing 97 to 192 VGPRs was allowed and then
rejected by the runtime. The RDNA3 devices that have 512 VGPRs (rather
than 768) allocate them in blocks of 8, not 12.
The register limits for normal functions move into gcn-devices.def too.
They were chosen for the 256-VGPR, 10-wave devices: a kernel that calls
functions must reserve at least these, and 24 VGPRs and 62 SGPRs still let
it reach 10 waves per SIMD. Those devices keep their values. The other
devices now get the most that still allows full occupancy: on CDNA2/3, 40
VGPRs plus 24 AVGPRs (64 of the shared 512 for 8 waves) and 76 SGPRs, or
74 on CDNA3 where architected flat scratch takes two more; on RDNA, 32
VGPRs, or 48 on the RDNA3 devices with 768, and all 96 usable SGPRs, as
each wave has a fixed SGPR allocation there.
Assisted-by: Claude Opus 5.5
Claude helped compile the correct values for the tables, found the existing
inconsistencies, assisted with refactoring the code, tested the results, and
generated the changelog.
gcc/ChangeLog:
* config/gcn/gcn-devices.def: Document and add the "SIMDs per CU",
"Max waves per SIMD", "VGPR granularity", "SGPRs per SIMD",
"SGPR granularity", "Max normal SGPRs", "Max normal VGPRs" and
"Max normal AVGPRs" fields to every device.
* config/gcn/gcn-opts.h (TARGET_VGPR_GRANULARITY): Delete.
(TARGET_SHARED_SGPRS): New.
* config/gcn/gcn.h (struct gcn_device_def): Add simds_per_cu,
max_waves_per_simd, vgpr_granularity, sgprs_per_simd,
sgpr_granularity, max_normal_sgprs, max_normal_vgprs and
max_normal_avgprs.
* config/gcn/gcn.cc (MAX_NORMAL_SGPR_COUNT): Delete.
(MAX_NORMAL_VGPR_COUNT): Delete.
(MAX_NORMAL_AVGPR_COUNT): Delete.
(kernel_max_sgprs): New.
(kernel_max_vgprs): New.
(kernel_max_avgprs): New.
(gcn_devices): Initialize the new fields.
(gcn_kernel_extra_sgprs): New.
(gcn_set_kernel_register_limits): New.
(gcn_option_override): Call it for -mmin-waves-per-cu > 4.
(gcn_conditional_register_usage): Restrict kernel entry functions to
the kernel register budget. Use the device's vgpr_granularity
instead of TARGET_VGPR_GRANULARITY, and the device's max_normal_*
fields instead of MAX_NORMAL_*_COUNT.
(gcn_hsa_declare_function_name): Use the device's vgpr_granularity
and max_normal_* fields.
Include the special SGPRs in the metadata .sgpr_count.
* config/gcn/gcn.opt (mmin-waves-per-cu=): New.
* doc/invoke.texi (AMD GCN Options): Document -mmin-waves-per-cu.
gcc/testsuite/ChangeLog:
* gcc.target/gcn/ivopts-reg-budget-1.c: Update expectation.
libgomp/ChangeLog:
* plugin/plugin-gcn.c (struct isa_occupancy): New.
(isa_occupancy): New.
(kernel_waves_per_cu): New.
(run_kernel): Use kernel_waves_per_cu to limit threads/workers.
---
gcc/config/gcn/gcn-devices.def | 327 ++++++++++++++++--
gcc/config/gcn/gcn-opts.h | 7 +-
gcc/config/gcn/gcn.cc | 129 ++++++-
gcc/config/gcn/gcn.h | 8 +
gcc/config/gcn/gcn.opt | 4 +
gcc/doc/invoke.texi | 17 +-
.../gcc.target/gcn/ivopts-reg-budget-1.c | 9 +-
libgomp/plugin/plugin-gcn.c | 74 +++-
8 files changed, 509 insertions(+), 66 deletions(-)
diff --git a/gcc/config/gcn/gcn-devices.def b/gcc/config/gcn/gcn-devices.def
index 0b90e488950..5888b659870 100644
--- a/gcc/config/gcn/gcn-devices.def
+++ b/gcc/config/gcn/gcn-devices.def
@@ -75,6 +75,33 @@
The name of the generic ISA this device is compatible with or "NONE",
where the generic name is the NAME (field 2) of the associated
generic device.
+ 12 "SIMDs per CU" (integer, internal)
+ The number of SIMDs in each compute unit, for the purposes of
+ calculating maximum occupancy.
+ 13 "Max waves per SIMD" (integer, internal)
+ The maximum number of wave64 wavefronts that can be resident on each
+ SIMD.
+ 14 "VGPR granularity" (integer, internal)
+ The number of VGPRs (and AVGPRs) allocated at once, as used in the
+ kernel descriptor.
+ 15 "SGPRs per SIMD" (integer, internal)
+ The number of SGPRs shared by the wavefronts on each SIMD, or 0 if
+ each wavefront has a fixed allocation.
+ 16 "SGPR granularity" (integer, internal)
+ The number of SGPRs allocated at once, or 0 if each wavefront has a
+ fixed allocation. Each wavefront is allocated one more block of this
+ size than its SGPR count needs, as measured on gfx90a.
+ 17 "Max normal SGPRs" (integer, internal)
+ The number of SGPRs, not counting VCC, that normal (non-kernel)
+ functions may use. Kernels that make calls reserve at least this
+ many, so all code for a device must agree. Where possible, the
+ normal limits are the most that still let such a kernel reach the
+ maximum occupancy, including the special SGPRs allocated with them.
+ SGPRs above 95 are never available.
+ 18 "Max normal VGPRs" (integer, internal)
+ As above, for VGPRs.
+ 19 "Max normal AVGPRs" (integer, internal)
+ As above, for AVGPRs, or 0 for devices without them.
Fields marked "external", above, have values defined elsewhere (HSA, ROCM,
LLVM, ELF, etc.) and must have matching definitions here. Fields marked
@@ -91,7 +118,15 @@ GCN_DEVICE(gfx900, GFX900, 0x2c, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx902, GFX902, 0x2d, ISA_GCN5,
@@ -102,7 +137,15 @@ GCN_DEVICE(gfx902, GFX902, 0x2d, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx904, GFX904, 0x2e, ISA_GCN5,
@@ -113,7 +156,15 @@ GCN_DEVICE(gfx904, GFX904, 0x2e, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx906, GFX906, 0x2f, ISA_GCN5,
@@ -124,7 +175,15 @@ GCN_DEVICE(gfx906, GFX906, 0x2f, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx908, GFX908, 0x30, ISA_CDNA1,
@@ -135,7 +194,15 @@ GCN_DEVICE(gfx908, GFX908, 0x30, ISA_CDNA1,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 24
)
GCN_DEVICE(gfx909, GFX909, 0x31, ISA_GCN5,
@@ -146,7 +213,15 @@ GCN_DEVICE(gfx909, GFX909, 0x31, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx90a, GFX90A, 0x3f, ISA_CDNA2,
@@ -157,7 +232,15 @@ GCN_DEVICE(gfx90a, GFX90A, 0x3f, ISA_CDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 8,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 76,
+ /* Max normal VGPRs */ 40,
+ /* Max normal AVGPRs */ 24
)
GCN_DEVICE(gfx90c, GFX90C, 0x32, ISA_GCN5,
@@ -168,7 +251,15 @@ GCN_DEVICE(gfx90c, GFX90C, 0x32, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_GENERIC
+ /* Generic Name */ GFX9_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx942, GFX942, 0x4c, ISA_CDNA3,
@@ -179,7 +270,15 @@ GCN_DEVICE(gfx942, GFX942, 0x4c, ISA_CDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_4_GENERIC
+ /* Generic Name */ GFX9_4_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 8,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 74,
+ /* Max normal VGPRs */ 40,
+ /* Max normal AVGPRs */ 24
)
GCN_DEVICE(gfx950, GFX950, 0x4f, ISA_CDNA3,
@@ -190,7 +289,15 @@ GCN_DEVICE(gfx950, GFX950, 0x4f, ISA_CDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX9,
- /* Generic Name */ GFX9_4_GENERIC
+ /* Generic Name */ GFX9_4_GENERIC,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 8,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 74,
+ /* Max normal VGPRs */ 40,
+ /* Max normal AVGPRs */ 24
)
GCN_DEVICE(gfx9-generic, GFX9_GENERIC, 0x051, ISA_GCN5,
@@ -201,7 +308,15 @@ GCN_DEVICE(gfx9-generic, GFX9_GENERIC, 0x051, ISA_GCN5,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 1,
/* Architecture Family */ GFX9,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 10,
+ /* VGPR granularity */ 4,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 62,
+ /* Max normal VGPRs */ 24,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx9-4-generic, GFX9_4_GENERIC, 0x05f, ISA_CDNA3,
@@ -212,7 +327,15 @@ GCN_DEVICE(gfx9-4-generic, GFX9_4_GENERIC, 0x05f,
ISA_CDNA3,
/* Max ISA VGPRs */ 256,
/* Generic code obj version */ 1,
/* Architecture Family */ GFX9,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 4,
+ /* Max waves per SIMD */ 8,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 800,
+ /* SGPR granularity */ 16,
+ /* Max normal SGPRs */ 74,
+ /* Max normal VGPRs */ 40,
+ /* Max normal AVGPRs */ 24
)
/* GCN GFX10.3 (RDNA 2) */
@@ -225,7 +348,15 @@ GCN_DEVICE(gfx1030, GFX1030, 0x36, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1031, GFX1031, 0x37, ISA_RDNA2,
@@ -236,7 +367,15 @@ GCN_DEVICE(gfx1031, GFX1031, 0x37, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1032, GFX1032, 0x38, ISA_RDNA2,
@@ -247,7 +386,15 @@ GCN_DEVICE(gfx1032, GFX1032, 0x38, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1033, GFX1033, 0x39, ISA_RDNA2,
@@ -258,7 +405,15 @@ GCN_DEVICE(gfx1033, GFX1033, 0x39, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1034, GFX1034, 0x3e, ISA_RDNA2,
@@ -269,7 +424,15 @@ GCN_DEVICE(gfx1034, GFX1034, 0x3e, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1035, GFX1035, 0x3d, ISA_RDNA2,
@@ -280,7 +443,15 @@ GCN_DEVICE(gfx1035, GFX1035, 0x3d, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1036, GFX1036, 0x45, ISA_RDNA2,
@@ -291,7 +462,15 @@ GCN_DEVICE(gfx1036, GFX1036, 0x45, ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX10,
- /* Generic Name */ GFX10_3_GENERIC
+ /* Generic Name */ GFX10_3_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx10-3-generic, GFX10_3_GENERIC, 0x053, ISA_RDNA2,
@@ -302,7 +481,15 @@ GCN_DEVICE(gfx10-3-generic, GFX10_3_GENERIC, 0x053,
ISA_RDNA2,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 1,
/* Architecture Family */ GFX10,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
/* GCN GFX11 (RDNA 3) */
@@ -315,7 +502,15 @@ GCN_DEVICE(gfx1100, GFX1100, 0x41, ISA_RDNA3,
/* Max ISA VGPRs */ 768,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 12,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 48,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1101, GFX1101, 0x46, ISA_RDNA3,
@@ -326,7 +521,15 @@ GCN_DEVICE(gfx1101, GFX1101, 0x46, ISA_RDNA3,
/* Max ISA VGPRs */ 768,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 12,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 48,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1102, GFX1102, 0x47, ISA_RDNA3,
@@ -337,7 +540,15 @@ GCN_DEVICE(gfx1102, GFX1102, 0x47, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1103, GFX1103, 0x44, ISA_RDNA3,
@@ -348,7 +559,15 @@ GCN_DEVICE(gfx1103, GFX1103, 0x44, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1150, GFX1150, 0x43, ISA_RDNA3,
@@ -359,7 +578,15 @@ GCN_DEVICE(gfx1150, GFX1150, 0x43, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1151, GFX1151, 0x4a, ISA_RDNA3,
@@ -370,7 +597,15 @@ GCN_DEVICE(gfx1151, GFX1151, 0x4a, ISA_RDNA3,
/* Max ISA VGPRs */ 768,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 12,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 48,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1152, GFX1152, 0x55, ISA_RDNA3,
@@ -381,7 +616,15 @@ GCN_DEVICE(gfx1152, GFX1152, 0x55, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1153, GFX1153, 0x58, ISA_RDNA3,
@@ -392,7 +635,15 @@ GCN_DEVICE(gfx1153, GFX1153, 0x58, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx1154, GFX1154, 0x57, ISA_RDNA3,
@@ -403,7 +654,15 @@ GCN_DEVICE(gfx1154, GFX1154, 0x57, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 0, /* non-generic */
/* Architecture Family */ GFX11,
- /* Generic Name */ GFX11_GENERIC
+ /* Generic Name */ GFX11_GENERIC,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 8,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
GCN_DEVICE(gfx11-generic, GFX11_GENERIC, 0x054, ISA_RDNA3,
@@ -414,7 +673,15 @@ GCN_DEVICE(gfx11-generic, GFX11_GENERIC, 0x054, ISA_RDNA3,
/* Max ISA VGPRs */ 512,
/* Generic code obj version */ 1,
/* Architecture Family */ GFX11,
- /* Generic Name */ NONE
+ /* Generic Name */ NONE,
+ /* SIMDs per CU */ 2,
+ /* Max waves per SIMD */ 16,
+ /* VGPR granularity */ 12,
+ /* SGPRs per SIMD */ 0,
+ /* SGPR granularity */ 0,
+ /* Max normal SGPRs */ 96,
+ /* Max normal VGPRs */ 32,
+ /* Max normal AVGPRs */ 0
)
#undef GCN_DEVICE
diff --git a/gcc/config/gcn/gcn-opts.h b/gcc/config/gcn/gcn-opts.h
index b983d59343b..c7148c0a8a2 100644
--- a/gcc/config/gcn/gcn-opts.h
+++ b/gcc/config/gcn/gcn-opts.h
@@ -70,6 +70,8 @@ enum hsaco_attr_type
#define TARGET_AVGPR_MEMOPS TARGET_CDNA2_PLUS
/* AVGPRS may have their own register file, or be combined with VGPRS. */
#define TARGET_AVGPR_COMBINED TARGET_CDNA2_PLUS
+/* The wavefronts on each SIMD share one pool of SGPRs. */
+#define TARGET_SHARED_SGPRS (!TARGET_RDNA2_PLUS)
/* global_load/store has reduced offset. */
#define TARGET_11BIT_GLOBAL_OFFSET TARGET_RDNA2_PLUS
/* The work item details are all encoded into v0. */
@@ -98,11 +100,6 @@ enum hsaco_attr_type
CDNA3 also uses 'nt' instead of 'slc' and 'sc1' instead of 'scc'; however,
there is no non-scalar user so far. */
#define TARGET_GLC_NAME (TARGET_CDNA3 ? " sc0" : " glc")
-/* The metadata on different devices need different granularity. */
-#define TARGET_VGPR_GRANULARITY \
- (TARGET_RDNA3 ? 12 \
- : TARGET_RDNA2_PLUS || TARGET_CDNA2_PLUS ? 8 \
- : 4)
/* This mostly affects the metadata. */
#define TARGET_ARCHITECTED_FLAT_SCRATCH (TARGET_RDNA3 || TARGET_CDNA3)
/* Device has Sub-DWord Addressing instructions. */
diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc
index 0d2edc7595c..85159b51213 100644
--- a/gcc/config/gcn/gcn.cc
+++ b/gcc/config/gcn/gcn.cc
@@ -93,20 +93,22 @@ static int acc_lds_size = 0x600;
static int gang_private_hwm = 32;
static hash_map<tree, int> lds_allocs;
-/* The number of registers usable by normal non-kernel functions.
- The SGPR count includes any special extra registers such as VCC. */
+/* The number of registers usable by kernel entry functions, derived from
+ -mmin-waves-per-cu. Zero means no limit. */
-#define MAX_NORMAL_SGPR_COUNT 62 // i.e. 64 with VCC
-#define MAX_NORMAL_VGPR_COUNT 24
-#define MAX_NORMAL_AVGPR_COUNT 24
+static int kernel_max_sgprs = 0;
+static int kernel_max_vgprs = 0;
+static int kernel_max_avgprs = 0;
/* Import all the data from gcn-devices.def.
The PROCESSOR_GFXnnn should be indices for this table. */
const struct gcn_device_def gcn_devices[] = {
#define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAMECC, WAVE64, CU, VGPRS, \
- GEN_VER, ARCH_FAM, ...) \
+ GEN_VER, ARCH_FAM, GEN_NAME, SIMDS, WAVES, VGPR_GRAN, \
+ SGPRS, SGPR_GRAN, NORMAL_SGPRS, NORMAL_VGPRS, NORMAL_AVGPRS)
\
{PROCESSOR_ ## NAME, #name, #NAME, ISA, XNACK, SRAMECC, WAVE64, CU, VGPRS,
\
- GEN_VER, #ARCH_FAM},
+ GEN_VER, #ARCH_FAM, SIMDS, WAVES, VGPR_GRAN, SGPRS, SGPR_GRAN, \
+ NORMAL_SGPRS, NORMAL_VGPRS, NORMAL_AVGPRS},
#include "gcn-devices.def"
};
@@ -136,6 +138,77 @@ gcn_init_machine_status (void)
return f;
}
+/* Return the number of special SGPRs that a kernel's SGPR allocation must
+ hold in addition to those it uses directly: VCC, which is always reserved,
+ plus, where SGPRs are shared, the XNACK mask, if enabled, and architected
+ flat scratch. */
+
+static int
+gcn_kernel_extra_sgprs ()
+{
+ int extra = VCC_HI_REG - VCC_LO_REG + 1;
+ /* Devices with a fixed SGPR allocation count only VCC. */
+ if (!TARGET_SHARED_SGPRS)
+ return extra;
+ /* Flat scratch is allocated above the XNACK mask, so with architected
+ flat scratch both are always allocated. */
+ if (TARGET_ARCHITECTED_FLAT_SCRATCH)
+ extra += FLAT_SCRATCH_HI_REG - FLAT_SCRATCH_LO_REG + 1;
+ if (TARGET_XNACK || TARGET_ARCHITECTED_FLAT_SCRATCH)
+ extra += XNACK_MASK_HI_REG - XNACK_MASK_LO_REG + 1;
+ return extra;
+}
+
+/* Translate -mmin-waves-per-cu into the number of each kind of register that
+ kernel entry functions may use. The default, 4, needs no limit on any
+ device.
+
+ The budgets cannot go below what normal functions are allowed, because
+ kernels must be able to call them. LDS usage is not considered: it is
+ allocated per work-group, and the number of waves per work-group is not
+ known at compile time. */
+
+static void
+gcn_set_kernel_register_limits ()
+{
+ const gcn_device_def &dev = gcn_devices[gcn_arch];
+
+ int waves_per_simd = CEIL (gcn_min_waves_per_cu, dev.simds_per_cu);
+ if (waves_per_simd > dev.max_waves_per_simd)
+ {
+ warning (0, "%<-mmin-waves-per-cu=%d%> exceeds the maximum of %d for "
+ "%<-march=%s%>", gcn_min_waves_per_cu,
+ dev.max_waves_per_simd * dev.simds_per_cu, dev.name);
+ waves_per_simd = dev.max_waves_per_simd;
+ }
+
+ /* Each wave is allocated one more block of SGPRs than it needs, and the
+ allocation also holds the special SGPRs. */
+ if (TARGET_SHARED_SGPRS)
+ {
+ int sgprs = dev.sgprs_per_simd / waves_per_simd;
+ sgprs -= sgprs % dev.sgpr_granularity;
+ sgprs -= dev.sgpr_granularity;
+ sgprs -= gcn_kernel_extra_sgprs ();
+ if (sgprs <= LAST_SGPR_REG - FIRST_SGPR_REG)
+ kernel_max_sgprs = MAX (sgprs, dev.max_normal_sgprs);
+ }
+
+ int vgprs = dev.max_isa_vgprs / waves_per_simd;
+ vgprs -= vgprs % dev.vgpr_granularity;
+ if (TARGET_AVGPR_COMBINED)
+ {
+ /* The VGPRs and AVGPRs share one register file. Give the AVGPRs only
+ what callees may use, so that spills go to memory instead. */
+ kernel_max_avgprs = dev.max_normal_avgprs;
+ vgprs -= dev.max_normal_avgprs;
+ }
+ else if (TARGET_AVGPRS)
+ /* The AVGPRs are a separate register file of the same size. */
+ kernel_max_avgprs = MAX (vgprs, dev.max_normal_avgprs);
+ kernel_max_vgprs = MAX (vgprs, dev.max_normal_vgprs);
+}
+
/* Implement TARGET_OPTION_OVERRIDE.
Override option settings where defaults are variable, or we have specific
@@ -193,6 +266,9 @@ gcn_option_override (void)
if (flag_sram_ecc == HSACO_ATTR_DEFAULT)
flag_sram_ecc = gcn_devices[gcn_arch].sramecc_default;
+ if (gcn_min_waves_per_cu > 4)
+ gcn_set_kernel_register_limits ();
+
/* TODO: This seems to produce tighter loops, but the testsuites expects it
to be set to '2', so I'll leave it default for now.
SET_OPTION_IF_UNSET (&global_options, &global_options_set,
@@ -2584,7 +2660,7 @@ gcn_conditional_register_usage (void)
{
/* Some architectures have a register allocation granularity that does not
permit use of the full register count. */
- for (int i = 256 - (256 % TARGET_VGPR_GRANULARITY);
+ for (int i = 256 - (256 % gcn_devices[gcn_arch].vgpr_granularity);
i < 256;
i++)
fixed_regs[VGPR_REGNO (i)] = call_used_regs[VGPR_REGNO (i)] = 1;
@@ -2597,20 +2673,33 @@ gcn_conditional_register_usage (void)
{
/* Restrict the set of SGPRs, VGPRs and AVGPRs used by non-kernel
functions. */
- for (int i = SGPR_REGNO (MAX_NORMAL_SGPR_COUNT);
+ for (int i = SGPR_REGNO (gcn_devices[gcn_arch].max_normal_sgprs);
i <= LAST_SGPR_REG; i++)
fixed_regs[i] = 1, call_used_regs[i] = 1;
- for (int i = VGPR_REGNO (MAX_NORMAL_VGPR_COUNT);
+ for (int i = VGPR_REGNO (gcn_devices[gcn_arch].max_normal_vgprs);
i <= LAST_VGPR_REG; i++)
fixed_regs[i] = 1, call_used_regs[i] = 1;
- for (int i = AVGPR_REGNO (MAX_NORMAL_AVGPR_COUNT);
+ for (int i = AVGPR_REGNO (gcn_devices[gcn_arch].max_normal_avgprs);
i <= LAST_AVGPR_REG; i++)
fixed_regs[i] = 1, call_used_regs[i] = 1;
return;
}
+ /* Restrict kernel entry functions to the budget for -mmin-waves-per-cu. */
+ if (kernel_max_sgprs)
+ for (int i = SGPR_REGNO (kernel_max_sgprs); i <= LAST_SGPR_REG; i++)
+ fixed_regs[i] = 1, call_used_regs[i] = 1;
+
+ if (kernel_max_vgprs)
+ for (int i = VGPR_REGNO (kernel_max_vgprs); i <= LAST_VGPR_REG; i++)
+ fixed_regs[i] = 1, call_used_regs[i] = 1;
+
+ if (kernel_max_avgprs)
+ for (int i = AVGPR_REGNO (kernel_max_avgprs); i <= LAST_AVGPR_REG; i++)
+ fixed_regs[i] = 1, call_used_regs[i] = 1;
+
if (!cfun->machine)
return;
@@ -6983,16 +7072,17 @@ gcn_hsa_declare_function_name (FILE *file, const char
*name,
if (!leaf_function_p ())
{
/* We can't know how many registers function calls might use. */
- if (vgpr < MAX_NORMAL_VGPR_COUNT)
- vgpr = MAX_NORMAL_VGPR_COUNT;
- if (sgpr < MAX_NORMAL_SGPR_COUNT)
- sgpr = MAX_NORMAL_SGPR_COUNT;
- if (avgpr < MAX_NORMAL_AVGPR_COUNT)
- avgpr = MAX_NORMAL_AVGPR_COUNT;
+ const gcn_device_def &dev = gcn_devices[gcn_arch];
+ if (vgpr < dev.max_normal_vgprs)
+ vgpr = dev.max_normal_vgprs;
+ if (sgpr < dev.max_normal_sgprs)
+ sgpr = dev.max_normal_sgprs;
+ if (avgpr < dev.max_normal_avgprs)
+ avgpr = dev.max_normal_avgprs;
}
/* Round up to the allocation block size. */
- int vgpr_block_size = TARGET_VGPR_GRANULARITY;
+ int vgpr_block_size = gcn_devices[gcn_arch].vgpr_granularity;
if (vgpr % vgpr_block_size)
vgpr += vgpr_block_size - (vgpr % vgpr_block_size);
if (avgpr % vgpr_block_size)
@@ -7109,7 +7199,8 @@ gcn_hsa_declare_function_name (FILE *file, const char
*name,
cfun->machine->kernarg_segment_byte_size,
cfun->machine->kernarg_segment_alignment,
LDS_SIZE,
- sgpr, next_free_vgpr,
+ /* The metadata count includes the special SGPRs. */
+ sgpr + gcn_kernel_extra_sgprs (), next_free_vgpr,
(TARGET_WAVE64_COMPAT
? " ; wavefrontsize64 counts double on SIMD32"
: ""));
diff --git a/gcc/config/gcn/gcn.h b/gcc/config/gcn/gcn.h
index 6255fe324f1..d2a1c39d2f4 100644
--- a/gcc/config/gcn/gcn.h
+++ b/gcc/config/gcn/gcn.h
@@ -30,6 +30,14 @@ extern const struct gcn_device_def {
int max_isa_vgprs;
unsigned generic_version;
const char *arch_family;
+ int simds_per_cu;
+ int max_waves_per_simd;
+ int vgpr_granularity;
+ int sgprs_per_simd;
+ int sgpr_granularity;
+ int max_normal_sgprs;
+ int max_normal_vgprs;
+ int max_normal_avgprs;
} gcn_devices[];
#define TARGET_CPU_CPP_BUILTINS()
\
diff --git a/gcc/config/gcn/gcn.opt b/gcc/config/gcn/gcn.opt
index acc61e4847b..470282f728c 100644
--- a/gcc/config/gcn/gcn.opt
+++ b/gcc/config/gcn/gcn.opt
@@ -61,6 +61,10 @@ mstack-size=
Target RejectNegative Joined UInteger Var(stack_size_opt) Init(-1) Undocumented
Obsolete; use GCN_STACK_SIZE at runtime.
+mmin-waves-per-cu=
+Target RejectNegative Joined UInteger IntegerRange(4, 40)
Var(gcn_min_waves_per_cu) Init(4)
+-mmin-waves-per-cu=<n> Limit the registers used by kernels so that at least
<n> wavefronts fit on each compute unit.
+
Variable
int gang_private_size_opt = -1
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index b17b996677b..5284996177d 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -922,7 +922,7 @@ Objective-C and Objective-C++ Dialects}.
@emph{AMD GCN Options} (@ref{AMD GCN Options})
@gccoptlist{-march=@var{gpu} -mtune=@var{gpu}
--mgang-private-size=@var{bytes}
+-mgang-private-size=@var{bytes} -mmin-waves-per-cu=@var{n}
-msram-ecc=@r{[}on@r{|}off@r{|}any@r{]}
-mxnack=@r{[}on@r{|}off@r{|}any@r{]}
-Wopenacc-dims}
@@ -22546,6 +22546,21 @@ gfx1102, gfx1103, gfx1150, gfx1151, gfx1152, gfx1153,
gfx1154.
Set the amount of local data-share (LDS) memory to reserve for
gang-private variables. The default is 512.
+@opindex mmin-waves-per-cu
+@item -mmin-waves-per-cu=@var{n}
+Limit the register resources used by kernel entry functions so that at least
+@var{n} wavefronts can run at once on each compute unit. AMD GPUs have a large
+register file that is divided between the running waves (threads), so
+specifying a larger @var{n} means reducing the register reservation for each.
+This option only sets a minimum; the compiler does not reserve registers if it
+did not need them. Only register usage is limited; LDS usage may still
+restrict occupancy. Functions called from a kernel already use a fixed small
+set of registers, so they are not affected. @var{n} must be between 4 and 40;
+values above the maximum for the selected device are reduced to that maximum,
+with a warning. The default is 4, which imposes no limit. In order to run
+more than 16 waves concurrently, on a CU, it must be occupied by multiple teams
+or gangs.
+
@opindex msram-ecc
@item -msram-ecc=on
@itemx -msram-ecc=off
diff --git a/gcc/testsuite/gcc.target/gcn/ivopts-reg-budget-1.c
b/gcc/testsuite/gcc.target/gcn/ivopts-reg-budget-1.c
index 378f93e1a06..2651f038a9d 100644
--- a/gcc/testsuite/gcc.target/gcn/ivopts-reg-budget-1.c
+++ b/gcc/testsuite/gcc.target/gcn/ivopts-reg-budget-1.c
@@ -1,9 +1,10 @@
-/* Non-kernel functions may only use the first MAX_NORMAL_SGPR_COUNT SGPRs
+/* Non-kernel functions may only use the device's "Max normal SGPRs"
(see gcn_conditional_register_usage), so the register budget seen by the
- loop optimizers must be smaller for them than for kernels. */
+ loop optimizers must be smaller for them than for kernels. The budget is
+ device-specific, so pin the device. */
/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-ivopts-details" } */
+/* { dg-options "-march=gfx90a -O2 -fdump-tree-ivopts-details" } */
/* A normal function. It is expanded before main, i.e. it is the first
function for which RTL is initialized. */
@@ -31,5 +32,5 @@ main ()
/* { dg-final { scan-assembler "\\.amdhsa_kernel\\s+main" } } */
/* { dg-final { scan-assembler-not "\\.amdhsa_kernel\\s+f\\M" } } */
-/* { dg-final { scan-tree-dump-times "target_avail_regs 44" 1 "ivopts" } } */
+/* { dg-final { scan-tree-dump-times "target_avail_regs 58" 1 "ivopts" } } */
/* { dg-final { scan-tree-dump-times "target_avail_regs 78" 1 "ivopts" } } */
diff --git a/libgomp/plugin/plugin-gcn.c b/libgomp/plugin/plugin-gcn.c
index ca006c977dc..e89b424b2c9 100644
--- a/libgomp/plugin/plugin-gcn.c
+++ b/libgomp/plugin/plugin-gcn.c
@@ -1852,6 +1852,67 @@ max_isa_vgprs (int isa)
}
}
+/* Occupancy parameters of a device, from gcn-devices.def. */
+
+struct isa_occupancy
+{
+ int simds_per_cu;
+ int max_waves_per_simd;
+ int vgpr_granularity;
+ int sgprs_per_simd; /* Zero if not shared. */
+ int sgpr_granularity;
+};
+
+static struct isa_occupancy
+isa_occupancy (int isa)
+{
+ switch (isa)
+ {
+#define GCN_DEVICE(name, NAME, ELF, ISA, XNACK, SRAM, WAVE64, CU, VGPRS, \
+ GEN_VER, ARCH_FAM, GEN_NAME, SIMDS, WAVES, VGPR_GRAN, \
+ SGPRS, SGPR_GRAN, ...) \
+ case ELF: \
+ return (struct isa_occupancy) {SIMDS, WAVES, VGPR_GRAN, SGPRS, \
+ SGPR_GRAN};
+#include "../../gcc/config/gcn/gcn-devices.def"
+ default:
+ GOMP_PLUGIN_fatal ("unhandled ISA in isa_occupancy");
+ }
+}
+
+/* Return how many wavefronts of KERNEL fit on each CU of AGENT, given its
+ register usage. The register counts from the kernel metadata already
+ include any AVGPRs and special SGPRs. */
+
+static int
+kernel_waves_per_cu (struct agent_info *agent, struct kernel_info *kernel)
+{
+ int isa = agent->device_isa;
+ struct isa_occupancy occ = isa_occupancy (isa);
+ int waves = occ.max_waves_per_simd;
+
+ int vgprs = kernel->description->vpgr_count;
+ if (vgprs > 0)
+ {
+ vgprs = ((vgprs + occ.vgpr_granularity - 1) / occ.vgpr_granularity
+ * occ.vgpr_granularity);
+ if (max_isa_vgprs (isa) / vgprs < waves)
+ waves = max_isa_vgprs (isa) / vgprs;
+ }
+
+ int sgprs = kernel->description->sgpr_count;
+ if (sgprs > 0 && occ.sgprs_per_simd > 0)
+ {
+ /* Each wave is allocated one more block than it needs. */
+ sgprs = ((sgprs + occ.sgpr_granularity - 1) / occ.sgpr_granularity
+ * occ.sgpr_granularity + occ.sgpr_granularity);
+ if (occ.sgprs_per_simd / sgprs < waves)
+ waves = occ.sgprs_per_simd / sgprs;
+ }
+
+ return waves * occ.simds_per_cu;
+}
+
/* }}} */
/* {{{ Run */
struct gomp_offload_session
@@ -2427,16 +2488,15 @@ run_kernel (struct gomp_offload_session *session,
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)
+ registers available to run the kernels together. */
+ if (kla->ndim == 3)
{
- int max_vgprs = max_isa_vgprs (agent->device_isa);
- int granulated_vgprs = (kernel->description->vpgr_count + 3) & ~3;
- int max_threads = (max_vgprs / granulated_vgprs) * 4;
+ int max_threads = kernel_waves_per_cu (agent, kernel);
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",
+ GCN_WARNING ("Too many registers 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;
}
--
2.54.0