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

    <tr>
        <th>Summary</th>
        <td>
            [clang++] Loop vectorization not happening in outlined lambda function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang,
            clang:frontend,
            clang:codegen
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          kavitha-natarajan
      </td>
    </tr>
</table>

<pre>
    void foo(int *A, int *B, int *C, int *D, int len) {
  auto func = [&] () {
 for (int i =0; i < len; i++)
      A[i] = B[i] * C[i] + D[i];
  };
 func();
}

$ clang++ lambda.cpp -O3 -c -S -emit-llvm -fno-unroll-loops -fno-inline

The issue is lambda is outlined into another function due to which variable "len" is loaded inside the loop. Loop bounds are not determined due to this load (compiler might be expecting alias to the load instruction). 

The original problem is in Geekbench 6.1 src/geekbench/ml/backend/cpu/depthwise_convolution_2d.cpp Line no: 402. The reason for adding "-fno-unroll-loops -fno-inline" in the above code is that for smaller functions, lambda is inlined and there is no problem in vectorization. In Geekbench, inlining doesn't happen for lambda, but outlined, and hence fails to vectorize. A good performance gain is expected in Geekbench if vectorization happens for this loop.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VE-Po7gT_TTOpQQCQzqdAwfSUX76SSPtYfc-KnABnjYuZJvM7n76lQ3pZPawEQpVxn7v1T-j93q0RI04XsTxesA1TOyaT7zrMGFmMaDDH2gPHau_mjtrBQOzkO_aBhCybYX8gN2-vNgfL_b1YRuyQp5BnC6iaAFwDQzDansQ1RWiAPkmjlcQ8v1l28AOdj4dNxaiuiTrI-FFR8hLes4JNv5acbzohFVd4fLlyBY-ns4Frrsjqk2ROD3MKGvTsS3EL0UbH1lDb9COGykYnDuFeb8skP1WQdZD9jtkNOuQGXOfIRssZ6t1bExmmBe_rWhrtKUN8o-JQHu_xv8dL1q8h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