Skip to content

Commit 008fe51

Browse files
authored
Cranelift: revert rules added in #12926. (#13081)
* Cranelift: revert rules added in #12926. These rules appear to be correct, but for an as-yet-undiagnosed reason, cause rewrite blowups. The proper fix is probably to use `subsume` but I'd like for us to bottom out why the blowup is happening first (cc @myunbin). In the meantime, this removes the rules so we get back to a workable state on `main`. Tests are retained and re-blessed with the now-less-optimized output. Fixes #13068. * update disas test
1 parent 9d94576 commit 008fe51

5 files changed

Lines changed: 80 additions & 154 deletions

File tree

cranelift/codegen/src/opts/arithmetic.isle

Lines changed: 7 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -321,77 +321,6 @@
321321
(rule (simplify (iadd ty (bnot ty x) x)) (iconst_s ty -1))
322322
(rule (simplify (iadd ty x (bnot ty x))) (iconst_s ty -1))
323323

324-
;; ((x + y) - (x + z)) --> (y - z)
325-
(rule (simplify (isub ty (iadd ty x y) (iadd ty x z))) (isub ty y z))
326-
(rule (simplify (isub ty (iadd ty x y) (iadd ty z x))) (isub ty y z))
327-
(rule (simplify (isub ty (iadd ty y x) (iadd ty x z))) (isub ty y z))
328-
(rule (simplify (isub ty (iadd ty y x) (iadd ty z x))) (isub ty y z))
329-
330-
;; ((x - z) - (y - z)) --> (x - y)
331-
(rule (simplify (isub ty (isub ty x z) (isub ty y z))) (isub ty x y))
332-
333-
;; ((x - y) - (x - z)) --> (z - y)
334-
(rule (simplify (isub ty (isub ty x y) (isub ty x z))) (isub ty z y))
335-
336-
;; umin(x, y) + umax(x, y) --> x + y
337-
(rule (simplify (iadd ty (umin ty x y) (umax ty x y))) (iadd ty x y))
338-
(rule (simplify (iadd ty (umax ty x y) (umin ty x y))) (iadd ty x y))
339-
(rule (simplify (iadd ty (umin ty x y) (umax ty y x))) (iadd ty x y))
340-
(rule (simplify (iadd ty (umax ty y x) (umin ty x y))) (iadd ty x y))
341-
(rule (simplify (iadd ty (umin ty y x) (umax ty x y))) (iadd ty x y))
342-
(rule (simplify (iadd ty (umax ty x y) (umin ty y x))) (iadd ty x y))
343-
(rule (simplify (iadd ty (umin ty y x) (umax ty y x))) (iadd ty x y))
344-
(rule (simplify (iadd ty (umax ty y x) (umin ty y x))) (iadd ty x y))
345-
346-
;; smin(x, y) + smax(x, y) --> x + y
347-
(rule (simplify (iadd ty (smin ty x y) (smax ty x y))) (iadd ty x y))
348-
(rule (simplify (iadd ty (smax ty x y) (smin ty x y))) (iadd ty x y))
349-
(rule (simplify (iadd ty (smin ty x y) (smax ty y x))) (iadd ty x y))
350-
(rule (simplify (iadd ty (smax ty y x) (smin ty x y))) (iadd ty x y))
351-
(rule (simplify (iadd ty (smin ty y x) (smax ty x y))) (iadd ty x y))
352-
(rule (simplify (iadd ty (smax ty x y) (smin ty y x))) (iadd ty x y))
353-
(rule (simplify (iadd ty (smin ty y x) (smax ty y x))) (iadd ty x y))
354-
(rule (simplify (iadd ty (smax ty y x) (smin ty y x))) (iadd ty x y))
355-
356-
;; ((x + z) - (y + z)) --> (x - y)
357-
(rule (simplify (isub ty (iadd ty x z) (iadd ty y z))) (isub ty x y))
358-
(rule (simplify (isub ty (iadd ty x z) (iadd ty z y))) (isub ty x y))
359-
(rule (simplify (isub ty (iadd ty z x) (iadd ty y z))) (isub ty x y))
360-
(rule (simplify (isub ty (iadd ty z x) (iadd ty z y))) (isub ty x y))
361-
362-
;; ((x - y) + (y + z)) --> (x + z)
363-
(rule (simplify (iadd ty (isub ty x y) (iadd ty y z))) (iadd ty x z))
364-
(rule (simplify (iadd ty (iadd ty y z) (isub ty x y))) (iadd ty x z))
365-
(rule (simplify (iadd ty (isub ty x y) (iadd ty z y))) (iadd ty x z))
366-
(rule (simplify (iadd ty (iadd ty z y) (isub ty x y))) (iadd ty x z))
367-
368-
;; (x - (x + y)) --> -y
369-
(rule (simplify (isub ty x (iadd ty x y))) (ineg ty y))
370-
(rule (simplify (isub ty x (iadd ty y x))) (ineg ty y))
371-
372-
;; (x + (y + (z - x))) --> (y + z)
373-
(rule (simplify (iadd ty x (iadd ty y (isub ty z x)))) (iadd ty y z))
374-
(rule (simplify (iadd ty (iadd ty y (isub ty z x)) x)) (iadd ty y z))
375-
(rule (simplify (iadd ty x (iadd ty (isub ty z x) y))) (iadd ty y z))
376-
(rule (simplify (iadd ty (iadd ty (isub ty z x) y) x)) (iadd ty y z))
377-
378-
;; Helper to create a "true" value for a comparison. For scalar integers this is
379-
;; a value of 1 but for vectors this is -1 since each lane is filled with all
380-
;; 1s.
381-
(decl cmp_true (Type) Value)
382-
(rule 0 (cmp_true (ty_int ty)) (iconst_u ty 1))
383-
(rule 1 (cmp_true (ty_vec128 ty)) (iconst_s ty -1))
384-
385-
;; (x + y) == (y + x) --> true
386-
(rule (simplify (eq ty (iadd cty x y) (iadd cty y x))) (cmp_true ty))
387-
(rule (simplify (eq ty (iadd cty y x) (iadd cty x y))) (cmp_true ty))
388-
(rule (simplify (eq ty (iadd cty x y) (iadd cty x y))) (cmp_true ty))
389-
(rule (simplify (eq ty (iadd cty y x) (iadd cty y x))) (cmp_true ty))
390-
391-
;; (x - y) != x --> y != 0
392-
(rule (simplify (ne cty (isub ty x y) x)) (ne cty y (iconst_u ty 0)))
393-
(rule (simplify (ne cty x (isub ty x y))) (ne cty y (iconst_u ty 0)))
394-
395324
;; (x - y) == x --> y == 0
396325
(rule (simplify (eq cty (isub ty x y) x)) (eq cty y (iconst_u ty 0)))
397326
(rule (simplify (eq cty x (isub ty x y))) (eq cty y (iconst_u ty 0)))
@@ -409,6 +338,13 @@
409338
(rule (simplify (ineg ty (imul ty (ineg ty y) x))) (imul ty x y))
410339
(rule (simplify (ineg ty (imul ty x (ineg ty y)))) (imul ty x y))
411340

341+
;; Helper to create a "true" value for a comparison. For scalar integers this is
342+
;; a value of 1 but for vectors this is -1 since each lane is filled with all
343+
;; 1s.
344+
(decl cmp_true (Type) Value)
345+
(rule 0 (cmp_true (ty_int ty)) (iconst_u ty 1))
346+
(rule 1 (cmp_true (ty_vec128 ty)) (iconst_s ty -1))
347+
412348
;; max(x, y) >= x
413349
(rule (simplify (sge ty (smax ty x y) x)) (cmp_true ty))
414350
(rule (simplify (sge ty (smax ty y x) x)) (cmp_true ty))

cranelift/codegen/src/opts/bitops.isle

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -545,42 +545,6 @@
545545
(rule (simplify (bor ty y (band ty x (bnot ty y)))) (bor ty y x))
546546
(rule (simplify (bor ty (band ty x (bnot ty y)) y)) (bor ty y x))
547547

548-
;; ((x & ~y) - (x & y)) --> ((x ^ y) - y)
549-
(rule (simplify (isub ty (band ty x (bnot ty y)) (band ty x y))) (isub ty (bxor ty x y) y))
550-
(rule (simplify (isub ty (band ty x (bnot ty y)) (band ty y x))) (isub ty (bxor ty x y) y))
551-
(rule (simplify (isub ty (band ty (bnot ty y) x) (band ty x y))) (isub ty (bxor ty x y) y))
552-
(rule (simplify (isub ty (band ty (bnot ty y) x) (band ty y x))) (isub ty (bxor ty x y) y))
553-
(rule (simplify (isub ty (band ty x y) (band ty x (bnot ty y)))) (isub ty y (bxor ty x y)))
554-
(rule (simplify (isub ty (band ty x y) (band ty (bnot ty y) x))) (isub ty y (bxor ty x y)))
555-
(rule (simplify (isub ty (band ty y x) (band ty x (bnot ty y)))) (isub ty y (bxor ty x y)))
556-
(rule (simplify (isub ty (band ty y x) (band ty (bnot ty y) x))) (isub ty y (bxor ty x y)))
557-
558-
;; (x & ~y) | (~x & y) --> (x ^ y)
559-
(rule (simplify (bor ty (band ty x (bnot ty y)) (band ty (bnot ty x) y))) (bxor ty x y))
560-
(rule (simplify (bor ty (band ty (bnot ty x) y) (band ty x (bnot ty y)))) (bxor ty x y))
561-
(rule (simplify (bor ty (band ty x (bnot ty y)) (band ty y (bnot ty x)))) (bxor ty x y))
562-
(rule (simplify (bor ty (band ty y (bnot ty x)) (band ty x (bnot ty y)))) (bxor ty x y))
563-
(rule (simplify (bor ty (band ty (bnot ty y) x) (band ty (bnot ty x) y))) (bxor ty x y))
564-
(rule (simplify (bor ty (band ty (bnot ty x) y) (band ty (bnot ty y) x))) (bxor ty x y))
565-
(rule (simplify (bor ty (band ty (bnot ty y) x) (band ty y (bnot ty x)))) (bxor ty x y))
566-
(rule (simplify (bor ty (band ty y (bnot ty x)) (band ty (bnot ty y) x))) (bxor ty x y))
567-
568-
;; (x & ~y) | (x ^ y) --> (x ^ y)
569-
(rule (simplify (bor ty (band ty x (bnot ty y)) (bxor ty x y))) (bxor ty x y))
570-
(rule (simplify (bor ty (bxor ty x y) (band ty x (bnot ty y)))) (bxor ty x y))
571-
(rule (simplify (bor ty (band ty x (bnot ty y)) (bxor ty y x))) (bxor ty x y))
572-
(rule (simplify (bor ty (bxor ty y x) (band ty x (bnot ty y)))) (bxor ty x y))
573-
(rule (simplify (bor ty (band ty (bnot ty y) x) (bxor ty x y))) (bxor ty x y))
574-
(rule (simplify (bor ty (bxor ty x y) (band ty (bnot ty y) x))) (bxor ty x y))
575-
(rule (simplify (bor ty (band ty (bnot ty y) x) (bxor ty y x))) (bxor ty x y))
576-
(rule (simplify (bor ty (bxor ty y x) (band ty (bnot ty y) x))) (bxor ty x y))
577-
578-
;; (x & ~y) ^ ~x --> ~(x & y)
579-
(rule (simplify (bxor ty (band ty x (bnot ty y)) (bnot ty x))) (bnot ty (band ty x y)))
580-
(rule (simplify (bxor ty (bnot ty x) (band ty x (bnot ty y)))) (bnot ty (band ty x y)))
581-
(rule (simplify (bxor ty (band ty (bnot ty y) x) (bnot ty x))) (bnot ty (band ty x y)))
582-
(rule (simplify (bxor ty (bnot ty x) (band ty (bnot ty y) x))) (bnot ty (band ty x y)))
583-
584548
;; (~x & y) ^ x --> x | y
585549
(rule (simplify (bxor ty (band ty (bnot ty x) y) x)) (bor ty x y))
586550
(rule (simplify (bxor ty x (band ty (bnot ty x) y))) (bor ty x y))
@@ -714,4 +678,4 @@
714678
(rule (simplify (uge ty (bor ty x y) x)) (iconst_u ty 1))
715679
(rule (simplify (uge ty (bor ty y x) x)) (iconst_u ty 1))
716680
(rule (simplify (ule ty x (bor ty x y))) (iconst_u ty 1))
717-
(rule (simplify (ule ty x (bor ty y x))) (iconst_u ty 1))
681+
(rule (simplify (ule ty x (bor ty y x))) (iconst_u ty 1))

cranelift/filetests/filetests/egraph/arithmetic-precise.clif

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ block0(v0: i32, v1: i32, v2: i32):
9696

9797
; function %test_isub_iadd_iadd_cancel(i32, i32, i32) -> i32 fast {
9898
; block0(v0: i32, v1: i32, v2: i32):
99-
; v6 = isub v1, v2
100-
; return v6
99+
; v3 = iadd v0, v1
100+
; v4 = iadd v0, v2
101+
; v5 = isub v3, v4
102+
; return v5
101103
; }
102104

103105
;; ((x - z) - (y - z)) --> (x - y)
@@ -111,8 +113,10 @@ block0(v0: i32, v1: i32, v2: i32):
111113

112114
; function %test_isub_isub_isub_cancel(i32, i32, i32) -> i32 fast {
113115
; block0(v0: i32, v1: i32, v2: i32):
114-
; v6 = isub v0, v1
115-
; return v6
116+
; v3 = isub v0, v2
117+
; v4 = isub v1, v2
118+
; v5 = isub v3, v4
119+
; return v5
116120
; }
117121

118122
;; ((x - y) - (x - z)) --> (z - y)
@@ -126,8 +130,10 @@ block0(v0: i32, v1: i32, v2: i32):
126130

127131
; function %test_isub_isub_isub_swap_cancel(i32, i32, i32) -> i32 fast {
128132
; block0(v0: i32, v1: i32, v2: i32):
129-
; v6 = isub v2, v1
130-
; return v6
133+
; v3 = isub v0, v1
134+
; v4 = isub v0, v2
135+
; v5 = isub v3, v4
136+
; return v5
131137
; }
132138

133139
;; umin(x, y) + umax(x, y) --> x + y
@@ -141,8 +147,10 @@ block0(v0: i32, v1: i32):
141147

142148
; function %test_iadd_umin_umax(i32, i32) -> i32 fast {
143149
; block0(v0: i32, v1: i32):
144-
; v6 = iadd v1, v0
145-
; return v6
150+
; v2 = umin v0, v1
151+
; v3 = umax v0, v1
152+
; v4 = iadd v2, v3
153+
; return v4
146154
; }
147155

148156
;; smin(x, y) + smax(x, y) --> x + y
@@ -156,8 +164,10 @@ block0(v0: i32, v1: i32):
156164

157165
; function %test_iadd_smin_smax(i32, i32) -> i32 fast {
158166
; block0(v0: i32, v1: i32):
159-
; v6 = iadd v1, v0
160-
; return v6
167+
; v2 = smin v0, v1
168+
; v3 = smax v0, v1
169+
; v4 = iadd v2, v3
170+
; return v4
161171
; }
162172

163173
;; ((x + z) - (y + z)) --> (x - y)
@@ -171,8 +181,10 @@ block0(v0: i32, v1: i32, v2: i32):
171181

172182
; function %test_isub_iadd_iadd_common_rhs(i32, i32, i32) -> i32 fast {
173183
; block0(v0: i32, v1: i32, v2: i32):
174-
; v6 = isub v0, v1
175-
; return v6
184+
; v3 = iadd v0, v2
185+
; v4 = iadd v1, v2
186+
; v5 = isub v3, v4
187+
; return v5
176188
; }
177189

178190
;; ((x - y) + (y + z)) --> (x + z)
@@ -186,8 +198,10 @@ block0(v0: i32, v1: i32, v2: i32):
186198

187199
; function %test_iadd_isub_iadd_cancel(i32, i32, i32) -> i32 fast {
188200
; block0(v0: i32, v1: i32, v2: i32):
189-
; v6 = iadd v0, v2
190-
; return v6
201+
; v3 = isub v0, v1
202+
; v4 = iadd v1, v2
203+
; v5 = iadd v3, v4
204+
; return v5
191205
; }
192206

193207
;; (x - (x + y)) --> -y
@@ -200,8 +214,9 @@ block0(v0: i32, v1: i32):
200214

201215
; function %test_isub_iadd_to_ineg(i32, i32) -> i32 fast {
202216
; block0(v0: i32, v1: i32):
203-
; v4 = ineg v1
204-
; return v4
217+
; v2 = iadd v0, v1
218+
; v3 = isub v0, v2
219+
; return v3
205220
; }
206221

207222
;; (x + (y + (z - x))) --> (y + z)
@@ -215,8 +230,10 @@ block0(v0: i32, v1: i32, v2: i32):
215230

216231
; function %test_iadd_iadd_isub_cancel(i32, i32, i32) -> i32 fast {
217232
; block0(v0: i32, v1: i32, v2: i32):
218-
; v6 = iadd v1, v2
219-
; return v6
233+
; v3 = isub v2, v0
234+
; v4 = iadd v1, v3
235+
; v5 = iadd v0, v4
236+
; return v5
220237
; }
221238

222239
;; (eq ty (iadd cty x y) (iadd cty y x)) -> 1
@@ -230,8 +247,8 @@ block0(v0: i32, v1: i32):
230247

231248
; function %simplify_icmp_eq_iadd_commute(i32, i32) -> i8 fast {
232249
; block0(v0: i32, v1: i32):
233-
; v5 = iconst.i8 1
234-
; return v5 ; v5 = 1
250+
; v6 = iconst.i8 1
251+
; return v6 ; v6 = 1
235252
; }
236253

237254
;; (eq ty (iadd cty x y) (iadd cty y x)) -> 1
@@ -244,11 +261,9 @@ block0(v0: i32x4, v1: i32x4):
244261
}
245262

246263
; function %simplify_vector_icmp_eq_iadd_commute(i32x4, i32x4) -> i32x4 fast {
247-
; const0 = 0xffffffffffffffffffffffffffffffff
248-
;
249264
; block0(v0: i32x4, v1: i32x4):
250-
; v9 = icmp eq v0, v0
251-
; return v9
265+
; v5 = icmp eq v0, v0
266+
; return v5
252267
; }
253268

254269
;; (x - y) != x --> y != 0
@@ -261,9 +276,9 @@ block0(v0: i32, v1: i32):
261276

262277
; function %simplify_icmp_ne_isub_x(i32, i32) -> i8 fast {
263278
; block0(v0: i32, v1: i32):
264-
; v4 = iconst.i32 0
265-
; v5 = icmp ne v1, v4 ; v4 = 0
266-
; return v5
279+
; v2 = isub v0, v1
280+
; v3 = icmp ne v2, v0
281+
; return v3
267282
; }
268283

269284
;; (x - y) == x --> y == 0

cranelift/filetests/filetests/egraph/fold-bitops.clif

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,11 @@ block0(v0: i32, v1: i32):
213213

214214
; function %test_isub_band_band_bnot(i32, i32) -> i32 fast {
215215
; block0(v0: i32, v1: i32):
216-
; v6 = bxor v0, v1
217-
; v7 = isub v6, v1
218-
; return v7
216+
; v2 = bnot v1
217+
; v3 = band v0, v2
218+
; v4 = band v0, v1
219+
; v5 = isub v3, v4
220+
; return v5
219221
; }
220222

221223
;; (x & ~y) | (~x & y) --> (x ^ y)
@@ -231,8 +233,12 @@ block0(v0: i32, v1: i32):
231233

232234
; function %test_bor_band_bnot_to_bxor(i32, i32) -> i32 fast {
233235
; block0(v0: i32, v1: i32):
234-
; v8 = bxor v1, v0
235-
; return v8
236+
; v2 = bnot v1
237+
; v3 = band v0, v2
238+
; v4 = bnot v0
239+
; v5 = band v4, v1
240+
; v6 = bor v3, v5
241+
; return v6
236242
; }
237243

238244
;; (x & ~y) | (x ^ y) --> (x ^ y)
@@ -247,8 +253,11 @@ block0(v0: i32, v1: i32):
247253

248254
; function %test_bor_band_bnot_bxor_absorb(i32, i32) -> i32 fast {
249255
; block0(v0: i32, v1: i32):
256+
; v2 = bnot v1
257+
; v3 = band v0, v2
250258
; v4 = bxor v0, v1
251-
; return v4
259+
; v5 = bor v3, v4
260+
; return v5
252261
; }
253262

254263
;; (x & ~y) ^ ~x --> ~(x & y)
@@ -263,9 +272,9 @@ block0(v0: i32, v1: i32):
263272

264273
; function %test_bxor_band_bnot_to_bnot_band(i32, i32) -> i32 fast {
265274
; block0(v0: i32, v1: i32):
266-
; v8 = band v1, v0
267-
; v12 = bnot v8
268-
; return v12
275+
; v6 = band v1, v0
276+
; v10 = bnot v6
277+
; return v10
269278
; }
270279

271280
;; (~x & y) ^ x --> x | y

tests/disas/x64-optimize-vector-types.wat

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,12 @@
212212
;; wasm[0]::function[9]:
213213
;; pushq %rbp
214214
;; movq %rsp, %rbp
215-
;; pxor %xmm0, %xmm0
216-
;; pcmpeqd %xmm0, %xmm1
217-
;; pcmpeqd %xmm2, %xmm2
218-
;; movdqa %xmm1, %xmm0
219-
;; pxor %xmm2, %xmm0
215+
;; movdqa %xmm0, %xmm2
216+
;; psubd %xmm1, %xmm0
217+
;; movdqa %xmm2, %xmm1
218+
;; pcmpeqd %xmm1, %xmm0
219+
;; pcmpeqd %xmm1, %xmm1
220+
;; pxor %xmm1, %xmm0
220221
;; movq %rbp, %rsp
221222
;; popq %rbp
222223
;; retq
@@ -243,11 +244,12 @@
243244
;; wasm[0]::function[12]:
244245
;; pushq %rbp
245246
;; movq %rsp, %rbp
246-
;; pxor %xmm0, %xmm0
247-
;; pcmpeqw %xmm0, %xmm1
248-
;; pcmpeqd %xmm2, %xmm2
249-
;; movdqa %xmm1, %xmm0
250-
;; pxor %xmm2, %xmm0
247+
;; movdqa %xmm0, %xmm2
248+
;; psubw %xmm1, %xmm0
249+
;; movdqa %xmm2, %xmm1
250+
;; pcmpeqw %xmm1, %xmm0
251+
;; pcmpeqd %xmm1, %xmm1
252+
;; pxor %xmm1, %xmm0
251253
;; movq %rbp, %rsp
252254
;; popq %rbp
253255
;; retq

0 commit comments

Comments
 (0)