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

    <tr>
        <th>Summary</th>
        <td>
            Windows AArch64 frame lowering fails to align large stack frames
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    It appears that if `__chkstk` is called, and the frame requires an alignment greater than 16, the `and` operation to align `sp` is missing. This causes a crash [in x265](https://bitbucket.org/multicoreware/x265_git/src/9b59d45549f460e41a852cfd276f9b89eed2112a/source/encoder/sao.cpp#lines-786:808) (autovectorization leads to writing to `(diff | 16) - 16`), which was reported as https://github.com/HandBrake/HandBrake/issues/3692

clang arguments: `-target arm64-pc-windows -O2`

```c
int external_function(char *p);

void basic_example() {
    char data[4096] __attribute__((aligned(32)));
    external_function(data + 0x10);
}
```

```
basic_example:
    // no "and" instruction in prolog to ensure 32-byte alignment
    str     x28, [sp, #-32]!
    stp     x29, x30, [sp, #8]
    add     x29, sp, #8
    mov     x15, #256
    bl      __chkstk
    sub     sp, sp, x15, lsl #4

    // assumes 32-bit alignment, and transforms "add" to "orr"
    mov     x8, sp
    orr     x0, x8, #0x10
    bl      external_function

    add     sp, sp, #1, lsl #12
    ldp     x29, x30, [sp, #8]
    ldr     x28, [sp], #32
    ret
```

([from Compiler Explorer](https://godbolt.org/z/EPsd51bE7))

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVU2TozYQ_TX4orILxIfh4IPnI5WckkOqcnQJqTHaEYhIYuzdX59u4Rkzs1NbFRcGie5-6n56alqrvh_-CExMEwjnWehFYLpjSZWeTrJ_8eEFh0x7JoUxoBL-yMSo0A9Y58QAzMG_s3bg8TUTRp_HAcbAzg5EAEd4I8sqCqMQxMJoQrQTOBG0HVmwSxwZ_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