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

    <tr>
        <th>Summary</th>
        <td>
            [ARM] Incorrect calling convention when invoking __sync builtins
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:ARM
      </td>
    </tr>

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

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

<pre>
    ## IR

```llvm
define void @foo(ptr %a, i32 %x) {
  %val = trunc i32 %x to i8
  %1 = atomicrmw max ptr %a, i8 %val seq_cst
  ret void
}
```

## Asm

```asm
foo:
        push    {r11, lr}
        bl      __sync_fetch_and_max_1
        pop     {r11, lr}
        mov     pc, lr
```

## The problem

The ARM calling convention specified that integers smaller than the word size need to be sign/zero-extended to the word size. `__sync_fetch_and_max_1` expects a signed `i8` for its second argument, which should be in sign-extended form. However LLVM isn't performing a sign-extension for this parameter, which leads to issues such as https://github.com/rust-lang/rust/issues/100650.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVFGPozgM_jXhxZoqJIUyDzx0rqpupZ2X0eleqxAM5DYkXBza7v76U6Dd2a72dhAikf19tj_LWBGZ3iHWrHhhxSFTcxx8qPejcgbnrPHt15oJyYSET2-MHxjf374lX19rz-NqarEzDuHsTQtsyzvvmaimGICJQjHxBxgp0v3KxDOw3cvKgmQ6KwtMHiCG2envOIgeTPUDLF9AKvrR6DBeYFRXeEhQ3YMR_nvSFO_cgHGp61b77vCTiAdlq9w9jb_Uq-72pE_u7xnWZ5ppSCfbvYQ8TyXZ8D3bHdTY9Tyd6KvTpw6jHk7KtadRXU_5TwH9BB8GHP15Besb4ENxfw0IU_CNxQeRybx_ewWtrDWuB-3dGV003gFNqE1nsIU4qAjGRewxENCorMWQrA7igHDxoQUy3xAcJrSHBiENGRPHbxj8E14junZ1PRA2wEr-P00pOeB1Qh0J1BIN24Q2VfJ0PoCJBITauxZU6OcRXUzNuAxGD0CDn22bCjFuYb8X0fkwbuBPf8EzBvj8-e9XMOSY2EWYMCRvaoT6gUWpHSllHAzBpIIaMWJ4z2ZRtbTMLtGMBDTrARTBEONEaWTEkYljb-IwNxvtRyaOYab4ZJXrb3cmjiuZiWPOeVnwTdbWsn2WzyrDOi8rLkVe5EU21G3Fpcp3Ocquw12lEJUoBdfNtqr4VqrM1IILybc857KoZLHpyl1XbAuuGqU7WSHbchyVsZv0L2986LMleV3mVcUzqxq0tCwIIRqlv6Brmdzv316ZEGlnhDrxnpq5J7bl1lCk90jRRLtsl4QvDvDJaR8C6virIbsM6MC4s_-SHOsoQDMbG42jbA62_k0Pl0W0Hk9T8P-gfmjjIua_AAAA__8jmn_k">