<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58233>58233</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [OpenMP IRBuilder] Aggregate struct not filled in serialized parallel region
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            openmp,
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          kiranchandramohan
      </td>
    </tr>
</table>

<pre>
    While outlining a parallel region, the liveins were passed by value. This caused some issues since the ABI sometimes limits how many variables can be passed in registers. A solution to this issue is to pass the arguments in a struct. In https://reviews.llvm.org/D110114, the Extractor in llvm is used to outline and aggregate and it does this for the parallel region. When the `if` clause is present there is a serial region as well, we have to call the outlined function from here as well. While for the original parallel region the extractor filled the aggregate struct in llvm, in the serial region it is not filled. So we have to do this manually.

The initial attempt (https://reviews.llvm.org/D129285) to fix this was not robust. We have two ways to proceed,
1. Copy the filling of the aggregate struct in the serialized region.
2. Push the handling of the serial region into the OpenMP runtime.

Incorrect IR generated: The call to `@sb_..omp_par` from `kmpc_fork_call` in block `omp_parallel` has the aggregate filled in while the call to `@sb_..omp_par` in the serialized parallel region in block `6` does not have this.
```
define void @sb_(ptr %0, ptr %1) !dbg !3 {
  %structArg = alloca { ptr }, align 8
  %tid.addr = alloca i32, align 4, !dbg !7
  %zero.addr = alloca i32, align 4, !dbg !7
  store i32 0, ptr %tid.addr, align 4, !dbg !7
  store i32 0, ptr %zero.addr, align 4, !dbg !7
  %3 = load i32, ptr %0, align 4, !dbg !7
  %4 = icmp ne i32 %3, 0, !dbg !7                 
  br label %entry, !dbg !7

entry:                                            ; preds = %2
  %omp_global_thread_num = call i32 @__kmpc_global_thread_num(ptr @1), !dbg !7
  br i1 %4, label %5, label %6

5:                                                ; preds = %entry
  br label %omp_parallel

omp_parallel:                                     ; preds = %5
  %gep_ = getelementptr { ptr }, ptr %structArg, i32 0, i32 0
  store ptr %1, ptr %gep_, align 8
  call void (ptr, i32, ptr, ...) @__kmpc_fork_call(ptr @1, i32 1, ptr @sb_..omp_par, ptr %structArg), !dbg !9
  br label %omp.par.outlined.exit

omp.par.outlined.exit:                            ; preds = %omp_parallel
  br label %omp.par.exit.split

omp.par.exit.split:                               ; preds = %omp.par.outlined.exit
  br label %7

6:                                                ; preds = %entry
  call void @__kmpc_serialized_parallel(ptr @1, i32 %omp_global_thread_num)
  call void @sb_..omp_par(ptr %tid.addr, ptr %zero.addr, ptr %structArg), !dbg !9
  call void @__kmpc_end_serialized_parallel(ptr @1, i32 %omp_global_thread_num)
  br label %7

7:                                                ; preds = %6, %omp.par.exit.split
  ret void, !dbg !10
}
```

While this is a general OpenMP IRBuilder issue, the primary use case is the OpenMP lowering in flang.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytV0tv4zgM_jXORVjDjzwPObTTHaCHxQ5mB-gxkC3F1laWDElumvn1S0p24qTOtjNoEPghieRH8hNNFZodt0-1kJzozkmhhKoIJS01VEouieGV0CrKvhBXcyLFCxfKkgM3HNZYyxkpjuSFyo7H5EctLClph6NWN5wIaztuiRWq5F7-7v7RzzjRwLgUjXCW1PpAGqpQjRG0kByVKFKcLAjlYVjHjY3JHWiQnQNUxGnQCja9HbjiAMp4W9RUXcMVGAB5SqwzXeli8qhI7Vxro_wuyr7C3_AXwQ82lvKlibWpYOghTZM0nQ9e__nqDC2dNqgJl6Ep7yXYC1EDc4oRWlUAlLrwJhxhmtsAcQ_SqOsqsDF5qrnyM9EyEXu4kFJiCNFGa7gFD3Da-AFwg0OMBmlCMRVSItADJzV94QipBAteZY-NkX2nSh-xvdEN8dp6UQSAyR_waSMqocDCFVA_yU-B2AuYYyHOJ6dDiIcgISgR5C5BQ1zAFaVdryUm_-gxfNZnFTjRAYRjHCUPUXIXrj9AHZDUoT7qHG9aR6Js_YGUZptsvYiyDZrYi9dg40ADEqOLzgI7ngYYB4BEj4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