← Back to Pull requests

[offload][OpenMP] Improve cross-team reduction grid selection #208253

Closed
ro-i closed this pull request without merging on Nov 17, 2026 10:03 UTC
View on GitHub
ro-i ro-i commented on Jul 08, 2026 16:23 UTC
Author

The default cross-team reduction algorithm benefits from larger and fewer teams. Implement that by using 2 x the default number of threads and 1/2 x the default number of teams for reduction kernels. This doesn't change the default total number of threads, it just redistributes them.

This is a first, rather simple heuristic, derived from (a subset of) what ROCm does.
The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942
(c71339705091500f731e2a39f247d2660bacbdce) are up to a few percent, with no regressions.

Claude assisted with this patch.

Diff: 114 lines (+110 / -4) across 4 files Reviewer: dhruvachak Review duration: 189700m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.41) × (1 − Review Confidence (0.85)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.41 · 114 lines diff across 4 files
0.41
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.75 · 5 substantive comment(s)
0.75
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
85%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.284
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
12 items
llvmorg-github-actions[bot] llvmorg-github-actions[bot] commented on Jul 08, 2026 16:23 UTC
Bot

<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-offload

Author: Robert Imschweiler (ro-i)

<details>
<summary>Changes</summary>

The default cross-team reduction algorithm benefits from larger and fewer teams. Implement that by using 2 x the default number of threads and 1/2 x the default number of teams for reduction kernels. This doesn't change the default total number of threads, it just redistributes them.

This is a first, rather simple heuristic, derived from (a subset of) what AOMP does.
The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942
(c71339705091500f731e2a39f247d2660bacbdce) are up to a few percent, with no regressions.

Claude assisted with this patch.

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

3 Files Affected:

  • (modified) clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp (+22)

  • (modified) offload/plugins-nextgen/common/include/PluginInterface.h (+5)

  • (modified) offload/plugins-nextgen/common/src/PluginInterface.cpp (+22-4)
  • ``````

    The server is unavailable at this time. Please wait a few minutes before you try again.
    
    ``````

    </details>

    dhruvachak dhruvachak commented on Jul 11, 2026 18:08 UTC
    Contributor

    This is a first, rather simple heuristic, derived from (a subset of) what AOMP does.

    I would say ROCm instead of AOMP.

    dhruvachak dhruvachak commented on Jul 11, 2026 18:11 UTC
    Contributor

    Can you add a test?

    ro-i ro-i commented on Jul 12, 2026 14:57 UTC
    Author

    Can you add a test?

    yeah, this should now be possible without depending on specific hardware. I initially had no test because I was following a different selection strategy, but with the current redistribution, it's not a problem anymore. Thanks for the reminder!

    dhruvachak dhruvachak commented on code on Jul 12, 2026 21:01 UTC
    Code Review
    clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp:1
    @@ -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;

    Clamp it to GV_Max_WG_Size.

    A more general concern is whether it makes sense to tie the reduction kernel wg_size to the default wg_size. If you think that reduction kernels should default to a different wg_size, perhaps use a different wg_size independently. Otherwise, what happens if in the future, the default_wg_size is changed? Would reduction kernels still benefit from the doubling?

    dhruvachak dhruvachak commented on code on Jul 12, 2026 21:09 UTC
    Code Review
    offload/test/offloading/xteam_reduction_default_grid.c:1
    @@ -0,0 +1,61 @@
    +// RUN: %libomptarget-compile-generic
    +// RUN: env LIBOMPTARGET_INFO=16 \
    +// RUN:   %libomptarget-run-generic 2>&1 | %fcheck-generic
    +// RUN: %libomptarget-compileopt-generic
    +// RUN: env LIBOMPTARGET_INFO=16 \
    +// RUN:   %libomptarget-run-generic 2>&1 | %fcheck-generic
    +
    +// REQUIRES: amdgpu

    Can you split the test so that target-agnostic checks work across targets and possibly have target-specific tests?

    dhruvachak dhruvachak commented on Jul 12, 2026 21:10 UTC
    Contributor

    Re: The performance benefits I observed for the reduction tests in https://github.com/ro-i/xteam-test on a gfx942

    What about other targets since they are impacted as well?

    ro-i ro-i commented on code on Jul 12, 2026 21:36 UTC
    Code Review
    clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp:1
    @@ -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;

    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 a lot of different 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.

    dhruvachak dhruvachak commented on code on Jul 15, 2026 18:21 UTC
    Code Review
    clang/lib/CodeGen/CGOpenMPRuntimeGPU.cpp:1
    @@ -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;

    Agreed, that's a reasonable choice. But clamp it to GV_Max_WG_Size.

    dhruvachak dhruvachak commented on code on Jul 16, 2026 00:00 UTC
    Code Review
    offload/plugins-nextgen/common/src/PluginInterface.cpp:1
    @@ -422,6 +427,19 @@ uint32_t GenericKernelTy::getEffectiveNumBlocks(
           // will execute one iteration of the loop; rounded up to the nearest
           // integer. However, if that results in too few blocks, we artificially
           // reduce the thread count per block to increase the outer parallelism.
    +
    +      // Favor fewer, larger teams for cross-team reductions. As a heuristic, we
    +      // aim for double the default number of threads and half the default
    +      // number of blocks.
    +      if (isCrossTeamReduction()) {
    +        // Never launch more teams than the trip count needs.
    +        uint64_t NumBlocks =

    Your change halves the number of blocks even if the blocksize could not be increased for some reason. To guard against that problem, guard it like below. Additionally, instead of a hardcoded 2, use a scale-factor.

    if (isCrossTeamReduction() && EffectiveNumThreads > PreferredNumThreads) {
      uint64_t Scaled = std::max<uint64_t>(
          (uint64_t(DefaultNumBlocks) * PreferredNumThreads) / EffectiveNumThreads, 1);
      uint64_t NumBlocks = std::min<uint64_t>(
          Scaled, ((LoopTripCount - 1) / EffectiveNumThreads) + 1);
      return std::min(NumBlocks, uint64_t(GenericDevice.getBlockLimit(EffectiveNumThreads)));
    }
    

    ro-i ro-i commented on Nov 17, 2026 10:03 UTC
    Author

    this has been superseded by my other PRs

    Reviewers
    Assignees
    ro-i ro-i
    Labels
    risk: low confidence: 85% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 40.8% (MEDIUM) · Higher = larger blast radius
    40.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 85% · Lower = rushed or shallow review
    85%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.1% · Safe to merge
    6.1%