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

    <tr>
        <th>Summary</th>
        <td>
            "Instruction does not dominate" error with multi-dimensional reductions
        </td>
    </tr>

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

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

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

<pre>
    Recently, reductions with multi-dimensional arrays started to fail with flang. Reproducer:
```
    subroutine parallel_reduction(a, x, N, M)
        use omp_lib
        implicit none
        integer, intent(in) :: N, M
 real, intent(in) :: a(N, M)
        real, intent(out) :: x(M)
 integer :: i, j
        
        x = 0.0
        
        !$OMP PARALLEL DO REDUCTION(+:x)
        do j = 1, M
            do i = 1, N
 x(j) = x(j) + a(i, j)
            end do
        end do
        !$OMP END PARALLEL DO
    end subroutine parallel_reduction
```
How to reproduce:
```
$ flang -c -fopenmp reduction-sub.f90 
flang-20: warning: OpenMP support in flang is still experimental [-Wexperimental-option]
Instruction does not dominate all uses!
  %73 = alloca { ptr, i64, i32, i8, i8, i8, i8, [1 x [3 x i64]] }, i64 1, align 8
 call void @llvm.memcpy.p0.p0.i32(ptr %34, ptr %73, i32 48, i1 false)
error: failed to create the LLVM module
```
Information about the flang compiler that can reproduce the issue (any recent will probably work):
```
$ flang --version
flang version 20.0.0git (https://github.com/llvm/llvm-project.git bdf727065b581c45b68a81090272f497f1ce5485)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/eepshteyn/src/flang-upstream/build/bin
Build config: +unoptimized, +assertions
```
"git bisect" pointed to the following commit that caused the regression:
```
commit 7f72d71de7c3b7d36d9f463b1459a6d2f6c989e6 (HEAD)
Author: Kareem Ergawy <kareem.ergawy@amd.com>
Date:   Thu Dec 5 05:23:49 2024 +0100

    [OpenMP][OMPIRBuilder] Refactor reduction initialization logic into one util (#118447)
    
    This refactors the logic needed to emit init logic for reductions by
    moving some duplicated code into a shared util. The logic for doing is
    quite involved and is needed for any construct that has reductions.
    Moreover, when a construct has both private and reduction clauses, both
    sets of clauses need to cooperate with each other when emitting the
    logic needed for allocation and initialization. Therefore, this PR
    clearly sets the boundaries for the logic needed to initialize
    reductions.
```
github commit: https://github.com/llvm/llvm-project/commit/7f72d71de7c3b7d36d9f463b1459a6d2f6c989e6

github PR: https://github.com/llvm/llvm-project/pull/118447


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJycVl1v2zrS_jXMzcCGREmWfOELu45xijdpgiDv7mVBiSOLLUVqSSqOz69fDCUnzqan2LOGYVkkZ-aZr2covFdHg7hhxY5xju4oTmfGOSv2N2IMnXUbHI9oEAffBTybm9rK8-YJGzRBnxn_Ag7l2ARljYeTCh30ow5qIVWPxitrhAbhnDh78EG4gBKChVYoPZ1utTDHJTzh4KwcG3Qs27Jky1bJ_E22AAB-rJ0dgzIIg3BCa9Tf3wwzXglC8ko_3-jnnvH1LEmf0SPYfviuVX21qvpBq0YFMNbg9boJeERHeuivCYxXyjC-BsKWbS8mki04FPqvzwnGq1_B-SRlx3Al9sp4dZGYsVy2FIn9uFJ19fcVWLaHZJn8epvxlPH84f4RHrdP27u72zvYP8DT7f7_vzx_ffjGeMX4jmXb149YpYUfUXH65vTVR1pQ77vfaJfQ_5i82V-98F0Mx-zABxP0QSNB2qvFTwvv-G-_7a99mM-QwO-r5GNV_WFPVInuUnif647xfCpPWDSwaO2Aph_ey33hx3rZrhMKcjy24Anl6CScUeZIfx8GNPeP4MdhsC6AMrM-Rc2gtAZ8HdBRpwShgRW7xT-vVxZ2iMCLPUu2X40PbrIM0qIHYwNI2ysjAoLQmqrcU5QoHowXZRZTILS2jQBW7mAIU1Gv8vjIeHxUv_5lxS6lmip2GbxGmWLPij2wcj8rmZIutDoaqMhqQyherJLA8kTrl37ZY98M5-WQ0DcarIbgCF0WMcw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