From c50bdf600e84477cd7743a1180c1cad188678575 Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Thu, 5 Jan 2023 15:08:06 -0800 Subject: [PATCH] Cranelift: GVN all idempotently trapping but otherwise pure instructions (#5534) --- .../codegen/meta/src/shared/instructions.rs | 18 +++-- .../simple_gvn/idempotent-trapping.clif | 68 +++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 cranelift/filetests/filetests/simple_gvn/idempotent-trapping.clif diff --git a/cranelift/codegen/meta/src/shared/instructions.rs b/cranelift/codegen/meta/src/shared/instructions.rs index 9f4246f4ca..8cb16e33ba 100755 --- a/cranelift/codegen/meta/src/shared/instructions.rs +++ b/cranelift/codegen/meta/src/shared/instructions.rs @@ -1686,7 +1686,8 @@ pub(crate) fn define( ) .operands_in(vec![x, y]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); ig.push( @@ -1704,7 +1705,8 @@ pub(crate) fn define( ) .operands_in(vec![x, y]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); ig.push( @@ -1719,7 +1721,8 @@ pub(crate) fn define( ) .operands_in(vec![x, y]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); ig.push( @@ -1734,7 +1737,8 @@ pub(crate) fn define( ) .operands_in(vec![x, y]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); } @@ -3324,7 +3328,8 @@ pub(crate) fn define( ) .operands_in(vec![x]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); ig.push( @@ -3342,7 +3347,8 @@ pub(crate) fn define( ) .operands_in(vec![x]) .operands_out(vec![a]) - .can_trap(true), + .can_trap(true) + .side_effects_idempotent(true), ); let x = &Operand::new("x", Float); diff --git a/cranelift/filetests/filetests/simple_gvn/idempotent-trapping.clif b/cranelift/filetests/filetests/simple_gvn/idempotent-trapping.clif new file mode 100644 index 0000000000..d9b320c31f --- /dev/null +++ b/cranelift/filetests/filetests/simple_gvn/idempotent-trapping.clif @@ -0,0 +1,68 @@ +;; Test that we GVN instructions that can trap (which is idempotent as long as +;; it isn't a resumable trap), but which are still otherwise pure functions of +;; their inputs. + +test simple-gvn + +function %udiv(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = udiv v0, v1 + v3 = udiv v0, v1 + v4 = iadd v2, v3 +; check: v4 = iadd v2, v2 + return v4 +} + +function %sdiv(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = sdiv v0, v1 + v3 = sdiv v0, v1 + v4 = iadd v2, v3 +; check: v4 = iadd v2, v2 + return v4 +} + +function %urem(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = urem v0, v1 + v3 = urem v0, v1 + v4 = iadd v2, v3 +; check: v4 = iadd v2, v2 + return v4 +} + +function %srem(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = srem v0, v1 + v3 = srem v0, v1 + v4 = iadd v2, v3 +; check: v4 = iadd v2, v2 + return v4 +} + +function %uadd_overflow_trap(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = uadd_overflow_trap v0, v1, heap_oob + v3 = uadd_overflow_trap v0, v1, heap_oob + v4 = iadd v2, v3 +; check: v4 = iadd v2, v2 + return v4 +} + +function %fcvt_to_uint(f32) -> i32 { +block0(v0: f32): + v1 = fcvt_to_uint.i32 v0 + v2 = fcvt_to_uint.i32 v0 + v3 = iadd v1, v2 +; check: v3 = iadd v1, v1 + return v3 +} + +function %fcvt_to_sint(f32) -> i32 { +block0(v0: f32): + v1 = fcvt_to_sint.i32 v0 + v2 = fcvt_to_sint.i32 v0 + v3 = iadd v1, v2 +; check: v3 = iadd v1, v1 + return v3 +}