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

    <tr>
        <th>Summary</th>
        <td>
            [Clang][RISCV] ABI lowering implementation doesn't conform to latest ABI spec about integer argument passing in stack
        </td>
    </tr>

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

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

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

<pre>
    As the [ABI](https://github.com/riscv-non-isa/riscv-elf-psabi-doc/blob/master/riscv-cc.adoc#integer-calling-convention) described:
> When passed in registers or on the stack, integer scalars narrower than XLEN bits are widened according to the sign of their type up to 32 bits, then sign-extended to XLEN bits.

But clang's implementation doesn't conform to this rule. For example:

```
$ cat a.c
void foo(short a0, short a1, short a2, short a3, short a4, short a5, short a6, short a7, short a8);

void test() {
  foo(1, 1, 1, 1, 1, 1, 1, 1, 1);
}
```

```
$ clang --target=riscv64 -emit-llvm -S a.c -o -
; ModuleID = 'a.c'
source_filename = "a.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n64-S128"
target triple = "riscv64"

; Function Attrs: noinline nounwind optnone
define dso_local void @test() #0 {
entry:
  call void @foo(i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef signext 1, i16 noundef 1)
  ret void
}

declare dso_local void @foo(i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef signext, i16 noundef) #1

attributes #0 = { noinline nounwind optnone "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+64bit,+a,+c,+m,+relax,-save-restore" }
attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-features"="+64bit,+a,+c,+m,+relax,-save-restore" }

!llvm.module.flags = !{!0, !1, !2, !3}
!llvm.ident = !{!4}

!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 1, !"target-abi", !"lp64"}
!2 = !{i32 7, !"frame-pointer", i32 2}
!3 = !{i32 1, !"SmallDataLimit", i32 8}
!4 = !{!"clang version 16.0.0 (https://github.com/llvm/llvm-project.git 1dbe8561e7b1190da33d2e248f171816ea9d108f)"}
```

We can see the last argument which is passed in stack does not have the `signext` attribute like others.

I think this is because the rule I mention above is introduced to RISCV's ABI spec after clang's current implementation. See this: https://github.com/riscv-non-isa/riscv-elf-psabi-doc/commit/cec39a064ee0e5b0129973fffab7e3ad1710498f

I do not find such a problem in backend, so I wonder this issue is worth fixing. How about your opinions?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVV8GO2zYQ_Rr5QkgQJVmSDz6s4yy6QNpDF2hyCyhqZLOhSIOk7N1-fYeULMtJtkjRSwMYIiUOh8M3j8PnRrev2wdL3BFItN497J6i9T7K6qNzJxvlD1H2iL-DcMehSbju8cUIy8-x0ioWls3vILv4ZFkj4lZz_NpI3WDTM-vAzFacJyyM50I5OICJOZNSqEPMtTqDckKrKNuQFiw3ooHWh5Duo_Qhyt-Tj0dQ5MSshZYIRQwchPduiTZEq7AH6xj_EmXvyOSfWFyAoYlixugLfnBHpsinD-9_I41wljAD5CJaUOiTca5Ni9EQp0dv4qCI7nxf4MzXE5Dh5AfzLMz2CzkflDeM4cWBatEPGswLJFP44bkbHOGSqUOUVZaI_iShx00zv23SarC4-QpNtOq06ccohCVmkJCQR9wlvDA_6YbK-CzT6Te-ZgXhzBGW8PHDWYuWdFpjXu1RGxxJfeRTny762aKfL_rFor9e9MtFv1r0a0xilO-WQYYgHFiHUfgUR9U0TKbQQhw_9lj4rvbfx-BNYDz8JI4dMwdwUb4PxCwLEkMvXCzluSfxs8eOxJrEV_LtyK-6xTQ87QnOIZgmj25WjeNWD4b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