From 6094063c35d3b3f7c07b6c069ebeb27797ddadc1 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Tue, 16 Apr 2024 11:11:30 -0700 Subject: [PATCH] Update `checked_ops` so 32- and 64-bit gets the same checks --- ...cked_shl.PreCodegen.after.panic-abort.mir} | 0 ...cked_shl.PreCodegen.after.panic-unwind.mir | 62 +++++++++++++ tests/mir-opt/pre-codegen/checked_ops.rs | 5 +- ...p_forward.PreCodegen.after.panic-abort.mir | 92 +++++++++++++++++++ ...forward.PreCodegen.after.panic-unwind.mir} | 34 +++---- 5 files changed, 174 insertions(+), 19 deletions(-) rename tests/mir-opt/pre-codegen/{checked_ops.checked_shl.PreCodegen.after.mir => checked_ops.checked_shl.PreCodegen.after.panic-abort.mir} (100%) create mode 100644 tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-unwind.mir create mode 100644 tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir rename tests/mir-opt/pre-codegen/{checked_ops.step_forward.PreCodegen.after.mir => checked_ops.step_forward.PreCodegen.after.panic-unwind.mir} (66%) diff --git a/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-abort.mir similarity index 100% rename from tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.mir rename to tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-abort.mir diff --git a/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-unwind.mir b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-unwind.mir new file mode 100644 index 00000000000..cadebcdf1f8 --- /dev/null +++ b/tests/mir-opt/pre-codegen/checked_ops.checked_shl.PreCodegen.after.panic-unwind.mir @@ -0,0 +1,62 @@ +// MIR for `checked_shl` after PreCodegen + +fn checked_shl(_1: u32, _2: u32) -> Option { + debug x => _1; + debug rhs => _2; + let mut _0: std::option::Option; + scope 1 (inlined core::num::::checked_shl) { + debug self => _1; + debug rhs => _2; + let mut _6: bool; + scope 2 { + debug a => _4; + debug b => _5; + } + scope 3 (inlined core::num::::overflowing_shl) { + let mut _4: u32; + let mut _5: bool; + scope 4 (inlined core::num::::wrapping_shl) { + let mut _3: u32; + scope 5 (inlined core::num::::unchecked_shl) { + } + } + } + } + + bb0: { + StorageLive(_4); + StorageLive(_5); + StorageLive(_3); + _3 = BitAnd(_2, const 31_u32); + _4 = ShlUnchecked(_1, _3); + StorageDead(_3); + _5 = Ge(_2, const core::num::::BITS); + StorageLive(_6); + _6 = unlikely(move _5) -> [return: bb1, unwind unreachable]; + } + + bb1: { + switchInt(move _6) -> [0: bb2, otherwise: bb3]; + } + + bb2: { + _0 = Option::::Some(_4); + goto -> bb4; + } + + bb3: { + _0 = const Option::::None; + goto -> bb4; + } + + bb4: { + StorageDead(_6); + StorageDead(_5); + StorageDead(_4); + return; + } +} + +ALLOC0 (size: 8, align: 4) { + 00 00 00 00 __ __ __ __ │ ....░░░░ +} diff --git a/tests/mir-opt/pre-codegen/checked_ops.rs b/tests/mir-opt/pre-codegen/checked_ops.rs index 3ff1123d0b1..8dd5c4b495e 100644 --- a/tests/mir-opt/pre-codegen/checked_ops.rs +++ b/tests/mir-opt/pre-codegen/checked_ops.rs @@ -1,12 +1,13 @@ // skip-filecheck //@ compile-flags: -O -Zmir-opt-level=2 -Cdebuginfo=2 -//@ needs-unwind +// EMIT_MIR_FOR_EACH_PANIC_STRATEGY #![crate_type = "lib"] #![feature(step_trait)] // EMIT_MIR checked_ops.step_forward.PreCodegen.after.mir -pub fn step_forward(x: u32, n: usize) -> u32 { +pub fn step_forward(x: u16, n: usize) -> u16 { + // This uses `u16` so that the conversion to usize is always widening. std::iter::Step::forward(x, n) } diff --git a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir new file mode 100644 index 00000000000..9ff61d35fc5 --- /dev/null +++ b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-abort.mir @@ -0,0 +1,92 @@ +// MIR for `step_forward` after PreCodegen + +fn step_forward(_1: u16, _2: usize) -> u16 { + debug x => _1; + debug n => _2; + let mut _0: u16; + scope 1 (inlined ::forward) { + debug start => _1; + debug n => _2; + let mut _8: u16; + scope 2 { + } + scope 3 (inlined ::forward_checked) { + scope 4 { + scope 6 (inlined core::num::::checked_add) { + let mut _7: bool; + scope 7 { + } + scope 8 (inlined core::num::::overflowing_add) { + let mut _5: (u16, bool); + let _6: bool; + scope 9 { + } + } + } + } + scope 5 (inlined convert::num::ptr_try_from_impls:: for u16>::try_from) { + let mut _3: bool; + let mut _4: u16; + } + } + scope 10 (inlined Option::::is_none) { + scope 11 (inlined Option::::is_some) { + } + } + scope 12 (inlined core::num::::wrapping_add) { + } + } + + bb0: { + StorageLive(_4); + StorageLive(_3); + _3 = Gt(_2, const 65535_usize); + switchInt(move _3) -> [0: bb1, otherwise: bb5]; + } + + bb1: { + _4 = _2 as u16 (IntToInt); + StorageDead(_3); + StorageLive(_6); + StorageLive(_5); + _5 = CheckedAdd(_1, _4); + _6 = (_5.1: bool); + StorageDead(_5); + StorageLive(_7); + _7 = unlikely(move _6) -> [return: bb2, unwind unreachable]; + } + + bb2: { + switchInt(move _7) -> [0: bb3, otherwise: bb4]; + } + + bb3: { + StorageDead(_7); + StorageDead(_6); + goto -> bb7; + } + + bb4: { + StorageDead(_7); + StorageDead(_6); + goto -> bb6; + } + + bb5: { + StorageDead(_3); + goto -> bb6; + } + + bb6: { + assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::::MAX, const 1_u16) -> [success: bb7, unwind unreachable]; + } + + bb7: { + StorageLive(_8); + _8 = _2 as u16 (IntToInt); + _0 = Add(_1, _8); + StorageDead(_8); + StorageDead(_4); + return; + } +} diff --git a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir similarity index 66% rename from tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir rename to tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir index abbd170b4b9..36cba172a1a 100644 --- a/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.mir +++ b/tests/mir-opt/pre-codegen/checked_ops.step_forward.PreCodegen.after.panic-unwind.mir @@ -1,51 +1,51 @@ // MIR for `step_forward` after PreCodegen -fn step_forward(_1: u32, _2: usize) -> u32 { +fn step_forward(_1: u16, _2: usize) -> u16 { debug x => _1; debug n => _2; - let mut _0: u32; - scope 1 (inlined ::forward) { + let mut _0: u16; + scope 1 (inlined ::forward) { debug start => _1; debug n => _2; - let mut _8: u32; + let mut _8: u16; scope 2 { } - scope 3 (inlined ::forward_checked) { + scope 3 (inlined ::forward_checked) { scope 4 { - scope 6 (inlined core::num::::checked_add) { + scope 6 (inlined core::num::::checked_add) { let mut _7: bool; scope 7 { } - scope 8 (inlined core::num::::overflowing_add) { - let mut _5: (u32, bool); + scope 8 (inlined core::num::::overflowing_add) { + let mut _5: (u16, bool); let _6: bool; scope 9 { } } } } - scope 5 (inlined convert::num::ptr_try_from_impls:: for u32>::try_from) { + scope 5 (inlined convert::num::ptr_try_from_impls:: for u16>::try_from) { let mut _3: bool; - let mut _4: u32; + let mut _4: u16; } } - scope 10 (inlined Option::::is_none) { - scope 11 (inlined Option::::is_some) { + scope 10 (inlined Option::::is_none) { + scope 11 (inlined Option::::is_some) { } } - scope 12 (inlined core::num::::wrapping_add) { + scope 12 (inlined core::num::::wrapping_add) { } } bb0: { StorageLive(_4); StorageLive(_3); - _3 = Gt(_2, const 4294967295_usize); + _3 = Gt(_2, const 65535_usize); switchInt(move _3) -> [0: bb1, otherwise: bb5]; } bb1: { - _4 = _2 as u32 (IntToInt); + _4 = _2 as u16 (IntToInt); StorageDead(_3); StorageLive(_6); StorageLive(_5); @@ -78,12 +78,12 @@ fn step_forward(_1: u32, _2: usize) -> u32 { } bb6: { - assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::::MAX, const 1_u32) -> [success: bb7, unwind continue]; + assert(!const true, "attempt to compute `{} + {}`, which would overflow", const core::num::::MAX, const 1_u16) -> [success: bb7, unwind continue]; } bb7: { StorageLive(_8); - _8 = _2 as u32 (IntToInt); + _8 = _2 as u16 (IntToInt); _0 = Add(_1, _8); StorageDead(_8); StorageDead(_4);