Skip to content

Commit fbeac86

Browse files
committed
RISC-V: Use symbolic instructions on inline assembly
While many intrinsics use `.insn` to generate raw machine code from numbers, all ratified instructions can be symbolic using `.option` directives. By saving the assembler environment with `.option push` then modifying the architecture with `.option arch`, we can temporarily enable certain extensions (as we use `.option pop` immediately after the target instruction, surrounding environment is completely intact in this commit; *almost* completely intact in general). Note: `hinval.vvma` and `hinval.gvma` instructions are a part of the Svinval extension (the H extension is not directly required on LLVM) but denoted as `+h,+svinval` to show semantic context (they are useful only when the H extension is present and according to the documentation, they are provided only if the hypervisor extension is enabled).
1 parent 91f0c19 commit fbeac86

File tree

2 files changed

+144
-43
lines changed

2 files changed

+144
-43
lines changed

crates/core_arch/src/riscv64/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ pub use zk::*;
2121
pub unsafe fn hlv_wu(src: *const u32) -> u32 {
2222
let value: u32;
2323
asm!(
24-
".insn i 0x73, 0x4, {}, {}, 0x681",
24+
".option push",
25+
".option arch, +h",
26+
"hlv.wu {}, 0({})",
27+
".option pop",
2528
lateout(reg) value,
2629
in(reg) src,
2730
options(readonly, nostack, preserves_flags)
@@ -44,7 +47,10 @@ pub unsafe fn hlv_wu(src: *const u32) -> u32 {
4447
pub unsafe fn hlv_d(src: *const i64) -> i64 {
4548
let value: i64;
4649
asm!(
47-
".insn i 0x73, 0x4, {}, {}, 0x6C0",
50+
".option push",
51+
".option arch, +h",
52+
"hlv.d {}, 0({})",
53+
".option pop",
4854
lateout(reg) value,
4955
in(reg) src,
5056
options(readonly, nostack, preserves_flags)
@@ -64,7 +70,10 @@ pub unsafe fn hlv_d(src: *const i64) -> i64 {
6470
#[unstable(feature = "riscv_ext_intrinsics", issue = "114544")]
6571
pub unsafe fn hsv_d(dst: *mut i64, src: i64) {
6672
asm!(
67-
".insn r 0x73, 0x4, 0x37, x0, {}, {}",
73+
".option push",
74+
".option arch, +h",
75+
"hsv.d {}, 0({})",
76+
".option pop",
6877
in(reg) dst,
6978
in(reg) src,
7079
options(nostack, preserves_flags)

0 commit comments

Comments
 (0)