[all-commits] [llvm/llvm-project] afbcf9: [OpenMP 6.0 ]Codegen for Reduction over private va...

CHANDRA GHALE via All-commits all-commits at lists.llvm.org
Wed Jun 11 01:31:53 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: afbcf9529a1edb88d067e6fca8d9534901310d5e
      https://github.com/llvm/llvm-project/commit/afbcf9529a1edb88d067e6fca8d9534901310d5e
  Author: CHANDRA GHALE <chandra.nitdgp at gmail.com>
  Date:   2025-06-11 (Wed, 11 Jun 2025)

  Changed paths:
    M clang/docs/OpenMPSupport.rst
    M clang/docs/ReleaseNotes.rst
    M clang/lib/CodeGen/CGOpenMPRuntime.cpp
    M clang/lib/CodeGen/CGOpenMPRuntime.h
    M clang/lib/CodeGen/CGStmtOpenMP.cpp
    M clang/lib/Sema/SemaOpenMP.cpp
    M clang/test/OpenMP/distribute_simd_misc_messages.c
    A clang/test/OpenMP/for_private_reduction_codegen.cpp
    M clang/test/OpenMP/for_reduction_messages.cpp
    M clang/test/OpenMP/for_simd_reduction_messages.cpp
    M clang/test/OpenMP/sections_reduction_messages.cpp
    A openmp/runtime/test/worksharing/for/omp_for_private_reduction.cpp

  Log Message:
  -----------
  [OpenMP 6.0 ]Codegen for Reduction over private variables with reduction clause (#134709)

Codegen support for reduction over private variable with reduction
clause. Section 7.6.10 in in OpenMP 6.0 spec.
- An internal shared copy is initialized with an initializer value.
- The shared copy is updated by combining its value with the values from
the private copies created by the clause.
- Once an encountering thread verifies that all updates are complete,
its original list item is updated by merging its value with that of the
shared copy and then broadcast to all threads.

Sample Test Case from OpenMP 6.0 Example 
```
#include <assert.h>
#include <omp.h>
#define N 10

void do_red(int n, int *v, int &sum_v)
{
    sum_v = 0; // sum_v is private
    #pragma omp for reduction(original(private),+: sum_v)
    for (int i = 0; i < n; i++) 
    {
        sum_v += v[i];
    }
}

int main(void)
{
    int v[N];
    for (int i = 0; i < N; i++)
        v[i] = i;
    #pragma omp parallel num_threads(4)
    {
        int s_v; // s_v is private
        do_red(N, v, s_v);
        assert(s_v == 45);
    }
    return 0;
}
```
Expected Codegen:
```
 // A shared global/static variable is introduced for the reduction result.
 // This variable is initialized (e.g., using memset or a UDR initializer)
 // e.g., .omp.reduction.internal_private_var

 // Barrier before any thread performs combination
  call void @__kmpc_barrier(...)

 // Initialization block (executed by thread 0)
 // e.g., call void @llvm.memset.p0.i64(...) or call @udr_initializer(...)

  call void @__kmpc_critical(...)
    // Inside critical section:
    // Load the current value from the shared variable
    // Load the thread-local private variable's value
    // Perform the reduction operation 
    // Store the result back to the shared variable

  call void @__kmpc_end_critical(...)
  // Barrier after all threads complete their combinations

  call void @__kmpc_barrier(...)
 // Broadcast phase:
 // Load the final result from the shared variable)
 // Store the final result to the original private variable in each thread
 // Final barrier after broadcast

  call void @__kmpc_barrier(...)
```

---------

Co-authored-by: Chandra Ghale <ghale at pe31.hpc.amslabs.hpecorp.net>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list