[clang] [llvm] [offload][OpenMP] Improve cross-team reduction grid selection (PR #208253)

Robert Imschweiler via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 12 14:37:05 PDT 2026


================
@@ -751,6 +764,15 @@ void CGOpenMPRuntimeGPU::emitKernelInit(const OMPExecutableDirective &D,
              : llvm::omp::OMPTgtExecModeFlags::OMP_TGT_EXEC_MODE_GENERIC;
   computeMinAndMaxThreadsAndTeams(D, CGF, Attrs);
 
+  // Set MaxThreads to twice the default workgroup size for SPMD cross-team
+  // reductions. For these reductions, we want to have fewer, larger teams to
+  // make better use of intra-team reduction and not over-emphasize the
+  // inter-team part. Note that this only buys us more freedom. The actual grid
+  // size for the launch is going to be chosen later by the offload plugin.
+  if (IsSPMD && Attrs.MaxThreads.front() < 0 && hasTeamsReduction(D))
+    Attrs.MaxThreads.front() =
+        2 * CGM.getTarget().getGridValue().GV_Default_WG_Size;
----------------
ro-i wrote:

I actually previously followed the strategy to aim for 50% occupancy (using getHardwareParallelism()). But it seemed too random.
Being based on the default total number of threads means that the reductions don't arbitrarily aim for a different total number of threads. They just redistribute that total number by doubling the team size and cutting the number of teams in half.
The reason why this makes sense is that we know the algorithm that is executed by the cross-team reduction and that it benefits from a tendency towards larger and fewer teams due to less inter-team synchronization and less work in the final team.
This is far from perfect. It's just re-using the simple rule that we have for ROCm in a more portable and less arbitrary way. See e.g. https://github.com/ROCm/llvm-project/blob/7f90160e0c374525bbfd7d3485949f9130cb732b/llvm/include/llvm/Frontend/OpenMP/OMPConstants.h#L312. Unfortunately, ROCm has 1000 aspects and tuning knobs that affect the actual grid layout. As I mentioned, I'm trying to get started with some conservative and simple rule.

https://github.com/llvm/llvm-project/pull/208253


More information about the cfe-commits mailing list