diff options
author | Max Graey <maxgraey@gmail.com> | 2022-08-19 00:31:51 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-18 14:31:51 -0700 |
commit | fe188d28dad8a71bc905939306d64eaba6567a40 (patch) | |
tree | 049d44fc9bfa50b04858002d423504a1eaca344f | |
parent | 296a63eaaabd28990d6b3acf63af3d9d21190b8d (diff) | |
download | binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.tar.gz binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.tar.bz2 binaryen-fe188d28dad8a71bc905939306d64eaba6567a40.zip |
[OptimizeInstructions] Simplify rounding or conversions after int to float casts (#4720)
i32 -> f64 -> i32 rountripping optimizations:
```rust
i32(f64(i32(x))) -> x // i32.trunc(_sat)_f64_s(f64.convert_i32_s(x)) -> x
u32(f64(u32(x))) -> x // i32.trunc(_sat)_f64_u(f64.convert_i32_u(x)) -> x
// note assymetric signed / unsigned or unsigned / signed cases can't be simplified in similar way
```
and rounding after integer to float casts:
```rust
ceil(float(int(x))) -> float(int(x))
floor(float(int(x))) -> float(int(x))
trunc(float(int(x))) -> float(int(x))
nearest(float(int(x))) -> float(int(x))
```
where `float = f32 | f64`, `int = i32 | i64 | u32 | u64`
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 67 | ||||
-rw-r--r-- | test/lit/passes/optimize-instructions-nontrapping-float-to-int.wast | 77 | ||||
-rw-r--r-- | test/lit/passes/optimize-instructions.wast | 295 |
3 files changed, 439 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 4fe26e8ee..394a7c998 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -1039,6 +1039,10 @@ struct OptimizeInstructions if (auto* ret = deduplicateUnary(curr)) { return replaceCurrent(ret); } + + if (auto* ret = simplifyRoundingsAndConversions(curr)) { + return replaceCurrent(ret); + } } void visitSelect(Select* curr) { @@ -3619,6 +3623,69 @@ private: return nullptr; } + Expression* simplifyRoundingsAndConversions(Unary* curr) { + using namespace Abstract; + using namespace Match; + + switch (curr->op) { + case TruncSFloat64ToInt32: + case TruncSatSFloat64ToInt32: { + // i32 -> f64 -> i32 rountripping optimization: + // i32.trunc(_sat)_f64_s(f64.convert_i32_s(x)) ==> x + Expression* x; + if (matches(curr->value, unary(ConvertSInt32ToFloat64, any(&x)))) { + return x; + } + break; + } + case TruncUFloat64ToInt32: + case TruncSatUFloat64ToInt32: { + // u32 -> f64 -> u32 rountripping optimization: + // i32.trunc(_sat)_f64_u(f64.convert_i32_u(x)) ==> x + Expression* x; + if (matches(curr->value, unary(ConvertUInt32ToFloat64, any(&x)))) { + return x; + } + break; + } + case CeilFloat32: + case CeilFloat64: + case FloorFloat32: + case FloorFloat64: + case TruncFloat32: + case TruncFloat64: + case NearestFloat32: + case NearestFloat64: { + // Rounding after integer to float conversion may be skipped + // ceil(float(int(x))) ==> float(int(x)) + // floor(float(int(x))) ==> float(int(x)) + // trunc(float(int(x))) ==> float(int(x)) + // nearest(float(int(x))) ==> float(int(x)) + Unary* inner; + if (matches(curr->value, unary(&inner, any()))) { + switch (inner->op) { + case ConvertSInt32ToFloat32: + case ConvertSInt32ToFloat64: + case ConvertUInt32ToFloat32: + case ConvertUInt32ToFloat64: + case ConvertSInt64ToFloat32: + case ConvertSInt64ToFloat64: + case ConvertUInt64ToFloat32: + case ConvertUInt64ToFloat64: { + return inner; + } + default: { + } + } + } + break; + } + default: { + } + } + return nullptr; + } + Expression* deduplicateUnary(Unary* unaryOuter) { if (auto* unaryInner = unaryOuter->value->dynCast<Unary>()) { if (unaryInner->op == unaryOuter->op) { diff --git a/test/lit/passes/optimize-instructions-nontrapping-float-to-int.wast b/test/lit/passes/optimize-instructions-nontrapping-float-to-int.wast new file mode 100644 index 000000000..d3e759e1c --- /dev/null +++ b/test/lit/passes/optimize-instructions-nontrapping-float-to-int.wast @@ -0,0 +1,77 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. +;; RUN: wasm-opt %s --optimize-instructions --enable-nontrapping-float-to-int -S -o - | filecheck %s + +(module + (memory 0) + + ;; CHECK: (func $simplify_int_float_sat_conversion_roundtrips (param $x i32) (param $y i64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_sat_f64_s + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_sat_f64_u + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_sat_f32_u + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_sat_f64_u + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_sat_f64_s + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_sat_f32_u + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_sat_f32_s + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_int_float_sat_conversion_roundtrips (param $x i32) (param $y i64) + (drop (i32.trunc_sat_f64_u (f64.convert_i32_u (local.get $x)))) + (drop (i32.trunc_sat_f64_s (f64.convert_i32_s (local.get $x)))) + + ;; skips + (drop (i32.trunc_sat_f64_s (f64.convert_i32_u (local.get $x)))) + (drop (i32.trunc_sat_f64_u (f64.convert_i32_s (local.get $x)))) + (drop (i32.trunc_sat_f32_u (f32.convert_i32_u (local.get $x)))) + (drop (i32.trunc_sat_f64_u (f64.convert_i64_u (local.get $y)))) + (drop (i64.trunc_sat_f64_s (f64.convert_i64_s (local.get $y)))) + (drop (i64.trunc_sat_f32_u (f32.convert_i32_s (local.get $x)))) + (drop (i64.trunc_sat_f32_s (f32.convert_i64_s (local.get $y)))) + ) +) diff --git a/test/lit/passes/optimize-instructions.wast b/test/lit/passes/optimize-instructions.wast index 1bf3f751c..e90eba2a8 100644 --- a/test/lit/passes/optimize-instructions.wast +++ b/test/lit/passes/optimize-instructions.wast @@ -14092,6 +14092,301 @@ (drop (f64.reinterpret_i64 (i64.reinterpret_f64 (local.get $w)))) ) + ;; CHECK: (func $simplify_int_float_conversion_roundtrips (param $x i32) (param $y i64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_f64_u + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_f64_s + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_f32_u + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i32.trunc_f64_u + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_f64_s + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_f32_u + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (i64.trunc_f32_s + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $y) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_int_float_conversion_roundtrips (param $x i32) (param $y i64) + (drop (i32.trunc_f64_u (f64.convert_i32_u (local.get $x)))) + (drop (i32.trunc_f64_s (f64.convert_i32_s (local.get $x)))) + + ;; skips + (drop (i32.trunc_f64_u (f64.convert_i32_s (local.get $x)))) + (drop (i32.trunc_f64_s (f64.convert_i32_u (local.get $x)))) + (drop (i32.trunc_f32_u (f32.convert_i32_u (local.get $x)))) + (drop (i32.trunc_f64_u (f64.convert_i64_u (local.get $y)))) + (drop (i64.trunc_f64_s (f64.convert_i64_s (local.get $y)))) + (drop (i64.trunc_f32_u (f32.convert_i32_s (local.get $x)))) + (drop (i64.trunc_f32_s (f32.convert_i64_s (local.get $y)))) + ) + + ;; CHECK: (func $simplify_rounding_after_conversions_i32_to_f64 (param $x i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_rounding_after_conversions_i32_to_f64 (param $x i32) + (drop (f64.ceil (f64.convert_i32_u (local.get $x)))) + (drop (f64.ceil (f64.convert_i32_s (local.get $x)))) + + (drop (f64.floor (f64.convert_i32_u (local.get $x)))) + (drop (f64.floor (f64.convert_i32_s (local.get $x)))) + + (drop (f64.trunc (f64.convert_i32_u (local.get $x)))) + (drop (f64.trunc (f64.convert_i32_s (local.get $x)))) + + (drop (f64.nearest (f64.convert_i32_u (local.get $x)))) + (drop (f64.nearest (f64.convert_i32_s (local.get $x)))) + ) + + ;; CHECK: (func $simplify_rounding_after_conversions_i32_to_f32 (param $x i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i32_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_rounding_after_conversions_i32_to_f32 (param $x i32) + (drop (f32.ceil (f32.convert_i32_u (local.get $x)))) + (drop (f32.ceil (f32.convert_i32_s (local.get $x)))) + + (drop (f32.floor (f32.convert_i32_u (local.get $x)))) + (drop (f32.floor (f32.convert_i32_s (local.get $x)))) + + (drop (f32.trunc (f32.convert_i32_u (local.get $x)))) + (drop (f32.trunc (f32.convert_i32_s (local.get $x)))) + + (drop (f32.nearest (f32.convert_i32_u (local.get $x)))) + (drop (f32.nearest (f32.convert_i32_s (local.get $x)))) + ) + + ;; CHECK: (func $simplify_rounding_after_conversions_i64_to_f64 (param $x i64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f64.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_rounding_after_conversions_i64_to_f64 (param $x i64) + (drop (f64.ceil (f64.convert_i64_u (local.get $x)))) + (drop (f64.ceil (f64.convert_i64_s (local.get $x)))) + + (drop (f64.floor (f64.convert_i64_u (local.get $x)))) + (drop (f64.floor (f64.convert_i64_s (local.get $x)))) + + (drop (f64.trunc (f64.convert_i64_u (local.get $x)))) + (drop (f64.trunc (f64.convert_i64_s (local.get $x)))) + + (drop (f64.nearest (f64.convert_i64_u (local.get $x)))) + (drop (f64.nearest (f64.convert_i64_s (local.get $x)))) + ) + + ;; CHECK: (func $simplify_rounding_after_conversions_i64_to_f32 (param $x i64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_u + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (f32.convert_i64_s + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $simplify_rounding_after_conversions_i64_to_f32 (param $x i64) + (drop (f32.ceil (f32.convert_i64_u (local.get $x)))) + (drop (f32.ceil (f32.convert_i64_s (local.get $x)))) + + (drop (f32.floor (f32.convert_i64_u (local.get $x)))) + (drop (f32.floor (f32.convert_i64_s (local.get $x)))) + + (drop (f32.trunc (f32.convert_i64_u (local.get $x)))) + (drop (f32.trunc (f32.convert_i64_s (local.get $x)))) + + (drop (f32.nearest (f32.convert_i64_u (local.get $x)))) + (drop (f32.nearest (f32.convert_i64_s (local.get $x)))) + ) + ;; u64(i32.load(_8|_16)(_u|_s)(x)) => i64.load(_8|_16|_32)(_u|_s)(x) ;; except: ;; i64.extend_i32_u(i32.load8_s(x)) and |