]> git.lizzy.rs Git - rust.git/commitdiff
Replace a lot of print+trap with plain trap
authorbjorn3 <bjorn3@users.noreply.github.com>
Sun, 13 Mar 2022 16:29:25 +0000 (17:29 +0100)
committerbjorn3 <bjorn3@users.noreply.github.com>
Mon, 14 Mar 2022 11:02:36 +0000 (12:02 +0100)
This reduces binary sizes by a decent amount:

libstd.so: 17% reduction
mini_core_hello_world: 27% reduction
simple-raytracer: 27% reduction

This also improves compile time of simple-raytracer by 0.5s (4% +- 2%)
In addition it is also a pre-requisite for building standalone binaries.

src/abi/mod.rs
src/base.rs
src/discriminant.rs
src/intrinsics/llvm.rs
src/intrinsics/mod.rs
src/lib.rs
src/trap.rs

index 3d527bd72b6f0c9f4ce242a3337fe302d1ff05b6..ef56fb191bff5aabbbb884150ea83421b560c5b3 100644 (file)
@@ -507,7 +507,7 @@ enum CallTarget {
         let ret_block = fx.get_block(dest);
         fx.bcx.ins().jump(ret_block, &[]);
     } else {
-        trap_unreachable(fx, "[corruption] Diverging function returned");
+        fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
     }
 }
 
index 59e9e23d882b2f5f72565998090b2e12eb54f40a..0a0d17a69c593e2adc64f2ed18a9be3c66b54bf5 100644 (file)
@@ -90,7 +90,7 @@ pub(crate) fn codegen_fn<'tcx>(
     } else if arg_uninhabited {
         fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
         fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
-        crate::trap::trap_unreachable(&mut fx, "function has uninhabited argument");
+        fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
     } else {
         tcx.sess.time("codegen clif ir", || {
             tcx.sess
@@ -424,18 +424,16 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) {
                         fx.bcx.ins().jump(destination_block, &[]);
                     }
                     None => {
-                        crate::trap::trap_unreachable(
-                            fx,
-                            "[corruption] Returned from noreturn inline asm",
-                        );
+                        fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
                     }
                 }
             }
             TerminatorKind::Resume | TerminatorKind::Abort => {
-                trap_unreachable(fx, "[corruption] Unwinding bb reached.");
+                // FIXME implement unwinding
+                fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
             }
             TerminatorKind::Unreachable => {
-                trap_unreachable(fx, "[corruption] Hit unreachable code.");
+                fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
             }
             TerminatorKind::Yield { .. }
             | TerminatorKind::FalseEdge { .. }
@@ -925,5 +923,5 @@ pub(crate) fn codegen_panic_inner<'tcx>(
         args,
     );
 
-    crate::trap::trap_unreachable(fx, "panic lang item returned");
+    fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
 }
index 3326f87f000757099e3a9940de6677609889b533..6b2893fdaeb29c53ce09550a27fed4c2c31fb520 100644 (file)
@@ -68,11 +68,10 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
     let layout = value.layout();
 
     if layout.abi == Abi::Uninhabited {
-        return trap_unreachable_ret_value(
-            fx,
-            dest_layout,
-            "[panic] Tried to get discriminant for uninhabited type.",
-        );
+        let true_ = fx.bcx.ins().iconst(types::I32, 1);
+        fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
+        // Return a dummy value
+        return CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout);
     }
 
     let (tag_scalar, tag_field, tag_encoding) = match &layout.variants {
index 098862b0662f398c0e917b2482dc492664843e93..0e4f7ee907a5146bb8347a2d65f2564657af264c 100644 (file)
@@ -126,12 +126,9 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>(
         };
     }
 
-    if let Some((_, dest)) = destination {
-        let ret_block = fx.get_block(dest);
-        fx.bcx.ins().jump(ret_block, &[]);
-    } else {
-        trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
-    }
+    let dest = destination.expect("all llvm intrinsics used by stdlib should return").1;
+    let ret_block = fx.get_block(dest);
+    fx.bcx.ins().jump(ret_block, &[]);
 }
 
 // llvm.x86.avx2.vperm2i128
index 8411ec0e035c9bfb0bdcfde8a5f311486b0a73d2..eeda0dd6f7040b94cdce4d903cea7c70a24cc253 100644 (file)
@@ -229,7 +229,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
             // Insert non returning intrinsics here
             match intrinsic {
                 sym::abort => {
-                    trap_abort(fx, "Called intrinsic::abort.");
+                    fx.bcx.ins().trap(TrapCode::User(0));
                 }
                 sym::transmute => {
                     crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", span);
@@ -1119,6 +1119,6 @@ fn swap(bcx: &mut FunctionBuilder<'_>, v: Value) -> Value {
         let ret_block = fx.get_block(dest);
         fx.bcx.ins().jump(ret_block, &[]);
     } else {
-        trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
+        fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
     }
 }
index a3c794fb156b46c0334f009ad9f816f138da3b7d..878b9390e1318c6c650cf56fb0ed6a3b1c132682 100644 (file)
@@ -105,7 +105,6 @@ mod prelude {
     pub(crate) use crate::common::*;
     pub(crate) use crate::debuginfo::{DebugContext, UnwindContext};
     pub(crate) use crate::pointer::Pointer;
-    pub(crate) use crate::trap::*;
     pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue};
 }
 
index 99b5366e3499359470f5d946f05aca567db1998e..923269c4de9ab8993fcf5e564f0a0a7f2f11dea4 100644 (file)
@@ -25,12 +25,6 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) {
     fx.bcx.ins().call(puts, &[msg_ptr]);
 }
 
-/// Trap code: user1
-pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<str>) {
-    codegen_print(fx, msg.as_ref());
-    fx.bcx.ins().trap(TrapCode::User(1));
-}
-
 /// Use this for example when a function call should never return. This will fill the current block,
 /// so you can **not** add instructions to it afterwards.
 ///
@@ -39,21 +33,6 @@ pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef<
     codegen_print(fx, msg.as_ref());
     fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
 }
-
-/// Like `trap_unreachable` but returns a fake value of the specified type.
-///
-/// Trap code: user65535
-pub(crate) fn trap_unreachable_ret_value<'tcx>(
-    fx: &mut FunctionCx<'_, '_, 'tcx>,
-    dest_layout: TyAndLayout<'tcx>,
-    msg: impl AsRef<str>,
-) -> CValue<'tcx> {
-    codegen_print(fx, msg.as_ref());
-    let true_ = fx.bcx.ins().iconst(types::I32, 1);
-    fx.bcx.ins().trapnz(true_, TrapCode::UnreachableCodeReached);
-    CValue::by_ref(Pointer::const_addr(fx, 0), dest_layout)
-}
-
 /// Use this when something is unimplemented, but `libcore` or `libstd` requires it to codegen.
 /// Unlike `trap_unreachable` this will not fill the current block, so you **must** add instructions
 /// to it afterwards.