Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: zero-extend RISC-V swizzle splat index (#14942)

Christopher Onyiuke963f9d8fb96f
diff --git a/cranelift/codegen/src/isa/riscv64/lower.isle b/cranelift/codegen/src/isa/riscv64/lower.isle
index 598a8d6acd..9a75a860cd 100644
--- a/cranelift/codegen/src/isa/riscv64/lower.isle
+++ b/cranelift/codegen/src/isa/riscv64/lower.isle
@@ -3002,7 +3002,11 @@
(rv_vrgather_vv x y (unmasked) ty))
(rule 1 (lower (swizzle (ty_supported_vec ty) x (splat _ y)))
- (rv_vrgather_vx x y (unmasked) ty))
+ ;; The upper bits of an `i8`/`i16`/`i32` value are not guaranteed to be
+ ;; zero, and `vrgather.vx` uses the full scalar register as the index.
+ ;; Zero-extend the index first so out-of-range upper bits can't change the
+ ;; result.
+ (rv_vrgather_vx x (zext y) (unmasked) ty))
(rule 2 (lower (swizzle (ty_supported_vec ty) x y))
(if-let y_imm (replicated_uimm5 y))
diff --git a/cranelift/filetests/filetests/isa/riscv64/simd-swizzle.clif b/cranelift/filetests/filetests/isa/riscv64/simd-swizzle.clif
index 04a93b690b..bb3c6a8d51 100644
--- a/cranelift/filetests/filetests/isa/riscv64/simd-swizzle.clif
+++ b/cranelift/filetests/filetests/isa/riscv64/simd-swizzle.clif
@@ -56,9 +56,10 @@ block0(v0: i8x16, v1: i8):
; sd fp,0(sp)
; mv fp,sp
; block0:
-; vle8.v v9,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
-; vrgather.vx v8,v9,a1 #avl=16, #vtype=(e8, m1, ta, ma)
-; vse8.v v8,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
+; vle8.v v8,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
+; andi a1,a1,255
+; vrgather.vx v9,v8,a1 #avl=16, #vtype=(e8, m1, ta, ma)
+; vse8.v v9,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
; ld ra,8(sp)
; ld fp,0(sp)
; addi sp,sp,16
@@ -73,9 +74,10 @@ block0(v0: i8x16, v1: i8):
; block1: ; offset 0x10
; .byte 0x57, 0x70, 0x08, 0xcc
; addi t6, sp, 0x10
-; .byte 0x87, 0x84, 0x0f, 0x02
-; .byte 0x57, 0xc4, 0x95, 0x32
-; .byte 0x27, 0x04, 0x05, 0x02
+; .byte 0x07, 0x84, 0x0f, 0x02
+; andi a1, a1, 0xff
+; .byte 0xd7, 0xc4, 0x85, 0x32
+; .byte 0xa7, 0x04, 0x05, 0x02
; ld ra, 8(sp)
; ld s0, 0(sp)
; addi sp, sp, 0x10
diff --git a/cranelift/filetests/filetests/runtests/simd-swizzle.clif b/cranelift/filetests/filetests/runtests/simd-swizzle.clif
index 82dc6dafa0..6b08bd9d5f 100644
--- a/cranelift/filetests/filetests/runtests/simd-swizzle.clif
+++ b/cranelift/filetests/filetests/runtests/simd-swizzle.clif
@@ -30,6 +30,15 @@ block0(v0: i8x16, v1: i8):
; run: %swizzle_splat_i8x16([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16], 5) == [6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6]
; run: %swizzle_splat_i8x16([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16], 99) == [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
+function %swizzle_splat_junk_upper_bits(i8x16, i64) -> i8x16 {
+block0(v0: i8x16, v1: i64):
+ v2 = ireduce.i8 v1
+ v3 = splat.i8x16 v2
+ v4 = swizzle v0, v3
+ return v4
+}
+; run: %swizzle_splat_junk_upper_bits([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16], 0x100) == [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]
+
function %swizzle_splat_const_i8x16(i8x16) -> i8x16 {
block0(v0: i8x16):