Skip to content

Commit 0bc447b

Browse files
authored
Cranelift: riscv64: fix zero-extension in trapif + icmp folding. (#12952)
* Add bad test. * Fix lowering to properly zero-extend compared values.
1 parent e430575 commit 0bc447b

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

cranelift/codegen/src/isa/riscv64/lower.isle

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,10 @@
21262126

21272127
; fold icmp + trapz
21282128
(rule 2 (lower (trapz (icmp _ cc x @ (value_type (fits_in_64 _)) y) code))
2129-
(gen_trapif (intcc_complement cc) x y code))
2129+
(gen_trapif (intcc_complement cc)
2130+
(put_value_in_reg_for_icmp cc x)
2131+
(put_value_in_reg_for_icmp cc y)
2132+
code))
21302133

21312134
;;;;; Rules for `trapnz`;;;;;;;;;
21322135
(rule
@@ -2139,7 +2142,10 @@
21392142

21402143
; fold icmp + trapnz
21412144
(rule 2 (lower (trapnz (icmp _ cc x @ (value_type (fits_in_64 _)) y) code))
2142-
(gen_trapif cc x y code))
2145+
(gen_trapif cc
2146+
(put_value_in_reg_for_icmp cc x)
2147+
(put_value_in_reg_for_icmp cc y)
2148+
code))
21432149

21442150
;;;;; Rules for `uload8`;;;;;;;;;
21452151
(rule (lower (uload8 _ (little_or_native_endian flags) addr offset))
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
test compile precise-output
2+
target riscv64
3+
4+
function %f(i64, i64) {
5+
block0(v0: i64, v1: i64):
6+
v2 = load.i32 notrap aligned readonly v0
7+
v3 = ireduce.i32 v1
8+
v4 = icmp ult v2, v3
9+
trapz v4, user15
10+
return
11+
}
12+
13+
; VCode:
14+
; block0:
15+
; lw a0,0(a0)
16+
; slli a0,a0,32
17+
; srli a0,a0,32
18+
; slli a1,a1,32
19+
; srli a1,a1,32
20+
; trap_if user15##(a0 uge a1)
21+
; ret
22+
;
23+
; Disassembled:
24+
; block0: ; offset 0x0
25+
; lw a0, 0(a0)
26+
; slli a0, a0, 0x20
27+
; srli a0, a0, 0x20
28+
; slli a1, a1, 0x20
29+
; srli a1, a1, 0x20
30+
; bltu a0, a1, 8
31+
; .byte 0x00, 0x00, 0x00, 0x00 ; trap: user15
32+
; ret
33+

0 commit comments

Comments
 (0)