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

    <tr>
        <th>Summary</th>
        <td>
            Broken epilog vector body missing %resume.val contribution in phi
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AArch64,
            miscompilation,
            vectorization,
            SVE
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          peterwaller-arm
      </td>
    </tr>
</table>

<pre>
    I have bisected a case of a checksum failure in a non-trivial internal C++ workload to a recent change, 582f8ef19fe791e66c0bbe090edebfd74ba2b55d (`[LV] Keep track of cost-based ScalarAfterVec in VPWidenPointerInd`, committed Mon Sep 19 18:14:35 2022 +0100).

I have made a minimal IR reproducer, which shows the issue when fed to `bin/opt -passes='function(loop-vectorize),default<O2>' -S` (though `default<O2>` is not necessary to induce the issue, that is what is shown below).

```llvm
target triple = "aarch64"

define void @repro_issue_57912(ptr %arg, i64 %arg1) "target-features"="+sve,+sve2" {
entry:
  br label %loop.body
loop.body:
  %ind = phi i64 [ %ind.next, %loop.body ], [ 0, %entry ]
  %gep = phi ptr [ %gep.next, %loop.body ], [ %arg, %entry ]
  store i8 0, ptr %gep, align 1
  %gep.next = getelementptr inbounds i8, ptr %gep, i64 1
  %ind.next = add nuw nsw i64 %ind, 1
  %cmp = icmp eq i64 %ind, %arg1
  br i1 %cmp, label %exit, label %loop.body
exit:
  ret void
}
```

On main this produces the following epilog loop:

```llvm
vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %pointer.phi11 = phi ptr [ %arg, %vec.epilog.ph ], [ %ptr.ind12, %vec.epilog.vector.body ]
  %index10 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next16, %vec.epilog.vector.body ]
  store <vscale x 8 x i8> zeroinitializer, ptr %pointer.phi11, align 1
  %index.next16 = add nuw i64 %index10, %2
  %ptr.ind12 = getelementptr i8, ptr %pointer.phi11, i64 %2
  %9 = icmp eq i64 %index.next16, %n.vec6
  br i1 %9, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !2
```

With a revert of 582f8ef19fe791e66c0bbe090edebfd74ba2b55d on main, I get the following, for which the workload does not have a checksum failure:

```llvm
vec.epilog.vector.body:                           ; preds = %vec.epilog.vector.body, %vec.epilog.ph
  %index8 = phi i64 [ %vec.epilog.resume.val, %vec.epilog.ph ], [ %index.next10, %vec.epilog.vector.body ]
  %next.gep9 = getelementptr i8, ptr %arg, i64 %index8
  store <vscale x 8 x i8> zeroinitializer, ptr %next.gep9, align 1
  %index.next10 = add nuw i64 %index8, %2
  %9 = icmp eq i64 %index.next10, %n.vec4
  br i1 %9, label %vec.epilog.middle.block, label %vec.epilog.vector.body, !llvm.loop !2
```

What I see is that in the broken case, the `%pointer.phi11` begins at `%arg`, and in the working case, it the equivalent store destination `%next.gep9` begins at `%arg + %vec.epilog.resume.val`. So it appears there is a problem with how the phi has been constructed in this epilog body, and it is not commencing where the main vector body has left off as it should.

cc @fhahn - My guess is that the patch is not at fault but has revealed a bug in epilog vectorization, do you concur? Would it be possible to revert your patch until a fix can be found? Thanks in advance.

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNV9tu2zgQ_Rr7hbChi2VbD37IpQWC3aLFZtE-FpQ0sriRSZek7GS_fs9Qcnxp0nSxWKCBY0u8zJy5HHKmMNXT6k40ckeiUI5KT5WQopSOhKn5qaHywXUbUUvVdpaE0hjVRk-8VTslWwx4shoPN6PkGh-xN_ahNbIS3mCppZK0hxyp1zRKbkS2TOol1XFe0yKPaT4vo6KgKI-ooqKuFrNCJkWWVWKULEfzaJRd__55lN2K34i2wltZPjCy0jg_KQCzEvelbKW9qgHjM5UM8POnL6oi_ckEbHe6YjlQXZrNRnk28YPR4h7y4lzEy1F6Fc_wlWYiiZIEiq-jOMKOfDqKbkfRVf89uGkjK4J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