about summary refs log tree commit diff
path: root/compiler/rustc_codegen_ssa/src
diff options
context:
space:
mode:
authorErik Desjardins <erikdesjardins@users.noreply.github.com>2022-06-13 21:49:59 -0400
committerErik Desjardins <erikdesjardins@users.noreply.github.com>2022-06-14 17:09:07 -0400
commit50f6a9ed87e47c7a8ff6aefcde01a33821e80e20 (patch)
treeba1c3aa1679492531f6238685c14c86997b262d2 /compiler/rustc_codegen_ssa/src
parentca122c7ebb3ab50149c9d3d24ddb59c252b32272 (diff)
downloadrust-50f6a9ed87e47c7a8ff6aefcde01a33821e80e20.tar.gz
rust-50f6a9ed87e47c7a8ff6aefcde01a33821e80e20.zip
use unchecked mul to compute slice sizes
...since slice sizes can't signed wrap

see https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html

> The total size len * mem::size_of::<T>() of the slice must be no larger than isize::MAX.
Diffstat (limited to 'compiler/rustc_codegen_ssa/src')
-rw-r--r--compiler/rustc_codegen_ssa/src/glue.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/compiler/rustc_codegen_ssa/src/glue.rs b/compiler/rustc_codegen_ssa/src/glue.rs
index 694f5434e9a..e6f402ef19d 100644
--- a/compiler/rustc_codegen_ssa/src/glue.rs
+++ b/compiler/rustc_codegen_ssa/src/glue.rs
@@ -39,7 +39,12 @@ pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
             // The info in this case is the length of the str, so the size is that
             // times the unit size.
             (
-                bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())),
+                // All slice sizes must fit into `isize`, so this multiplication cannot (signed) wrap.
+                // NOTE: ideally, we want the effects of both `unchecked_smul` and `unchecked_umul`
+                // (resulting in `mul nsw nuw` in LLVM IR), since we know that the multiplication
+                // cannot signed wrap, and that both operands are non-negative. But at the time of writing,
+                // `BuilderMethods` can't do this, and it doesn't seem to enable any further optimizations.
+                bx.unchecked_smul(info.unwrap(), bx.const_usize(unit.size.bytes())),
                 bx.const_usize(unit.align.abi.bytes()),
             )
         }