Skip to content

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

riscv64: Fix `negated_replicated_imm5` rule (#13339)

Alex Crichtoneef7379b13c8
diff --git a/cranelift/codegen/src/isa/riscv64/inst.isle b/cranelift/codegen/src/isa/riscv64/inst.isle
index 94e408d655..83d624224e 100644
--- a/cranelift/codegen/src/isa/riscv64/inst.isle
+++ b/cranelift/codegen/src/isa/riscv64/inst.isle
@@ -2065,11 +2065,20 @@
(rule (negated_replicated_imm5 (splat _ n))
(if-let imm5 (imm5_from_negated_value n))
imm5)
-(rule (negated_replicated_imm5 (vconst _ (u128_from_constant n128)))
+(rule (negated_replicated_imm5 (vconst (multi_lane 64 _) (u128_from_constant n128)))
(if-let (u128_replicated_u64 n64) n128)
- (if-let (u64_replicated_u32 n32) n64)
- (if-let (u32_replicated_u16 n16) n32)
- (if-let (u16_replicated_u8 n8) n16)
+ (if-let n (i64_to_imm5 (i64_wrapping_neg (u64_cast_signed n64))))
+ n)
+(rule (negated_replicated_imm5 (vconst (multi_lane 32 _) (u128_from_constant n128)))
+ (if-let (u128_replicated_u32 n32) n128)
+ (if-let n (i32_to_imm5 (i32_wrapping_neg (u32_cast_signed n32))))
+ n)
+(rule (negated_replicated_imm5 (vconst (multi_lane 16 _) (u128_from_constant n128)))
+ (if-let (u128_replicated_u16 n16) n128)
+ (if-let n (i16_to_imm5 (i16_wrapping_neg (u16_cast_signed n16))))
+ n)
+(rule (negated_replicated_imm5 (vconst (multi_lane 8 _) (u128_from_constant n128)))
+ (if-let (u128_replicated_u8 n8) n128)
(if-let n (i8_to_imm5 (i8_wrapping_neg (u8_cast_signed n8))))
n)
diff --git a/cranelift/filetests/filetests/isa/riscv64/simd-iadd-opt.clif b/cranelift/filetests/filetests/isa/riscv64/simd-iadd-opt.clif
index 28c29d74a2..db54b16253 100644
--- a/cranelift/filetests/filetests/isa/riscv64/simd-iadd-opt.clif
+++ b/cranelift/filetests/filetests/isa/riscv64/simd-iadd-opt.clif
@@ -328,3 +328,327 @@ block0(v0: i64x2):
; .byte 0x01, 0x00, 0x00, 0x00
; .byte 0x01, 0x00, 0x00, 0x00
+function %isub_const_i8x16(i8x16) -> i8x16 {
+block0(v0: i8x16):
+ v2 = vconst.i8x16 0x01010101_01010101_01010101_01010101
+ v3 = isub v0, v2
+
+ v4 = vconst.i8x16 0x00010001_00010001_00010001_00010001
+ v5 = isub v3, v4
+
+ v6 = vconst.i8x16 0x00000001_00000001_00000001_00000001
+ v7 = isub v5, v6
+
+ v8 = vconst.i8x16 0x00000000_00000001_00000000_00000001
+ v9 = isub v7, v8
+
+ return v9
+}
+
+; VCode:
+; addi sp,sp,-16
+; sd ra,8(sp)
+; sd fp,0(sp)
+; mv fp,sp
+; block0:
+; vle8.v v8,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
+; vadd.vi v10,v8,-1 #avl=16, #vtype=(e8, m1, ta, ma)
+; vle8.v v11,[const(2)] #avl=16, #vtype=(e8, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=16, #vtype=(e8, m1, ta, ma)
+; vle8.v v11,[const(1)] #avl=16, #vtype=(e8, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=16, #vtype=(e8, m1, ta, ma)
+; vle8.v v11,[const(0)] #avl=16, #vtype=(e8, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=16, #vtype=(e8, m1, ta, ma)
+; vse8.v v10,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
+; ld ra,8(sp)
+; ld fp,0(sp)
+; addi sp,sp,16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; addi sp, sp, -0x10
+; sd ra, 8(sp)
+; sd s0, 0(sp)
+; mv s0, sp
+; block1: ; offset 0x10
+; .byte 0x57, 0x70, 0x08, 0xcc
+; addi t6, sp, 0x10
+; .byte 0x07, 0x84, 0x0f, 0x02
+; .byte 0x57, 0xb5, 0x8f, 0x02
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0x85, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0x85, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0x85, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; .byte 0x27, 0x05, 0x05, 0x02
+; ld ra, 8(sp)
+; ld s0, 0(sp)
+; addi sp, sp, 0x10
+; ret
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+
+function %isub_const_i16x8(i16x8) -> i16x8 {
+block0(v0: i16x8):
+ v2 = vconst.i16x8 0x01010101_01010101_01010101_01010101
+ v3 = isub v0, v2
+
+ v4 = vconst.i16x8 0x00010001_00010001_00010001_00010001
+ v5 = isub v3, v4
+
+ v6 = vconst.i16x8 0x00000001_00000001_00000001_00000001
+ v7 = isub v5, v6
+
+ v8 = vconst.i16x8 0x00000000_00000001_00000000_00000001
+ v9 = isub v7, v8
+
+ return v9
+}
+
+; VCode:
+; addi sp,sp,-16
+; sd ra,8(sp)
+; sd fp,0(sp)
+; mv fp,sp
+; block0:
+; vle8.v v8,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
+; vle16.v v10,[const(2)] #avl=8, #vtype=(e16, m1, ta, ma)
+; vsub.vv v10,v8,v10 #avl=8, #vtype=(e16, m1, ta, ma)
+; vadd.vi v10,v10,-1 #avl=8, #vtype=(e16, m1, ta, ma)
+; vle16.v v11,[const(1)] #avl=8, #vtype=(e16, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=8, #vtype=(e16, m1, ta, ma)
+; vle16.v v11,[const(0)] #avl=8, #vtype=(e16, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=8, #vtype=(e16, m1, ta, ma)
+; vse8.v v10,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
+; ld ra,8(sp)
+; ld fp,0(sp)
+; addi sp,sp,16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; addi sp, sp, -0x10
+; sd ra, 8(sp)
+; sd s0, 0(sp)
+; mv s0, sp
+; block1: ; offset 0x10
+; .byte 0x57, 0x70, 0x08, 0xcc
+; addi t6, sp, 0x10
+; .byte 0x07, 0x84, 0x0f, 0x02
+; .byte 0x57, 0x70, 0x84, 0xcc
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x07, 0xd5, 0x0f, 0x02
+; .byte 0x57, 0x05, 0x85, 0x0a
+; .byte 0x57, 0xb5, 0xaf, 0x02
+; auipc t6, 0
+; addi t6, t6, 0x4c
+; .byte 0x87, 0xd5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x4c
+; .byte 0x87, 0xd5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; .byte 0x57, 0x70, 0x08, 0xcc
+; .byte 0x27, 0x05, 0x05, 0x02
+; ld ra, 8(sp)
+; ld s0, 0(sp)
+; addi sp, sp, 0x10
+; ret
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+
+function %isub_const_i32x4(i32x4) -> i32x4 {
+block0(v0: i32x4):
+ v2 = vconst.i32x4 0x01010101_01010101_01010101_01010101
+ v3 = isub v0, v2
+
+ v4 = vconst.i32x4 0x00010001_00010001_00010001_00010001
+ v5 = isub v3, v4
+
+ v6 = vconst.i32x4 0x00000001_00000001_00000001_00000001
+ v7 = isub v5, v6
+
+ v8 = vconst.i32x4 0x00000000_00000001_00000000_00000001
+ v9 = isub v7, v8
+
+ return v9
+}
+
+; VCode:
+; addi sp,sp,-16
+; sd ra,8(sp)
+; sd fp,0(sp)
+; mv fp,sp
+; block0:
+; vle8.v v8,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
+; vle32.v v10,[const(2)] #avl=4, #vtype=(e32, m1, ta, ma)
+; vsub.vv v10,v8,v10 #avl=4, #vtype=(e32, m1, ta, ma)
+; vle32.v v11,[const(1)] #avl=4, #vtype=(e32, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=4, #vtype=(e32, m1, ta, ma)
+; vadd.vi v10,v10,-1 #avl=4, #vtype=(e32, m1, ta, ma)
+; vle32.v v11,[const(0)] #avl=4, #vtype=(e32, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=4, #vtype=(e32, m1, ta, ma)
+; vse8.v v10,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
+; ld ra,8(sp)
+; ld fp,0(sp)
+; addi sp,sp,16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; addi sp, sp, -0x10
+; sd ra, 8(sp)
+; sd s0, 0(sp)
+; mv s0, sp
+; block1: ; offset 0x10
+; .byte 0x57, 0x70, 0x08, 0xcc
+; addi t6, sp, 0x10
+; .byte 0x07, 0x84, 0x0f, 0x02
+; .byte 0x57, 0x70, 0x02, 0xcd
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x07, 0xe5, 0x0f, 0x02
+; .byte 0x57, 0x05, 0x85, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0xe5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; .byte 0x57, 0xb5, 0xaf, 0x02
+; auipc t6, 0
+; addi t6, t6, 0x4c
+; .byte 0x87, 0xe5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; .byte 0x57, 0x70, 0x08, 0xcc
+; .byte 0x27, 0x05, 0x05, 0x02
+; ld ra, 8(sp)
+; ld s0, 0(sp)
+; addi sp, sp, 0x10
+; ret
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x00, 0x00, 0x00, 0x00
+
+function %isub_const_i64x2(i64x2) -> i64x2 {
+block0(v0: i64x2):
+ v2 = vconst.i64x2 0x01010101_01010101_01010101_01010101
+ v3 = isub v0, v2
+
+ v4 = vconst.i64x2 0x00010001_00010001_00010001_00010001
+ v5 = isub v3, v4
+
+ v6 = vconst.i64x2 0x00000001_00000001_00000001_00000001
+ v7 = isub v5, v6
+
+ v8 = vconst.i64x2 0x00000000_00000001_00000000_00000001
+ v9 = isub v7, v8
+
+ return v9
+}
+
+; VCode:
+; addi sp,sp,-16
+; sd ra,8(sp)
+; sd fp,0(sp)
+; mv fp,sp
+; block0:
+; vle8.v v8,-16(incoming_arg) #avl=16, #vtype=(e8, m1, ta, ma)
+; vle64.v v10,[const(2)] #avl=2, #vtype=(e64, m1, ta, ma)
+; vsub.vv v10,v8,v10 #avl=2, #vtype=(e64, m1, ta, ma)
+; vle64.v v11,[const(1)] #avl=2, #vtype=(e64, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=2, #vtype=(e64, m1, ta, ma)
+; vle64.v v11,[const(0)] #avl=2, #vtype=(e64, m1, ta, ma)
+; vsub.vv v10,v10,v11 #avl=2, #vtype=(e64, m1, ta, ma)
+; vadd.vi v10,v10,-1 #avl=2, #vtype=(e64, m1, ta, ma)
+; vse8.v v10,0(a0) #avl=16, #vtype=(e8, m1, ta, ma)
+; ld ra,8(sp)
+; ld fp,0(sp)
+; addi sp,sp,16
+; ret
+;
+; Disassembled:
+; block0: ; offset 0x0
+; addi sp, sp, -0x10
+; sd ra, 8(sp)
+; sd s0, 0(sp)
+; mv s0, sp
+; block1: ; offset 0x10
+; .byte 0x57, 0x70, 0x08, 0xcc
+; addi t6, sp, 0x10
+; .byte 0x07, 0x84, 0x0f, 0x02
+; .byte 0x57, 0x70, 0x81, 0xcd
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x07, 0xf5, 0x0f, 0x02
+; .byte 0x57, 0x05, 0x85, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0xf5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; auipc t6, 0
+; addi t6, t6, 0x50
+; .byte 0x87, 0xf5, 0x0f, 0x02
+; .byte 0x57, 0x85, 0xa5, 0x0a
+; .byte 0x57, 0xb5, 0xaf, 0x02
+; .byte 0x57, 0x70, 0x08, 0xcc
+; .byte 0x27, 0x05, 0x05, 0x02
+; ld ra, 8(sp)
+; ld s0, 0(sp)
+; addi sp, sp, 0x10
+; ret
+; .byte 0x00, 0x00, 0x00, 0x00
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x01, 0x01, 0x01
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x01, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+; .byte 0x01, 0x00, 0x00, 0x00
+
diff --git a/tests/misc_testsuite/simd/riscv64-replicated-imm5-works.wast b/tests/misc_testsuite/simd/riscv64-replicated-imm5-works.wast
index bed4ea723e..9262a41b9c 100644
--- a/tests/misc_testsuite/simd/riscv64-replicated-imm5-works.wast
+++ b/tests/misc_testsuite/simd/riscv64-replicated-imm5-works.wast
@@ -6,7 +6,15 @@
(i32x4.extract_lane 0
(i32x4.add
(local.get 0)
- (i32x4.splat (i32.const 0x01010101))))))
+ (i32x4.splat (i32.const 0x01010101)))))
+ (func (export "sub_and_extract") (param v128) (result i32)
+ (i32x4.extract_lane 0
+ (i32x4.sub
+ (local.get 0)
+ (i32x4.splat (i32.const 0x01010101)))))
+)
(assert_return (invoke "add_and_extract" (v128.const i32x4 0 0 0 0))
(i32.const 0x01010101))
+(assert_return (invoke "sub_and_extract" (v128.const i32x4 0 0 0 0))
+ (i32.const 0xfefefeff))