Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Nim
Objective-C
Objective-C++
OCaml
OpenCL C
Pascal
Pony
Python
Racket
Ruby
Rust
Snowball
Scala
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
TypeScript Native
V
Vala
Visual Basic
WASM
Zig
Javascript
GIMPLE
Ygen
rust source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
mrustc (master)
rustc 1.0.0
rustc 1.1.0
rustc 1.10.0
rustc 1.11.0
rustc 1.12.0
rustc 1.13.0
rustc 1.14.0
rustc 1.15.1
rustc 1.16.0
rustc 1.17.0
rustc 1.18.0
rustc 1.19.0
rustc 1.2.0
rustc 1.20.0
rustc 1.21.0
rustc 1.22.0
rustc 1.23.0
rustc 1.24.0
rustc 1.25.0
rustc 1.26.0
rustc 1.27.0
rustc 1.27.1
rustc 1.28.0
rustc 1.29.0
rustc 1.3.0
rustc 1.30.0
rustc 1.31.0
rustc 1.32.0
rustc 1.33.0
rustc 1.34.0
rustc 1.35.0
rustc 1.36.0
rustc 1.37.0
rustc 1.38.0
rustc 1.39.0
rustc 1.4.0
rustc 1.40.0
rustc 1.41.0
rustc 1.42.0
rustc 1.43.0
rustc 1.44.0
rustc 1.45.0
rustc 1.45.2
rustc 1.46.0
rustc 1.47.0
rustc 1.48.0
rustc 1.49.0
rustc 1.5.0
rustc 1.50.0
rustc 1.51.0
rustc 1.52.0
rustc 1.53.0
rustc 1.54.0
rustc 1.55.0
rustc 1.56.0
rustc 1.57.0
rustc 1.58.0
rustc 1.59.0
rustc 1.6.0
rustc 1.60.0
rustc 1.61.0
rustc 1.62.0
rustc 1.63.0
rustc 1.64.0
rustc 1.65.0
rustc 1.66.0
rustc 1.67.0
rustc 1.68.0
rustc 1.69.0
rustc 1.7.0
rustc 1.70.0
rustc 1.71.0
rustc 1.72.0
rustc 1.73.0
rustc 1.74.0
rustc 1.75.0
rustc 1.76.0
rustc 1.77.0
rustc 1.78.0
rustc 1.79.0
rustc 1.8.0
rustc 1.80.0
rustc 1.81.0
rustc 1.82.0
rustc 1.9.0
rustc beta
rustc nightly
rustc-cg-gcc (master)
x86-64 GCCRS (GCC master)
x86-64 GCCRS (GCCRS master)
x86-64 GCCRS 14.1 (GCC)
x86-64 GCCRS 14.2 (GCC)
Options
Source code
#![feature(asm, test)] use std::mem; use std::convert::TryInto; /// An identity function that *__hints__* to the compiler to be madummyimally pessimistic about what /// `bench_identity` could do. /// /// Unlike [`std::convert::identity`], a Rust compiler is encouraged to assume that `bench_identity` can /// use `dummy` in any possible valid way that Rust code is allowed to without introducing undefined /// behavior in the calling code. This property makes `bench_identity` useful for writing code in which /// certain optimizations are not desired, such as benchmarks. /// /// Note however, that `bench_identity` is only (and can only be) provided on a "best-effort" basis. The /// extent to which it can block optimisations may vary depending upon the platform and code-gen /// backend used. Programs cannot rely on `bench_identity` for *correctness* in any way. /// /// [`std::convert::identity`]: crate::convert::identity fn bench_identity<T>(mut dummy: T) -> T { unsafe { // The `for_size` macro plus the match statement below produce the // optimized identity code for each of the sizes fitting within 8 bytes. // This is necessary because code like the following doesn't work yet, // due to current limitations of const generics. // // ``` // fn bench_identity_specialized<T>(mut dummy: T) -> T { // unsafe { // let dummy_md = mem::ManuallyDrop::new(dummy); // // let mut dummy_bytes = [0_u8; mem::size_of::<usize>()]; // let dummy_tsize: &mut [u8; mem::size_of::<T>()] = (&mut dummy_bytes[0..mem::size_of::<T>()]).try_into().unwrap(); // *dummy_tsize = mem::transmute_copy(&dummy_md); // let mut dummy_usize = usize::from_ne_bytes(dummy_bytes); // // asm!("/* {dummy} */", dummy = inout(reg) dummy_usize, options(nostack)); // // dummy_bytes = dummy_usize.to_ne_bytes(); // let dummy_tsize: &[u8; mem::size_of::<T>()] = &dummy_bytes[0..mem::size_of::<T>()].try_into().unwrap(); // dummy = mem::transmute_copy(dummy_tsize); // } // dummy // } // ``` macro_rules! for_size { ($size:literal) => {{ // Without ManuallyDrop, copying the value could result // in dropping `dummy` at end of scope, prematurely triggering // destructors. let dummy_md = mem::ManuallyDrop::new(dummy); // In order to convert `dummy: T` into a `usize`, we will first // copy its value as raw bytes, then create a `usize` from // those bytes. // `dummy_bytes` is a byte array the same size as `usize`. // `dummy_tsize` is a reference to a portion of `dummy_bytes` the same // size as `T`, using `try_into` to convert `&[u8]` to `&[u8; N]`. // `dummy_usize` is `dummy_bytes` converted to a single `usize`. let mut dummy_bytes = [0_u8; mem::size_of::<usize>()]; let dummy_tsize: &mut [u8; $size] = (&mut dummy_bytes[0..$size]).try_into().unwrap(); *dummy_tsize = mem::transmute_copy(&dummy_md); let mut dummy_usize = usize::from_ne_bytes(dummy_bytes); // "Use" the argument in some way LLVM can't introspect. // This implementation is better than the fallback one below, // as it results in fewer instructions and memory accesses. asm!("/* {dummy} */", dummy = inout(reg) dummy_usize, options(nostack)); // Convert `usize` back to bytes, extract N bytes, and // convert those bytes back to `T`. dummy_bytes = dummy_usize.to_ne_bytes(); let dummy_tsize: &[u8; $size] = &dummy_bytes[0..$size].try_into().unwrap(); dummy = mem::transmute_copy(dummy_tsize); }} } match (mem::size_of::<T>(), mem::size_of::<T>() > mem::size_of::<usize>()) { (0, false) => for_size!(0), (1, false) => for_size!(1), (2, false) => for_size!(2), (3, false) => for_size!(3), (4, false) => for_size!(4), (5, false) => for_size!(5), (6, false) => for_size!(6), (7, false) => for_size!(7), (8, false) => for_size!(8), _ => { // "Use" the argument in some way LLVM can't introspect. // This isn't the greatest implementation since it probably deoptimizes // more than we want, but it's so far good enough. asm!("/* {dummy} */", dummy = in(reg) &mut dummy) } } } dummy } pub fn std_black_box_u8(x: u8) -> u8 { std::hint::black_box(x) } pub fn bench_identity_u8(x: u8) -> u8 { bench_identity(x) } pub fn std_black_box_u16(x: u16) -> u16 { std::hint::black_box(x) } pub fn bench_identity_u16(x: u16) -> u16 { bench_identity(x) } pub fn std_black_box_u24(x: (u16, u8)) -> (u16, u8) { std::hint::black_box(x) } pub fn bench_identity_u24(x: (u16, u8)) -> (u16, u8) { bench_identity(x) } pub fn std_black_box_u32(x: u32) -> u32 { std::hint::black_box(x) } pub fn bench_identity_u32(x: u32) -> u32 { bench_identity(x) } pub fn std_black_box_u40(x: (u32, u8)) -> (u32, u8) { std::hint::black_box(x) } pub fn bench_identity_u40(x: (u32, u8)) -> (u32, u8) { bench_identity(x) } pub fn std_black_box_u48(x: (u32, u16)) -> (u32, u16) { std::hint::black_box(x) } pub fn bench_identity_u48(x: (u32, u16)) -> (u32, u16) { bench_identity(x) } pub fn std_black_box_u56(x: (u32, u16, u8)) -> (u32, u16, u8) { std::hint::black_box(x) } pub fn bench_identity_u56(x: (u32, u16, u8)) -> (u32, u16, u8) { bench_identity(x) } pub fn std_black_box_u64(x: u64) -> u64 { std::hint::black_box(x) } pub fn bench_identity_u64(x: u64) -> u64 { bench_identity(x) } pub fn std_black_box_u128(x: (u64, u64)) -> (u64, u64) { std::hint::black_box(x) } pub fn bench_identity_u128(x: (u64, u64)) -> (u64, u64) { bench_identity(x) }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
About the author
Statistics
Changelog
Version tree