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

    <tr>
        <th>Summary</th>
        <td>
            [flang]Array copies are slow
        </td>
    </tr>

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

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

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

<pre>
    Analyzing why spec-2017 527/cam4_r is ~6x slower than using another Fortran compiler, I found that the vast majrorfy of the tiem (about 80% of the total time for the Flang-compiled version of code). 

```
program demo
  implicit none 

  integer, parameter :: x   = 100
  real, pointer, dimension(:) :: tt1, tt2, rr
  integer :: i

  do i = 1,2
     if (i .eq. 1) then
        allocate(tt1(x))
        tt1 = 2
     else
        allocate(tt2(x))
        tt2 = 1
     end if
  end do
  
  do i = 1, 10000000
     rr => tt1
     tt2 = rr
  end do
end program demo
```

This results in a temporary array, same size as `tt1` being allocated, then `tt1` copied into the temporary, and this temporary is then copied to `tt2`.

This doesn't appear to be a Alias-Analysis issue, as even when I hard-code the AA to "never alias", it still makes a copy - it appears that it's something to do with "unpacking" (given the names in LLVM-IR), but the data in this case isn't actually stored in any different way, we're just reading from location `R0+X` storing it in `R1+X`, then reading from `R1+X` and storing in `R2+X `- where `R0` is the address allocated by `tt1`, `R1` is the temporary variable, and `R2` is `tt2`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx9VUuP2zgM_jXOhUhgK3EehxzSpgEGmF6KYrG3BW3Riaay5ZXkyaS_fkk5mWTa3TUMx-bzI_lRqZy-bHcd2stP0x3hfLpA6KmeqrxYQalWmTrU2C7-8mACZKsvyzcI1p3JQzxhB0MQL-xcPLHo4Hz0LK1d2xtLPlOf4QkaN3RazCM_CF4xRGjxxTvfXMA1SRgNtZCpNVZuiLDOM1W-q1xEywYtcSSfRAeL3XF6zaLhlXwwrhO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