Skip to content

Commit 2f56a32

Browse files
authored
Slightly simplify logic in ref.test translation (#10192)
When the ref is null or i31, just use an `iconst` result value directly, rather than reusing the result of the check for null or i31. Reusing the result doesn't actually yield better code (equivalently good or worse depending on if null/i31 are allowed or not) and is somewhat subtle -- I have to stop and re-think through its correctness each time I see it again -- so this change should be a welcome improvement. This does not change the logic of the emitted code, but does slightly change the emitted code itself.
1 parent bb2aa73 commit 2f56a32

17 files changed

+464
-514
lines changed

crates/cranelift/src/gc/enabled.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -922,17 +922,11 @@ pub fn translate_ref_test(
922922

923923
// Current block: check if the reference is null and branch appropriately.
924924
let is_null = func_env.translate_ref_is_null(builder.cursor(), val)?;
925-
let is_null_result = if ref_ty.nullable {
926-
is_null
927-
} else {
928-
let zero = builder.ins().iconst(ir::types::I32, 0);
929-
let one = builder.ins().iconst(ir::types::I32, 1);
930-
builder.ins().select(is_null, zero, one)
931-
};
925+
let result_when_is_null = builder.ins().iconst(ir::types::I32, ref_ty.nullable as i64);
932926
builder.ins().brif(
933927
is_null,
934928
continue_block,
935-
&[is_null_result],
929+
&[result_when_is_null],
936930
non_null_block,
937931
&[],
938932
);
@@ -949,17 +943,17 @@ pub fn translate_ref_test(
949943
let is_i31 = builder.ins().band(val, i31_mask);
950944
// If it is an `i31`, then create the result value based on whether we
951945
// want `i31`s to pass the test or not.
952-
let is_i31_result = if ref_ty.heap_type == WasmHeapType::Eq {
953-
is_i31
954-
} else {
955-
let zero = builder.ins().iconst(ir::types::I32, 0);
956-
let one = builder.ins().iconst(ir::types::I32, 1);
957-
builder.ins().select(is_i31, zero, one)
958-
};
946+
let result_when_is_i31 = builder.ins().iconst(
947+
ir::types::I32,
948+
matches!(
949+
ref_ty.heap_type,
950+
WasmHeapType::Any | WasmHeapType::Eq | WasmHeapType::I31
951+
) as i64,
952+
);
959953
builder.ins().brif(
960954
is_i31,
961955
continue_block,
962-
&[is_i31_result],
956+
&[result_when_is_i31],
963957
non_null_non_i31_block,
964958
&[],
965959
);

tests/disas/gc/drc/br-on-cast-fail.wat

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,57 @@
3131
;; stack_limit = gv2
3232
;;
3333
;; block0(v0: i64, v1: i64, v2: i32):
34-
;; v45 = stack_addr.i64 ss0
35-
;; store notrap v2, v45
36-
;; v47 = iconst.i32 0
37-
;; @002e v4 = icmp eq v2, v47 ; v47 = 0
34+
;; v41 = stack_addr.i64 ss0
35+
;; store notrap v2, v41
36+
;; v43 = iconst.i32 0
37+
;; @002e v4 = icmp eq v2, v43 ; v43 = 0
3838
;; @002e v5 = uextend.i32 v4
39-
;; @002e v7 = iconst.i32 1
40-
;; v55 = select v2, v7, v47 ; v7 = 1, v47 = 0
41-
;; @002e brif v5, block5(v55), block3
39+
;; @002e brif v5, block5(v43), block3 ; v43 = 0
4240
;;
4341
;; block3:
44-
;; v62 = iconst.i32 1
45-
;; v63 = band.i32 v2, v62 ; v62 = 1
46-
;; v64 = iconst.i32 0
47-
;; v65 = select v63, v64, v62 ; v64 = 0, v62 = 1
48-
;; @002e brif v63, block5(v65), block4
42+
;; @002e v7 = iconst.i32 1
43+
;; @002e v8 = band.i32 v2, v7 ; v7 = 1
44+
;; v47 = iconst.i32 0
45+
;; @002e brif v8, block5(v47), block4 ; v47 = 0
4946
;;
5047
;; block4:
51-
;; @002e v21 = uextend.i64 v2
52-
;; @002e v22 = iconst.i64 4
53-
;; @002e v23 = uadd_overflow_trap v21, v22, user1 ; v22 = 4
54-
;; @002e v24 = iconst.i64 8
55-
;; @002e v25 = uadd_overflow_trap v23, v24, user1 ; v24 = 8
56-
;; @002e v20 = load.i64 notrap aligned readonly v0+48
57-
;; @002e v26 = icmp ule v25, v20
58-
;; @002e trapz v26, user1
59-
;; @002e v18 = load.i64 notrap aligned readonly v0+40
60-
;; @002e v27 = iadd v18, v23
61-
;; @002e v28 = load.i32 notrap aligned readonly v27
62-
;; @002e v15 = load.i64 notrap aligned readonly v0+64
63-
;; @002e v16 = load.i32 notrap aligned readonly v15
64-
;; @002e v29 = icmp eq v28, v16
65-
;; @002e v30 = uextend.i32 v29
66-
;; @002e brif v30, block7(v30), block6
48+
;; @002e v17 = uextend.i64 v2
49+
;; @002e v18 = iconst.i64 4
50+
;; @002e v19 = uadd_overflow_trap v17, v18, user1 ; v18 = 4
51+
;; @002e v20 = iconst.i64 8
52+
;; @002e v21 = uadd_overflow_trap v19, v20, user1 ; v20 = 8
53+
;; @002e v16 = load.i64 notrap aligned readonly v0+48
54+
;; @002e v22 = icmp ule v21, v16
55+
;; @002e trapz v22, user1
56+
;; @002e v14 = load.i64 notrap aligned readonly v0+40
57+
;; @002e v23 = iadd v14, v19
58+
;; @002e v24 = load.i32 notrap aligned readonly v23
59+
;; @002e v11 = load.i64 notrap aligned readonly v0+64
60+
;; @002e v12 = load.i32 notrap aligned readonly v11
61+
;; @002e v25 = icmp eq v24, v12
62+
;; @002e v26 = uextend.i32 v25
63+
;; @002e brif v26, block7(v26), block6
6764
;;
6865
;; block6:
69-
;; @002e v32 = call fn0(v0, v28, v16), stack_map=[i32 @ ss0+0]
70-
;; @002e jump block7(v32)
66+
;; @002e v28 = call fn0(v0, v24, v12), stack_map=[i32 @ ss0+0]
67+
;; @002e jump block7(v28)
7168
;;
72-
;; block7(v33: i32):
73-
;; @002e jump block5(v33)
69+
;; block7(v29: i32):
70+
;; @002e jump block5(v29)
7471
;;
75-
;; block5(v34: i32):
76-
;; v41 = load.i32 notrap v45
77-
;; @002e brif v34, block8, block2
72+
;; block5(v30: i32):
73+
;; v37 = load.i32 notrap v41
74+
;; @002e brif v30, block8, block2
7875
;;
7976
;; block8:
80-
;; @0034 v36 = load.i64 notrap aligned readonly v0+72
81-
;; @0034 v37 = load.i64 notrap aligned readonly v0+88
82-
;; @0034 call_indirect sig1, v36(v37, v0)
77+
;; @0034 v32 = load.i64 notrap aligned readonly v0+72
78+
;; @0034 v33 = load.i64 notrap aligned readonly v0+88
79+
;; @0034 call_indirect sig1, v32(v33, v0)
8380
;; @0036 return
8481
;;
8582
;; block2:
86-
;; @0038 v39 = load.i64 notrap aligned readonly v0+96
87-
;; @0038 v40 = load.i64 notrap aligned readonly v0+112
88-
;; @0038 call_indirect sig2, v39(v40, v0)
83+
;; @0038 v35 = load.i64 notrap aligned readonly v0+96
84+
;; @0038 v36 = load.i64 notrap aligned readonly v0+112
85+
;; @0038 call_indirect sig2, v35(v36, v0)
8986
;; @003a return
9087
;; }

tests/disas/gc/drc/br-on-cast.wat

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,57 @@
3131
;; stack_limit = gv2
3232
;;
3333
;; block0(v0: i64, v1: i64, v2: i32):
34-
;; v45 = stack_addr.i64 ss0
35-
;; store notrap v2, v45
36-
;; v47 = iconst.i32 0
37-
;; @002f v4 = icmp eq v2, v47 ; v47 = 0
34+
;; v41 = stack_addr.i64 ss0
35+
;; store notrap v2, v41
36+
;; v43 = iconst.i32 0
37+
;; @002f v4 = icmp eq v2, v43 ; v43 = 0
3838
;; @002f v5 = uextend.i32 v4
39-
;; @002f v7 = iconst.i32 1
40-
;; v55 = select v2, v7, v47 ; v7 = 1, v47 = 0
41-
;; @002f brif v5, block5(v55), block3
39+
;; @002f brif v5, block5(v43), block3 ; v43 = 0
4240
;;
4341
;; block3:
44-
;; v62 = iconst.i32 1
45-
;; v63 = band.i32 v2, v62 ; v62 = 1
46-
;; v64 = iconst.i32 0
47-
;; v65 = select v63, v64, v62 ; v64 = 0, v62 = 1
48-
;; @002f brif v63, block5(v65), block4
42+
;; @002f v7 = iconst.i32 1
43+
;; @002f v8 = band.i32 v2, v7 ; v7 = 1
44+
;; v47 = iconst.i32 0
45+
;; @002f brif v8, block5(v47), block4 ; v47 = 0
4946
;;
5047
;; block4:
51-
;; @002f v21 = uextend.i64 v2
52-
;; @002f v22 = iconst.i64 4
53-
;; @002f v23 = uadd_overflow_trap v21, v22, user1 ; v22 = 4
54-
;; @002f v24 = iconst.i64 8
55-
;; @002f v25 = uadd_overflow_trap v23, v24, user1 ; v24 = 8
56-
;; @002f v20 = load.i64 notrap aligned readonly v0+48
57-
;; @002f v26 = icmp ule v25, v20
58-
;; @002f trapz v26, user1
59-
;; @002f v18 = load.i64 notrap aligned readonly v0+40
60-
;; @002f v27 = iadd v18, v23
61-
;; @002f v28 = load.i32 notrap aligned readonly v27
62-
;; @002f v15 = load.i64 notrap aligned readonly v0+64
63-
;; @002f v16 = load.i32 notrap aligned readonly v15
64-
;; @002f v29 = icmp eq v28, v16
65-
;; @002f v30 = uextend.i32 v29
66-
;; @002f brif v30, block7(v30), block6
48+
;; @002f v17 = uextend.i64 v2
49+
;; @002f v18 = iconst.i64 4
50+
;; @002f v19 = uadd_overflow_trap v17, v18, user1 ; v18 = 4
51+
;; @002f v20 = iconst.i64 8
52+
;; @002f v21 = uadd_overflow_trap v19, v20, user1 ; v20 = 8
53+
;; @002f v16 = load.i64 notrap aligned readonly v0+48
54+
;; @002f v22 = icmp ule v21, v16
55+
;; @002f trapz v22, user1
56+
;; @002f v14 = load.i64 notrap aligned readonly v0+40
57+
;; @002f v23 = iadd v14, v19
58+
;; @002f v24 = load.i32 notrap aligned readonly v23
59+
;; @002f v11 = load.i64 notrap aligned readonly v0+64
60+
;; @002f v12 = load.i32 notrap aligned readonly v11
61+
;; @002f v25 = icmp eq v24, v12
62+
;; @002f v26 = uextend.i32 v25
63+
;; @002f brif v26, block7(v26), block6
6764
;;
6865
;; block6:
69-
;; @002f v32 = call fn0(v0, v28, v16), stack_map=[i32 @ ss0+0]
70-
;; @002f jump block7(v32)
66+
;; @002f v28 = call fn0(v0, v24, v12), stack_map=[i32 @ ss0+0]
67+
;; @002f jump block7(v28)
7168
;;
72-
;; block7(v33: i32):
73-
;; @002f jump block5(v33)
69+
;; block7(v29: i32):
70+
;; @002f jump block5(v29)
7471
;;
75-
;; block5(v34: i32):
76-
;; v41 = load.i32 notrap v45
77-
;; @002f brif v34, block2, block8
72+
;; block5(v30: i32):
73+
;; v37 = load.i32 notrap v41
74+
;; @002f brif v30, block2, block8
7875
;;
7976
;; block8:
80-
;; @0035 v36 = load.i64 notrap aligned readonly v0+72
81-
;; @0035 v37 = load.i64 notrap aligned readonly v0+88
82-
;; @0035 call_indirect sig1, v36(v37, v0)
77+
;; @0035 v32 = load.i64 notrap aligned readonly v0+72
78+
;; @0035 v33 = load.i64 notrap aligned readonly v0+88
79+
;; @0035 call_indirect sig1, v32(v33, v0)
8380
;; @0037 return
8481
;;
8582
;; block2:
86-
;; @0039 v39 = load.i64 notrap aligned readonly v0+96
87-
;; @0039 v40 = load.i64 notrap aligned readonly v0+112
88-
;; @0039 call_indirect sig2, v39(v40, v0)
83+
;; @0039 v35 = load.i64 notrap aligned readonly v0+96
84+
;; @0039 v36 = load.i64 notrap aligned readonly v0+112
85+
;; @0039 call_indirect sig2, v35(v36, v0)
8986
;; @003b return
9087
;; }

tests/disas/gc/drc/ref-cast.wat

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,49 @@
1919
;; stack_limit = gv2
2020
;;
2121
;; block0(v0: i64, v1: i64, v2: i32):
22-
;; v39 = stack_addr.i64 ss0
23-
;; store notrap v2, v39
24-
;; v41 = iconst.i32 0
25-
;; @001e v4 = icmp eq v2, v41 ; v41 = 0
22+
;; v35 = stack_addr.i64 ss0
23+
;; store notrap v2, v35
24+
;; v37 = iconst.i32 0
25+
;; @001e v4 = icmp eq v2, v37 ; v37 = 0
2626
;; @001e v5 = uextend.i32 v4
27-
;; @001e v7 = iconst.i32 1
28-
;; v49 = select v2, v7, v41 ; v7 = 1, v41 = 0
29-
;; @001e brif v5, block4(v49), block2
27+
;; @001e brif v5, block4(v37), block2 ; v37 = 0
3028
;;
3129
;; block2:
32-
;; v56 = iconst.i32 1
33-
;; v57 = band.i32 v2, v56 ; v56 = 1
34-
;; v58 = iconst.i32 0
35-
;; v59 = select v57, v58, v56 ; v58 = 0, v56 = 1
36-
;; @001e brif v57, block4(v59), block3
30+
;; @001e v7 = iconst.i32 1
31+
;; @001e v8 = band.i32 v2, v7 ; v7 = 1
32+
;; v41 = iconst.i32 0
33+
;; @001e brif v8, block4(v41), block3 ; v41 = 0
3734
;;
3835
;; block3:
39-
;; @001e v21 = uextend.i64 v2
40-
;; @001e v22 = iconst.i64 4
41-
;; @001e v23 = uadd_overflow_trap v21, v22, user1 ; v22 = 4
42-
;; @001e v24 = iconst.i64 8
43-
;; @001e v25 = uadd_overflow_trap v23, v24, user1 ; v24 = 8
44-
;; @001e v20 = load.i64 notrap aligned readonly v0+48
45-
;; @001e v26 = icmp ule v25, v20
46-
;; @001e trapz v26, user1
47-
;; @001e v18 = load.i64 notrap aligned readonly v0+40
48-
;; @001e v27 = iadd v18, v23
49-
;; @001e v28 = load.i32 notrap aligned readonly v27
50-
;; @001e v15 = load.i64 notrap aligned readonly v0+64
51-
;; @001e v16 = load.i32 notrap aligned readonly v15
52-
;; @001e v29 = icmp eq v28, v16
53-
;; @001e v30 = uextend.i32 v29
54-
;; @001e brif v30, block6(v30), block5
36+
;; @001e v17 = uextend.i64 v2
37+
;; @001e v18 = iconst.i64 4
38+
;; @001e v19 = uadd_overflow_trap v17, v18, user1 ; v18 = 4
39+
;; @001e v20 = iconst.i64 8
40+
;; @001e v21 = uadd_overflow_trap v19, v20, user1 ; v20 = 8
41+
;; @001e v16 = load.i64 notrap aligned readonly v0+48
42+
;; @001e v22 = icmp ule v21, v16
43+
;; @001e trapz v22, user1
44+
;; @001e v14 = load.i64 notrap aligned readonly v0+40
45+
;; @001e v23 = iadd v14, v19
46+
;; @001e v24 = load.i32 notrap aligned readonly v23
47+
;; @001e v11 = load.i64 notrap aligned readonly v0+64
48+
;; @001e v12 = load.i32 notrap aligned readonly v11
49+
;; @001e v25 = icmp eq v24, v12
50+
;; @001e v26 = uextend.i32 v25
51+
;; @001e brif v26, block6(v26), block5
5552
;;
5653
;; block5:
57-
;; @001e v32 = call fn0(v0, v28, v16), stack_map=[i32 @ ss0+0]
58-
;; @001e jump block6(v32)
54+
;; @001e v28 = call fn0(v0, v24, v12), stack_map=[i32 @ ss0+0]
55+
;; @001e jump block6(v28)
5956
;;
60-
;; block6(v33: i32):
61-
;; @001e jump block4(v33)
57+
;; block6(v29: i32):
58+
;; @001e jump block4(v29)
6259
;;
63-
;; block4(v34: i32):
64-
;; @001e trapz v34, user19
65-
;; v35 = load.i32 notrap v39
60+
;; block4(v30: i32):
61+
;; @001e trapz v30, user19
62+
;; v31 = load.i32 notrap v35
6663
;; @0021 jump block1
6764
;;
6865
;; block1:
69-
;; @0021 return v35
66+
;; @0021 return v31
7067
;; }

0 commit comments

Comments
 (0)