Skip to content

[Mono] [RISCV] Fix abi issues (Monthly pr) #88649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 53 commits into from
Jul 24, 2023
Merged

Conversation

Xinlong-Wu
Copy link
Contributor

  • use 'ld/lw' to load both signed and unsigned arguments as abi specific.
  • Emit 'lwu' only when zero extends
  • Fix imm check
  • Fix order of parameters on the stack
  • Lowering/output some institutions

In this pr, Mono can run 'default-interface-members-versions' and 'exploration' of repo https://github.com/dotnet/samples

More information plz refer to https://github.com/Xinlong-Wu/runtime/blob/riscv-jit-rv64/RISCV64-PORTING.md

process stack size larger than imm32
fix mono_riscv_emit_branch_exc
lowering OP_XOR_IMM&OP_IXOR_IMM
- OP_FCONV_TO_I8
- OP_LXOR_IMM
- OP_ISHR
- relaxed_nop
- float_cle
- long_shl
- OP_ICONV_TO_R_UN
- OP_LCONV_TO_I2
- OP_FBGE
- OP_IDIV_IMM
- The ABI specify that If the reture value would have been passed by reference, the caller will allocates memory for the return value, and passes the address as an implicit first parameter.
- The Mono will treat first parameter as this_pointer reference to `mono_vcall_trampoline()`.  They are conflict a bit.
OP_ATOMIC_LOAD_U1
OP_IREM_UN_IMM
OP_IREM -> riscv_remw
OP_IREM_UN -> riscv_remuw
use mono_riscv_emit_loadu to emit lwu
fix rem
@ghost ghost added community-contribution Indicates that the PR has been added by a community member area-VM-meta-mono labels Jul 11, 2023
@@ -393,7 +393,11 @@ mono_type_to_load_membase (MonoCompile *cfg, MonoType *type)
case MONO_TYPE_I4:
return OP_LOADI4_MEMBASE;
case MONO_TYPE_U4:
#ifdef TARGET_RISCV64
return OP_LOADI4_MEMBASE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed ?

Copy link
Contributor Author

@Xinlong-Wu Xinlong-Wu Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cuz the psABI of RISCV hase specify that

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.

The syntax of LW and LWU in riscv-spec-20191213.pdf are

LW: x[rd] = sext(M[x[rs1] + sext(offset)][31:0])
LWU: x[rd] = M[x[rs1] + sext(offset)][31:0]
SW: M[x[rs1] + sext(offset) = x[rs2][31: 0]
there is no SWU inst

The LW instruction loads a 32-bit value from memory and sign-extends this to 64 bits before storing it in register rd for RV64I. The LWU instruction, on the other hand, zero-extends the 32-bit value from memory for RV64I. LH and LHU are defined analogously for 16-bit values, as are LB and LBU for 8-bit values. The SD, SW, SH, and SB instructions store 64-bit, 32-bit, 16-bit, and 8-bit values from the low bits of register rs2 to memory respectively

which means the unsigned value on stack will be load by the same instruction as signed value. Following picture is generate by clang rv64 trunk https://godbolt.org/z/Tj3e3nYM6 as example.
6f791ef3f9cb2643c1136d271e3f137

while OP_LOADU4_MEMBASE inst will only be used when zero extend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except that, the (uint32_t)-1 will be load into reg as 0xFFFFFFFFFFFFFFFF in rv64 directly. if we use lwu to load -1 on stack, it will be 0x00000000FFFFFFFF. Thus, it will cause the -1 in reg doesn't equals to the -1 on stack.

Hope I have explain it clearly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this conversation be solved? Since I explain why I write like that.

@vargaz
Copy link
Contributor

vargaz commented Jul 11, 2023

The rest of the changes look ok.

@vargaz vargaz merged commit 08aba69 into dotnet:main Jul 24, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants