diff --git a/Cargo.lock b/Cargo.lock index faed52727c8..f15e319e3b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "anyhow" -version = "1.0.51" +version = "1.0.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203" +checksum = "94a45b455c14666b85fc40a019e8ab9eb75e3a124e05494f5397122bc9eb06e0" [[package]] name = "ar" @@ -33,18 +33,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc0cb7df82c8cf8f2e6a8dd394a0932a71369c160cc9b027dca414fced242513" +checksum = "71447555acc6c875c52c407d572fc1327dc5c34cba72b4b2e7ad048aa4e4fd19" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe4463c15fa42eee909e61e5eac4866b7c6d22d0d8c621e57a0c5380753bfa8c" +checksum = "ec9a10261891a7a919b0d4f6aa73582e88441d9a8f6173c88efbe4a5a362ea67" dependencies = [ "cranelift-bforest", "cranelift-codegen-meta", @@ -59,31 +59,30 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "793f6a94a053a55404ea16e1700202a88101672b8cd6b4df63e13cde950852bf" +checksum = "815755d76fcbcf6e17ab888545b28ab775f917cb12ce0797e60cd41a2288692c" dependencies = [ "cranelift-codegen-shared", - "cranelift-entity", ] [[package]] name = "cranelift-codegen-shared" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44aa1846df275bce5eb30379d65964c7afc63c05a117076e62a119c25fe174be" +checksum = "23ea92f2a67335a2e4d3c9c65624c3b14ae287d595b0650822c41824febab66b" [[package]] name = "cranelift-entity" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3a45d8d6318bf8fc518154d9298eab2a8154ec068a8885ff113f6db8d69bb3a" +checksum = "bd25847875e388c500ad3624b4d2e14067955c93185194a7222246a25b91c975" [[package]] name = "cranelift-frontend" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e07339bd461766deb7605169de039e01954768ff730fa1254e149001884a8525" +checksum = "308bcfb7eb47bdf5ff6e1ace262af4ed39ec19f204c751fffb037e0e82a0c8bf" dependencies = [ "cranelift-codegen", "log", @@ -93,9 +92,9 @@ dependencies = [ [[package]] name = "cranelift-jit" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e8f0d60fb5d67f7a1e5c49db38ba96d1c846921faef02085fc5590b74781747" +checksum = "f560b3a314b8d15facf411e5d29b917c3e787a2bbc3fcdc5183bc0c5b7d4fe01" dependencies = [ "anyhow", "cranelift-codegen", @@ -111,21 +110,19 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "825ac7e0959cbe7ddc9cc21209f0319e611a57f9fcb2b723861fe7ef2017e651" +checksum = "3a57aba9e603d694d1430ff38bd914bae23ef9c2e44b25a65e318905807e654c" dependencies = [ "anyhow", "cranelift-codegen", - "cranelift-entity", - "log", ] [[package]] name = "cranelift-native" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03e2fca76ff57e0532936a71e3fc267eae6a19a86656716479c66e7f912e3d7b" +checksum = "12cdc799aee673be2317e631d4569a1ba0a7e77a07a7ce45557086d2e02e9514" dependencies = [ "cranelift-codegen", "libc", @@ -134,9 +131,9 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.78.0" +version = "0.81.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55500d0fc9bb05c0944fc4506649249d28f55bd4fe95b87f0e55bf41058f0e6d" +checksum = "502a7333836052fcdf4425d7f7a21264d99f862d32b9c3a0e47cd920487a9b60" dependencies = [ "anyhow", "cranelift-codegen", @@ -148,18 +145,18 @@ dependencies = [ [[package]] name = "crc32fast" -version = "1.3.0" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "738c290dfaea84fc1ca15ad9c168d083b05a714e1efddd8edaab678dc28d2836" +checksum = "a2209c310e29876f7f0b2721e7e26b84aff178aa3da5d091f9bfbf47669e60e3" dependencies = [ "cfg-if", ] [[package]] name = "gimli" -version = "0.25.0" +version = "0.26.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" dependencies = [ "indexmap", ] @@ -182,9 +179,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.112" +version = "0.2.116" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "565dbd88872dbe4cc8a46e527f26483c1d1f7afa6b884a3bd6cd893d4f98da74" [[package]] name = "libloading" @@ -233,9 +230,9 @@ dependencies = [ [[package]] name = "regalloc" -version = "0.0.32" +version = "0.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6304468554ed921da3d32c355ea107b8d13d7b8996c3adfb7aab48d3bc321f4" +checksum = "62446b1d3ebf980bdc68837700af1d77b37bc430e524bf95319c6eada2a4cc02" dependencies = [ "log", "rustc-hash", @@ -281,9 +278,9 @@ dependencies = [ [[package]] name = "smallvec" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309" +checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83" [[package]] name = "target-lexicon" diff --git a/Cargo.toml b/Cargo.toml index 2d19040b509..178404af42d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,14 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { version = "0.78.0", features = ["unwind", "all-arch"] } -cranelift-frontend = "0.78.0" -cranelift-module = "0.78.0" -cranelift-native = "0.78.0" -cranelift-jit = { version = "0.78.0", optional = true } -cranelift-object = "0.78.0" +cranelift-codegen = { version = "0.81.0", features = ["unwind", "all-arch"] } +cranelift-frontend = "0.81.0" +cranelift-module = "0.81.0" +cranelift-native = "0.81.0" +cranelift-jit = { version = "0.81.0", optional = true } +cranelift-object = "0.81.0" target-lexicon = "0.12.0" -gimli = { version = "0.25.0", default-features = false, features = ["write"]} +gimli = { version = "0.26.0", default-features = false, features = ["write"]} object = { version = "0.27.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] } ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "do_not_remove_cg_clif_ranlib" } diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index dd096562480..b9786395142 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -34,15 +34,15 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.0.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "cc" -version = "1.0.72" +version = "1.0.73" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22a9137b95ea06864e018375b72adfb7db6e6f68cfc8df5a04d00288050485ee" +checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" [[package]] name = "cfg-if" @@ -56,7 +56,7 @@ dependencies = [ [[package]] name = "compiler_builtins" -version = "0.1.66" +version = "0.1.70" dependencies = [ "rustc-std-workspace-core", ] @@ -110,9 +110,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758" dependencies = [ "compiler_builtins", "rustc-std-workspace-alloc", @@ -121,9 +121,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.19" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +checksum = "1ab7905ea95c6d9af62940f9d7dd9596d54c334ae2c15300c482051292d5637f" dependencies = [ "compiler_builtins", "libc", @@ -132,9 +132,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.112" +version = "0.2.119" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b03d17f364a3a042d5e5d46b053bbbf82c92c9430c592dd4c064dc6ee997125" +checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" dependencies = [ "rustc-std-workspace-core", ] @@ -319,9 +319,9 @@ dependencies = [ [[package]] name = "wasi" -version = "0.9.0+wasi-snapshot-preview1" +version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" dependencies = [ "compiler_builtins", "rustc-std-workspace-alloc", diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index 1382c7e5379..0a56eb131ed 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -49,7 +49,7 @@ pub(crate) fn build_backend( cmd.env("RUSTFLAGS", rustflags); eprintln!("[BUILD] rustc_codegen_cranelift"); - crate::utils::spawn_and_wait(cmd); + super::utils::spawn_and_wait(cmd); Path::new("target").join(host_triple).join(channel) } diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 2956fb698e1..c9c003d4610 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -3,9 +3,9 @@ use std::fs; use std::path::{Path, PathBuf}; use std::process::{self, Command}; -use crate::rustc_info::{get_file_name, get_rustc_version}; -use crate::utils::{spawn_and_wait, try_hard_link}; -use crate::SysrootKind; +use super::rustc_info::{get_file_name, get_rustc_version}; +use super::utils::{spawn_and_wait, try_hard_link}; +use super::SysrootKind; pub(crate) fn build_sysroot( channel: &str, @@ -52,7 +52,7 @@ pub(crate) fn build_sysroot( .arg("-g"); spawn_and_wait(build_cargo_wrapper_cmd); - let default_sysroot = crate::rustc_info::get_default_sysroot(); + let default_sysroot = super::rustc_info::get_default_sysroot(); let rustlib = target_dir.join("lib").join("rustlib"); let host_rustlib_lib = rustlib.join(host_triple).join("lib"); @@ -167,7 +167,7 @@ fn build_clif_sysroot_for_triple( let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel); - if !crate::config::get_bool("keep_sysroot") { + if !super::config::get_bool("keep_sysroot") { // Cleanup the target dir with the exception of build scripts and the incremental cache for dir in ["build", "deps", "examples", "native"] { if build_dir.join(dir).exists() { diff --git a/build_system/mod.rs b/build_system/mod.rs new file mode 100644 index 00000000000..b228da3981f --- /dev/null +++ b/build_system/mod.rs @@ -0,0 +1,127 @@ +use std::env; +use std::path::PathBuf; +use std::process; + +mod build_backend; +mod build_sysroot; +mod config; +mod prepare; +mod rustc_info; +mod utils; + +fn usage() { + eprintln!("Usage:"); + eprintln!(" ./y.rs prepare"); + eprintln!( + " ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" + ); +} + +macro_rules! arg_error { + ($($err:tt)*) => {{ + eprintln!($($err)*); + usage(); + std::process::exit(1); + }}; +} + +enum Command { + Build, +} + +#[derive(Copy, Clone)] +pub(crate) enum SysrootKind { + None, + Clif, + Llvm, +} + +pub fn main() { + env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); + env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); + // The target dir is expected in the default location. Guard against the user changing it. + env::set_var("CARGO_TARGET_DIR", "target"); + + let mut args = env::args().skip(1); + let command = match args.next().as_deref() { + Some("prepare") => { + if args.next().is_some() { + arg_error!("./x.rs prepare doesn't expect arguments"); + } + prepare::prepare(); + process::exit(0); + } + Some("build") => Command::Build, + Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), + Some(command) => arg_error!("Unknown command {}", command), + None => { + usage(); + process::exit(0); + } + }; + + let mut target_dir = PathBuf::from("build"); + let mut channel = "release"; + let mut sysroot_kind = SysrootKind::Clif; + let mut use_unstable_features = true; + while let Some(arg) = args.next().as_deref() { + match arg { + "--target-dir" => { + target_dir = PathBuf::from(args.next().unwrap_or_else(|| { + arg_error!("--target-dir requires argument"); + })) + } + "--debug" => channel = "debug", + "--sysroot" => { + sysroot_kind = match args.next().as_deref() { + Some("none") => SysrootKind::None, + Some("clif") => SysrootKind::Clif, + Some("llvm") => SysrootKind::Llvm, + Some(arg) => arg_error!("Unknown sysroot kind {}", arg), + None => arg_error!("--sysroot requires argument"), + } + } + "--no-unstable-features" => use_unstable_features = false, + flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag), + arg => arg_error!("Unexpected argument {}", arg), + } + } + + let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { + host_triple + } else if let Some(host_triple) = config::get_value("host") { + host_triple + } else { + rustc_info::get_host_triple() + }; + let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") { + if target_triple != "" { + target_triple + } else { + host_triple.clone() // Empty target triple can happen on GHA + } + } else if let Some(target_triple) = config::get_value("target") { + target_triple + } else { + host_triple.clone() + }; + + if target_triple.ends_with("-msvc") { + eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift."); + eprintln!("Switch to the MinGW toolchain for Windows support."); + eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to"); + eprintln!("set the global default target to MinGW"); + process::exit(1); + } + + let cg_clif_build_dir = + build_backend::build_backend(channel, &host_triple, use_unstable_features); + build_sysroot::build_sysroot( + channel, + sysroot_kind, + &target_dir, + cg_clif_build_dir, + &host_triple, + &target_triple, + ); +} diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 561e2ed7b00..4a7df2cebbc 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -5,8 +5,8 @@ use std::fs; use std::path::Path; use std::process::Command; -use crate::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; -use crate::utils::{copy_dir_recursively, spawn_and_wait}; +use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; +use super::utils::{copy_dir_recursively, spawn_and_wait}; pub(crate) fn prepare() { prepare_sysroot(); @@ -92,7 +92,7 @@ fn prepare_sysroot() { clone_repo( "build_sysroot/compiler-builtins", "https://github.com/rust-lang/compiler-builtins.git", - "0.1.66", + "0.1.70", ); apply_patches("compiler-builtins", Path::new("build_sysroot/compiler-builtins")); } diff --git a/clean_all.sh b/clean_all.sh index 865de7d234f..ea1f8c1e892 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -2,5 +2,5 @@ set -e rm -rf build_sysroot/{sysroot_src/,target/,compiler-builtins/,rustc_version} -rm -rf target/ build/ perf.data{,.old} +rm -rf target/ build/ perf.data{,.old} y.bin rm -rf rand/ regex/ simple-raytracer/ portable-simd/ diff --git a/patches/0001-compiler-builtins-Disable-128bit-atomic-operations.patch b/patches/0001-compiler-builtins-Disable-128bit-atomic-operations.patch index 7daea99f579..460e42d1d8c 100644 --- a/patches/0001-compiler-builtins-Disable-128bit-atomic-operations.patch +++ b/patches/0001-compiler-builtins-Disable-128bit-atomic-operations.patch @@ -13,33 +13,33 @@ index 107762c..2d1ae10 100644 --- a/src/mem/mod.rs +++ b/src/mem/mod.rs @@ -137,10 +137,6 @@ intrinsics! { - pub extern "C" fn __llvm_memcpy_element_unordered_atomic_8(dest: *mut u64, src: *const u64, bytes: usize) -> () { + pub unsafe extern "C" fn __llvm_memcpy_element_unordered_atomic_8(dest: *mut u64, src: *const u64, bytes: usize) -> () { memcpy_element_unordered_atomic(dest, src, bytes); } - #[cfg(target_has_atomic_load_store = "128")] -- pub extern "C" fn __llvm_memcpy_element_unordered_atomic_16(dest: *mut u128, src: *const u128, bytes: usize) -> () { +- pub unsafe extern "C" fn __llvm_memcpy_element_unordered_atomic_16(dest: *mut u128, src: *const u128, bytes: usize) -> () { - memcpy_element_unordered_atomic(dest, src, bytes); - } #[cfg(target_has_atomic_load_store = "8")] - pub extern "C" fn __llvm_memmove_element_unordered_atomic_1(dest: *mut u8, src: *const u8, bytes: usize) -> () { + pub unsafe extern "C" fn __llvm_memmove_element_unordered_atomic_1(dest: *mut u8, src: *const u8, bytes: usize) -> () { @@ -158,10 +154,6 @@ intrinsics! { - pub extern "C" fn __llvm_memmove_element_unordered_atomic_8(dest: *mut u64, src: *const u64, bytes: usize) -> () { + pub unsafe extern "C" fn __llvm_memmove_element_unordered_atomic_8(dest: *mut u64, src: *const u64, bytes: usize) -> () { memmove_element_unordered_atomic(dest, src, bytes); } - #[cfg(target_has_atomic_load_store = "128")] -- pub extern "C" fn __llvm_memmove_element_unordered_atomic_16(dest: *mut u128, src: *const u128, bytes: usize) -> () { +- pub unsafe extern "C" fn __llvm_memmove_element_unordered_atomic_16(dest: *mut u128, src: *const u128, bytes: usize) -> () { - memmove_element_unordered_atomic(dest, src, bytes); - } #[cfg(target_has_atomic_load_store = "8")] - pub extern "C" fn __llvm_memset_element_unordered_atomic_1(s: *mut u8, c: u8, bytes: usize) -> () { + pub unsafe extern "C" fn __llvm_memset_element_unordered_atomic_1(s: *mut u8, c: u8, bytes: usize) -> () { @@ -179,8 +171,4 @@ intrinsics! { - pub extern "C" fn __llvm_memset_element_unordered_atomic_8(s: *mut u64, c: u8, bytes: usize) -> () { + pub unsafe extern "C" fn __llvm_memset_element_unordered_atomic_8(s: *mut u64, c: u8, bytes: usize) -> () { memset_element_unordered_atomic(s, c, bytes); } - #[cfg(target_has_atomic_load_store = "128")] -- pub extern "C" fn __llvm_memset_element_unordered_atomic_16(s: *mut u128, c: u8, bytes: usize) -> () { +- pub unsafe extern "C" fn __llvm_memset_element_unordered_atomic_16(s: *mut u128, c: u8, bytes: usize) -> () { - memset_element_unordered_atomic(s, c, bytes); - } } diff --git a/patches/0022-sysroot-Disable-not-compiling-tests.patch b/patches/0022-sysroot-Disable-not-compiling-tests.patch index 25a315f666e..1c45c7573c8 100644 --- a/patches/0022-sysroot-Disable-not-compiling-tests.patch +++ b/patches/0022-sysroot-Disable-not-compiling-tests.patch @@ -22,7 +22,7 @@ index 0000000..46fd999 +[package] +name = "core" +version = "0.0.0" -+edition = "2018" ++edition = "2021" + +[lib] +name = "coretests" @@ -44,7 +44,7 @@ index 1a6be3a..42dbd59 100644 --- a/library/core/tests/ptr.rs +++ b/library/core/tests/ptr.rs @@ -250,6 +250,7 @@ fn test_unsized_nonnull() { - assert!(ys == zs); + }; } +/* diff --git a/patches/0027-sysroot-128bit-atomic-operations.patch b/patches/0027-sysroot-128bit-atomic-operations.patch index ffee641457a..8e6652af374 100644 --- a/patches/0027-sysroot-128bit-atomic-operations.patch +++ b/patches/0027-sysroot-128bit-atomic-operations.patch @@ -8,7 +8,6 @@ Cranelift doesn't support them yet library/core/src/panic/unwind_safe.rs | 6 ----- library/core/src/sync/atomic.rs | 38 --------------------------- library/core/tests/atomic.rs | 4 --- - library/std/src/time/monotonic.rs | 6 +++-- 4 files changed, 4 insertions(+), 50 deletions(-) diff --git a/library/core/src/panic/unwind_safe.rs b/library/core/src/panic/unwind_safe.rs @@ -99,38 +98,6 @@ index b735957..ea728b6 100644 #[cfg(target_has_atomic = "ptr")] assert_eq!(align_of::(), size_of::()); #[cfg(target_has_atomic = "ptr")] -diff --git a/library/std/src/time/monotonic.rs b/library/std/src/time/monotonic.rs -index fa96b7a..2854f9c 100644 ---- a/library/std/src/time/monotonic.rs -+++ b/library/std/src/time/monotonic.rs -@@ -5,7 +5,7 @@ pub(super) fn monotonize(raw: time::Instant) -> time::Instant { - inner::monotonize(raw) - } - --#[cfg(any(all(target_has_atomic = "64", not(target_has_atomic = "128")), target_arch = "aarch64"))] -+#[cfg(target_has_atomic = "64")] - pub mod inner { - use crate::sync::atomic::AtomicU64; - use crate::sync::atomic::Ordering::*; -@@ -70,6 +70,7 @@ pub mod inner { - } - } - -+/* - #[cfg(all(target_has_atomic = "128", not(target_arch = "aarch64")))] - pub mod inner { - use crate::sync::atomic::AtomicU128; -@@ -94,8 +95,9 @@ pub mod inner { - ZERO.checked_add_duration(&Duration::new(secs, nanos)).unwrap() - } - } -+*/ - --#[cfg(not(any(target_has_atomic = "64", target_has_atomic = "128")))] -+#[cfg(not(target_has_atomic = "64"))] - pub mod inner { - use crate::cmp; - use crate::sys::time; -- 2.26.2.7.g19db9cfb68 diff --git a/rust-toolchain b/rust-toolchain index cab94c0b8cf..1019b1f069e 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2021-12-30" +channel = "nightly-2022-02-23" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 6bcc3049ecc..b146ea36037 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -8,7 +8,7 @@ source ./scripts/setup_rust_fork.sh echo "[TEST] Test suite of rustc" pushd rust -cargo install ripgrep +command -v rg >/dev/null 2>&1 || cargo install ripgrep rm -r src/test/ui/{extern/,panics/,unsized-locals/,lto/,simd*,linkage*,unwind-*.rs} || true for test in $(rg --files-with-matches "asm!|catch_unwind|should_panic|lto|// needs-asm-support" src/test/ui); do @@ -78,6 +78,8 @@ rm src/test/ui/intrinsics/const-eval-select-x86_64.rs # same rm src/test/ui/match/issue-82392.rs # differing error rm src/test/ui/consts/min_const_fn/address_of_const.rs # same rm src/test/ui/consts/issue-miri-1910.rs # same +rm src/test/ui/generic-associated-types/bugs/issue-80626.rs # same +rm src/test/ui/generic-associated-types/bugs/issue-89008.rs # same rm src/test/ui/type-alias-impl-trait/cross_crate_ice*.rs # requires removed aux dep rm src/test/ui/allocator/no_std-alloc-error-handler-default.rs # missing rust_oom definition @@ -98,6 +100,7 @@ rm src/test/ui/abi/stack-protector.rs # requires stack protector support rm src/test/incremental/issue-80691-bad-eval-cache.rs # wrong exit code rm src/test/incremental/spike-neg1.rs # errors out for some reason rm src/test/incremental/spike-neg2.rs # same +rm src/test/ui/issues/issue-74564-if-expr-stack-overflow.rs # gives a stackoverflow before the backend runs rm src/test/incremental/thinlto/cgu_invalidated_when_import_{added,removed}.rs # requires LLVM diff --git a/src/allocator.rs b/src/allocator.rs index 637d30f9344..82247b47888 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -3,7 +3,6 @@ use crate::prelude::*; -use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink}; use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS}; /// Returns whether an allocator shim was created @@ -91,9 +90,7 @@ fn codegen_inner( bcx.seal_all_blocks(); bcx.finalize(); } - module - .define_function(func_id, &mut ctx, &mut NullTrapSink {}, &mut NullStackMapSink {}) - .unwrap(); + module.define_function(func_id, &mut ctx).unwrap(); unwind_context.add_function(func_id, &ctx, module.isa()); } @@ -130,8 +127,6 @@ fn codegen_inner( bcx.seal_all_blocks(); bcx.finalize(); } - module - .define_function(func_id, &mut ctx, &mut NullTrapSink {}, &mut NullStackMapSink {}) - .unwrap(); + module.define_function(func_id, &mut ctx).unwrap(); unwind_context.add_function(func_id, &ctx, module.isa()); } diff --git a/src/base.rs b/src/base.rs index 917afa4eae0..0a6ef6441fa 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,13 +1,15 @@ //! Codegen of a single function -use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink}; use rustc_ast::InlineAsmOptions; use rustc_index::vec::IndexVec; use rustc_middle::ty::adjustment::PointerCast; use rustc_middle::ty::layout::FnAbiOf; +use indexmap::IndexSet; + use crate::constant::ConstantCx; use crate::prelude::*; +use crate::pretty_clif::CommentWriter; pub(crate) fn codegen_fn<'tcx>( cx: &mut crate::CodegenCx<'tcx>, @@ -99,27 +101,54 @@ pub(crate) fn codegen_fn<'tcx>( // Recover all necessary data from fx, before accessing func will prevent future access to it. let instance = fx.instance; - let mut clif_comments = fx.clif_comments; + let clif_comments = fx.clif_comments; let source_info_set = fx.source_info_set; let local_map = fx.local_map; fx.constants_cx.finalize(fx.tcx, &mut *fx.module); - // Store function in context - let context = &mut cx.cached_context; - context.func = func; - crate::pretty_clif::write_clif_file( tcx, "unopt", module.isa(), instance, - &context, + &func, &clif_comments, ); // Verify function - verify_func(tcx, &clif_comments, &context.func); + verify_func(tcx, &clif_comments, &func); + + compile_fn( + cx, + module, + instance, + symbol_name.name, + func_id, + func, + clif_comments, + source_info_set, + local_map, + ); +} + +fn compile_fn<'tcx>( + cx: &mut crate::CodegenCx<'tcx>, + module: &mut dyn Module, + instance: Instance<'tcx>, + symbol_name: &str, + func_id: FuncId, + func: Function, + mut clif_comments: CommentWriter, + source_info_set: IndexSet, + local_map: IndexVec>, +) { + let tcx = cx.tcx; + + // Store function in context + let context = &mut cx.cached_context; + context.clear(); + context.func = func; // If the return block is not reachable, then the SSA builder may have inserted an `iconst.i128` // instruction, which doesn't have an encoding. @@ -145,9 +174,7 @@ pub(crate) fn codegen_fn<'tcx>( // Define function tcx.sess.time("define function", || { context.want_disasm = crate::pretty_clif::should_write_ir(tcx); - module - .define_function(func_id, context, &mut NullTrapSink {}, &mut NullStackMapSink {}) - .unwrap() + module.define_function(func_id, context).unwrap() }); // Write optimized function to file for debugging @@ -156,7 +183,7 @@ pub(crate) fn codegen_fn<'tcx>( "opt", module.isa(), instance, - &context, + &context.func, &clif_comments, ); @@ -177,7 +204,7 @@ pub(crate) fn codegen_fn<'tcx>( debug_context.define_function( instance, func_id, - symbol_name.name, + symbol_name, isa, context, &source_info_set, @@ -186,9 +213,6 @@ pub(crate) fn codegen_fn<'tcx>( } unwind_context.add_function(func_id, &context, isa); }); - - // Clear context to make it usable for the next function - context.clear(); } pub(crate) fn verify_func( diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 046e4393a68..2e047c7eea1 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -56,6 +56,9 @@ fn emit_module( let tmp_file = tcx.output_filenames(()).temp_path(OutputType::Object, Some(&name)); let obj = product.object.write().unwrap(); + + tcx.sess.prof.artifact_size("object_file", name.clone(), obj.len().try_into().unwrap()); + if let Err(err) = std::fs::write(&tmp_file, obj) { tcx.sess.fatal(&format!("error writing object file: {}", err)); } diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 309d27090b5..9e07528313d 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -7,7 +7,6 @@ use std::lazy::SyncOnceCell; use std::os::raw::{c_char, c_int}; use std::sync::{mpsc, Mutex}; -use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink}; use rustc_codegen_ssa::CrateInfo; use rustc_middle::mir::mono::MonoItem; use rustc_session::Session; @@ -381,12 +380,5 @@ fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx>, module: &mut JITModule, inst: In let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec(); trampoline_builder.ins().return_(&ret_vals); - module - .define_function( - func_id, - &mut cx.cached_context, - &mut NullTrapSink {}, - &mut NullStackMapSink {}, - ) - .unwrap(); + module.define_function(func_id, &mut cx.cached_context).unwrap(); } diff --git a/src/intrinsics/llvm.rs b/src/intrinsics/llvm.rs index be3704ca276..098862b0662 100644 --- a/src/intrinsics/llvm.rs +++ b/src/intrinsics/llvm.rs @@ -8,14 +8,14 @@ use rustc_middle::ty::subst::SubstsRef; pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, intrinsic: &str, - substs: SubstsRef<'tcx>, + _substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], destination: Option<(CPlace<'tcx>, BasicBlock)>, ) { let ret = destination.unwrap().0; intrinsic_match! { - fx, intrinsic, substs, args, + fx, intrinsic, args, _ => { fx.tcx.sess.warn(&format!("unsupported llvm intrinsic {}; replacing with trap", intrinsic)); crate::trap::trap_unimplemented(fx, intrinsic); @@ -52,8 +52,8 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( ret.write_cvalue(fx, res); }; "llvm.x86.sse2.cmp.ps" | "llvm.x86.sse2.cmp.pd", (c x, c y, o kind) { - let kind_const = crate::constant::mir_operand_get_const_val(fx, kind).expect("llvm.x86.sse2.cmp.* kind not const"); - let flt_cc = match kind_const.try_to_bits(Size::from_bytes(1)).unwrap_or_else(|| panic!("kind not scalar: {:?}", kind_const)) { + let kind = crate::constant::mir_operand_get_const_val(fx, kind).expect("llvm.x86.sse2.cmp.* kind not const"); + let flt_cc = match kind.try_to_bits(Size::from_bytes(1)).unwrap_or_else(|| panic!("kind not scalar: {:?}", kind)) { 0 => FloatCC::Equal, 1 => FloatCC::LessThan, 2 => FloatCC::LessThanOrEqual, @@ -73,32 +73,30 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( kind => unreachable!("kind {:?}", kind), }; - simd_pair_for_each_lane(fx, x, y, ret, |fx, lane_layout, res_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { + simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, res_lane_ty, x_lane, y_lane| { + let res_lane = match lane_ty.kind() { ty::Float(_) => fx.bcx.ins().fcmp(flt_cc, x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), + _ => unreachable!("{:?}", lane_ty), }; - bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane) + bool_to_zero_or_max_uint(fx, res_lane_ty, res_lane) }); }; "llvm.x86.sse2.psrli.d", (c a, o imm8) { let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const"); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| { - let res_lane = match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) { + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) { imm8 if imm8 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm8 as u8)), _ => fx.bcx.ins().iconst(types::I32, 0), - }; - CValue::by_val(res_lane, res_lane_layout) + } }); }; "llvm.x86.sse2.pslli.d", (c a, o imm8) { let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8).expect("llvm.x86.sse2.psrli.d imm8 not const"); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, res_lane_layout, lane| { - let res_lane = match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) { + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + match imm8.try_to_bits(Size::from_bytes(4)).unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) { imm8 if imm8 < 32 => fx.bcx.ins().ishl_imm(lane, i64::from(imm8 as u8)), _ => fx.bcx.ins().iconst(types::I32, 0), - }; - CValue::by_val(res_lane, res_lane_layout) + } }); }; "llvm.x86.sse2.storeu.dq", (v mem_addr, c a) { diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 42717ad0ae0..4dfb13476c2 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -9,7 +9,8 @@ pub(crate) use cpuid::codegen_cpuid_call; pub(crate) use llvm::codegen_llvm_intrinsic_call; use rustc_middle::ty::print::with_no_trimmed_paths; -use rustc_span::symbol::{kw, sym}; +use rustc_middle::ty::subst::SubstsRef; +use rustc_span::symbol::{kw, sym, Symbol}; use crate::prelude::*; use cranelift_codegen::ir::AtomicRmwOp; @@ -30,51 +31,29 @@ macro intrinsic_pat { } macro intrinsic_arg { - (o $fx:expr, $arg:ident) => { - $arg - }, + (o $fx:expr, $arg:ident) => {}, (c $fx:expr, $arg:ident) => { - codegen_operand($fx, $arg) + let $arg = codegen_operand($fx, $arg); }, (v $fx:expr, $arg:ident) => { - codegen_operand($fx, $arg).load_scalar($fx) - } -} - -macro intrinsic_substs { - ($substs:expr, $index:expr,) => {}, - ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => { - let $first = $substs.type_at($index); - intrinsic_substs!($substs, $index+1, $($rest),*); + let $arg = codegen_operand($fx, $arg).load_scalar($fx); } } macro intrinsic_match { - ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr, + ($fx:expr, $intrinsic:expr, $args:expr, _ => $unknown:block; $( - $($($name:tt).*)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block; + $($($name:tt).*)|+ $(if $cond:expr)?, ($($a:ident $arg:ident),*) $content:block; )*) => { - let _ = $substs; // Silence warning when substs is unused. match $intrinsic { $( $(intrinsic_pat!($($name).*))|* $(if $cond)? => { - #[allow(unused_parens, non_snake_case)] - { - $( - intrinsic_substs!($substs, 0, $($subst),*); - )? - if let [$($arg),*] = $args { - let ($($arg,)*) = ( - $(intrinsic_arg!($a $fx, $arg),)* - ); - #[warn(unused_parens, non_snake_case)] - { - $content - } - } else { - bug!("wrong number of args for intrinsic {:?}", $intrinsic); - } + if let [$($arg),*] = $args { + $(intrinsic_arg!($a $fx, $arg);)* + $content + } else { + bug!("wrong number of args for intrinsic {:?}", $intrinsic); } } )* @@ -83,63 +62,21 @@ macro intrinsic_match { } } -macro call_intrinsic_match { - ($fx:expr, $intrinsic:expr, $substs:expr, $ret:expr, $destination:expr, $args:expr, $( - $name:ident($($arg:ident),*) -> $ty:ident => $func:ident, - )*) => { - match $intrinsic { - $( - sym::$name => { - assert!($substs.is_empty()); - if let [$(ref $arg),*] = *$args { - let ($($arg,)*) = ( - $(codegen_operand($fx, $arg),)* - ); - let res = $fx.easy_call(stringify!($func), &[$($arg),*], $fx.tcx.types.$ty); - $ret.write_cvalue($fx, res); - - if let Some((_, dest)) = $destination { - let ret_block = $fx.get_block(dest); - $fx.bcx.ins().jump(ret_block, &[]); - return; - } else { - unreachable!(); - } - } else { - bug!("wrong number of args for intrinsic {:?}", $intrinsic); - } - } - )* - _ => {} - } - } -} - -macro validate_atomic_type($fx:ident, $intrinsic:ident, $span:ident, $ty:expr) { - match $ty.kind() { - ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} - _ => { - $fx.tcx.sess.span_err( - $span, - &format!( - "`{}` intrinsic: expected basic integer or raw pointer type, found `{:?}`", - $intrinsic, $ty - ), - ); - // Prevent verifier error - crate::trap::trap_unreachable($fx, "compilation should not have succeeded"); - return; - } - } -} - -macro validate_simd_type($fx:ident, $intrinsic:ident, $span:ident, $ty:expr) { - if !$ty.is_simd() { - $fx.tcx.sess.span_err($span, &format!("invalid monomorphization of `{}` intrinsic: expected SIMD input type, found non-SIMD `{}`", $intrinsic, $ty)); - // Prevent verifier error - crate::trap::trap_unreachable($fx, "compilation should not have succeeded"); - return; - } +fn report_atomic_type_validation_error<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + intrinsic: Symbol, + span: Span, + ty: Ty<'tcx>, +) { + fx.tcx.sess.span_err( + span, + &format!( + "`{}` intrinsic: expected basic integer or raw pointer type, found `{:?}`", + intrinsic, ty + ), + ); + // Prevent verifier error + crate::trap::trap_unreachable(fx, "compilation should not have succeeded"); } pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> Option { @@ -159,12 +96,7 @@ fn simd_for_each_lane<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, val: CValue<'tcx>, ret: CPlace<'tcx>, - f: impl Fn( - &mut FunctionCx<'_, '_, 'tcx>, - TyAndLayout<'tcx>, - TyAndLayout<'tcx>, - Value, - ) -> CValue<'tcx>, + f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value) -> Value, ) { let layout = val.layout(); @@ -177,7 +109,8 @@ fn simd_for_each_lane<'tcx>( for lane_idx in 0..lane_count { let lane = val.value_lane(fx, lane_idx).load_scalar(fx); - let res_lane = f(fx, lane_layout, ret_lane_layout, lane); + let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, lane); + let res_lane = CValue::by_val(res_lane, ret_lane_layout); ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); } @@ -188,13 +121,7 @@ fn simd_pair_for_each_lane<'tcx>( x: CValue<'tcx>, y: CValue<'tcx>, ret: CPlace<'tcx>, - f: impl Fn( - &mut FunctionCx<'_, '_, 'tcx>, - TyAndLayout<'tcx>, - TyAndLayout<'tcx>, - Value, - Value, - ) -> CValue<'tcx>, + f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Ty<'tcx>, Value, Value) -> Value, ) { assert_eq!(x.layout(), y.layout()); let layout = x.layout(); @@ -209,7 +136,8 @@ fn simd_pair_for_each_lane<'tcx>( let x_lane = x.value_lane(fx, lane_idx).load_scalar(fx); let y_lane = y.value_lane(fx, lane_idx).load_scalar(fx); - let res_lane = f(fx, lane_layout, ret_lane_layout, x_lane, y_lane); + let res_lane = f(fx, lane_layout.ty, ret_lane_layout.ty, x_lane, y_lane); + let res_lane = CValue::by_val(res_lane, ret_lane_layout); ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); } @@ -220,7 +148,7 @@ fn simd_reduce<'tcx>( val: CValue<'tcx>, acc: Option, ret: CPlace<'tcx>, - f: impl Fn(&mut FunctionCx<'_, '_, 'tcx>, TyAndLayout<'tcx>, Value, Value) -> Value, + f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Ty<'tcx>, Value, Value) -> Value, ) { let (lane_count, lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); let lane_layout = fx.layout_of(lane_ty); @@ -230,7 +158,7 @@ fn simd_reduce<'tcx>( if let Some(acc) = acc { (acc, 0) } else { (val.value_lane(fx, 0).load_scalar(fx), 1) }; for lane_idx in start_lane..lane_count { let lane = val.value_lane(fx, lane_idx).load_scalar(fx); - res_val = f(fx, lane_layout, res_val, lane); + res_val = f(fx, lane_layout.ty, res_val, lane); } let res = CValue::by_val(res_val, lane_layout); ret.write_cvalue(fx, res); @@ -241,7 +169,7 @@ fn simd_reduce_bool<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, val: CValue<'tcx>, ret: CPlace<'tcx>, - f: impl Fn(&mut FunctionCx<'_, '_, 'tcx>, Value, Value) -> Value, + f: &dyn Fn(&mut FunctionCx<'_, '_, 'tcx>, Value, Value) -> Value, ) { let (lane_count, _lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); assert!(ret.layout().ty.is_bool()); @@ -264,10 +192,10 @@ fn simd_reduce_bool<'tcx>( fn bool_to_zero_or_max_uint<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, - layout: TyAndLayout<'tcx>, + ty: Ty<'tcx>, val: Value, -) -> CValue<'tcx> { - let ty = fx.clif_type(layout.ty).unwrap(); +) -> Value { + let ty = fx.clif_type(ty).unwrap(); let int_ty = match ty { types::F32 => types::I32, @@ -282,122 +210,7 @@ fn bool_to_zero_or_max_uint<'tcx>( res = fx.bcx.ins().bitcast(ty, res); } - CValue::by_val(res, layout) -} - -macro simd_cmp { - ($fx:expr, $cc:ident|$cc_f:ident($x:ident, $y:ident) -> $ret:ident) => { - let vector_ty = clif_vector_type($fx.tcx, $x.layout()); - - if let Some(vector_ty) = vector_ty { - let x = $x.load_scalar($fx); - let y = $y.load_scalar($fx); - let val = if vector_ty.lane_type().is_float() { - $fx.bcx.ins().fcmp(FloatCC::$cc_f, x, y) - } else { - $fx.bcx.ins().icmp(IntCC::$cc, x, y) - }; - - // HACK This depends on the fact that icmp for vectors represents bools as 0 and !0, not 0 and 1. - let val = $fx.bcx.ins().raw_bitcast(vector_ty, val); - - $ret.write_cvalue($fx, CValue::by_val(val, $ret.layout())); - } else { - simd_pair_for_each_lane( - $fx, - $x, - $y, - $ret, - |fx, lane_layout, res_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Uint(_) | ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc, x_lane, y_lane), - ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::$cc_f, x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), - }; - bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane) - }, - ); - } - }, - ($fx:expr, $cc_u:ident|$cc_s:ident|$cc_f:ident($x:ident, $y:ident) -> $ret:ident) => { - // FIXME use vector icmp when possible - simd_pair_for_each_lane( - $fx, - $x, - $y, - $ret, - |fx, lane_layout, res_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Uint(_) => fx.bcx.ins().icmp(IntCC::$cc_u, x_lane, y_lane), - ty::Int(_) => fx.bcx.ins().icmp(IntCC::$cc_s, x_lane, y_lane), - ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::$cc_f, x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), - }; - bool_to_zero_or_max_uint(fx, res_lane_layout, res_lane) - }, - ); - }, -} - -macro simd_int_binop { - ($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) => { - simd_int_binop!($fx, $op|$op($x, $y) -> $ret); - }, - ($fx:expr, $op_u:ident|$op_s:ident($x:ident, $y:ident) -> $ret:ident) => { - simd_pair_for_each_lane( - $fx, - $x, - $y, - $ret, - |fx, lane_layout, ret_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane), - ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), - }; - CValue::by_val(res_lane, ret_lane_layout) - }, - ); - }, -} - -macro simd_int_flt_binop { - ($fx:expr, $op:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => { - simd_int_flt_binop!($fx, $op|$op|$op_f($x, $y) -> $ret); - }, - ($fx:expr, $op_u:ident|$op_s:ident|$op_f:ident($x:ident, $y:ident) -> $ret:ident) => { - simd_pair_for_each_lane( - $fx, - $x, - $y, - $ret, - |fx, lane_layout, ret_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Uint(_) => fx.bcx.ins().$op_u(x_lane, y_lane), - ty::Int(_) => fx.bcx.ins().$op_s(x_lane, y_lane), - ty::Float(_) => fx.bcx.ins().$op_f(x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), - }; - CValue::by_val(res_lane, ret_lane_layout) - }, - ); - }, -} - -macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) { - simd_pair_for_each_lane( - $fx, - $x, - $y, - $ret, - |fx, lane_layout, ret_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Float(_) => fx.bcx.ins().$op(x_lane, y_lane), - _ => unreachable!("{:?}", lane_layout.ty), - }; - CValue::by_val(res_lane, ret_lane_layout) - }, - ); + res } pub(crate) fn codegen_intrinsic_call<'tcx>( @@ -428,59 +241,111 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; if intrinsic.as_str().starts_with("simd_") { - self::simd::codegen_simd_intrinsic_call(fx, instance, args, ret, span); + self::simd::codegen_simd_intrinsic_call(fx, intrinsic, substs, args, ret, span); let ret_block = fx.get_block(destination.expect("SIMD intrinsics don't diverge").1); fx.bcx.ins().jump(ret_block, &[]); - return; + } else if codegen_float_intrinsic_call(fx, intrinsic, args, ret) { + let ret_block = fx.get_block(destination.expect("Float intrinsics don't diverge").1); + fx.bcx.ins().jump(ret_block, &[]); + } else { + codegen_regular_intrinsic_call( + fx, + instance, + intrinsic, + substs, + args, + ret, + span, + destination, + ); + } +} + +fn codegen_float_intrinsic_call<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + intrinsic: Symbol, + args: &[mir::Operand<'tcx>], + ret: CPlace<'tcx>, +) -> bool { + let (name, arg_count, ty) = match intrinsic { + sym::expf32 => ("expf", 1, fx.tcx.types.f32), + sym::expf64 => ("exp", 1, fx.tcx.types.f64), + sym::exp2f32 => ("exp2f", 1, fx.tcx.types.f32), + sym::exp2f64 => ("exp2", 1, fx.tcx.types.f64), + sym::sqrtf32 => ("sqrtf", 1, fx.tcx.types.f32), + sym::sqrtf64 => ("sqrt", 1, fx.tcx.types.f64), + sym::powif32 => ("__powisf2", 2, fx.tcx.types.f32), // compiler-builtins + sym::powif64 => ("__powidf2", 2, fx.tcx.types.f64), // compiler-builtins + sym::powf32 => ("powf", 2, fx.tcx.types.f32), + sym::powf64 => ("pow", 2, fx.tcx.types.f64), + sym::logf32 => ("logf", 1, fx.tcx.types.f32), + sym::logf64 => ("log", 1, fx.tcx.types.f64), + sym::log2f32 => ("log2f", 1, fx.tcx.types.f32), + sym::log2f64 => ("log2", 1, fx.tcx.types.f64), + sym::log10f32 => ("log10f", 1, fx.tcx.types.f32), + sym::log10f64 => ("log10", 1, fx.tcx.types.f64), + sym::fabsf32 => ("fabsf", 1, fx.tcx.types.f32), + sym::fabsf64 => ("fabs", 1, fx.tcx.types.f64), + sym::fmaf32 => ("fmaf", 3, fx.tcx.types.f32), + sym::fmaf64 => ("fma", 3, fx.tcx.types.f64), + sym::copysignf32 => ("copysignf", 2, fx.tcx.types.f32), + sym::copysignf64 => ("copysign", 2, fx.tcx.types.f64), + sym::floorf32 => ("floorf", 1, fx.tcx.types.f32), + sym::floorf64 => ("floor", 1, fx.tcx.types.f64), + sym::ceilf32 => ("ceilf", 1, fx.tcx.types.f32), + sym::ceilf64 => ("ceil", 1, fx.tcx.types.f64), + sym::truncf32 => ("truncf", 1, fx.tcx.types.f32), + sym::truncf64 => ("trunc", 1, fx.tcx.types.f64), + sym::roundf32 => ("roundf", 1, fx.tcx.types.f32), + sym::roundf64 => ("round", 1, fx.tcx.types.f64), + sym::sinf32 => ("sinf", 1, fx.tcx.types.f32), + sym::sinf64 => ("sin", 1, fx.tcx.types.f64), + sym::cosf32 => ("cosf", 1, fx.tcx.types.f32), + sym::cosf64 => ("cos", 1, fx.tcx.types.f64), + _ => return false, + }; + + if args.len() != arg_count { + bug!("wrong number of args for intrinsic {:?}", intrinsic); } + let (a, b, c); + let args = match args { + [x] => { + a = [codegen_operand(fx, x)]; + &a as &[_] + } + [x, y] => { + b = [codegen_operand(fx, x), codegen_operand(fx, y)]; + &b + } + [x, y, z] => { + c = [codegen_operand(fx, x), codegen_operand(fx, y), codegen_operand(fx, z)]; + &c + } + _ => unreachable!(), + }; + + let res = fx.easy_call(name, &args, ty); + ret.write_cvalue(fx, res); + + true +} + +fn codegen_regular_intrinsic_call<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + instance: Instance<'tcx>, + intrinsic: Symbol, + substs: SubstsRef<'tcx>, + args: &[mir::Operand<'tcx>], + ret: CPlace<'tcx>, + span: Span, + destination: Option<(CPlace<'tcx>, BasicBlock)>, +) { let usize_layout = fx.layout_of(fx.tcx.types.usize); - call_intrinsic_match! { - fx, intrinsic, substs, ret, destination, args, - expf32(flt) -> f32 => expf, - expf64(flt) -> f64 => exp, - exp2f32(flt) -> f32 => exp2f, - exp2f64(flt) -> f64 => exp2, - sqrtf32(flt) -> f32 => sqrtf, - sqrtf64(flt) -> f64 => sqrt, - powif32(a, x) -> f32 => __powisf2, // compiler-builtins - powif64(a, x) -> f64 => __powidf2, // compiler-builtins - powf32(a, x) -> f32 => powf, - powf64(a, x) -> f64 => pow, - logf32(flt) -> f32 => logf, - logf64(flt) -> f64 => log, - log2f32(flt) -> f32 => log2f, - log2f64(flt) -> f64 => log2, - log10f32(flt) -> f32 => log10f, - log10f64(flt) -> f64 => log10, - fabsf32(flt) -> f32 => fabsf, - fabsf64(flt) -> f64 => fabs, - fmaf32(x, y, z) -> f32 => fmaf, - fmaf64(x, y, z) -> f64 => fma, - copysignf32(x, y) -> f32 => copysignf, - copysignf64(x, y) -> f64 => copysign, - - // rounding variants - // FIXME use clif insts - floorf32(flt) -> f32 => floorf, - floorf64(flt) -> f64 => floor, - ceilf32(flt) -> f32 => ceilf, - ceilf64(flt) -> f64 => ceil, - truncf32(flt) -> f32 => truncf, - truncf64(flt) -> f64 => trunc, - roundf32(flt) -> f32 => roundf, - roundf64(flt) -> f64 => round, - - // trigonometry - sinf32(flt) -> f32 => sinf, - sinf64(flt) -> f64 => sin, - cosf32(flt) -> f32 => cosf, - cosf64(flt) -> f64 => cos, - } - intrinsic_match! { - fx, intrinsic, substs, args, + fx, intrinsic, args, _ => { fx.tcx.sess.span_fatal(span, &format!("unsupported intrinsic {}", intrinsic)); }; @@ -492,7 +357,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( breakpoint, () { fx.bcx.ins().debugtrap(); }; - copy | copy_nonoverlapping, (v src, v dst, v count) { + copy | copy_nonoverlapping, (v src, v dst, v count) { + let elem_ty = substs.type_at(0); let elem_size: u64 = fx.layout_of(elem_ty).size.bytes(); assert_eq!(args.len(), 3); let byte_amount = if elem_size != 1 { @@ -510,7 +376,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( } }; // NOTE: the volatile variants have src and dst swapped - volatile_copy_memory | volatile_copy_nonoverlapping_memory, (v dst, v src, v count) { + volatile_copy_memory | volatile_copy_nonoverlapping_memory, (v dst, v src, v count) { + let elem_ty = substs.type_at(0); let elem_size: u64 = fx.layout_of(elem_ty).size.bytes(); assert_eq!(args.len(), 3); let byte_amount = if elem_size != 1 { @@ -528,8 +395,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( fx.bcx.call_memmove(fx.target_config, dst, src, byte_amount); } }; - size_of_val, (c ptr) { - let layout = fx.layout_of(T); + size_of_val, (c ptr) { + let layout = fx.layout_of(substs.type_at(0)); let size = if layout.is_unsized() { let (_ptr, info) = ptr.load_scalar_pair(fx); let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout, info); @@ -542,8 +409,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; ret.write_cvalue(fx, CValue::by_val(size, usize_layout)); }; - min_align_of_val, (c ptr) { - let layout = fx.layout_of(T); + min_align_of_val, (c ptr) { + let layout = fx.layout_of(substs.type_at(0)); let align = if layout.is_unsized() { let (_ptr, info) = ptr.load_scalar_pair(fx); let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout, info); @@ -557,12 +424,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ret.write_cvalue(fx, CValue::by_val(align, usize_layout)); }; - unchecked_add | unchecked_sub | unchecked_div | exact_div | unchecked_rem + unchecked_add | unchecked_sub | unchecked_mul | unchecked_div | exact_div | unchecked_rem | unchecked_shl | unchecked_shr, (c x, c y) { // FIXME trap on overflow let bin_op = match intrinsic { sym::unchecked_add => BinOp::Add, sym::unchecked_sub => BinOp::Sub, + sym::unchecked_mul => BinOp::Mul, sym::unchecked_div | sym::exact_div => BinOp::Div, sym::unchecked_rem => BinOp::Rem, sym::unchecked_shl => BinOp::Shl, @@ -589,7 +457,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ); ret.write_cvalue(fx, res); }; - saturating_add | saturating_sub, (c lhs, c rhs) { + saturating_add | saturating_sub, (c lhs, c rhs) { assert_eq!(lhs.layout().ty, rhs.layout().ty); let bin_op = match intrinsic { sym::saturating_add => BinOp::Add, @@ -597,7 +465,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( _ => unreachable!(), }; - let signed = type_sign(T); + let signed = type_sign(lhs.layout().ty); let checked_res = crate::num::codegen_checked_int_binop( fx, @@ -607,7 +475,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ); let (val, has_overflow) = checked_res.load_scalar_pair(fx); - let clif_ty = fx.clif_type(T).unwrap(); + let clif_ty = fx.clif_type(lhs.layout().ty).unwrap(); let (min, max) = type_min_max_value(&mut fx.bcx, clif_ty, signed); @@ -629,17 +497,19 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( _ => unreachable!(), }; - let res = CValue::by_val(val, fx.layout_of(T)); + let res = CValue::by_val(val, lhs.layout()); ret.write_cvalue(fx, res); }; - rotate_left, (v x, v y) { - let layout = fx.layout_of(T); + rotate_left, (c x, v y) { + let layout = x.layout(); + let x = x.load_scalar(fx); let res = fx.bcx.ins().rotl(x, y); ret.write_cvalue(fx, CValue::by_val(res, layout)); }; - rotate_right, (v x, v y) { - let layout = fx.layout_of(T); + rotate_right, (c x, v y) { + let layout = x.layout(); + let x = x.load_scalar(fx); let res = fx.bcx.ins().rotr(x, y); ret.write_cvalue(fx, CValue::by_val(res, layout)); }; @@ -675,29 +545,33 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( // FIXME use emit_small_memset fx.bcx.call_memset(fx.target_config, dst_ptr, val, count); }; - ctlz | ctlz_nonzero, (v arg) { + ctlz | ctlz_nonzero, (c arg) { + let val = arg.load_scalar(fx); // FIXME trap on `ctlz_nonzero` with zero arg. - let res = fx.bcx.ins().clz(arg); - let res = CValue::by_val(res, fx.layout_of(T)); + let res = fx.bcx.ins().clz(val); + let res = CValue::by_val(res, arg.layout()); ret.write_cvalue(fx, res); }; - cttz | cttz_nonzero, (v arg) { + cttz | cttz_nonzero, (c arg) { + let val = arg.load_scalar(fx); // FIXME trap on `cttz_nonzero` with zero arg. - let res = fx.bcx.ins().ctz(arg); - let res = CValue::by_val(res, fx.layout_of(T)); + let res = fx.bcx.ins().ctz(val); + let res = CValue::by_val(res, arg.layout()); ret.write_cvalue(fx, res); }; - ctpop, (v arg) { - let res = fx.bcx.ins().popcnt(arg); - let res = CValue::by_val(res, fx.layout_of(T)); + ctpop, (c arg) { + let val = arg.load_scalar(fx); + let res = fx.bcx.ins().popcnt(val); + let res = CValue::by_val(res, arg.layout()); ret.write_cvalue(fx, res); }; - bitreverse, (v arg) { - let res = fx.bcx.ins().bitrev(arg); - let res = CValue::by_val(res, fx.layout_of(T)); + bitreverse, (c arg) { + let val = arg.load_scalar(fx); + let res = fx.bcx.ins().bitrev(val); + let res = CValue::by_val(res, arg.layout()); ret.write_cvalue(fx, res); }; - bswap, (v arg) { + bswap, (c arg) { // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift fn swap(bcx: &mut FunctionBuilder<'_>, v: Value) -> Value { match bcx.func.dfg.value_type(v) { @@ -773,16 +647,17 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ty => unreachable!("bswap {}", ty), } } - let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T)); + let val = arg.load_scalar(fx); + let res = CValue::by_val(swap(&mut fx.bcx, val), arg.layout()); ret.write_cvalue(fx, res); }; - assert_inhabited | assert_zero_valid | assert_uninit_valid, () { - let layout = fx.layout_of(T); + assert_inhabited | assert_zero_valid | assert_uninit_valid, () { + let layout = fx.layout_of(substs.type_at(0)); if layout.abi.is_uninhabited() { with_no_trimmed_paths!({ crate::base::codegen_panic( fx, - &format!("attempted to instantiate uninhabited type `{}`", T), + &format!("attempted to instantiate uninhabited type `{}`", layout.ty), span, ) }); @@ -793,7 +668,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( with_no_trimmed_paths!({ crate::base::codegen_panic( fx, - &format!("attempted to zero-initialize type `{}`, which is invalid", T), + &format!("attempted to zero-initialize type `{}`, which is invalid", layout.ty), span, ); }); @@ -804,7 +679,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( with_no_trimmed_paths!({ crate::base::codegen_panic( fx, - &format!("attempted to leave type `{}` uninitialized, which is invalid", T), + &format!("attempted to leave type `{}` uninitialized, which is invalid", layout.ty), span, ) }); @@ -838,10 +713,11 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ret.write_cvalue(fx, val); }; - ptr_offset_from, (v ptr, v base) { + ptr_offset_from, (v ptr, v base) { + let ty = substs.type_at(0); let isize_layout = fx.layout_of(fx.tcx.types.isize); - let pointee_size: u64 = fx.layout_of(T).size.bytes(); + let pointee_size: u64 = fx.layout_of(ty).size.bytes(); let diff = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. let val = CValue::by_val(fx.bcx.ins().sdiv_imm(diff, pointee_size as i64), isize_layout); @@ -870,17 +746,31 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( // FIXME use a compiler fence once Cranelift supports it fx.bcx.ins().fence(); }; - _ if intrinsic.as_str().starts_with("atomic_load"), (v ptr) { - validate_atomic_type!(fx, intrinsic, span, T); - let ty = fx.clif_type(T).unwrap(); + _ if intrinsic.as_str().starts_with("atomic_load"), (v ptr) { + let ty = substs.type_at(0); + match ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, ty); + return; + } + } + let clif_ty = fx.clif_type(ty).unwrap(); - let val = fx.bcx.ins().atomic_load(ty, MemFlags::trusted(), ptr); + let val = fx.bcx.ins().atomic_load(clif_ty, MemFlags::trusted(), ptr); - let val = CValue::by_val(val, fx.layout_of(T)); + let val = CValue::by_val(val, fx.layout_of(ty)); ret.write_cvalue(fx, val); }; _ if intrinsic.as_str().starts_with("atomic_store"), (v ptr, c val) { - validate_atomic_type!(fx, intrinsic, span, val.layout().ty); + let ty = substs.type_at(0); + match ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, ty); + return; + } + } let val = val.load_scalar(fx); @@ -888,7 +778,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_xchg"), (v ptr, c new) { let layout = new.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let new = new.load_scalar(fx); @@ -900,7 +796,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_cxchg"), (v ptr, c test_old, c new) { // both atomic_cxchg_* and atomic_cxchgweak_* let layout = new.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let test_old = test_old.load_scalar(fx); let new = new.load_scalar(fx); @@ -914,7 +816,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( _ if intrinsic.as_str().starts_with("atomic_xadd"), (v ptr, c amount) { let layout = amount.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let amount = amount.load_scalar(fx); @@ -926,7 +834,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_xsub"), (v ptr, c amount) { let layout = amount.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let amount = amount.load_scalar(fx); @@ -938,7 +852,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_and"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -950,7 +870,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_or"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -962,7 +888,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_xor"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -974,7 +906,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_nand"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -986,7 +924,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_max"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -998,7 +942,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_umax"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -1010,7 +960,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_min"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -1022,7 +978,13 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.as_str().starts_with("atomic_umin"), (v ptr, c src) { let layout = src.layout(); - validate_atomic_type!(fx, intrinsic, span, layout.ty); + match layout.ty.kind() { + ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} + _ => { + report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + return; + } + } let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); @@ -1107,18 +1069,14 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ret.write_cvalue(fx, CValue::by_val(res, ret.layout())); }; - raw_eq, (v lhs_ref, v rhs_ref) { - fn type_by_size(size: Size) -> Option { - Type::int(size.bits().try_into().ok()?) - } - - let size = fx.layout_of(T).layout.size; + raw_eq, (v lhs_ref, v rhs_ref) { + let size = fx.layout_of(substs.type_at(0)).layout.size; // FIXME add and use emit_small_memcmp let is_eq_value = if size == Size::ZERO { // No bytes means they're trivially equal fx.bcx.ins().iconst(types::I8, 1) - } else if let Some(clty) = type_by_size(size) { + } else if let Some(clty) = size.bits().try_into().ok().and_then(Type::int) { // Can't use `trusted` for these loads; they could be unaligned. let mut flags = MemFlags::new(); flags.set_notrap(); @@ -1141,6 +1099,16 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout())); }; + const_allocate, (c _size, c _align) { + // returns a null pointer at runtime. + let null = fx.bcx.ins().iconst(fx.pointer_type, 0); + ret.write_cvalue(fx, CValue::by_val(null, ret.layout())); + }; + + const_deallocate, (c _ptr, c _size, c _align) { + // nop at runtime. + }; + black_box, (c a) { // FIXME implement black_box semantics ret.write_cvalue(fx, a); diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 6c0631d9ecb..4153bc08e8a 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -1,71 +1,116 @@ //! Codegen `extern "platform-intrinsic"` intrinsics. +use rustc_middle::ty::subst::SubstsRef; +use rustc_span::Symbol; + use super::*; use crate::prelude::*; +fn report_simd_type_validation_error( + fx: &mut FunctionCx<'_, '_, '_>, + intrinsic: Symbol, + span: Span, + ty: Ty<'_>, +) { + fx.tcx.sess.span_err(span, &format!("invalid monomorphization of `{}` intrinsic: expected SIMD input type, found non-SIMD `{}`", intrinsic, ty)); + // Prevent verifier error + crate::trap::trap_unreachable(fx, "compilation should not have succeeded"); +} + pub(super) fn codegen_simd_intrinsic_call<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, - instance: Instance<'tcx>, + intrinsic: Symbol, + _substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], ret: CPlace<'tcx>, span: Span, ) { - let def_id = instance.def_id(); - let substs = instance.substs; - - let intrinsic = fx.tcx.item_name(def_id); - intrinsic_match! { - fx, intrinsic, substs, args, + fx, intrinsic, args, _ => { fx.tcx.sess.span_fatal(span, &format!("Unknown SIMD intrinsic {}", intrinsic)); }; simd_cast, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, lane_layout, ret_lane_layout, lane| { - let ret_lane_ty = fx.clif_type(ret_lane_layout.ty).unwrap(); + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } - let from_signed = type_sign(lane_layout.ty); - let to_signed = type_sign(ret_lane_layout.ty); + simd_for_each_lane(fx, a, ret, &|fx, lane_ty, ret_lane_ty, lane| { + let ret_lane_clif_ty = fx.clif_type(ret_lane_ty).unwrap(); - let ret_lane = clif_int_or_float_cast(fx, lane, from_signed, ret_lane_ty, to_signed); - CValue::by_val(ret_lane, ret_lane_layout) + let from_signed = type_sign(lane_ty); + let to_signed = type_sign(ret_lane_ty); + + clif_int_or_float_cast(fx, lane, from_signed, ret_lane_clif_ty, to_signed) }); }; - simd_eq, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!(fx, Equal|Equal(x, y) -> ret); - }; - simd_ne, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!(fx, NotEqual|NotEqual(x, y) -> ret); - }; - simd_lt, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!(fx, UnsignedLessThan|SignedLessThan|LessThan(x, y) -> ret); - }; - simd_le, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!(fx, UnsignedLessThanOrEqual|SignedLessThanOrEqual|LessThanOrEqual(x, y) -> ret); - }; - simd_gt, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!(fx, UnsignedGreaterThan|SignedGreaterThan|GreaterThan(x, y) -> ret); - }; - simd_ge, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_cmp!( - fx, - UnsignedGreaterThanOrEqual|SignedGreaterThanOrEqual|GreaterThanOrEqual - (x, y) -> ret - ); + simd_eq | simd_ne | simd_lt | simd_le | simd_gt | simd_ge, (c x, c y) { + if !x.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty); + return; + } + + // FIXME use vector instructions when possible + simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, res_lane_ty, x_lane, y_lane| { + let res_lane = match (lane_ty.kind(), intrinsic) { + (ty::Uint(_), sym::simd_eq) => fx.bcx.ins().icmp(IntCC::Equal, x_lane, y_lane), + (ty::Uint(_), sym::simd_ne) => fx.bcx.ins().icmp(IntCC::NotEqual, x_lane, y_lane), + (ty::Uint(_), sym::simd_lt) => { + fx.bcx.ins().icmp(IntCC::UnsignedLessThan, x_lane, y_lane) + } + (ty::Uint(_), sym::simd_le) => { + fx.bcx.ins().icmp(IntCC::UnsignedLessThanOrEqual, x_lane, y_lane) + } + (ty::Uint(_), sym::simd_gt) => { + fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, x_lane, y_lane) + } + (ty::Uint(_), sym::simd_ge) => { + fx.bcx.ins().icmp(IntCC::UnsignedGreaterThanOrEqual, x_lane, y_lane) + } + + (ty::Int(_), sym::simd_eq) => fx.bcx.ins().icmp(IntCC::Equal, x_lane, y_lane), + (ty::Int(_), sym::simd_ne) => fx.bcx.ins().icmp(IntCC::NotEqual, x_lane, y_lane), + (ty::Int(_), sym::simd_lt) => fx.bcx.ins().icmp(IntCC::SignedLessThan, x_lane, y_lane), + (ty::Int(_), sym::simd_le) => { + fx.bcx.ins().icmp(IntCC::SignedLessThanOrEqual, x_lane, y_lane) + } + (ty::Int(_), sym::simd_gt) => { + fx.bcx.ins().icmp(IntCC::SignedGreaterThan, x_lane, y_lane) + } + (ty::Int(_), sym::simd_ge) => { + fx.bcx.ins().icmp(IntCC::SignedGreaterThanOrEqual, x_lane, y_lane) + } + + (ty::Float(_), sym::simd_eq) => fx.bcx.ins().fcmp(FloatCC::Equal, x_lane, y_lane), + (ty::Float(_), sym::simd_ne) => fx.bcx.ins().fcmp(FloatCC::NotEqual, x_lane, y_lane), + (ty::Float(_), sym::simd_lt) => fx.bcx.ins().fcmp(FloatCC::LessThan, x_lane, y_lane), + (ty::Float(_), sym::simd_le) => { + fx.bcx.ins().fcmp(FloatCC::LessThanOrEqual, x_lane, y_lane) + } + (ty::Float(_), sym::simd_gt) => fx.bcx.ins().fcmp(FloatCC::GreaterThan, x_lane, y_lane), + (ty::Float(_), sym::simd_ge) => { + fx.bcx.ins().fcmp(FloatCC::GreaterThanOrEqual, x_lane, y_lane) + } + + _ => unreachable!(), + }; + + let ty = fx.clif_type(res_lane_ty).unwrap(); + + let res_lane = fx.bcx.ins().bint(ty, res_lane); + fx.bcx.ins().ineg(res_lane) + }); }; // simd_shuffle32(x: T, y: T, idx: [u32; 32]) -> U _ if intrinsic.as_str().starts_with("simd_shuffle"), (c x, c y, o idx) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); + if !x.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty); + return; + } // If this intrinsic is the older "simd_shuffleN" form, simply parse the integer. // If there is no suffix, use the index array length. @@ -167,7 +212,11 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_extract, (c v, o idx) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + let idx_const = if let Some(idx_const) = crate::constant::mir_operand_get_const_val(fx, idx) { idx_const } else { @@ -195,95 +244,82 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_neg, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, lane_layout, ret_lane_layout, lane| { - let ret_lane = match lane_layout.ty.kind() { + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } + + simd_for_each_lane(fx, a, ret, &|fx, lane_ty, _ret_lane_ty, lane| { + match lane_ty.kind() { ty::Int(_) => fx.bcx.ins().ineg(lane), ty::Float(_) => fx.bcx.ins().fneg(lane), _ => unreachable!(), - }; - CValue::by_val(ret_lane, ret_lane_layout) + } }); }; - simd_fabs, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, ret_lane_layout, lane| { - let ret_lane = fx.bcx.ins().fabs(lane); - CValue::by_val(ret_lane, ret_lane_layout) - }); - }; + simd_add | simd_sub | simd_mul | simd_div | simd_rem + | simd_shl | simd_shr | simd_and | simd_or | simd_xor, (c x, c y) { + if !x.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty); + return; + } - simd_fsqrt, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, ret_lane_layout, lane| { - let ret_lane = fx.bcx.ins().sqrt(lane); - CValue::by_val(ret_lane, ret_lane_layout) - }); - }; + // FIXME use vector instructions when possible + simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, _ret_lane_ty, x_lane, y_lane| match ( + lane_ty.kind(), + intrinsic, + ) { + (ty::Uint(_), sym::simd_add) => fx.bcx.ins().iadd(x_lane, y_lane), + (ty::Uint(_), sym::simd_sub) => fx.bcx.ins().isub(x_lane, y_lane), + (ty::Uint(_), sym::simd_mul) => fx.bcx.ins().imul(x_lane, y_lane), + (ty::Uint(_), sym::simd_div) => fx.bcx.ins().udiv(x_lane, y_lane), + (ty::Uint(_), sym::simd_rem) => fx.bcx.ins().urem(x_lane, y_lane), - simd_add, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_flt_binop!(fx, iadd|fadd(x, y) -> ret); - }; - simd_sub, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_flt_binop!(fx, isub|fsub(x, y) -> ret); - }; - simd_mul, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_flt_binop!(fx, imul|fmul(x, y) -> ret); - }; - simd_div, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_flt_binop!(fx, udiv|sdiv|fdiv(x, y) -> ret); - }; - simd_rem, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_pair_for_each_lane(fx, x, y, ret, |fx, lane_layout, ret_lane_layout, x_lane, y_lane| { - let res_lane = match lane_layout.ty.kind() { - ty::Uint(_) => fx.bcx.ins().urem(x_lane, y_lane), - ty::Int(_) => fx.bcx.ins().srem(x_lane, y_lane), - ty::Float(FloatTy::F32) => fx.lib_call( - "fmodf", - vec![AbiParam::new(types::F32), AbiParam::new(types::F32)], - vec![AbiParam::new(types::F32)], - &[x_lane, y_lane], - )[0], - ty::Float(FloatTy::F64) => fx.lib_call( - "fmod", - vec![AbiParam::new(types::F64), AbiParam::new(types::F64)], - vec![AbiParam::new(types::F64)], - &[x_lane, y_lane], - )[0], - _ => unreachable!("{:?}", lane_layout.ty), - }; - CValue::by_val(res_lane, ret_lane_layout) + (ty::Int(_), sym::simd_add) => fx.bcx.ins().iadd(x_lane, y_lane), + (ty::Int(_), sym::simd_sub) => fx.bcx.ins().isub(x_lane, y_lane), + (ty::Int(_), sym::simd_mul) => fx.bcx.ins().imul(x_lane, y_lane), + (ty::Int(_), sym::simd_div) => fx.bcx.ins().sdiv(x_lane, y_lane), + (ty::Int(_), sym::simd_rem) => fx.bcx.ins().srem(x_lane, y_lane), + + (ty::Float(_), sym::simd_add) => fx.bcx.ins().fadd(x_lane, y_lane), + (ty::Float(_), sym::simd_sub) => fx.bcx.ins().fsub(x_lane, y_lane), + (ty::Float(_), sym::simd_mul) => fx.bcx.ins().fmul(x_lane, y_lane), + (ty::Float(_), sym::simd_div) => fx.bcx.ins().fdiv(x_lane, y_lane), + (ty::Float(FloatTy::F32), sym::simd_rem) => fx.lib_call( + "fmodf", + vec![AbiParam::new(types::F32), AbiParam::new(types::F32)], + vec![AbiParam::new(types::F32)], + &[x_lane, y_lane], + )[0], + (ty::Float(FloatTy::F64), sym::simd_rem) => fx.lib_call( + "fmod", + vec![AbiParam::new(types::F64), AbiParam::new(types::F64)], + vec![AbiParam::new(types::F64)], + &[x_lane, y_lane], + )[0], + + (ty::Uint(_), sym::simd_shl) => fx.bcx.ins().ishl(x_lane, y_lane), + (ty::Uint(_), sym::simd_shr) => fx.bcx.ins().ushr(x_lane, y_lane), + (ty::Uint(_), sym::simd_and) => fx.bcx.ins().band(x_lane, y_lane), + (ty::Uint(_), sym::simd_or) => fx.bcx.ins().bor(x_lane, y_lane), + (ty::Uint(_), sym::simd_xor) => fx.bcx.ins().bxor(x_lane, y_lane), + + (ty::Int(_), sym::simd_shl) => fx.bcx.ins().ishl(x_lane, y_lane), + (ty::Int(_), sym::simd_shr) => fx.bcx.ins().sshr(x_lane, y_lane), + (ty::Int(_), sym::simd_and) => fx.bcx.ins().band(x_lane, y_lane), + (ty::Int(_), sym::simd_or) => fx.bcx.ins().bor(x_lane, y_lane), + (ty::Int(_), sym::simd_xor) => fx.bcx.ins().bxor(x_lane, y_lane), + + _ => unreachable!(), }); }; - simd_shl, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_binop!(fx, ishl(x, y) -> ret); - }; - simd_shr, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_binop!(fx, ushr|sshr(x, y) -> ret); - }; - simd_and, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_binop!(fx, band(x, y) -> ret); - }; - simd_or, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_binop!(fx, bor(x, y) -> ret); - }; - simd_xor, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_int_binop!(fx, bxor(x, y) -> ret); - }; simd_fma, (c a, c b, c c) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } assert_eq!(a.layout(), b.layout()); assert_eq!(a.layout(), c.layout()); let layout = a.layout(); @@ -305,19 +341,34 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( } }; - simd_fmin, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_flt_binop!(fx, fmin(x, y) -> ret); - }; - simd_fmax, (c x, c y) { - validate_simd_type!(fx, intrinsic, span, x.layout().ty); - simd_flt_binop!(fx, fmax(x, y) -> ret); + simd_fmin | simd_fmax, (c x, c y) { + if !x.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, x.layout().ty); + return; + } + + // FIXME use vector instructions when possible + simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, _ret_lane_ty, x_lane, y_lane| { + match lane_ty.kind() { + ty::Float(_) => {}, + _ => unreachable!("{:?}", lane_ty), + } + match intrinsic { + sym::simd_fmin => fx.bcx.ins().fmin(x_lane, y_lane), + sym::simd_fmax => fx.bcx.ins().fmax(x_lane, y_lane), + _ => unreachable!(), + } + }); }; simd_round, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, lane_layout, ret_lane_layout, lane| { - let res_lane = match lane_layout.ty.kind() { + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } + + simd_for_each_lane(fx, a, ret, &|fx, lane_ty, _ret_lane_ty, lane| { + match lane_ty.kind() { ty::Float(FloatTy::F32) => fx.lib_call( "roundf", vec![AbiParam::new(types::F32)], @@ -330,37 +381,41 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( vec![AbiParam::new(types::F64)], &[lane], )[0], - _ => unreachable!("{:?}", lane_layout.ty), - }; - CValue::by_val(res_lane, ret_lane_layout) + _ => unreachable!("{:?}", lane_ty), + } }); }; - simd_ceil, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, ret_lane_layout, lane| { - let ret_lane = fx.bcx.ins().ceil(lane); - CValue::by_val(ret_lane, ret_lane_layout) - }); - }; - simd_floor, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, ret_lane_layout, lane| { - let ret_lane = fx.bcx.ins().floor(lane); - CValue::by_val(ret_lane, ret_lane_layout) - }); - }; - simd_trunc, (c a) { - validate_simd_type!(fx, intrinsic, span, a.layout().ty); - simd_for_each_lane(fx, a, ret, |fx, _lane_layout, ret_lane_layout, lane| { - let ret_lane = fx.bcx.ins().trunc(lane); - CValue::by_val(ret_lane, ret_lane_layout) + + simd_fabs | simd_fsqrt | simd_ceil | simd_floor | simd_trunc, (c a) { + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } + + simd_for_each_lane(fx, a, ret, &|fx, lane_ty, _ret_lane_ty, lane| { + match lane_ty.kind() { + ty::Float(_) => {}, + _ => unreachable!("{:?}", lane_ty), + } + match intrinsic { + sym::simd_fabs => fx.bcx.ins().fabs(lane), + sym::simd_fsqrt => fx.bcx.ins().sqrt(lane), + sym::simd_ceil => fx.bcx.ins().ceil(lane), + sym::simd_floor => fx.bcx.ins().floor(lane), + sym::simd_trunc => fx.bcx.ins().trunc(lane), + _ => unreachable!(), + } }); }; simd_reduce_add_ordered | simd_reduce_add_unordered, (c v, v acc) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, Some(acc), ret, |fx, lane_layout, a, b| { - if lane_layout.ty.is_floating_point() { + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, Some(acc), ret, &|fx, lane_ty, a, b| { + if lane_ty.is_floating_point() { fx.bcx.ins().fadd(a, b) } else { fx.bcx.ins().iadd(a, b) @@ -369,9 +424,13 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_reduce_mul_ordered | simd_reduce_mul_unordered, (c v, v acc) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, Some(acc), ret, |fx, lane_layout, a, b| { - if lane_layout.ty.is_floating_point() { + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, Some(acc), ret, &|fx, lane_ty, a, b| { + if lane_ty.is_floating_point() { fx.bcx.ins().fmul(a, b) } else { fx.bcx.ins().imul(a, b) @@ -380,34 +439,58 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_reduce_all, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce_bool(fx, v, ret, |fx, a, b| fx.bcx.ins().band(a, b)); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce_bool(fx, v, ret, &|fx, a, b| fx.bcx.ins().band(a, b)); }; simd_reduce_any, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce_bool(fx, v, ret, |fx, a, b| fx.bcx.ins().bor(a, b)); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce_bool(fx, v, ret, &|fx, a, b| fx.bcx.ins().bor(a, b)); }; simd_reduce_and, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, None, ret, |fx, _layout, a, b| fx.bcx.ins().band(a, b)); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().band(a, b)); }; simd_reduce_or, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, None, ret, |fx, _layout, a, b| fx.bcx.ins().bor(a, b)); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().bor(a, b)); }; simd_reduce_xor, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, None, ret, |fx, _layout, a, b| fx.bcx.ins().bxor(a, b)); + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| fx.bcx.ins().bxor(a, b)); }; simd_reduce_min, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, None, ret, |fx, layout, a, b| { - let lt = match layout.ty.kind() { + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, None, ret, &|fx, ty, a, b| { + let lt = match ty.kind() { ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedLessThan, a, b), ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedLessThan, a, b), ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::LessThan, a, b), @@ -418,9 +501,13 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_reduce_max, (c v) { - validate_simd_type!(fx, intrinsic, span, v.layout().ty); - simd_reduce(fx, v, None, ret, |fx, layout, a, b| { - let gt = match layout.ty.kind() { + if !v.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, v.layout().ty); + return; + } + + simd_reduce(fx, v, None, ret, &|fx, ty, a, b| { + let gt = match ty.kind() { ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedGreaterThan, a, b), ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, a, b), ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::GreaterThan, a, b), @@ -431,8 +518,14 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }; simd_select, (c m, c a, c b) { - validate_simd_type!(fx, intrinsic, span, m.layout().ty); - validate_simd_type!(fx, intrinsic, span, a.layout().ty); + if !m.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, m.layout().ty); + return; + } + if !a.layout().ty.is_simd() { + report_simd_type_validation_error(fx, intrinsic, span, a.layout().ty); + return; + } assert_eq!(a.layout(), b.layout()); let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx); diff --git a/src/main_shim.rs b/src/main_shim.rs index 8fd1e4f5811..9ce727279c2 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -1,4 +1,3 @@ -use cranelift_codegen::binemit::{NullStackMapSink, NullTrapSink}; use rustc_hir::LangItem; use rustc_middle::ty::subst::GenericArg; use rustc_middle::ty::AssocKind; @@ -152,8 +151,7 @@ pub(crate) fn maybe_create_entry_wrapper( bcx.seal_all_blocks(); bcx.finalize(); } - m.define_function(cmain_func_id, &mut ctx, &mut NullTrapSink {}, &mut NullStackMapSink {}) - .unwrap(); + m.define_function(cmain_func_id, &mut ctx).unwrap(); unwind_context.add_function(cmain_func_id, &ctx, m.isa()); } } diff --git a/src/optimize/mod.rs b/src/optimize/mod.rs index 61033d85a12..d1f89adb3bb 100644 --- a/src/optimize/mod.rs +++ b/src/optimize/mod.rs @@ -15,6 +15,6 @@ pub(crate) fn optimize_function<'tcx>( ) { // FIXME classify optimizations over opt levels once we have more - crate::pretty_clif::write_clif_file(tcx, "preopt", isa, instance, &ctx, &*clif_comments); + crate::pretty_clif::write_clif_file(tcx, "preopt", isa, instance, &ctx.func, &*clif_comments); crate::base::verify_func(tcx, &*clif_comments, &ctx.func); } diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index 4dffb89e105..ca7116b887d 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -234,7 +234,7 @@ pub(crate) fn write_clif_file<'tcx>( postfix: &str, isa: &dyn cranelift_codegen::isa::TargetIsa, instance: Instance<'tcx>, - context: &cranelift_codegen::Context, + func: &cranelift_codegen::ir::Function, mut clif_comments: &CommentWriter, ) { write_ir_file( @@ -242,12 +242,8 @@ pub(crate) fn write_clif_file<'tcx>( || format!("{}.{}.clif", tcx.symbol_name(instance).name, postfix), |file| { let mut clif = String::new(); - cranelift_codegen::write::decorate_function( - &mut clif_comments, - &mut clif, - &context.func, - ) - .unwrap(); + cranelift_codegen::write::decorate_function(&mut clif_comments, &mut clif, func) + .unwrap(); for flag in isa.flags().iter() { writeln!(file, "set {}", flag)?; diff --git a/y.rs b/y.rs index 98b114de910..18528d54297 100755 --- a/y.rs +++ b/y.rs @@ -23,136 +23,9 @@ exec ${0/.rs/.bin} $@ //! //! The name `y.rs` was chosen to not conflict with rustc's `x.py`. -use std::env; -use std::path::PathBuf; -use std::process; - -#[path = "build_system/build_backend.rs"] -mod build_backend; -#[path = "build_system/build_sysroot.rs"] -mod build_sysroot; -#[path = "build_system/config.rs"] -mod config; -#[path = "build_system/prepare.rs"] -mod prepare; -#[path = "build_system/rustc_info.rs"] -mod rustc_info; -#[path = "build_system/utils.rs"] -mod utils; - -fn usage() { - eprintln!("Usage:"); - eprintln!(" ./y.rs prepare"); - eprintln!( - " ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" - ); -} - -macro_rules! arg_error { - ($($err:tt)*) => {{ - eprintln!($($err)*); - usage(); - std::process::exit(1); - }}; -} - -enum Command { - Build, -} - -#[derive(Copy, Clone)] -enum SysrootKind { - None, - Clif, - Llvm, -} +#[path = "build_system/mod.rs"] +mod build_system; fn main() { - env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); - env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); - // The target dir is expected in the default location. Guard against the user changing it. - env::set_var("CARGO_TARGET_DIR", "target"); - - let mut args = env::args().skip(1); - let command = match args.next().as_deref() { - Some("prepare") => { - if args.next().is_some() { - arg_error!("./x.rs prepare doesn't expect arguments"); - } - prepare::prepare(); - process::exit(0); - } - Some("build") => Command::Build, - Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), - Some(command) => arg_error!("Unknown command {}", command), - None => { - usage(); - process::exit(0); - } - }; - - let mut target_dir = PathBuf::from("build"); - let mut channel = "release"; - let mut sysroot_kind = SysrootKind::Clif; - let mut use_unstable_features = true; - while let Some(arg) = args.next().as_deref() { - match arg { - "--target-dir" => { - target_dir = PathBuf::from(args.next().unwrap_or_else(|| { - arg_error!("--target-dir requires argument"); - })) - } - "--debug" => channel = "debug", - "--sysroot" => { - sysroot_kind = match args.next().as_deref() { - Some("none") => SysrootKind::None, - Some("clif") => SysrootKind::Clif, - Some("llvm") => SysrootKind::Llvm, - Some(arg) => arg_error!("Unknown sysroot kind {}", arg), - None => arg_error!("--sysroot requires argument"), - } - } - "--no-unstable-features" => use_unstable_features = false, - flag if flag.starts_with("-") => arg_error!("Unknown flag {}", flag), - arg => arg_error!("Unexpected argument {}", arg), - } - } - - let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { - host_triple - } else if let Some(host_triple) = crate::config::get_value("host") { - host_triple - } else { - rustc_info::get_host_triple() - }; - let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") { - if target_triple != "" { - target_triple - } else { - host_triple.clone() // Empty target triple can happen on GHA - } - } else if let Some(target_triple) = crate::config::get_value("target") { - target_triple - } else { - host_triple.clone() - }; - - if target_triple.ends_with("-msvc") { - eprintln!("The MSVC toolchain is not yet supported by rustc_codegen_cranelift."); - eprintln!("Switch to the MinGW toolchain for Windows support."); - eprintln!("Hint: You can use `rustup set default-host x86_64-pc-windows-gnu` to"); - eprintln!("set the global default target to MinGW"); - process::exit(1); - } - - let cg_clif_build_dir = - build_backend::build_backend(channel, &host_triple, use_unstable_features); - build_sysroot::build_sysroot( - channel, - sysroot_kind, - &target_dir, - cg_clif_build_dir, - &host_triple, - &target_triple, - ); + build_system::main(); }