From 2b8f2acd5f0f450fb3d9320a3295f5b86dbd31f1 Mon Sep 17 00:00:00 2001 From: Tyson Nottingham Date: Mon, 25 Jan 2021 12:56:21 -0800 Subject: [PATCH 01/61] Indicate both start and end of pass RSS in time-passes output Previously, only the end of pass RSS was indicated. This could easily lead one to believe that the change in RSS from one pass to the next was attributable to the second pass, when in fact it occurred between the end of the first pass and the start of the second. Also, improve alignment of columns. --- src/bin/cg_clif.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/bin/cg_clif.rs b/src/bin/cg_clif.rs index 58e45b4e9b972..be369b07fddfe 100644 --- a/src/bin/cg_clif.rs +++ b/src/bin/cg_clif.rs @@ -6,7 +6,7 @@ extern crate rustc_interface; extern crate rustc_session; extern crate rustc_target; -use rustc_data_structures::profiling::print_time_passes_entry; +use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry}; use rustc_interface::interface; use rustc_session::config::ErrorOutputType; use rustc_session::early_error; @@ -39,7 +39,8 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks { } fn main() { - let start = std::time::Instant::now(); + let start_time = std::time::Instant::now(); + let start_rss = get_resident_set_size(); rustc_driver::init_rustc_env_logger(); let mut callbacks = CraneliftPassesCallbacks::default(); rustc_driver::install_ice_hook(); @@ -61,7 +62,11 @@ fn main() { }))); run_compiler.run() }); - // The extra `\t` is necessary to align this label with the others. - print_time_passes_entry(callbacks.time_passes, "\ttotal", start.elapsed()); + + if callbacks.time_passes { + let end_rss = get_resident_set_size(); + print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss); + } + std::process::exit(exit_code) } From a75f9bc3be1d9488de2d13e4afbf724ffadfc70f Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 1 Feb 2021 10:11:46 +0100 Subject: [PATCH 02/61] Merge commit 'd556c56f792756dd7cfec742b9f2e07612dc10f4' into sync_cg_clif-2021-02-01 --- .github/workflows/main.yml | 5 +- .gitignore | 1 + .vscode/settings.json | 2 +- Cargo.lock | 89 ++-- Cargo.toml | 4 +- Readme.md | 7 +- build.sh | 54 ++- build_sysroot/Cargo.lock | 16 +- build_sysroot/Cargo.toml | 3 +- build_sysroot/build_sysroot.sh | 5 +- build_sysroot/prepare_sysroot_src.sh | 9 +- clean_all.sh | 2 +- ...builtins-Remove-rotate_left-from-Int.patch | 35 ++ example/alloc_example.rs | 3 +- example/mini_core.rs | 4 +- example/mod_bench.rs | 3 +- ...022-core-Disable-not-compiling-tests.patch | 16 + rust-toolchain | 2 +- scripts/config.sh | 30 +- scripts/ext_config.sh | 27 ++ scripts/tests.sh | 2 +- src/abi/comments.rs | 26 +- src/abi/mod.rs | 397 ++++++----------- src/abi/pass_mode.rs | 421 ++++++++++++------ src/abi/returning.rs | 194 ++++++-- src/analyze.rs | 11 +- src/base.rs | 15 +- src/bin/cg_clif_build_sysroot.rs | 5 +- src/codegen_i128.rs | 62 +-- src/common.rs | 57 ++- src/driver/aot.rs | 7 - src/driver/jit.rs | 12 +- src/driver/mod.rs | 9 +- src/lib.rs | 33 +- src/main_shim.rs | 4 +- src/num.rs | 1 - src/pretty_clif.rs | 9 +- src/value_and_place.rs | 125 +++--- test.sh | 4 +- 39 files changed, 993 insertions(+), 718 deletions(-) create mode 100644 crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch create mode 100644 scripts/ext_config.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e6d3375fb1bab..20c58423a0c50 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,6 +12,9 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest] + env: + - BACKEND: "" + - BACKEND: --oldbe steps: - uses: actions/checkout@v2 @@ -51,7 +54,7 @@ jobs: export COMPILE_RUNS=2 export RUN_RUNS=2 - ./test.sh + ./test.sh $BACKEND - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz build diff --git a/.gitignore b/.gitignore index 18196bce00945..b241bef9d1e7f 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ perf.data.old *.string* /build /build_sysroot/sysroot_src +/build_sysroot/compiler-builtins /rust /rand /regex diff --git a/.vscode/settings.json b/.vscode/settings.json index 7618251acd5c2..19ea41563dfd6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,7 +1,7 @@ { // source for rustc_* is not included in the rust-src component; disable the errors about this "rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate"], - "rust-analyzer.assist.importMergeBehaviour": "last", + "rust-analyzer.assist.importMergeBehavior": "last", "rust-analyzer.cargo.loadOutDirsFromCheck": true, "rust-analyzer.linkedProjects": [ "./Cargo.toml", diff --git a/Cargo.lock b/Cargo.lock index 0382835269d1f..5495cfa5eaa0d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,9 +2,9 @@ # It is not intended for manual editing. [[package]] name = "anyhow" -version = "1.0.34" +version = "1.0.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf8dcb5b4bbaa28653b647d8c77bd4ed40183b48882e130c1f1ffb73de069fd7" +checksum = "afddf7f520a80dbf76e6f50a35bca42a2331ef227a28b3b6dc5c2e2338d114b1" [[package]] name = "ar" @@ -25,15 +25,15 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "byteorder" -version = "1.3.4" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" +checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" [[package]] name = "cc" -version = "1.0.62" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1770ced377336a88a67c473594ccc14eca6f4559217c34f64aac8f83d641b40" +checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" [[package]] name = "cfg-if" @@ -49,16 +49,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "byteorder", "cranelift-bforest", @@ -75,8 +75,8 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -84,18 +84,18 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" [[package]] name = "cranelift-entity" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" [[package]] name = "cranelift-frontend" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "cranelift-codegen", "log", @@ -105,8 +105,8 @@ dependencies = [ [[package]] name = "cranelift-jit" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "anyhow", "cranelift-codegen", @@ -123,8 +123,8 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "anyhow", "cranelift-codegen", @@ -135,8 +135,8 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "cranelift-codegen", "raw-cpuid", @@ -145,8 +145,8 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.68.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#8f7f8ee0b4c5007ace6de29b45505c360450b1bb" +version = "0.69.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" dependencies = [ "anyhow", "cranelift-codegen", @@ -209,9 +209,9 @@ checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" [[package]] name = "indexmap" -version = "1.6.0" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55e2e4c765aa53a0424761bf9f41aa7a6ac1efa87238f59560640e27fca028f2" +checksum = "4fb1fa934250de4de8aef298d81c729a7d33d8c239daa3a7575e6b92bfc7313b" dependencies = [ "autocfg", "hashbrown", @@ -219,15 +219,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.80" +version = "0.2.82" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" +checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" [[package]] name = "libloading" -version = "0.6.5" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1090080fe06ec2648d0da3881d9453d97e71a45f00eb179af7fdd7e3f686fdb0" +checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" dependencies = [ "cfg-if 1.0.0", "winapi", @@ -235,9 +235,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.11" +version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" +checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" dependencies = [ "cfg-if 0.1.10", ] @@ -272,9 +272,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.7" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" dependencies = [ "proc-macro2", ] @@ -333,6 +333,7 @@ dependencies = [ "indexmap", "libloading", "object", + "smallvec", "target-lexicon", ] @@ -362,15 +363,15 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "smallvec" -version = "1.4.2" +version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252" +checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "syn" -version = "1.0.48" +version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc371affeffc477f42a221a1e4297aedcea33d47d19b61455588bd9d8f6b19ac" +checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" dependencies = [ "proc-macro2", "quote", @@ -385,18 +386,18 @@ checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" [[package]] name = "thiserror" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e9ae34b84616eedaaf1e9dd6026dbe00dcafa92aa0c8077cb69df1fcfe5e53e" +checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.22" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ba20f23e85b10754cd195504aebf6a27e2e6cbe28c17778a0c930724628dd56" +checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 8e1933bb14e7c..3820fce6d1e0d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind"] } +cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind", "x86", "x64"] } cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" } cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" } cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true } @@ -21,6 +21,7 @@ object = { version = "0.22.0", default-features = false, features = ["std", "rea ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "do_not_remove_cg_clif_ranlib" } indexmap = "1.0.2" libloading = { version = "0.6.0", optional = true } +smallvec = "1.6.1" # Uncomment to use local checkout of cranelift #[patch."https://github.com/bytecodealliance/wasmtime/"] @@ -37,6 +38,7 @@ libloading = { version = "0.6.0", optional = true } default = ["jit", "inline_asm"] jit = ["cranelift-jit", "libloading"] inline_asm = [] +oldbe = [] [profile.dev] # By compiling dependencies with optimizations, performing tests gets much faster. diff --git a/Readme.md b/Readme.md index 22d9e00923f00..6fa5eebdc2f3d 100644 --- a/Readme.md +++ b/Readme.md @@ -1,6 +1,4 @@ -# WIP Cranelift codegen backend for rust - -> ⚠⚠⚠ Certain kinds of FFI don't work yet. ⚠⚠⚠ +# Cranelift codegen backend for rust The goal of this project is to create an alternative codegen backend for the rust compiler based on [Cranelift](https://github.com/bytecodealliance/wasmtime/blob/main/cranelift). This has the potential to improve compilation times in debug mode. @@ -103,8 +101,7 @@ function jit_calc() { ## Not yet supported -* Good non-rust abi support ([several problems](https://github.com/bjorn3/rustc_codegen_cranelift/issues/10)) -* Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041) +* Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041)) * On Linux there is support for invoking an external assembler for `global_asm!` and `asm!`. `llvm_asm!` will remain unimplemented forever. `asm!` doesn't yet support reg classes. You have to specify specific registers instead. diff --git a/build.sh b/build.sh index 26041b59cca18..598ce35eceaac 100755 --- a/build.sh +++ b/build.sh @@ -3,23 +3,29 @@ set -e # Settings export CHANNEL="release" -build_sysroot=1 +build_sysroot="clif" target_dir='build' +oldbe='' while [[ $# != 0 ]]; do case $1 in "--debug") export CHANNEL="debug" ;; - "--without-sysroot") - build_sysroot=0 + "--sysroot") + build_sysroot=$2 + shift ;; "--target-dir") target_dir=$2 shift ;; + "--oldbe") + oldbe='--features oldbe' + ;; *) echo "Unknown flag '$1'" - echo "Usage: ./build.sh [--debug] [--without-sysroot] [--target-dir DIR]" + echo "Usage: ./build.sh [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--oldbe]" + exit 1 ;; esac shift @@ -27,23 +33,24 @@ done # Build cg_clif unset CARGO_TARGET_DIR -export RUSTFLAGS="-Zrun_dsymutil=no" unamestr=$(uname) if [[ "$unamestr" == 'Linux' ]]; then export RUSTFLAGS='-Clink-arg=-Wl,-rpath=$ORIGIN/../lib '$RUSTFLAGS elif [[ "$unamestr" == 'Darwin' ]]; then - export RUSTFLAGS='-Clink-arg=-Wl,-rpath,@loader_path/../lib -Zosx-rpath-install-name '$RUSTFLAGS + export RUSTFLAGS='-Csplit-debuginfo=unpacked -Clink-arg=-Wl,-rpath,@loader_path/../lib -Zosx-rpath-install-name '$RUSTFLAGS dylib_ext='dylib' else echo "Unsupported os" exit 1 fi if [[ "$CHANNEL" == "release" ]]; then - cargo build --release + cargo build $oldbe --release else - cargo build + cargo build $oldbe fi +source scripts/ext_config.sh + rm -rf "$target_dir" mkdir "$target_dir" mkdir "$target_dir"/bin "$target_dir"/lib @@ -51,10 +58,29 @@ ln target/$CHANNEL/cg_clif{,_build_sysroot} "$target_dir"/bin ln target/$CHANNEL/*rustc_codegen_cranelift* "$target_dir"/lib ln rust-toolchain scripts/config.sh scripts/cargo.sh "$target_dir" -if [[ "$build_sysroot" == "1" ]]; then - echo "[BUILD] sysroot" - export CG_CLIF_INCR_CACHE_DISABLED=1 - dir=$(pwd) - cd "$target_dir" - time "$dir/build_sysroot/build_sysroot.sh" +mkdir -p "$target_dir/lib/rustlib/$TARGET_TRIPLE/lib/" +if [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then + cp $(rustc --print sysroot)/lib/rustlib/$TARGET_TRIPLE/lib/*.o "$target_dir/lib/rustlib/$TARGET_TRIPLE/lib/" fi + +case "$build_sysroot" in + "none") + ;; + "llvm") + cp -r $(rustc --print sysroot)/lib/rustlib/$TARGET_TRIPLE/lib "$target_dir/lib/rustlib/$TARGET_TRIPLE/" + ;; + "clif") + echo "[BUILD] sysroot" + dir=$(pwd) + cd "$target_dir" + time "$dir/build_sysroot/build_sysroot.sh" + cp lib/rustlib/*/lib/libstd-* lib/ + ;; + *) + echo "Unknown sysroot kind \`$build_sysroot\`." + echo "The allowed values are:" + echo " none A sysroot that doesn't contain the standard library" + echo " llvm Copy the sysroot from rustc compiled by cg_llvm" + echo " clif Build a new sysroot using cg_clif" + exit 1 +esac diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index 990557694ead4..0da9999c17285 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -2,9 +2,9 @@ # It is not intended for manual editing. [[package]] name = "addr2line" -version = "0.14.0" +version = "0.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7c0929d69e78dd9bf5408269919fcbcaeb2e35e5d43e5815517cdc6a8e11a423" +checksum = "a55f82cfe485775d02112886f4169bde0c5894d75e79ead7eafe7e40a25e45f7" dependencies = [ "compiler_builtins", "gimli", @@ -63,9 +63,7 @@ dependencies = [ [[package]] name = "compiler_builtins" -version = "0.1.36" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7cd0782e0a7da7598164153173e5a5d4d9b1da094473c98dce0ff91406112369" +version = "0.1.39" dependencies = [ "rustc-std-workspace-core", ] @@ -130,9 +128,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.17" +version = "0.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5aca5565f760fb5b220e499d72710ed156fdb74e631659e99377d9ebfbd13ae8" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" dependencies = [ "compiler_builtins", "libc", @@ -141,9 +139,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.81" +version = "0.2.84" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1482821306169ec4d07f6aca392a4681f66c75c9918aa49641a2595db64053cb" +checksum = "1cca32fa0182e8c0989459524dc356b8f2b5c10f1b9eb521b7d182c03cf8c5ff" dependencies = [ "rustc-std-workspace-core", ] diff --git a/build_sysroot/Cargo.toml b/build_sysroot/Cargo.toml index 3dbd28c286a24..82516c98af2a2 100644 --- a/build_sysroot/Cargo.toml +++ b/build_sysroot/Cargo.toml @@ -11,12 +11,13 @@ test = { path = "./sysroot_src/library/test" } alloc_system = { path = "./alloc_system" } -compiler_builtins = { version = "=0.1.36", default-features = false } +compiler_builtins = { version = "0.1.39", default-features = false, features = ["no-asm"] } [patch.crates-io] rustc-std-workspace-core = { path = "./sysroot_src/library/rustc-std-workspace-core" } rustc-std-workspace-alloc = { path = "./sysroot_src/library/rustc-std-workspace-alloc" } rustc-std-workspace-std = { path = "./sysroot_src/library/rustc-std-workspace-std" } +compiler_builtins = { path = "./compiler-builtins" } [profile.dev] lto = "off" diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh index d7a72df2eb283..282ce4a582c4b 100755 --- a/build_sysroot/build_sysroot.sh +++ b/build_sysroot/build_sysroot.sh @@ -24,17 +24,16 @@ export CARGO_TARGET_DIR=target # Build libs export RUSTFLAGS="$RUSTFLAGS -Zforce-unstable-if-unmarked -Cpanic=abort" +export __CARGO_DEFAULT_LIB_METADATA="cg_clif" if [[ "$1" != "--debug" ]]; then sysroot_channel='release' # FIXME Enable incremental again once rust-lang/rust#74946 is fixed - # FIXME Enable -Zmir-opt-level=2 again once it doesn't ice anymore - CARGO_INCREMENTAL=0 RUSTFLAGS="$RUSTFLAGS" cargo build --target "$TARGET_TRIPLE" --release + CARGO_INCREMENTAL=0 RUSTFLAGS="$RUSTFLAGS -Zmir-opt-level=2" cargo build --target "$TARGET_TRIPLE" --release else sysroot_channel='debug' cargo build --target "$TARGET_TRIPLE" fi # Copy files to sysroot -mkdir -p "$dir/lib/rustlib/$TARGET_TRIPLE/lib/" ln "target/$TARGET_TRIPLE/$sysroot_channel/deps/"* "$dir/lib/rustlib/$TARGET_TRIPLE/lib/" rm "$dir/lib/rustlib/$TARGET_TRIPLE/lib/"*.{rmeta,d} diff --git a/build_sysroot/prepare_sysroot_src.sh b/build_sysroot/prepare_sysroot_src.sh index 40fbaf646a2f6..d3b87e02ba891 100755 --- a/build_sysroot/prepare_sysroot_src.sh +++ b/build_sysroot/prepare_sysroot_src.sh @@ -29,4 +29,11 @@ git commit --no-gpg-sign -m "Patch $file" done popd -echo "Successfully prepared libcore for building" +git clone https://github.com/rust-lang/compiler-builtins.git || echo "rust-lang/compiler-builtins has already been cloned" +pushd compiler-builtins +git checkout -- . +git checkout 0.1.39 +git apply ../../crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch +popd + +echo "Successfully prepared sysroot source for building" diff --git a/clean_all.sh b/clean_all.sh index 5a69c862d016d..b47efe72bce03 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -1,5 +1,5 @@ #!/bin/bash --verbose set -e -rm -rf target/ build/ build_sysroot/{sysroot_src/,target/} perf.data{,.old} +rm -rf target/ build/ build_sysroot/{sysroot_src/,target/,compiler-builtins/} perf.data{,.old} rm -rf rand/ regex/ simple-raytracer/ diff --git a/crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch b/crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch new file mode 100644 index 0000000000000..e14768910a9ac --- /dev/null +++ b/crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch @@ -0,0 +1,35 @@ +From 7078cca3cb614e1e82da428380b4e16fc3afef46 Mon Sep 17 00:00:00 2001 +From: bjorn3 +Date: Thu, 21 Jan 2021 14:46:36 +0100 +Subject: [PATCH] Remove rotate_left from Int + +--- + src/int/mod.rs | 5 ----- + 1 file changed, 5 deletions(-) + +diff --git a/src/int/mod.rs b/src/int/mod.rs +index 06054c8..3bea17b 100644 +--- a/src/int/mod.rs ++++ b/src/int/mod.rs +@@ -85,7 +85,6 @@ pub trait Int: + fn wrapping_sub(self, other: Self) -> Self; + fn wrapping_shl(self, other: u32) -> Self; + fn wrapping_shr(self, other: u32) -> Self; +- fn rotate_left(self, other: u32) -> Self; + fn overflowing_add(self, other: Self) -> (Self, bool); + fn aborting_div(self, other: Self) -> Self; + fn aborting_rem(self, other: Self) -> Self; +@@ -209,10 +208,6 @@ macro_rules! int_impl_common { + ::wrapping_shr(self, other) + } + +- fn rotate_left(self, other: u32) -> Self { +- ::rotate_left(self, other) +- } +- + fn overflowing_add(self, other: Self) -> (Self, bool) { + ::overflowing_add(self, other) + } +-- +2.26.2.7.g19db9cfb68 + diff --git a/example/alloc_example.rs b/example/alloc_example.rs index dc2ad4c676e80..f59600ebb330c 100644 --- a/example/alloc_example.rs +++ b/example/alloc_example.rs @@ -11,7 +11,8 @@ use alloc_system::System; #[global_allocator] static ALLOC: System = System; -#[link(name = "c")] +#[cfg_attr(unix, link(name = "c"))] +#[cfg_attr(target_env = "msvc", link(name = "msvcrt"))] extern "C" { fn puts(s: *const u8) -> i32; } diff --git a/example/mini_core.rs b/example/mini_core.rs index 10cba99205629..002ec7e2e3d7a 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -532,8 +532,8 @@ pub mod intrinsics { } pub mod libc { - #[cfg_attr(not(windows), link(name = "c"))] - #[cfg_attr(windows, link(name = "msvcrt"))] + #[cfg_attr(unix, link(name = "c"))] + #[cfg_attr(target_env = "msvc", link(name = "msvcrt"))] extern "C" { pub fn puts(s: *const i8) -> i32; pub fn printf(format: *const i8, ...) -> i32; diff --git a/example/mod_bench.rs b/example/mod_bench.rs index bc65221362346..152041aa9ed0b 100644 --- a/example/mod_bench.rs +++ b/example/mod_bench.rs @@ -1,7 +1,8 @@ #![feature(start, box_syntax, core_intrinsics, lang_items)] #![no_std] -#[link(name = "c")] +#[cfg_attr(unix, link(name = "c"))] +#[cfg_attr(target_env = "msvc", link(name = "msvcrt"))] extern {} #[panic_handler] diff --git a/patches/0022-core-Disable-not-compiling-tests.patch b/patches/0022-core-Disable-not-compiling-tests.patch index 8cfffe580a1f0..3eb10069adad6 100644 --- a/patches/0022-core-Disable-not-compiling-tests.patch +++ b/patches/0022-core-Disable-not-compiling-tests.patch @@ -119,5 +119,21 @@ index 6609bc3..241b497 100644 #[test] #[should_panic(expected = "index 0 greater than length of slice")] +diff --git a/library/core/tests/num/ops.rs b/library/core/tests/num/ops.rs +index 9979cc8..d5d1d83 100644 +--- a/library/core/tests/num/ops.rs ++++ b/library/core/tests/num/ops.rs +@@ -238,7 +238,7 @@ macro_rules! test_shift_assign { + } + }; + } +-test_shift!(test_shl_defined, Shl::shl); +-test_shift_assign!(test_shl_assign_defined, ShlAssign::shl_assign); +-test_shift!(test_shr_defined, Shr::shr); +-test_shift_assign!(test_shr_assign_defined, ShrAssign::shr_assign); ++//test_shift!(test_shl_defined, Shl::shl); ++//test_shift_assign!(test_shl_assign_defined, ShlAssign::shl_assign); ++//test_shift!(test_shr_defined, Shr::shr); ++//test_shift_assign!(test_shr_assign_defined, ShrAssign::shr_assign); -- 2.21.0 (Apple Git-122) diff --git a/rust-toolchain b/rust-toolchain index d6ad24bcf26dd..a08f00d19c20f 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2020-12-23 +nightly-2021-01-30 diff --git a/scripts/config.sh b/scripts/config.sh index dea037e2bc002..834708aa9a6fb 100644 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -12,28 +12,6 @@ else exit 1 fi -HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") -TARGET_TRIPLE=$HOST_TRIPLE -#TARGET_TRIPLE="x86_64-pc-windows-gnu" -#TARGET_TRIPLE="aarch64-unknown-linux-gnu" - -linker='' -RUN_WRAPPER='' -export JIT_SUPPORTED=1 -if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then - export JIT_SUPPORTED=0 - if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then - # We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - linker='-Clinker=aarch64-linux-gnu-gcc' - RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu' - elif [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then - # We are cross-compiling for Windows. Run tests in wine. - RUN_WRAPPER='wine' - else - echo "Unknown non-native platform" - fi -fi - if echo "$RUSTC_WRAPPER" | grep sccache; then echo echo -e "\x1b[1;93m=== Warning: Unset RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m" @@ -44,16 +22,14 @@ fi dir=$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd) export RUSTC=$dir"/bin/cg_clif" -export RUSTFLAGS=$linker" "$RUSTFLAGS + export RUSTDOCFLAGS=$linker' -Cpanic=abort -Zpanic-abort-tests '\ '-Zcodegen-backend='$dir'/lib/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$dir # FIXME remove once the atomic shim is gone -if [[ $(uname) == 'Darwin' ]]; then +if [[ "$unamestr" == 'Darwin' ]]; then export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup" fi -export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib" +export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:"$dir"/lib" export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH - -export CG_CLIF_DISPLAY_CG_TIME=1 diff --git a/scripts/ext_config.sh b/scripts/ext_config.sh new file mode 100644 index 0000000000000..7971f620df14b --- /dev/null +++ b/scripts/ext_config.sh @@ -0,0 +1,27 @@ +# Note to people running shellcheck: this file should only be sourced, not executed directly. + +# Various env vars that should only be set for the build system but not for cargo.sh + +set -e + +export CG_CLIF_DISPLAY_CG_TIME=1 +export CG_CLIF_INCR_CACHE_DISABLED=1 + +export HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") +export TARGET_TRIPLE=${TARGET_TRIPLE:-$HOST_TRIPLE} + +export RUN_WRAPPER='' +export JIT_SUPPORTED=1 +if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then + export JIT_SUPPORTED=0 + if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then + # We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. + export RUSTFLAGS='-Clinker=aarch64-linux-gnu-gcc '$RUSTFLAGS + export RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu' + elif [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then + # We are cross-compiling for Windows. Run tests in wine. + export RUN_WRAPPER='wine' + else + echo "Unknown non-native platform" + fi +fi diff --git a/scripts/tests.sh b/scripts/tests.sh index a61774f479ec7..d37b57babe612 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -3,7 +3,7 @@ set -e source build/config.sh -export CG_CLIF_INCR_CACHE_DISABLED=1 +source scripts/ext_config.sh MY_RUSTC="$RUSTC $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2" function no_sysroot_tests() { diff --git a/src/abi/comments.rs b/src/abi/comments.rs index 01073d26e832a..9aab45b62e211 100644 --- a/src/abi/comments.rs +++ b/src/abi/comments.rs @@ -4,10 +4,10 @@ use std::borrow::Cow; use rustc_middle::mir; +use rustc_target::abi::call::PassMode; use cranelift_codegen::entity::EntityRef; -use crate::abi::pass_mode::*; use crate::prelude::*; pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) { @@ -21,9 +21,9 @@ pub(super) fn add_arg_comment<'tcx>( kind: &str, local: Option, local_field: Option, - params: EmptySinglePair, - pass_mode: PassMode, - ty: Ty<'tcx>, + params: &[Value], + arg_abi_mode: PassMode, + arg_layout: TyAndLayout<'tcx>, ) { let local = if let Some(local) = local { Cow::Owned(format!("{:?}", local)) @@ -37,12 +37,20 @@ pub(super) fn add_arg_comment<'tcx>( }; let params = match params { - Empty => Cow::Borrowed("-"), - Single(param) => Cow::Owned(format!("= {:?}", param)), - Pair(param_a, param_b) => Cow::Owned(format!("= {:?}, {:?}", param_a, param_b)), + [] => Cow::Borrowed("-"), + [param] => Cow::Owned(format!("= {:?}", param)), + [param_a, param_b] => Cow::Owned(format!("= {:?},{:?}", param_a, param_b)), + params => Cow::Owned(format!( + "= {}", + params + .iter() + .map(ToString::to_string) + .collect::>() + .join(",") + )), }; - let pass_mode = format!("{:?}", pass_mode); + let pass_mode = format!("{:?}", arg_abi_mode); fx.add_global_comment(format!( "{kind:5}{local:>3}{local_field:<5} {params:10} {pass_mode:36} {ty:?}", kind = kind, @@ -50,7 +58,7 @@ pub(super) fn add_arg_comment<'tcx>( local_field = local_field, params = params, pass_mode = pass_mode, - ty = ty, + ty = arg_layout.ty, )); } diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 76e1987459f87..6a025f2e88ae3 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -6,199 +6,50 @@ mod pass_mode; mod returning; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; +use rustc_middle::ty::layout::FnAbiExt; +use rustc_target::abi::call::{Conv, FnAbi}; use rustc_target::spec::abi::Abi; -use cranelift_codegen::ir::{AbiParam, ArgumentPurpose}; +use cranelift_codegen::ir::AbiParam; +use smallvec::smallvec; use self::pass_mode::*; use crate::prelude::*; pub(crate) use self::returning::{can_return_to_ssa_var, codegen_return}; -// Copied from https://github.com/rust-lang/rust/blob/f52c72948aa1dd718cc1f168d21c91c584c0a662/src/librustc_middle/ty/layout.rs#L2301 -#[rustfmt::skip] -pub(crate) fn fn_sig_for_fn_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> ty::PolyFnSig<'tcx> { - use rustc_middle::ty::subst::Subst; - - // FIXME(davidtwco,eddyb): A `ParamEnv` should be passed through to this function. - let ty = instance.ty(tcx, ty::ParamEnv::reveal_all()); - match *ty.kind() { - ty::FnDef(..) => { - // HACK(davidtwco,eddyb): This is a workaround for polymorphization considering - // parameters unused if they show up in the signature, but not in the `mir::Body` - // (i.e. due to being inside a projection that got normalized, see - // `src/test/ui/polymorphization/normalized_sig_types.rs`), and codegen not keeping - // track of a polymorphization `ParamEnv` to allow normalizing later. - let mut sig = match *ty.kind() { - ty::FnDef(def_id, substs) => tcx - .normalize_erasing_regions(tcx.param_env(def_id), tcx.fn_sig(def_id)) - .subst(tcx, substs), - _ => unreachable!(), - }; - - if let ty::InstanceDef::VtableShim(..) = instance.def { - // Modify `fn(self, ...)` to `fn(self: *mut Self, ...)`. - sig = sig.map_bound(|mut sig| { - let mut inputs_and_output = sig.inputs_and_output.to_vec(); - inputs_and_output[0] = tcx.mk_mut_ptr(inputs_and_output[0]); - sig.inputs_and_output = tcx.intern_type_list(&inputs_and_output); - sig - }); - } - sig - } - ty::Closure(def_id, substs) => { - let sig = substs.as_closure().sig(); - - let env_ty = tcx.closure_env_ty(def_id, substs).unwrap(); - sig.map_bound(|sig| { - tcx.mk_fn_sig( - std::iter::once(env_ty.skip_binder()).chain(sig.inputs().iter().cloned()), - sig.output(), - sig.c_variadic, - sig.unsafety, - sig.abi, - ) - }) - } - ty::Generator(_, substs, _) => { - let sig = substs.as_generator().poly_sig(); - - let env_region = ty::ReLateBound(ty::INNERMOST, ty::BoundRegion { kind: ty::BrEnv }); - let env_ty = tcx.mk_mut_ref(tcx.mk_region(env_region), ty); - - let pin_did = tcx.require_lang_item(rustc_hir::LangItem::Pin, None); - let pin_adt_ref = tcx.adt_def(pin_did); - let pin_substs = tcx.intern_substs(&[env_ty.into()]); - let env_ty = tcx.mk_adt(pin_adt_ref, pin_substs); - - sig.map_bound(|sig| { - let state_did = tcx.require_lang_item(rustc_hir::LangItem::GeneratorState, None); - let state_adt_ref = tcx.adt_def(state_did); - let state_substs = - tcx.intern_substs(&[sig.yield_ty.into(), sig.return_ty.into()]); - let ret_ty = tcx.mk_adt(state_adt_ref, state_substs); - - tcx.mk_fn_sig( - [env_ty, sig.resume_ty].iter(), - &ret_ty, - false, - rustc_hir::Unsafety::Normal, - rustc_target::spec::abi::Abi::Rust, - ) - }) - } - _ => bug!("unexpected type {:?} in Instance::fn_sig", ty), - } -} - -fn clif_sig_from_fn_sig<'tcx>( +fn clif_sig_from_fn_abi<'tcx>( tcx: TyCtxt<'tcx>, triple: &target_lexicon::Triple, - sig: FnSig<'tcx>, - span: Span, - is_vtable_fn: bool, - requires_caller_location: bool, + fn_abi: &FnAbi<'tcx, Ty<'tcx>>, ) -> Signature { - let abi = match sig.abi { - Abi::System => Abi::C, - abi => abi, - }; - let (call_conv, inputs, output): (CallConv, Vec>, Ty<'tcx>) = match abi { - Abi::Rust => ( - CallConv::triple_default(triple), - sig.inputs().to_vec(), - sig.output(), - ), - Abi::C | Abi::Unadjusted => ( - CallConv::triple_default(triple), - sig.inputs().to_vec(), - sig.output(), - ), - Abi::SysV64 => (CallConv::SystemV, sig.inputs().to_vec(), sig.output()), - Abi::RustCall => { - assert_eq!(sig.inputs().len(), 2); - let extra_args = match sig.inputs().last().unwrap().kind() { - ty::Tuple(ref tupled_arguments) => tupled_arguments, - _ => bug!("argument to function with \"rust-call\" ABI is not a tuple"), - }; - let mut inputs: Vec> = vec![sig.inputs()[0]]; - inputs.extend(extra_args.types()); - (CallConv::triple_default(triple), inputs, sig.output()) + let call_conv = match fn_abi.conv { + Conv::Rust | Conv::C => CallConv::triple_default(triple), + Conv::X86_64SysV => CallConv::SystemV, + Conv::X86_64Win64 => CallConv::WindowsFastcall, + Conv::ArmAapcs + | Conv::Msp430Intr + | Conv::PtxKernel + | Conv::X86Fastcall + | Conv::X86Intr + | Conv::X86Stdcall + | Conv::X86ThisCall + | Conv::X86VectorCall + | Conv::AmdGpuKernel + | Conv::AvrInterrupt + | Conv::AvrNonBlockingInterrupt => { + todo!("{:?}", fn_abi.conv) } - Abi::System => unreachable!(), - Abi::RustIntrinsic => ( - CallConv::triple_default(triple), - sig.inputs().to_vec(), - sig.output(), - ), - _ => unimplemented!("unsupported abi {:?}", sig.abi), }; - - let inputs = inputs - .into_iter() - .enumerate() - .map(|(i, ty)| { - let mut layout = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap(); - if i == 0 && is_vtable_fn { - // Virtual calls turn their self param into a thin pointer. - // See https://github.com/rust-lang/rust/blob/37b6a5e5e82497caf5353d9d856e4eb5d14cbe06/src/librustc/ty/layout.rs#L2519-L2572 for more info - layout = tcx - .layout_of(ParamEnv::reveal_all().and(tcx.mk_mut_ptr(tcx.mk_unit()))) - .unwrap(); - } - let pass_mode = get_pass_mode(tcx, layout); - if abi != Abi::Rust && abi != Abi::RustCall && abi != Abi::RustIntrinsic { - match pass_mode { - PassMode::NoPass | PassMode::ByVal(_) => {} - PassMode::ByRef { size: Some(size) } => { - let purpose = ArgumentPurpose::StructArgument(u32::try_from(size.bytes()).expect("struct too big to pass on stack")); - return EmptySinglePair::Single(AbiParam::special(pointer_ty(tcx), purpose)).into_iter(); - } - PassMode::ByValPair(_, _) | PassMode::ByRef { size: None } => { - tcx.sess.span_warn( - span, - &format!( - "Argument of type `{:?}` with pass mode `{:?}` is not yet supported \ - for non-rust abi `{}`. Calling this function may result in a crash.", - layout.ty, - pass_mode, - abi, - ), - ); - } - } - } - pass_mode.get_param_ty(tcx).map(AbiParam::new).into_iter() - }) + let inputs = fn_abi + .args + .iter() + .map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()) .flatten(); - let (mut params, returns): (Vec<_>, Vec<_>) = match get_pass_mode( - tcx, - tcx.layout_of(ParamEnv::reveal_all().and(output)).unwrap(), - ) { - PassMode::NoPass => (inputs.collect(), vec![]), - PassMode::ByVal(ret_ty) => (inputs.collect(), vec![AbiParam::new(ret_ty)]), - PassMode::ByValPair(ret_ty_a, ret_ty_b) => ( - inputs.collect(), - vec![AbiParam::new(ret_ty_a), AbiParam::new(ret_ty_b)], - ), - PassMode::ByRef { size: Some(_) } => { - ( - Some(pointer_ty(tcx)) // First param is place to put return val - .into_iter() - .map(|ty| AbiParam::special(ty, ArgumentPurpose::StructReturn)) - .chain(inputs) - .collect(), - vec![], - ) - } - PassMode::ByRef { size: None } => todo!(), - }; - - if requires_caller_location { - params.push(AbiParam::new(pointer_ty(tcx))); - } + let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx); + // Sometimes the first param is an pointer to the place where the return value needs to be stored. + let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect(); Signature { params, @@ -207,30 +58,17 @@ fn clif_sig_from_fn_sig<'tcx>( } } -pub(crate) fn get_function_name_and_sig<'tcx>( +pub(crate) fn get_function_sig<'tcx>( tcx: TyCtxt<'tcx>, triple: &target_lexicon::Triple, inst: Instance<'tcx>, - support_vararg: bool, -) -> (String, Signature) { +) -> Signature { assert!(!inst.substs.needs_infer()); - let fn_sig = tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_sig_for_fn_abi(tcx, inst)); - if fn_sig.c_variadic && !support_vararg { - tcx.sess.span_fatal( - tcx.def_span(inst.def_id()), - "Variadic function definitions are not yet supported", - ); - } - let sig = clif_sig_from_fn_sig( + clif_sig_from_fn_abi( tcx, triple, - fn_sig, - tcx.def_span(inst.def_id()), - false, - inst.def.requires_caller_location(tcx), - ); - (tcx.symbol_name(inst).name.to_string(), sig) + &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[]), + ) } /// Instance must be monomorphized @@ -239,7 +77,8 @@ pub(crate) fn import_function<'tcx>( module: &mut impl Module, inst: Instance<'tcx>, ) -> FuncId { - let (name, sig) = get_function_name_and_sig(tcx, module.isa().triple(), inst, true); + let name = tcx.symbol_name(inst).name.to_string(); + let sig = get_function_sig(tcx, module.isa().triple(), inst); module .declare_function(&name, Linkage::Import, &sig) .unwrap() @@ -263,13 +102,13 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { pub(crate) fn lib_call( &mut self, name: &str, - input_tys: Vec, - output_tys: Vec, + params: Vec, + returns: Vec, args: &[Value], ) -> &[Value] { let sig = Signature { - params: input_tys.iter().cloned().map(AbiParam::new).collect(), - returns: output_tys.iter().cloned().map(AbiParam::new).collect(), + params, + returns, call_conv: CallConv::triple_default(self.triple()), }; let func_id = self @@ -301,16 +140,18 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { .iter() .map(|arg| { ( - self.clif_type(arg.layout().ty).unwrap(), + AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), arg.load_scalar(self), ) }) .unzip(); let return_layout = self.layout_of(return_ty); let return_tys = if let ty::Tuple(tup) = return_ty.kind() { - tup.types().map(|ty| self.clif_type(ty).unwrap()).collect() + tup.types() + .map(|ty| AbiParam::new(self.clif_type(ty).unwrap())) + .collect() } else { - vec![self.clif_type(return_ty).unwrap()] + vec![AbiParam::new(self.clif_type(return_ty).unwrap())] }; let ret_vals = self.lib_call(name, input_tys, return_tys, &args); match *ret_vals { @@ -352,12 +193,25 @@ pub(crate) fn codegen_fn_prelude<'tcx>( fx: &mut FunctionCx<'_, 'tcx, impl Module>, start_block: Block, ) { + fx.bcx.append_block_params_for_function_params(start_block); + + fx.bcx.switch_to_block(start_block); + fx.bcx.ins().nop(); + let ssa_analyzed = crate::analyze::analyze(fx); #[cfg(debug_assertions)] self::comments::add_args_header_comment(fx); - let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, start_block); + let mut block_params_iter = fx + .bcx + .func + .dfg + .block_params(start_block) + .to_vec() + .into_iter(); + let ret_place = + self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter); assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE); // None means pass_mode == NoPass @@ -366,6 +220,9 @@ pub(crate) fn codegen_fn_prelude<'tcx>( Spread(Vec>>), } + let fn_abi = fx.fn_abi.take().unwrap(); + let mut arg_abis_iter = fn_abi.args.iter(); + let func_params = fx .mir .args_iter() @@ -385,14 +242,18 @@ pub(crate) fn codegen_fn_prelude<'tcx>( }; let mut params = Vec::new(); - for (i, arg_ty) in tupled_arg_tys.types().enumerate() { - let param = cvalue_for_param(fx, start_block, Some(local), Some(i), arg_ty); + for (i, _arg_ty) in tupled_arg_tys.types().enumerate() { + let arg_abi = arg_abis_iter.next().unwrap(); + let param = + cvalue_for_param(fx, Some(local), Some(i), arg_abi, &mut block_params_iter); params.push(param); } (local, ArgKind::Spread(params), arg_ty) } else { - let param = cvalue_for_param(fx, start_block, Some(local), None, arg_ty); + let arg_abi = arg_abis_iter.next().unwrap(); + let param = + cvalue_for_param(fx, Some(local), None, arg_abi, &mut block_params_iter); (local, ArgKind::Normal(param), arg_ty) } }) @@ -401,13 +262,14 @@ pub(crate) fn codegen_fn_prelude<'tcx>( assert!(fx.caller_location.is_none()); if fx.instance.def.requires_caller_location(fx.tcx) { // Store caller location for `#[track_caller]`. - fx.caller_location = Some( - cvalue_for_param(fx, start_block, None, None, fx.tcx.caller_location_ty()).unwrap(), - ); + let arg_abi = arg_abis_iter.next().unwrap(); + fx.caller_location = + Some(cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter).unwrap()); } - fx.bcx.switch_to_block(start_block); - fx.bcx.ins().nop(); + assert!(arg_abis_iter.next().is_none(), "ArgAbi left behind"); + fx.fn_abi = Some(fn_abi); + assert!(block_params_iter.next().is_none(), "arg_value left behind"); #[cfg(debug_assertions)] self::comments::add_locals_header_comment(fx); @@ -533,6 +395,21 @@ pub(crate) fn codegen_terminator_call<'tcx>( None }; + let extra_args = &args[fn_sig.inputs().len()..]; + let extra_args = extra_args + .iter() + .map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx))) + .collect::>(); + let fn_abi = if let Some(instance) = instance { + FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args) + } else { + FnAbi::of_fn_ptr( + &RevealAllLayoutCx(fx.tcx), + fn_ty.fn_sig(fx.tcx), + &extra_args, + ) + }; + let is_cold = instance .map(|inst| { fx.tcx @@ -570,8 +447,8 @@ pub(crate) fn codegen_terminator_call<'tcx>( // | indirect call target // | | the first argument to be passed - // v v v virtual calls are special cased below - let (func_ref, first_arg, is_virtual_call) = match instance { + // v v + let (func_ref, first_arg) = match instance { // Trait object call Some(Instance { def: InstanceDef::Virtual(_, idx), @@ -582,23 +459,19 @@ pub(crate) fn codegen_terminator_call<'tcx>( let nop_inst = fx.bcx.ins().nop(); fx.add_comment( nop_inst, - format!( - "virtual call; self arg pass mode: {:?}", - get_pass_mode(fx.tcx, args[0].layout()) - ), + format!("virtual call; self arg pass mode: {:?}", &fn_abi.args[0],), ); } let (ptr, method) = crate::vtable::get_ptr_and_method_ref(fx, args[0], idx); - (Some(method), Single(ptr), true) + (Some(method), smallvec![ptr]) } // Normal call Some(_) => ( None, args.get(0) - .map(|arg| adjust_arg_for_abi(fx, *arg)) - .unwrap_or(Empty), - false, + .map(|arg| adjust_arg_for_abi(fx, *arg, &fn_abi.args[0])) + .unwrap_or(smallvec![]), ), // Indirect call @@ -612,23 +485,27 @@ pub(crate) fn codegen_terminator_call<'tcx>( ( Some(func), args.get(0) - .map(|arg| adjust_arg_for_abi(fx, *arg)) - .unwrap_or(Empty), - false, + .map(|arg| adjust_arg_for_abi(fx, *arg, &fn_abi.args[0])) + .unwrap_or(smallvec![]), ) } }; let ret_place = destination.map(|(place, _)| place); - let (call_inst, call_args) = - self::returning::codegen_with_call_return_arg(fx, fn_sig, ret_place, |fx, return_ptr| { + let (call_inst, call_args) = self::returning::codegen_with_call_return_arg( + fx, + &fn_abi.ret, + ret_place, + |fx, return_ptr| { + let regular_args_count = args.len(); let mut call_args: Vec = return_ptr .into_iter() .chain(first_arg.into_iter()) .chain( args.into_iter() + .enumerate() .skip(1) - .map(|arg| adjust_arg_for_abi(fx, arg).into_iter()) + .map(|(i, arg)| adjust_arg_for_abi(fx, arg, &fn_abi.args[i]).into_iter()) .flatten(), ) .collect::>(); @@ -639,18 +516,17 @@ pub(crate) fn codegen_terminator_call<'tcx>( { // Pass the caller location for `#[track_caller]`. let caller_location = fx.get_caller_location(span); - call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter()); + call_args.extend( + adjust_arg_for_abi(fx, caller_location, &fn_abi.args[regular_args_count]) + .into_iter(), + ); + assert_eq!(fn_abi.args.len(), regular_args_count + 1); + } else { + assert_eq!(fn_abi.args.len(), regular_args_count); } let call_inst = if let Some(func_ref) = func_ref { - let sig = clif_sig_from_fn_sig( - fx.tcx, - fx.triple(), - fn_sig, - span, - is_virtual_call, - false, // calls through function pointers never pass the caller location - ); + let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi); let sig = fx.bcx.import_signature(sig); fx.bcx.ins().call_indirect(sig, func_ref, &call_args) } else { @@ -660,7 +536,8 @@ pub(crate) fn codegen_terminator_call<'tcx>( }; (call_inst, call_args) - }); + }, + ); // FIXME find a cleaner way to support varargs if fn_sig.c_variadic { @@ -701,37 +578,33 @@ pub(crate) fn codegen_drop<'tcx>( drop_place: CPlace<'tcx>, ) { let ty = drop_place.layout().ty; - let drop_fn = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx); + let drop_instance = Instance::resolve_drop_in_place(fx.tcx, ty).polymorphize(fx.tcx); - if let ty::InstanceDef::DropGlue(_, None) = drop_fn.def { + if let ty::InstanceDef::DropGlue(_, None) = drop_instance.def { // we don't actually need to drop anything } else { - let drop_fn_ty = drop_fn.ty(fx.tcx, ParamEnv::reveal_all()); - let fn_sig = fx.tcx.normalize_erasing_late_bound_regions( - ParamEnv::reveal_all(), - drop_fn_ty.fn_sig(fx.tcx), - ); - assert_eq!(fn_sig.output(), fx.tcx.mk_unit()); - match ty.kind() { ty::Dynamic(..) => { let (ptr, vtable) = drop_place.to_ptr_maybe_unsized(); let ptr = ptr.get_addr(fx); let drop_fn = crate::vtable::drop_fn_of_obj(fx, vtable.unwrap()); - let sig = clif_sig_from_fn_sig( - fx.tcx, - fx.triple(), - fn_sig, - span, - true, - false, // `drop_in_place` is never `#[track_caller]` - ); + // FIXME(eddyb) perhaps move some of this logic into + // `Instance::resolve_drop_in_place`? + let virtual_drop = Instance { + def: ty::InstanceDef::Virtual(drop_instance.def_id(), 0), + substs: drop_instance.substs, + }; + let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), virtual_drop, &[]); + + let sig = clif_sig_from_fn_abi(fx.tcx, fx.triple(), &fn_abi); let sig = fx.bcx.import_signature(sig); fx.bcx.ins().call_indirect(sig, drop_fn, &[ptr]); } _ => { - assert!(!matches!(drop_fn.def, InstanceDef::Virtual(_, _))); + assert!(!matches!(drop_instance.def, InstanceDef::Virtual(_, _))); + + let fn_abi = FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), drop_instance, &[]); let arg_value = drop_place.place_ref( fx, @@ -743,17 +616,19 @@ pub(crate) fn codegen_drop<'tcx>( }, )), ); - let arg_value = adjust_arg_for_abi(fx, arg_value); + let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0]); let mut call_args: Vec = arg_value.into_iter().collect::>(); - if drop_fn.def.requires_caller_location(fx.tcx) { + if drop_instance.def.requires_caller_location(fx.tcx) { // Pass the caller location for `#[track_caller]`. let caller_location = fx.get_caller_location(span); - call_args.extend(adjust_arg_for_abi(fx, caller_location).into_iter()); + call_args.extend( + adjust_arg_for_abi(fx, caller_location, &fn_abi.args[1]).into_iter(), + ); } - let func_ref = fx.get_function_ref(drop_fn); + let func_ref = fx.get_function_ref(drop_instance); fx.bcx.ins().call(func_ref, &call_args); } } diff --git a/src/abi/pass_mode.rs b/src/abi/pass_mode.rs index 8e3682c86c5fb..1202c23dbe7b3 100644 --- a/src/abi/pass_mode.rs +++ b/src/abi/pass_mode.rs @@ -1,140 +1,281 @@ //! Argument passing use crate::prelude::*; +use crate::value_and_place::assert_assignable; -pub(super) use EmptySinglePair::*; +use cranelift_codegen::ir::{ArgumentExtension, ArgumentPurpose}; +use rustc_target::abi::call::{ + ArgAbi, ArgAttributes, ArgExtension as RustcArgExtension, CastTarget, PassMode, Reg, RegKind, +}; +use smallvec::{smallvec, SmallVec}; -#[derive(Copy, Clone, Debug)] -pub(super) enum PassMode { - NoPass, - ByVal(Type), - ByValPair(Type, Type), - ByRef { size: Option }, +pub(super) trait ArgAbiExt<'tcx> { + fn get_abi_param(&self, tcx: TyCtxt<'tcx>) -> SmallVec<[AbiParam; 2]>; + fn get_abi_return(&self, tcx: TyCtxt<'tcx>) -> (Option, Vec); } -#[derive(Copy, Clone, Debug)] -pub(super) enum EmptySinglePair { - Empty, - Single(T), - Pair(T, T), +fn reg_to_abi_param(reg: Reg) -> AbiParam { + let clif_ty = match (reg.kind, reg.size.bytes()) { + (RegKind::Integer, 1) => types::I8, + (RegKind::Integer, 2) => types::I16, + (RegKind::Integer, 4) => types::I32, + (RegKind::Integer, 8) => types::I64, + (RegKind::Integer, 16) => types::I128, + (RegKind::Float, 4) => types::F32, + (RegKind::Float, 8) => types::F64, + (RegKind::Vector, size) => types::I8.by(u16::try_from(size).unwrap()).unwrap(), + _ => unreachable!("{:?}", reg), + }; + AbiParam::new(clif_ty) } -impl EmptySinglePair { - pub(super) fn into_iter(self) -> EmptySinglePairIter { - EmptySinglePairIter(self) - } - - pub(super) fn map(self, mut f: impl FnMut(T) -> U) -> EmptySinglePair { - match self { - Empty => Empty, - Single(v) => Single(f(v)), - Pair(a, b) => Pair(f(a), f(b)), - } +fn apply_arg_attrs_to_abi_param(mut param: AbiParam, arg_attrs: ArgAttributes) -> AbiParam { + match arg_attrs.arg_ext { + RustcArgExtension::None => {} + RustcArgExtension::Zext => param.extension = ArgumentExtension::Uext, + RustcArgExtension::Sext => param.extension = ArgumentExtension::Sext, } + param } -pub(super) struct EmptySinglePairIter(EmptySinglePair); - -impl Iterator for EmptySinglePairIter { - type Item = T; +fn cast_target_to_abi_params(cast: CastTarget) -> SmallVec<[AbiParam; 2]> { + let (rest_count, rem_bytes) = if cast.rest.unit.size.bytes() == 0 { + (0, 0) + } else { + ( + cast.rest.total.bytes() / cast.rest.unit.size.bytes(), + cast.rest.total.bytes() % cast.rest.unit.size.bytes(), + ) + }; - fn next(&mut self) -> Option { - match std::mem::replace(&mut self.0, Empty) { - Empty => None, - Single(v) => Some(v), - Pair(a, b) => { - self.0 = Single(b); - Some(a) - } + if cast.prefix.iter().all(|x| x.is_none()) { + // Simplify to a single unit when there is no prefix and size <= unit size + if cast.rest.total <= cast.rest.unit.size { + let clif_ty = match (cast.rest.unit.kind, cast.rest.unit.size.bytes()) { + (RegKind::Integer, 1) => types::I8, + (RegKind::Integer, 2) => types::I16, + (RegKind::Integer, 3..=4) => types::I32, + (RegKind::Integer, 5..=8) => types::I64, + (RegKind::Integer, 9..=16) => types::I128, + (RegKind::Float, 4) => types::F32, + (RegKind::Float, 8) => types::F64, + (RegKind::Vector, size) => types::I8.by(u16::try_from(size).unwrap()).unwrap(), + _ => unreachable!("{:?}", cast.rest.unit), + }; + return smallvec![AbiParam::new(clif_ty)]; } } -} -impl EmptySinglePair { - pub(super) fn assert_single(self) -> T { - match self { - Single(v) => v, - _ => panic!("Called assert_single on {:?}", self), - } - } + // Create list of fields in the main structure + let mut args = cast + .prefix + .iter() + .flatten() + .map(|&kind| { + reg_to_abi_param(Reg { + kind, + size: cast.prefix_chunk_size, + }) + }) + .chain((0..rest_count).map(|_| reg_to_abi_param(cast.rest.unit))) + .collect::>(); - pub(super) fn assert_pair(self) -> (T, T) { - match self { - Pair(a, b) => (a, b), - _ => panic!("Called assert_pair on {:?}", self), - } + // Append final integer + if rem_bytes != 0 { + // Only integers can be really split further. + assert_eq!(cast.rest.unit.kind, RegKind::Integer); + args.push(reg_to_abi_param(Reg { + kind: RegKind::Integer, + size: Size::from_bytes(rem_bytes), + })); } -} -impl PassMode { - pub(super) fn get_param_ty(self, tcx: TyCtxt<'_>) -> EmptySinglePair { - match self { - PassMode::NoPass => Empty, - PassMode::ByVal(clif_type) => Single(clif_type), - PassMode::ByValPair(a, b) => Pair(a, b), - PassMode::ByRef { size: Some(_) } => Single(pointer_ty(tcx)), - PassMode::ByRef { size: None } => Pair(pointer_ty(tcx), pointer_ty(tcx)), - } - } + args } -pub(super) fn get_pass_mode<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx>) -> PassMode { - if layout.is_zst() { - // WARNING zst arguments must never be passed, as that will break CastKind::ClosureFnPointer - PassMode::NoPass - } else { - match &layout.abi { - Abi::Uninhabited => PassMode::NoPass, - Abi::Scalar(scalar) => PassMode::ByVal(scalar_to_clif_type(tcx, scalar.clone())), - Abi::ScalarPair(a, b) => { - let a = scalar_to_clif_type(tcx, a.clone()); - let b = scalar_to_clif_type(tcx, b.clone()); - if a == types::I128 && b == types::I128 { - // Returning (i128, i128) by-val-pair would take 4 regs, while only 3 are - // available on x86_64. Cranelift gets confused when too many return params - // are used. - PassMode::ByRef { - size: Some(layout.size), - } - } else { - PassMode::ByValPair(a, b) +impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { + fn get_abi_param(&self, tcx: TyCtxt<'tcx>) -> SmallVec<[AbiParam; 2]> { + match self.mode { + PassMode::Ignore => smallvec![], + PassMode::Direct(attrs) => match &self.layout.abi { + Abi::Scalar(scalar) => { + smallvec![apply_arg_attrs_to_abi_param( + AbiParam::new(scalar_to_clif_type(tcx, scalar.clone())), + attrs + )] } - } - - // FIXME implement Vector Abi in a cg_llvm compatible way - Abi::Vector { .. } => { - if let Some(vector_ty) = crate::intrinsics::clif_vector_type(tcx, layout) { - PassMode::ByVal(vector_ty) + Abi::Vector { .. } => { + let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap(); + smallvec![AbiParam::new(vector_ty)] + } + _ => unreachable!("{:?}", self.layout.abi), + }, + PassMode::Pair(attrs_a, attrs_b) => match &self.layout.abi { + Abi::ScalarPair(a, b) => { + let a = scalar_to_clif_type(tcx, a.clone()); + let b = scalar_to_clif_type(tcx, b.clone()); + smallvec![ + apply_arg_attrs_to_abi_param(AbiParam::new(a), attrs_a), + apply_arg_attrs_to_abi_param(AbiParam::new(b), attrs_b), + ] + } + _ => unreachable!("{:?}", self.layout.abi), + }, + PassMode::Cast(cast) => cast_target_to_abi_params(cast), + PassMode::Indirect { + attrs, + extra_attrs: None, + on_stack, + } => { + if on_stack { + let size = u32::try_from(self.layout.size.bytes()).unwrap(); + smallvec![apply_arg_attrs_to_abi_param( + AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructArgument(size),), + attrs + )] } else { - PassMode::ByRef { - size: Some(layout.size), - } + smallvec![apply_arg_attrs_to_abi_param( + AbiParam::new(pointer_ty(tcx)), + attrs + )] } } + PassMode::Indirect { + attrs, + extra_attrs: Some(extra_attrs), + on_stack, + } => { + assert!(!on_stack); + smallvec![ + apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs), + apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), extra_attrs), + ] + } + } + } - Abi::Aggregate { sized: true } => PassMode::ByRef { - size: Some(layout.size), + fn get_abi_return(&self, tcx: TyCtxt<'tcx>) -> (Option, Vec) { + match self.mode { + PassMode::Ignore => (None, vec![]), + PassMode::Direct(_) => match &self.layout.abi { + Abi::Scalar(scalar) => ( + None, + vec![AbiParam::new(scalar_to_clif_type(tcx, scalar.clone()))], + ), + Abi::Vector { .. } => { + let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap(); + (None, vec![AbiParam::new(vector_ty)]) + } + _ => unreachable!("{:?}", self.layout.abi), + }, + PassMode::Pair(_, _) => match &self.layout.abi { + Abi::ScalarPair(a, b) => { + let a = scalar_to_clif_type(tcx, a.clone()); + let b = scalar_to_clif_type(tcx, b.clone()); + (None, vec![AbiParam::new(a), AbiParam::new(b)]) + } + _ => unreachable!("{:?}", self.layout.abi), }, - Abi::Aggregate { sized: false } => PassMode::ByRef { size: None }, + PassMode::Cast(cast) => (None, cast_target_to_abi_params(cast).into_iter().collect()), + PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack, + } => { + assert!(!on_stack); + ( + Some(AbiParam::special( + pointer_ty(tcx), + ArgumentPurpose::StructReturn, + )), + vec![], + ) + } + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => unreachable!("unsized return value"), } } } +pub(super) fn to_casted_value<'tcx>( + fx: &mut FunctionCx<'_, 'tcx, impl Module>, + arg: CValue<'tcx>, + cast: CastTarget, +) -> SmallVec<[Value; 2]> { + let (ptr, meta) = arg.force_stack(fx); + assert!(meta.is_none()); + let mut offset = 0; + cast_target_to_abi_params(cast) + .into_iter() + .map(|param| { + let val = ptr + .offset_i64(fx, offset) + .load(fx, param.value_type, MemFlags::new()); + offset += i64::from(param.value_type.bytes()); + val + }) + .collect() +} + +pub(super) fn from_casted_value<'tcx>( + fx: &mut FunctionCx<'_, 'tcx, impl Module>, + block_params: &[Value], + layout: TyAndLayout<'tcx>, + cast: CastTarget, +) -> CValue<'tcx> { + let abi_params = cast_target_to_abi_params(cast); + let abi_param_size: u32 = abi_params + .iter() + .map(|param| param.value_type.bytes()) + .sum(); + let layout_size = u32::try_from(layout.size.bytes()).unwrap(); + let stack_slot = fx.bcx.create_stack_slot(StackSlotData { + kind: StackSlotKind::ExplicitSlot, + // FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to + // specify stack slot alignment. + // Stack slot size may be bigger for for example `[u8; 3]` which is packed into an `i32`. + // It may also be smaller for example when the type is a wrapper around an integer with a + // larger alignment than the integer. + size: (std::cmp::max(abi_param_size, layout_size) + 15) / 16 * 16, + offset: None, + }); + let ptr = Pointer::new(fx.bcx.ins().stack_addr(pointer_ty(fx.tcx), stack_slot, 0)); + let mut offset = 0; + let mut block_params_iter = block_params.into_iter().copied(); + for param in abi_params { + let val = ptr.offset_i64(fx, offset).store( + fx, + block_params_iter.next().unwrap(), + MemFlags::new(), + ); + offset += i64::from(param.value_type.bytes()); + val + } + assert_eq!(block_params_iter.next(), None, "Leftover block param"); + CValue::by_ref(ptr, layout) +} + /// Get a set of values to be passed as function arguments. pub(super) fn adjust_arg_for_abi<'tcx>( fx: &mut FunctionCx<'_, 'tcx, impl Module>, arg: CValue<'tcx>, -) -> EmptySinglePair { - match get_pass_mode(fx.tcx, arg.layout()) { - PassMode::NoPass => Empty, - PassMode::ByVal(_) => Single(arg.load_scalar(fx)), - PassMode::ByValPair(_, _) => { + arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, +) -> SmallVec<[Value; 2]> { + assert_assignable(fx, arg.layout().ty, arg_abi.layout.ty); + match arg_abi.mode { + PassMode::Ignore => smallvec![], + PassMode::Direct(_) => smallvec![arg.load_scalar(fx)], + PassMode::Pair(_, _) => { let (a, b) = arg.load_scalar_pair(fx); - Pair(a, b) + smallvec![a, b] } - PassMode::ByRef { size: _ } => match arg.force_stack(fx) { - (ptr, None) => Single(ptr.get_addr(fx)), - (ptr, Some(meta)) => Pair(ptr.get_addr(fx), meta), + PassMode::Cast(cast) => to_casted_value(fx, arg, cast), + PassMode::Indirect { .. } => match arg.force_stack(fx) { + (ptr, None) => smallvec![ptr.get_addr(fx)], + (ptr, Some(meta)) => smallvec![ptr.get_addr(fx), meta], }, } } @@ -143,20 +284,23 @@ pub(super) fn adjust_arg_for_abi<'tcx>( /// as necessary. pub(super) fn cvalue_for_param<'tcx>( fx: &mut FunctionCx<'_, 'tcx, impl Module>, - start_block: Block, #[cfg_attr(not(debug_assertions), allow(unused_variables))] local: Option, #[cfg_attr(not(debug_assertions), allow(unused_variables))] local_field: Option, - arg_ty: Ty<'tcx>, + arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, + block_params_iter: &mut impl Iterator, ) -> Option> { - let layout = fx.layout_of(arg_ty); - let pass_mode = get_pass_mode(fx.tcx, layout); - - if let PassMode::NoPass = pass_mode { - return None; - } - - let clif_types = pass_mode.get_param_ty(fx.tcx); - let block_params = clif_types.map(|t| fx.bcx.append_block_param(start_block, t)); + let block_params = arg_abi + .get_abi_param(fx.tcx) + .into_iter() + .map(|abi_param| { + let block_param = block_params_iter.next().unwrap(); + assert_eq!( + fx.bcx.func.dfg.value_type(block_param), + abi_param.value_type + ); + block_param + }) + .collect::>(); #[cfg(debug_assertions)] crate::abi::comments::add_arg_comment( @@ -164,25 +308,48 @@ pub(super) fn cvalue_for_param<'tcx>( "arg", local, local_field, - block_params, - pass_mode, - arg_ty, + &block_params, + arg_abi.mode, + arg_abi.layout, ); - match pass_mode { - PassMode::NoPass => unreachable!(), - PassMode::ByVal(_) => Some(CValue::by_val(block_params.assert_single(), layout)), - PassMode::ByValPair(_, _) => { - let (a, b) = block_params.assert_pair(); - Some(CValue::by_val_pair(a, b, layout)) + match arg_abi.mode { + PassMode::Ignore => None, + PassMode::Direct(_) => { + assert_eq!(block_params.len(), 1, "{:?}", block_params); + Some(CValue::by_val(block_params[0], arg_abi.layout)) + } + PassMode::Pair(_, _) => { + assert_eq!(block_params.len(), 2, "{:?}", block_params); + Some(CValue::by_val_pair( + block_params[0], + block_params[1], + arg_abi.layout, + )) + } + PassMode::Cast(cast) => Some(from_casted_value(fx, &block_params, arg_abi.layout, cast)), + PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack: _, + } => { + assert_eq!(block_params.len(), 1, "{:?}", block_params); + Some(CValue::by_ref( + Pointer::new(block_params[0]), + arg_abi.layout, + )) } - PassMode::ByRef { size: Some(_) } => Some(CValue::by_ref( - Pointer::new(block_params.assert_single()), - layout, - )), - PassMode::ByRef { size: None } => { - let (ptr, meta) = block_params.assert_pair(); - Some(CValue::by_ref_unsized(Pointer::new(ptr), meta, layout)) + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => { + assert_eq!(block_params.len(), 2, "{:?}", block_params); + Some(CValue::by_ref_unsized( + Pointer::new(block_params[0]), + block_params[1], + arg_abi.layout, + )) } } } diff --git a/src/abi/returning.rs b/src/abi/returning.rs index f6d40c880d094..a382963bf1ed7 100644 --- a/src/abi/returning.rs +++ b/src/abi/returning.rs @@ -1,21 +1,57 @@ //! Return value handling -use crate::abi::pass_mode::*; use crate::prelude::*; -fn return_layout<'a, 'tcx>(fx: &mut FunctionCx<'a, 'tcx, impl Module>) -> TyAndLayout<'tcx> { - fx.layout_of(fx.monomorphize(&fx.mir.local_decls[RETURN_PLACE].ty)) -} +use rustc_middle::ty::layout::FnAbiExt; +use rustc_target::abi::call::{ArgAbi, FnAbi, PassMode}; +use smallvec::{smallvec, SmallVec}; /// Can the given type be returned into an ssa var or does it need to be returned on the stack. pub(crate) fn can_return_to_ssa_var<'tcx>( - tcx: TyCtxt<'tcx>, - dest_layout: TyAndLayout<'tcx>, + fx: &FunctionCx<'_, 'tcx, impl Module>, + func: &mir::Operand<'tcx>, + args: &[mir::Operand<'tcx>], ) -> bool { - match get_pass_mode(tcx, dest_layout) { - PassMode::NoPass | PassMode::ByVal(_) | PassMode::ByValPair(_, _) => true, - // FIXME Make it possible to return ByRef to an ssa var. - PassMode::ByRef { size: _ } => false, + let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx)); + let fn_sig = fx + .tcx + .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); + + // Handle special calls like instrinsics and empty drop glue. + let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() { + let instance = ty::Instance::resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs) + .unwrap() + .unwrap() + .polymorphize(fx.tcx); + + match instance.def { + InstanceDef::Intrinsic(_) | InstanceDef::DropGlue(_, _) => { + return true; + } + _ => Some(instance), + } + } else { + None + }; + + let extra_args = &args[fn_sig.inputs().len()..]; + let extra_args = extra_args + .iter() + .map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx))) + .collect::>(); + let fn_abi = if let Some(instance) = instance { + FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args) + } else { + FnAbi::of_fn_ptr( + &RevealAllLayoutCx(fx.tcx), + fn_ty.fn_sig(fx.tcx), + &extra_args, + ) + }; + match fn_abi.ret.mode { + PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) => true, + // FIXME Make it possible to return Cast and Indirect to an ssa var. + PassMode::Cast(_) | PassMode::Indirect { .. } => false, } } @@ -24,27 +60,45 @@ pub(crate) fn can_return_to_ssa_var<'tcx>( pub(super) fn codegen_return_param<'tcx>( fx: &mut FunctionCx<'_, 'tcx, impl Module>, ssa_analyzed: &rustc_index::vec::IndexVec, - start_block: Block, + block_params_iter: &mut impl Iterator, ) -> CPlace<'tcx> { - let ret_layout = return_layout(fx); - let ret_pass_mode = get_pass_mode(fx.tcx, ret_layout); - let (ret_place, ret_param) = match ret_pass_mode { - PassMode::NoPass => (CPlace::no_place(ret_layout), Empty), - PassMode::ByVal(_) | PassMode::ByValPair(_, _) => { + let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode { + PassMode::Ignore => ( + CPlace::no_place(fx.fn_abi.as_ref().unwrap().ret.layout), + smallvec![], + ), + PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => { let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa; ( - super::make_local_place(fx, RETURN_PLACE, ret_layout, is_ssa), - Empty, + super::make_local_place( + fx, + RETURN_PLACE, + fx.fn_abi.as_ref().unwrap().ret.layout, + is_ssa, + ), + smallvec![], ) } - PassMode::ByRef { size: Some(_) } => { - let ret_param = fx.bcx.append_block_param(start_block, fx.pointer_type); + PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack: _, + } => { + let ret_param = block_params_iter.next().unwrap(); + assert_eq!(fx.bcx.func.dfg.value_type(ret_param), pointer_ty(fx.tcx)); ( - CPlace::for_ptr(Pointer::new(ret_param), ret_layout), - Single(ret_param), + CPlace::for_ptr( + Pointer::new(ret_param), + fx.fn_abi.as_ref().unwrap().ret.layout, + ), + smallvec![ret_param], ) } - PassMode::ByRef { size: None } => todo!(), + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => unreachable!("unsized return value"), }; #[cfg(not(debug_assertions))] @@ -56,9 +110,9 @@ pub(super) fn codegen_return_param<'tcx>( "ret", Some(RETURN_PLACE), None, - ret_param, - ret_pass_mode, - ret_layout.ty, + &ret_param, + fx.fn_abi.as_ref().unwrap().ret.mode, + fx.fn_abi.as_ref().unwrap().ret.layout, ); ret_place @@ -68,42 +122,71 @@ pub(super) fn codegen_return_param<'tcx>( /// returns the call return value(s) if any are written to the correct place. pub(super) fn codegen_with_call_return_arg<'tcx, M: Module, T>( fx: &mut FunctionCx<'_, 'tcx, M>, - fn_sig: FnSig<'tcx>, + ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, ret_place: Option>, f: impl FnOnce(&mut FunctionCx<'_, 'tcx, M>, Option) -> (Inst, T), ) -> (Inst, T) { - let ret_layout = fx.layout_of(fn_sig.output()); - - let output_pass_mode = get_pass_mode(fx.tcx, ret_layout); - let return_ptr = match output_pass_mode { - PassMode::NoPass => None, - PassMode::ByRef { size: Some(_) } => match ret_place { + let return_ptr = match ret_arg_abi.mode { + PassMode::Ignore => None, + PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack: _, + } => match ret_place { Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)), None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)), // FIXME allocate temp stack slot }, - PassMode::ByRef { size: None } => todo!(), - PassMode::ByVal(_) | PassMode::ByValPair(_, _) => None, + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => unreachable!("unsized return value"), + PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => None, }; let (call_inst, meta) = f(fx, return_ptr); - match output_pass_mode { - PassMode::NoPass => {} - PassMode::ByVal(_) => { + match ret_arg_abi.mode { + PassMode::Ignore => {} + PassMode::Direct(_) => { if let Some(ret_place) = ret_place { let ret_val = fx.bcx.inst_results(call_inst)[0]; - ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_layout)); + ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_arg_abi.layout)); } } - PassMode::ByValPair(_, _) => { + PassMode::Pair(_, _) => { if let Some(ret_place) = ret_place { let ret_val_a = fx.bcx.inst_results(call_inst)[0]; let ret_val_b = fx.bcx.inst_results(call_inst)[1]; - ret_place.write_cvalue(fx, CValue::by_val_pair(ret_val_a, ret_val_b, ret_layout)); + ret_place.write_cvalue( + fx, + CValue::by_val_pair(ret_val_a, ret_val_b, ret_arg_abi.layout), + ); } } - PassMode::ByRef { size: Some(_) } => {} - PassMode::ByRef { size: None } => todo!(), + PassMode::Cast(cast) => { + if let Some(ret_place) = ret_place { + let results = fx + .bcx + .inst_results(call_inst) + .into_iter() + .copied() + .collect::>(); + let result = + super::pass_mode::from_casted_value(fx, &results, ret_place.layout(), cast); + ret_place.write_cvalue(fx, result); + } + } + PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack: _, + } => {} + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => unreachable!("unsized return value"), } (call_inst, meta) @@ -111,20 +194,35 @@ pub(super) fn codegen_with_call_return_arg<'tcx, M: Module, T>( /// Codegen a return instruction with the right return value(s) if any. pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, impl Module>) { - match get_pass_mode(fx.tcx, return_layout(fx)) { - PassMode::NoPass | PassMode::ByRef { size: Some(_) } => { + match fx.fn_abi.as_ref().unwrap().ret.mode { + PassMode::Ignore + | PassMode::Indirect { + attrs: _, + extra_attrs: None, + on_stack: _, + } => { fx.bcx.ins().return_(&[]); } - PassMode::ByRef { size: None } => todo!(), - PassMode::ByVal(_) => { + PassMode::Indirect { + attrs: _, + extra_attrs: Some(_), + on_stack: _, + } => unreachable!("unsized return value"), + PassMode::Direct(_) => { let place = fx.get_local_place(RETURN_PLACE); let ret_val = place.to_cvalue(fx).load_scalar(fx); fx.bcx.ins().return_(&[ret_val]); } - PassMode::ByValPair(_, _) => { + PassMode::Pair(_, _) => { let place = fx.get_local_place(RETURN_PLACE); let (ret_val_a, ret_val_b) = place.to_cvalue(fx).load_scalar_pair(fx); fx.bcx.ins().return_(&[ret_val_a, ret_val_b]); } + PassMode::Cast(cast) => { + let place = fx.get_local_place(RETURN_PLACE); + let ret_val = place.to_cvalue(fx); + let ret_vals = super::pass_mode::to_casted_value(fx, ret_val, cast); + fx.bcx.ins().return_(&ret_vals); + } } } diff --git a/src/analyze.rs b/src/analyze.rs index adf5c7ac4fee7..62fbcfe3f7a5d 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -40,11 +40,14 @@ pub(crate) fn analyze(fx: &FunctionCx<'_, '_, impl Module>) -> IndexVec { + TerminatorKind::Call { + destination, + func, + args, + .. + } => { if let Some((dest_place, _dest_bb)) = destination { - let dest_layout = fx - .layout_of(fx.monomorphize(&dest_place.ty(&fx.mir.local_decls, fx.tcx).ty)); - if !crate::abi::can_return_to_ssa_var(fx.tcx, dest_layout) { + if !crate::abi::can_return_to_ssa_var(fx, func, args) { not_ssa(&mut flag_map, dest_place.local) } } diff --git a/src/base.rs b/src/base.rs index 34c9561d67622..4842628a99da7 100644 --- a/src/base.rs +++ b/src/base.rs @@ -2,6 +2,8 @@ use rustc_index::vec::IndexVec; use rustc_middle::ty::adjustment::PointerCast; +use rustc_middle::ty::layout::FnAbiExt; +use rustc_target::abi::call::FnAbi; use crate::prelude::*; @@ -19,7 +21,8 @@ pub(crate) fn codegen_fn<'tcx>( let mir = tcx.instance_mir(instance.def); // Declare function - let (name, sig) = get_function_name_and_sig(tcx, cx.module.isa().triple(), instance, false); + let name = tcx.symbol_name(instance).name.to_string(); + let sig = get_function_sig(tcx, cx.module.isa().triple(), instance); let func_id = cx.module.declare_function(&name, linkage, &sig).unwrap(); cx.cached_context.clear(); @@ -50,6 +53,7 @@ pub(crate) fn codegen_fn<'tcx>( instance, mir, + fn_abi: Some(FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])), bcx, block_map, @@ -117,6 +121,9 @@ pub(crate) fn codegen_fn<'tcx>( context.compute_domtree(); context.eliminate_unreachable_code(cx.module.isa()).unwrap(); context.dce(cx.module.isa()).unwrap(); + // Some Cranelift optimizations expect the domtree to not yet be computed and as such don't + // invalidate it when it would change. + context.domtree.clear(); context.want_disasm = crate::pretty_clif::should_write_ir(tcx); @@ -1053,7 +1060,11 @@ pub(crate) fn codegen_panic_inner<'tcx>( fx.lib_call( &*symbol_name, - vec![fx.pointer_type, fx.pointer_type, fx.pointer_type], + vec![ + AbiParam::new(fx.pointer_type), + AbiParam::new(fx.pointer_type), + AbiParam::new(fx.pointer_type), + ], vec![], args, ); diff --git a/src/bin/cg_clif_build_sysroot.rs b/src/bin/cg_clif_build_sysroot.rs index 8ee4cd46c94e0..83e5dc6e6724d 100644 --- a/src/bin/cg_clif_build_sysroot.rs +++ b/src/bin/cg_clif_build_sysroot.rs @@ -53,10 +53,7 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks { .unwrap() .parent() .unwrap() - .parent() - .unwrap() - .join("build_sysroot") - .join("sysroot"), + .to_owned(), ); } } diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index d6a38bdafc9ba..866ba90e4ae4b 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -1,5 +1,7 @@ //! Replaces 128-bit operators with lang item calls where necessary +use cranelift_codegen::ir::ArgumentPurpose; + use crate::prelude::*; pub(crate) fn maybe_codegen<'tcx>( @@ -24,41 +26,41 @@ pub(crate) fn maybe_codegen<'tcx>( None } BinOp::Add | BinOp::Sub if !checked => None, - BinOp::Add => { - let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - return Some(if is_signed { - fx.easy_call("__rust_i128_addo", &[lhs, rhs], out_ty) + BinOp::Mul if !checked => { + let val_ty = if is_signed { + fx.tcx.types.i128 } else { - fx.easy_call("__rust_u128_addo", &[lhs, rhs], out_ty) - }); + fx.tcx.types.u128 + }; + Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty)) } - BinOp::Sub => { + BinOp::Add | BinOp::Sub | BinOp::Mul => { + assert!(checked); let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - return Some(if is_signed { - fx.easy_call("__rust_i128_subo", &[lhs, rhs], out_ty) - } else { - fx.easy_call("__rust_u128_subo", &[lhs, rhs], out_ty) - }); - } - BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"), - BinOp::Mul => { - let res = if checked { - let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - if is_signed { - fx.easy_call("__rust_i128_mulo", &[lhs, rhs], out_ty) - } else { - fx.easy_call("__rust_u128_mulo", &[lhs, rhs], out_ty) - } - } else { - let val_ty = if is_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; - fx.easy_call("__multi3", &[lhs, rhs], val_ty) + let out_place = CPlace::new_stack_slot(fx, fx.layout_of(out_ty)); + let param_types = vec![ + AbiParam::special(pointer_ty(fx.tcx), ArgumentPurpose::StructReturn), + AbiParam::new(types::I128), + AbiParam::new(types::I128), + ]; + let args = [ + out_place.to_ptr().get_addr(fx), + lhs.load_scalar(fx), + rhs.load_scalar(fx), + ]; + let name = match (bin_op, is_signed) { + (BinOp::Add, false) => "__rust_u128_addo", + (BinOp::Add, true) => "__rust_i128_addo", + (BinOp::Sub, false) => "__rust_u128_subo", + (BinOp::Sub, true) => "__rust_i128_subo", + (BinOp::Mul, false) => "__rust_u128_mulo", + (BinOp::Mul, true) => "__rust_i128_mulo", + _ => unreachable!(), }; - Some(res) + fx.lib_call(name, param_types, vec![], &args); + Some(out_place.to_cvalue(fx)) } + BinOp::Offset => unreachable!("offset should only be used on pointers, not 128bit ints"), BinOp::Div => { assert!(!checked); if is_signed { diff --git a/src/common.rs b/src/common.rs index 1485d4451b815..fbee84e09f7a6 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1,4 +1,5 @@ use rustc_index::vec::IndexVec; +use rustc_target::abi::call::FnAbi; use rustc_target::abi::{Integer, Primitive}; use rustc_target::spec::{HasTargetSpec, Target}; @@ -294,6 +295,7 @@ pub(crate) struct FunctionCx<'clif, 'tcx, M: Module> { pub(crate) instance: Instance<'tcx>, pub(crate) mir: &'tcx Body<'tcx>, + pub(crate) fn_abi: Option>>, pub(crate) bcx: FunctionBuilder<'clif>, pub(crate) block_map: IndexVec, @@ -319,16 +321,7 @@ impl<'tcx, M: Module> LayoutOf for FunctionCx<'_, 'tcx, M> { type TyAndLayout = TyAndLayout<'tcx>; fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> { - assert!(!ty.still_further_specializable()); - self.tcx - .layout_of(ParamEnv::reveal_all().and(&ty)) - .unwrap_or_else(|e| { - if let layout::LayoutError::SizeOverflow(_) = e { - self.tcx.sess.fatal(&e.to_string()) - } else { - bug!("failed to get layout for `{}`: {}", ty, e) - } - }) + RevealAllLayoutCx(self.tcx).layout_of(ty) } } @@ -442,3 +435,47 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { self.bcx.ins().global_value(self.pointer_type, local_msg_id) } } + +pub(crate) struct RevealAllLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>); + +impl<'tcx> LayoutOf for RevealAllLayoutCx<'tcx> { + type Ty = Ty<'tcx>; + type TyAndLayout = TyAndLayout<'tcx>; + + fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> { + assert!(!ty.still_further_specializable()); + self.0 + .layout_of(ParamEnv::reveal_all().and(&ty)) + .unwrap_or_else(|e| { + if let layout::LayoutError::SizeOverflow(_) = e { + self.0.sess.fatal(&e.to_string()) + } else { + bug!("failed to get layout for `{}`: {}", ty, e) + } + }) + } +} + +impl<'tcx> layout::HasTyCtxt<'tcx> for RevealAllLayoutCx<'tcx> { + fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { + self.0 + } +} + +impl<'tcx> rustc_target::abi::HasDataLayout for RevealAllLayoutCx<'tcx> { + fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout { + &self.0.data_layout + } +} + +impl<'tcx> layout::HasParamEnv<'tcx> for RevealAllLayoutCx<'tcx> { + fn param_env(&self) -> ParamEnv<'tcx> { + ParamEnv::reveal_all() + } +} + +impl<'tcx> HasTargetSpec for RevealAllLayoutCx<'tcx> { + fn target_spec(&self) -> &Target { + &self.0.sess.target + } +} diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 16f9bfc99189f..df89883f0bbb7 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -281,9 +281,6 @@ pub(super) fn run_aot( None }; - rustc_incremental::assert_dep_graph(tcx); - rustc_incremental::save_dep_graph(tcx); - let metadata_module = if need_metadata_module { let _timer = tcx.prof.generic_activity("codegen crate metadata"); let (metadata_cgu_name, tmp_file) = tcx.sess.time("write compressed metadata", || { @@ -322,10 +319,6 @@ pub(super) fn run_aot( None }; - if tcx.sess.opts.output_types.should_codegen() { - rustc_incremental::assert_module_sources::assert_module_sources(tcx); - } - Box::new(( CodegenResults { crate_name: tcx.crate_name(LOCAL_CRATE), diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 9a42c675cc144..2d14ff2c0221d 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -156,12 +156,8 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8 let jit_module = jit_module.as_mut().unwrap(); let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false); - let (name, sig) = crate::abi::get_function_name_and_sig( - tcx, - cx.module.isa().triple(), - instance, - true, - ); + let name = tcx.symbol_name(instance).name.to_string(); + let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), instance); let func_id = cx .module .declare_function(&name, Linkage::Export, &sig) @@ -246,8 +242,8 @@ pub(super) fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx, impl Module>, inst: In let pointer_type = cx.module.target_config().pointer_type(); - let (name, sig) = - crate::abi::get_function_name_and_sig(tcx, cx.module.isa().triple(), inst, true); + let name = tcx.symbol_name(inst).name.to_string(); + let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), inst); let func_id = cx .module .declare_function(&name, Linkage::Export, &sig) diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 9f4ea9a386551..2497f9dfdfbcf 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -50,12 +50,9 @@ fn predefine_mono_items<'tcx>( for &(mono_item, (linkage, visibility)) in mono_items { match mono_item { MonoItem::Fn(instance) => { - let (name, sig) = get_function_name_and_sig( - cx.tcx, - cx.module.isa().triple(), - instance, - false, - ); + let name = cx.tcx.symbol_name(instance).name.to_string(); + let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); + let sig = get_function_sig(cx.tcx, cx.module.isa().triple(), instance); let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); cx.module.declare_function(&name, linkage, &sig).unwrap(); } diff --git a/src/lib.rs b/src/lib.rs index f31c58b92e407..170750461cace 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,6 @@ extern crate rustc_incremental; extern crate rustc_index; extern crate rustc_session; extern crate rustc_span; -extern crate rustc_symbol_mangling; extern crate rustc_target; // This prevents duplicating functions and statics that are already part of the host rustc process. @@ -90,7 +89,8 @@ mod prelude { pub(crate) use rustc_middle::mir::{self, *}; pub(crate) use rustc_middle::ty::layout::{self, TyAndLayout}; pub(crate) use rustc_middle::ty::{ - self, FloatTy, FnSig, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut, TypeFoldable, UintTy, + self, FloatTy, Instance, InstanceDef, IntTy, ParamEnv, Ty, TyCtxt, TypeAndMut, + TypeFoldable, UintTy, }; pub(crate) use rustc_target::abi::{Abi, LayoutOf, Scalar, Size, VariantIdx}; @@ -256,8 +256,6 @@ impl CodegenBackend for CraneliftCodegenBackend { }; let res = driver::codegen_crate(tcx, metadata, need_metadata_module, config); - rustc_symbol_mangling::test::report_symbol_names(tcx); - res } @@ -279,18 +277,14 @@ impl CodegenBackend for CraneliftCodegenBackend { ) -> Result<(), ErrorReported> { use rustc_codegen_ssa::back::link::link_binary; - let _timer = sess.prof.generic_activity("link_crate"); - - sess.time("linking", || { - let target_cpu = crate::target_triple(sess).to_string(); - link_binary::>( - sess, - &codegen_results, - outputs, - &codegen_results.crate_name.as_str(), - &target_cpu, - ); - }); + let target_cpu = crate::target_triple(sess).to_string(); + link_binary::>( + sess, + &codegen_results, + outputs, + &codegen_results.crate_name.as_str(), + &target_cpu, + ); Ok(()) } @@ -345,7 +339,12 @@ fn build_isa(sess: &Session) -> Box { let flags = settings::Flags::new(flags_builder); - let mut isa_builder = cranelift_codegen::isa::lookup(target_triple).unwrap(); + let variant = if cfg!(feature = "oldbe") { + cranelift_codegen::isa::BackendVariant::Legacy + } else { + cranelift_codegen::isa::BackendVariant::MachInst + }; + let mut isa_builder = cranelift_codegen::isa::lookup_variant(target_triple, variant).unwrap(); // Don't use "haswell", as it implies `has_lzcnt`.macOS CI is still at Ivy Bridge EP, so `lzcnt` // is interpreted as `bsr`. isa_builder.enable("nehalem").unwrap(); diff --git a/src/main_shim.rs b/src/main_shim.rs index 6c472e6774fe7..b193cea877dad 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -69,8 +69,8 @@ pub(crate) fn maybe_create_entry_wrapper( let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx); - let (main_name, main_sig) = - get_function_name_and_sig(tcx, m.isa().triple(), instance, false); + let main_name = tcx.symbol_name(instance).name.to_string(); + let main_sig = get_function_sig(tcx, m.isa().triple(), instance); let main_func_id = m .declare_function(&main_name, Linkage::Import, &main_sig) .unwrap(); diff --git a/src/num.rs b/src/num.rs index 41f4a9b9662bc..d1d2b3b872a4b 100644 --- a/src/num.rs +++ b/src/num.rs @@ -280,7 +280,6 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( (val, fx.bcx.ins().bor(has_underflow, has_overflow)) } types::I64 => { - //let val = fx.easy_call("__mulodi4", &[lhs, rhs, overflow_ptr], types::I64); let val = fx.bcx.ins().imul(lhs, rhs); let has_overflow = if !signed { let val_hi = fx.bcx.ins().umulhi(lhs, rhs); diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index 22c94fec82fc1..f4a15ab12d511 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -61,7 +61,9 @@ use cranelift_codegen::{ write::{FuncWriter, PlainWriter}, }; +use rustc_middle::ty::layout::FnAbiExt; use rustc_session::config::OutputType; +use rustc_target::abi::call::FnAbi; use crate::prelude::*; @@ -78,11 +80,8 @@ impl CommentWriter { format!("symbol {}", tcx.symbol_name(instance).name), format!("instance {:?}", instance), format!( - "sig {:?}", - tcx.normalize_erasing_late_bound_regions( - ParamEnv::reveal_all(), - crate::abi::fn_sig_for_fn_abi(tcx, instance) - ) + "abi {:?}", + FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[]) ), String::new(), ] diff --git a/src/value_and_place.rs b/src/value_and_place.rs index 5bcb11fd515a0..765604e0f984e 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -334,7 +334,9 @@ impl<'tcx> CPlace<'tcx> { let stack_slot = fx.bcx.create_stack_slot(StackSlotData { kind: StackSlotKind::ExplicitSlot, - size: u32::try_from(layout.size.bytes()).unwrap(), + // FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to + // specify stack slot alignment. + size: (u32::try_from(layout.size.bytes()).unwrap() + 15) / 16 * 16, offset: None, }); CPlace { @@ -450,64 +452,6 @@ impl<'tcx> CPlace<'tcx> { fx: &mut FunctionCx<'_, 'tcx, impl Module>, from: CValue<'tcx>, ) { - fn assert_assignable<'tcx>( - fx: &FunctionCx<'_, 'tcx, impl Module>, - from_ty: Ty<'tcx>, - to_ty: Ty<'tcx>, - ) { - match (from_ty.kind(), to_ty.kind()) { - (ty::Ref(_, a, _), ty::Ref(_, b, _)) - | ( - ty::RawPtr(TypeAndMut { ty: a, mutbl: _ }), - ty::RawPtr(TypeAndMut { ty: b, mutbl: _ }), - ) => { - assert_assignable(fx, a, b); - } - (ty::FnPtr(_), ty::FnPtr(_)) => { - let from_sig = fx.tcx.normalize_erasing_late_bound_regions( - ParamEnv::reveal_all(), - from_ty.fn_sig(fx.tcx), - ); - let to_sig = fx.tcx.normalize_erasing_late_bound_regions( - ParamEnv::reveal_all(), - to_ty.fn_sig(fx.tcx), - ); - assert_eq!( - from_sig, to_sig, - "Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}", - from_sig, to_sig, fx, - ); - // fn(&T) -> for<'l> fn(&'l T) is allowed - } - (&ty::Dynamic(from_traits, _), &ty::Dynamic(to_traits, _)) => { - for (from, to) in from_traits.iter().zip(to_traits) { - let from = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from); - let to = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to); - assert_eq!( - from, to, - "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}", - from_traits, to_traits, fx, - ); - } - // dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed - } - _ => { - assert_eq!( - from_ty, - to_ty, - "Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}", - from_ty, - to_ty, - fx, - ); - } - } - } - assert_assignable(fx, from.layout().ty, self.layout().ty); self.write_cvalue_maybe_transmute(fx, from, "write_cvalue"); @@ -556,7 +500,9 @@ impl<'tcx> CPlace<'tcx> { // FIXME do something more efficient for transmutes between vectors and integers. let stack_slot = fx.bcx.create_stack_slot(StackSlotData { kind: StackSlotKind::ExplicitSlot, - size: src_ty.bytes(), + // FIXME Don't force the size to a multiple of 16 bytes once Cranelift gets a way to + // specify stack slot alignment. + size: (src_ty.bytes() + 15) / 16 * 16, offset: None, }); let ptr = Pointer::stack_slot(stack_slot); @@ -794,3 +740,62 @@ impl<'tcx> CPlace<'tcx> { } } } + +#[track_caller] +pub(crate) fn assert_assignable<'tcx>( + fx: &FunctionCx<'_, 'tcx, impl Module>, + from_ty: Ty<'tcx>, + to_ty: Ty<'tcx>, +) { + match (from_ty.kind(), to_ty.kind()) { + (ty::Ref(_, a, _), ty::Ref(_, b, _)) + | ( + ty::RawPtr(TypeAndMut { ty: a, mutbl: _ }), + ty::RawPtr(TypeAndMut { ty: b, mutbl: _ }), + ) => { + assert_assignable(fx, a, b); + } + (ty::Ref(_, a, _), ty::RawPtr(TypeAndMut { ty: b, mutbl: _ })) + | (ty::RawPtr(TypeAndMut { ty: a, mutbl: _ }), ty::Ref(_, b, _)) => { + assert_assignable(fx, a, b); + } + (ty::FnPtr(_), ty::FnPtr(_)) => { + let from_sig = fx.tcx.normalize_erasing_late_bound_regions( + ParamEnv::reveal_all(), + from_ty.fn_sig(fx.tcx), + ); + let to_sig = fx + .tcx + .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to_ty.fn_sig(fx.tcx)); + assert_eq!( + from_sig, to_sig, + "Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}", + from_sig, to_sig, fx, + ); + // fn(&T) -> for<'l> fn(&'l T) is allowed + } + (&ty::Dynamic(from_traits, _), &ty::Dynamic(to_traits, _)) => { + for (from, to) in from_traits.iter().zip(to_traits) { + let from = fx + .tcx + .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from); + let to = fx + .tcx + .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to); + assert_eq!( + from, to, + "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}", + from_traits, to_traits, fx, + ); + } + // dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed + } + _ => { + assert_eq!( + from_ty, to_ty, + "Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}", + from_ty, to_ty, fx, + ); + } + } +} diff --git a/test.sh b/test.sh index c6c4956e48174..5ab10e0e905c7 100755 --- a/test.sh +++ b/test.sh @@ -1,9 +1,7 @@ #!/bin/bash set -e -export RUSTFLAGS="-Zrun_dsymutil=no" - -./build.sh --without-sysroot "$@" +./build.sh --sysroot none "$@" rm -r target/out || true From e4ae9ef3b0ee28b3f3602a55e02d5be857379dd3 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 2 Feb 2021 10:24:19 +0100 Subject: [PATCH 03/61] Rustup to rustc 1.51.0-nightly (d4e3570db 2021-02-01) --- build_sysroot/Cargo.lock | 4 ++-- rust-toolchain | 2 +- src/archive.rs | 6 +----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index 0da9999c17285..d29ab8c18033d 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -139,9 +139,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.84" +version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1cca32fa0182e8c0989459524dc356b8f2b5c10f1b9eb521b7d182c03cf8c5ff" +checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" dependencies = [ "rustc-std-workspace-core", ] diff --git a/rust-toolchain b/rust-toolchain index a08f00d19c20f..de8e5f71cdc5b 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-01-30 +nightly-2021-02-02 diff --git a/src/archive.rs b/src/archive.rs index 96579054389ab..d2ed553b700b3 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -30,7 +30,6 @@ pub(crate) struct ArArchiveBuilder<'a> { // Don't use `HashMap` here, as the order is important. `rust.metadata.bin` must always be at // the end of an archive for linkers to not get confused. entries: Vec<(String, ArchiveEntry)>, - update_symbols: bool, } impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { @@ -69,7 +68,6 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { src_archives, entries, - update_symbols: false, } } @@ -136,9 +134,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { }) } - fn update_symbols(&mut self) { - self.update_symbols = true; - } + fn update_symbols(&mut self) {} fn build(mut self) { enum BuilderKind { From 54523b8ea84cb9360639568a6c3c3e17c31ab5a8 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 22 Jan 2021 14:15:12 +0100 Subject: [PATCH 04/61] Fix rustc bootstrap test --- scripts/test_bootstrap.sh | 40 ++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/scripts/test_bootstrap.sh b/scripts/test_bootstrap.sh index db69541b226cf..15f97c867a1ec 100755 --- a/scripts/test_bootstrap.sh +++ b/scripts/test_bootstrap.sh @@ -14,21 +14,18 @@ git checkout -- . git checkout "$(rustc -V | cut -d' ' -f3 | tr -d '(')" git apply - < config.toml < Date: Sun, 24 Jan 2021 17:15:05 +0000 Subject: [PATCH 05/61] Add a new ABI to support cmse_nonsecure_call This commit adds a new ABI to be selected via `extern "C-cmse-nonsecure-call"` on function pointers in order for the compiler to apply the corresponding cmse_nonsecure_call callsite attribute. For Armv8-M targets supporting TrustZone-M, this will perform a non-secure function call by saving, clearing and calling a non-secure function pointer using the BLXNS instruction. See the page on the unstable book for details. Signed-off-by: Hugues de Valon --- src/abi/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 6a025f2e88ae3..b2647e6c8d384 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -28,6 +28,7 @@ fn clif_sig_from_fn_abi<'tcx>( Conv::X86_64SysV => CallConv::SystemV, Conv::X86_64Win64 => CallConv::WindowsFastcall, Conv::ArmAapcs + | Conv::CCmseNonSecureCall | Conv::Msp430Intr | Conv::PtxKernel | Conv::X86Fastcall From 0f861e1035a64368494aa0d1381065f3bb1efede Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 3 Feb 2021 11:53:23 +0100 Subject: [PATCH 06/61] Update Cranelift --- Cargo.lock | 66 +++++---------------------- Cargo.toml | 2 +- src/debuginfo/mod.rs | 105 +++++++------------------------------------ 3 files changed, 29 insertions(+), 144 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5495cfa5eaa0d..99d9bbf18922c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,12 +29,6 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" -[[package]] -name = "cc" -version = "1.0.66" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" - [[package]] name = "cfg-if" version = "0.1.10" @@ -50,7 +44,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "cranelift-entity", ] @@ -58,7 +52,7 @@ dependencies = [ [[package]] name = "cranelift-codegen" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "byteorder", "cranelift-bforest", @@ -76,7 +70,7 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -85,17 +79,17 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" [[package]] name = "cranelift-entity" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" [[package]] name = "cranelift-frontend" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "cranelift-codegen", "log", @@ -106,7 +100,7 @@ dependencies = [ [[package]] name = "cranelift-jit" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "anyhow", "cranelift-codegen", @@ -124,7 +118,7 @@ dependencies = [ [[package]] name = "cranelift-module" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "anyhow", "cranelift-codegen", @@ -136,17 +130,16 @@ dependencies = [ [[package]] name = "cranelift-native" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "cranelift-codegen", - "raw-cpuid", "target-lexicon", ] [[package]] name = "cranelift-object" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#986b5768f9e68f1564b43f32b8a4080a6582c8ca" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" dependencies = [ "anyhow", "cranelift-codegen", @@ -253,9 +246,9 @@ dependencies = [ [[package]] name = "object" -version = "0.22.0" +version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d3b63360ec3cb337817c2dbd47ab4a0f170d285d8e5a2064600f3def1402397" +checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4" dependencies = [ "crc32fast", "indexmap", @@ -279,17 +272,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "raw-cpuid" -version = "8.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fdf7d9dbd43f3d81d94a49c1c3df73cc2b3827995147e6cf7f89d4ec5483e73" -dependencies = [ - "bitflags", - "cc", - "rustc_version", -] - [[package]] name = "regalloc" version = "0.0.31" @@ -337,30 +319,6 @@ dependencies = [ "target-lexicon", ] -[[package]] -name = "rustc_version" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a" -dependencies = [ - "semver", -] - -[[package]] -name = "semver" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" -dependencies = [ - "semver-parser", -] - -[[package]] -name = "semver-parser" -version = "0.7.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" - [[package]] name = "smallvec" version = "1.6.1" diff --git a/Cargo.toml b/Cargo.toml index 3820fce6d1e0d..41e23b51afaff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch cranelift-object = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" } target-lexicon = "0.11.0" gimli = { version = "0.23.0", default-features = false, features = ["write"]} -object = { version = "0.22.0", default-features = false, features = ["std", "read_core", "write", "coff", "elf", "macho", "pe"] } +object = { version = "0.23.0", default-features = false, features = ["std", "read_core", "write", "coff", "elf", "macho", "pe"] } ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "do_not_remove_cg_clif_ranlib" } indexmap = "1.0.2" diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index a6f4ded41b64b..28adbe2fdb6e2 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -9,7 +9,7 @@ use crate::prelude::*; use rustc_index::vec::IndexVec; use cranelift_codegen::entity::EntityRef; -use cranelift_codegen::ir::{StackSlots, ValueLabel, ValueLoc}; +use cranelift_codegen::ir::{LabelValueLoc, StackSlots, ValueLabel, ValueLoc}; use cranelift_codegen::isa::TargetIsa; use cranelift_codegen::ValueLocRange; @@ -39,7 +39,6 @@ pub(crate) struct DebugContext<'tcx> { dwarf: DwarfUnit, unit_range_list: RangeList, - clif_types: FxHashMap, types: FxHashMap, UnitEntryId>, } @@ -115,48 +114,10 @@ impl<'tcx> DebugContext<'tcx> { dwarf, unit_range_list: RangeList(Vec::new()), - clif_types: FxHashMap::default(), types: FxHashMap::default(), } } - fn dwarf_ty_for_clif_ty(&mut self, ty: Type) -> UnitEntryId { - if let Some(type_id) = self.clif_types.get(&ty) { - return *type_id; - } - - let new_entry = |dwarf: &mut DwarfUnit, tag| dwarf.unit.add(dwarf.unit.root(), tag); - - let primitive = |dwarf: &mut DwarfUnit, ate| { - let type_id = new_entry(dwarf, gimli::DW_TAG_base_type); - let type_entry = dwarf.unit.get_mut(type_id); - type_entry.set(gimli::DW_AT_encoding, AttributeValue::Encoding(ate)); - type_id - }; - - let type_id = if ty.is_bool() { - primitive(&mut self.dwarf, gimli::DW_ATE_boolean) - } else if ty.is_int() { - primitive(&mut self.dwarf, gimli::DW_ATE_address) - } else if ty.is_float() { - primitive(&mut self.dwarf, gimli::DW_ATE_float) - } else { - new_entry(&mut self.dwarf, gimli::DW_TAG_structure_type) - }; - - let type_entry = self.dwarf.unit.get_mut(type_id); - type_entry.set( - gimli::DW_AT_name, - AttributeValue::String(format!("{}", ty).replace('i', "u").into_bytes()), - ); - type_entry.set( - gimli::DW_AT_byte_size, - AttributeValue::Udata(u64::from(ty.bytes())), - ); - - type_id - } - fn dwarf_ty(&mut self, ty: Ty<'tcx>) -> UnitEntryId { if let Some(type_id) = self.types.get(ty) { return *type_id; @@ -312,51 +273,6 @@ impl<'tcx> DebugContext<'tcx> { // Using Udata for DW_AT_high_pc requires at least DWARF4 func_entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(end))); - // FIXME Remove once actual debuginfo for locals works. - for (i, (param, &val)) in context - .func - .signature - .params - .iter() - .zip( - context - .func - .dfg - .block_params(context.func.layout.entry_block().unwrap()), - ) - .enumerate() - { - use cranelift_codegen::ir::ArgumentPurpose; - let base_name = match param.purpose { - ArgumentPurpose::Normal => "arg", - ArgumentPurpose::StructArgument(_) => "struct_arg", - ArgumentPurpose::StructReturn => "sret", - ArgumentPurpose::Link - | ArgumentPurpose::FramePointer - | ArgumentPurpose::CalleeSaved => continue, - ArgumentPurpose::VMContext - | ArgumentPurpose::SignatureId - | ArgumentPurpose::CallerTLS - | ArgumentPurpose::CalleeTLS - | ArgumentPurpose::StackLimit => unreachable!(), - }; - let name = format!("{}{}", base_name, i); - - let dw_ty = self.dwarf_ty_for_clif_ty(param.value_type); - let loc = - translate_loc(isa, context.func.locations[val], &context.func.stack_slots).unwrap(); - - let arg_id = self - .dwarf - .unit - .add(entry_id, gimli::DW_TAG_formal_parameter); - let var_entry = self.dwarf.unit.get_mut(arg_id); - - var_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes())); - var_entry.set(gimli::DW_AT_type, AttributeValue::UnitRef(dw_ty)); - var_entry.set(gimli::DW_AT_location, AttributeValue::Exprloc(loc)); - } - // FIXME make it more reliable and implement scopes before re-enabling this. if false { let value_labels_ranges = context.build_value_labels_ranges(isa).unwrap(); @@ -463,17 +379,17 @@ fn place_location<'tcx>( // Adapted from https://github.com/CraneStation/wasmtime/blob/5a1845b4caf7a5dba8eda1fef05213a532ed4259/crates/debug/src/transform/expression.rs#L59-L137 fn translate_loc( isa: &dyn TargetIsa, - loc: ValueLoc, + loc: LabelValueLoc, stack_slots: &StackSlots, ) -> Option { match loc { - ValueLoc::Reg(reg) => { + LabelValueLoc::ValueLoc( ValueLoc::Reg(reg)) => { let machine_reg = isa.map_dwarf_register(reg).unwrap(); let mut expr = Expression::new(); expr.op_reg(gimli::Register(machine_reg)); Some(expr) } - ValueLoc::Stack(ss) => { + LabelValueLoc::ValueLoc(ValueLoc::Stack(ss)) => { if let Some(ss_offset) = stack_slots[ss].offset { let mut expr = Expression::new(); expr.op_breg(X86_64::RBP, i64::from(ss_offset) + 16); @@ -482,6 +398,17 @@ fn translate_loc( None } } - _ => None, + LabelValueLoc::ValueLoc(ValueLoc::Unassigned) => unreachable!(), + LabelValueLoc::Reg(reg) => { + let machine_reg = isa.map_regalloc_reg_to_dwarf(reg).unwrap(); + let mut expr = Expression::new(); + expr.op_reg(gimli::Register(machine_reg)); + Some(expr) + } + LabelValueLoc::SPOffset(offset) => { + let mut expr = Expression::new(); + expr.op_breg(X86_64::RSP, offset); + Some(expr) + } } } From 2d3603ff294c52fcc0be4da41db7a5f819395c81 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 3 Feb 2021 11:55:17 +0100 Subject: [PATCH 07/61] Update dependencies --- Cargo.lock | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 99d9bbf18922c..ca2295dac21e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,12 +29,6 @@ version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" -[[package]] -name = "cfg-if" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" - [[package]] name = "cfg-if" version = "1.0.0" @@ -155,7 +149,7 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81156fece84ab6a9f2afdb109ce3ae577e42b1228441eded99bd77f627953b1a" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", ] [[package]] @@ -212,9 +206,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.82" +version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89203f3fba0a3795506acaad8ebce3c80c0af93f994d5a1d7a0b1eeb23271929" +checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" [[package]] name = "libloading" @@ -222,17 +216,17 @@ version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "winapi", ] [[package]] name = "log" -version = "0.4.13" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcf3805d4480bb5b86070dcfeb9e2cb2ebc148adb753c5cca5f884d1d65a42b2" +checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", ] [[package]] @@ -327,9 +321,9 @@ checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" [[package]] name = "syn" -version = "1.0.58" +version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc60a3d73ea6594cd712d830cc1f0390fd71542d8c8cd24e70cc54cdfd5e05d5" +checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081" dependencies = [ "proc-macro2", "quote", From decb9b2880355e63aa9529c3c0f9b4c02fdc4730 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 3 Feb 2021 12:21:22 +0100 Subject: [PATCH 08/61] Rustfmt --- src/debuginfo/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 28adbe2fdb6e2..97704903b96c4 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -383,7 +383,7 @@ fn translate_loc( stack_slots: &StackSlots, ) -> Option { match loc { - LabelValueLoc::ValueLoc( ValueLoc::Reg(reg)) => { + LabelValueLoc::ValueLoc(ValueLoc::Reg(reg)) => { let machine_reg = isa.map_dwarf_register(reg).unwrap(); let mut expr = Expression::new(); expr.op_reg(gimli::Register(machine_reg)); @@ -407,8 +407,8 @@ fn translate_loc( } LabelValueLoc::SPOffset(offset) => { let mut expr = Expression::new(); - expr.op_breg(X86_64::RSP, offset); - Some(expr) + expr.op_breg(X86_64::RSP, offset); + Some(expr) } } } From 176965f51727ab539411ccd5520f5b693142bcce Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 12:30:06 +0100 Subject: [PATCH 09/61] Rustup to rustc 1.52.0-nightly (0fc6756b4 2021-02-08) --- build_sysroot/Cargo.lock | 4 ++-- rust-toolchain | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index d29ab8c18033d..6b35dec0058aa 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -139,9 +139,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.85" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" dependencies = [ "rustc-std-workspace-core", ] diff --git a/rust-toolchain b/rust-toolchain index de8e5f71cdc5b..6d8303f76f8ce 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-02-02 +nightly-2021-02-09 From 6fe5eace3c4c573b91fb751873d1ec8d06e2bccf Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 12:34:13 +0100 Subject: [PATCH 10/61] Update Cranelift --- Cargo.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ca2295dac21e1..85d28c6fd515d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,7 +38,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "cranelift-entity", ] @@ -46,7 +46,7 @@ dependencies = [ [[package]] name = "cranelift-codegen" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "byteorder", "cranelift-bforest", @@ -64,7 +64,7 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -73,17 +73,17 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" [[package]] name = "cranelift-entity" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" [[package]] name = "cranelift-frontend" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "cranelift-codegen", "log", @@ -94,7 +94,7 @@ dependencies = [ [[package]] name = "cranelift-jit" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "anyhow", "cranelift-codegen", @@ -112,7 +112,7 @@ dependencies = [ [[package]] name = "cranelift-module" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "anyhow", "cranelift-codegen", @@ -124,7 +124,7 @@ dependencies = [ [[package]] name = "cranelift-native" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "cranelift-codegen", "target-lexicon", @@ -133,7 +133,7 @@ dependencies = [ [[package]] name = "cranelift-object" version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#256cc8a5185c8d2ee82838fe2b12c8672baa69a3" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" dependencies = [ "anyhow", "cranelift-codegen", From bfcf97bd8360931eb088d65f247fc9e1016f8199 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 12:40:43 +0100 Subject: [PATCH 11/61] Update libc and libloading --- Cargo.lock | 8 ++++---- Cargo.toml | 2 +- src/driver/jit.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85d28c6fd515d..5659729838d54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -206,15 +206,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.85" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "libloading" -version = "0.6.7" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" +checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" dependencies = [ "cfg-if", "winapi", diff --git a/Cargo.toml b/Cargo.toml index 41e23b51afaff..bb1116484b692 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ object = { version = "0.23.0", default-features = false, features = ["std", "rea ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "do_not_remove_cg_clif_ranlib" } indexmap = "1.0.2" -libloading = { version = "0.6.0", optional = true } +libloading = { version = "0.7.0", optional = true } smallvec = "1.6.1" # Uncomment to use local checkout of cranelift diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 2d14ff2c0221d..6c73fa56fd48b 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -210,7 +210,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { let mut imported_symbols = Vec::new(); for path in dylib_paths { use object::{Object, ObjectSymbol}; - let lib = libloading::Library::new(&path).unwrap(); + let lib = unsafe { libloading::Library::new(&path).unwrap() }; let obj = std::fs::read(path).unwrap(); let obj = object::File::parse(&obj).unwrap(); imported_symbols.extend(obj.dynamic_symbols().filter_map(|symbol| { From c5dff34ae97007ed5a6f53b9b8c71b4d40cde997 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 13:19:19 +0100 Subject: [PATCH 12/61] Revert "Update libc and libloading" This reverts commit bfcf97bd8360931eb088d65f247fc9e1016f8199. It causes a "can't resolve symbol" crash on macOS --- Cargo.lock | 8 ++++---- Cargo.toml | 2 +- src/driver/jit.rs | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5659729838d54..85d28c6fd515d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -206,15 +206,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.86" +version = "0.2.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" +checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" [[package]] name = "libloading" -version = "0.7.0" +version = "0.6.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f84d96438c15fcd6c3f244c8fce01d1e2b9c6b5623e9c711dc9286d8fc92d6a" +checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883" dependencies = [ "cfg-if", "winapi", diff --git a/Cargo.toml b/Cargo.toml index bb1116484b692..41e23b51afaff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ object = { version = "0.23.0", default-features = false, features = ["std", "rea ar = { git = "https://github.com/bjorn3/rust-ar.git", branch = "do_not_remove_cg_clif_ranlib" } indexmap = "1.0.2" -libloading = { version = "0.7.0", optional = true } +libloading = { version = "0.6.0", optional = true } smallvec = "1.6.1" # Uncomment to use local checkout of cranelift diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 6c73fa56fd48b..2d14ff2c0221d 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -210,7 +210,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { let mut imported_symbols = Vec::new(); for path in dylib_paths { use object::{Object, ObjectSymbol}; - let lib = unsafe { libloading::Library::new(&path).unwrap() }; + let lib = libloading::Library::new(&path).unwrap(); let obj = std::fs::read(path).unwrap(); let obj = object::File::parse(&obj).unwrap(); imported_symbols.extend(obj.dynamic_symbols().filter_map(|symbol| { From 260a5d4d640b286a0ec52fb8e52e357954d024e9 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Tue, 9 Feb 2021 17:30:37 -0500 Subject: [PATCH 13/61] Stabilize str_split_once --- src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 170750461cace..1480ab2513378 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,7 @@ associated_type_bounds, never_type, try_blocks, - hash_drain_filter, - str_split_once + hash_drain_filter )] #![warn(rust_2018_idioms)] #![warn(unused_lifetimes)] From 94aac0af59d66bc0007149c6eb55039b8d440a54 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 12 Feb 2021 15:39:58 +0000 Subject: [PATCH 14/61] Pass around BackendConfig --- src/driver/aot.rs | 12 ++++++++---- src/driver/jit.rs | 21 +++++++++++++++------ src/driver/mod.rs | 8 ++++---- src/lib.rs | 8 ++++++-- 4 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/driver/aot.rs b/src/driver/aot.rs index df89883f0bbb7..4ae7b42fa664a 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -14,7 +14,7 @@ use rustc_session::config::{DebugInfo, OutputType}; use cranelift_object::{ObjectModule, ObjectProduct}; -use crate::prelude::*; +use crate::{prelude::*, BackendConfig}; use crate::backend::AddConstructor; @@ -117,7 +117,10 @@ fn reuse_workproduct_for_cgu( } } -fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodegenResult { +fn module_codegen( + tcx: TyCtxt<'_>, + (backend_config, cgu_name): (BackendConfig, rustc_span::Symbol), +) -> ModuleCodegenResult { let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); @@ -148,9 +151,9 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege let mut cx = crate::CodegenCx::new( tcx, + backend_config, module, tcx.sess.opts.debuginfo != DebugInfo::None, - true, ); super::predefine_mono_items(&mut cx, &mono_items); for (mono_item, (linkage, visibility)) in mono_items { @@ -202,6 +205,7 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege pub(super) fn run_aot( tcx: TyCtxt<'_>, + backend_config: BackendConfig, metadata: EncodedMetadata, need_metadata_module: bool, ) -> Box<(CodegenResults, FxHashMap)> { @@ -242,7 +246,7 @@ pub(super) fn run_aot( let (ModuleCodegenResult(module, work_product), _) = tcx.dep_graph.with_task( dep_node, tcx, - cgu.name(), + (backend_config, cgu.name()), module_codegen, rustc_middle::dep_graph::hash_result, ); diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 2d14ff2c0221d..841b496dd1c83 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -10,14 +10,15 @@ use rustc_middle::mir::mono::MonoItem; use cranelift_jit::{JITBuilder, JITModule}; -use crate::prelude::*; +use crate::{prelude::*, BackendConfig}; use crate::{CodegenCx, CodegenMode}; thread_local! { + pub static BACKEND_CONFIG: RefCell> = RefCell::new(None); pub static CURRENT_MODULE: RefCell> = RefCell::new(None); } -pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { +pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { if !tcx.sess.opts.output_types.should_codegen() { tcx.sess.fatal("JIT mode doesn't work with `cargo check`."); } @@ -46,7 +47,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { crate::build_isa(tcx.sess), cranelift_module::default_libcall_names(), ); - jit_builder.hotswap(matches!(codegen_mode, CodegenMode::JitLazy)); + jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy)); jit_builder.symbols(imported_symbols); let mut jit_module = JITModule::new(jit_builder); assert_eq!(pointer_ty(tcx), jit_module.target_config().pointer_type()); @@ -74,14 +75,14 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { .into_iter() .collect::>(); - let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false); + let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); super::time(tcx, "codegen mono items", || { super::predefine_mono_items(&mut cx, &mono_items); for (mono_item, (linkage, visibility)) in mono_items { let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); match mono_item { - MonoItem::Fn(inst) => match codegen_mode { + MonoItem::Fn(inst) => match backend_config.codegen_mode { CodegenMode::Aot => unreachable!(), CodegenMode::Jit => { cx.tcx.sess.time("codegen fn", || { @@ -137,6 +138,12 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { // useful as some dynamic linkers use it as a marker to jump over. argv.push(std::ptr::null()); + BACKEND_CONFIG.with(|tls_backend_config| { + assert!(tls_backend_config + .borrow_mut() + .replace(backend_config) + .is_none()) + }); CURRENT_MODULE .with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none())); @@ -154,7 +161,9 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8 CURRENT_MODULE.with(|jit_module| { let mut jit_module = jit_module.borrow_mut(); let jit_module = jit_module.as_mut().unwrap(); - let mut cx = crate::CodegenCx::new(tcx, jit_module, false, false); + let backend_config = + BACKEND_CONFIG.with(|backend_config| backend_config.borrow().clone().unwrap()); + let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); let name = tcx.symbol_name(instance).name.to_string(); let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), instance); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 2497f9dfdfbcf..360810cb58e18 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -17,12 +17,12 @@ pub(crate) fn codegen_crate( tcx: TyCtxt<'_>, metadata: EncodedMetadata, need_metadata_module: bool, - config: crate::BackendConfig, + backend_config: crate::BackendConfig, ) -> Box { tcx.sess.abort_if_errors(); - match config.codegen_mode { - CodegenMode::Aot => aot::run_aot(tcx, metadata, need_metadata_module), + match backend_config.codegen_mode { + CodegenMode::Aot => aot::run_aot(tcx, backend_config, metadata, need_metadata_module), CodegenMode::Jit | CodegenMode::JitLazy => { let is_executable = tcx .sess @@ -33,7 +33,7 @@ pub(crate) fn codegen_crate( } #[cfg(feature = "jit")] - let _: ! = jit::run_jit(tcx, config.codegen_mode); + let _: ! = jit::run_jit(tcx, backend_config); #[cfg(not(feature = "jit"))] tcx.sess diff --git a/src/lib.rs b/src/lib.rs index 170750461cace..119716268c115 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,8 +142,12 @@ struct CodegenCx<'tcx, M: Module> { } impl<'tcx, M: Module> CodegenCx<'tcx, M> { - fn new(tcx: TyCtxt<'tcx>, module: M, debug_info: bool, pic_eh_frame: bool) -> Self { - let unwind_context = UnwindContext::new(tcx, module.isa(), pic_eh_frame); + fn new(tcx: TyCtxt<'tcx>, backend_config: BackendConfig, module: M, debug_info: bool) -> Self { + let unwind_context = UnwindContext::new( + tcx, + module.isa(), + matches!(backend_config.codegen_mode, CodegenMode::Aot), + ); let debug_context = if debug_info { Some(DebugContext::new(tcx, module.isa())) } else { From 658d12408bfec3b499f24ccdffbec9e8679e87a1 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 12 Feb 2021 15:40:08 +0000 Subject: [PATCH 15/61] Rustfmt --- src/abi/mod.rs | 4 +--- src/abi/pass_mode.rs | 10 ++++------ 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index b2647e6c8d384..32cfb58bd3556 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -38,9 +38,7 @@ fn clif_sig_from_fn_abi<'tcx>( | Conv::X86VectorCall | Conv::AmdGpuKernel | Conv::AvrInterrupt - | Conv::AvrNonBlockingInterrupt => { - todo!("{:?}", fn_abi.conv) - } + | Conv::AvrNonBlockingInterrupt => todo!("{:?}", fn_abi.conv), }; let inputs = fn_abi .args diff --git a/src/abi/pass_mode.rs b/src/abi/pass_mode.rs index 1202c23dbe7b3..9dd1e6700291c 100644 --- a/src/abi/pass_mode.rs +++ b/src/abi/pass_mode.rs @@ -98,12 +98,10 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { match self.mode { PassMode::Ignore => smallvec![], PassMode::Direct(attrs) => match &self.layout.abi { - Abi::Scalar(scalar) => { - smallvec![apply_arg_attrs_to_abi_param( - AbiParam::new(scalar_to_clif_type(tcx, scalar.clone())), - attrs - )] - } + Abi::Scalar(scalar) => smallvec![apply_arg_attrs_to_abi_param( + AbiParam::new(scalar_to_clif_type(tcx, scalar.clone())), + attrs + )], Abi::Vector { .. } => { let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap(); smallvec![AbiParam::new(vector_ty)] From 9be3936e955139c010c3502670f67ec92440ed0c Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 14 Feb 2021 14:43:23 +0100 Subject: [PATCH 16/61] Use #!/usr/bin/env bash FreeBSD doesn't have /bin/bash --- build.sh | 2 +- build_sysroot/build_sysroot.sh | 2 +- build_sysroot/prepare_sysroot_src.sh | 2 +- clean_all.sh | 2 +- prepare.sh | 2 +- scripts/cargo.sh | 2 +- scripts/rustup.sh | 2 +- scripts/test_bootstrap.sh | 2 +- scripts/tests.sh | 2 +- test.sh | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/build.sh b/build.sh index 598ce35eceaac..19b7344b163b0 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e # Settings diff --git a/build_sysroot/build_sysroot.sh b/build_sysroot/build_sysroot.sh index 282ce4a582c4b..636aa5f3f3dc2 100755 --- a/build_sysroot/build_sysroot.sh +++ b/build_sysroot/build_sysroot.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # Requires the CHANNEL env var to be set to `debug` or `release.` diff --git a/build_sysroot/prepare_sysroot_src.sh b/build_sysroot/prepare_sysroot_src.sh index d3b87e02ba891..d6e012148ac7e 100755 --- a/build_sysroot/prepare_sysroot_src.sh +++ b/build_sysroot/prepare_sysroot_src.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e cd "$(dirname "$0")" diff --git a/clean_all.sh b/clean_all.sh index b47efe72bce03..a7bbeb05cac5a 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -1,4 +1,4 @@ -#!/bin/bash --verbose +#!/usr/bin/env bash set -e rm -rf target/ build/ build_sysroot/{sysroot_src/,target/,compiler-builtins/} perf.data{,.old} diff --git a/prepare.sh b/prepare.sh index 08e7cb1802999..ee995ffcfa9f7 100755 --- a/prepare.sh +++ b/prepare.sh @@ -1,4 +1,4 @@ -#!/bin/bash --verbose +#!/usr/bin/env bash set -e rustup component add rust-src rustc-dev llvm-tools-preview diff --git a/scripts/cargo.sh b/scripts/cargo.sh index a3d6d303057b8..669d2d45b71b5 100755 --- a/scripts/cargo.sh +++ b/scripts/cargo.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash dir=$(dirname "$0") source "$dir/config.sh" diff --git a/scripts/rustup.sh b/scripts/rustup.sh index 430f5c469b4b2..694945a87c268 100755 --- a/scripts/rustup.sh +++ b/scripts/rustup.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e diff --git a/scripts/test_bootstrap.sh b/scripts/test_bootstrap.sh index 15f97c867a1ec..5bc4548844468 100755 --- a/scripts/test_bootstrap.sh +++ b/scripts/test_bootstrap.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e cd "$(dirname "$0")/../" diff --git a/scripts/tests.sh b/scripts/tests.sh index d37b57babe612..cfc0c3fe9bd46 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e diff --git a/test.sh b/test.sh index 5ab10e0e905c7..e222adc7b8058 100755 --- a/test.sh +++ b/test.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e ./build.sh --sysroot none "$@" From 87ecdaa8482b7bcdc5a0adabea1cfb3f493705fa Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 14 Feb 2021 15:09:01 +0100 Subject: [PATCH 17/61] Recognize FreeBSD as OS --- build.sh | 4 ++-- scripts/config.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sh b/build.sh index 19b7344b163b0..52bc21d0a1760 100755 --- a/build.sh +++ b/build.sh @@ -34,13 +34,13 @@ done # Build cg_clif unset CARGO_TARGET_DIR unamestr=$(uname) -if [[ "$unamestr" == 'Linux' ]]; then +if [[ "$unamestr" == 'Linux' || "$unamestr" == "FreeBSD" ]]; then export RUSTFLAGS='-Clink-arg=-Wl,-rpath=$ORIGIN/../lib '$RUSTFLAGS elif [[ "$unamestr" == 'Darwin' ]]; then export RUSTFLAGS='-Csplit-debuginfo=unpacked -Clink-arg=-Wl,-rpath,@loader_path/../lib -Zosx-rpath-install-name '$RUSTFLAGS dylib_ext='dylib' else - echo "Unsupported os" + echo "Unsupported os $unamestr" exit 1 fi if [[ "$CHANNEL" == "release" ]]; then diff --git a/scripts/config.sh b/scripts/config.sh index 834708aa9a6fb..e1f54e4a8b551 100644 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -3,7 +3,7 @@ set -e unamestr=$(uname) -if [[ "$unamestr" == 'Linux' ]]; then +if [[ "$unamestr" == 'Linux' || "$unamestr" == 'FreeBSD' ]]; then dylib_ext='so' elif [[ "$unamestr" == 'Darwin' ]]; then dylib_ext='dylib' From 47bc1db236e5117021aa2eda575436bf90f889b2 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 14 Feb 2021 12:54:00 +0100 Subject: [PATCH 18/61] Test FreeBSD on Cirrus CI --- .cirrus.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .cirrus.yml diff --git a/.cirrus.yml b/.cirrus.yml new file mode 100644 index 0000000000000..e173df423a765 --- /dev/null +++ b/.cirrus.yml @@ -0,0 +1,25 @@ +task: + name: freebsd + freebsd_instance: + image: freebsd-12-1-release-amd64 + setup_rust_script: + - pkg install -y curl git bash + - curl https://sh.rustup.rs -sSf --output rustup.sh + - sh rustup.sh --default-toolchain none -y --profile=minimal + cargo_bin_cache: + folder: ~/.cargo/bin + target_cache: + folder: target + prepare_script: + - . $HOME/.cargo/env + - git config --global user.email "user@example.com" + - git config --global user.name "User" + - ./prepare.sh + test_script: + - . $HOME/.cargo/env + - # Enable backtraces for easier debugging + - export RUST_BACKTRACE=1 + - # Reduce amount of benchmark runs as they are slow + - export COMPILE_RUNS=2 + - export RUN_RUNS=2 + - ./test.sh From fe8d11bb99da0c4869f985137f40ab337a955890 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 30 Jan 2021 19:18:48 +0100 Subject: [PATCH 19/61] Use an ItemId inside mir::GlobalAsm. --- src/driver/aot.rs | 4 ++-- src/driver/jit.rs | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/driver/aot.rs b/src/driver/aot.rs index df89883f0bbb7..c9e503a43b9e1 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -164,8 +164,8 @@ fn module_codegen(tcx: TyCtxt<'_>, cgu_name: rustc_span::Symbol) -> ModuleCodege MonoItem::Static(def_id) => { crate::constant::codegen_static(&mut cx.constants_cx, def_id) } - MonoItem::GlobalAsm(hir_id) => { - let item = cx.tcx.hir().expect_item(hir_id); + MonoItem::GlobalAsm(item_id) => { + let item = cx.tcx.hir().item(item_id); if let rustc_hir::ItemKind::GlobalAsm(rustc_hir::GlobalAsm { asm }) = item.kind { cx.global_asm.push_str(&*asm.as_str()); cx.global_asm.push_str("\n\n"); diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 2d14ff2c0221d..f784d8d27cc74 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -93,10 +93,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, codegen_mode: CodegenMode) -> ! { MonoItem::Static(def_id) => { crate::constant::codegen_static(&mut cx.constants_cx, def_id); } - MonoItem::GlobalAsm(hir_id) => { - let item = cx.tcx.hir().expect_item(hir_id); - tcx.sess - .span_fatal(item.span, "Global asm is not supported in JIT mode"); + MonoItem::GlobalAsm(item_id) => { + let item = cx.tcx.hir().item(item_id); + tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode"); } } } From c39cb46da7a51c92f1911d4df5f4bab6450cd0c7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 18 Feb 2021 17:59:13 +0100 Subject: [PATCH 20/61] Update Cranelift --- Cargo.lock | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85d28c6fd515d..90b5fcfb3ec10 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,16 +37,16 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "byteorder", "cranelift-bforest", @@ -63,8 +63,8 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -72,18 +72,18 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" [[package]] name = "cranelift-entity" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" [[package]] name = "cranelift-frontend" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "cranelift-codegen", "log", @@ -93,8 +93,8 @@ dependencies = [ [[package]] name = "cranelift-jit" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "anyhow", "cranelift-codegen", @@ -111,8 +111,8 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "anyhow", "cranelift-codegen", @@ -123,8 +123,8 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "cranelift-codegen", "target-lexicon", @@ -132,8 +132,8 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.69.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#5b55ba80537de25d15b66ac04914ddc170d33eec" +version = "0.70.0" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" dependencies = [ "anyhow", "cranelift-codegen", From 92f765fce96b6344ccfe9b288bbd8b652f5ad0ef Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 13:47:29 +0100 Subject: [PATCH 21/61] Remove support for x86 oldBE --- .github/workflows/main.yml | 5 +--- Cargo.toml | 3 +-- build.sh | 10 +++----- src/base.rs | 14 +++++------ src/debuginfo/line_info.rs | 49 ++++++++++---------------------------- src/debuginfo/mod.rs | 7 +----- src/lib.rs | 6 +---- 7 files changed, 26 insertions(+), 68 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 20c58423a0c50..e6d3375fb1bab 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,9 +12,6 @@ jobs: fail-fast: false matrix: os: [ubuntu-latest, macos-latest] - env: - - BACKEND: "" - - BACKEND: --oldbe steps: - uses: actions/checkout@v2 @@ -54,7 +51,7 @@ jobs: export COMPILE_RUNS=2 export RUN_RUNS=2 - ./test.sh $BACKEND + ./test.sh - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz build diff --git a/Cargo.toml b/Cargo.toml index 41e23b51afaff..9861af1f8eae2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind", "x86", "x64"] } +cranelift-codegen = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", features = ["unwind", "x64"] } cranelift-frontend = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" } cranelift-module = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main" } cranelift-jit = { git = "https://github.com/bytecodealliance/wasmtime/", branch = "main", optional = true } @@ -38,7 +38,6 @@ smallvec = "1.6.1" default = ["jit", "inline_asm"] jit = ["cranelift-jit", "libloading"] inline_asm = [] -oldbe = [] [profile.dev] # By compiling dependencies with optimizations, performing tests gets much faster. diff --git a/build.sh b/build.sh index 52bc21d0a1760..090349e54b148 100755 --- a/build.sh +++ b/build.sh @@ -5,7 +5,6 @@ set -e export CHANNEL="release" build_sysroot="clif" target_dir='build' -oldbe='' while [[ $# != 0 ]]; do case $1 in "--debug") @@ -19,12 +18,9 @@ while [[ $# != 0 ]]; do target_dir=$2 shift ;; - "--oldbe") - oldbe='--features oldbe' - ;; *) echo "Unknown flag '$1'" - echo "Usage: ./build.sh [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--oldbe]" + echo "Usage: ./build.sh [--debug] [--sysroot none|clif|llvm] [--target-dir DIR]" exit 1 ;; esac @@ -44,9 +40,9 @@ else exit 1 fi if [[ "$CHANNEL" == "release" ]]; then - cargo build $oldbe --release + cargo build --release else - cargo build $oldbe + cargo build fi source scripts/ext_config.sh diff --git a/src/base.rs b/src/base.rs index 4842628a99da7..03889a4687f82 100644 --- a/src/base.rs +++ b/src/base.rs @@ -149,14 +149,12 @@ pub(crate) fn codegen_fn<'tcx>( &clif_comments, ); - if let Some(mach_compile_result) = &context.mach_compile_result { - if let Some(disasm) = &mach_compile_result.disasm { - crate::pretty_clif::write_ir_file( - tcx, - &format!("{}.vcode", tcx.symbol_name(instance).name), - |file| file.write_all(disasm.as_bytes()), - ) - } + if let Some(disasm) = &context.mach_compile_result.as_ref().unwrap().disasm { + crate::pretty_clif::write_ir_file( + tcx, + &format!("{}.vcode", tcx.symbol_name(instance).name), + |file| file.write_all(disasm.as_bytes()), + ) } // Define debuginfo for function diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index d226755d85de0..a40f963400c66 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -129,7 +129,6 @@ impl<'tcx> DebugContext<'tcx> { pub(super) fn create_debug_lines( &mut self, - isa: &dyn cranelift_codegen::isa::TargetIsa, symbol: usize, entry_id: UnitEntryId, context: &Context, @@ -138,7 +137,6 @@ impl<'tcx> DebugContext<'tcx> { ) -> CodeOffset { let tcx = self.tcx; let line_program = &mut self.dwarf.unit.line_program; - let func = &context.func; let line_strings = &mut self.dwarf.line_strings; let mut last_span = None; @@ -202,43 +200,22 @@ impl<'tcx> DebugContext<'tcx> { let mut func_end = 0; - if let Some(ref mcr) = &context.mach_compile_result { - for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() { - line_program.row().address_offset = u64::from(start); - if !loc.is_default() { - let source_info = *source_info_set.get_index(loc.bits() as usize).unwrap(); - create_row_for_span(line_program, source_info.span); - } else { - create_row_for_span(line_program, function_span); - } - func_end = end; - } - - line_program.end_sequence(u64::from(func_end)); - - func_end = mcr.buffer.total_size(); - } else { - let encinfo = isa.encoding_info(); - let mut blocks = func.layout.blocks().collect::>(); - blocks.sort_by_key(|block| func.offsets[*block]); // Ensure inst offsets always increase - - for block in blocks { - for (offset, inst, size) in func.inst_offsets(block, &encinfo) { - let srcloc = func.srclocs[inst]; - line_program.row().address_offset = u64::from(offset); - if !srcloc.is_default() { - let source_info = - *source_info_set.get_index(srcloc.bits() as usize).unwrap(); - create_row_for_span(line_program, source_info.span); - } else { - create_row_for_span(line_program, function_span); - } - func_end = offset + size; - } + let mcr = context.mach_compile_result.as_ref().unwrap(); + for &MachSrcLoc { start, end, loc } in mcr.buffer.get_srclocs_sorted() { + line_program.row().address_offset = u64::from(start); + if !loc.is_default() { + let source_info = *source_info_set.get_index(loc.bits() as usize).unwrap(); + create_row_for_span(line_program, source_info.span); + } else { + create_row_for_span(line_program, function_span); } - line_program.end_sequence(u64::from(func_end)); + func_end = end; } + line_program.end_sequence(u64::from(func_end)); + + let func_end = mcr.buffer.total_size(); + assert_ne!(func_end, 0); let entry = self.dwarf.unit.get_mut(entry_id); diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 97704903b96c4..87b1c5fb50e6d 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -252,18 +252,13 @@ impl<'tcx> DebugContext<'tcx> { AttributeValue::StringRef(name_id), ); - let end = - self.create_debug_lines(isa, symbol, entry_id, context, mir.span, source_info_set); + let end = self.create_debug_lines(symbol, entry_id, context, mir.span, source_info_set); self.unit_range_list.0.push(Range::StartLength { begin: Address::Symbol { symbol, addend: 0 }, length: u64::from(end), }); - if isa.get_mach_backend().is_some() { - return; // Not yet implemented for the AArch64 backend. - } - let func_entry = self.dwarf.unit.get_mut(entry_id); // Gdb requires both DW_AT_low_pc and DW_AT_high_pc. Otherwise the DW_TAG_subprogram is skipped. func_entry.set( diff --git a/src/lib.rs b/src/lib.rs index 119716268c115..f081a2d85a904 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,11 +343,7 @@ fn build_isa(sess: &Session) -> Box { let flags = settings::Flags::new(flags_builder); - let variant = if cfg!(feature = "oldbe") { - cranelift_codegen::isa::BackendVariant::Legacy - } else { - cranelift_codegen::isa::BackendVariant::MachInst - }; + let variant = cranelift_codegen::isa::BackendVariant::MachInst; let mut isa_builder = cranelift_codegen::isa::lookup_variant(target_triple, variant).unwrap(); // Don't use "haswell", as it implies `has_lzcnt`.macOS CI is still at Ivy Bridge EP, so `lzcnt` // is interpreted as `bsr`. From f2f5452089a6cf8eb611badf20118960030f6585 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 9 Feb 2021 14:36:25 +0100 Subject: [PATCH 22/61] Use real atomic instructions instead of a global lock --- scripts/config.sh | 2 +- src/atomic_shim.rs | 185 -------------------------------- src/backend.rs | 45 +------- src/driver/aot.rs | 42 +------- src/driver/jit.rs | 20 +--- src/intrinsics/mod.rs | 238 ++++++++++++++++++++++-------------------- src/lib.rs | 3 - src/main_shim.rs | 5 - 8 files changed, 129 insertions(+), 411 deletions(-) delete mode 100644 src/atomic_shim.rs diff --git a/scripts/config.sh b/scripts/config.sh index e1f54e4a8b551..c2ed2bf256d59 100644 --- a/scripts/config.sh +++ b/scripts/config.sh @@ -26,7 +26,7 @@ export RUSTC=$dir"/bin/cg_clif" export RUSTDOCFLAGS=$linker' -Cpanic=abort -Zpanic-abort-tests '\ '-Zcodegen-backend='$dir'/lib/librustc_codegen_cranelift.'$dylib_ext' --sysroot '$dir -# FIXME remove once the atomic shim is gone +# FIXME fix `#[linkage = "extern_weak"]` without this if [[ "$unamestr" == 'Darwin' ]]; then export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup" fi diff --git a/src/atomic_shim.rs b/src/atomic_shim.rs deleted file mode 100644 index 674e6d907510a..0000000000000 --- a/src/atomic_shim.rs +++ /dev/null @@ -1,185 +0,0 @@ -//! Atomic intrinsics are implemented using a global lock for now, as Cranelift doesn't support -//! atomic operations yet. - -// FIXME implement atomic instructions in Cranelift. - -use crate::prelude::*; - -#[cfg(all(feature = "jit", unix))] -#[no_mangle] -static mut __cg_clif_global_atomic_mutex: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; - -pub(crate) fn init_global_lock( - module: &mut impl Module, - bcx: &mut FunctionBuilder<'_>, - use_jit: bool, -) { - if use_jit { - // When using JIT, dylibs won't find the __cg_clif_global_atomic_mutex data object defined here, - // so instead we define it in the cg_clif dylib. - - return; - } - - let mut data_ctx = DataContext::new(); - data_ctx.define_zeroinit(1024); // 1024 bytes should be big enough on all platforms. - data_ctx.set_align(16); - let atomic_mutex = module - .declare_data( - "__cg_clif_global_atomic_mutex", - Linkage::Export, - true, - false, - ) - .unwrap(); - module.define_data(atomic_mutex, &data_ctx).unwrap(); - - let pthread_mutex_init = module - .declare_function( - "pthread_mutex_init", - Linkage::Import, - &cranelift_codegen::ir::Signature { - call_conv: module.target_config().default_call_conv, - params: vec![ - AbiParam::new( - module.target_config().pointer_type(), /* *mut pthread_mutex_t */ - ), - AbiParam::new( - module.target_config().pointer_type(), /* *const pthread_mutex_attr_t */ - ), - ], - returns: vec![AbiParam::new(types::I32 /* c_int */)], - }, - ) - .unwrap(); - - let pthread_mutex_init = module.declare_func_in_func(pthread_mutex_init, bcx.func); - - let atomic_mutex = module.declare_data_in_func(atomic_mutex, bcx.func); - let atomic_mutex = bcx - .ins() - .global_value(module.target_config().pointer_type(), atomic_mutex); - - let nullptr = bcx.ins().iconst(module.target_config().pointer_type(), 0); - - bcx.ins().call(pthread_mutex_init, &[atomic_mutex, nullptr]); -} - -pub(crate) fn init_global_lock_constructor( - module: &mut impl Module, - constructor_name: &str, -) -> FuncId { - let sig = Signature::new(CallConv::SystemV); - let init_func_id = module - .declare_function(constructor_name, Linkage::Export, &sig) - .unwrap(); - - let mut ctx = Context::new(); - ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig); - { - let mut func_ctx = FunctionBuilderContext::new(); - let mut bcx = FunctionBuilder::new(&mut ctx.func, &mut func_ctx); - - let block = bcx.create_block(); - bcx.switch_to_block(block); - - crate::atomic_shim::init_global_lock(module, &mut bcx, false); - - bcx.ins().return_(&[]); - bcx.seal_all_blocks(); - bcx.finalize(); - } - module - .define_function( - init_func_id, - &mut ctx, - &mut cranelift_codegen::binemit::NullTrapSink {}, - ) - .unwrap(); - - init_func_id -} - -pub(crate) fn lock_global_lock(fx: &mut FunctionCx<'_, '_, impl Module>) { - let atomic_mutex = fx - .cx - .module - .declare_data( - "__cg_clif_global_atomic_mutex", - Linkage::Import, - true, - false, - ) - .unwrap(); - - let pthread_mutex_lock = fx - .cx - .module - .declare_function( - "pthread_mutex_lock", - Linkage::Import, - &cranelift_codegen::ir::Signature { - call_conv: fx.cx.module.target_config().default_call_conv, - params: vec![AbiParam::new( - fx.cx.module.target_config().pointer_type(), /* *mut pthread_mutex_t */ - )], - returns: vec![AbiParam::new(types::I32 /* c_int */)], - }, - ) - .unwrap(); - - let pthread_mutex_lock = fx - .cx - .module - .declare_func_in_func(pthread_mutex_lock, fx.bcx.func); - - let atomic_mutex = fx.cx.module.declare_data_in_func(atomic_mutex, fx.bcx.func); - let atomic_mutex = fx - .bcx - .ins() - .global_value(fx.cx.module.target_config().pointer_type(), atomic_mutex); - - fx.bcx.ins().call(pthread_mutex_lock, &[atomic_mutex]); -} - -pub(crate) fn unlock_global_lock(fx: &mut FunctionCx<'_, '_, impl Module>) { - let atomic_mutex = fx - .cx - .module - .declare_data( - "__cg_clif_global_atomic_mutex", - Linkage::Import, - true, - false, - ) - .unwrap(); - - let pthread_mutex_unlock = fx - .cx - .module - .declare_function( - "pthread_mutex_unlock", - Linkage::Import, - &cranelift_codegen::ir::Signature { - call_conv: fx.cx.module.target_config().default_call_conv, - params: vec![AbiParam::new( - fx.cx.module.target_config().pointer_type(), /* *mut pthread_mutex_t */ - )], - returns: vec![AbiParam::new(types::I32 /* c_int */)], - }, - ) - .unwrap(); - - let pthread_mutex_unlock = fx - .cx - .module - .declare_func_in_func(pthread_mutex_unlock, fx.bcx.func); - - let atomic_mutex = fx.cx.module.declare_data_in_func(atomic_mutex, fx.bcx.func); - let atomic_mutex = fx - .bcx - .ins() - .global_value(fx.cx.module.target_config().pointer_type(), atomic_mutex); - - fx.bcx.ins().call(pthread_mutex_unlock, &[atomic_mutex]); -} diff --git a/src/backend.rs b/src/backend.rs index 0ce34c904bdcc..a5718244816d2 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -8,7 +8,7 @@ use rustc_session::Session; use cranelift_module::FuncId; use object::write::*; -use object::{RelocationEncoding, RelocationKind, SectionKind, SymbolFlags}; +use object::{RelocationEncoding, SectionKind, SymbolFlags}; use cranelift_object::{ObjectBuilder, ObjectModule, ObjectProduct}; @@ -118,49 +118,6 @@ impl WriteDebugInfo for ObjectProduct { } } -// FIXME remove once atomic instructions are implemented in Cranelift. -pub(crate) trait AddConstructor { - fn add_constructor(&mut self, func_id: FuncId); -} - -impl AddConstructor for ObjectProduct { - fn add_constructor(&mut self, func_id: FuncId) { - let symbol = self.function_symbol(func_id); - let segment = self - .object - .segment_name(object::write::StandardSegment::Data); - let init_array_section = - self.object - .add_section(segment.to_vec(), b".init_array".to_vec(), SectionKind::Data); - let address_size = self - .object - .architecture() - .address_size() - .expect("address_size must be known") - .bytes(); - self.object.append_section_data( - init_array_section, - &std::iter::repeat(0) - .take(address_size.into()) - .collect::>(), - 8, - ); - self.object - .add_relocation( - init_array_section, - object::write::Relocation { - offset: 0, - size: address_size * 8, - kind: RelocationKind::Absolute, - encoding: RelocationEncoding::Generic, - symbol, - addend: 0, - }, - ) - .unwrap(); - } -} - pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object)) -> Vec { let triple = crate::build_isa(sess).triple().clone(); diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 4ae7b42fa664a..8085a04184b2b 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -12,12 +12,10 @@ use rustc_middle::mir::mono::{CodegenUnit, MonoItem}; use rustc_session::cgu_reuse_tracker::CguReuse; use rustc_session::config::{DebugInfo, OutputType}; -use cranelift_object::{ObjectModule, ObjectProduct}; +use cranelift_object::ObjectModule; use crate::{prelude::*, BackendConfig}; -use crate::backend::AddConstructor; - fn new_module(tcx: TyCtxt<'_>, name: String) -> ObjectModule { let module = crate::backend::make_module(tcx.sess, name); assert_eq!(pointer_ty(tcx), module.target_config().pointer_type()); @@ -39,7 +37,6 @@ fn emit_module( module: ObjectModule, debug: Option>, unwind_context: UnwindContext<'_>, - map_product: impl FnOnce(ObjectProduct) -> ObjectProduct, ) -> ModuleCodegenResult { let mut product = module.finish(); @@ -49,8 +46,6 @@ fn emit_module( unwind_context.emit(&mut product); - let product = map_product(product); - let tmp_file = tcx .output_filenames(LOCAL_CRATE) .temp_path(OutputType::Object, Some(&name)); @@ -124,30 +119,7 @@ fn module_codegen( let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); - let mut module = new_module(tcx, cgu_name.as_str().to_string()); - - // Initialize the global atomic mutex using a constructor for proc-macros. - // FIXME implement atomic instructions in Cranelift. - let mut init_atomics_mutex_from_constructor = None; - if tcx - .sess - .crate_types() - .contains(&rustc_session::config::CrateType::ProcMacro) - { - if mono_items.iter().any(|(mono_item, _)| match mono_item { - rustc_middle::mir::mono::MonoItem::Static(def_id) => tcx - .symbol_name(Instance::mono(tcx, *def_id)) - .name - .contains("__rustc_proc_macro_decls_"), - _ => false, - }) { - init_atomics_mutex_from_constructor = - Some(crate::atomic_shim::init_global_lock_constructor( - &mut module, - &format!("{}_init_atomics_mutex", cgu_name.as_str()), - )); - } - } + let module = new_module(tcx, cgu_name.as_str().to_string()); let mut cx = crate::CodegenCx::new( tcx, @@ -180,7 +152,7 @@ fn module_codegen( } let (mut module, global_asm, debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize()); - crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context, false); + crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context); let codegen_result = emit_module( tcx, @@ -189,13 +161,6 @@ fn module_codegen( module, debug, unwind_context, - |mut product| { - if let Some(func_id) = init_atomics_mutex_from_constructor { - product.add_constructor(func_id); - } - - product - }, ); codegen_global_asm(tcx, &cgu.name().as_str(), &global_asm); @@ -275,7 +240,6 @@ pub(super) fn run_aot( allocator_module, None, allocator_unwind_context, - |product| product, ); if let Some((id, product)) = work_product { work_products.insert(id, product); diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 841b496dd1c83..4abfcfcd7b19e 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -23,24 +23,6 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { tcx.sess.fatal("JIT mode doesn't work with `cargo check`."); } - #[cfg(unix)] - unsafe { - // When not using our custom driver rustc will open us without the RTLD_GLOBAL flag, so - // __cg_clif_global_atomic_mutex will not be exported. We fix this by opening ourself again - // as global. - // FIXME remove once atomic_shim is gone - - let mut dl_info: libc::Dl_info = std::mem::zeroed(); - assert_ne!( - libc::dladdr(run_jit as *const libc::c_void, &mut dl_info), - 0 - ); - assert_ne!( - libc::dlopen(dl_info.dli_fname, libc::RTLD_NOW | libc::RTLD_GLOBAL), - std::ptr::null_mut(), - ); - } - let imported_symbols = load_imported_symbols_for_jit(tcx); let mut jit_builder = JITBuilder::with_isa( @@ -111,7 +93,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { tcx.sess.fatal("Inline asm is not supported in JIT mode"); } - crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context, true); + crate::main_shim::maybe_create_entry_wrapper(tcx, &mut jit_module, &mut unwind_context); crate::allocator::codegen(tcx, &mut jit_module, &mut unwind_context); tcx.sess.abort_if_errors(); diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 8946ac43bc65a..4aed09d2b143b 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -9,6 +9,7 @@ pub(crate) use cpuid::codegen_cpuid_call; pub(crate) use llvm::codegen_llvm_intrinsic_call; use crate::prelude::*; +use cranelift_codegen::ir::AtomicRmwOp; use rustc_middle::ty::print::with_no_trimmed_paths; macro intrinsic_pat { @@ -112,38 +113,6 @@ macro call_intrinsic_match { } } -macro atomic_binop_return_old($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) { - crate::atomic_shim::lock_global_lock($fx); - - let clif_ty = $fx.clif_type($T).unwrap(); - let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0); - let new = $fx.bcx.ins().$op(old, $src); - $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0); - $ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T))); - - crate::atomic_shim::unlock_global_lock($fx); -} - -macro atomic_minmax($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) { - crate::atomic_shim::lock_global_lock($fx); - - // Read old - let clif_ty = $fx.clif_type($T).unwrap(); - let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0); - - // Compare - let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src); - let new = $fx.bcx.ins().select(is_eq, old, $src); - - // Write new - $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0); - - let ret_val = CValue::by_val(old, $ret.layout()); - $ret.write_cvalue($fx, ret_val); - - crate::atomic_shim::unlock_global_lock($fx); -} - macro validate_atomic_type($fx:ident, $intrinsic:ident, $span:ident, $ty:expr) { match $ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} @@ -912,136 +881,175 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; _ if intrinsic.starts_with("atomic_fence"), () { - crate::atomic_shim::lock_global_lock(fx); - crate::atomic_shim::unlock_global_lock(fx); + fx.bcx.ins().fence(); }; _ if intrinsic.starts_with("atomic_singlethreadfence"), () { - crate::atomic_shim::lock_global_lock(fx); - crate::atomic_shim::unlock_global_lock(fx); + // FIXME use a compiler fence once Cranelift supports it + fx.bcx.ins().fence(); }; - _ if intrinsic.starts_with("atomic_load"), (c ptr) { - crate::atomic_shim::lock_global_lock(fx); + _ if intrinsic.starts_with("atomic_load"), (v ptr) { + validate_atomic_type!(fx, intrinsic, span, T); + let ty = fx.clif_type(T).unwrap(); - let inner_layout = - fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty); - validate_atomic_type!(fx, intrinsic, span, inner_layout.ty); - let val = CValue::by_ref(Pointer::new(ptr.load_scalar(fx)), inner_layout); - ret.write_cvalue(fx, val); + let val = fx.bcx.ins().atomic_load(ty, MemFlags::trusted(), ptr); - crate::atomic_shim::unlock_global_lock(fx); + let val = CValue::by_val(val, fx.layout_of(T)); + ret.write_cvalue(fx, val); }; _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) { validate_atomic_type!(fx, intrinsic, span, val.layout().ty); - crate::atomic_shim::lock_global_lock(fx); - - let dest = CPlace::for_ptr(Pointer::new(ptr), val.layout()); - dest.write_cvalue(fx, val); + let val = val.load_scalar(fx); - crate::atomic_shim::unlock_global_lock(fx); + fx.bcx.ins().atomic_store(MemFlags::trusted(), val, ptr); }; - _ if intrinsic.starts_with("atomic_xchg"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, T); - - crate::atomic_shim::lock_global_lock(fx); + _ if intrinsic.starts_with("atomic_xchg"), (v ptr, c new) { + let layout = new.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); - // Read old - let clif_ty = fx.clif_type(T).unwrap(); - let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0); - ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T))); + let new = new.load_scalar(fx); - // Write new - let dest = CPlace::for_ptr(Pointer::new(ptr), src.layout()); - dest.write_cvalue(fx, src); + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xchg, ptr, new); - crate::atomic_shim::unlock_global_lock(fx); + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_cxchg"), (v ptr, c test_old, c new) { // both atomic_cxchg_* and atomic_cxchgweak_* - validate_atomic_type!(fx, intrinsic, span, T); + _ if intrinsic.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); let test_old = test_old.load_scalar(fx); let new = new.load_scalar(fx); - crate::atomic_shim::lock_global_lock(fx); - - // Read old - let clif_ty = fx.clif_type(T).unwrap(); - let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0); - - // Compare + let old = fx.bcx.ins().atomic_cas(MemFlags::trusted(), ptr, test_old, new); let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old); - let new = fx.bcx.ins().select(is_eq, new, old); // Keep old if not equal to test_old - - // Write new - fx.bcx.ins().store(MemFlags::new(), new, ptr, 0); let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout()); - ret.write_cvalue(fx, ret_val); - - crate::atomic_shim::unlock_global_lock(fx); + ret.write_cvalue(fx, ret_val) }; - _ if intrinsic.starts_with("atomic_xadd"), (v ptr, c amount) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_xadd"), (v ptr, c amount) { + let layout = amount.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let amount = amount.load_scalar(fx); - atomic_binop_return_old! (fx, iadd(ptr, amount) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Add, ptr, amount); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_xsub"), (v ptr, c amount) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_xsub"), (v ptr, c amount) { + let layout = amount.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let amount = amount.load_scalar(fx); - atomic_binop_return_old! (fx, isub(ptr, amount) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Sub, ptr, amount); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_and"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_and"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_binop_return_old! (fx, band(ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::And, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_nand"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, T); + _ if intrinsic.starts_with("atomic_or"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); let src = src.load_scalar(fx); - crate::atomic_shim::lock_global_lock(fx); - - let clif_ty = fx.clif_type(T).unwrap(); - let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0); - let and = fx.bcx.ins().band(old, src); - let new = fx.bcx.ins().bnot(and); - fx.bcx.ins().store(MemFlags::new(), new, ptr, 0); - ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T))); + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Or, ptr, src); - crate::atomic_shim::unlock_global_lock(fx); + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_or"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_xor"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_binop_return_old! (fx, bor(ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Xor, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_xor"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + + // FIXME https://github.com/bytecodealliance/wasmtime/issues/2647 + _ if intrinsic.starts_with("atomic_nand"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_binop_return_old! (fx, bxor(ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Nand, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; + _ if intrinsic.starts_with("atomic_max"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); - _ if intrinsic.starts_with("atomic_max"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); let src = src.load_scalar(fx); - atomic_minmax!(fx, IntCC::SignedGreaterThan, (ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smax, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_umax"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_umax"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_minmax!(fx, IntCC::UnsignedGreaterThan, (ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umax, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_min"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_min"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_minmax!(fx, IntCC::SignedLessThan, (ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Smin, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; - _ if intrinsic.starts_with("atomic_umin"), (v ptr, c src) { - validate_atomic_type!(fx, intrinsic, span, ret.layout().ty); + _ if intrinsic.starts_with("atomic_umin"), (v ptr, c src) { + let layout = src.layout(); + validate_atomic_type!(fx, intrinsic, span, layout.ty); + let ty = fx.clif_type(layout.ty).unwrap(); + let src = src.load_scalar(fx); - atomic_minmax!(fx, IntCC::UnsignedLessThan, (ptr, src) -> ret); + + let old = fx.bcx.ins().atomic_rmw(ty, MemFlags::trusted(), AtomicRmwOp::Umin, ptr, src); + + let old = CValue::by_val(old, layout); + ret.write_cvalue(fx, old); }; minnumf32, (v a, v b) { diff --git a/src/lib.rs b/src/lib.rs index f081a2d85a904..417934f760326 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,8 +12,6 @@ #![warn(unused_lifetimes)] #![warn(unreachable_pub)] -#[cfg(feature = "jit")] -extern crate libc; extern crate snap; #[macro_use] extern crate rustc_middle; @@ -54,7 +52,6 @@ mod abi; mod allocator; mod analyze; mod archive; -mod atomic_shim; mod backend; mod base; mod cast; diff --git a/src/main_shim.rs b/src/main_shim.rs index b193cea877dad..4ea9b9f995303 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -9,7 +9,6 @@ pub(crate) fn maybe_create_entry_wrapper( tcx: TyCtxt<'_>, module: &mut impl Module, unwind_context: &mut UnwindContext<'_>, - use_jit: bool, ) { let (main_def_id, use_start_lang_item) = match tcx.entry_fn(LOCAL_CRATE) { Some((def_id, entry_ty)) => ( @@ -33,7 +32,6 @@ pub(crate) fn maybe_create_entry_wrapper( unwind_context, main_def_id, use_start_lang_item, - use_jit, ); fn create_entry_fn( @@ -42,7 +40,6 @@ pub(crate) fn maybe_create_entry_wrapper( unwind_context: &mut UnwindContext<'_>, rust_main_def_id: DefId, use_start_lang_item: bool, - use_jit: bool, ) { let main_ret_ty = tcx.fn_sig(rust_main_def_id).output(); // Given that `main()` has no arguments, @@ -86,8 +83,6 @@ pub(crate) fn maybe_create_entry_wrapper( let arg_argc = bcx.append_block_param(block, m.target_config().pointer_type()); let arg_argv = bcx.append_block_param(block, m.target_config().pointer_type()); - crate::atomic_shim::init_global_lock(m, &mut bcx, use_jit); - let main_func_ref = m.declare_func_in_func(main_func_id, &mut bcx.func); let call_inst = if use_start_lang_item { From 537db9a1ef2ad5f195378c7f64676b147f56f57a Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Thu, 18 Feb 2021 18:32:40 +0100 Subject: [PATCH 23/61] Disable 128bit atomic operations on macOS --- build_sysroot/prepare_sysroot_src.sh | 2 +- ...ins-Disable-128bit-atomic-operations.patch | 48 ++++++++ ...027-Disable-128bit-atomic-operations.patch | 103 ++++++++++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 crate_patches/0002-compiler-builtins-Disable-128bit-atomic-operations.patch create mode 100644 patches/0027-Disable-128bit-atomic-operations.patch diff --git a/build_sysroot/prepare_sysroot_src.sh b/build_sysroot/prepare_sysroot_src.sh index d6e012148ac7e..c90205db0fbd0 100755 --- a/build_sysroot/prepare_sysroot_src.sh +++ b/build_sysroot/prepare_sysroot_src.sh @@ -33,7 +33,7 @@ git clone https://github.com/rust-lang/compiler-builtins.git || echo "rust-lang/ pushd compiler-builtins git checkout -- . git checkout 0.1.39 -git apply ../../crate_patches/0001-compiler-builtins-Remove-rotate_left-from-Int.patch +git apply ../../crate_patches/000*-compiler-builtins-*.patch popd echo "Successfully prepared sysroot source for building" diff --git a/crate_patches/0002-compiler-builtins-Disable-128bit-atomic-operations.patch b/crate_patches/0002-compiler-builtins-Disable-128bit-atomic-operations.patch new file mode 100644 index 0000000000000..7daea99f5794d --- /dev/null +++ b/crate_patches/0002-compiler-builtins-Disable-128bit-atomic-operations.patch @@ -0,0 +1,48 @@ +From 1d574bf5e32d51641dcacaf8ef777e95b44f6f2a Mon Sep 17 00:00:00 2001 +From: bjorn3 +Date: Thu, 18 Feb 2021 18:30:55 +0100 +Subject: [PATCH] Disable 128bit atomic operations + +Cranelift doesn't support them yet +--- + src/mem/mod.rs | 12 ------------ + 1 file changed, 12 deletions(-) + +diff --git a/src/mem/mod.rs b/src/mem/mod.rs +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) -> () { + 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) -> () { +- 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) -> () { +@@ -158,10 +154,6 @@ intrinsics! { + pub 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) -> () { +- 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) -> () { +@@ -179,8 +171,4 @@ intrinsics! { + pub 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) -> () { +- memset_element_unordered_atomic(s, c, bytes); +- } + } +-- +2.26.2.7.g19db9cfb68 + diff --git a/patches/0027-Disable-128bit-atomic-operations.patch b/patches/0027-Disable-128bit-atomic-operations.patch new file mode 100644 index 0000000000000..054299e8aab28 --- /dev/null +++ b/patches/0027-Disable-128bit-atomic-operations.patch @@ -0,0 +1,103 @@ +From 894e07dfec2624ba539129b1c1d63e1d7d812bda Mon Sep 17 00:00:00 2001 +From: bjorn3 +Date: Thu, 18 Feb 2021 18:45:28 +0100 +Subject: [PATCH] Disable 128bit atomic operations + +Cranelift doesn't support them yet +--- + library/core/src/sync/atomic.rs | 38 --------------------------------- + library/core/tests/atomic.rs | 4 ---- + library/std/src/panic.rs | 6 ------ + 3 files changed, 48 deletions(-) + +diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs +index 81c9e1d..65c9503 100644 +--- a/library/core/src/sync/atomic.rs ++++ b/library/core/src/sync/atomic.rs +@@ -2228,44 +2228,6 @@ atomic_int! { + "AtomicU64::new(0)", + u64 AtomicU64 ATOMIC_U64_INIT + } +-#[cfg(target_has_atomic_load_store = "128")] +-atomic_int! { +- cfg(target_has_atomic = "128"), +- cfg(target_has_atomic_equal_alignment = "128"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), +- unstable(feature = "integer_atomics", issue = "32976"), +- "i128", "../../../std/primitive.i128.html", +- "#![feature(integer_atomics)]\n\n", +- atomic_min, atomic_max, +- 16, +- "AtomicI128::new(0)", +- i128 AtomicI128 ATOMIC_I128_INIT +-} +-#[cfg(target_has_atomic_load_store = "128")] +-atomic_int! { +- cfg(target_has_atomic = "128"), +- cfg(target_has_atomic_equal_alignment = "128"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- unstable(feature = "integer_atomics", issue = "32976"), +- rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), +- unstable(feature = "integer_atomics", issue = "32976"), +- "u128", "../../../std/primitive.u128.html", +- "#![feature(integer_atomics)]\n\n", +- atomic_umin, atomic_umax, +- 16, +- "AtomicU128::new(0)", +- u128 AtomicU128 ATOMIC_U128_INIT +-} + + macro_rules! atomic_int_ptr_sized { + ( $($target_pointer_width:literal $align:literal)* ) => { $( +diff --git a/library/core/tests/atomic.rs b/library/core/tests/atomic.rs +index 2d1e449..cb6da5d 100644 +--- a/library/core/tests/atomic.rs ++++ b/library/core/tests/atomic.rs +@@ -145,10 +145,6 @@ fn atomic_alignment() { + assert_eq!(align_of::(), size_of::()); + #[cfg(target_has_atomic = "64")] + assert_eq!(align_of::(), size_of::()); +- #[cfg(target_has_atomic = "128")] +- assert_eq!(align_of::(), size_of::()); +- #[cfg(target_has_atomic = "128")] +- assert_eq!(align_of::(), size_of::()); + #[cfg(target_has_atomic = "ptr")] + assert_eq!(align_of::(), size_of::()); + #[cfg(target_has_atomic = "ptr")] +diff --git a/library/std/src/panic.rs b/library/std/src/panic.rs +index 89a822a..779fd88 100644 +--- a/library/std/src/panic.rs ++++ b/library/std/src/panic.rs +@@ -279,9 +279,6 @@ impl RefUnwindSafe for atomic::AtomicI32 {} + #[cfg(target_has_atomic_load_store = "64")] + #[stable(feature = "integer_atomics_stable", since = "1.34.0")] + impl RefUnwindSafe for atomic::AtomicI64 {} +-#[cfg(target_has_atomic_load_store = "128")] +-#[unstable(feature = "integer_atomics", issue = "32976")] +-impl RefUnwindSafe for atomic::AtomicI128 {} + + #[cfg(target_has_atomic_load_store = "ptr")] + #[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")] +@@ -298,9 +295,6 @@ impl RefUnwindSafe for atomic::AtomicU32 {} + #[cfg(target_has_atomic_load_store = "64")] + #[stable(feature = "integer_atomics_stable", since = "1.34.0")] + impl RefUnwindSafe for atomic::AtomicU64 {} +-#[cfg(target_has_atomic_load_store = "128")] +-#[unstable(feature = "integer_atomics", issue = "32976")] +-impl RefUnwindSafe for atomic::AtomicU128 {} + + #[cfg(target_has_atomic_load_store = "8")] + #[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")] +-- +2.26.2.7.g19db9cfb68 + From 702676bfff241bc1b8766f11f6dd42b356ebc071 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 18 Jan 2021 23:53:42 +0100 Subject: [PATCH 24/61] Use a QueryContext for try_mark_green. --- src/driver/aot.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/driver/aot.rs b/src/driver/aot.rs index c9e503a43b9e1..39781e2482a6a 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -465,9 +465,5 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR cgu.name() ); - if tcx.dep_graph.try_mark_green(tcx, &dep_node).is_some() { - CguReuse::PreLto - } else { - CguReuse::No - } + if tcx.try_mark_green(&dep_node) { CguReuse::PreLto } else { CguReuse::No } } From 92c1b858ef7855fea75162499fff93d3d04144f1 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 20 Feb 2021 16:55:30 +0100 Subject: [PATCH 25/61] Disable stack2reg optimization It causes a miscompilation of lewton Fixes #1142 --- src/optimize/mod.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/optimize/mod.rs b/src/optimize/mod.rs index 3ce7f8cd9a831..389f50e797e89 100644 --- a/src/optimize/mod.rs +++ b/src/optimize/mod.rs @@ -19,7 +19,12 @@ pub(crate) fn optimize_function<'tcx>( if tcx.sess.opts.optimize == rustc_session::config::OptLevel::No { return; // FIXME classify optimizations over opt levels } - self::stack2reg::optimize_function(ctx, clif_comments); + + // FIXME(#1142) stack2reg miscompiles lewton + if false { + self::stack2reg::optimize_function(ctx, clif_comments); + } + crate::pretty_clif::write_clif_file(tcx, "stack2reg", None, instance, &ctx, &*clif_comments); crate::base::verify_func(tcx, &*clif_comments, &ctx.func); } From 25f7eeec4638694bfeb5dc7569701ee802e2bf45 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 10:40:30 +0100 Subject: [PATCH 26/61] Don't import the metadata symbol Helps with #1134 --- src/driver/jit.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 4abfcfcd7b19e..1640c622b91c9 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -209,6 +209,11 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { if name.is_empty() || !symbol.is_global() || symbol.is_undefined() { return None; } + if name.starts_with("rust_metadata_") { + // The metadata is part of a section that is not loaded by the dynamic linker in + // case of cg_llvm. + return None; + } let dlsym_name = if cfg!(target_os = "macos") { // On macOS `dlsym` expects the name without leading `_`. assert!(name.starts_with('_'), "{:?}", name); From a8f3877c361f368ee045025f2c5d055cd9f66b0f Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 10:42:31 +0100 Subject: [PATCH 27/61] Don't write clif ir by default when debug assertions are enabled --- src/pretty_clif.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index f4a15ab12d511..71020b4047374 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -201,12 +201,10 @@ impl FunctionCx<'_, '_, M> { } pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool { - cfg!(debug_assertions) - || tcx - .sess - .opts - .output_types - .contains_key(&OutputType::LlvmAssembly) + tcx.sess + .opts + .output_types + .contains_key(&OutputType::LlvmAssembly) } pub(crate) fn write_ir_file<'tcx>( From b36fd9d82b645d67282eda2ca6bbcb09eca5163f Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 10:49:55 +0100 Subject: [PATCH 28/61] Remove unnecessary parts from alloc_system --- build_sysroot/alloc_system/Cargo.toml | 2 + build_sysroot/alloc_system/lib.rs | 134 +------------------------- 2 files changed, 4 insertions(+), 132 deletions(-) diff --git a/build_sysroot/alloc_system/Cargo.toml b/build_sysroot/alloc_system/Cargo.toml index 9fffca843006f..09b13db772611 100644 --- a/build_sysroot/alloc_system/Cargo.toml +++ b/build_sysroot/alloc_system/Cargo.toml @@ -2,11 +2,13 @@ authors = ["The Rust Project Developers", "bjorn3 (edited to be usable outside the rust source)"] name = "alloc_system" version = "0.0.0" + [lib] name = "alloc_system" path = "lib.rs" test = false doc = false + [dependencies] core = { path = "../sysroot_src/library/core" } libc = { version = "0.2.43", features = ['rustc-dep-of-std'], default-features = false } diff --git a/build_sysroot/alloc_system/lib.rs b/build_sysroot/alloc_system/lib.rs index c832d5e5ebb9b..405ccbae6d62c 100644 --- a/build_sysroot/alloc_system/lib.rs +++ b/build_sysroot/alloc_system/lib.rs @@ -8,66 +8,24 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. #![no_std] -#![allow(unused_attributes)] -#![unstable(feature = "alloc_system", - reason = "this library is unlikely to be stabilized in its current \ - form or name", - issue = "32838")] #![feature(allocator_api)] -#![feature(core_intrinsics)] -#![feature(nll)] -#![feature(staged_api)] -#![feature(rustc_attrs)] -#![feature(alloc_layout_extra)] -#![cfg_attr( - all(target_arch = "wasm32", not(target_os = "emscripten")), - feature(integer_atomics, stdsimd) -)] #![cfg_attr(any(unix, target_os = "redox"), feature(libc))] + // The minimum alignment guaranteed by the architecture. This value is used to // add fast paths for low alignment values. #[cfg(all(any(target_arch = "x86", target_arch = "arm", target_arch = "mips", target_arch = "powerpc", - target_arch = "powerpc64", - target_arch = "asmjs", - target_arch = "wasm32")))] -#[allow(dead_code)] + target_arch = "powerpc64")))] const MIN_ALIGN: usize = 8; #[cfg(all(any(target_arch = "x86_64", target_arch = "aarch64", target_arch = "mips64", target_arch = "s390x", target_arch = "sparc64")))] -#[allow(dead_code)] const MIN_ALIGN: usize = 16; -/// The default memory allocator provided by the operating system. -/// -/// This is based on `malloc` on Unix platforms and `HeapAlloc` on Windows, -/// plus related functions. -/// -/// This type can be used in a `static` item -/// with the `#[global_allocator]` attribute -/// to force the global allocator to be the system’s one. -/// (The default is jemalloc for executables, on some platforms.) -/// -/// ```rust -/// use std::alloc::System; -/// -/// #[global_allocator] -/// static A: System = System; -/// -/// fn main() { -/// let a = Box::new(4); // Allocates from the system allocator. -/// println!("{}", a); -/// } -/// ``` -/// -/// It can also be used directly to allocate memory -/// independently of the standard library’s global allocator. -#[stable(feature = "alloc_system_type", since = "1.28.0")] pub struct System; #[cfg(any(windows, unix, target_os = "redox"))] mod realloc_fallback { @@ -96,7 +54,6 @@ mod platform { use MIN_ALIGN; use System; use core::alloc::{GlobalAlloc, Layout}; - #[stable(feature = "alloc_system_type", since = "1.28.0")] unsafe impl GlobalAlloc for System { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { @@ -221,7 +178,6 @@ mod platform { }; ptr as *mut u8 } - #[stable(feature = "alloc_system_type", since = "1.28.0")] unsafe impl GlobalAlloc for System { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { @@ -254,89 +210,3 @@ mod platform { } } } -// This is an implementation of a global allocator on the wasm32 platform when -// emscripten is not in use. In that situation there's no actual runtime for us -// to lean on for allocation, so instead we provide our own! -// -// The wasm32 instruction set has two instructions for getting the current -// amount of memory and growing the amount of memory. These instructions are the -// foundation on which we're able to build an allocator, so we do so! Note that -// the instructions are also pretty "global" and this is the "global" allocator -// after all! -// -// The current allocator here is the `dlmalloc` crate which we've got included -// in the rust-lang/rust repository as a submodule. The crate is a port of -// dlmalloc.c from C to Rust and is basically just so we can have "pure Rust" -// for now which is currently technically required (can't link with C yet). -// -// The crate itself provides a global allocator which on wasm has no -// synchronization as there are no threads! -#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] -mod platform { - extern crate dlmalloc; - use core::alloc::{GlobalAlloc, Layout}; - use System; - static mut DLMALLOC: dlmalloc::Dlmalloc = dlmalloc::DLMALLOC_INIT; - #[stable(feature = "alloc_system_type", since = "1.28.0")] - unsafe impl GlobalAlloc for System { - #[inline] - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - let _lock = lock::lock(); - DLMALLOC.malloc(layout.size(), layout.align()) - } - #[inline] - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { - let _lock = lock::lock(); - DLMALLOC.calloc(layout.size(), layout.align()) - } - #[inline] - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - let _lock = lock::lock(); - DLMALLOC.free(ptr, layout.size(), layout.align()) - } - #[inline] - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { - let _lock = lock::lock(); - DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) - } - } - #[cfg(target_feature = "atomics")] - mod lock { - use core::arch::wasm32; - use core::sync::atomic::{AtomicI32, Ordering::SeqCst}; - static LOCKED: AtomicI32 = AtomicI32::new(0); - pub struct DropLock; - pub fn lock() -> DropLock { - loop { - if LOCKED.swap(1, SeqCst) == 0 { - return DropLock - } - unsafe { - let r = wasm32::atomic::wait_i32( - &LOCKED as *const AtomicI32 as *mut i32, - 1, // expected value - -1, // timeout - ); - debug_assert!(r == 0 || r == 1); - } - } - } - impl Drop for DropLock { - fn drop(&mut self) { - let r = LOCKED.swap(0, SeqCst); - debug_assert_eq!(r, 1); - unsafe { - wasm32::atomic::wake( - &LOCKED as *const AtomicI32 as *mut i32, - 1, // only one thread - ); - } - } - } - } - #[cfg(not(target_feature = "atomics"))] - mod lock { - #[inline] - pub fn lock() {} // no atomics, no threads, that's easy! - } -} From 93373e13f0df27c7f887caacdcc02182925c5644 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 10:56:25 +0100 Subject: [PATCH 29/61] Don't build alloc_system as part of the sysroot --- build_sysroot/Cargo.lock | 10 ---------- build_sysroot/Cargo.toml | 2 -- build_sysroot/alloc_system/Cargo.toml | 15 --------------- example/alloc_example.rs | 2 +- .../lib.rs => example/alloc_system.rs | 2 +- scripts/tests.sh | 3 +++ 6 files changed, 5 insertions(+), 29 deletions(-) delete mode 100644 build_sysroot/alloc_system/Cargo.toml rename build_sysroot/alloc_system/lib.rs => example/alloc_system.rs (99%) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index 6b35dec0058aa..2bafe2c526932 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -30,15 +30,6 @@ dependencies = [ "core", ] -[[package]] -name = "alloc_system" -version = "0.0.0" -dependencies = [ - "compiler_builtins", - "core", - "libc", -] - [[package]] name = "autocfg" version = "1.0.1" @@ -258,7 +249,6 @@ name = "sysroot" version = "0.0.0" dependencies = [ "alloc", - "alloc_system", "compiler_builtins", "core", "std", diff --git a/build_sysroot/Cargo.toml b/build_sysroot/Cargo.toml index 82516c98af2a2..04748d5dbab59 100644 --- a/build_sysroot/Cargo.toml +++ b/build_sysroot/Cargo.toml @@ -9,8 +9,6 @@ alloc = { path = "./sysroot_src/library/alloc" } std = { path = "./sysroot_src/library/std", features = ["panic_unwind", "backtrace"] } test = { path = "./sysroot_src/library/test" } -alloc_system = { path = "./alloc_system" } - compiler_builtins = { version = "0.1.39", default-features = false, features = ["no-asm"] } [patch.crates-io] diff --git a/build_sysroot/alloc_system/Cargo.toml b/build_sysroot/alloc_system/Cargo.toml deleted file mode 100644 index 09b13db772611..0000000000000 --- a/build_sysroot/alloc_system/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -authors = ["The Rust Project Developers", "bjorn3 (edited to be usable outside the rust source)"] -name = "alloc_system" -version = "0.0.0" - -[lib] -name = "alloc_system" -path = "lib.rs" -test = false -doc = false - -[dependencies] -core = { path = "../sysroot_src/library/core" } -libc = { version = "0.2.43", features = ['rustc-dep-of-std'], default-features = false } -compiler_builtins = "0.1" diff --git a/example/alloc_example.rs b/example/alloc_example.rs index f59600ebb330c..71e93e87b6c41 100644 --- a/example/alloc_example.rs +++ b/example/alloc_example.rs @@ -1,4 +1,4 @@ -#![feature(start, box_syntax, alloc_system, core_intrinsics, alloc_prelude, alloc_error_handler)] +#![feature(start, box_syntax, core_intrinsics, alloc_prelude, alloc_error_handler)] #![no_std] extern crate alloc; diff --git a/build_sysroot/alloc_system/lib.rs b/example/alloc_system.rs similarity index 99% rename from build_sysroot/alloc_system/lib.rs rename to example/alloc_system.rs index 405ccbae6d62c..5f66ca67f2d40 100644 --- a/build_sysroot/alloc_system/lib.rs +++ b/example/alloc_system.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. #![no_std] -#![feature(allocator_api)] +#![feature(allocator_api, rustc_private)] #![cfg_attr(any(unix, target_os = "redox"), feature(libc))] // The minimum alignment guaranteed by the architecture. This value is used to diff --git a/scripts/tests.sh b/scripts/tests.sh index cfc0c3fe9bd46..321e0af553e5a 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -34,6 +34,9 @@ function no_sysroot_tests() { } function base_sysroot_tests() { + echo "[AOT] alloc_system" + $MY_RUSTC example/alloc_system.rs --crate-type lib --target "$TARGET_TRIPLE" + echo "[AOT] alloc_example" $MY_RUSTC example/alloc_example.rs --crate-type bin --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/alloc_example From 4f45ea73efb7e92cc1eefd684959300f2b630880 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 11:18:01 +0100 Subject: [PATCH 30/61] Re-enable some now compiling libcore tests cc #806 --- .../0022-core-Disable-not-compiling-tests.patch | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/patches/0022-core-Disable-not-compiling-tests.patch b/patches/0022-core-Disable-not-compiling-tests.patch index 3eb10069adad6..8cfffe580a1f0 100644 --- a/patches/0022-core-Disable-not-compiling-tests.patch +++ b/patches/0022-core-Disable-not-compiling-tests.patch @@ -119,21 +119,5 @@ index 6609bc3..241b497 100644 #[test] #[should_panic(expected = "index 0 greater than length of slice")] -diff --git a/library/core/tests/num/ops.rs b/library/core/tests/num/ops.rs -index 9979cc8..d5d1d83 100644 ---- a/library/core/tests/num/ops.rs -+++ b/library/core/tests/num/ops.rs -@@ -238,7 +238,7 @@ macro_rules! test_shift_assign { - } - }; - } --test_shift!(test_shl_defined, Shl::shl); --test_shift_assign!(test_shl_assign_defined, ShlAssign::shl_assign); --test_shift!(test_shr_defined, Shr::shr); --test_shift_assign!(test_shr_assign_defined, ShrAssign::shr_assign); -+//test_shift!(test_shl_defined, Shl::shl); -+//test_shift_assign!(test_shl_assign_defined, ShlAssign::shl_assign); -+//test_shift!(test_shr_defined, Shr::shr); -+//test_shift_assign!(test_shr_assign_defined, ShrAssign::shr_assign); -- 2.21.0 (Apple Git-122) From c5f98b586f9c835e8e3ebecf3db260d2f0ad402a Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:03:02 +0100 Subject: [PATCH 31/61] Remove shift amount masking Unlike the old x75 backend, the new x64 backend masks the shift amount itself, matching the specified semantics. --- src/num.rs | 26 ++++++-------------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/num.rs b/src/num.rs index d1d2b3b872a4b..4cce853d4b0c6 100644 --- a/src/num.rs +++ b/src/num.rs @@ -168,20 +168,12 @@ pub(crate) fn codegen_int_binop<'tcx>( BinOp::BitXor => b.bxor(lhs, rhs), BinOp::BitAnd => b.band(lhs, rhs), BinOp::BitOr => b.bor(lhs, rhs), - BinOp::Shl => { - let lhs_ty = fx.bcx.func.dfg.value_type(lhs); - let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); - let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); - fx.bcx.ins().ishl(lhs, actual_shift) - } + BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs), BinOp::Shr => { - let lhs_ty = fx.bcx.func.dfg.value_type(lhs); - let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); - let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); if signed { - fx.bcx.ins().sshr(lhs, actual_shift) + fx.bcx.ins().sshr(lhs, rhs) } else { - fx.bcx.ins().ushr(lhs, actual_shift) + fx.bcx.ins().ushr(lhs, rhs) } } // Compare binops handles by `codegen_binop`. @@ -303,10 +295,7 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( } } BinOp::Shl => { - let lhs_ty = fx.bcx.func.dfg.value_type(lhs); - let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); - let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); - let val = fx.bcx.ins().ishl(lhs, actual_shift); + let val = fx.bcx.ins().ishl(lhs, rhs); let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; let has_overflow = fx @@ -316,13 +305,10 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( (val, has_overflow) } BinOp::Shr => { - let lhs_ty = fx.bcx.func.dfg.value_type(lhs); - let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); - let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); let val = if !signed { - fx.bcx.ins().ushr(lhs, actual_shift) + fx.bcx.ins().ushr(lhs, rhs) } else { - fx.bcx.ins().sshr(lhs, actual_shift) + fx.bcx.ins().sshr(lhs, rhs) }; let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; From 9c980276d81b727bb8efcbe63f51866ba19793a0 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:09:13 +0100 Subject: [PATCH 32/61] Directly use Cranelift instructions for 128bit int shifts --- src/codegen_i128.rs | 65 +++++++-------------------------------------- src/common.rs | 47 -------------------------------- 2 files changed, 9 insertions(+), 103 deletions(-) diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index 866ba90e4ae4b..7b674df446a23 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -97,70 +97,23 @@ pub(crate) fn maybe_codegen<'tcx>( None }; - // Optimize `val >> 64`, because compiler_builtins uses it to deconstruct an 128bit - // integer into its lsb and msb. - // https://github.com/rust-lang-nursery/compiler-builtins/blob/79a6a1603d5672cbb9187ff41ff4d9b5048ac1cb/src/int/mod.rs#L217 - if resolve_value_imm(fx.bcx.func, rhs_val) == Some(64) { - let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs_val); - let all_zeros = fx.bcx.ins().iconst(types::I64, 0); - let val = match (bin_op, is_signed) { - (BinOp::Shr, false) => { - let val = fx.bcx.ins().iconcat(lhs_msb, all_zeros); - Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.u128))) - } - (BinOp::Shr, true) => { - let sign = fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, lhs_msb, 0); - let all_ones = fx.bcx.ins().iconst(types::I64, u64::MAX as i64); - let all_sign_bits = fx.bcx.ins().select(sign, all_zeros, all_ones); - - let val = fx.bcx.ins().iconcat(lhs_msb, all_sign_bits); - Some(CValue::by_val(val, fx.layout_of(fx.tcx.types.i128))) - } - (BinOp::Shl, _) => { - let val_ty = if is_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; - let val = fx.bcx.ins().iconcat(all_zeros, lhs_lsb); - Some(CValue::by_val(val, fx.layout_of(val_ty))) - } - _ => None, - }; - if let Some(val) = val { - if let Some(is_overflow) = is_overflow { - let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - let val = val.load_scalar(fx); - return Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty))); + let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false); + let val = match bin_op { + BinOp::Shl => fx.bcx.ins().ishl(lhs_val, truncated_rhs), + BinOp::Shr => { + if is_signed { + fx.bcx.ins().sshr(lhs_val, truncated_rhs) } else { - return Some(val); + fx.bcx.ins().ushr(lhs_val, truncated_rhs) } } - } - - let truncated_rhs = clif_intcast(fx, rhs_val, types::I32, false); - let truncated_rhs = CValue::by_val(truncated_rhs, fx.layout_of(fx.tcx.types.u32)); - let val = match (bin_op, is_signed) { - (BinOp::Shl, false) => { - fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.u128) - } - (BinOp::Shl, true) => { - fx.easy_call("__ashlti3", &[lhs, truncated_rhs], fx.tcx.types.i128) - } - (BinOp::Shr, false) => { - fx.easy_call("__lshrti3", &[lhs, truncated_rhs], fx.tcx.types.u128) - } - (BinOp::Shr, true) => { - fx.easy_call("__ashrti3", &[lhs, truncated_rhs], fx.tcx.types.i128) - } - (_, _) => unreachable!(), + _ => unreachable!(), }; if let Some(is_overflow) = is_overflow { let out_ty = fx.tcx.mk_tup([lhs.layout().ty, fx.tcx.types.bool].iter()); - let val = val.load_scalar(fx); Some(CValue::by_val_pair(val, is_overflow, fx.layout_of(out_ty))) } else { - Some(val) + Some(CValue::by_val(val, lhs.layout())) } } } diff --git a/src/common.rs b/src/common.rs index fbee84e09f7a6..1443aedb3b8a7 100644 --- a/src/common.rs +++ b/src/common.rs @@ -3,8 +3,6 @@ use rustc_target::abi::call::FnAbi; use rustc_target::abi::{Integer, Primitive}; use rustc_target::spec::{HasTargetSpec, Target}; -use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef}; - use crate::prelude::*; pub(crate) fn pointer_ty(tcx: TyCtxt<'_>) -> types::Type { @@ -175,51 +173,6 @@ pub(crate) fn codegen_icmp_imm( } } -fn resolve_normal_value_imm(func: &Function, val: Value) -> Option { - if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) { - if let InstructionData::UnaryImm { - opcode: Opcode::Iconst, - imm, - } = func.dfg[inst] - { - Some(imm.into()) - } else { - None - } - } else { - None - } -} - -fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option { - let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) { - if let InstructionData::Binary { - opcode: Opcode::Iconcat, - args: [lsb, msb], - } = func.dfg[inst] - { - (lsb, msb) - } else { - return None; - } - } else { - return None; - }; - - let lsb = u128::from(resolve_normal_value_imm(func, lsb)? as u64); - let msb = u128::from(resolve_normal_value_imm(func, msb)? as u64); - - Some(msb << 64 | lsb) -} - -pub(crate) fn resolve_value_imm(func: &Function, val: Value) -> Option { - if func.dfg.value_type(val) == types::I128 { - resolve_128bit_value_imm(func, val) - } else { - resolve_normal_value_imm(func, val).map(|imm| u128::from(imm as u64)) - } -} - pub(crate) fn type_min_max_value( bcx: &mut FunctionBuilder<'_>, ty: Type, From ee36a1fbb1e7a2d7e7eaa044eaf68e21e04be3b4 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:18:43 +0100 Subject: [PATCH 33/61] Remove workaround for stack_{load,store}.i128 not being implemented --- src/pointer.rs | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/src/pointer.rs b/src/pointer.rs index b2036d7bcd452..d08d878c45b6f 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -168,13 +168,7 @@ impl Pointer { match self.base { PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset), PointerBase::Stack(stack_slot) => { - if ty == types::I128 || ty.is_vector() { - // WORKAROUND for stack_load.i128 and stack_load.iXxY not being implemented - let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0); - fx.bcx.ins().load(ty, flags, base_addr, self.offset) - } else { - fx.bcx.ins().stack_load(ty, stack_slot, self.offset) - } + fx.bcx.ins().stack_load(ty, stack_slot, self.offset) } PointerBase::Dangling(_align) => unreachable!(), } @@ -191,14 +185,7 @@ impl Pointer { fx.bcx.ins().store(flags, value, base_addr, self.offset); } PointerBase::Stack(stack_slot) => { - let val_ty = fx.bcx.func.dfg.value_type(value); - if val_ty == types::I128 || val_ty.is_vector() { - // WORKAROUND for stack_store.i128 and stack_store.iXxY not being implemented - let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0); - fx.bcx.ins().store(flags, value, base_addr, self.offset); - } else { - fx.bcx.ins().stack_store(value, stack_slot, self.offset); - } + fx.bcx.ins().stack_store(value, stack_slot, self.offset); } PointerBase::Dangling(_align) => unreachable!(), } From b0f870edd63c671645647c7bb9026b3e0e5f0966 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:34:21 +0100 Subject: [PATCH 34/61] Fix CI --- src/codegen_i128.rs | 6 +++++- src/pointer.rs | 4 +--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index 7b674df446a23..ed9097e5a5b58 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -11,7 +11,11 @@ pub(crate) fn maybe_codegen<'tcx>( lhs: CValue<'tcx>, rhs: CValue<'tcx>, ) -> Option> { - if lhs.layout().ty != fx.tcx.types.u128 && lhs.layout().ty != fx.tcx.types.i128 { + if lhs.layout().ty != fx.tcx.types.u128 + && lhs.layout().ty != fx.tcx.types.i128 + && rhs.layout().ty != fx.tcx.types.u128 + && rhs.layout().ty != fx.tcx.types.i128 + { return None; } diff --git a/src/pointer.rs b/src/pointer.rs index d08d878c45b6f..7b31d0a20078d 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -167,9 +167,7 @@ impl Pointer { ) -> Value { match self.base { PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset), - PointerBase::Stack(stack_slot) => { - fx.bcx.ins().stack_load(ty, stack_slot, self.offset) - } + PointerBase::Stack(stack_slot) => fx.bcx.ins().stack_load(ty, stack_slot, self.offset), PointerBase::Dangling(_align) => unreachable!(), } } From 74f39b64c8d6a1395c6029d59d50342f80cae04e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 12:48:43 +0100 Subject: [PATCH 35/61] Avoid a temporary stackslot in codegen_checked_int_binop --- src/num.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/num.rs b/src/num.rs index 4cce853d4b0c6..850ff3561f7f2 100644 --- a/src/num.rs +++ b/src/num.rs @@ -328,18 +328,11 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( let has_overflow = fx.bcx.ins().bint(types::I8, has_overflow); - // FIXME directly write to result place instead - let out_place = CPlace::new_stack_slot( - fx, - fx.layout_of( - fx.tcx - .mk_tup([in_lhs.layout().ty, fx.tcx.types.bool].iter()), - ), + let out_layout = fx.layout_of( + fx.tcx + .mk_tup([in_lhs.layout().ty, fx.tcx.types.bool].iter()), ); - let out_layout = out_place.layout(); - out_place.write_cvalue(fx, CValue::by_val_pair(res, has_overflow, out_layout)); - - out_place.to_cvalue(fx) + CValue::by_val_pair(res, has_overflow, out_layout) } pub(crate) fn codegen_float_binop<'tcx>( From 0610490c8dc71868ba886efbb8707cb77fc35928 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 13:58:56 +0100 Subject: [PATCH 36/61] Assume constants can't fail to evaluate See rust-lang/rust#81327 for the same change to cg_llvm --- src/base.rs | 9 ++++++--- src/constant.rs | 14 +++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/base.rs b/src/base.rs index 03889a4687f82..0be498172ae72 100644 --- a/src/base.rs +++ b/src/base.rs @@ -74,7 +74,12 @@ pub(crate) fn codegen_fn<'tcx>( .is_uninhabited() }); - if arg_uninhabited { + if !crate::constant::check_constants(&mut fx) { + fx.bcx + .append_block_params_for_function_params(fx.block_map[START_BLOCK]); + fx.bcx.switch_to_block(fx.block_map[START_BLOCK]); + crate::trap::trap_unreachable(&mut fx, "compilation should have been aborted"); + } else if arg_uninhabited { fx.bcx .append_block_params_for_function_params(fx.block_map[START_BLOCK]); fx.bcx.switch_to_block(fx.block_map[START_BLOCK]); @@ -205,8 +210,6 @@ pub(crate) fn verify_func( } fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) { - crate::constant::check_constants(fx); - for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() { let block = fx.get_block(bb); fx.bcx.switch_to_block(block); diff --git a/src/constant.rs b/src/constant.rs index 5702832bcb67d..f3e17774fdfa5 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -36,7 +36,8 @@ impl ConstantCx { } } -pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) { +pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) -> bool { + let mut all_constants_ok = true; for constant in &fx.mir.required_consts { let const_ = fx.monomorphize(constant.literal); match const_.val { @@ -46,6 +47,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) { fx.tcx .const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) { + all_constants_ok = false; match err { ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => { fx.tcx @@ -69,6 +71,7 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) { | ConstKind::Error(_) => unreachable!("{:?}", const_), } } + all_constants_ok } pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) { @@ -134,14 +137,7 @@ pub(crate) fn codegen_constant<'tcx>( { Ok(const_val) => const_val, Err(_) => { - fx.tcx - .sess - .span_err(constant.span, "erroneous constant encountered"); - return crate::trap::trap_unreachable_ret_value( - fx, - fx.layout_of(const_.ty), - "erroneous constant encountered", - ); + span_bug!(constant.span, "erroneous constant not captured by required_consts"); } } } From 7fc34839acfe1d69df83f79c4c2c8802dea17cdc Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 14:39:31 +0100 Subject: [PATCH 37/61] Revert "Remove shift amount masking" This reverts commit c5f98b586f9c835e8e3ebecf3db260d2f0ad402a. It turns out to be necessary anyway --- src/num.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/num.rs b/src/num.rs index 850ff3561f7f2..2022614841dec 100644 --- a/src/num.rs +++ b/src/num.rs @@ -168,12 +168,20 @@ pub(crate) fn codegen_int_binop<'tcx>( BinOp::BitXor => b.bxor(lhs, rhs), BinOp::BitAnd => b.band(lhs, rhs), BinOp::BitOr => b.bor(lhs, rhs), - BinOp::Shl => fx.bcx.ins().ishl(lhs, rhs), + BinOp::Shl => { + let lhs_ty = fx.bcx.func.dfg.value_type(lhs); + let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); + let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); + fx.bcx.ins().ishl(lhs, actual_shift) + } BinOp::Shr => { + let lhs_ty = fx.bcx.func.dfg.value_type(lhs); + let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); + let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); if signed { - fx.bcx.ins().sshr(lhs, rhs) + fx.bcx.ins().sshr(lhs, actual_shift) } else { - fx.bcx.ins().ushr(lhs, rhs) + fx.bcx.ins().ushr(lhs, actual_shift) } } // Compare binops handles by `codegen_binop`. @@ -295,7 +303,10 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( } } BinOp::Shl => { - let val = fx.bcx.ins().ishl(lhs, rhs); + let lhs_ty = fx.bcx.func.dfg.value_type(lhs); + let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); + let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); + let val = fx.bcx.ins().ishl(lhs, actual_shift); let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; let has_overflow = fx @@ -305,10 +316,13 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( (val, has_overflow) } BinOp::Shr => { + let lhs_ty = fx.bcx.func.dfg.value_type(lhs); + let actual_shift = fx.bcx.ins().band_imm(rhs, i64::from(lhs_ty.bits() - 1)); + let actual_shift = clif_intcast(fx, actual_shift, types::I8, false); let val = if !signed { - fx.bcx.ins().ushr(lhs, rhs) + fx.bcx.ins().ushr(lhs, actual_shift) } else { - fx.bcx.ins().sshr(lhs, rhs) + fx.bcx.ins().sshr(lhs, actual_shift) }; let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; From ecba16f29616265b81b415921f69e5b104085123 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 16:42:39 +0100 Subject: [PATCH 38/61] Various enum to int cast fixes --- src/base.rs | 58 +++++++++++------------------------------- src/value_and_place.rs | 8 +++++- 2 files changed, 22 insertions(+), 44 deletions(-) diff --git a/src/base.rs b/src/base.rs index 0be498172ae72..9b2249956c5ef 100644 --- a/src/base.rs +++ b/src/base.rs @@ -627,50 +627,22 @@ fn codegen_stmt<'tcx>( ty::Uint(_) | ty::Int(_) => {} _ => unreachable!("cast adt {} -> {}", from_ty, to_ty), } + let to_clif_ty = fx.clif_type(to_ty).unwrap(); - use rustc_target::abi::{Int, TagEncoding, Variants}; - - match operand.layout().variants { - Variants::Single { index } => { - let discr = operand - .layout() - .ty - .discriminant_for_variant(fx.tcx, index) - .unwrap(); - let discr = if discr.ty.is_signed() { - fx.layout_of(discr.ty).size.sign_extend(discr.val) - } else { - discr.val - }; - let discr = discr.into(); - - let discr = CValue::const_val(fx, fx.layout_of(to_ty), discr); - lval.write_cvalue(fx, discr); - } - Variants::Multiple { - ref tag, - tag_field, - tag_encoding: TagEncoding::Direct, - variants: _, - } => { - let cast_to = fx.clif_type(dest_layout.ty).unwrap(); - - // Read the tag/niche-encoded discriminant from memory. - let encoded_discr = - operand.value_field(fx, mir::Field::new(tag_field)); - let encoded_discr = encoded_discr.load_scalar(fx); - - // Decode the discriminant (specifically if it's niche-encoded). - let signed = match tag.value { - Int(_, signed) => signed, - _ => false, - }; - let val = clif_intcast(fx, encoded_discr, cast_to, signed); - let val = CValue::by_val(val, dest_layout); - lval.write_cvalue(fx, val); - } - Variants::Multiple { .. } => unreachable!(), - } + let discriminant = crate::discriminant::codegen_get_discriminant( + fx, + operand, + fx.layout_of(operand.layout().ty.discriminant_ty(fx.tcx)), + ) + .load_scalar(fx); + + let res = crate::cast::clif_intcast( + fx, + discriminant, + to_clif_ty, + to_ty.is_signed(), + ); + lval.write_cvalue(fx, CValue::by_val(res, dest_layout)); } else { let to_clif_ty = fx.clif_type(to_ty).unwrap(); let from = operand.load_scalar(fx); diff --git a/src/value_and_place.rs b/src/value_and_place.rs index 765604e0f984e..c2520b5c6c4b5 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -233,7 +233,13 @@ impl<'tcx> CValue<'tcx> { layout: TyAndLayout<'tcx>, const_val: ty::ScalarInt, ) -> CValue<'tcx> { - assert_eq!(const_val.size(), layout.size); + assert_eq!( + const_val.size(), + layout.size, + "{:#?}: {:?}", + const_val, + layout + ); use cranelift_codegen::ir::immediates::{Ieee32, Ieee64}; let clif_ty = fx.clif_type(layout.ty).unwrap(); From 013b3c5a7ca7f9e180d557e9243ef2e523082002 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 17:03:21 +0100 Subject: [PATCH 39/61] Rustfmt --- src/constant.rs | 5 ++++- src/driver/aot.rs | 6 +++++- src/driver/jit.rs | 3 ++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/constant.rs b/src/constant.rs index f3e17774fdfa5..60f569bca9308 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -137,7 +137,10 @@ pub(crate) fn codegen_constant<'tcx>( { Ok(const_val) => const_val, Err(_) => { - span_bug!(constant.span, "erroneous constant not captured by required_consts"); + span_bug!( + constant.span, + "erroneous constant not captured by required_consts" + ); } } } diff --git a/src/driver/aot.rs b/src/driver/aot.rs index a7fcbb2f1e4b2..1a26642a49b3e 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -433,5 +433,9 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR cgu.name() ); - if tcx.try_mark_green(&dep_node) { CguReuse::PreLto } else { CguReuse::No } + if tcx.try_mark_green(&dep_node) { + CguReuse::PreLto + } else { + CguReuse::No + } } diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 7857e83714399..bab4586e7f622 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -78,7 +78,8 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { } MonoItem::GlobalAsm(item_id) => { let item = cx.tcx.hir().item(item_id); - tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode"); + tcx.sess + .span_fatal(item.span, "Global asm is not supported in JIT mode"); } } } From a5bf6d2f18841d36584c631d0dc8bc35d446c580 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 21 Feb 2021 17:27:53 +0100 Subject: [PATCH 40/61] Rustup to rustc 1.52.0-nightly (83b30a639 2021-02-20) --- build_sysroot/Cargo.lock | 6 ++-- ...itrary_self_types_pointers_and_wrappers.rs | 31 ++++++------------- rust-toolchain | 2 +- scripts/tests.sh | 4 +-- 4 files changed, 16 insertions(+), 27 deletions(-) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index 2bafe2c526932..a7650ab995b0f 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "addr2line" version = "0.14.1" @@ -38,9 +40,9 @@ checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] name = "cc" -version = "1.0.66" +version = "1.0.67" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c0496836a84f8d0495758516b8621a622beb77c0fed418570e50764093ced48" +checksum = "e3c69b077ad434294d3ce9f1f6143a2a4b89a8a2d54ef813d85003a4fd1137fd" [[package]] name = "cfg-if" diff --git a/example/arbitrary_self_types_pointers_and_wrappers.rs b/example/arbitrary_self_types_pointers_and_wrappers.rs index 0b0039a1370a4..ddeb752f93ed7 100644 --- a/example/arbitrary_self_types_pointers_and_wrappers.rs +++ b/example/arbitrary_self_types_pointers_and_wrappers.rs @@ -1,22 +1,12 @@ // Adapted from rustc run-pass test suite -#![feature(no_core, arbitrary_self_types, box_syntax)] +#![feature(arbitrary_self_types, unsize, coerce_unsized, dispatch_from_dyn)] #![feature(rustc_attrs)] -#![feature(start, lang_items)] -#![no_core] - -extern crate mini_core; - -use mini_core::*; - -macro_rules! assert_eq { - ($l:expr, $r: expr) => { - if $l != $r { - panic(stringify!($l != $r)); - } - } -} +use std::{ + ops::{Deref, CoerceUnsized, DispatchFromDyn}, + marker::Unsize, +}; struct Ptr(Box); @@ -67,16 +57,13 @@ impl Trait for i32 { } } -#[start] -fn main(_: isize, _: *const *const u8) -> isize { - let pw = Ptr(box Wrapper(5)) as Ptr>; +fn main() { + let pw = Ptr(Box::new(Wrapper(5))) as Ptr>; assert_eq!(pw.ptr_wrapper(), 5); - let wp = Wrapper(Ptr(box 6)) as Wrapper>; + let wp = Wrapper(Ptr(Box::new(6))) as Wrapper>; assert_eq!(wp.wrapper_ptr(), 6); - let wpw = Wrapper(Ptr(box Wrapper(7))) as Wrapper>>; + let wpw = Wrapper(Ptr(Box::new(Wrapper(7)))) as Wrapper>>; assert_eq!(wpw.wrapper_ptr_wrapper(), 7); - - 0 } diff --git a/rust-toolchain b/rust-toolchain index 6d8303f76f8ce..8c8c58d90d914 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-02-09 +nightly-2021-02-21 diff --git a/scripts/tests.sh b/scripts/tests.sh index 321e0af553e5a..f9a9fb091fb4a 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -27,13 +27,13 @@ function no_sysroot_tests() { $MY_RUSTC example/mini_core_hello_world.rs --crate-name mini_core_hello_world --crate-type bin -g --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/mini_core_hello_world abc bcd # (echo "break set -n main"; echo "run"; sleep 1; echo "si -c 10"; sleep 1; echo "frame variable") | lldb -- ./target/out/mini_core_hello_world abc bcd +} +function base_sysroot_tests() { echo "[AOT] arbitrary_self_types_pointers_and_wrappers" $MY_RUSTC example/arbitrary_self_types_pointers_and_wrappers.rs --crate-name arbitrary_self_types_pointers_and_wrappers --crate-type bin --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/arbitrary_self_types_pointers_and_wrappers -} -function base_sysroot_tests() { echo "[AOT] alloc_system" $MY_RUSTC example/alloc_system.rs --crate-type lib --target "$TARGET_TRIPLE" From d5ac849a249086ca18d6ad58c96f6b6f25011b55 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 22 Feb 2021 14:46:24 +0100 Subject: [PATCH 41/61] Update Cranelift --- Cargo.lock | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 90b5fcfb3ec10..edfb6e52f0d4f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,7 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +version = 3 + [[package]] name = "anyhow" version = "1.0.38" @@ -38,7 +40,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "cranelift-entity", ] @@ -46,7 +48,7 @@ dependencies = [ [[package]] name = "cranelift-codegen" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "byteorder", "cranelift-bforest", @@ -64,7 +66,7 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -73,17 +75,17 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" [[package]] name = "cranelift-entity" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" [[package]] name = "cranelift-frontend" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "cranelift-codegen", "log", @@ -94,7 +96,7 @@ dependencies = [ [[package]] name = "cranelift-jit" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "anyhow", "cranelift-codegen", @@ -112,7 +114,7 @@ dependencies = [ [[package]] name = "cranelift-module" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "anyhow", "cranelift-codegen", @@ -124,7 +126,7 @@ dependencies = [ [[package]] name = "cranelift-native" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "cranelift-codegen", "target-lexicon", @@ -133,7 +135,7 @@ dependencies = [ [[package]] name = "cranelift-object" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#c07ec4c525318afb47b2c6ff24dcddb5d23b5f2a" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" dependencies = [ "anyhow", "cranelift-codegen", From b0028fa409e152accdd53ba6cd1b888893c190de Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 22 Feb 2021 14:56:06 +0100 Subject: [PATCH 42/61] Update dependencies --- Cargo.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index edfb6e52f0d4f..ea7e70a329dc7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -208,9 +208,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.85" +version = "0.2.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" +checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c" [[package]] name = "libloading" @@ -261,9 +261,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.8" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" +checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" dependencies = [ "proc-macro2", ] @@ -334,24 +334,24 @@ dependencies = [ [[package]] name = "target-lexicon" -version = "0.11.1" +version = "0.11.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee5a98e506fb7231a304c3a1bd7c132a55016cf65001e0282480665870dfcb9" +checksum = "422045212ea98508ae3d28025bc5aaa2bd4a9cdaecd442a08da2ee620ee9ea95" [[package]] name = "thiserror" -version = "1.0.23" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76cc616c6abf8c8928e2fdcc0dbfab37175edd8fb49a4641066ad1364fdab146" +checksum = "e0f4a65597094d4483ddaed134f409b2cb7c1beccf25201a9f73c719254fa98e" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.23" +version = "1.0.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9be73a2caec27583d0046ef3796c3794f868a5bc813db689eed00c7631275cd1" +checksum = "7765189610d8241a44529806d6fd1f2e0a08734313a35d5b3a556f92b381f3c0" dependencies = [ "proc-macro2", "quote", From c24744657021aa20bb577eb0e23cc8dcee55805c Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 23 Feb 2021 09:56:16 +0100 Subject: [PATCH 43/61] Disable macro-error rust-analyzer diagnostic --- .vscode/settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 19ea41563dfd6..a13d5931ffa89 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,6 @@ { // source for rustc_* is not included in the rust-src component; disable the errors about this - "rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate"], + "rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "macro-error"], "rust-analyzer.assist.importMergeBehavior": "last", "rust-analyzer.cargo.loadOutDirsFromCheck": true, "rust-analyzer.linkedProjects": [ From ef96a0f3f0235bb9a487fd5d8ee900bb4e9c4208 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Tue, 23 Feb 2021 18:48:45 +0100 Subject: [PATCH 44/61] Update Cranelift --- Cargo.lock | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ea7e70a329dc7..e01eca27ef7a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,7 +40,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "cranelift-entity", ] @@ -48,7 +48,7 @@ dependencies = [ [[package]] name = "cranelift-codegen" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "byteorder", "cranelift-bforest", @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -75,17 +75,17 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" [[package]] name = "cranelift-entity" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" [[package]] name = "cranelift-frontend" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "cranelift-codegen", "log", @@ -96,7 +96,7 @@ dependencies = [ [[package]] name = "cranelift-jit" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "anyhow", "cranelift-codegen", @@ -114,7 +114,7 @@ dependencies = [ [[package]] name = "cranelift-module" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "anyhow", "cranelift-codegen", @@ -126,7 +126,7 @@ dependencies = [ [[package]] name = "cranelift-native" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "cranelift-codegen", "target-lexicon", @@ -135,7 +135,7 @@ dependencies = [ [[package]] name = "cranelift-object" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#584d6fbf0e6f716f6a435ad7077c9c9b8f9f0abc" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" dependencies = [ "anyhow", "cranelift-codegen", From 73d3a1c14ad1703644667f8cb0df8e4d316aa4e1 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 22 Feb 2021 16:00:20 +0100 Subject: [PATCH 45/61] Use dynamic dispatch for the inner Module This improves compilation time and shrinks the linked backend from 13MB to 9.7MB on Linux. --- src/abi/comments.rs | 8 +++--- src/abi/mod.rs | 17 +++++------- src/abi/pass_mode.rs | 8 +++--- src/abi/returning.rs | 12 ++++----- src/analyze.rs | 2 +- src/base.rs | 28 +++++++------------- src/cast.rs | 4 +-- src/codegen_i128.rs | 2 +- src/common.rs | 18 ++++++------- src/constant.rs | 40 +++++++++++++--------------- src/discriminant.rs | 4 +-- src/driver/aot.rs | 6 ++--- src/driver/jit.rs | 17 ++++++------ src/driver/mod.rs | 2 +- src/inline_asm.rs | 4 +-- src/intrinsics/cpuid.rs | 2 +- src/intrinsics/llvm.rs | 2 +- src/intrinsics/mod.rs | 28 ++++++++++---------- src/intrinsics/simd.rs | 2 +- src/lib.rs | 24 ++++++++--------- src/num.rs | 14 +++++----- src/pointer.rs | 39 +++++---------------------- src/pretty_clif.rs | 4 +-- src/trap.rs | 12 ++++----- src/unsize.rs | 8 +++--- src/value_and_place.rs | 58 ++++++++++++++++------------------------- src/vtable.rs | 16 ++++++------ 27 files changed, 162 insertions(+), 219 deletions(-) diff --git a/src/abi/comments.rs b/src/abi/comments.rs index 9aab45b62e211..8570805fcd27b 100644 --- a/src/abi/comments.rs +++ b/src/abi/comments.rs @@ -10,14 +10,14 @@ use cranelift_codegen::entity::EntityRef; use crate::prelude::*; -pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) { +pub(super) fn add_args_header_comment(fx: &mut FunctionCx<'_, '_, '_>) { fx.add_global_comment( "kind loc.idx param pass mode ty".to_string(), ); } pub(super) fn add_arg_comment<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, kind: &str, local: Option, local_field: Option, @@ -62,7 +62,7 @@ pub(super) fn add_arg_comment<'tcx>( )); } -pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, impl Module>) { +pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, '_>) { fx.add_global_comment(String::new()); fx.add_global_comment( "kind local ty size align (abi,pref)".to_string(), @@ -70,7 +70,7 @@ pub(super) fn add_locals_header_comment(fx: &mut FunctionCx<'_, '_, impl Module> } pub(super) fn add_local_place_comments<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>, local: Local, ) { diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 32cfb58bd3556..849f293c41993 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -73,7 +73,7 @@ pub(crate) fn get_function_sig<'tcx>( /// Instance must be monomorphized pub(crate) fn import_function<'tcx>( tcx: TyCtxt<'tcx>, - module: &mut impl Module, + module: &mut dyn Module, inst: Instance<'tcx>, ) -> FuncId { let name = tcx.symbol_name(inst).name.to_string(); @@ -83,10 +83,10 @@ pub(crate) fn import_function<'tcx>( .unwrap() } -impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { +impl<'tcx> FunctionCx<'_, '_, 'tcx> { /// Instance must be monomorphized pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { - let func_id = import_function(self.tcx, &mut self.cx.module, inst); + let func_id = import_function(self.tcx, self.cx.module, inst); let func_ref = self .cx .module @@ -167,7 +167,7 @@ impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { /// Make a [`CPlace`] capable of holding value of the specified type. fn make_local_place<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, local: Local, layout: TyAndLayout<'tcx>, is_ssa: bool, @@ -188,10 +188,7 @@ fn make_local_place<'tcx>( place } -pub(crate) fn codegen_fn_prelude<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - start_block: Block, -) { +pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_block: Block) { fx.bcx.append_block_params_for_function_params(start_block); fx.bcx.switch_to_block(start_block); @@ -344,7 +341,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>( } pub(crate) fn codegen_terminator_call<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span, current_block: Block, func: &Operand<'tcx>, @@ -572,7 +569,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( } pub(crate) fn codegen_drop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span, drop_place: CPlace<'tcx>, ) { diff --git a/src/abi/pass_mode.rs b/src/abi/pass_mode.rs index 9dd1e6700291c..57b5ff9d02b9d 100644 --- a/src/abi/pass_mode.rs +++ b/src/abi/pass_mode.rs @@ -199,7 +199,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { } pub(super) fn to_casted_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, arg: CValue<'tcx>, cast: CastTarget, ) -> SmallVec<[Value; 2]> { @@ -219,7 +219,7 @@ pub(super) fn to_casted_value<'tcx>( } pub(super) fn from_casted_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, block_params: &[Value], layout: TyAndLayout<'tcx>, cast: CastTarget, @@ -258,7 +258,7 @@ pub(super) fn from_casted_value<'tcx>( /// Get a set of values to be passed as function arguments. pub(super) fn adjust_arg_for_abi<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, arg: CValue<'tcx>, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, ) -> SmallVec<[Value; 2]> { @@ -281,7 +281,7 @@ pub(super) fn adjust_arg_for_abi<'tcx>( /// Create a [`CValue`] containing the value of a function parameter adding clif function parameters /// as necessary. pub(super) fn cvalue_for_param<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, #[cfg_attr(not(debug_assertions), allow(unused_variables))] local: Option, #[cfg_attr(not(debug_assertions), allow(unused_variables))] local_field: Option, arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, diff --git a/src/abi/returning.rs b/src/abi/returning.rs index a382963bf1ed7..40a0320b57996 100644 --- a/src/abi/returning.rs +++ b/src/abi/returning.rs @@ -8,7 +8,7 @@ use smallvec::{smallvec, SmallVec}; /// Can the given type be returned into an ssa var or does it need to be returned on the stack. pub(crate) fn can_return_to_ssa_var<'tcx>( - fx: &FunctionCx<'_, 'tcx, impl Module>, + fx: &FunctionCx<'_, '_, 'tcx>, func: &mir::Operand<'tcx>, args: &[mir::Operand<'tcx>], ) -> bool { @@ -58,7 +58,7 @@ pub(crate) fn can_return_to_ssa_var<'tcx>( /// Return a place where the return value of the current function can be written to. If necessary /// this adds an extra parameter pointing to where the return value needs to be stored. pub(super) fn codegen_return_param<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, ssa_analyzed: &rustc_index::vec::IndexVec, block_params_iter: &mut impl Iterator, ) -> CPlace<'tcx> { @@ -120,11 +120,11 @@ pub(super) fn codegen_return_param<'tcx>( /// Invokes the closure with if necessary a value representing the return pointer. When the closure /// returns the call return value(s) if any are written to the correct place. -pub(super) fn codegen_with_call_return_arg<'tcx, M: Module, T>( - fx: &mut FunctionCx<'_, 'tcx, M>, +pub(super) fn codegen_with_call_return_arg<'tcx, T>( + fx: &mut FunctionCx<'_, '_, 'tcx>, ret_arg_abi: &ArgAbi<'tcx, Ty<'tcx>>, ret_place: Option>, - f: impl FnOnce(&mut FunctionCx<'_, 'tcx, M>, Option) -> (Inst, T), + f: impl FnOnce(&mut FunctionCx<'_, '_, 'tcx>, Option) -> (Inst, T), ) -> (Inst, T) { let return_ptr = match ret_arg_abi.mode { PassMode::Ignore => None, @@ -193,7 +193,7 @@ pub(super) fn codegen_with_call_return_arg<'tcx, M: Module, T>( } /// Codegen a return instruction with the right return value(s) if any. -pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, impl Module>) { +pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) { match fx.fn_abi.as_ref().unwrap().ret.mode { PassMode::Ignore | PassMode::Indirect { diff --git a/src/analyze.rs b/src/analyze.rs index 62fbcfe3f7a5d..9aeea38c09156 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -11,7 +11,7 @@ pub(crate) enum SsaKind { Ssa, } -pub(crate) fn analyze(fx: &FunctionCx<'_, '_, impl Module>) -> IndexVec { +pub(crate) fn analyze(fx: &FunctionCx<'_, '_, '_>) -> IndexVec { let mut flag_map = fx .mir .local_decls diff --git a/src/base.rs b/src/base.rs index 9b2249956c5ef..8a45e1bad42eb 100644 --- a/src/base.rs +++ b/src/base.rs @@ -8,7 +8,7 @@ use rustc_target::abi::call::FnAbi; use crate::prelude::*; pub(crate) fn codegen_fn<'tcx>( - cx: &mut crate::CodegenCx<'tcx, impl Module>, + cx: &mut crate::CodegenCx<'_, 'tcx>, instance: Instance<'tcx>, linkage: Linkage, ) { @@ -209,7 +209,7 @@ pub(crate) fn verify_func( }); } -fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) { +fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { for (bb, bb_data) in fx.mir.basic_blocks().iter_enumerated() { let block = fx.get_block(bb); fx.bcx.switch_to_block(block); @@ -453,7 +453,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) { } fn codegen_stmt<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, #[allow(unused_variables)] cur_block: Block, stmt: &Statement<'tcx>, ) { @@ -595,10 +595,7 @@ fn codegen_stmt<'tcx>( let from_ty = operand.layout().ty; let to_ty = fx.monomorphize(to_ty); - fn is_fat_ptr<'tcx>( - fx: &FunctionCx<'_, 'tcx, impl Module>, - ty: Ty<'tcx>, - ) -> bool { + fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { ty.builtin_deref(true) .map( |ty::TypeAndMut { @@ -895,10 +892,7 @@ fn codegen_stmt<'tcx>( } } -fn codegen_array_len<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - place: CPlace<'tcx>, -) -> Value { +fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>) -> Value { match *place.layout().ty.kind() { ty::Array(_elem_ty, len) => { let len = fx @@ -915,7 +909,7 @@ fn codegen_array_len<'tcx>( } pub(crate) fn codegen_place<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, place: Place<'tcx>, ) -> CPlace<'tcx> { let mut cplace = fx.get_local_place(place.local); @@ -987,7 +981,7 @@ pub(crate) fn codegen_place<'tcx>( } pub(crate) fn codegen_operand<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, operand: &Operand<'tcx>, ) -> CValue<'tcx> { match operand { @@ -999,11 +993,7 @@ pub(crate) fn codegen_operand<'tcx>( } } -pub(crate) fn codegen_panic<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - msg_str: &str, - span: Span, -) { +pub(crate) fn codegen_panic<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, msg_str: &str, span: Span) { let location = fx.get_caller_location(span).load_scalar(fx); let msg_ptr = fx.anonymous_str("assert", msg_str); @@ -1017,7 +1007,7 @@ pub(crate) fn codegen_panic<'tcx>( } pub(crate) fn codegen_panic_inner<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, lang_item: rustc_hir::LangItem, args: &[Value], span: Span, diff --git a/src/cast.rs b/src/cast.rs index 57204de1135be..432533c7636bc 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -3,7 +3,7 @@ use crate::prelude::*; pub(crate) fn clif_intcast( - fx: &mut FunctionCx<'_, '_, impl Module>, + fx: &mut FunctionCx<'_, '_, '_>, val: Value, to: Type, signed: bool, @@ -51,7 +51,7 @@ pub(crate) fn clif_intcast( } pub(crate) fn clif_int_or_float_cast( - fx: &mut FunctionCx<'_, '_, impl Module>, + fx: &mut FunctionCx<'_, '_, '_>, from: Value, from_signed: bool, to_ty: Type, diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index ed9097e5a5b58..60fffa60484e3 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -5,7 +5,7 @@ use cranelift_codegen::ir::ArgumentPurpose; use crate::prelude::*; pub(crate) fn maybe_codegen<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, checked: bool, lhs: CValue<'tcx>, diff --git a/src/common.rs b/src/common.rs index 1443aedb3b8a7..2c34c8ee5e832 100644 --- a/src/common.rs +++ b/src/common.rs @@ -130,7 +130,7 @@ pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { } pub(crate) fn codegen_icmp_imm( - fx: &mut FunctionCx<'_, '_, impl Module>, + fx: &mut FunctionCx<'_, '_, '_>, intcc: IntCC, lhs: Value, rhs: i128, @@ -241,8 +241,8 @@ pub(crate) fn type_sign(ty: Ty<'_>) -> bool { } } -pub(crate) struct FunctionCx<'clif, 'tcx, M: Module> { - pub(crate) cx: &'clif mut crate::CodegenCx<'tcx, M>, +pub(crate) struct FunctionCx<'m, 'clif, 'tcx> { + pub(crate) cx: &'clif mut crate::CodegenCx<'m, 'tcx>, pub(crate) tcx: TyCtxt<'tcx>, pub(crate) pointer_type: Type, // Cached from module @@ -269,7 +269,7 @@ pub(crate) struct FunctionCx<'clif, 'tcx, M: Module> { pub(crate) inline_asm_index: u32, } -impl<'tcx, M: Module> LayoutOf for FunctionCx<'_, 'tcx, M> { +impl<'tcx> LayoutOf for FunctionCx<'_, '_, 'tcx> { type Ty = Ty<'tcx>; type TyAndLayout = TyAndLayout<'tcx>; @@ -278,31 +278,31 @@ impl<'tcx, M: Module> LayoutOf for FunctionCx<'_, 'tcx, M> { } } -impl<'tcx, M: Module> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, M> { +impl<'tcx> layout::HasTyCtxt<'tcx> for FunctionCx<'_, '_, 'tcx> { fn tcx<'b>(&'b self) -> TyCtxt<'tcx> { self.tcx } } -impl<'tcx, M: Module> rustc_target::abi::HasDataLayout for FunctionCx<'_, 'tcx, M> { +impl<'tcx> rustc_target::abi::HasDataLayout for FunctionCx<'_, '_, 'tcx> { fn data_layout(&self) -> &rustc_target::abi::TargetDataLayout { &self.tcx.data_layout } } -impl<'tcx, M: Module> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, M> { +impl<'tcx> layout::HasParamEnv<'tcx> for FunctionCx<'_, '_, 'tcx> { fn param_env(&self) -> ParamEnv<'tcx> { ParamEnv::reveal_all() } } -impl<'tcx, M: Module> HasTargetSpec for FunctionCx<'_, 'tcx, M> { +impl<'tcx> HasTargetSpec for FunctionCx<'_, '_, 'tcx> { fn target_spec(&self) -> &Target { &self.tcx.sess.target } } -impl<'tcx, M: Module> FunctionCx<'_, 'tcx, M> { +impl<'tcx> FunctionCx<'_, '_, 'tcx> { pub(crate) fn monomorphize(&self, value: T) -> T where T: TypeFoldable<'tcx> + Copy, diff --git a/src/constant.rs b/src/constant.rs index 60f569bca9308..3712357746b74 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -28,7 +28,7 @@ enum TodoItem { } impl ConstantCx { - pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut impl Module) { + pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut dyn Module) { //println!("todo {:?}", self.todo); define_all_allocs(tcx, module, &mut self); //println!("done {:?}", self.done); @@ -36,7 +36,7 @@ impl ConstantCx { } } -pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) -> bool { +pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool { let mut all_constants_ok = true; for constant in &fx.mir.required_consts { let const_ = fx.monomorphize(constant.literal); @@ -79,11 +79,11 @@ pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) { } pub(crate) fn codegen_tls_ref<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, def_id: DefId, layout: TyAndLayout<'tcx>, ) -> CValue<'tcx> { - let data_id = data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false); + let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("tls {:?}", def_id)); @@ -92,11 +92,11 @@ pub(crate) fn codegen_tls_ref<'tcx>( } fn codegen_static_ref<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, def_id: DefId, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { - let data_id = data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false); + let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(local_data_id, format!("{:?}", def_id)); @@ -113,7 +113,7 @@ fn codegen_static_ref<'tcx>( } pub(crate) fn codegen_constant<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, constant: &Constant<'tcx>, ) -> CValue<'tcx> { let const_ = fx.monomorphize(constant.literal); @@ -155,7 +155,7 @@ pub(crate) fn codegen_constant<'tcx>( } pub(crate) fn codegen_const_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, const_val: ConstValue<'tcx>, ty: Ty<'tcx>, ) -> CValue<'tcx> { @@ -189,11 +189,8 @@ pub(crate) fn codegen_const_value<'tcx>( let base_addr = match alloc_kind { Some(GlobalAlloc::Memory(alloc)) => { fx.cx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id)); - let data_id = data_id_for_alloc_id( - &mut fx.cx.module, - ptr.alloc_id, - alloc.mutability, - ); + let data_id = + data_id_for_alloc_id(fx.cx.module, ptr.alloc_id, alloc.mutability); let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] @@ -202,15 +199,14 @@ pub(crate) fn codegen_const_value<'tcx>( } Some(GlobalAlloc::Function(instance)) => { let func_id = - crate::abi::import_function(fx.tcx, &mut fx.cx.module, instance); + crate::abi::import_function(fx.tcx, fx.cx.module, instance); let local_func_id = fx.cx.module.declare_func_in_func(func_id, &mut fx.bcx.func); fx.bcx.ins().func_addr(fx.pointer_type, local_func_id) } Some(GlobalAlloc::Static(def_id)) => { assert!(fx.tcx.is_static(def_id)); - let data_id = - data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false); + let data_id = data_id_for_static(fx.tcx, fx.cx.module, def_id, false); let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] @@ -249,12 +245,12 @@ pub(crate) fn codegen_const_value<'tcx>( } fn pointer_for_allocation<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, alloc: &'tcx Allocation, ) -> crate::pointer::Pointer { let alloc_id = fx.tcx.create_memory_alloc(alloc); fx.cx.constants_cx.todo.push(TodoItem::Alloc(alloc_id)); - let data_id = data_id_for_alloc_id(&mut fx.cx.module, alloc_id, alloc.mutability); + let data_id = data_id_for_alloc_id(fx.cx.module, alloc_id, alloc.mutability); let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func); #[cfg(debug_assertions)] @@ -264,7 +260,7 @@ fn pointer_for_allocation<'tcx>( } fn data_id_for_alloc_id( - module: &mut impl Module, + module: &mut dyn Module, alloc_id: AllocId, mutability: rustc_hir::Mutability, ) -> DataId { @@ -280,7 +276,7 @@ fn data_id_for_alloc_id( fn data_id_for_static( tcx: TyCtxt<'_>, - module: &mut impl Module, + module: &mut dyn Module, def_id: DefId, definition: bool, ) -> DataId { @@ -355,7 +351,7 @@ fn data_id_for_static( } } -fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut impl Module, cx: &mut ConstantCx) { +fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut ConstantCx) { while let Some(todo_item) = cx.todo.pop() { let (data_id, alloc, section_name) = match todo_item { TodoItem::Alloc(alloc_id) => { @@ -456,7 +452,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut impl Module, cx: &mut Constan } pub(crate) fn mir_operand_get_const_val<'tcx>( - fx: &FunctionCx<'_, 'tcx, impl Module>, + fx: &FunctionCx<'_, '_, 'tcx>, operand: &Operand<'tcx>, ) -> Option<&'tcx Const<'tcx>> { match operand { diff --git a/src/discriminant.rs b/src/discriminant.rs index ad635016a9136..3815e149ffa1a 100644 --- a/src/discriminant.rs +++ b/src/discriminant.rs @@ -7,7 +7,7 @@ use rustc_target::abi::{Int, TagEncoding, Variants}; use crate::prelude::*; pub(crate) fn codegen_set_discriminant<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>, variant_index: VariantIdx, ) { @@ -70,7 +70,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>( } pub(crate) fn codegen_get_discriminant<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, value: CValue<'tcx>, dest_layout: TyAndLayout<'tcx>, ) -> CValue<'tcx> { diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 1a26642a49b3e..07c6c27817c54 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -119,12 +119,12 @@ fn module_codegen( let cgu = tcx.codegen_unit(cgu_name); let mono_items = cgu.items_in_deterministic_order(tcx); - let module = new_module(tcx, cgu_name.as_str().to_string()); + let mut module = new_module(tcx, cgu_name.as_str().to_string()); let mut cx = crate::CodegenCx::new( tcx, backend_config, - module, + &mut module, tcx.sess.opts.debuginfo != DebugInfo::None, ); super::predefine_mono_items(&mut cx, &mono_items); @@ -150,7 +150,7 @@ fn module_codegen( } } } - let (mut module, global_asm, debug, mut unwind_context) = + let (global_asm, debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize()); crate::main_shim::maybe_create_entry_wrapper(tcx, &mut module, &mut unwind_context); diff --git a/src/driver/jit.rs b/src/driver/jit.rs index bab4586e7f622..beb316c58c3d1 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -57,7 +57,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { .into_iter() .collect::>(); - let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); + let mut cx = crate::CodegenCx::new(tcx, backend_config, &mut jit_module, false); super::time(tcx, "codegen mono items", || { super::predefine_mono_items(&mut cx, &mono_items); @@ -85,7 +85,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { } }); - let (mut jit_module, global_asm, _debug, mut unwind_context) = + let (global_asm, _debug, mut unwind_context) = tcx.sess.time("finalize CodegenCx", || cx.finalize()); jit_module.finalize_definitions(); @@ -145,21 +145,20 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8 let jit_module = jit_module.as_mut().unwrap(); let backend_config = BACKEND_CONFIG.with(|backend_config| backend_config.borrow().clone().unwrap()); - let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); let name = tcx.symbol_name(instance).name.to_string(); - let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), instance); - let func_id = cx - .module + let sig = crate::abi::get_function_sig(tcx, jit_module.isa().triple(), instance); + let func_id = jit_module .declare_function(&name, Linkage::Export, &sig) .unwrap(); - cx.module.prepare_for_function_redefine(func_id).unwrap(); + jit_module.prepare_for_function_redefine(func_id).unwrap(); + let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); tcx.sess.time("codegen fn", || { crate::base::codegen_fn(&mut cx, instance, Linkage::Export) }); - let (jit_module, global_asm, _debug_context, unwind_context) = cx.finalize(); + let (global_asm, _debug_context, unwind_context) = cx.finalize(); assert!(global_asm.is_empty()); jit_module.finalize_definitions(); std::mem::forget(unsafe { unwind_context.register_jit(&jit_module) }); @@ -233,7 +232,7 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { imported_symbols } -pub(super) fn codegen_shim<'tcx>(cx: &mut CodegenCx<'tcx, impl Module>, inst: Instance<'tcx>) { +pub(super) fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'tcx>) { let tcx = cx.tcx; let pointer_type = cx.module.target_config().pointer_type(); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 360810cb58e18..91cfde654f938 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -43,7 +43,7 @@ pub(crate) fn codegen_crate( } fn predefine_mono_items<'tcx>( - cx: &mut crate::CodegenCx<'tcx, impl Module>, + cx: &mut crate::CodegenCx<'_, 'tcx>, mono_items: &[(MonoItem<'tcx>, (RLinkage, Visibility))], ) { cx.tcx.sess.time("predefine functions", || { diff --git a/src/inline_asm.rs b/src/inline_asm.rs index 04aac780125d9..c8f027e7b1a5b 100644 --- a/src/inline_asm.rs +++ b/src/inline_asm.rs @@ -9,7 +9,7 @@ use rustc_middle::mir::InlineAsmOperand; use rustc_target::asm::*; pub(crate) fn codegen_inline_asm<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, _span: Span, template: &[InlineAsmTemplatePiece], operands: &[InlineAsmOperand<'tcx>], @@ -203,7 +203,7 @@ fn generate_asm_wrapper( } fn call_inline_asm<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, asm_name: &str, slot_size: Size, inputs: Vec<(InlineAsmReg, Size, Value)>, diff --git a/src/intrinsics/cpuid.rs b/src/intrinsics/cpuid.rs index c1a1cdbe4eb7d..4c82e793ad265 100644 --- a/src/intrinsics/cpuid.rs +++ b/src/intrinsics/cpuid.rs @@ -6,7 +6,7 @@ use crate::prelude::*; /// /// This emulates an intel cpu with sse and sse2 support, but which doesn't support anything else. pub(crate) fn codegen_cpuid_call<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, leaf: Value, _subleaf: Value, ) -> (Value, Value, Value, Value) { diff --git a/src/intrinsics/llvm.rs b/src/intrinsics/llvm.rs index d58e4d4995842..0692da397eb94 100644 --- a/src/intrinsics/llvm.rs +++ b/src/intrinsics/llvm.rs @@ -6,7 +6,7 @@ use crate::prelude::*; use rustc_middle::ty::subst::SubstsRef; pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, intrinsic: &str, substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 4aed09d2b143b..f0ca7400d5031 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -153,12 +153,12 @@ pub(crate) fn clif_vector_type<'tcx>(tcx: TyCtxt<'tcx>, layout: TyAndLayout<'tcx } } -fn simd_for_each_lane<'tcx, M: Module>( - fx: &mut FunctionCx<'_, 'tcx, M>, +fn simd_for_each_lane<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, val: CValue<'tcx>, ret: CPlace<'tcx>, f: impl Fn( - &mut FunctionCx<'_, 'tcx, M>, + &mut FunctionCx<'_, '_, 'tcx>, TyAndLayout<'tcx>, TyAndLayout<'tcx>, Value, @@ -182,13 +182,13 @@ fn simd_for_each_lane<'tcx, M: Module>( } } -fn simd_pair_for_each_lane<'tcx, M: Module>( - fx: &mut FunctionCx<'_, 'tcx, M>, +fn simd_pair_for_each_lane<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, x: CValue<'tcx>, y: CValue<'tcx>, ret: CPlace<'tcx>, f: impl Fn( - &mut FunctionCx<'_, 'tcx, M>, + &mut FunctionCx<'_, '_, 'tcx>, TyAndLayout<'tcx>, TyAndLayout<'tcx>, Value, @@ -215,11 +215,11 @@ fn simd_pair_for_each_lane<'tcx, M: Module>( } } -fn simd_reduce<'tcx, M: Module>( - fx: &mut FunctionCx<'_, 'tcx, M>, +fn simd_reduce<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, val: CValue<'tcx>, ret: CPlace<'tcx>, - f: impl Fn(&mut FunctionCx<'_, 'tcx, M>, TyAndLayout<'tcx>, Value, Value) -> Value, + f: impl Fn(&mut FunctionCx<'_, '_, 'tcx>, TyAndLayout<'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); @@ -236,11 +236,11 @@ fn simd_reduce<'tcx, M: Module>( ret.write_cvalue(fx, res); } -fn simd_reduce_bool<'tcx, M: Module>( - fx: &mut FunctionCx<'_, 'tcx, M>, +fn simd_reduce_bool<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, val: CValue<'tcx>, ret: CPlace<'tcx>, - f: impl Fn(&mut FunctionCx<'_, 'tcx, M>, Value, Value) -> Value, + f: impl 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()); @@ -259,7 +259,7 @@ fn simd_reduce_bool<'tcx, M: Module>( } fn bool_to_zero_or_max_uint<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, val: Value, ) -> CValue<'tcx> { @@ -393,7 +393,7 @@ macro simd_flt_binop($fx:expr, $op:ident($x:ident, $y:ident) -> $ret:ident) { } pub(crate) fn codegen_intrinsic_call<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, instance: Instance<'tcx>, args: &[mir::Operand<'tcx>], destination: Option<(CPlace<'tcx>, BasicBlock)>, diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index e0eb5c59590ff..1f8eeb1e71491 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -4,7 +4,7 @@ use super::*; use crate::prelude::*; pub(super) fn codegen_simd_intrinsic_call<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, instance: Instance<'tcx>, args: &[mir::Operand<'tcx>], ret: CPlace<'tcx>, diff --git a/src/lib.rs b/src/lib.rs index 417934f760326..122ad4e366dda 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,9 +127,9 @@ impl String> Drop for PrintOnPanic { } } -struct CodegenCx<'tcx, M: Module> { +struct CodegenCx<'m, 'tcx: 'm> { tcx: TyCtxt<'tcx>, - module: M, + module: &'m mut dyn Module, global_asm: String, constants_cx: ConstantCx, cached_context: Context, @@ -138,8 +138,13 @@ struct CodegenCx<'tcx, M: Module> { unwind_context: UnwindContext<'tcx>, } -impl<'tcx, M: Module> CodegenCx<'tcx, M> { - fn new(tcx: TyCtxt<'tcx>, backend_config: BackendConfig, module: M, debug_info: bool) -> Self { +impl<'m, 'tcx> CodegenCx<'m, 'tcx> { + fn new( + tcx: TyCtxt<'tcx>, + backend_config: BackendConfig, + module: &'m mut dyn Module, + debug_info: bool, + ) -> Self { let unwind_context = UnwindContext::new( tcx, module.isa(), @@ -162,14 +167,9 @@ impl<'tcx, M: Module> CodegenCx<'tcx, M> { } } - fn finalize(mut self) -> (M, String, Option>, UnwindContext<'tcx>) { - self.constants_cx.finalize(self.tcx, &mut self.module); - ( - self.module, - self.global_asm, - self.debug_context, - self.unwind_context, - ) + fn finalize(self) -> (String, Option>, UnwindContext<'tcx>) { + self.constants_cx.finalize(self.tcx, self.module); + (self.global_asm, self.debug_context, self.unwind_context) } } diff --git a/src/num.rs b/src/num.rs index 2022614841dec..4fc0cdfe0e79d 100644 --- a/src/num.rs +++ b/src/num.rs @@ -41,7 +41,7 @@ pub(crate) fn bin_op_to_intcc(bin_op: BinOp, signed: bool) -> Option { } fn codegen_compare_bin_op<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, signed: bool, lhs: Value, @@ -54,7 +54,7 @@ fn codegen_compare_bin_op<'tcx>( } pub(crate) fn codegen_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, @@ -103,7 +103,7 @@ pub(crate) fn codegen_binop<'tcx>( } pub(crate) fn codegen_bool_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, @@ -124,7 +124,7 @@ pub(crate) fn codegen_bool_binop<'tcx>( } pub(crate) fn codegen_int_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, @@ -197,7 +197,7 @@ pub(crate) fn codegen_int_binop<'tcx>( } pub(crate) fn codegen_checked_int_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, @@ -350,7 +350,7 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( } pub(crate) fn codegen_float_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, @@ -395,7 +395,7 @@ pub(crate) fn codegen_float_binop<'tcx>( } pub(crate) fn codegen_ptr_binop<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, bin_op: BinOp, in_lhs: CValue<'tcx>, in_rhs: CValue<'tcx>, diff --git a/src/pointer.rs b/src/pointer.rs index 7b31d0a20078d..03b1e6cc4acee 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -36,10 +36,7 @@ impl Pointer { } } - pub(crate) fn const_addr<'a, 'tcx>( - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - addr: i64, - ) -> Self { + pub(crate) fn const_addr(fx: &mut FunctionCx<'_, '_, '_>, addr: i64) -> Self { let addr = fx.bcx.ins().iconst(fx.pointer_type, addr); Pointer { base: PointerBase::Addr(addr), @@ -59,7 +56,7 @@ impl Pointer { (self.base, self.offset) } - pub(crate) fn get_addr<'a, 'tcx>(self, fx: &mut FunctionCx<'a, 'tcx, impl Module>) -> Value { + pub(crate) fn get_addr(self, fx: &mut FunctionCx<'_, '_, '_>) -> Value { match self.base { PointerBase::Addr(base_addr) => { let offset: i64 = self.offset.into(); @@ -81,19 +78,11 @@ impl Pointer { } } - pub(crate) fn offset<'a, 'tcx>( - self, - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - extra_offset: Offset32, - ) -> Self { + pub(crate) fn offset(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Offset32) -> Self { self.offset_i64(fx, extra_offset.into()) } - pub(crate) fn offset_i64<'a, 'tcx>( - self, - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - extra_offset: i64, - ) -> Self { + pub(crate) fn offset_i64(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: i64) -> Self { if let Some(new_offset) = self.offset.try_add_i64(extra_offset) { Pointer { base: self.base, @@ -126,11 +115,7 @@ impl Pointer { } } - pub(crate) fn offset_value<'a, 'tcx>( - self, - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - extra_offset: Value, - ) -> Self { + pub(crate) fn offset_value(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: Value) -> Self { match self.base { PointerBase::Addr(addr) => Pointer { base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)), @@ -159,12 +144,7 @@ impl Pointer { } } - pub(crate) fn load<'a, 'tcx>( - self, - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - ty: Type, - flags: MemFlags, - ) -> Value { + pub(crate) fn load(self, fx: &mut FunctionCx<'_, '_, '_>, ty: Type, flags: MemFlags) -> Value { match self.base { PointerBase::Addr(base_addr) => fx.bcx.ins().load(ty, flags, base_addr, self.offset), PointerBase::Stack(stack_slot) => fx.bcx.ins().stack_load(ty, stack_slot, self.offset), @@ -172,12 +152,7 @@ impl Pointer { } } - pub(crate) fn store<'a, 'tcx>( - self, - fx: &mut FunctionCx<'a, 'tcx, impl Module>, - value: Value, - flags: MemFlags, - ) { + pub(crate) fn store(self, fx: &mut FunctionCx<'_, '_, '_>, value: Value, flags: MemFlags) { match self.base { PointerBase::Addr(base_addr) => { fx.bcx.ins().store(flags, value, base_addr, self.offset); diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index 71020b4047374..a5ae7fa829d56 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -186,7 +186,7 @@ impl FuncWriter for &'_ CommentWriter { } #[cfg(debug_assertions)] -impl FunctionCx<'_, '_, M> { +impl FunctionCx<'_, '_, '_> { pub(crate) fn add_global_comment>(&mut self, comment: S) { self.clif_comments.add_global_comment(comment); } @@ -276,7 +276,7 @@ pub(crate) fn write_clif_file<'tcx>( ); } -impl fmt::Debug for FunctionCx<'_, '_, M> { +impl fmt::Debug for FunctionCx<'_, '_, '_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "{:?}", self.instance.substs)?; writeln!(f, "{:?}", self.local_map)?; diff --git a/src/trap.rs b/src/trap.rs index 67495c7414840..bb63d72addf98 100644 --- a/src/trap.rs +++ b/src/trap.rs @@ -2,7 +2,7 @@ use crate::prelude::*; -fn codegen_print(fx: &mut FunctionCx<'_, '_, impl Module>, msg: &str) { +fn codegen_print(fx: &mut FunctionCx<'_, '_, '_>, msg: &str) { let puts = fx .cx .module @@ -29,7 +29,7 @@ fn codegen_print(fx: &mut FunctionCx<'_, '_, impl Module>, msg: &str) { } /// Trap code: user1 -pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, impl Module>, msg: impl AsRef) { +pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef) { codegen_print(fx, msg.as_ref()); fx.bcx.ins().trap(TrapCode::User(1)); } @@ -38,7 +38,7 @@ pub(crate) fn trap_abort(fx: &mut FunctionCx<'_, '_, impl Module>, msg: impl AsR /// so you can **not** add instructions to it afterwards. /// /// Trap code: user65535 -pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, impl Module>, msg: impl AsRef) { +pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef) { codegen_print(fx, msg.as_ref()); fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } @@ -47,7 +47,7 @@ pub(crate) fn trap_unreachable(fx: &mut FunctionCx<'_, '_, impl Module>, msg: im /// /// Trap code: user65535 pub(crate) fn trap_unreachable_ret_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, dest_layout: TyAndLayout<'tcx>, msg: impl AsRef, ) -> CValue<'tcx> { @@ -62,7 +62,7 @@ pub(crate) fn trap_unreachable_ret_value<'tcx>( /// to it afterwards. /// /// Trap code: user65535 -pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, impl Module>, msg: impl AsRef) { +pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, '_>, msg: impl AsRef) { codegen_print(fx, msg.as_ref()); let true_ = fx.bcx.ins().iconst(types::I32, 1); fx.bcx.ins().trapnz(true_, TrapCode::User(!0)); @@ -72,7 +72,7 @@ pub(crate) fn trap_unimplemented(fx: &mut FunctionCx<'_, '_, impl Module>, msg: /// /// Trap code: user65535 pub(crate) fn trap_unimplemented_ret_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, dest_layout: TyAndLayout<'tcx>, msg: impl AsRef, ) -> CValue<'tcx> { diff --git a/src/unsize.rs b/src/unsize.rs index c77ff5d56ba62..109aee5a04411 100644 --- a/src/unsize.rs +++ b/src/unsize.rs @@ -13,7 +13,7 @@ use crate::prelude::*; /// in an upcast, where the new vtable for an object will be derived /// from the old one. pub(crate) fn unsized_info<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, source: Ty<'tcx>, target: Ty<'tcx>, old_info: Option, @@ -45,7 +45,7 @@ pub(crate) fn unsized_info<'tcx>( /// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer. fn unsize_thin_ptr<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, src: Value, src_layout: TyAndLayout<'tcx>, dst_layout: TyAndLayout<'tcx>, @@ -89,7 +89,7 @@ fn unsize_thin_ptr<'tcx>( /// Coerce `src`, which is a reference to a value of type `src_ty`, /// to a value of type `dst_ty` and store the result in `dst` pub(crate) fn coerce_unsized_into<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, src: CValue<'tcx>, dst: CPlace<'tcx>, ) { @@ -142,7 +142,7 @@ pub(crate) fn coerce_unsized_into<'tcx>( // Adapted from https://github.com/rust-lang/rust/blob/2a663555ddf36f6b041445894a8c175cd1bc718c/src/librustc_codegen_ssa/glue.rs pub(crate) fn size_and_align_of_dst<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, info: Value, ) -> (Value, Value) { diff --git a/src/value_and_place.rs b/src/value_and_place.rs index c2520b5c6c4b5..0dad9b2edafdf 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -6,7 +6,7 @@ use cranelift_codegen::entity::EntityRef; use cranelift_codegen::ir::immediates::Offset32; fn codegen_field<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, base: Pointer, extra: Option, layout: TyAndLayout<'tcx>, @@ -15,7 +15,7 @@ fn codegen_field<'tcx>( let field_offset = layout.fields.offset(field.index()); let field_layout = layout.field(&*fx, field.index()); - let simple = |fx: &mut FunctionCx<'_, '_, _>| { + let simple = |fx: &mut FunctionCx<'_, '_, '_>| { ( base.offset_i64(fx, i64::try_from(field_offset.bytes()).unwrap()), field_layout, @@ -106,10 +106,7 @@ impl<'tcx> CValue<'tcx> { } // FIXME remove - pub(crate) fn force_stack( - self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - ) -> (Pointer, Option) { + pub(crate) fn force_stack(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> (Pointer, Option) { let layout = self.1; match self.0 { CValueInner::ByRef(ptr, meta) => (ptr, meta), @@ -129,7 +126,7 @@ impl<'tcx> CValue<'tcx> { } /// Load a value with layout.abi of scalar - pub(crate) fn load_scalar(self, fx: &mut FunctionCx<'_, 'tcx, impl Module>) -> Value { + pub(crate) fn load_scalar(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> Value { let layout = self.1; match self.0 { CValueInner::ByRef(ptr, None) => { @@ -153,10 +150,7 @@ impl<'tcx> CValue<'tcx> { } /// Load a value pair with layout.abi of scalar pair - pub(crate) fn load_scalar_pair( - self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - ) -> (Value, Value) { + pub(crate) fn load_scalar_pair(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> (Value, Value) { let layout = self.1; match self.0 { CValueInner::ByRef(ptr, None) => { @@ -183,7 +177,7 @@ impl<'tcx> CValue<'tcx> { pub(crate) fn value_field( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, field: mir::Field, ) -> CValue<'tcx> { let layout = self.1; @@ -219,17 +213,13 @@ impl<'tcx> CValue<'tcx> { } } - pub(crate) fn unsize_value( - self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - dest: CPlace<'tcx>, - ) { + pub(crate) fn unsize_value(self, fx: &mut FunctionCx<'_, '_, 'tcx>, dest: CPlace<'tcx>) { crate::unsize::coerce_unsized_into(fx, self, dest); } /// If `ty` is signed, `const_val` must already be sign extended. pub(crate) fn const_val( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, const_val: ty::ScalarInt, ) -> CValue<'tcx> { @@ -330,7 +320,7 @@ impl<'tcx> CPlace<'tcx> { } pub(crate) fn new_stack_slot( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { assert!(!layout.is_unsized()); @@ -352,7 +342,7 @@ impl<'tcx> CPlace<'tcx> { } pub(crate) fn new_var( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, local: Local, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { @@ -366,7 +356,7 @@ impl<'tcx> CPlace<'tcx> { } pub(crate) fn new_var_pair( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, local: Local, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { @@ -402,7 +392,7 @@ impl<'tcx> CPlace<'tcx> { } } - pub(crate) fn to_cvalue(self, fx: &mut FunctionCx<'_, 'tcx, impl Module>) -> CValue<'tcx> { + pub(crate) fn to_cvalue(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> CValue<'tcx> { let layout = self.layout(); match self.inner { CPlaceInner::Var(_local, var) => { @@ -453,11 +443,7 @@ impl<'tcx> CPlace<'tcx> { } } - pub(crate) fn write_cvalue( - self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, - from: CValue<'tcx>, - ) { + pub(crate) fn write_cvalue(self, fx: &mut FunctionCx<'_, '_, 'tcx>, from: CValue<'tcx>) { assert_assignable(fx, from.layout().ty, self.layout().ty); self.write_cvalue_maybe_transmute(fx, from, "write_cvalue"); @@ -465,7 +451,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn write_cvalue_transmute( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, from: CValue<'tcx>, ) { self.write_cvalue_maybe_transmute(fx, from, "write_cvalue_transmute"); @@ -473,12 +459,12 @@ impl<'tcx> CPlace<'tcx> { fn write_cvalue_maybe_transmute( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, from: CValue<'tcx>, #[cfg_attr(not(debug_assertions), allow(unused_variables))] method: &'static str, ) { fn transmute_value<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, var: Variable, data: Value, dst_ty: Type, @@ -638,7 +624,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn place_field( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, field: mir::Field, ) -> CPlace<'tcx> { let layout = self.layout(); @@ -686,7 +672,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn place_index( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, index: Value, ) -> CPlace<'tcx> { let (elem_layout, ptr) = match self.layout().ty.kind() { @@ -703,7 +689,7 @@ impl<'tcx> CPlace<'tcx> { CPlace::for_ptr(ptr.offset_value(fx, offset), elem_layout) } - pub(crate) fn place_deref(self, fx: &mut FunctionCx<'_, 'tcx, impl Module>) -> CPlace<'tcx> { + pub(crate) fn place_deref(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> CPlace<'tcx> { let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap().ty); if has_ptr_meta(fx.tcx, inner_layout.ty) { let (addr, extra) = self.to_cvalue(fx).load_scalar_pair(fx); @@ -718,7 +704,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn place_ref( self, - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, ) -> CValue<'tcx> { if has_ptr_meta(fx.tcx, self.layout().ty) { @@ -735,7 +721,7 @@ impl<'tcx> CPlace<'tcx> { pub(crate) fn downcast_variant( self, - fx: &FunctionCx<'_, 'tcx, impl Module>, + fx: &FunctionCx<'_, '_, 'tcx>, variant: VariantIdx, ) -> Self { assert!(!self.layout().is_unsized()); @@ -749,7 +735,7 @@ impl<'tcx> CPlace<'tcx> { #[track_caller] pub(crate) fn assert_assignable<'tcx>( - fx: &FunctionCx<'_, 'tcx, impl Module>, + fx: &FunctionCx<'_, '_, 'tcx>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>, ) { diff --git a/src/vtable.rs b/src/vtable.rs index 8f15586a9dc06..a1f0aa40f0f16 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -15,7 +15,7 @@ fn vtable_memflags() -> MemFlags { flags } -pub(crate) fn drop_fn_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value { +pub(crate) fn drop_fn_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value { let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize; fx.bcx.ins().load( pointer_ty(fx.tcx), @@ -25,7 +25,7 @@ pub(crate) fn drop_fn_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: V ) } -pub(crate) fn size_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value { +pub(crate) fn size_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value { let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize; fx.bcx.ins().load( pointer_ty(fx.tcx), @@ -35,7 +35,7 @@ pub(crate) fn size_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Valu ) } -pub(crate) fn min_align_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: Value) -> Value { +pub(crate) fn min_align_of_obj(fx: &mut FunctionCx<'_, '_, '_>, vtable: Value) -> Value { let usize_size = fx.layout_of(fx.tcx.types.usize).size.bytes() as usize; fx.bcx.ins().load( pointer_ty(fx.tcx), @@ -46,7 +46,7 @@ pub(crate) fn min_align_of_obj(fx: &mut FunctionCx<'_, '_, impl Module>, vtable: } pub(crate) fn get_ptr_and_method_ref<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, arg: CValue<'tcx>, idx: usize, ) -> (Value, Value) { @@ -68,7 +68,7 @@ pub(crate) fn get_ptr_and_method_ref<'tcx>( } pub(crate) fn get_vtable<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, trait_ref: Option>, ) -> Value { @@ -85,7 +85,7 @@ pub(crate) fn get_vtable<'tcx>( } fn build_vtable<'tcx>( - fx: &mut FunctionCx<'_, 'tcx, impl Module>, + fx: &mut FunctionCx<'_, '_, 'tcx>, layout: TyAndLayout<'tcx>, trait_ref: Option>, ) -> DataId { @@ -94,7 +94,7 @@ fn build_vtable<'tcx>( let drop_in_place_fn = import_function( tcx, - &mut fx.cx.module, + fx.cx.module, Instance::resolve_drop_in_place(tcx, layout.ty).polymorphize(fx.tcx), ); @@ -111,7 +111,7 @@ fn build_vtable<'tcx>( opt_mth.map(|(def_id, substs)| { import_function( tcx, - &mut fx.cx.module, + fx.cx.module, Instance::resolve_for_vtable(tcx, ParamEnv::reveal_all(), def_id, substs) .unwrap() .polymorphize(fx.tcx), From 27a5f4f4727db27db8cbf37ca74cb7ea4f9a820b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 1 Mar 2021 12:19:22 +0100 Subject: [PATCH 46/61] Update Cranelift Fixes #1143 --- Cargo.lock | 20 ++++++++++---------- example/mini_core.rs | 16 ++++++++++++++++ example/mini_core_hello_world.rs | 3 +++ 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e01eca27ef7a3..76d9f0d27ce46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,7 +40,7 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "cranelift-entity", ] @@ -48,7 +48,7 @@ dependencies = [ [[package]] name = "cranelift-codegen" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "byteorder", "cranelift-bforest", @@ -66,7 +66,7 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "cranelift-codegen-shared", "cranelift-entity", @@ -75,17 +75,17 @@ dependencies = [ [[package]] name = "cranelift-codegen-shared" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" [[package]] name = "cranelift-entity" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" [[package]] name = "cranelift-frontend" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "cranelift-codegen", "log", @@ -96,7 +96,7 @@ dependencies = [ [[package]] name = "cranelift-jit" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "anyhow", "cranelift-codegen", @@ -114,7 +114,7 @@ dependencies = [ [[package]] name = "cranelift-module" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "anyhow", "cranelift-codegen", @@ -126,7 +126,7 @@ dependencies = [ [[package]] name = "cranelift-native" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "cranelift-codegen", "target-lexicon", @@ -135,7 +135,7 @@ dependencies = [ [[package]] name = "cranelift-object" version = "0.70.0" -source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#ee1ddfb6f95750d97598b77f3ebf129a15a241b2" +source = "git+https://github.com/bytecodealliance/wasmtime/?branch=main#cdb60ec5a9df087262ae8960a31067e88cd80058" dependencies = [ "anyhow", "cranelift-codegen", diff --git a/example/mini_core.rs b/example/mini_core.rs index 002ec7e2e3d7a..7c6d7fc106ded 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -365,6 +365,22 @@ impl PartialEq for Option { } } +#[lang = "shl"] +pub trait Shl { + type Output; + + #[must_use] + fn shl(self, rhs: RHS) -> Self::Output; +} + +impl Shl for u128 { + type Output = u128; + + fn shl(self, rhs: u128) -> u128 { + self << rhs + } +} + #[lang = "neg"] pub trait Neg { type Output; diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 4a8375afac3ce..237f4d11d57f1 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -264,6 +264,9 @@ fn main() { assert_eq!(f2 as i8, -128); assert_eq!(f2 as u8, 0); + let amount = 0; + assert_eq!(1u128 << amount, 1); + static ANOTHER_STATIC: &u8 = &A_STATIC; assert_eq!(*ANOTHER_STATIC, 42); From 7d33e5bf3d3fa9750e84dccd6a3cb717630206f9 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 1 Mar 2021 12:34:49 +0100 Subject: [PATCH 47/61] Fix bootstrap test script --- scripts/test_bootstrap.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/test_bootstrap.sh b/scripts/test_bootstrap.sh index 5bc4548844468..6473c6ad67d2e 100755 --- a/scripts/test_bootstrap.sh +++ b/scripts/test_bootstrap.sh @@ -19,8 +19,8 @@ index 5bd1147cad5..10d68a2ff14 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,5 +111,7 @@ rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' } - # source code for this crate. - backtrace = { path = "library/backtrace" } + rustc-std-workspace-alloc = { path = 'library/rustc-std-workspace-alloc' } + rustc-std-workspace-std = { path = 'library/rustc-std-workspace-std' } +compiler_builtins = { path = "../build_sysroot/compiler-builtins" } + From 44c2794976986677c4f5524f63562d02e0e53d57 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 28 Feb 2021 22:41:05 -0500 Subject: [PATCH 48/61] Clean up error reporting for deprecated passes - Use spans for deprecated attributes - Use a proper diagnostic for unknown passes, instead of error logging - Add tests for unknown passes - Improve some wording in diagnostics --- src/librustdoc/config.rs | 5 +- src/librustdoc/core.rs | 57 ++++++++++++--------- src/test/rustdoc-ui/deprecated-attrs.rs | 16 ++++-- src/test/rustdoc-ui/deprecated-attrs.stderr | 32 ++++++++++-- 4 files changed, 74 insertions(+), 36 deletions(-) diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index d9f5b5bfa3ae2..6a62cacd34d18 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -654,9 +654,8 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &rustc_errors::Han { continue; } - let mut err = - diag.struct_warn(&format!("the '{}' flag is considered deprecated", flag)); - err.warn( + let mut err = diag.struct_warn(&format!("the `{}` flag is deprecated", flag)); + err.note( "see issue #44136 \ for more information", ); diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 6218aaf8276d5..a935bd6b456d5 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -22,7 +22,7 @@ use rustc_session::DiagnosticOutput; use rustc_session::Session; use rustc_span::source_map; use rustc_span::symbol::sym; -use rustc_span::DUMMY_SP; +use rustc_span::{Span, DUMMY_SP}; use std::mem; use std::rc::Rc; @@ -461,7 +461,7 @@ crate fn run_global_ctxt( tcx: TyCtxt<'_>, resolver: Rc>, mut default_passes: passes::DefaultPassOption, - mut manual_passes: Vec, + manual_passes: Vec, render_options: RenderOptions, output_format: OutputFormat, ) -> (clean::Crate, RenderOptions, Cache) { @@ -562,10 +562,10 @@ crate fn run_global_ctxt( } } - fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler) { - let mut msg = diag - .struct_warn(&format!("the `#![doc({})]` attribute is considered deprecated", name)); - msg.warn( + fn report_deprecated_attr(name: &str, diag: &rustc_errors::Handler, sp: Span) { + let mut msg = + diag.struct_span_warn(sp, &format!("the `#![doc({})]` attribute is deprecated", name)); + msg.note( "see issue #44136 \ for more information", ); @@ -577,6 +577,27 @@ crate fn run_global_ctxt( msg.emit(); } + let parse_pass = |name: &str, sp: Option| { + if let Some(pass) = passes::find_pass(name) { + Some(ConditionalPass::always(pass)) + } else { + let msg = &format!("ignoring unknown pass `{}`", name); + let mut warning = if let Some(sp) = sp { + tcx.sess.struct_span_warn(sp, msg) + } else { + tcx.sess.struct_warn(msg) + }; + if name == "collapse-docs" { + warning.note("the `collapse-docs` pass was removed in #80261 "); + } + warning.emit(); + None + } + }; + + let mut manual_passes: Vec<_> = + manual_passes.into_iter().flat_map(|name| parse_pass(&name, None)).collect(); + // Process all of the crate attributes, extracting plugin metadata along // with the passes which we are supposed to run. for attr in krate.module.as_ref().unwrap().attrs.lists(sym::doc) { @@ -585,19 +606,18 @@ crate fn run_global_ctxt( let name = attr.name_or_empty(); if attr.is_word() { if name == sym::no_default_passes { - report_deprecated_attr("no_default_passes", diag); + report_deprecated_attr("no_default_passes", diag, attr.span()); if default_passes == passes::DefaultPassOption::Default { default_passes = passes::DefaultPassOption::None; } } } else if let Some(value) = attr.value_str() { - let sink = match name { + match name { sym::passes => { - report_deprecated_attr("passes = \"...\"", diag); - &mut manual_passes + report_deprecated_attr("passes = \"...\"", diag, attr.span()); } sym::plugins => { - report_deprecated_attr("plugins = \"...\"", diag); + report_deprecated_attr("plugins = \"...\"", diag, attr.span()); eprintln!( "WARNING: `#![doc(plugins = \"...\")]` \ no longer functions; see CVE-2018-1000622" @@ -607,7 +627,8 @@ crate fn run_global_ctxt( _ => continue, }; for name in value.as_str().split_whitespace() { - sink.push(name.to_string()); + let span = attr.name_value_literal_span().unwrap_or(attr.span()); + manual_passes.extend(parse_pass(name, Some(span))); } } @@ -616,17 +637,7 @@ crate fn run_global_ctxt( } } - let passes = passes::defaults(default_passes).iter().copied().chain( - manual_passes.into_iter().flat_map(|name| { - if let Some(pass) = passes::find_pass(&name) { - Some(ConditionalPass::always(pass)) - } else { - error!("unknown pass {}, skipping", name); - None - } - }), - ); - + let passes = passes::defaults(default_passes).iter().copied().chain(manual_passes); info!("Executing passes"); for p in passes { diff --git a/src/test/rustdoc-ui/deprecated-attrs.rs b/src/test/rustdoc-ui/deprecated-attrs.rs index ca626afbe5359..a0439acc0240c 100644 --- a/src/test/rustdoc-ui/deprecated-attrs.rs +++ b/src/test/rustdoc-ui/deprecated-attrs.rs @@ -1,7 +1,13 @@ // check-pass +// compile-flags: --passes unknown-pass +// error-pattern: ignoring unknown pass `unknown-pass` -#![doc(no_default_passes, passes = "unindent-comments")] - -struct SomeStruct; - -pub struct OtherStruct; +#![doc(no_default_passes)] +//~^ WARNING attribute is deprecated +//~| NOTE see issue #44136 +//~| HELP use `#![doc(document_private_items)]` +#![doc(passes = "collapse-docs unindent-comments")] +//~^ WARNING attribute is deprecated +//~| NOTE see issue #44136 +//~| WARNING ignoring unknown pass +//~| NOTE `collapse-docs` pass was removed diff --git a/src/test/rustdoc-ui/deprecated-attrs.stderr b/src/test/rustdoc-ui/deprecated-attrs.stderr index f68fb46744805..42a229abf58e8 100644 --- a/src/test/rustdoc-ui/deprecated-attrs.stderr +++ b/src/test/rustdoc-ui/deprecated-attrs.stderr @@ -1,11 +1,33 @@ -warning: the `#![doc(no_default_passes)]` attribute is considered deprecated +warning: the `passes` flag is deprecated | - = warning: see issue #44136 for more information + = note: see issue #44136 for more information + +warning: ignoring unknown pass `unknown-pass` + +warning: the `#![doc(no_default_passes)]` attribute is deprecated + --> $DIR/deprecated-attrs.rs:5:8 + | +LL | #![doc(no_default_passes)] + | ^^^^^^^^^^^^^^^^^ + | + = note: see issue #44136 for more information = help: you may want to use `#![doc(document_private_items)]` -warning: the `#![doc(passes = "...")]` attribute is considered deprecated +warning: the `#![doc(passes = "...")]` attribute is deprecated + --> $DIR/deprecated-attrs.rs:9:8 + | +LL | #![doc(passes = "collapse-docs unindent-comments")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #44136 for more information + +warning: ignoring unknown pass `collapse-docs` + --> $DIR/deprecated-attrs.rs:9:17 + | +LL | #![doc(passes = "collapse-docs unindent-comments")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = warning: see issue #44136 for more information + = note: the `collapse-docs` pass was removed in #80261 -warning: 2 warnings emitted +warning: 4 warnings emitted From d5c300b1f2e9a6f7f05b2fa51ca2b4d011d289f4 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 28 Feb 2021 22:51:35 -0500 Subject: [PATCH 49/61] Report that `doc(plugins)` doesn't work using diagnostics instead of `println!` This also adds a test for the output and fixes `rustc_attr` to properly know that `plugins` is a valid attribute. --- compiler/rustc_passes/src/check_attr.rs | 3 ++- src/librustdoc/core.rs | 6 ++---- src/test/rustdoc-ui/deprecated-attrs.rs | 4 ++++ src/test/rustdoc-ui/deprecated-attrs.stderr | 11 ++++++++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index afd1642bbd57c..d0ab0d657d9e5 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -559,7 +559,8 @@ impl CheckAttrVisitor<'tcx> { sym::masked, sym::no_default_passes, // deprecated sym::no_inline, - sym::passes, // deprecated + sym::passes, // deprecated + sym::plugins, // removed, but rustdoc warns about it itself sym::primitive, sym::spotlight, sym::test, diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index a935bd6b456d5..19821c3ccefd2 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -572,6 +572,8 @@ crate fn run_global_ctxt( if name == "no_default_passes" { msg.help("you may want to use `#![doc(document_private_items)]`"); + } else if name.starts_with("plugins") { + msg.warn("`#![doc(plugins = \"...\")]` no longer functions; see CVE-2018-1000622 "); } msg.emit(); @@ -618,10 +620,6 @@ crate fn run_global_ctxt( } sym::plugins => { report_deprecated_attr("plugins = \"...\"", diag, attr.span()); - eprintln!( - "WARNING: `#![doc(plugins = \"...\")]` \ - no longer functions; see CVE-2018-1000622" - ); continue; } _ => continue, diff --git a/src/test/rustdoc-ui/deprecated-attrs.rs b/src/test/rustdoc-ui/deprecated-attrs.rs index a0439acc0240c..5febc5eb9cd54 100644 --- a/src/test/rustdoc-ui/deprecated-attrs.rs +++ b/src/test/rustdoc-ui/deprecated-attrs.rs @@ -11,3 +11,7 @@ //~| NOTE see issue #44136 //~| WARNING ignoring unknown pass //~| NOTE `collapse-docs` pass was removed +#![doc(plugins = "xxx")] +//~^ WARNING attribute is deprecated +//~| NOTE see issue #44136 +//~| WARNING no longer functions; see CVE diff --git a/src/test/rustdoc-ui/deprecated-attrs.stderr b/src/test/rustdoc-ui/deprecated-attrs.stderr index 42a229abf58e8..b855cedf5223e 100644 --- a/src/test/rustdoc-ui/deprecated-attrs.stderr +++ b/src/test/rustdoc-ui/deprecated-attrs.stderr @@ -29,5 +29,14 @@ LL | #![doc(passes = "collapse-docs unindent-comments")] | = note: the `collapse-docs` pass was removed in #80261 -warning: 4 warnings emitted +warning: the `#![doc(plugins = "...")]` attribute is deprecated + --> $DIR/deprecated-attrs.rs:14:8 + | +LL | #![doc(plugins = "xxx")] + | ^^^^^^^^^^^^^^^ + | + = note: see issue #44136 for more information + = warning: `#![doc(plugins = "...")]` no longer functions; see CVE-2018-1000622 + +warning: 5 warnings emitted From d9d69212c3d1dca7e8975d5163f09289db4a7420 Mon Sep 17 00:00:00 2001 From: Henry Boisdequin <65845077+henryboisdequin@users.noreply.github.com> Date: Wed, 3 Mar 2021 16:50:07 +0530 Subject: [PATCH 50/61] Fix diagnostic suggests adding type `[type error]` Fixes #79040 Unresolved questions: Why does this change output the diagnostic twice? Why does the CI fail when the errors are pointed out (UI tests)? --- compiler/rustc_typeck/src/collect/type_of.rs | 2 +- src/test/ui/79040.rs | 5 +++++ src/test/ui/79040.stderr | 19 +++++++++++++++++++ ...em-free-const-no-body-semantic-fail.stderr | 8 +------- ...m-free-static-no-body-semantic-fail.stderr | 14 +------------- 5 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 src/test/ui/79040.rs create mode 100644 src/test/ui/79040.stderr diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index e4eabca9c3b76..92e2b5232027b 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -659,7 +659,7 @@ fn infer_placeholder_type( format!("{}: {}", item_ident, ty), Applicability::MachineApplicable, ) - .emit(); + .emit_unless(matches!(ty.kind(), ty::Error(_))); } None => { let mut diag = bad_placeholder_type(tcx, vec![span]); diff --git a/src/test/ui/79040.rs b/src/test/ui/79040.rs new file mode 100644 index 0000000000000..adc9dc8b42656 --- /dev/null +++ b/src/test/ui/79040.rs @@ -0,0 +1,5 @@ +fn main() { + const FOO = "hello" + 1; + //^~ ERROR cannot add `{integer}` to `&str` + println!("{}", FOO); +} \ No newline at end of file diff --git a/src/test/ui/79040.stderr b/src/test/ui/79040.stderr new file mode 100644 index 0000000000000..be7d2363c2d52 --- /dev/null +++ b/src/test/ui/79040.stderr @@ -0,0 +1,19 @@ +error[E0369]: cannot add `{integer}` to `&str` + --> $DIR/79040.rs:2:25 + | +LL | const FOO = "hello" + 1; + | ------- ^ - {integer} + | | + | &str + +error[E0369]: cannot add `{integer}` to `&str` + --> $DIR/79040.rs:2:25 + | +LL | const FOO = "hello" + 1; + | ------- ^ - {integer} + | | + | &str + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0369`. diff --git a/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr index 4e97229fa1a41..aa75e5cee01d4 100644 --- a/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-const-no-body-semantic-fail.stderr @@ -14,11 +14,5 @@ LL | const B; | | | help: provide a definition for the constant: `= ;` -error: missing type for `const` item - --> $DIR/item-free-const-no-body-semantic-fail.rs:6:7 - | -LL | const B; - | ^ help: provide a type for the item: `B: [type error]` - -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr index 60b7bb34c698b..2a270c8290fd4 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr @@ -30,17 +30,5 @@ LL | static mut D; | | | help: provide a definition for the static: `= ;` -error: missing type for `static` item - --> $DIR/item-free-static-no-body-semantic-fail.rs:6:8 - | -LL | static B; - | ^ help: provide a type for the item: `B: [type error]` - -error: missing type for `static mut` item - --> $DIR/item-free-static-no-body-semantic-fail.rs:10:12 - | -LL | static mut D; - | ^ help: provide a type for the item: `D: [type error]` - -error: aborting due to 6 previous errors +error: aborting due to 4 previous errors From b3c4e25e1740da69686f940165dd3c9d563d54e5 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 22 Feb 2021 09:31:41 -0500 Subject: [PATCH 51/61] Don't require a `DocContext` for `report_diagnostic` This is needed for the next commit, which needs access to the `cx` from within the `decorate` closure. - Change `as_local_hir_id` to an associated function, since it only needs a `TyCtxt` - Change `source_span_for_markdown_range` to only take a `TyCtxt` --- src/librustdoc/core.rs | 6 +++--- .../passes/check_code_block_syntax.rs | 9 ++++++--- .../passes/collect_intra_doc_links.rs | 20 +++++++++---------- src/librustdoc/passes/doc_test_lints.rs | 2 +- src/librustdoc/passes/html_tags.rs | 8 ++++---- src/librustdoc/passes/mod.rs | 5 +++-- src/librustdoc/passes/non_autolinks.rs | 4 ++-- 7 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 9cb0649b314f5..5ff50e2538050 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -169,13 +169,13 @@ impl<'tcx> DocContext<'tcx> { /// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds. /// (This avoids a slice-index-out-of-bounds panic.) - crate fn as_local_hir_id(&self, def_id: DefId) -> Option { + crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option { if MAX_DEF_IDX.with(|m| { m.borrow().get(&def_id.krate).map(|&idx| idx <= def_id.index).unwrap_or(false) }) { None } else { - def_id.as_local().map(|def_id| self.tcx.hir().local_def_id_to_hir_id(def_id)) + def_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id)) } } } @@ -479,7 +479,7 @@ crate fn run_global_ctxt( https://doc.rust-lang.org/nightly/rustdoc/how-to-write-documentation.html"; tcx.struct_lint_node( crate::lint::MISSING_CRATE_LEVEL_DOCS, - ctxt.as_local_hir_id(m.def_id).unwrap(), + DocContext::as_local_hir_id(tcx, m.def_id).unwrap(), |lint| { let mut diag = lint.build("no documentation found for this crate's top-level module"); diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index c85490864ec70..98886139f3088 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -48,9 +48,12 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { let buffer = buffer.borrow(); if buffer.has_errors || is_empty { - let mut diag = if let Some(sp) = - super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs) - { + let mut diag = if let Some(sp) = super::source_span_for_markdown_range( + self.cx.tcx, + &dox, + &code_block.range, + &item.attrs, + ) { let (warning_message, suggest_using_text) = if buffer.has_errors { ("could not parse code block as Rust code", true) } else { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 38efecb393b79..93aa069593694 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -1146,7 +1146,7 @@ impl LinkCollector<'_, '_> { suggest_disambiguator(resolved, diag, path_str, dox, sp, &ori_link.range); }; report_diagnostic( - self.cx, + self.cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, &item, @@ -1220,7 +1220,7 @@ impl LinkCollector<'_, '_> { && !self.cx.tcx.features().intra_doc_pointers { let span = super::source_span_for_markdown_range( - self.cx, + self.cx.tcx, dox, &ori_link.range, &item.attrs, @@ -1674,7 +1674,7 @@ impl Suggestion { /// parameter of the callback will contain it, and the primary span of the diagnostic will be set /// to it. fn report_diagnostic( - cx: &DocContext<'_>, + tcx: TyCtxt<'_>, lint: &'static Lint, msg: &str, item: &Item, @@ -1682,7 +1682,7 @@ fn report_diagnostic( link_range: &Range, decorate: impl FnOnce(&mut DiagnosticBuilder<'_>, Option), ) { - let hir_id = match cx.as_local_hir_id(item.def_id) { + let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. @@ -1694,10 +1694,10 @@ fn report_diagnostic( let attrs = &item.attrs; let sp = span_of_attrs(attrs).unwrap_or(item.source.span()); - cx.tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| { + tcx.struct_span_lint_hir(lint, hir_id, sp, |lint| { let mut diag = lint.build(msg); - let span = super::source_span_for_markdown_range(cx, dox, link_range, attrs); + let span = super::source_span_for_markdown_range(tcx, dox, link_range, attrs); if let Some(sp) = span { diag.set_span(sp); @@ -1742,7 +1742,7 @@ fn resolution_failure( ) { let tcx = collector.cx.tcx; report_diagnostic( - collector.cx, + tcx, BROKEN_INTRA_DOC_LINKS, &format!("unresolved link to `{}`", path_str), item, @@ -1973,7 +1973,7 @@ fn anchor_failure( ), }; - report_diagnostic(cx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| { + report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| { if let Some(sp) = sp { diag.span_label(sp, "contains invalid anchor"); } @@ -2013,7 +2013,7 @@ fn ambiguity_error( } } - report_diagnostic(cx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| { + report_diagnostic(cx.tcx, BROKEN_INTRA_DOC_LINKS, &msg, item, dox, &link_range, |diag, sp| { if let Some(sp) = sp { diag.span_label(sp, "ambiguous link"); } else { @@ -2066,7 +2066,7 @@ fn privacy_error(cx: &DocContext<'_>, item: &Item, path_str: &str, dox: &str, li let msg = format!("public documentation for `{}` links to private item `{}`", item_name, path_str); - report_diagnostic(cx, PRIVATE_INTRA_DOC_LINKS, &msg, item, dox, &link.range, |diag, sp| { + report_diagnostic(cx.tcx, PRIVATE_INTRA_DOC_LINKS, &msg, item, dox, &link.range, |diag, sp| { if let Some(sp) = sp { diag.span_label(sp, "this item is private"); } diff --git a/src/librustdoc/passes/doc_test_lints.rs b/src/librustdoc/passes/doc_test_lints.rs index 81104236314d9..ed6d2d59845f2 100644 --- a/src/librustdoc/passes/doc_test_lints.rs +++ b/src/librustdoc/passes/doc_test_lints.rs @@ -73,7 +73,7 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo } crate fn look_for_tests<'tcx>(cx: &DocContext<'tcx>, dox: &str, item: &Item) { - let hir_id = match cx.as_local_hir_id(item.def_id) { + let hir_id = match DocContext::as_local_hir_id(cx.tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs index 27e669aa44fc0..5fff7c8def31c 100644 --- a/src/librustdoc/passes/html_tags.rs +++ b/src/librustdoc/passes/html_tags.rs @@ -167,7 +167,8 @@ fn extract_tags( impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { - let hir_id = match self.cx.as_local_hir_id(item.def_id) { + let tcx = self.cx.tcx; + let hir_id = match DocContext::as_local_hir_id(tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. @@ -176,13 +177,12 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { }; let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); if !dox.is_empty() { - let cx = &self.cx; let report_diag = |msg: &str, range: &Range| { - let sp = match super::source_span_for_markdown_range(cx, &dox, range, &item.attrs) { + let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs) { Some(sp) => sp, None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()), }; - cx.tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| { + tcx.struct_span_lint_hir(crate::lint::INVALID_HTML_TAGS, hir_id, sp, |lint| { lint.build(msg).emit() }); }; diff --git a/src/librustdoc/passes/mod.rs b/src/librustdoc/passes/mod.rs index 5813732facb6e..4c639c8496db3 100644 --- a/src/librustdoc/passes/mod.rs +++ b/src/librustdoc/passes/mod.rs @@ -1,6 +1,7 @@ //! Contains information about "passes", used to modify crate information during the documentation //! process. +use rustc_middle::ty::TyCtxt; use rustc_span::{InnerSpan, Span, DUMMY_SP}; use std::ops::Range; @@ -167,7 +168,7 @@ crate fn span_of_attrs(attrs: &clean::Attributes) -> Option { /// attributes are not all sugared doc comments. It's difficult to calculate the correct span in /// that case due to escaping and other source features. crate fn source_span_for_markdown_range( - cx: &DocContext<'_>, + tcx: TyCtxt<'_>, markdown: &str, md_range: &Range, attrs: &clean::Attributes, @@ -179,7 +180,7 @@ crate fn source_span_for_markdown_range( return None; } - let snippet = cx.sess().source_map().span_to_snippet(span_of_attrs(attrs)?).ok()?; + let snippet = tcx.sess.source_map().span_to_snippet(span_of_attrs(attrs)?).ok()?; let starting_line = markdown[..md_range.start].matches('\n').count(); let ending_line = starting_line + markdown[md_range.start..md_range.end].matches('\n').count(); diff --git a/src/librustdoc/passes/non_autolinks.rs b/src/librustdoc/passes/non_autolinks.rs index 09a1959fa113c..16ad940c62717 100644 --- a/src/librustdoc/passes/non_autolinks.rs +++ b/src/librustdoc/passes/non_autolinks.rs @@ -60,7 +60,7 @@ crate fn check_non_autolinks(krate: Crate, cx: &mut DocContext<'_>) -> Crate { impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> { fn fold_item(&mut self, item: Item) -> Option { - let hir_id = match self.cx.as_local_hir_id(item.def_id) { + let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { Some(hir_id) => hir_id, None => { // If non-local, no need to check anything. @@ -70,7 +70,7 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> { let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); if !dox.is_empty() { let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range| { - let sp = super::source_span_for_markdown_range(cx, &dox, &range, &item.attrs) + let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs) .or_else(|| span_of_attrs(&item.attrs)) .unwrap_or(item.source.span()); cx.tcx.struct_span_lint_hir(crate::lint::NON_AUTOLINKS, hir_id, sp, |lint| { From 675edd0231fd799f130ae2d46e079b3753475449 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Mon, 22 Feb 2021 09:21:47 -0500 Subject: [PATCH 52/61] Remove RefCell around module_trait_cache --- src/librustdoc/core.rs | 4 +- .../passes/collect_intra_doc_links.rs | 47 ++++++++++--------- src/librustdoc/passes/html_tags.rs | 3 +- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 5ff50e2538050..bcbc6e557d0a9 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -76,7 +76,7 @@ crate struct DocContext<'tcx> { /// /// See `collect_intra_doc_links::traits_implemented_by` for more details. /// `map>` - crate module_trait_cache: RefCell>>, + crate module_trait_cache: FxHashMap>, /// This same cache is used throughout rustdoc, including in [`crate::html::render`]. crate cache: Cache, /// Used by [`clean::inline`] to tell if an item has already been inlined. @@ -450,7 +450,7 @@ crate fn run_global_ctxt( .cloned() .filter(|trait_def_id| tcx.trait_is_auto(*trait_def_id)) .collect(), - module_trait_cache: RefCell::new(FxHashMap::default()), + module_trait_cache: FxHashMap::default(), cache: Cache::new(access_levels, render_options.document_private), inlined: FxHashSet::default(), output_format, diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index 93aa069593694..ec5994b762b8a 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -484,13 +484,13 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { /// Resolves a string as a path within a particular namespace. Returns an /// optional URL fragment in the case of variants and methods. fn resolve<'path>( - &self, + &mut self, path_str: &'path str, ns: Namespace, module_id: DefId, extra_fragment: &Option, ) -> Result<(Res, Option), ErrorKind<'path>> { - let cx = &self.cx; + let tcx = self.cx.tcx; if let Some(res) = self.resolve_path(path_str, ns, module_id) { match res { @@ -498,7 +498,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { // item a separate function. Res::Def(DefKind::AssocFn | DefKind::AssocConst, _) => assert_eq!(ns, ValueNS), Res::Def(DefKind::AssocTy, _) => assert_eq!(ns, TypeNS), - Res::Def(DefKind::Variant, _) => return handle_variant(cx, res, extra_fragment), + Res::Def(DefKind::Variant, _) => { + return handle_variant(self.cx, res, extra_fragment); + } // Not a trait item; just return what we found. Res::Primitive(ty) => { if extra_fragment.is_some() { @@ -565,13 +567,12 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { ) => { debug!("looking for associated item named {} for item {:?}", item_name, did); // Checks if item_name belongs to `impl SomeItem` - let assoc_item = cx - .tcx + let assoc_item = tcx .inherent_impls(did) .iter() .flat_map(|&imp| { - cx.tcx.associated_items(imp).find_by_name_and_namespace( - cx.tcx, + tcx.associated_items(imp).find_by_name_and_namespace( + tcx, Ident::with_dummy_span(item_name), ns, imp, @@ -587,7 +588,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { // something like [`ambi_fn`](::ambi_fn) .or_else(|| { let kind = - resolve_associated_trait_item(did, module_id, item_name, ns, &self.cx); + resolve_associated_trait_item(did, module_id, item_name, ns, self.cx); debug!("got associated item kind {:?}", kind); kind }); @@ -611,7 +612,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { debug!("looking for variants or fields named {} for {:?}", item_name, did); // FIXME(jynelson): why is this different from // `variant_field`? - match cx.tcx.type_of(did).kind() { + match tcx.type_of(did).kind() { ty::Adt(def, _) => { let field = if def.is_enum() { def.all_fields().find(|item| item.ident.name == item_name) @@ -652,10 +653,9 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { None } } - Res::Def(DefKind::Trait, did) => cx - .tcx + Res::Def(DefKind::Trait, did) => tcx .associated_items(did) - .find_by_name_and_namespace(cx.tcx, Ident::with_dummy_span(item_name), ns, did) + .find_by_name_and_namespace(tcx, Ident::with_dummy_span(item_name), ns, did) .map(|item| { let kind = match item.kind { ty::AssocKind::Const => "associatedconstant", @@ -699,7 +699,7 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> { /// This returns the `Res` even if it was erroneous for some reason /// (such as having invalid URL fragments or being in the wrong namespace). fn check_full_res( - &self, + &mut self, ns: Namespace, path_str: &str, module_id: DefId, @@ -733,7 +733,7 @@ fn resolve_associated_trait_item( module: DefId, item_name: Symbol, ns: Namespace, - cx: &DocContext<'_>, + cx: &mut DocContext<'_>, ) -> Option<(ty::AssocKind, DefId)> { // FIXME: this should also consider blanket impls (`impl X for T`). Unfortunately // `get_auto_trait_and_blanket_impls` is broken because the caching behavior is wrong. In the @@ -758,10 +758,10 @@ fn resolve_associated_trait_item( /// /// NOTE: this cannot be a query because more traits could be available when more crates are compiled! /// So it is not stable to serialize cross-crate. -fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> FxHashSet { - let mut cache = cx.module_trait_cache.borrow_mut(); - let in_scope_traits = cache.entry(module).or_insert_with(|| { - cx.enter_resolver(|resolver| { +fn traits_implemented_by(cx: &mut DocContext<'_>, type_: DefId, module: DefId) -> FxHashSet { + let mut resolver = cx.resolver.borrow_mut(); + let in_scope_traits = cx.module_trait_cache.entry(module).or_insert_with(|| { + resolver.access(|resolver| { let parent_scope = &ParentScope::module(resolver.get_module(module), resolver); resolver .traits_in_scope(None, parent_scope, SyntaxContext::root(), None) @@ -771,13 +771,14 @@ fn traits_implemented_by(cx: &DocContext<'_>, type_: DefId, module: DefId) -> Fx }) }); - let ty = cx.tcx.type_of(type_); + let tcx = cx.tcx; + let ty = tcx.type_of(type_); let iter = in_scope_traits.iter().flat_map(|&trait_| { trace!("considering explicit impl for trait {:?}", trait_); // Look at each trait implementation to see if it's an impl for `did` - cx.tcx.find_map_relevant_impl(trait_, ty, |impl_| { - let trait_ref = cx.tcx.impl_trait_ref(impl_).expect("this is not an inherent impl"); + tcx.find_map_relevant_impl(trait_, ty, |impl_| { + let trait_ref = tcx.impl_trait_ref(impl_).expect("this is not an inherent impl"); // Check if these are the same type. let impl_type = trait_ref.self_ty(); trace!( @@ -1308,7 +1309,7 @@ impl LinkCollector<'_, '_> { /// After parsing the disambiguator, resolve the main part of the link. // FIXME(jynelson): wow this is just so much fn resolve_with_disambiguator( - &self, + &mut self, key: &ResolutionInfo, diag: DiagnosticInfo<'_>, ) -> Option<(Res, Option)> { @@ -1732,7 +1733,7 @@ fn report_diagnostic( /// handled earlier. For example, if passed `Item::Crate(std)` and `path_str` /// `std::io::Error::x`, this will resolve `std::io::Error`. fn resolution_failure( - collector: &LinkCollector<'_, '_>, + collector: &mut LinkCollector<'_, '_>, item: &Item, path_str: &str, disambiguator: Option, diff --git a/src/librustdoc/passes/html_tags.rs b/src/librustdoc/passes/html_tags.rs index 5fff7c8def31c..23364b6fec9d1 100644 --- a/src/librustdoc/passes/html_tags.rs +++ b/src/librustdoc/passes/html_tags.rs @@ -178,7 +178,8 @@ impl<'a, 'tcx> DocFolder for InvalidHtmlTagsLinter<'a, 'tcx> { let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); if !dox.is_empty() { let report_diag = |msg: &str, range: &Range| { - let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs) { + let sp = match super::source_span_for_markdown_range(tcx, &dox, range, &item.attrs) + { Some(sp) => sp, None => span_of_attrs(&item.attrs).unwrap_or(item.source.span()), }; From b5f67dc703cc2f874a170927c0024e28e3a1121b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 5 Mar 2021 10:58:30 +0100 Subject: [PATCH 53/61] Rustup to rustc 1.52.0-nightly (45b3c2851 2021-03-04) --- patches/0027-Disable-128bit-atomic-operations.patch | 4 ++-- rust-toolchain | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/patches/0027-Disable-128bit-atomic-operations.patch b/patches/0027-Disable-128bit-atomic-operations.patch index 054299e8aab28..32e5930969061 100644 --- a/patches/0027-Disable-128bit-atomic-operations.patch +++ b/patches/0027-Disable-128bit-atomic-operations.patch @@ -30,7 +30,7 @@ index 81c9e1d..65c9503 100644 - unstable(feature = "integer_atomics", issue = "32976"), - rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), - unstable(feature = "integer_atomics", issue = "32976"), -- "i128", "../../../std/primitive.i128.html", +- "i128", - "#![feature(integer_atomics)]\n\n", - atomic_min, atomic_max, - 16, @@ -49,7 +49,7 @@ index 81c9e1d..65c9503 100644 - unstable(feature = "integer_atomics", issue = "32976"), - rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), - unstable(feature = "integer_atomics", issue = "32976"), -- "u128", "../../../std/primitive.u128.html", +- "u128", - "#![feature(integer_atomics)]\n\n", - atomic_umin, atomic_umax, - 16, diff --git a/rust-toolchain b/rust-toolchain index 8c8c58d90d914..908ca52135b66 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1 +1 @@ -nightly-2021-02-21 +nightly-2021-03-05 From 5e736293b3bcada7dba0a134604eb6c9866d5858 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 5 Mar 2021 11:21:44 +0100 Subject: [PATCH 54/61] Rustfmt --- rustfmt.toml | 4 + src/abi/comments.rs | 48 +++------ src/abi/mod.rs | 123 +++++------------------ src/abi/pass_mode.rs | 90 ++++------------- src/abi/returning.rs | 78 ++++----------- src/allocator.rs | 29 ++---- src/analyze.rs | 7 +- src/archive.rs | 43 ++------ src/backend.rs | 17 +--- src/base.rs | 166 +++++++++---------------------- src/bin/cg_clif.rs | 8 +- src/bin/cg_clif_build_sysroot.rs | 11 +- src/cast.rs | 30 +----- src/codegen_i128.rs | 12 +-- src/common.rs | 46 +++------ src/constant.rs | 71 ++++--------- src/debuginfo/emit.rs | 14 +-- src/debuginfo/line_info.rs | 26 +---- src/debuginfo/mod.rs | 45 ++------- src/debuginfo/unwind.rs | 27 ++--- src/discriminant.rs | 41 ++------ src/driver/aot.rs | 64 +++--------- src/driver/jit.rs | 69 ++++--------- src/driver/mod.rs | 22 +--- src/inline_asm.rs | 53 ++-------- src/intrinsics/cpuid.rs | 42 ++------ src/intrinsics/mod.rs | 10 +- src/lib.rs | 16 +-- src/main_shim.rs | 24 +---- src/metadata.rs | 4 +- src/num.rs | 49 ++------- src/optimize/code_layout.rs | 10 +- src/optimize/peephole.rs | 22 +--- src/optimize/stack2reg.rs | 49 ++------- src/pointer.rs | 65 ++++-------- src/pretty_clif.rs | 66 +++++------- src/toolchain.rs | 15 +-- src/unsize.rs | 67 ++++--------- src/value_and_place.rs | 135 ++++++------------------- src/vtable.rs | 7 +- 40 files changed, 429 insertions(+), 1296 deletions(-) create mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000000000..2bd8f7d1bc15d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,4 @@ +# Matches rustfmt.toml of rustc +version = "Two" +use_small_heuristics = "Max" +merge_derives = false diff --git a/src/abi/comments.rs b/src/abi/comments.rs index 8570805fcd27b..c3cf90e1e70be 100644 --- a/src/abi/comments.rs +++ b/src/abi/comments.rs @@ -42,11 +42,7 @@ pub(super) fn add_arg_comment<'tcx>( [param_a, param_b] => Cow::Owned(format!("= {:?},{:?}", param_a, param_b)), params => Cow::Owned(format!( "= {}", - params - .iter() - .map(ToString::to_string) - .collect::>() - .join(",") + params.iter().map(ToString::to_string).collect::>().join(",") )), }; @@ -75,14 +71,8 @@ pub(super) fn add_local_place_comments<'tcx>( local: Local, ) { let TyAndLayout { ty, layout } = place.layout(); - let rustc_target::abi::Layout { - size, - align, - abi: _, - variants: _, - fields: _, - largest_niche: _, - } = layout; + let rustc_target::abi::Layout { size, align, abi: _, variants: _, fields: _, largest_niche: _ } = + layout; let (kind, extra) = match *place.inner() { CPlaceInner::Var(place_local, var) => { @@ -91,10 +81,7 @@ pub(super) fn add_local_place_comments<'tcx>( } CPlaceInner::VarPair(place_local, var1, var2) => { assert_eq!(local, place_local); - ( - "ssa", - Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index())), - ) + ("ssa", Cow::Owned(format!(",var=({}, {})", var1.index(), var2.index()))) } CPlaceInner::VarLane(_local, _var, _lane) => unreachable!(), CPlaceInner::Addr(ptr, meta) => { @@ -104,18 +91,15 @@ pub(super) fn add_local_place_comments<'tcx>( Cow::Borrowed("") }; match ptr.base_and_offset() { - (crate::pointer::PointerBase::Addr(addr), offset) => ( - "reuse", - format!("storage={}{}{}", addr, offset, meta).into(), - ), - (crate::pointer::PointerBase::Stack(stack_slot), offset) => ( - "stack", - format!("storage={}{}{}", stack_slot, offset, meta).into(), - ), - (crate::pointer::PointerBase::Dangling(align), offset) => ( - "zst", - format!("align={},offset={}", align.bytes(), offset).into(), - ), + (crate::pointer::PointerBase::Addr(addr), offset) => { + ("reuse", format!("storage={}{}{}", addr, offset, meta).into()) + } + (crate::pointer::PointerBase::Stack(stack_slot), offset) => { + ("stack", format!("storage={}{}{}", stack_slot, offset, meta).into()) + } + (crate::pointer::PointerBase::Dangling(align), offset) => { + ("zst", format!("align={},offset={}", align.bytes(), offset).into()) + } } } }; @@ -128,11 +112,7 @@ pub(super) fn add_local_place_comments<'tcx>( size.bytes(), align.abi.bytes(), align.pref.bytes(), - if extra.is_empty() { - "" - } else { - " " - }, + if extra.is_empty() { "" } else { " " }, extra, )); } diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 849f293c41993..c79889f8ca1da 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -40,21 +40,13 @@ fn clif_sig_from_fn_abi<'tcx>( | Conv::AvrInterrupt | Conv::AvrNonBlockingInterrupt => todo!("{:?}", fn_abi.conv), }; - let inputs = fn_abi - .args - .iter() - .map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()) - .flatten(); + let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten(); let (return_ptr, returns) = fn_abi.ret.get_abi_return(tcx); // Sometimes the first param is an pointer to the place where the return value needs to be stored. let params: Vec<_> = return_ptr.into_iter().chain(inputs).collect(); - Signature { - params, - returns, - call_conv, - } + Signature { params, returns, call_conv } } pub(crate) fn get_function_sig<'tcx>( @@ -63,11 +55,7 @@ pub(crate) fn get_function_sig<'tcx>( inst: Instance<'tcx>, ) -> Signature { assert!(!inst.substs.needs_infer()); - clif_sig_from_fn_abi( - tcx, - triple, - &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[]), - ) + clif_sig_from_fn_abi(tcx, triple, &FnAbi::of_instance(&RevealAllLayoutCx(tcx), inst, &[])) } /// Instance must be monomorphized @@ -78,19 +66,14 @@ pub(crate) fn import_function<'tcx>( ) -> FuncId { let name = tcx.symbol_name(inst).name.to_string(); let sig = get_function_sig(tcx, module.isa().triple(), inst); - module - .declare_function(&name, Linkage::Import, &sig) - .unwrap() + module.declare_function(&name, Linkage::Import, &sig).unwrap() } impl<'tcx> FunctionCx<'_, '_, 'tcx> { /// Instance must be monomorphized pub(crate) fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef { let func_id = import_function(self.tcx, self.cx.module, inst); - let func_ref = self - .cx - .module - .declare_func_in_func(func_id, &mut self.bcx.func); + let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); #[cfg(debug_assertions)] self.add_comment(func_ref, format!("{:?}", inst)); @@ -105,20 +88,9 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { returns: Vec, args: &[Value], ) -> &[Value] { - let sig = Signature { - params, - returns, - call_conv: CallConv::triple_default(self.triple()), - }; - let func_id = self - .cx - .module - .declare_function(&name, Linkage::Import, &sig) - .unwrap(); - let func_ref = self - .cx - .module - .declare_func_in_func(func_id, &mut self.bcx.func); + let sig = Signature { params, returns, call_conv: CallConv::triple_default(self.triple()) }; + let func_id = self.cx.module.declare_function(&name, Linkage::Import, &sig).unwrap(); + let func_ref = self.cx.module.declare_func_in_func(func_id, &mut self.bcx.func); let call_inst = self.bcx.ins().call(func_ref, args); #[cfg(debug_assertions)] { @@ -138,17 +110,12 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { let (input_tys, args): (Vec<_>, Vec<_>) = args .iter() .map(|arg| { - ( - AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), - arg.load_scalar(self), - ) + (AbiParam::new(self.clif_type(arg.layout().ty).unwrap()), arg.load_scalar(self)) }) .unzip(); let return_layout = self.layout_of(return_ty); let return_tys = if let ty::Tuple(tup) = return_ty.kind() { - tup.types() - .map(|ty| AbiParam::new(self.clif_type(ty).unwrap())) - .collect() + tup.types().map(|ty| AbiParam::new(self.clif_type(ty).unwrap())).collect() } else { vec![AbiParam::new(self.clif_type(return_ty).unwrap())] }; @@ -199,13 +166,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_ #[cfg(debug_assertions)] self::comments::add_args_header_comment(fx); - let mut block_params_iter = fx - .bcx - .func - .dfg - .block_params(start_block) - .to_vec() - .into_iter(); + let mut block_params_iter = fx.bcx.func.dfg.block_params(start_block).to_vec().into_iter(); let ret_place = self::returning::codegen_return_param(fx, &ssa_analyzed, &mut block_params_iter); assert_eq!(fx.local_map.push(ret_place), RETURN_PLACE); @@ -281,10 +242,10 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_ if let Some((addr, meta)) = val.try_to_ptr() { let local_decl = &fx.mir.local_decls[local]; // v this ! is important - let internally_mutable = !val.layout().ty.is_freeze( - fx.tcx.at(local_decl.source_info.span), - ParamEnv::reveal_all(), - ); + let internally_mutable = !val + .layout() + .ty + .is_freeze(fx.tcx.at(local_decl.source_info.span), ParamEnv::reveal_all()); if local_decl.mutability == mir::Mutability::Not && !internally_mutable { // We wont mutate this argument, so it is fine to borrow the backing storage // of this argument, to prevent a copy. @@ -316,9 +277,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_ ArgKind::Spread(params) => { for (i, param) in params.into_iter().enumerate() { if let Some(param) = param { - place - .place_field(fx, mir::Field::new(i)) - .write_cvalue(fx, param); + place.place_field(fx, mir::Field::new(i)).write_cvalue(fx, param); } } } @@ -335,9 +294,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_ assert_eq!(fx.local_map.push(place), local); } - fx.bcx - .ins() - .jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]); + fx.bcx.ins().jump(*fx.block_map.get(START_BLOCK).unwrap(), &[]); } pub(crate) fn codegen_terminator_call<'tcx>( @@ -349,9 +306,8 @@ pub(crate) fn codegen_terminator_call<'tcx>( destination: Option<(Place<'tcx>, BasicBlock)>, ) { let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx)); - let fn_sig = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); + let fn_sig = + fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); let destination = destination.map(|(place, bb)| (codegen_place(fx, place), bb)); @@ -399,20 +355,11 @@ pub(crate) fn codegen_terminator_call<'tcx>( let fn_abi = if let Some(instance) = instance { FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args) } else { - FnAbi::of_fn_ptr( - &RevealAllLayoutCx(fx.tcx), - fn_ty.fn_sig(fx.tcx), - &extra_args, - ) + FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args) }; let is_cold = instance - .map(|inst| { - fx.tcx - .codegen_fn_attrs(inst.def_id()) - .flags - .contains(CodegenFnAttrFlags::COLD) - }) + .map(|inst| fx.tcx.codegen_fn_attrs(inst.def_id()).flags.contains(CodegenFnAttrFlags::COLD)) .unwrap_or(false); if is_cold { fx.cold_blocks.insert(current_block); @@ -436,9 +383,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( } args } else { - args.iter() - .map(|arg| codegen_operand(fx, arg)) - .collect::>() + args.iter().map(|arg| codegen_operand(fx, arg)).collect::>() }; // | indirect call target @@ -446,10 +391,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( // v v let (func_ref, first_arg) = match instance { // Trait object call - Some(Instance { - def: InstanceDef::Virtual(_, idx), - .. - }) => { + Some(Instance { def: InstanceDef::Virtual(_, idx), .. }) => { #[cfg(debug_assertions)] { let nop_inst = fx.bcx.ins().nop(); @@ -506,10 +448,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( ) .collect::>(); - if instance - .map(|inst| inst.def.requires_caller_location(fx.tcx)) - .unwrap_or(false) - { + if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) { // Pass the caller location for `#[track_caller]`. let caller_location = fx.get_caller_location(span); call_args.extend( @@ -538,10 +477,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( // FIXME find a cleaner way to support varargs if fn_sig.c_variadic { if fn_sig.abi != Abi::C { - fx.tcx.sess.span_fatal( - span, - &format!("Variadic call for non-C abi {:?}", fn_sig.abi), - ); + fx.tcx.sess.span_fatal(span, &format!("Variadic call for non-C abi {:?}", fn_sig.abi)); } let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap(); let abi_params = call_args @@ -550,9 +486,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( let ty = fx.bcx.func.dfg.value_type(arg); if !ty.is_int() { // FIXME set %al to upperbound on float args once floats are supported - fx.tcx - .sess - .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty)); + fx.tcx.sess.span_fatal(span, &format!("Non int ty {:?} for variadic call", ty)); } AbiParam::new(ty) }) @@ -606,10 +540,7 @@ pub(crate) fn codegen_drop<'tcx>( fx, fx.layout_of(fx.tcx.mk_ref( &ty::RegionKind::ReErased, - TypeAndMut { - ty, - mutbl: crate::rustc_hir::Mutability::Mut, - }, + TypeAndMut { ty, mutbl: crate::rustc_hir::Mutability::Mut }, )), ); let arg_value = adjust_arg_for_abi(fx, arg_value, &fn_abi.args[0]); diff --git a/src/abi/pass_mode.rs b/src/abi/pass_mode.rs index 57b5ff9d02b9d..d58f952f53c1a 100644 --- a/src/abi/pass_mode.rs +++ b/src/abi/pass_mode.rs @@ -71,12 +71,7 @@ fn cast_target_to_abi_params(cast: CastTarget) -> SmallVec<[AbiParam; 2]> { .prefix .iter() .flatten() - .map(|&kind| { - reg_to_abi_param(Reg { - kind, - size: cast.prefix_chunk_size, - }) - }) + .map(|&kind| reg_to_abi_param(Reg { kind, size: cast.prefix_chunk_size })) .chain((0..rest_count).map(|_| reg_to_abi_param(cast.rest.unit))) .collect::>(); @@ -120,11 +115,7 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { _ => unreachable!("{:?}", self.layout.abi), }, PassMode::Cast(cast) => cast_target_to_abi_params(cast), - PassMode::Indirect { - attrs, - extra_attrs: None, - on_stack, - } => { + PassMode::Indirect { attrs, extra_attrs: None, on_stack } => { if on_stack { let size = u32::try_from(self.layout.size.bytes()).unwrap(); smallvec![apply_arg_attrs_to_abi_param( @@ -132,17 +123,10 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { attrs )] } else { - smallvec![apply_arg_attrs_to_abi_param( - AbiParam::new(pointer_ty(tcx)), - attrs - )] + smallvec![apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs)] } } - PassMode::Indirect { - attrs, - extra_attrs: Some(extra_attrs), - on_stack, - } => { + PassMode::Indirect { attrs, extra_attrs: Some(extra_attrs), on_stack } => { assert!(!on_stack); smallvec![ apply_arg_attrs_to_abi_param(AbiParam::new(pointer_ty(tcx)), attrs), @@ -156,10 +140,9 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { match self.mode { PassMode::Ignore => (None, vec![]), PassMode::Direct(_) => match &self.layout.abi { - Abi::Scalar(scalar) => ( - None, - vec![AbiParam::new(scalar_to_clif_type(tcx, scalar.clone()))], - ), + Abi::Scalar(scalar) => { + (None, vec![AbiParam::new(scalar_to_clif_type(tcx, scalar.clone()))]) + } Abi::Vector { .. } => { let vector_ty = crate::intrinsics::clif_vector_type(tcx, self.layout).unwrap(); (None, vec![AbiParam::new(vector_ty)]) @@ -175,25 +158,13 @@ impl<'tcx> ArgAbiExt<'tcx> for ArgAbi<'tcx, Ty<'tcx>> { _ => unreachable!("{:?}", self.layout.abi), }, PassMode::Cast(cast) => (None, cast_target_to_abi_params(cast).into_iter().collect()), - PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack, - } => { + PassMode::Indirect { attrs: _, extra_attrs: None, on_stack } => { assert!(!on_stack); - ( - Some(AbiParam::special( - pointer_ty(tcx), - ArgumentPurpose::StructReturn, - )), - vec![], - ) + (Some(AbiParam::special(pointer_ty(tcx), ArgumentPurpose::StructReturn)), vec![]) + } + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { + unreachable!("unsized return value") } - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => unreachable!("unsized return value"), } } } @@ -209,9 +180,7 @@ pub(super) fn to_casted_value<'tcx>( cast_target_to_abi_params(cast) .into_iter() .map(|param| { - let val = ptr - .offset_i64(fx, offset) - .load(fx, param.value_type, MemFlags::new()); + let val = ptr.offset_i64(fx, offset).load(fx, param.value_type, MemFlags::new()); offset += i64::from(param.value_type.bytes()); val }) @@ -225,10 +194,7 @@ pub(super) fn from_casted_value<'tcx>( cast: CastTarget, ) -> CValue<'tcx> { let abi_params = cast_target_to_abi_params(cast); - let abi_param_size: u32 = abi_params - .iter() - .map(|param| param.value_type.bytes()) - .sum(); + let abi_param_size: u32 = abi_params.iter().map(|param| param.value_type.bytes()).sum(); let layout_size = u32::try_from(layout.size.bytes()).unwrap(); let stack_slot = fx.bcx.create_stack_slot(StackSlotData { kind: StackSlotKind::ExplicitSlot, @@ -292,10 +258,7 @@ pub(super) fn cvalue_for_param<'tcx>( .into_iter() .map(|abi_param| { let block_param = block_params_iter.next().unwrap(); - assert_eq!( - fx.bcx.func.dfg.value_type(block_param), - abi_param.value_type - ); + assert_eq!(fx.bcx.func.dfg.value_type(block_param), abi_param.value_type); block_param }) .collect::>(); @@ -319,29 +282,14 @@ pub(super) fn cvalue_for_param<'tcx>( } PassMode::Pair(_, _) => { assert_eq!(block_params.len(), 2, "{:?}", block_params); - Some(CValue::by_val_pair( - block_params[0], - block_params[1], - arg_abi.layout, - )) + Some(CValue::by_val_pair(block_params[0], block_params[1], arg_abi.layout)) } PassMode::Cast(cast) => Some(from_casted_value(fx, &block_params, arg_abi.layout, cast)), - PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack: _, - } => { + PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => { assert_eq!(block_params.len(), 1, "{:?}", block_params); - Some(CValue::by_ref( - Pointer::new(block_params[0]), - arg_abi.layout, - )) + Some(CValue::by_ref(Pointer::new(block_params[0]), arg_abi.layout)) } - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => { + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { assert_eq!(block_params.len(), 2, "{:?}", block_params); Some(CValue::by_ref_unsized( Pointer::new(block_params[0]), diff --git a/src/abi/returning.rs b/src/abi/returning.rs index 40a0320b57996..9fa066df69b3c 100644 --- a/src/abi/returning.rs +++ b/src/abi/returning.rs @@ -13,9 +13,8 @@ pub(crate) fn can_return_to_ssa_var<'tcx>( args: &[mir::Operand<'tcx>], ) -> bool { let fn_ty = fx.monomorphize(func.ty(fx.mir, fx.tcx)); - let fn_sig = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); + let fn_sig = + fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), fn_ty.fn_sig(fx.tcx)); // Handle special calls like instrinsics and empty drop glue. let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() { @@ -42,11 +41,7 @@ pub(crate) fn can_return_to_ssa_var<'tcx>( let fn_abi = if let Some(instance) = instance { FnAbi::of_instance(&RevealAllLayoutCx(fx.tcx), instance, &extra_args) } else { - FnAbi::of_fn_ptr( - &RevealAllLayoutCx(fx.tcx), - fn_ty.fn_sig(fx.tcx), - &extra_args, - ) + FnAbi::of_fn_ptr(&RevealAllLayoutCx(fx.tcx), fn_ty.fn_sig(fx.tcx), &extra_args) }; match fn_abi.ret.mode { PassMode::Ignore | PassMode::Direct(_) | PassMode::Pair(_, _) => true, @@ -63,10 +58,7 @@ pub(super) fn codegen_return_param<'tcx>( block_params_iter: &mut impl Iterator, ) -> CPlace<'tcx> { let (ret_place, ret_param): (_, SmallVec<[_; 2]>) = match fx.fn_abi.as_ref().unwrap().ret.mode { - PassMode::Ignore => ( - CPlace::no_place(fx.fn_abi.as_ref().unwrap().ret.layout), - smallvec![], - ), + PassMode::Ignore => (CPlace::no_place(fx.fn_abi.as_ref().unwrap().ret.layout), smallvec![]), PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => { let is_ssa = ssa_analyzed[RETURN_PLACE] == crate::analyze::SsaKind::Ssa; ( @@ -79,26 +71,17 @@ pub(super) fn codegen_return_param<'tcx>( smallvec![], ) } - PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack: _, - } => { + PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => { let ret_param = block_params_iter.next().unwrap(); assert_eq!(fx.bcx.func.dfg.value_type(ret_param), pointer_ty(fx.tcx)); ( - CPlace::for_ptr( - Pointer::new(ret_param), - fx.fn_abi.as_ref().unwrap().ret.layout, - ), + CPlace::for_ptr(Pointer::new(ret_param), fx.fn_abi.as_ref().unwrap().ret.layout), smallvec![ret_param], ) } - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => unreachable!("unsized return value"), + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { + unreachable!("unsized return value") + } }; #[cfg(not(debug_assertions))] @@ -128,19 +111,13 @@ pub(super) fn codegen_with_call_return_arg<'tcx, T>( ) -> (Inst, T) { let return_ptr = match ret_arg_abi.mode { PassMode::Ignore => None, - PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack: _, - } => match ret_place { + PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => match ret_place { Some(ret_place) => Some(ret_place.to_ptr().get_addr(fx)), None => Some(fx.bcx.ins().iconst(fx.pointer_type, 43)), // FIXME allocate temp stack slot }, - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => unreachable!("unsized return value"), + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { + unreachable!("unsized return value") + } PassMode::Direct(_) | PassMode::Pair(_, _) | PassMode::Cast(_) => None, }; @@ -177,16 +154,10 @@ pub(super) fn codegen_with_call_return_arg<'tcx, T>( ret_place.write_cvalue(fx, result); } } - PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack: _, - } => {} - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => unreachable!("unsized return value"), + PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => {} + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { + unreachable!("unsized return value") + } } (call_inst, meta) @@ -195,19 +166,12 @@ pub(super) fn codegen_with_call_return_arg<'tcx, T>( /// Codegen a return instruction with the right return value(s) if any. pub(crate) fn codegen_return(fx: &mut FunctionCx<'_, '_, '_>) { match fx.fn_abi.as_ref().unwrap().ret.mode { - PassMode::Ignore - | PassMode::Indirect { - attrs: _, - extra_attrs: None, - on_stack: _, - } => { + PassMode::Ignore | PassMode::Indirect { attrs: _, extra_attrs: None, on_stack: _ } => { fx.bcx.ins().return_(&[]); } - PassMode::Indirect { - attrs: _, - extra_attrs: Some(_), - on_stack: _, - } => unreachable!("unsized return value"), + PassMode::Indirect { attrs: _, extra_attrs: Some(_), on_stack: _ } => { + unreachable!("unsized return value") + } PassMode::Direct(_) => { let place = fx.get_local_place(RETURN_PLACE); let ret_val = place.to_cvalue(fx).load_scalar(fx); diff --git a/src/allocator.rs b/src/allocator.rs index 6c5916550ff63..efb64233ef2c3 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -66,13 +66,9 @@ fn codegen_inner( let callee_name = kind.fn_name(method.name); //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns); - let func_id = module - .declare_function(&caller_name, Linkage::Export, &sig) - .unwrap(); + let func_id = module.declare_function(&caller_name, Linkage::Export, &sig).unwrap(); - let callee_func_id = module - .declare_function(&callee_name, Linkage::Import, &sig) - .unwrap(); + let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap(); let mut ctx = Context::new(); ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig.clone()); @@ -96,11 +92,7 @@ fn codegen_inner( bcx.finalize(); } module - .define_function( - func_id, - &mut ctx, - &mut cranelift_codegen::binemit::NullTrapSink {}, - ) + .define_function(func_id, &mut ctx, &mut cranelift_codegen::binemit::NullTrapSink {}) .unwrap(); unwind_context.add_function(func_id, &ctx, module.isa()); } @@ -114,13 +106,10 @@ fn codegen_inner( let callee_name = kind.fn_name(sym::oom); //eprintln!("Codegen allocator shim {} -> {} ({:?} -> {:?})", caller_name, callee_name, sig.params, sig.returns); - let func_id = module - .declare_function("__rust_alloc_error_handler", Linkage::Export, &sig) - .unwrap(); + let func_id = + module.declare_function("__rust_alloc_error_handler", Linkage::Export, &sig).unwrap(); - let callee_func_id = module - .declare_function(&callee_name, Linkage::Import, &sig) - .unwrap(); + let callee_func_id = module.declare_function(&callee_name, Linkage::Import, &sig).unwrap(); let mut ctx = Context::new(); ctx.func = Function::with_name_signature(ExternalName::user(0, 0), sig); @@ -143,11 +132,7 @@ fn codegen_inner( bcx.finalize(); } module - .define_function( - func_id, - &mut ctx, - &mut cranelift_codegen::binemit::NullTrapSink {}, - ) + .define_function(func_id, &mut ctx, &mut cranelift_codegen::binemit::NullTrapSink {}) .unwrap(); unwind_context.add_function(func_id, &ctx, module.isa()); } diff --git a/src/analyze.rs b/src/analyze.rs index 9aeea38c09156..efead25552f4d 100644 --- a/src/analyze.rs +++ b/src/analyze.rs @@ -40,12 +40,7 @@ pub(crate) fn analyze(fx: &FunctionCx<'_, '_, '_>) -> IndexVec { } match &bb.terminator().kind { - TerminatorKind::Call { - destination, - func, - args, - .. - } => { + TerminatorKind::Call { destination, func, args, .. } => { if let Some((dest_place, _dest_bb)) = destination { if !crate::abi::can_return_to_ssa_var(fx, func, args) { not_ssa(&mut flag_map, dest_place.local) diff --git a/src/archive.rs b/src/archive.rs index d2ed553b700b3..7583fc424071e 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -12,10 +12,7 @@ use object::{Object, ObjectSymbol, SymbolKind}; #[derive(Debug)] enum ArchiveEntry { - FromArchive { - archive_index: usize, - entry_index: usize, - }, + FromArchive { archive_index: usize, entry_index: usize }, File(PathBuf), } @@ -45,10 +42,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { let entry = entry.unwrap(); entries.push(( String::from_utf8(entry.header().identifier().to_vec()).unwrap(), - ArchiveEntry::FromArchive { - archive_index: 0, - entry_index: i, - }, + ArchiveEntry::FromArchive { archive_index: 0, entry_index: i }, )); i += 1; } @@ -93,14 +87,9 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { fn add_native_library(&mut self, name: rustc_span::symbol::Symbol) { let location = find_library(name, &self.lib_search_paths, self.sess); - self.add_archive(location.clone(), |_| false) - .unwrap_or_else(|e| { - panic!( - "failed to add native library {}: {}", - location.to_string_lossy(), - e - ); - }); + self.add_archive(location.clone(), |_| false).unwrap_or_else(|e| { + panic!("failed to add native library {}: {}", location.to_string_lossy(), e); + }); } fn add_rlib( @@ -152,10 +141,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { // FIXME only read the symbol table of the object files to avoid having to keep all // object files in memory at once, or read them twice. let data = match entry { - ArchiveEntry::FromArchive { - archive_index, - entry_index, - } => { + ArchiveEntry::FromArchive { archive_index, entry_index } => { // FIXME read symbols from symtab use std::io::Read; let (ref _src_archive_path, ref mut src_archive) = @@ -221,10 +207,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { err )); }), - entries - .iter() - .map(|(name, _)| name.as_bytes().to_vec()) - .collect(), + entries.iter().map(|(name, _)| name.as_bytes().to_vec()).collect(), ar::GnuSymbolTableFormat::Size32, symbol_table, ) @@ -267,8 +250,7 @@ impl<'a> ArchiveBuilder<'a> for ArArchiveBuilder<'a> { .expect("Couldn't run ranlib"); if !status.success() { - self.sess - .fatal(&format!("Ranlib exited with code {:?}", status.code())); + self.sess.fatal(&format!("Ranlib exited with code {:?}", status.code())); } } } @@ -288,13 +270,8 @@ impl<'a> ArArchiveBuilder<'a> { let file_name = String::from_utf8(entry.header().identifier().to_vec()) .map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err))?; if !skip(&file_name) { - self.entries.push(( - file_name, - ArchiveEntry::FromArchive { - archive_index, - entry_index: i, - }, - )); + self.entries + .push((file_name, ArchiveEntry::FromArchive { archive_index, entry_index: i })); } i += 1; } diff --git a/src/backend.rs b/src/backend.rs index a5718244816d2..eb7927fc4adeb 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -22,9 +22,7 @@ pub(crate) trait WriteMetadata { impl WriteMetadata for object::write::Object { fn add_rustc_section(&mut self, symbol_name: String, data: Vec, _is_like_osx: bool) { - let segment = self - .segment_name(object::write::StandardSegment::Data) - .to_vec(); + let segment = self.segment_name(object::write::StandardSegment::Data).to_vec(); let section_id = self.add_section(segment, b".rustc".to_vec(), object::SectionKind::Data); let offset = self.append_section_data(section_id, &data, 1); // For MachO and probably PE this is necessary to prevent the linker from throwing away the @@ -74,11 +72,7 @@ impl WriteDebugInfo for ObjectProduct { let section_id = self.object.add_section( segment, name, - if id == SectionId::EhFrame { - SectionKind::ReadOnlyData - } else { - SectionKind::Debug - }, + if id == SectionId::EhFrame { SectionKind::ReadOnlyData } else { SectionKind::Debug }, ); self.object .section_mut(section_id) @@ -132,10 +126,9 @@ pub(crate) fn with_object(sess: &Session, name: &str, f: impl FnOnce(&mut Object target_lexicon::Architecture::X86_64 => object::Architecture::X86_64, target_lexicon::Architecture::Arm(_) => object::Architecture::Arm, target_lexicon::Architecture::Aarch64(_) => object::Architecture::Aarch64, - architecture => sess.fatal(&format!( - "target architecture {:?} is unsupported", - architecture, - )), + architecture => { + sess.fatal(&format!("target architecture {:?} is unsupported", architecture,)) + } }; let endian = match triple.endianness().unwrap() { target_lexicon::Endianness::Little => object::Endianness::Little, diff --git a/src/base.rs b/src/base.rs index 8a45e1bad42eb..0a7734d6a0443 100644 --- a/src/base.rs +++ b/src/base.rs @@ -38,9 +38,8 @@ pub(crate) fn codegen_fn<'tcx>( // Predefine blocks let start_block = bcx.create_block(); - let block_map: IndexVec = (0..mir.basic_blocks().len()) - .map(|_| bcx.create_block()) - .collect(); + let block_map: IndexVec = + (0..mir.basic_blocks().len()).map(|_| bcx.create_block()).collect(); // Make FunctionCx let pointer_type = cx.module.target_config().pointer_type(); @@ -68,27 +67,23 @@ pub(crate) fn codegen_fn<'tcx>( inline_asm_index: 0, }; - let arg_uninhabited = fx.mir.args_iter().any(|arg| { - fx.layout_of(fx.monomorphize(&fx.mir.local_decls[arg].ty)) - .abi - .is_uninhabited() - }); + let arg_uninhabited = fx + .mir + .args_iter() + .any(|arg| fx.layout_of(fx.monomorphize(&fx.mir.local_decls[arg].ty)).abi.is_uninhabited()); if !crate::constant::check_constants(&mut fx) { - fx.bcx - .append_block_params_for_function_params(fx.block_map[START_BLOCK]); + fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]); fx.bcx.switch_to_block(fx.block_map[START_BLOCK]); crate::trap::trap_unreachable(&mut fx, "compilation should have been aborted"); } else if arg_uninhabited { - fx.bcx - .append_block_params_for_function_params(fx.block_map[START_BLOCK]); + fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]); fx.bcx.switch_to_block(fx.block_map[START_BLOCK]); crate::trap::trap_unreachable(&mut fx, "function has uninhabited argument"); } else { tcx.sess.time("codegen clif ir", || { - tcx.sess.time("codegen prelude", || { - crate::abi::codegen_fn_prelude(&mut fx, start_block) - }); + tcx.sess + .time("codegen prelude", || crate::abi::codegen_fn_prelude(&mut fx, start_block)); codegen_fn_content(&mut fx); }); } @@ -136,11 +131,7 @@ pub(crate) fn codegen_fn<'tcx>( let module = &mut cx.module; tcx.sess.time("define function", || { module - .define_function( - func_id, - context, - &mut cranelift_codegen::binemit::NullTrapSink {}, - ) + .define_function(func_id, context, &mut cranelift_codegen::binemit::NullTrapSink {}) .unwrap() }); @@ -202,8 +193,7 @@ pub(crate) fn verify_func( Some(Box::new(writer)), err, ); - tcx.sess - .fatal(&format!("cranelift verify error:\n{}", pretty_error)); + tcx.sess.fatal(&format!("cranelift verify error:\n{}", pretty_error)); } } }); @@ -232,11 +222,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { #[cfg(debug_assertions)] { let mut terminator_head = "\n".to_string(); - bb_data - .terminator() - .kind - .fmt_head(&mut terminator_head) - .unwrap(); + bb_data.terminator().kind.fmt_head(&mut terminator_head).unwrap(); let inst = fx.bcx.func.layout.last_inst(block).unwrap(); fx.add_comment(inst, terminator_head); } @@ -268,13 +254,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { TerminatorKind::Return => { crate::abi::codegen_return(fx); } - TerminatorKind::Assert { - cond, - expected, - msg, - target, - cleanup: _, - } => { + TerminatorKind::Assert { cond, expected, msg, target, cleanup: _ } => { if !fx.tcx.sess.overflow_checks() { if let mir::AssertKind::OverflowNeg(_) = *msg { let target = fx.get_block(*target); @@ -320,11 +300,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { } } - TerminatorKind::SwitchInt { - discr, - switch_ty, - targets, - } => { + TerminatorKind::SwitchInt { discr, switch_ty, targets } => { let discr = codegen_operand(fx, discr).load_scalar(fx); let use_bool_opt = switch_ty.kind() == fx.tcx.types.bool.kind() @@ -434,11 +410,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { | TerminatorKind::GeneratorDrop => { bug!("shouldn't exist at codegen {:?}", bb_data.terminator()); } - TerminatorKind::Drop { - place, - target, - unwind: _, - } => { + TerminatorKind::Drop { place, target, unwind: _ } => { let drop_place = codegen_place(fx, *place); crate::abi::codegen_drop(fx, bb_data.terminator().source_info.span, drop_place); @@ -471,10 +443,7 @@ fn codegen_stmt<'tcx>( } match &stmt.kind { - StatementKind::SetDiscriminant { - place, - variant_index, - } => { + StatementKind::SetDiscriminant { place, variant_index } => { let place = codegen_place(fx, **place); crate::discriminant::codegen_set_discriminant(fx, place, *variant_index); } @@ -597,14 +566,9 @@ fn codegen_stmt<'tcx>( fn is_fat_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool { ty.builtin_deref(true) - .map( - |ty::TypeAndMut { - ty: pointee_ty, - mutbl: _, - }| { - has_ptr_meta(fx.tcx, pointee_ty) - }, - ) + .map(|ty::TypeAndMut { ty: pointee_ty, mutbl: _ }| { + has_ptr_meta(fx.tcx, pointee_ty) + }) .unwrap_or(false) } @@ -700,8 +664,7 @@ fn codegen_stmt<'tcx>( // FIXME use emit_small_memset where possible let addr = lval.to_ptr().get_addr(fx); let val = operand.load_scalar(fx); - fx.bcx - .call_memset(fx.cx.module.target_config(), addr, val, times); + fx.bcx.call_memset(fx.cx.module.target_config(), addr, val, times); } else { let loop_block = fx.bcx.create_block(); let loop_block2 = fx.bcx.create_block(); @@ -736,25 +699,19 @@ fn codegen_stmt<'tcx>( let content_ty = fx.monomorphize(content_ty); let layout = fx.layout_of(content_ty); let llsize = fx.bcx.ins().iconst(usize_type, layout.size.bytes() as i64); - let llalign = fx - .bcx - .ins() - .iconst(usize_type, layout.align.abi.bytes() as i64); + let llalign = fx.bcx.ins().iconst(usize_type, layout.align.abi.bytes() as i64); let box_layout = fx.layout_of(fx.tcx.mk_box(content_ty)); // Allocate space: - let def_id = match fx - .tcx - .lang_items() - .require(rustc_hir::LangItem::ExchangeMalloc) - { - Ok(id) => id, - Err(s) => { - fx.tcx - .sess - .fatal(&format!("allocation of `{}` {}", box_layout.ty, s)); - } - }; + let def_id = + match fx.tcx.lang_items().require(rustc_hir::LangItem::ExchangeMalloc) { + Ok(id) => id, + Err(s) => { + fx.tcx + .sess + .fatal(&format!("allocation of `{}` {}", box_layout.ty, s)); + } + }; let instance = ty::Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx); let func_ref = fx.get_function_ref(instance); let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]); @@ -762,10 +719,11 @@ fn codegen_stmt<'tcx>( lval.write_cvalue(fx, CValue::by_val(ptr, box_layout)); } Rvalue::NullaryOp(NullOp::SizeOf, ty) => { - assert!(lval - .layout() - .ty - .is_sized(fx.tcx.at(stmt.source_info.span), ParamEnv::reveal_all())); + assert!( + lval.layout() + .ty + .is_sized(fx.tcx.at(stmt.source_info.span), ParamEnv::reveal_all()) + ); let ty_size = fx.layout_of(fx.monomorphize(ty)).size.bytes(); let val = CValue::const_val(fx, fx.layout_of(fx.tcx.types.usize), ty_size.into()); @@ -793,11 +751,7 @@ fn codegen_stmt<'tcx>( StatementKind::LlvmInlineAsm(asm) => { use rustc_span::symbol::Symbol; - let LlvmInlineAsm { - asm, - outputs, - inputs, - } = &**asm; + let LlvmInlineAsm { asm, outputs, inputs } = &**asm; let rustc_hir::LlvmInlineAsmInner { asm: asm_code, // Name outputs: output_names, // Vec @@ -813,15 +767,9 @@ fn codegen_stmt<'tcx>( // Black box } "mov %rbx, %rsi\n cpuid\n xchg %rbx, %rsi" => { - assert_eq!( - input_names, - &[Symbol::intern("{eax}"), Symbol::intern("{ecx}")] - ); + assert_eq!(input_names, &[Symbol::intern("{eax}"), Symbol::intern("{ecx}")]); assert_eq!(output_names.len(), 4); - for (i, c) in (&["={eax}", "={esi}", "={ecx}", "={edx}"]) - .iter() - .enumerate() - { + for (i, c) in (&["={eax}", "={esi}", "={ecx}", "={edx}"]).iter().enumerate() { assert_eq!(&output_names[i].constraint.as_str(), c); assert!(!output_names[i].is_rw); assert!(!output_names[i].is_indirect); @@ -867,12 +815,7 @@ fn codegen_stmt<'tcx>( crate::trap::trap_unimplemented(fx, "_xgetbv arch intrinsic is not supported"); } // ___chkstk, ___chkstk_ms and __alloca are only used on Windows - _ if fx - .tcx - .symbol_name(fx.instance) - .name - .starts_with("___chkstk") => - { + _ if fx.tcx.symbol_name(fx.instance).name.starts_with("___chkstk") => { crate::trap::trap_unimplemented(fx, "Stack probes are not supported"); } _ if fx.tcx.symbol_name(fx.instance).name == "__alloca" => { @@ -895,15 +838,12 @@ fn codegen_stmt<'tcx>( fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>) -> Value { match *place.layout().ty.kind() { ty::Array(_elem_ty, len) => { - let len = fx - .monomorphize(len) - .eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64; + let len = fx.monomorphize(len).eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64; fx.bcx.ins().iconst(fx.pointer_type, len) } - ty::Slice(_elem_ty) => place - .to_ptr_maybe_unsized() - .1 - .expect("Length metadata for slice place"), + ty::Slice(_elem_ty) => { + place.to_ptr_maybe_unsized().1.expect("Length metadata for slice place") + } _ => bug!("Rvalue::Len({:?})", place), } } @@ -926,11 +866,7 @@ pub(crate) fn codegen_place<'tcx>( let index = fx.get_local_place(local).to_cvalue(fx).load_scalar(fx); cplace = cplace.place_index(fx, index); } - PlaceElem::ConstantIndex { - offset, - min_length: _, - from_end, - } => { + PlaceElem::ConstantIndex { offset, min_length: _, from_end } => { let offset: u64 = offset; let index = if !from_end { fx.bcx.ins().iconst(fx.pointer_type, offset as i64) @@ -997,10 +933,7 @@ pub(crate) fn codegen_panic<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, msg_str: &s let location = fx.get_caller_location(span).load_scalar(fx); let msg_ptr = fx.anonymous_str("assert", msg_str); - let msg_len = fx - .bcx - .ins() - .iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap()); + let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap()); let args = [msg_ptr, msg_len, location]; codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, span); @@ -1012,11 +945,8 @@ pub(crate) fn codegen_panic_inner<'tcx>( args: &[Value], span: Span, ) { - let def_id = fx - .tcx - .lang_items() - .require(lang_item) - .unwrap_or_else(|s| fx.tcx.sess.span_fatal(span, &s)); + let def_id = + fx.tcx.lang_items().require(lang_item).unwrap_or_else(|s| fx.tcx.sess.span_fatal(span, &s)); let instance = Instance::mono(fx.tcx, def_id).polymorphize(fx.tcx); let symbol_name = fx.tcx.symbol_name(instance).name; diff --git a/src/bin/cg_clif.rs b/src/bin/cg_clif.rs index be369b07fddfe..983839d48d2d7 100644 --- a/src/bin/cg_clif.rs +++ b/src/bin/cg_clif.rs @@ -27,13 +27,7 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks { config.opts.cg.panic = Some(PanicStrategy::Abort); config.opts.debugging_opts.panic_abort_tests = true; config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| { - std::env::current_exe() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .to_owned() + std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned() })); } } diff --git a/src/bin/cg_clif_build_sysroot.rs b/src/bin/cg_clif_build_sysroot.rs index 83e5dc6e6724d..e7cd5edbbf654 100644 --- a/src/bin/cg_clif_build_sysroot.rs +++ b/src/bin/cg_clif_build_sysroot.rs @@ -46,15 +46,8 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks { config.opts.cg.panic = Some(PanicStrategy::Abort); config.opts.debugging_opts.panic_abort_tests = true; - config.opts.maybe_sysroot = Some( - std::env::current_exe() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .to_owned(), - ); + config.opts.maybe_sysroot = + Some(std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned()); } } diff --git a/src/cast.rs b/src/cast.rs index 432533c7636bc..74c5e09f08da0 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -40,11 +40,7 @@ pub(crate) fn clif_intcast( // reduce (types::I128, _) => { let (lsb, _msb) = fx.bcx.ins().isplit(val); - if to == types::I64 { - lsb - } else { - fx.bcx.ins().ireduce(to, lsb) - } + if to == types::I64 { lsb } else { fx.bcx.ins().ireduce(to, lsb) } } (_, _) => fx.bcx.ins().ireduce(to, val), } @@ -87,11 +83,7 @@ pub(crate) fn clif_int_or_float_cast( }, ); - let from_rust_ty = if from_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; + let from_rust_ty = if from_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 }; let to_rust_ty = match to_ty { types::F32 => fx.tcx.types.f32, @@ -100,11 +92,7 @@ pub(crate) fn clif_int_or_float_cast( }; return fx - .easy_call( - &name, - &[CValue::by_val(from, fx.layout_of(from_rust_ty))], - to_rust_ty, - ) + .easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty) .load_scalar(fx); } @@ -138,18 +126,10 @@ pub(crate) fn clif_int_or_float_cast( _ => unreachable!(), }; - let to_rust_ty = if to_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; + let to_rust_ty = if to_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 }; return fx - .easy_call( - &name, - &[CValue::by_val(from, fx.layout_of(from_rust_ty))], - to_rust_ty, - ) + .easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty) .load_scalar(fx); } diff --git a/src/codegen_i128.rs b/src/codegen_i128.rs index 60fffa60484e3..ae75e6508cb0b 100644 --- a/src/codegen_i128.rs +++ b/src/codegen_i128.rs @@ -31,11 +31,7 @@ pub(crate) fn maybe_codegen<'tcx>( } BinOp::Add | BinOp::Sub if !checked => None, BinOp::Mul if !checked => { - let val_ty = if is_signed { - fx.tcx.types.i128 - } else { - fx.tcx.types.u128 - }; + let val_ty = if is_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 }; Some(fx.easy_call("__multi3", &[lhs, rhs], val_ty)) } BinOp::Add | BinOp::Sub | BinOp::Mul => { @@ -47,11 +43,7 @@ pub(crate) fn maybe_codegen<'tcx>( AbiParam::new(types::I128), AbiParam::new(types::I128), ]; - let args = [ - out_place.to_ptr().get_addr(fx), - lhs.load_scalar(fx), - rhs.load_scalar(fx), - ]; + let args = [out_place.to_ptr().get_addr(fx), lhs.load_scalar(fx), rhs.load_scalar(fx)]; let name = match (bin_op, is_signed) { (BinOp::Add, false) => "__rust_u128_addo", (BinOp::Add, true) => "__rust_i128_addo", diff --git a/src/common.rs b/src/common.rs index 2c34c8ee5e832..6a4a6744a5cf7 100644 --- a/src/common.rs +++ b/src/common.rs @@ -54,11 +54,7 @@ fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option types::F64, }, ty::FnPtr(_) => pointer_ty(tcx), - ty::RawPtr(TypeAndMut { - ty: pointee_ty, - mutbl: _, - }) - | ty::Ref(_, pointee_ty, _) => { + ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => { if has_ptr_meta(tcx, pointee_ty) { return None; } else { @@ -97,11 +93,7 @@ fn clif_pair_type_from_ty<'tcx>( } (a, b) } - ty::RawPtr(TypeAndMut { - ty: pointee_ty, - mutbl: _, - }) - | ty::Ref(_, pointee_ty, _) => { + ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => { if has_ptr_meta(tcx, pointee_ty) { (pointer_ty(tcx), pointer_ty(tcx)) } else { @@ -114,15 +106,8 @@ fn clif_pair_type_from_ty<'tcx>( /// Is a pointer to this type a fat ptr? pub(crate) fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool { - let ptr_ty = tcx.mk_ptr(TypeAndMut { - ty, - mutbl: rustc_hir::Mutability::Not, - }); - match &tcx - .layout_of(ParamEnv::reveal_all().and(ptr_ty)) - .unwrap() - .abi - { + let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc_hir::Mutability::Not }); + match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi { Abi::Scalar(_) => false, Abi::ScalarPair(_, _) => true, abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi), @@ -369,12 +354,7 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { let msg_id = self .cx .module - .declare_data( - &format!("__{}_{:08x}", prefix, msg_hash), - Linkage::Local, - false, - false, - ) + .declare_data(&format!("__{}_{:08x}", prefix, msg_hash), Linkage::Local, false, false) .unwrap(); // Ignore DuplicateDefinition error, as the data will be the same @@ -397,15 +377,13 @@ impl<'tcx> LayoutOf for RevealAllLayoutCx<'tcx> { fn layout_of(&self, ty: Ty<'tcx>) -> TyAndLayout<'tcx> { assert!(!ty.still_further_specializable()); - self.0 - .layout_of(ParamEnv::reveal_all().and(&ty)) - .unwrap_or_else(|e| { - if let layout::LayoutError::SizeOverflow(_) = e { - self.0.sess.fatal(&e.to_string()) - } else { - bug!("failed to get layout for `{}`: {}", ty, e) - } - }) + self.0.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap_or_else(|e| { + if let layout::LayoutError::SizeOverflow(_) = e { + self.0.sess.fatal(&e.to_string()) + } else { + bug!("failed to get layout for `{}`: {}", ty, e) + } + }) } } diff --git a/src/constant.rs b/src/constant.rs index 3712357746b74..b0639cf9e15fe 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -44,15 +44,12 @@ pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, '_>) -> bool { ConstKind::Value(_) => {} ConstKind::Unevaluated(def, ref substs, promoted) => { if let Err(err) = - fx.tcx - .const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) + fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) { all_constants_ok = false; match err { ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => { - fx.tcx - .sess - .span_err(constant.span, "erroneous constant encountered"); + fx.tcx.sess.span_err(constant.span, "erroneous constant encountered"); } ErrorHandled::TooGeneric => { span_bug!( @@ -131,16 +128,10 @@ pub(crate) fn codegen_constant<'tcx>( .to_cvalue(fx); } ConstKind::Unevaluated(def, ref substs, promoted) => { - match fx - .tcx - .const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) - { + match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) { Ok(const_val) => const_val, Err(_) => { - span_bug!( - constant.span, - "erroneous constant not captured by required_consts" - ); + span_bug!(constant.span, "erroneous constant not captured by required_consts"); } } } @@ -171,9 +162,7 @@ pub(crate) fn codegen_const_value<'tcx>( if fx.clif_type(layout.ty).is_none() { let (size, align) = (layout.size, layout.align.pref); let mut alloc = Allocation::from_bytes( - std::iter::repeat(0) - .take(size.bytes_usize()) - .collect::>(), + std::iter::repeat(0).take(size.bytes_usize()).collect::>(), align, ); let ptr = Pointer::new(AllocId(!0), Size::ZERO); // The alloc id is never used @@ -216,9 +205,7 @@ pub(crate) fn codegen_const_value<'tcx>( None => bug!("missing allocation {:?}", ptr.alloc_id), }; let val = if ptr.offset.bytes() != 0 { - fx.bcx - .ins() - .iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap()) + fx.bcx.ins().iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap()) } else { base_addr }; @@ -235,10 +222,10 @@ pub(crate) fn codegen_const_value<'tcx>( let ptr = pointer_for_allocation(fx, data) .offset_i64(fx, i64::try_from(start).unwrap()) .get_addr(fx); - let len = fx.bcx.ins().iconst( - fx.pointer_type, - i64::try_from(end.checked_sub(start).unwrap()).unwrap(), - ); + let len = fx + .bcx + .ins() + .iconst(fx.pointer_type, i64::try_from(end.checked_sub(start).unwrap()).unwrap()); CValue::by_val_pair(ptr, len, layout) } } @@ -299,12 +286,7 @@ fn data_id_for_static( } else { !ty.is_freeze(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) }; - let align = tcx - .layout_of(ParamEnv::reveal_all().and(ty)) - .unwrap() - .align - .pref - .bytes(); + let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes(); let attrs = tcx.codegen_fn_attrs(def_id); @@ -327,17 +309,11 @@ fn data_id_for_static( // zero. let ref_name = format!("_rust_extern_with_linkage_{}", symbol_name); - let ref_data_id = module - .declare_data(&ref_name, Linkage::Local, false, false) - .unwrap(); + let ref_data_id = module.declare_data(&ref_name, Linkage::Local, false, false).unwrap(); let mut data_ctx = DataContext::new(); data_ctx.set_align(align); let data = module.declare_data_in_data(data_id, &mut data_ctx); - data_ctx.define( - std::iter::repeat(0) - .take(pointer_ty(tcx).bytes() as usize) - .collect(), - ); + data_ctx.define(std::iter::repeat(0).take(pointer_ty(tcx).bytes() as usize).collect()); data_ctx.write_data_addr(0, data, 0); match module.define_data(ref_data_id, &data_ctx) { // Every time the static is referenced there will be another definition of this global, @@ -366,10 +342,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant TodoItem::Static(def_id) => { //println!("static {:?}", def_id); - let section_name = tcx - .codegen_fn_attrs(def_id) - .link_section - .map(|s| s.as_str()); + let section_name = tcx.codegen_fn_attrs(def_id).link_section.map(|s| s.as_str()); let alloc = tcx.eval_static_initializer(def_id).unwrap(); @@ -391,9 +364,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant data_ctx.set_segment_section("", &*section_name); } - let bytes = alloc - .inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()) - .to_vec(); + let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len()).to_vec(); data_ctx.define(bytes.into_boxed_slice()); for &(offset, (_tag, reloc)) in alloc.relocations().iter() { @@ -421,10 +392,7 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant data_id_for_alloc_id(module, reloc, target_alloc.mutability) } GlobalAlloc::Static(def_id) => { - if tcx - .codegen_fn_attrs(def_id) - .flags - .contains(CodegenFnAttrFlags::THREAD_LOCAL) + if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) { tcx.sess.fatal(&format!( "Allocation {:?} contains reference to TLS value {:?}", @@ -457,9 +425,8 @@ pub(crate) fn mir_operand_get_const_val<'tcx>( ) -> Option<&'tcx Const<'tcx>> { match operand { Operand::Copy(_) | Operand::Move(_) => None, - Operand::Constant(const_) => Some( - fx.monomorphize(const_.literal) - .eval(fx.tcx, ParamEnv::reveal_all()), - ), + Operand::Constant(const_) => { + Some(fx.monomorphize(const_.literal).eval(fx.tcx, ParamEnv::reveal_all())) + } } } diff --git a/src/debuginfo/emit.rs b/src/debuginfo/emit.rs index 6160f9b78d8b3..6018eefcd42fb 100644 --- a/src/debuginfo/emit.rs +++ b/src/debuginfo/emit.rs @@ -14,10 +14,7 @@ impl DebugContext<'_> { let unit_range_list_id = self.dwarf.unit.ranges.add(self.unit_range_list.clone()); let root = self.dwarf.unit.root(); let root = self.dwarf.unit.get_mut(root); - root.set( - gimli::DW_AT_ranges, - AttributeValue::RangeListRef(unit_range_list_id), - ); + root.set(gimli::DW_AT_ranges, AttributeValue::RangeListRef(unit_range_list_id)); let mut sections = Sections::new(WriterRelocate::new(self.endian)); self.dwarf.write(&mut sections).unwrap(); @@ -66,10 +63,7 @@ pub(super) struct WriterRelocate { impl WriterRelocate { pub(super) fn new(endian: RunTimeEndian) -> Self { - WriterRelocate { - relocs: Vec::new(), - writer: EndianVec::new(endian), - } + WriterRelocate { relocs: Vec::new(), writer: EndianVec::new(endian) } } /// Perform the collected relocations to be usable for JIT usage. @@ -85,9 +79,7 @@ impl WriterRelocate { cranelift_module::FuncId::from_u32(sym.try_into().unwrap()), ); let val = (addr as u64 as i64 + reloc.addend) as u64; - self.writer - .write_udata_at(reloc.offset as usize, val, reloc.size) - .unwrap(); + self.writer.write_udata_at(reloc.offset as usize, val, reloc.size).unwrap(); } } } diff --git a/src/debuginfo/line_info.rs b/src/debuginfo/line_info.rs index a40f963400c66..30ed356c7627f 100644 --- a/src/debuginfo/line_info.rs +++ b/src/debuginfo/line_info.rs @@ -53,11 +53,7 @@ pub(crate) fn make_file_info(hash: SourceFileHash) -> Option { if hash.kind == SourceFileHashAlgorithm::Md5 { let mut buf = [0u8; MD5_LEN]; buf.copy_from_slice(hash.hash_bytes()); - Some(FileInfo { - timestamp: 0, - size: 0, - md5: buf, - }) + Some(FileInfo { timestamp: 0, size: 0, md5: buf }) } else { None } @@ -112,19 +108,10 @@ impl<'tcx> DebugContext<'tcx> { let entry = self.dwarf.unit.get_mut(entry_id); - entry.set( - gimli::DW_AT_decl_file, - AttributeValue::FileIndex(Some(file_id)), - ); - entry.set( - gimli::DW_AT_decl_line, - AttributeValue::Udata(loc.line as u64), - ); + entry.set(gimli::DW_AT_decl_file, AttributeValue::FileIndex(Some(file_id))); + entry.set(gimli::DW_AT_decl_line, AttributeValue::Udata(loc.line as u64)); // FIXME: probably omit this - entry.set( - gimli::DW_AT_decl_column, - AttributeValue::Udata(loc.col.to_usize() as u64), - ); + entry.set(gimli::DW_AT_decl_column, AttributeValue::Udata(loc.col.to_usize() as u64)); } pub(super) fn create_debug_lines( @@ -223,10 +210,7 @@ impl<'tcx> DebugContext<'tcx> { gimli::DW_AT_low_pc, AttributeValue::Address(Address::Symbol { symbol, addend: 0 }), ); - entry.set( - gimli::DW_AT_high_pc, - AttributeValue::Udata(u64::from(func_end)), - ); + entry.set(gimli::DW_AT_high_pc, AttributeValue::Udata(u64::from(func_end))); self.emit_location(entry_id, function_span); diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 87b1c5fb50e6d..dc8bc8d9cb741 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -90,20 +90,11 @@ impl<'tcx> DebugContext<'tcx> { let root = dwarf.unit.root(); let root = dwarf.unit.get_mut(root); - root.set( - gimli::DW_AT_producer, - AttributeValue::StringRef(dwarf.strings.add(producer)), - ); - root.set( - gimli::DW_AT_language, - AttributeValue::Language(gimli::DW_LANG_Rust), - ); + root.set(gimli::DW_AT_producer, AttributeValue::StringRef(dwarf.strings.add(producer))); + root.set(gimli::DW_AT_language, AttributeValue::Language(gimli::DW_LANG_Rust)); root.set(gimli::DW_AT_name, AttributeValue::StringRef(name)); root.set(gimli::DW_AT_comp_dir, AttributeValue::StringRef(comp_dir)); - root.set( - gimli::DW_AT_low_pc, - AttributeValue::Address(Address::Constant(0)), - ); + root.set(gimli::DW_AT_low_pc, AttributeValue::Address(Address::Constant(0))); } DebugContext { @@ -142,10 +133,7 @@ impl<'tcx> DebugContext<'tcx> { ty::Int(_) => primitive(&mut self.dwarf, gimli::DW_ATE_signed), ty::Float(_) => primitive(&mut self.dwarf, gimli::DW_ATE_float), ty::Ref(_, pointee_ty, _mutbl) - | ty::RawPtr(ty::TypeAndMut { - ty: pointee_ty, - mutbl: _mutbl, - }) => { + | ty::RawPtr(ty::TypeAndMut { ty: pointee_ty, mutbl: _mutbl }) => { let type_id = new_entry(&mut self.dwarf, gimli::DW_TAG_pointer_type); // Ensure that type is inserted before recursing to avoid duplicates @@ -172,10 +160,7 @@ impl<'tcx> DebugContext<'tcx> { let field_offset = layout.fields.offset(field_idx); let field_layout = layout .field( - &layout::LayoutCx { - tcx: self.tcx, - param_env: ParamEnv::reveal_all(), - }, + &layout::LayoutCx { tcx: self.tcx, param_env: ParamEnv::reveal_all() }, field_idx, ) .unwrap(); @@ -204,10 +189,7 @@ impl<'tcx> DebugContext<'tcx> { let type_entry = self.dwarf.unit.get_mut(type_id); type_entry.set(gimli::DW_AT_name, AttributeValue::String(name.into_bytes())); - type_entry.set( - gimli::DW_AT_byte_size, - AttributeValue::Udata(layout.size.bytes()), - ); + type_entry.set(gimli::DW_AT_byte_size, AttributeValue::Udata(layout.size.bytes())); self.types.insert(ty, type_id); @@ -247,10 +229,7 @@ impl<'tcx> DebugContext<'tcx> { let name_id = self.dwarf.strings.add(name); // Gdb requires DW_AT_name. Otherwise the DW_TAG_subprogram is skipped. entry.set(gimli::DW_AT_name, AttributeValue::StringRef(name_id)); - entry.set( - gimli::DW_AT_linkage_name, - AttributeValue::StringRef(name_id), - ); + entry.set(gimli::DW_AT_linkage_name, AttributeValue::StringRef(name_id)); let end = self.create_debug_lines(symbol, entry_id, context, mir.span, source_info_set); @@ -287,10 +266,7 @@ impl<'tcx> DebugContext<'tcx> { context, &local_map, &value_labels_ranges, - Place { - local, - projection: ty::List::empty(), - }, + Place { local, projection: ty::List::empty() }, ); let var_entry = self.dwarf.unit.get_mut(var_id); @@ -328,10 +304,7 @@ fn place_location<'tcx>( symbol, addend: i64::from(value_loc_range.start), }, - end: Address::Symbol { - symbol, - addend: i64::from(value_loc_range.end), - }, + end: Address::Symbol { symbol, addend: i64::from(value_loc_range.end) }, data: translate_loc( isa, value_loc_range.loc, diff --git a/src/debuginfo/unwind.rs b/src/debuginfo/unwind.rs index 49de927cdba05..357c9fe6ed83a 100644 --- a/src/debuginfo/unwind.rs +++ b/src/debuginfo/unwind.rs @@ -28,11 +28,7 @@ impl<'tcx> UnwindContext<'tcx> { None }; - UnwindContext { - tcx, - frame_table, - cie_id, - } + UnwindContext { tcx, frame_table, cie_id } } pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) { @@ -46,10 +42,8 @@ impl<'tcx> UnwindContext<'tcx> { UnwindInfo::SystemV(unwind_info) => { self.frame_table.add_fde( self.cie_id.unwrap(), - unwind_info.to_fde(Address::Symbol { - symbol: func_id.as_u32() as usize, - addend: 0, - }), + unwind_info + .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }), ); } UnwindInfo::WindowsX64(_) => { @@ -60,9 +54,8 @@ impl<'tcx> UnwindContext<'tcx> { } pub(crate) fn emit(self, product: &mut P) { - let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian( - self.tcx, - ))); + let mut eh_frame = + EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx))); self.frame_table.write_eh_frame(&mut eh_frame).unwrap(); if !eh_frame.0.writer.slice().is_empty() { @@ -82,9 +75,8 @@ impl<'tcx> UnwindContext<'tcx> { self, jit_module: &cranelift_jit::JITModule, ) -> Option { - let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(super::target_endian( - self.tcx, - ))); + let mut eh_frame = + EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx))); self.frame_table.write_eh_frame(&mut eh_frame).unwrap(); if eh_frame.0.writer.slice().is_empty() { @@ -130,10 +122,7 @@ impl<'tcx> UnwindContext<'tcx> { registrations.push(ptr as usize); } - Some(UnwindRegistry { - _frame_table: eh_frame, - registrations, - }) + Some(UnwindRegistry { _frame_table: eh_frame, registrations }) } } diff --git a/src/discriminant.rs b/src/discriminant.rs index 3815e149ffa1a..3326f87f00075 100644 --- a/src/discriminant.rs +++ b/src/discriminant.rs @@ -26,11 +26,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>( variants: _, } => { let ptr = place.place_field(fx, mir::Field::new(tag_field)); - let to = layout - .ty - .discriminant_for_variant(fx.tcx, variant_index) - .unwrap() - .val; + let to = layout.ty.discriminant_for_variant(fx.tcx, variant_index).unwrap().val; let to = if ptr.layout().abi.is_signed() { ty::ScalarInt::try_from_int( ptr.layout().size.sign_extend(to) as i128, @@ -46,12 +42,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>( Variants::Multiple { tag: _, tag_field, - tag_encoding: - TagEncoding::Niche { - dataful_variant, - ref niche_variants, - niche_start, - }, + tag_encoding: TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start }, variants: _, } => { if variant_index != dataful_variant { @@ -101,12 +92,9 @@ pub(crate) fn codegen_get_discriminant<'tcx>( }; return CValue::const_val(fx, dest_layout, discr_val); } - Variants::Multiple { - tag, - tag_field, - tag_encoding, - variants: _, - } => (tag, *tag_field, tag_encoding), + Variants::Multiple { tag, tag_field, tag_encoding, variants: _ } => { + (tag, *tag_field, tag_encoding) + } }; let cast_to = fx.clif_type(dest_layout.ty).unwrap(); @@ -125,11 +113,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>( let val = clif_intcast(fx, tag, cast_to, signed); CValue::by_val(val, dest_layout) } - TagEncoding::Niche { - dataful_variant, - ref niche_variants, - niche_start, - } => { + TagEncoding::Niche { dataful_variant, ref niche_variants, niche_start } => { // Rebase from niche values to discriminants, and check // whether the result is in range for the niche variants. @@ -146,9 +130,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>( tag } else { // FIXME handle niche_start > i64::MAX - fx.bcx - .ins() - .iadd_imm(tag, -i64::try_from(niche_start).unwrap()) + fx.bcx.ins().iadd_imm(tag, -i64::try_from(niche_start).unwrap()) }; let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32(); let is_niche = { @@ -176,15 +158,10 @@ pub(crate) fn codegen_get_discriminant<'tcx>( } else { clif_intcast(fx, relative_discr, cast_to, false) }; - fx.bcx - .ins() - .iadd_imm(relative_discr, i64::from(niche_variants.start().as_u32())) + fx.bcx.ins().iadd_imm(relative_discr, i64::from(niche_variants.start().as_u32())) }; - let dataful_variant = fx - .bcx - .ins() - .iconst(cast_to, i64::from(dataful_variant.as_u32())); + let dataful_variant = fx.bcx.ins().iconst(cast_to, i64::from(dataful_variant.as_u32())); let discr = fx.bcx.ins().select(is_niche, niche_discr, dataful_variant); CValue::by_val(discr, dest_layout) } diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 07c6c27817c54..b87dcc41928b6 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -46,13 +46,10 @@ fn emit_module( unwind_context.emit(&mut product); - let tmp_file = tcx - .output_filenames(LOCAL_CRATE) - .temp_path(OutputType::Object, Some(&name)); + let tmp_file = tcx.output_filenames(LOCAL_CRATE).temp_path(OutputType::Object, Some(&name)); let obj = product.object.write().unwrap(); if let Err(err) = std::fs::write(&tmp_file, obj) { - tcx.sess - .fatal(&format!("error writing object file: {}", err)); + tcx.sess.fatal(&format!("error writing object file: {}", err)); } let work_product = if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() { @@ -66,13 +63,7 @@ fn emit_module( }; ModuleCodegenResult( - CompiledModule { - name, - kind, - object: Some(tmp_file), - dwarf_object: None, - bytecode: None, - }, + CompiledModule { name, kind, object: Some(tmp_file), dwarf_object: None, bytecode: None }, work_product, ) } @@ -132,9 +123,7 @@ fn module_codegen( let linkage = crate::linkage::get_clif_linkage(mono_item, linkage, visibility); match mono_item { MonoItem::Fn(inst) => { - cx.tcx.sess.time("codegen fn", || { - crate::base::codegen_fn(&mut cx, inst, linkage) - }); + cx.tcx.sess.time("codegen fn", || crate::base::codegen_fn(&mut cx, inst, linkage)); } MonoItem::Static(def_id) => { crate::constant::codegen_static(&mut cx.constants_cx, def_id) @@ -194,9 +183,7 @@ pub(super) fn run_aot( cgus.iter() .map(|cgu| { let cgu_reuse = determine_cgu_reuse(tcx, cgu); - tcx.sess - .cgu_reuse_tracker - .set_actual_reuse(&cgu.name().as_str(), cgu_reuse); + tcx.sess.cgu_reuse_tracker.set_actual_reuse(&cgu.name().as_str(), cgu_reuse); match cgu_reuse { _ if std::env::var("CG_CLIF_INCR_CACHE_DISABLED").is_ok() => {} @@ -269,8 +256,7 @@ pub(super) fn run_aot( }); if let Err(err) = std::fs::write(&tmp_file, obj) { - tcx.sess - .fatal(&format!("error writing metadata object file: {}", err)); + tcx.sess.fatal(&format!("error writing metadata object file: {}", err)); } (metadata_cgu_name, tmp_file) @@ -324,8 +310,7 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) { "asm! and global_asm! support is disabled while compiling rustc_codegen_cranelift", ); } else { - tcx.sess - .fatal("asm! and global_asm! are not yet supported on macOS and Windows"); + tcx.sess.fatal("asm! and global_asm! are not yet supported on macOS and Windows"); } } @@ -335,19 +320,12 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) { // Remove all LLVM style comments let global_asm = global_asm .lines() - .map(|line| { - if let Some(index) = line.find("//") { - &line[0..index] - } else { - line - } - }) + .map(|line| if let Some(index) = line.find("//") { &line[0..index] } else { line }) .collect::>() .join("\n"); - let output_object_file = tcx - .output_filenames(LOCAL_CRATE) - .temp_path(OutputType::Object, Some(cgu_name)); + let output_object_file = + tcx.output_filenames(LOCAL_CRATE).temp_path(OutputType::Object, Some(cgu_name)); // Assemble `global_asm` let global_asm_object_file = add_file_stem_postfix(output_object_file.clone(), ".asm"); @@ -357,16 +335,10 @@ fn codegen_global_asm(tcx: TyCtxt<'_>, cgu_name: &str, global_asm: &str) { .stdin(Stdio::piped()) .spawn() .expect("Failed to spawn `as`."); - child - .stdin - .take() - .unwrap() - .write_all(global_asm.as_bytes()) - .unwrap(); + child.stdin.take().unwrap().write_all(global_asm.as_bytes()).unwrap(); let status = child.wait().expect("Failed to wait for `as`."); if !status.success() { - tcx.sess - .fatal(&format!("Failed to assemble `{}`", global_asm)); + tcx.sess.fatal(&format!("Failed to assemble `{}`", global_asm)); } // Link the global asm and main object file together @@ -410,11 +382,7 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR } let work_product_id = &cgu.work_product_id(); - if tcx - .dep_graph - .previous_work_product(work_product_id) - .is_none() - { + if tcx.dep_graph.previous_work_product(work_product_id).is_none() { // We don't have anything cached for this CGU. This can happen // if the CGU did not exist in the previous session. return CguReuse::No; @@ -433,9 +401,5 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR cgu.name() ); - if tcx.try_mark_green(&dep_node) { - CguReuse::PreLto - } else { - CguReuse::No - } + if tcx.try_mark_green(&dep_node) { CguReuse::PreLto } else { CguReuse::No } } diff --git a/src/driver/jit.rs b/src/driver/jit.rs index beb316c58c3d1..245df03ffb84d 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -25,10 +25,8 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { let imported_symbols = load_imported_symbols_for_jit(tcx); - let mut jit_builder = JITBuilder::with_isa( - crate::build_isa(tcx.sess), - cranelift_module::default_libcall_names(), - ); + let mut jit_builder = + JITBuilder::with_isa(crate::build_isa(tcx.sess), cranelift_module::default_libcall_names()); jit_builder.hotswap(matches!(backend_config.codegen_mode, CodegenMode::JitLazy)); jit_builder.symbols(imported_symbols); let mut jit_module = JITModule::new(jit_builder); @@ -39,14 +37,10 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { AbiParam::new(jit_module.target_config().pointer_type()), AbiParam::new(jit_module.target_config().pointer_type()), ], - returns: vec![AbiParam::new( - jit_module.target_config().pointer_type(), /*isize*/ - )], + returns: vec![AbiParam::new(jit_module.target_config().pointer_type() /*isize*/)], call_conv: CallConv::triple_default(&crate::target_triple(tcx.sess)), }; - let main_func_id = jit_module - .declare_function("main", Linkage::Import, &sig) - .unwrap(); + let main_func_id = jit_module.declare_function("main", Linkage::Import, &sig).unwrap(); let (_, cgus) = tcx.collect_and_partition_mono_items(LOCAL_CRATE); let mono_items = cgus @@ -67,9 +61,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { MonoItem::Fn(inst) => match backend_config.codegen_mode { CodegenMode::Aot => unreachable!(), CodegenMode::Jit => { - cx.tcx.sess.time("codegen fn", || { - crate::base::codegen_fn(&mut cx, inst, linkage) - }); + cx.tcx + .sess + .time("codegen fn", || crate::base::codegen_fn(&mut cx, inst, linkage)); } CodegenMode::JitLazy => codegen_shim(&mut cx, inst), }, @@ -78,8 +72,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { } MonoItem::GlobalAsm(item_id) => { let item = cx.tcx.hir().item(item_id); - tcx.sess - .span_fatal(item.span, "Global asm is not supported in JIT mode"); + tcx.sess.span_fatal(item.span, "Global asm is not supported in JIT mode"); } } } @@ -104,7 +97,9 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { let finalized_main: *const u8 = jit_module.get_finalized_function(main_func_id); - println!("Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed"); + println!( + "Rustc codegen cranelift will JIT run the executable, because -Cllvm-args=mode=jit was passed" + ); let f: extern "C" fn(c_int, *const *const c_char) -> c_int = unsafe { ::std::mem::transmute(finalized_main) }; @@ -121,10 +116,7 @@ pub(super) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { argv.push(std::ptr::null()); BACKEND_CONFIG.with(|tls_backend_config| { - assert!(tls_backend_config - .borrow_mut() - .replace(backend_config) - .is_none()) + assert!(tls_backend_config.borrow_mut().replace(backend_config).is_none()) }); CURRENT_MODULE .with(|current_module| assert!(current_module.borrow_mut().replace(jit_module).is_none())); @@ -148,15 +140,12 @@ extern "C" fn __clif_jit_fn(instance_ptr: *const Instance<'static>) -> *const u8 let name = tcx.symbol_name(instance).name.to_string(); let sig = crate::abi::get_function_sig(tcx, jit_module.isa().triple(), instance); - let func_id = jit_module - .declare_function(&name, Linkage::Export, &sig) - .unwrap(); + let func_id = jit_module.declare_function(&name, Linkage::Export, &sig).unwrap(); jit_module.prepare_for_function_redefine(func_id).unwrap(); let mut cx = crate::CodegenCx::new(tcx, backend_config, jit_module, false); - tcx.sess.time("codegen fn", || { - crate::base::codegen_fn(&mut cx, instance, Linkage::Export) - }); + tcx.sess + .time("codegen fn", || crate::base::codegen_fn(&mut cx, instance, Linkage::Export)); let (global_asm, _debug_context, unwind_context) = cx.finalize(); assert!(global_asm.is_empty()); @@ -185,9 +174,8 @@ fn load_imported_symbols_for_jit(tcx: TyCtxt<'_>) -> Vec<(String, *const u8)> { Linkage::NotLinked | Linkage::IncludedFromDylib => {} Linkage::Static => { let name = tcx.crate_name(cnum); - let mut err = tcx - .sess - .struct_err(&format!("Can't load static lib {}", name.as_str())); + let mut err = + tcx.sess.struct_err(&format!("Can't load static lib {}", name.as_str())); err.note("rustc_codegen_cranelift can only load dylibs in JIT mode."); err.emit(); } @@ -239,10 +227,7 @@ pub(super) fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'t let name = tcx.symbol_name(inst).name.to_string(); let sig = crate::abi::get_function_sig(tcx, cx.module.isa().triple(), inst); - let func_id = cx - .module - .declare_function(&name, Linkage::Export, &sig) - .unwrap(); + let func_id = cx.module.declare_function(&name, Linkage::Export, &sig).unwrap(); let instance_ptr = Box::into_raw(Box::new(inst)); @@ -263,28 +248,18 @@ pub(super) fn codegen_shim<'tcx>(cx: &mut CodegenCx<'_, 'tcx>, inst: Instance<'t let mut builder_ctx = FunctionBuilderContext::new(); let mut trampoline_builder = FunctionBuilder::new(&mut trampoline, &mut builder_ctx); - let jit_fn = cx - .module - .declare_func_in_func(jit_fn, trampoline_builder.func); + let jit_fn = cx.module.declare_func_in_func(jit_fn, trampoline_builder.func); let sig_ref = trampoline_builder.func.import_signature(sig); let entry_block = trampoline_builder.create_block(); trampoline_builder.append_block_params_for_function_params(entry_block); - let fn_args = trampoline_builder - .func - .dfg - .block_params(entry_block) - .to_vec(); + let fn_args = trampoline_builder.func.dfg.block_params(entry_block).to_vec(); trampoline_builder.switch_to_block(entry_block); - let instance_ptr = trampoline_builder - .ins() - .iconst(pointer_type, instance_ptr as u64 as i64); + let instance_ptr = trampoline_builder.ins().iconst(pointer_type, instance_ptr as u64 as i64); let jitted_fn = trampoline_builder.ins().call(jit_fn, &[instance_ptr]); let jitted_fn = trampoline_builder.func.dfg.inst_results(jitted_fn)[0]; - let call_inst = trampoline_builder - .ins() - .call_indirect(sig_ref, jitted_fn, &fn_args); + let call_inst = trampoline_builder.ins().call_indirect(sig_ref, jitted_fn, &fn_args); let ret_vals = trampoline_builder.func.dfg.inst_results(call_inst).to_vec(); trampoline_builder.ins().return_(&ret_vals); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 91cfde654f938..b994f28ffef5b 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -24,10 +24,8 @@ pub(crate) fn codegen_crate( match backend_config.codegen_mode { CodegenMode::Aot => aot::run_aot(tcx, backend_config, metadata, need_metadata_module), CodegenMode::Jit | CodegenMode::JitLazy => { - let is_executable = tcx - .sess - .crate_types() - .contains(&rustc_session::config::CrateType::Executable); + let is_executable = + tcx.sess.crate_types().contains(&rustc_session::config::CrateType::Executable); if !is_executable { tcx.sess.fatal("can't jit non-executable crate"); } @@ -36,8 +34,7 @@ pub(crate) fn codegen_crate( let _: ! = jit::run_jit(tcx, backend_config); #[cfg(not(feature = "jit"))] - tcx.sess - .fatal("jit support was disabled when compiling rustc_codegen_cranelift"); + tcx.sess.fatal("jit support was disabled when compiling rustc_codegen_cranelift"); } } } @@ -63,21 +60,12 @@ fn predefine_mono_items<'tcx>( } fn time(tcx: TyCtxt<'_>, name: &'static str, f: impl FnOnce() -> R) -> R { - if std::env::var("CG_CLIF_DISPLAY_CG_TIME") - .as_ref() - .map(|val| &**val) - == Ok("1") - { + if std::env::var("CG_CLIF_DISPLAY_CG_TIME").as_ref().map(|val| &**val) == Ok("1") { println!("[{:<30}: {}] start", tcx.crate_name(LOCAL_CRATE), name); let before = std::time::Instant::now(); let res = tcx.sess.time(name, f); let after = std::time::Instant::now(); - println!( - "[{:<30}: {}] end time: {:?}", - tcx.crate_name(LOCAL_CRATE), - name, - after - before - ); + println!("[{:<30}: {}] end time: {:?}", tcx.crate_name(LOCAL_CRATE), name, after - before); res } else { tcx.sess.time(name, f) diff --git a/src/inline_asm.rs b/src/inline_asm.rs index c8f027e7b1a5b..5b3df2bd38280 100644 --- a/src/inline_asm.rs +++ b/src/inline_asm.rs @@ -53,11 +53,7 @@ pub(crate) fn codegen_inline_asm<'tcx>( crate::base::codegen_operand(fx, value).load_scalar(fx), )); } - InlineAsmOperand::Out { - reg, - late: _, - place, - } => { + InlineAsmOperand::Out { reg, late: _, place } => { let reg = expect_reg(reg); clobbered_regs.push((reg, new_slot(reg.reg_class()))); if let Some(place) = place { @@ -68,12 +64,7 @@ pub(crate) fn codegen_inline_asm<'tcx>( )); } } - InlineAsmOperand::InOut { - reg, - late: _, - ref in_value, - out_place, - } => { + InlineAsmOperand::InOut { reg, late: _, ref in_value, out_place } => { let reg = expect_reg(reg); clobbered_regs.push((reg, new_slot(reg.reg_class()))); inputs.push(( @@ -97,11 +88,8 @@ pub(crate) fn codegen_inline_asm<'tcx>( let inline_asm_index = fx.inline_asm_index; fx.inline_asm_index += 1; - let asm_name = format!( - "{}__inline_asm_{}", - fx.tcx.symbol_name(fx.instance).name, - inline_asm_index - ); + let asm_name = + format!("{}__inline_asm_{}", fx.tcx.symbol_name(fx.instance).name, inline_asm_index); let generated_asm = generate_asm_wrapper( &asm_name, @@ -129,12 +117,7 @@ fn generate_asm_wrapper( let mut generated_asm = String::new(); writeln!(generated_asm, ".globl {}", asm_name).unwrap(); writeln!(generated_asm, ".type {},@function", asm_name).unwrap(); - writeln!( - generated_asm, - ".section .text.{},\"ax\",@progbits", - asm_name - ) - .unwrap(); + writeln!(generated_asm, ".section .text.{},\"ax\",@progbits", asm_name).unwrap(); writeln!(generated_asm, "{}:", asm_name).unwrap(); generated_asm.push_str(".intel_syntax noprefix\n"); @@ -164,11 +147,7 @@ fn generate_asm_wrapper( InlineAsmTemplatePiece::String(s) => { generated_asm.push_str(s); } - InlineAsmTemplatePiece::Placeholder { - operand_idx: _, - modifier: _, - span: _, - } => todo!(), + InlineAsmTemplatePiece::Placeholder { operand_idx: _, modifier: _, span: _ } => todo!(), } } generated_asm.push('\n'); @@ -230,17 +209,12 @@ fn call_inline_asm<'tcx>( }, ) .unwrap(); - let inline_asm_func = fx - .cx - .module - .declare_func_in_func(inline_asm_func, &mut fx.bcx.func); + let inline_asm_func = fx.cx.module.declare_func_in_func(inline_asm_func, &mut fx.bcx.func); #[cfg(debug_assertions)] fx.add_comment(inline_asm_func, asm_name); for (_reg, offset, value) in inputs { - fx.bcx - .ins() - .stack_store(value, stack_slot, i32::try_from(offset.bytes()).unwrap()); + fx.bcx.ins().stack_store(value, stack_slot, i32::try_from(offset.bytes()).unwrap()); } let stack_slot_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0); @@ -248,10 +222,7 @@ fn call_inline_asm<'tcx>( for (_reg, offset, place) in outputs { let ty = fx.clif_type(place.layout().ty).unwrap(); - let value = fx - .bcx - .ins() - .stack_load(ty, stack_slot, i32::try_from(offset.bytes()).unwrap()); + let value = fx.bcx.ins().stack_load(ty, stack_slot, i32::try_from(offset.bytes()).unwrap()); place.write_cvalue(fx, CValue::by_val(value, place.layout())); } } @@ -267,8 +238,7 @@ fn save_register(generated_asm: &mut String, arch: InlineAsmArch, reg: InlineAsm match arch { InlineAsmArch::X86_64 => { write!(generated_asm, " mov [rbp+0x{:x}], ", offset.bytes()).unwrap(); - reg.emit(generated_asm, InlineAsmArch::X86_64, None) - .unwrap(); + reg.emit(generated_asm, InlineAsmArch::X86_64, None).unwrap(); generated_asm.push('\n'); } _ => unimplemented!("save_register for {:?}", arch), @@ -284,8 +254,7 @@ fn restore_register( match arch { InlineAsmArch::X86_64 => { generated_asm.push_str(" mov "); - reg.emit(generated_asm, InlineAsmArch::X86_64, None) - .unwrap(); + reg.emit(generated_asm, InlineAsmArch::X86_64, None).unwrap(); writeln!(generated_asm, ", [rbp+0x{:x}]", offset.bytes()).unwrap(); } _ => unimplemented!("restore_register for {:?}", arch), diff --git a/src/intrinsics/cpuid.rs b/src/intrinsics/cpuid.rs index 4c82e793ad265..b27b0eddfbad6 100644 --- a/src/intrinsics/cpuid.rs +++ b/src/intrinsics/cpuid.rs @@ -31,54 +31,28 @@ pub(crate) fn codegen_cpuid_call<'tcx>( fx.bcx.switch_to_block(leaf_0); let max_basic_leaf = fx.bcx.ins().iconst(types::I32, 1); - let vend0 = fx - .bcx - .ins() - .iconst(types::I32, i64::from(u32::from_le_bytes(*b"Genu"))); - let vend2 = fx - .bcx - .ins() - .iconst(types::I32, i64::from(u32::from_le_bytes(*b"ineI"))); - let vend1 = fx - .bcx - .ins() - .iconst(types::I32, i64::from(u32::from_le_bytes(*b"ntel"))); - fx.bcx - .ins() - .jump(dest, &[max_basic_leaf, vend0, vend1, vend2]); + let vend0 = fx.bcx.ins().iconst(types::I32, i64::from(u32::from_le_bytes(*b"Genu"))); + let vend2 = fx.bcx.ins().iconst(types::I32, i64::from(u32::from_le_bytes(*b"ineI"))); + let vend1 = fx.bcx.ins().iconst(types::I32, i64::from(u32::from_le_bytes(*b"ntel"))); + fx.bcx.ins().jump(dest, &[max_basic_leaf, vend0, vend1, vend2]); fx.bcx.switch_to_block(leaf_1); let cpu_signature = fx.bcx.ins().iconst(types::I32, 0); let additional_information = fx.bcx.ins().iconst(types::I32, 0); let ecx_features = fx.bcx.ins().iconst(types::I32, 0); - let edx_features = fx - .bcx - .ins() - .iconst(types::I32, 1 << 25 /* sse */ | 1 << 26 /* sse2 */); - fx.bcx.ins().jump( - dest, - &[ - cpu_signature, - additional_information, - ecx_features, - edx_features, - ], - ); + let edx_features = fx.bcx.ins().iconst(types::I32, 1 << 25 /* sse */ | 1 << 26 /* sse2 */); + fx.bcx.ins().jump(dest, &[cpu_signature, additional_information, ecx_features, edx_features]); fx.bcx.switch_to_block(leaf_8000_0000); let extended_max_basic_leaf = fx.bcx.ins().iconst(types::I32, 0); let zero = fx.bcx.ins().iconst(types::I32, 0); - fx.bcx - .ins() - .jump(dest, &[extended_max_basic_leaf, zero, zero, zero]); + fx.bcx.ins().jump(dest, &[extended_max_basic_leaf, zero, zero, zero]); fx.bcx.switch_to_block(leaf_8000_0001); let zero = fx.bcx.ins().iconst(types::I32, 0); let proc_info_ecx = fx.bcx.ins().iconst(types::I32, 0); let proc_info_edx = fx.bcx.ins().iconst(types::I32, 0); - fx.bcx - .ins() - .jump(dest, &[zero, zero, proc_info_ecx, proc_info_edx]); + fx.bcx.ins().jump(dest, &[zero, zero, proc_info_ecx, proc_info_edx]); fx.bcx.switch_to_block(unsupported_leaf); crate::trap::trap_unreachable( diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index f0ca7400d5031..39e047a98f9eb 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -227,9 +227,8 @@ fn simd_reduce<'tcx>( let mut res_val = val.value_field(fx, mir::Field::new(0)).load_scalar(fx); for lane_idx in 1..lane_count { - let lane = val - .value_field(fx, mir::Field::new(lane_idx.try_into().unwrap())) - .load_scalar(fx); + let lane = + val.value_field(fx, mir::Field::new(lane_idx.try_into().unwrap())).load_scalar(fx); res_val = f(fx, lane_layout, res_val, lane); } let res = CValue::by_val(res_val, lane_layout); @@ -248,9 +247,8 @@ fn simd_reduce_bool<'tcx>( let res_val = val.value_field(fx, mir::Field::new(0)).load_scalar(fx); let mut res_val = fx.bcx.ins().band_imm(res_val, 1); // mask to boolean for lane_idx in 1..lane_count { - let lane = val - .value_field(fx, mir::Field::new(lane_idx.try_into().unwrap())) - .load_scalar(fx); + let lane = + val.value_field(fx, mir::Field::new(lane_idx.try_into().unwrap())).load_scalar(fx); let lane = fx.bcx.ins().band_imm(lane, 1); // mask to boolean res_val = f(fx, res_val, lane); } diff --git a/src/lib.rs b/src/lib.rs index 81e34632f8f31..e1927ad3a6920 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -149,11 +149,8 @@ impl<'m, 'tcx> CodegenCx<'m, 'tcx> { module.isa(), matches!(backend_config.codegen_mode, CodegenMode::Aot), ); - let debug_context = if debug_info { - Some(DebugContext::new(tcx, module.isa())) - } else { - None - }; + let debug_context = + if debug_info { Some(DebugContext::new(tcx, module.isa())) } else { None }; CodegenCx { tcx, module, @@ -303,14 +300,7 @@ fn build_isa(sess: &Session) -> Box { flags_builder.enable("is_pic").unwrap(); flags_builder.set("enable_probestack", "false").unwrap(); // __cranelift_probestack is not provided flags_builder - .set( - "enable_verifier", - if cfg!(debug_assertions) { - "true" - } else { - "false" - }, - ) + .set("enable_verifier", if cfg!(debug_assertions) { "true" } else { "false" }) .unwrap(); let tls_model = match target_triple.binary_format { diff --git a/src/main_shim.rs b/src/main_shim.rs index 4ea9b9f995303..62e551b186ff7 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -26,13 +26,7 @@ pub(crate) fn maybe_create_entry_wrapper( return; } - create_entry_fn( - tcx, - module, - unwind_context, - main_def_id, - use_start_lang_item, - ); + create_entry_fn(tcx, module, unwind_context, main_def_id, use_start_lang_item); fn create_entry_fn( tcx: TyCtxt<'_>, @@ -54,23 +48,17 @@ pub(crate) fn maybe_create_entry_wrapper( AbiParam::new(m.target_config().pointer_type()), AbiParam::new(m.target_config().pointer_type()), ], - returns: vec![AbiParam::new( - m.target_config().pointer_type(), /*isize*/ - )], + returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)], call_conv: CallConv::triple_default(m.isa().triple()), }; - let cmain_func_id = m - .declare_function("main", Linkage::Export, &cmain_sig) - .unwrap(); + let cmain_func_id = m.declare_function("main", Linkage::Export, &cmain_sig).unwrap(); let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx); let main_name = tcx.symbol_name(instance).name.to_string(); let main_sig = get_function_sig(tcx, m.isa().triple(), instance); - let main_func_id = m - .declare_function(&main_name, Linkage::Import, &main_sig) - .unwrap(); + let main_func_id = m.declare_function(&main_name, Linkage::Import, &main_sig).unwrap(); let mut ctx = Context::new(); ctx.func = Function::with_name_signature(ExternalName::user(0, 0), cmain_sig); @@ -98,9 +86,7 @@ pub(crate) fn maybe_create_entry_wrapper( .polymorphize(tcx); let start_func_id = import_function(tcx, m, start_instance); - let main_val = bcx - .ins() - .func_addr(m.target_config().pointer_type(), main_func_ref); + let main_val = bcx.ins().func_addr(m.target_config().pointer_type(), main_func_ref); let func_ref = m.declare_func_in_func(start_func_id, &mut bcx.func); bcx.ins().call(func_ref, &[main_val, arg_argc, arg_argv]) diff --git a/src/metadata.rs b/src/metadata.rs index 2e3b9fb8364e4..190c4f45ccafd 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -94,9 +94,7 @@ pub(crate) fn write_metadata( assert!(kind == MetadataKind::Compressed); let mut compressed = tcx.metadata_encoding_version(); - FrameEncoder::new(&mut compressed) - .write_all(&metadata.raw_data) - .unwrap(); + FrameEncoder::new(&mut compressed).write_all(&metadata.raw_data).unwrap(); product.add_rustc_section( rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx), diff --git a/src/num.rs b/src/num.rs index 4fc0cdfe0e79d..da49e1c6c91db 100644 --- a/src/num.rs +++ b/src/num.rs @@ -93,12 +93,7 @@ pub(crate) fn codegen_binop<'tcx>( ty::Uint(_) | ty::Int(_) => crate::num::codegen_int_binop(fx, bin_op, in_lhs, in_rhs), ty::Float(_) => crate::num::codegen_float_binop(fx, bin_op, in_lhs, in_rhs), ty::RawPtr(..) | ty::FnPtr(..) => crate::num::codegen_ptr_binop(fx, bin_op, in_lhs, in_rhs), - _ => unreachable!( - "{:?}({:?}, {:?})", - bin_op, - in_lhs.layout().ty, - in_rhs.layout().ty - ), + _ => unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs.layout().ty, in_rhs.layout().ty), } } @@ -185,12 +180,7 @@ pub(crate) fn codegen_int_binop<'tcx>( } } // Compare binops handles by `codegen_binop`. - _ => unreachable!( - "{:?}({:?}, {:?})", - bin_op, - in_lhs.layout().ty, - in_rhs.layout().ty - ), + _ => unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs.layout().ty, in_rhs.layout().ty), }; CValue::by_val(val, in_lhs.layout()) @@ -268,9 +258,7 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( let rhs = fx.bcx.ins().sextend(ty.double_width().unwrap(), rhs); let val = fx.bcx.ins().imul(lhs, rhs); let has_underflow = - fx.bcx - .ins() - .icmp_imm(IntCC::SignedLessThan, val, -(1 << (ty.bits() - 1))); + fx.bcx.ins().icmp_imm(IntCC::SignedLessThan, val, -(1 << (ty.bits() - 1))); let has_overflow = fx.bcx.ins().icmp_imm( IntCC::SignedGreaterThan, val, @@ -309,10 +297,7 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( let val = fx.bcx.ins().ishl(lhs, actual_shift); let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; - let has_overflow = fx - .bcx - .ins() - .icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift); + let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift); (val, has_overflow) } BinOp::Shr => { @@ -326,26 +311,15 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( }; let ty = fx.bcx.func.dfg.value_type(val); let max_shift = i64::from(ty.bits()) - 1; - let has_overflow = fx - .bcx - .ins() - .icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift); + let has_overflow = fx.bcx.ins().icmp_imm(IntCC::UnsignedGreaterThan, rhs, max_shift); (val, has_overflow) } - _ => bug!( - "binop {:?} on checked int/uint lhs: {:?} rhs: {:?}", - bin_op, - in_lhs, - in_rhs - ), + _ => bug!("binop {:?} on checked int/uint lhs: {:?} rhs: {:?}", bin_op, in_lhs, in_rhs), }; let has_overflow = fx.bcx.ins().bint(types::I8, has_overflow); - let out_layout = fx.layout_of( - fx.tcx - .mk_tup([in_lhs.layout().ty, fx.tcx.types.bool].iter()), - ); + let out_layout = fx.layout_of(fx.tcx.mk_tup([in_lhs.layout().ty, fx.tcx.types.bool].iter())); CValue::by_val_pair(res, has_overflow, out_layout) } @@ -445,9 +419,7 @@ pub(crate) fn codegen_ptr_binop<'tcx>( let ptr_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_ptr, rhs_ptr); let ptr_cmp = - fx.bcx - .ins() - .icmp(bin_op_to_intcc(bin_op, false).unwrap(), lhs_ptr, rhs_ptr); + fx.bcx.ins().icmp(bin_op_to_intcc(bin_op, false).unwrap(), lhs_ptr, rhs_ptr); let extra_cmp = fx.bcx.ins().icmp( bin_op_to_intcc(bin_op, false).unwrap(), lhs_extra, @@ -459,9 +431,6 @@ pub(crate) fn codegen_ptr_binop<'tcx>( _ => panic!("bin_op {:?} on ptr", bin_op), }; - CValue::by_val( - fx.bcx.ins().bint(types::I8, res), - fx.layout_of(fx.tcx.types.bool), - ) + CValue::by_val(fx.bcx.ins().bint(types::I8, res), fx.layout_of(fx.tcx.types.bool)) } } diff --git a/src/optimize/code_layout.rs b/src/optimize/code_layout.rs index f02732014d1e3..ca9ff15ec10ff 100644 --- a/src/optimize/code_layout.rs +++ b/src/optimize/code_layout.rs @@ -15,10 +15,7 @@ pub(super) fn optimize_function(ctx: &mut Context, cold_blocks: &EntitySet>(); for &inst in &insts { ctx.func.layout.remove_inst(inst); @@ -28,10 +25,7 @@ pub(super) fn optimize_function(ctx: &mut Context, cold_blocks: &EntitySet, arg: Value) -> Value { if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) { match bcx.func.dfg[arg_inst] { - InstructionData::Unary { - opcode: Opcode::Bint, - arg, - } => arg, + InstructionData::Unary { opcode: Opcode::Bint, arg } => arg, _ => arg, } } else { @@ -54,12 +51,7 @@ pub(crate) fn make_branchable_value(bcx: &mut FunctionBuilder<'_>, arg: Value) - match bcx.func.dfg[arg_inst] { // This is the lowering of Rvalue::Not - InstructionData::Load { - opcode: Opcode::Load, - arg: ptr, - flags, - offset, - } => { + InstructionData::Load { opcode: Opcode::Load, arg: ptr, flags, offset } => { // Using `load.i8 + uextend.i32` would legalize to `uload8 + ireduce.i8 + // uextend.i32`. Just `uload8` is much faster. match bcx.func.dfg.ctrl_typevar(arg_inst) { @@ -95,20 +87,14 @@ pub(crate) fn maybe_known_branch_taken( }; match bcx.func.dfg[arg_inst] { - InstructionData::UnaryBool { - opcode: Opcode::Bconst, - imm, - } => { + InstructionData::UnaryBool { opcode: Opcode::Bconst, imm } => { if test_zero { Some(!imm) } else { Some(imm) } } - InstructionData::UnaryImm { - opcode: Opcode::Iconst, - imm, - } => { + InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => { if test_zero { Some(imm.bits() == 0) } else { diff --git a/src/optimize/stack2reg.rs b/src/optimize/stack2reg.rs index 3c939d5a58639..d111f37f5e455 100644 --- a/src/optimize/stack2reg.rs +++ b/src/optimize/stack2reg.rs @@ -175,16 +175,14 @@ impl<'a> OptimizeContext<'a> { } } - OptimizeContext { - ctx, - stack_slot_usage_map, - } + OptimizeContext { ctx, stack_slot_usage_map } } } pub(super) fn optimize_function( ctx: &mut Context, - #[cfg_attr(not(debug_assertions), allow(unused_variables))] clif_comments: &mut crate::pretty_clif::CommentWriter, + #[cfg_attr(not(debug_assertions), allow(unused_variables))] + clif_comments: &mut crate::pretty_clif::CommentWriter, ) { combine_stack_addr_with_load_store(&mut ctx.func); @@ -296,12 +294,7 @@ fn combine_stack_addr_with_load_store(func: &mut Function) { while let Some(_block) = cursor.next_block() { while let Some(inst) = cursor.next_inst() { match cursor.func.dfg[inst] { - InstructionData::Load { - opcode: Opcode::Load, - arg: addr, - flags: _, - offset, - } => { + InstructionData::Load { opcode: Opcode::Load, arg: addr, flags: _, offset } => { if cursor.func.dfg.ctrl_typevar(inst) == types::I128 || cursor.func.dfg.ctrl_typevar(inst).is_vector() { @@ -391,20 +384,14 @@ fn remove_unused_stack_addr_and_stack_load(opt_ctx: &mut OptimizeContext<'_>) { stack_slot_users .stack_addr .drain_filter(|inst| { - stack_addr_load_insts_users - .get(inst) - .map(|users| users.is_empty()) - .unwrap_or(true) + stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true) }) .for_each(|inst| StackSlotUsage::remove_unused_stack_addr(&mut func, inst)); stack_slot_users .stack_load .drain_filter(|inst| { - stack_addr_load_insts_users - .get(inst) - .map(|users| users.is_empty()) - .unwrap_or(true) + stack_addr_load_insts_users.get(inst).map(|users| users.is_empty()).unwrap_or(true) }) .for_each(|inst| StackSlotUsage::remove_unused_load(&mut func, inst)); } @@ -415,11 +402,8 @@ fn try_get_stack_slot_and_offset_for_addr( addr: Value, ) -> Option<(StackSlot, Offset32)> { if let ValueDef::Result(addr_inst, 0) = func.dfg.value_def(addr) { - if let InstructionData::StackLoad { - opcode: Opcode::StackAddr, - stack_slot, - offset, - } = func.dfg[addr_inst] + if let InstructionData::StackLoad { opcode: Opcode::StackAddr, stack_slot, offset } = + func.dfg[addr_inst] { return Some((stack_slot, offset)); } @@ -437,16 +421,8 @@ enum SpatialOverlap { fn spatial_overlap(func: &Function, src: Inst, dest: Inst) -> SpatialOverlap { fn inst_info(func: &Function, inst: Inst) -> (StackSlot, Offset32, u32) { match func.dfg[inst] { - InstructionData::StackLoad { - opcode: Opcode::StackAddr, - stack_slot, - offset, - } - | InstructionData::StackLoad { - opcode: Opcode::StackLoad, - stack_slot, - offset, - } + InstructionData::StackLoad { opcode: Opcode::StackAddr, stack_slot, offset } + | InstructionData::StackLoad { opcode: Opcode::StackLoad, stack_slot, offset } | InstructionData::StackStore { opcode: Opcode::StackStore, stack_slot, @@ -471,10 +447,7 @@ fn spatial_overlap(func: &Function, src: Inst, dest: Inst) -> SpatialOverlap { } let src_end: i64 = src_offset.try_add_i64(i64::from(src_size)).unwrap().into(); - let dest_end: i64 = dest_offset - .try_add_i64(i64::from(dest_size)) - .unwrap() - .into(); + let dest_end: i64 = dest_offset.try_add_i64(i64::from(dest_size)).unwrap().into(); if src_end <= dest_offset.into() || dest_end <= src_offset.into() { return SpatialOverlap::No; } diff --git a/src/pointer.rs b/src/pointer.rs index 03b1e6cc4acee..88a78f3214d87 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -23,32 +23,20 @@ pub(crate) enum PointerBase { impl Pointer { pub(crate) fn new(addr: Value) -> Self { - Pointer { - base: PointerBase::Addr(addr), - offset: Offset32::new(0), - } + Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) } } pub(crate) fn stack_slot(stack_slot: StackSlot) -> Self { - Pointer { - base: PointerBase::Stack(stack_slot), - offset: Offset32::new(0), - } + Pointer { base: PointerBase::Stack(stack_slot), offset: Offset32::new(0) } } pub(crate) fn const_addr(fx: &mut FunctionCx<'_, '_, '_>, addr: i64) -> Self { let addr = fx.bcx.ins().iconst(fx.pointer_type, addr); - Pointer { - base: PointerBase::Addr(addr), - offset: Offset32::new(0), - } + Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) } } pub(crate) fn dangling(align: Align) -> Self { - Pointer { - base: PointerBase::Dangling(align), - offset: Offset32::new(0), - } + Pointer { base: PointerBase::Dangling(align), offset: Offset32::new(0) } } #[cfg(debug_assertions)] @@ -60,21 +48,14 @@ impl Pointer { match self.base { PointerBase::Addr(base_addr) => { let offset: i64 = self.offset.into(); - if offset == 0 { - base_addr - } else { - fx.bcx.ins().iadd_imm(base_addr, offset) - } + if offset == 0 { base_addr } else { fx.bcx.ins().iadd_imm(base_addr, offset) } } PointerBase::Stack(stack_slot) => { - fx.bcx - .ins() - .stack_addr(fx.pointer_type, stack_slot, self.offset) + fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset) + } + PointerBase::Dangling(align) => { + fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()) } - PointerBase::Dangling(align) => fx - .bcx - .ins() - .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()), } } @@ -84,10 +65,7 @@ impl Pointer { pub(crate) fn offset_i64(self, fx: &mut FunctionCx<'_, '_, '_>, extra_offset: i64) -> Self { if let Some(new_offset) = self.offset.try_add_i64(extra_offset) { - Pointer { - base: self.base, - offset: new_offset, - } + Pointer { base: self.base, offset: new_offset } } else { let base_offset: i64 = self.offset.into(); if let Some(new_offset) = base_offset.checked_add(extra_offset) { @@ -96,16 +74,12 @@ impl Pointer { PointerBase::Stack(stack_slot) => { fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0) } - PointerBase::Dangling(align) => fx - .bcx - .ins() - .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()), + PointerBase::Dangling(align) => { + fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()) + } }; let addr = fx.bcx.ins().iadd_imm(base_addr, new_offset); - Pointer { - base: PointerBase::Addr(addr), - offset: Offset32::new(0), - } + Pointer { base: PointerBase::Addr(addr), offset: Offset32::new(0) } } else { panic!( "self.offset ({}) + extra_offset ({}) not representable in i64", @@ -122,20 +96,15 @@ impl Pointer { offset: self.offset, }, PointerBase::Stack(stack_slot) => { - let base_addr = fx - .bcx - .ins() - .stack_addr(fx.pointer_type, stack_slot, self.offset); + let base_addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, self.offset); Pointer { base: PointerBase::Addr(fx.bcx.ins().iadd(base_addr, extra_offset)), offset: Offset32::new(0), } } PointerBase::Dangling(align) => { - let addr = fx - .bcx - .ins() - .iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()); + let addr = + fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(align.bytes()).unwrap()); Pointer { base: PointerBase::Addr(fx.bcx.ins().iadd(addr, extra_offset)), offset: self.offset, diff --git a/src/pretty_clif.rs b/src/pretty_clif.rs index a5ae7fa829d56..9c91b92e515b1 100644 --- a/src/pretty_clif.rs +++ b/src/pretty_clif.rs @@ -79,20 +79,14 @@ impl CommentWriter { vec![ format!("symbol {}", tcx.symbol_name(instance).name), format!("instance {:?}", instance), - format!( - "abi {:?}", - FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[]) - ), + format!("abi {:?}", FnAbi::of_instance(&RevealAllLayoutCx(tcx), instance, &[])), String::new(), ] } else { vec![] }; - CommentWriter { - global_comments, - entity_comments: FxHashMap::default(), - } + CommentWriter { global_comments, entity_comments: FxHashMap::default() } } } @@ -201,10 +195,7 @@ impl FunctionCx<'_, '_, '_> { } pub(crate) fn should_write_ir(tcx: TyCtxt<'_>) -> bool { - tcx.sess - .opts - .output_types - .contains_key(&OutputType::LlvmAssembly) + tcx.sess.opts.output_types.contains_key(&OutputType::LlvmAssembly) } pub(crate) fn write_ir_file<'tcx>( @@ -243,37 +234,30 @@ pub(crate) fn write_clif_file<'tcx>( context: &cranelift_codegen::Context, mut clif_comments: &CommentWriter, ) { - write_ir_file( - tcx, - &format!("{}.{}.clif", tcx.symbol_name(instance).name, postfix), - |file| { - let value_ranges = isa.map(|isa| { - context - .build_value_labels_ranges(isa) - .expect("value location ranges") - }); + write_ir_file(tcx, &format!("{}.{}.clif", tcx.symbol_name(instance).name, postfix), |file| { + let value_ranges = + isa.map(|isa| context.build_value_labels_ranges(isa).expect("value location ranges")); - let mut clif = String::new(); - cranelift_codegen::write::decorate_function( - &mut clif_comments, - &mut clif, - &context.func, - &DisplayFunctionAnnotations { - isa: Some(&*crate::build_isa(tcx.sess)), - value_ranges: value_ranges.as_ref(), - }, - ) - .unwrap(); + let mut clif = String::new(); + cranelift_codegen::write::decorate_function( + &mut clif_comments, + &mut clif, + &context.func, + &DisplayFunctionAnnotations { + isa: Some(&*crate::build_isa(tcx.sess)), + value_ranges: value_ranges.as_ref(), + }, + ) + .unwrap(); - writeln!(file, "test compile")?; - writeln!(file, "set is_pic")?; - writeln!(file, "set enable_simd")?; - writeln!(file, "target {} haswell", crate::target_triple(tcx.sess))?; - writeln!(file)?; - file.write_all(clif.as_bytes())?; - Ok(()) - }, - ); + writeln!(file, "test compile")?; + writeln!(file, "set is_pic")?; + writeln!(file, "set enable_simd")?; + writeln!(file, "target {} haswell", crate::target_triple(tcx.sess))?; + writeln!(file)?; + file.write_all(clif.as_bytes())?; + Ok(()) + }); } impl fmt::Debug for FunctionCx<'_, '_, '_> { diff --git a/src/toolchain.rs b/src/toolchain.rs index 735c59d70c120..484a9b699a0aa 100644 --- a/src/toolchain.rs +++ b/src/toolchain.rs @@ -71,12 +71,9 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { flavor, )), (Some(linker), None) => { - let stem = linker - .file_stem() - .and_then(|stem| stem.to_str()) - .unwrap_or_else(|| { - sess.fatal("couldn't extract file stem from specified linker") - }); + let stem = linker.file_stem().and_then(|stem| stem.to_str()).unwrap_or_else(|| { + sess.fatal("couldn't extract file stem from specified linker") + }); let flavor = if stem == "emcc" { LinkerFlavor::Em @@ -105,11 +102,7 @@ fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { // linker and linker flavor specified via command line have precedence over what the target // specification specifies - if let Some(ret) = infer_from( - sess, - sess.opts.cg.linker.clone(), - sess.opts.cg.linker_flavor, - ) { + if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), sess.opts.cg.linker_flavor) { return ret; } diff --git a/src/unsize.rs b/src/unsize.rs index 109aee5a04411..042583cd5720f 100644 --- a/src/unsize.rs +++ b/src/unsize.rs @@ -19,13 +19,12 @@ pub(crate) fn unsized_info<'tcx>( old_info: Option, ) -> Value { let (source, target) = - fx.tcx - .struct_lockstep_tails_erasing_lifetimes(source, target, ParamEnv::reveal_all()); + fx.tcx.struct_lockstep_tails_erasing_lifetimes(source, target, ParamEnv::reveal_all()); match (&source.kind(), &target.kind()) { - (&ty::Array(_, len), &ty::Slice(_)) => fx.bcx.ins().iconst( - fx.pointer_type, - len.eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64, - ), + (&ty::Array(_, len), &ty::Slice(_)) => fx + .bcx + .ins() + .iconst(fx.pointer_type, len.eval_usize(fx.tcx, ParamEnv::reveal_all()) as i64), (&ty::Dynamic(..), &ty::Dynamic(..)) => { // For now, upcasts are limited to changes in marker // traits, and hence never actually require an actual @@ -35,11 +34,7 @@ pub(crate) fn unsized_info<'tcx>( (_, &ty::Dynamic(ref data, ..)) => { crate::vtable::get_vtable(fx, fx.layout_of(source), data.principal()) } - _ => bug!( - "unsized_info: invalid unsizing {:?} -> {:?}", - source, - target - ), + _ => bug!("unsized_info: invalid unsizing {:?} -> {:?}", source, target), } } @@ -96,17 +91,15 @@ pub(crate) fn coerce_unsized_into<'tcx>( let src_ty = src.layout().ty; let dst_ty = dst.layout().ty; let mut coerce_ptr = || { - let (base, info) = if fx - .layout_of(src.layout().ty.builtin_deref(true).unwrap().ty) - .is_unsized() - { - // fat-ptr to fat-ptr unsize preserves the vtable - // i.e., &'a fmt::Debug+Send => &'a fmt::Debug - src.load_scalar_pair(fx) - } else { - let base = src.load_scalar(fx); - unsize_thin_ptr(fx, base, src.layout(), dst.layout()) - }; + let (base, info) = + if fx.layout_of(src.layout().ty.builtin_deref(true).unwrap().ty).is_unsized() { + // fat-ptr to fat-ptr unsize preserves the vtable + // i.e., &'a fmt::Debug+Send => &'a fmt::Debug + src.load_scalar_pair(fx) + } else { + let base = src.load_scalar(fx); + unsize_thin_ptr(fx, base, src.layout(), dst.layout()) + }; dst.write_cvalue(fx, CValue::by_val_pair(base, info, dst.layout())); }; match (&src_ty.kind(), &dst_ty.kind()) { @@ -131,11 +124,7 @@ pub(crate) fn coerce_unsized_into<'tcx>( } } } - _ => bug!( - "coerce_unsized_into: invalid coercion {:?} -> {:?}", - src_ty, - dst_ty - ), + _ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}", src_ty, dst_ty), } } @@ -147,23 +136,14 @@ pub(crate) fn size_and_align_of_dst<'tcx>( info: Value, ) -> (Value, Value) { if !layout.is_unsized() { - let size = fx - .bcx - .ins() - .iconst(fx.pointer_type, layout.size.bytes() as i64); - let align = fx - .bcx - .ins() - .iconst(fx.pointer_type, layout.align.abi.bytes() as i64); + let size = fx.bcx.ins().iconst(fx.pointer_type, layout.size.bytes() as i64); + let align = fx.bcx.ins().iconst(fx.pointer_type, layout.align.abi.bytes() as i64); return (size, align); } match layout.ty.kind() { ty::Dynamic(..) => { // load size/align from vtable - ( - crate::vtable::size_of_obj(fx, info), - crate::vtable::min_align_of_obj(fx, info), - ) + (crate::vtable::size_of_obj(fx, info), crate::vtable::min_align_of_obj(fx, info)) } ty::Slice(_) | ty::Str => { let unit = layout.field(fx, 0); @@ -171,9 +151,7 @@ pub(crate) fn size_and_align_of_dst<'tcx>( // times the unit size. ( fx.bcx.ins().imul_imm(info, unit.size.bytes() as i64), - fx.bcx - .ins() - .iconst(fx.pointer_type, unit.align.abi.bytes() as i64), + fx.bcx.ins().iconst(fx.pointer_type, unit.align.abi.bytes() as i64), ) } _ => { @@ -211,10 +189,7 @@ pub(crate) fn size_and_align_of_dst<'tcx>( // Choose max of two known alignments (combined value must // be aligned according to more restrictive of the two). - let cmp = fx - .bcx - .ins() - .icmp(IntCC::UnsignedGreaterThan, sized_align, unsized_align); + let cmp = fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, sized_align, unsized_align); let align = fx.bcx.ins().select(cmp, sized_align, unsized_align); // Issue #27023: must add any necessary padding to `size` diff --git a/src/value_and_place.rs b/src/value_and_place.rs index 0dad9b2edafdf..eaaf71ff20f8d 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -16,10 +16,7 @@ fn codegen_field<'tcx>( let field_layout = layout.field(&*fx, field.index()); let simple = |fx: &mut FunctionCx<'_, '_, '_>| { - ( - base.offset_i64(fx, i64::try_from(field_offset.bytes()).unwrap()), - field_layout, - ) + (base.offset_i64(fx, i64::try_from(field_offset.bytes()).unwrap()), field_layout) }; if let Some(extra) = extra { @@ -58,10 +55,7 @@ fn scalar_pair_calculate_b_offset( a_scalar: &Scalar, b_scalar: &Scalar, ) -> Offset32 { - let b_offset = a_scalar - .value - .size(&tcx) - .align_to(b_scalar.value.align(&tcx).abi); + let b_offset = a_scalar.value.size(&tcx).align_to(b_scalar.value.align(&tcx).abi); Offset32::new(b_offset.bytes().try_into().unwrap()) } @@ -223,13 +217,7 @@ impl<'tcx> CValue<'tcx> { layout: TyAndLayout<'tcx>, const_val: ty::ScalarInt, ) -> CValue<'tcx> { - assert_eq!( - const_val.size(), - layout.size, - "{:#?}: {:?}", - const_val, - layout - ); + assert_eq!(const_val.size(), layout.size, "{:#?}: {:?}", const_val, layout); use cranelift_codegen::ir::immediates::{Ieee32, Ieee64}; let clif_ty = fx.clif_type(layout.ty).unwrap(); @@ -246,18 +234,11 @@ impl<'tcx> CValue<'tcx> { ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => { let const_val = const_val.to_bits(layout.size).unwrap(); let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64); - let msb = fx - .bcx - .ins() - .iconst(types::I64, (const_val >> 64) as u64 as i64); + let msb = fx.bcx.ins().iconst(types::I64, (const_val >> 64) as u64 as i64); fx.bcx.ins().iconcat(lsb, msb) } - ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Ref(..) - | ty::RawPtr(..) => { - fx - .bcx - .ins() - .iconst(clif_ty, const_val.to_bits(layout.size).unwrap() as i64) + ty::Bool | ty::Char | ty::Uint(_) | ty::Int(_) | ty::Ref(..) | ty::RawPtr(..) => { + fx.bcx.ins().iconst(clif_ty, const_val.to_bits(layout.size).unwrap() as i64) } ty::Float(FloatTy::F32) => { fx.bcx.ins().f32const(Ieee32::with_bits(u32::try_from(const_val).unwrap())) @@ -275,14 +256,8 @@ impl<'tcx> CValue<'tcx> { } pub(crate) fn cast_pointer_to(self, layout: TyAndLayout<'tcx>) -> Self { - assert!(matches!( - self.layout().ty.kind(), - ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) - )); - assert!(matches!( - layout.ty.kind(), - ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) - )); + assert!(matches!(self.layout().ty.kind(), ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..))); + assert!(matches!(layout.ty.kind(), ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..))); assert_eq!(self.layout().abi, layout.abi); CValue(self.0, layout) } @@ -313,10 +288,7 @@ impl<'tcx> CPlace<'tcx> { } pub(crate) fn no_place(layout: TyAndLayout<'tcx>) -> CPlace<'tcx> { - CPlace { - inner: CPlaceInner::Addr(Pointer::dangling(layout.align.pref), None), - layout, - } + CPlace { inner: CPlaceInner::Addr(Pointer::dangling(layout.align.pref), None), layout } } pub(crate) fn new_stack_slot( @@ -335,10 +307,7 @@ impl<'tcx> CPlace<'tcx> { size: (u32::try_from(layout.size.bytes()).unwrap() + 15) / 16 * 16, offset: None, }); - CPlace { - inner: CPlaceInner::Addr(Pointer::stack_slot(stack_slot), None), - layout, - } + CPlace { inner: CPlaceInner::Addr(Pointer::stack_slot(stack_slot), None), layout } } pub(crate) fn new_var( @@ -349,10 +318,7 @@ impl<'tcx> CPlace<'tcx> { let var = Variable::with_u32(fx.next_ssa_var); fx.next_ssa_var += 1; fx.bcx.declare_var(var, fx.clif_type(layout.ty).unwrap()); - CPlace { - inner: CPlaceInner::Var(local, var), - layout, - } + CPlace { inner: CPlaceInner::Var(local, var), layout } } pub(crate) fn new_var_pair( @@ -368,17 +334,11 @@ impl<'tcx> CPlace<'tcx> { let (ty1, ty2) = fx.clif_pair_type(layout.ty).unwrap(); fx.bcx.declare_var(var1, ty1); fx.bcx.declare_var(var2, ty2); - CPlace { - inner: CPlaceInner::VarPair(local, var1, var2), - layout, - } + CPlace { inner: CPlaceInner::VarPair(local, var1, var2), layout } } pub(crate) fn for_ptr(ptr: Pointer, layout: TyAndLayout<'tcx>) -> CPlace<'tcx> { - CPlace { - inner: CPlaceInner::Addr(ptr, None), - layout, - } + CPlace { inner: CPlaceInner::Addr(ptr, None), layout } } pub(crate) fn for_ptr_with_extra( @@ -386,10 +346,7 @@ impl<'tcx> CPlace<'tcx> { extra: Value, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { - CPlace { - inner: CPlaceInner::Addr(ptr, Some(extra)), - layout, - } + CPlace { inner: CPlaceInner::Addr(ptr, Some(extra)), layout } } pub(crate) fn to_cvalue(self, fx: &mut FunctionCx<'_, '_, 'tcx>) -> CValue<'tcx> { @@ -397,23 +354,19 @@ impl<'tcx> CPlace<'tcx> { match self.inner { CPlaceInner::Var(_local, var) => { let val = fx.bcx.use_var(var); - fx.bcx - .set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); + fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); CValue::by_val(val, layout) } CPlaceInner::VarPair(_local, var1, var2) => { let val1 = fx.bcx.use_var(var1); - fx.bcx - .set_val_label(val1, cranelift_codegen::ir::ValueLabel::new(var1.index())); + fx.bcx.set_val_label(val1, cranelift_codegen::ir::ValueLabel::new(var1.index())); let val2 = fx.bcx.use_var(var2); - fx.bcx - .set_val_label(val2, cranelift_codegen::ir::ValueLabel::new(var2.index())); + fx.bcx.set_val_label(val2, cranelift_codegen::ir::ValueLabel::new(var2.index())); CValue::by_val_pair(val1, val2, layout) } CPlaceInner::VarLane(_local, var, lane) => { let val = fx.bcx.use_var(var); - fx.bcx - .set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); + fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); let val = fx.bcx.ins().extractlane(val, lane); CValue::by_val(val, layout) } @@ -503,8 +456,7 @@ impl<'tcx> CPlace<'tcx> { } _ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty, dst_ty), }; - fx.bcx - .set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index())); + fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index())); fx.bcx.def_var(var, data); } @@ -550,15 +502,13 @@ impl<'tcx> CPlace<'tcx> { // First get the old vector let vector = fx.bcx.use_var(var); - fx.bcx - .set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); + fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); // Next insert the written lane into the vector let vector = fx.bcx.ins().insertlane(vector, data, lane); // Finally write the new vector - fx.bcx - .set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); + fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); fx.bcx.def_var(var, vector); return; @@ -596,10 +546,7 @@ impl<'tcx> CPlace<'tcx> { to_ptr.store(fx, val, flags); } CValueInner::ByValPair(_, _) => { - bug!( - "Non ScalarPair abi {:?} for ByValPair CValue", - dst_layout.abi - ); + bug!("Non ScalarPair abi {:?} for ByValPair CValue", dst_layout.abi); } CValueInner::ByRef(from_ptr, None) => { let from_addr = from_ptr.get_addr(fx); @@ -642,18 +589,8 @@ impl<'tcx> CPlace<'tcx> { let layout = layout.field(&*fx, field.index()); match field.as_u32() { - 0 => { - return CPlace { - inner: CPlaceInner::Var(local, var1), - layout, - } - } - 1 => { - return CPlace { - inner: CPlaceInner::Var(local, var2), - layout, - } - } + 0 => return CPlace { inner: CPlaceInner::Var(local, var1), layout }, + 1 => return CPlace { inner: CPlaceInner::Var(local, var2), layout }, _ => unreachable!("field should be 0 or 1"), } } @@ -681,10 +618,7 @@ impl<'tcx> CPlace<'tcx> { _ => bug!("place_index({:?})", self.layout().ty), }; - let offset = fx - .bcx - .ins() - .imul_imm(index, elem_layout.size.bytes() as i64); + let offset = fx.bcx.ins().imul_imm(index, elem_layout.size.bytes() as i64); CPlace::for_ptr(ptr.offset_value(fx, offset), elem_layout) } @@ -695,10 +629,7 @@ impl<'tcx> CPlace<'tcx> { let (addr, extra) = self.to_cvalue(fx).load_scalar_pair(fx); CPlace::for_ptr_with_extra(Pointer::new(addr), extra, inner_layout) } else { - CPlace::for_ptr( - Pointer::new(self.to_cvalue(fx).load_scalar(fx)), - inner_layout, - ) + CPlace::for_ptr(Pointer::new(self.to_cvalue(fx).load_scalar(fx)), inner_layout) } } @@ -726,10 +657,7 @@ impl<'tcx> CPlace<'tcx> { ) -> Self { assert!(!self.layout().is_unsized()); let layout = self.layout().for_variant(fx, variant); - CPlace { - inner: self.inner, - layout, - } + CPlace { inner: self.inner, layout } } } @@ -768,12 +696,9 @@ pub(crate) fn assert_assignable<'tcx>( } (&ty::Dynamic(from_traits, _), &ty::Dynamic(to_traits, _)) => { for (from, to) in from_traits.iter().zip(to_traits) { - let from = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from); - let to = fx - .tcx - .normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to); + let from = + fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), from); + let to = fx.tcx.normalize_erasing_late_bound_regions(ParamEnv::reveal_all(), to); assert_eq!( from, to, "Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}", diff --git a/src/vtable.rs b/src/vtable.rs index a1f0aa40f0f16..4d2551a061b99 100644 --- a/src/vtable.rs +++ b/src/vtable.rs @@ -165,11 +165,8 @@ fn build_vtable<'tcx>( } fn write_usize(tcx: TyCtxt<'_>, buf: &mut [u8], idx: usize, num: u64) { - let pointer_size = tcx - .layout_of(ParamEnv::reveal_all().and(tcx.types.usize)) - .unwrap() - .size - .bytes() as usize; + let pointer_size = + tcx.layout_of(ParamEnv::reveal_all().and(tcx.types.usize)).unwrap().size.bytes() as usize; let target = &mut buf[idx * pointer_size..(idx + 1) * pointer_size]; match tcx.data_layout.endian { From 69a879f3d199580dde491bb679df319867a44e56 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 5 Mar 2021 10:35:22 -0500 Subject: [PATCH 55/61] Store `UNVERSIONED_FILES` in a data structure This allows querying it programatically. --- src/librustdoc/clean/types.rs | 18 ----------- src/librustdoc/html/render/write_shared.rs | 36 +++++++++++++--------- src/librustdoc/lib.rs | 18 +++++++++++ 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index e3f47a85d5154..1d34359db99c0 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -1612,24 +1612,6 @@ impl PrimitiveType { CELL.get_or_init(move || { use self::PrimitiveType::*; - /// A macro to create a FxHashMap. - /// - /// Example: - /// - /// ``` - /// let letters = map!{"a" => "b", "c" => "d"}; - /// ``` - /// - /// Trailing commas are allowed. - /// Commas between elements are required (even if the expression is a block). - macro_rules! map { - ($( $key: expr => $val: expr ),* $(,)*) => {{ - let mut map = ::rustc_data_structures::fx::FxHashMap::default(); - $( map.insert($key, $val); )* - map - }} - } - let single = |a: Option| a.into_iter().collect(); let both = |a: Option, b: Option| -> ArrayVec<_> { a.into_iter().chain(b).collect() diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index cbf0f9a4927c6..7fcfe35f9a967 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -4,6 +4,7 @@ use std::fs::{self, File}; use std::io::prelude::*; use std::io::{self, BufReader}; use std::path::{Component, Path, PathBuf}; +use std::lazy::SyncLazy as Lazy; use itertools::Itertools; use rustc_data_structures::flock; @@ -212,21 +213,26 @@ themePicker.onblur = handleThemeButtonsBlur; static_files::NORMALIZE_CSS, options.enable_minification, )?; - write(cx.dst.join("FiraSans-Regular.woff2"), static_files::fira_sans::REGULAR2)?; - write(cx.dst.join("FiraSans-Medium.woff2"), static_files::fira_sans::MEDIUM2)?; - write(cx.dst.join("FiraSans-Regular.woff"), static_files::fira_sans::REGULAR)?; - write(cx.dst.join("FiraSans-Medium.woff"), static_files::fira_sans::MEDIUM)?; - write(cx.dst.join("FiraSans-LICENSE.txt"), static_files::fira_sans::LICENSE)?; - write(cx.dst.join("SourceSerifPro-Regular.ttf.woff"), static_files::source_serif_pro::REGULAR)?; - write(cx.dst.join("SourceSerifPro-Bold.ttf.woff"), static_files::source_serif_pro::BOLD)?; - write(cx.dst.join("SourceSerifPro-It.ttf.woff"), static_files::source_serif_pro::ITALIC)?; - write(cx.dst.join("SourceSerifPro-LICENSE.md"), static_files::source_serif_pro::LICENSE)?; - write(cx.dst.join("SourceCodePro-Regular.woff"), static_files::source_code_pro::REGULAR)?; - write(cx.dst.join("SourceCodePro-Semibold.woff"), static_files::source_code_pro::SEMIBOLD)?; - write(cx.dst.join("SourceCodePro-LICENSE.txt"), static_files::source_code_pro::LICENSE)?; - write(cx.dst.join("LICENSE-MIT.txt"), static_files::LICENSE_MIT)?; - write(cx.dst.join("LICENSE-APACHE.txt"), static_files::LICENSE_APACHE)?; - write(cx.dst.join("COPYRIGHT.txt"), static_files::COPYRIGHT)?; + static FILES_UNVERSIONED: Lazy> = Lazy::new(|| map! { + "FiraSans-Regular.woff2" => static_files::fira_sans::REGULAR2, + "FiraSans-Medium.woff2" => static_files::fira_sans::MEDIUM2, + "FiraSans-Regular.woff" => static_files::fira_sans::REGULAR, + "FiraSans-Medium.woff" => static_files::fira_sans::MEDIUM, + "FiraSans-LICENSE.txt" => static_files::fira_sans::LICENSE, + "SourceSerifPro-Regular.ttf.woff" => static_files::source_serif_pro::REGULAR, + "SourceSerifPro-Bold.ttf.woff" => static_files::source_serif_pro::BOLD, + "SourceSerifPro-It.ttf.woff" => static_files::source_serif_pro::ITALIC, + "SourceSerifPro-LICENSE.md" => static_files::source_serif_pro::LICENSE, + "SourceCodePro-Regular.woff" => static_files::source_code_pro::REGULAR, + "SourceCodePro-Semibold.woff" => static_files::source_code_pro::SEMIBOLD, + "SourceCodePro-LICENSE.txt" => static_files::source_code_pro::LICENSE, + "LICENSE-MIT.txt" => static_files::LICENSE_MIT, + "LICENSE-APACHE.txt" => static_files::LICENSE_APACHE, + "COPYRIGHT.txt" => static_files::COPYRIGHT, + }); + for (file, contents) in &*FILES_UNVERSIONED { + write(cx.dst.join(file), contents)?; + } fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec, Vec)> { let mut ret = Vec::new(); diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index e447b49c11a77..d214cea8fc439 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -71,6 +71,24 @@ use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGro use rustc_session::getopts; use rustc_session::{early_error, early_warn}; +/// A macro to create a FxHashMap. +/// +/// Example: +/// +/// ``` +/// let letters = map!{"a" => "b", "c" => "d"}; +/// ``` +/// +/// Trailing commas are allowed. +/// Commas between elements are required (even if the expression is a block). +macro_rules! map { + ($( $key: expr => $val: expr ),* $(,)*) => {{ + let mut map = ::rustc_data_structures::fx::FxHashMap::default(); + $( map.insert($key, $val); )* + map + }} +} + #[macro_use] mod externalfiles; From 9a0c32934ebe376128230aa8da3275697b2053e7 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 5 Mar 2021 10:45:56 +0100 Subject: [PATCH 56/61] Disable value debuginfo This prevents an expensive Cranelift pass from running to reconstruct the debuginfo --- src/value_and_place.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/value_and_place.rs b/src/value_and_place.rs index eaaf71ff20f8d..cffaf79ded10b 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -354,19 +354,19 @@ impl<'tcx> CPlace<'tcx> { match self.inner { CPlaceInner::Var(_local, var) => { let val = fx.bcx.use_var(var); - fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); + //fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); CValue::by_val(val, layout) } CPlaceInner::VarPair(_local, var1, var2) => { let val1 = fx.bcx.use_var(var1); - fx.bcx.set_val_label(val1, cranelift_codegen::ir::ValueLabel::new(var1.index())); + //fx.bcx.set_val_label(val1, cranelift_codegen::ir::ValueLabel::new(var1.index())); let val2 = fx.bcx.use_var(var2); - fx.bcx.set_val_label(val2, cranelift_codegen::ir::ValueLabel::new(var2.index())); + //fx.bcx.set_val_label(val2, cranelift_codegen::ir::ValueLabel::new(var2.index())); CValue::by_val_pair(val1, val2, layout) } CPlaceInner::VarLane(_local, var, lane) => { let val = fx.bcx.use_var(var); - fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); + //fx.bcx.set_val_label(val, cranelift_codegen::ir::ValueLabel::new(var.index())); let val = fx.bcx.ins().extractlane(val, lane); CValue::by_val(val, layout) } @@ -456,7 +456,7 @@ impl<'tcx> CPlace<'tcx> { } _ => unreachable!("write_cvalue_transmute: {:?} -> {:?}", src_ty, dst_ty), }; - fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index())); + //fx.bcx.set_val_label(data, cranelift_codegen::ir::ValueLabel::new(var.index())); fx.bcx.def_var(var, data); } @@ -502,13 +502,13 @@ impl<'tcx> CPlace<'tcx> { // First get the old vector let vector = fx.bcx.use_var(var); - fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); + //fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); // Next insert the written lane into the vector let vector = fx.bcx.ins().insertlane(vector, data, lane); // Finally write the new vector - fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); + //fx.bcx.set_val_label(vector, cranelift_codegen::ir::ValueLabel::new(var.index())); fx.bcx.def_var(var, vector); return; From 2371914a05f8f2763dffe6e2511d0870bcd6b461 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Wed, 3 Mar 2021 21:09:01 +0100 Subject: [PATCH 57/61] Prevent Zip specialization from calling __iterator_get_unchecked twice with the same index after calling next_back --- library/core/src/iter/adapters/zip.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/library/core/src/iter/adapters/zip.rs b/library/core/src/iter/adapters/zip.rs index 817fc2a51e981..ea7a809c6badb 100644 --- a/library/core/src/iter/adapters/zip.rs +++ b/library/core/src/iter/adapters/zip.rs @@ -13,9 +13,10 @@ use crate::iter::{InPlaceIterable, SourceIter, TrustedLen}; pub struct Zip { a: A, b: B, - // index and len are only used by the specialized version of zip + // index, len and a_len are only used by the specialized version of zip index: usize, len: usize, + a_len: usize, } impl Zip { pub(in crate::iter) fn new(a: A, b: B) -> Zip { @@ -110,6 +111,7 @@ where b, index: 0, // unused len: 0, // unused + a_len: 0, // unused } } @@ -184,8 +186,9 @@ where B: TrustedRandomAccess + Iterator, { fn new(a: A, b: B) -> Self { - let len = cmp::min(a.size(), b.size()); - Zip { a, b, index: 0, len } + let a_len = a.size(); + let len = cmp::min(a_len, b.size()); + Zip { a, b, index: 0, len, a_len } } #[inline] @@ -197,7 +200,7 @@ where unsafe { Some((self.a.__iterator_get_unchecked(i), self.b.__iterator_get_unchecked(i))) } - } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a.size() { + } else if A::MAY_HAVE_SIDE_EFFECT && self.index < self.a_len { let i = self.index; self.index += 1; self.len += 1; @@ -262,6 +265,7 @@ where for _ in 0..sz_a - self.len { self.a.next_back(); } + self.a_len = self.len; } let sz_b = self.b.size(); if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len { @@ -273,6 +277,7 @@ where } if self.index < self.len { self.len -= 1; + self.a_len -= 1; let i = self.len; // SAFETY: `i` is smaller than the previous value of `self.len`, // which is also smaller than or equal to `self.a.len()` and `self.b.len()` From c1bfb9a78db6d481be1d03355672712c766e20b0 Mon Sep 17 00:00:00 2001 From: Giacomo Stevanato Date: Fri, 19 Feb 2021 15:25:09 +0100 Subject: [PATCH 58/61] Add relevant test --- library/core/tests/iter/adapters/zip.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/library/core/tests/iter/adapters/zip.rs b/library/core/tests/iter/adapters/zip.rs index a597710392952..000c15f72c886 100644 --- a/library/core/tests/iter/adapters/zip.rs +++ b/library/core/tests/iter/adapters/zip.rs @@ -265,3 +265,26 @@ fn test_issue_82282() { panic!(); } } + +#[test] +fn test_issue_82291() { + use std::cell::Cell; + + let mut v1 = [()]; + let v2 = [()]; + + let called = Cell::new(0); + + let mut zip = v1 + .iter_mut() + .map(|r| { + called.set(called.get() + 1); + r + }) + .zip(&v2); + + zip.next_back(); + assert_eq!(called.get(), 1); + zip.next(); + assert_eq!(called.get(), 1); +} From 173d2aaa009d7d1b76addefc204a2bde329caa83 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 5 Mar 2021 10:54:37 -0500 Subject: [PATCH 59/61] Add an unstable option to print all unversioned files This allows sharing those files between different doc invocations without having to know their names ahead of time. --- src/librustdoc/config.rs | 7 ++++ src/librustdoc/html/render/mod.rs | 1 + src/librustdoc/html/render/write_shared.rs | 39 ++++++++++--------- src/librustdoc/lib.rs | 3 ++ .../print-unversioned-files/Makefile | 4 ++ .../unversioned-files.txt | 15 +++++++ 6 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 src/test/run-make-fulldeps/print-unversioned-files/Makefile create mode 100644 src/test/run-make-fulldeps/print-unversioned-files/unversioned-files.txt diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index de6942968ea0d..c8c83ec5ab511 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -315,6 +315,13 @@ impl Options { return Err(0); } + if matches.opt_strs("print").iter().any(|opt| opt == "unversioned-files") { + for file in crate::html::render::FILES_UNVERSIONED.keys() { + println!("{}", file); + } + return Err(0); + } + let color = config::parse_color(&matches); let (json_rendered, _artifacts) = config::parse_json(&matches); let error_format = config::parse_error_format(&matches, color, json_rendered); diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 50cae50c2c3eb..9b9ec2581cfc2 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -33,6 +33,7 @@ mod print_item; mod write_shared; crate use context::*; +crate use write_shared::FILES_UNVERSIONED; use std::cell::{Cell, RefCell}; use std::collections::VecDeque; diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 7fcfe35f9a967..02ad01aa29a02 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -3,8 +3,8 @@ use std::fmt::Write; use std::fs::{self, File}; use std::io::prelude::*; use std::io::{self, BufReader}; -use std::path::{Component, Path, PathBuf}; use std::lazy::SyncLazy as Lazy; +use std::path::{Component, Path, PathBuf}; use itertools::Itertools; use rustc_data_structures::flock; @@ -19,6 +19,26 @@ use crate::error::Error; use crate::formats::FormatRenderer; use crate::html::{layout, static_files}; +crate static FILES_UNVERSIONED: Lazy> = Lazy::new(|| { + map! { + "FiraSans-Regular.woff2" => static_files::fira_sans::REGULAR2, + "FiraSans-Medium.woff2" => static_files::fira_sans::MEDIUM2, + "FiraSans-Regular.woff" => static_files::fira_sans::REGULAR, + "FiraSans-Medium.woff" => static_files::fira_sans::MEDIUM, + "FiraSans-LICENSE.txt" => static_files::fira_sans::LICENSE, + "SourceSerifPro-Regular.ttf.woff" => static_files::source_serif_pro::REGULAR, + "SourceSerifPro-Bold.ttf.woff" => static_files::source_serif_pro::BOLD, + "SourceSerifPro-It.ttf.woff" => static_files::source_serif_pro::ITALIC, + "SourceSerifPro-LICENSE.md" => static_files::source_serif_pro::LICENSE, + "SourceCodePro-Regular.woff" => static_files::source_code_pro::REGULAR, + "SourceCodePro-Semibold.woff" => static_files::source_code_pro::SEMIBOLD, + "SourceCodePro-LICENSE.txt" => static_files::source_code_pro::LICENSE, + "LICENSE-MIT.txt" => static_files::LICENSE_MIT, + "LICENSE-APACHE.txt" => static_files::LICENSE_APACHE, + "COPYRIGHT.txt" => static_files::COPYRIGHT, + } +}); + pub(super) fn write_shared( cx: &Context<'_>, krate: &Crate, @@ -213,23 +233,6 @@ themePicker.onblur = handleThemeButtonsBlur; static_files::NORMALIZE_CSS, options.enable_minification, )?; - static FILES_UNVERSIONED: Lazy> = Lazy::new(|| map! { - "FiraSans-Regular.woff2" => static_files::fira_sans::REGULAR2, - "FiraSans-Medium.woff2" => static_files::fira_sans::MEDIUM2, - "FiraSans-Regular.woff" => static_files::fira_sans::REGULAR, - "FiraSans-Medium.woff" => static_files::fira_sans::MEDIUM, - "FiraSans-LICENSE.txt" => static_files::fira_sans::LICENSE, - "SourceSerifPro-Regular.ttf.woff" => static_files::source_serif_pro::REGULAR, - "SourceSerifPro-Bold.ttf.woff" => static_files::source_serif_pro::BOLD, - "SourceSerifPro-It.ttf.woff" => static_files::source_serif_pro::ITALIC, - "SourceSerifPro-LICENSE.md" => static_files::source_serif_pro::LICENSE, - "SourceCodePro-Regular.woff" => static_files::source_code_pro::REGULAR, - "SourceCodePro-Semibold.woff" => static_files::source_code_pro::SEMIBOLD, - "SourceCodePro-LICENSE.txt" => static_files::source_code_pro::LICENSE, - "LICENSE-MIT.txt" => static_files::LICENSE_MIT, - "LICENSE-APACHE.txt" => static_files::LICENSE_APACHE, - "COPYRIGHT.txt" => static_files::COPYRIGHT, - }); for (file, contents) in &*FILES_UNVERSIONED { write(cx.dst.join(file), contents)?; } diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index d214cea8fc439..9e8432454429f 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -524,6 +524,9 @@ fn opts() -> Vec { "Generate JSON file at the top level instead of generating HTML redirection files", ) }), + unstable("print", |o| { + o.optmulti("", "print", "Rustdoc information to print on stdout", "[unversioned-files]") + }), ] } diff --git a/src/test/run-make-fulldeps/print-unversioned-files/Makefile b/src/test/run-make-fulldeps/print-unversioned-files/Makefile new file mode 100644 index 0000000000000..e368f61cddfc3 --- /dev/null +++ b/src/test/run-make-fulldeps/print-unversioned-files/Makefile @@ -0,0 +1,4 @@ +-include ../tools.mk + +all: + $(RUSTDOC) -Z unstable-options --print unversioned-files | sort | diff - unversioned-files.txt diff --git a/src/test/run-make-fulldeps/print-unversioned-files/unversioned-files.txt b/src/test/run-make-fulldeps/print-unversioned-files/unversioned-files.txt new file mode 100644 index 0000000000000..c5a9bf2438160 --- /dev/null +++ b/src/test/run-make-fulldeps/print-unversioned-files/unversioned-files.txt @@ -0,0 +1,15 @@ +COPYRIGHT.txt +FiraSans-LICENSE.txt +FiraSans-Medium.woff +FiraSans-Medium.woff2 +FiraSans-Regular.woff +FiraSans-Regular.woff2 +LICENSE-APACHE.txt +LICENSE-MIT.txt +SourceCodePro-LICENSE.txt +SourceCodePro-Regular.woff +SourceCodePro-Semibold.woff +SourceSerifPro-Bold.ttf.woff +SourceSerifPro-It.ttf.woff +SourceSerifPro-LICENSE.md +SourceSerifPro-Regular.ttf.woff From 7d3a6f16551fa9ff54fbfa9c648c21ae0f6d550f Mon Sep 17 00:00:00 2001 From: Henry Boisdequin <65845077+henryboisdequin@users.noreply.github.com> Date: Wed, 3 Mar 2021 17:09:40 +0530 Subject: [PATCH 60/61] address comments --- compiler/rustc_typeck/src/collect/type_of.rs | 6 ++++-- src/test/ui/79040.rs | 5 ----- src/test/ui/parser/item-free-const-no-body-semantic-fail.rs | 1 - .../ui/parser/item-free-static-no-body-semantic-fail.rs | 2 -- .../ui/parser/item-free-static-no-body-semantic-fail.stderr | 4 ++-- src/test/ui/typeck/issue-79040.rs | 5 +++++ src/test/ui/{79040.stderr => typeck/issue-79040.stderr} | 4 ++-- 7 files changed, 13 insertions(+), 14 deletions(-) delete mode 100644 src/test/ui/79040.rs create mode 100644 src/test/ui/typeck/issue-79040.rs rename src/test/ui/{79040.stderr => typeck/issue-79040.stderr} (88%) diff --git a/compiler/rustc_typeck/src/collect/type_of.rs b/compiler/rustc_typeck/src/collect/type_of.rs index 92e2b5232027b..1f47e4899f18c 100644 --- a/compiler/rustc_typeck/src/collect/type_of.rs +++ b/compiler/rustc_typeck/src/collect/type_of.rs @@ -659,11 +659,12 @@ fn infer_placeholder_type( format!("{}: {}", item_ident, ty), Applicability::MachineApplicable, ) - .emit_unless(matches!(ty.kind(), ty::Error(_))); + .emit_unless(ty.references_error()); } None => { let mut diag = bad_placeholder_type(tcx, vec![span]); - if !matches!(ty.kind(), ty::Error(_)) { + + if !ty.references_error() { diag.span_suggestion( span, "replace `_` with the correct type", @@ -671,6 +672,7 @@ fn infer_placeholder_type( Applicability::MaybeIncorrect, ); } + diag.emit(); } } diff --git a/src/test/ui/79040.rs b/src/test/ui/79040.rs deleted file mode 100644 index adc9dc8b42656..0000000000000 --- a/src/test/ui/79040.rs +++ /dev/null @@ -1,5 +0,0 @@ -fn main() { - const FOO = "hello" + 1; - //^~ ERROR cannot add `{integer}` to `&str` - println!("{}", FOO); -} \ No newline at end of file diff --git a/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs b/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs index 613b3c9856171..15a15a207b1ca 100644 --- a/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs +++ b/src/test/ui/parser/item-free-const-no-body-semantic-fail.rs @@ -4,4 +4,3 @@ fn main() {} const A: u8; //~ ERROR free constant item without body const B; //~ ERROR free constant item without body -//~^ ERROR missing type for `const` item diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs b/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs index 780479e3d26ac..61d3eab24d8c7 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.rs @@ -4,8 +4,6 @@ fn main() {} static A: u8; //~ ERROR free static item without body static B; //~ ERROR free static item without body -//~^ ERROR missing type for `static` item static mut C: u8; //~ ERROR free static item without body static mut D; //~ ERROR free static item without body -//~^ ERROR missing type for `static mut` item diff --git a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr index 2a270c8290fd4..7b408323674de 100644 --- a/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr +++ b/src/test/ui/parser/item-free-static-no-body-semantic-fail.stderr @@ -15,7 +15,7 @@ LL | static B; | help: provide a definition for the static: `= ;` error: free static item without body - --> $DIR/item-free-static-no-body-semantic-fail.rs:9:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:8:1 | LL | static mut C: u8; | ^^^^^^^^^^^^^^^^- @@ -23,7 +23,7 @@ LL | static mut C: u8; | help: provide a definition for the static: `= ;` error: free static item without body - --> $DIR/item-free-static-no-body-semantic-fail.rs:10:1 + --> $DIR/item-free-static-no-body-semantic-fail.rs:9:1 | LL | static mut D; | ^^^^^^^^^^^^- diff --git a/src/test/ui/typeck/issue-79040.rs b/src/test/ui/typeck/issue-79040.rs new file mode 100644 index 0000000000000..af2a9c1ba87ef --- /dev/null +++ b/src/test/ui/typeck/issue-79040.rs @@ -0,0 +1,5 @@ +fn main() { + const FOO = "hello" + 1; //~ ERROR cannot add `{integer}` to `&str` + //~^ ERROR cannot add `{integer}` to `&str` + println!("{}", FOO); +} diff --git a/src/test/ui/79040.stderr b/src/test/ui/typeck/issue-79040.stderr similarity index 88% rename from src/test/ui/79040.stderr rename to src/test/ui/typeck/issue-79040.stderr index be7d2363c2d52..32049e5d96860 100644 --- a/src/test/ui/79040.stderr +++ b/src/test/ui/typeck/issue-79040.stderr @@ -1,5 +1,5 @@ error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/79040.rs:2:25 + --> $DIR/issue-79040.rs:2:25 | LL | const FOO = "hello" + 1; | ------- ^ - {integer} @@ -7,7 +7,7 @@ LL | const FOO = "hello" + 1; | &str error[E0369]: cannot add `{integer}` to `&str` - --> $DIR/79040.rs:2:25 + --> $DIR/issue-79040.rs:2:25 | LL | const FOO = "hello" + 1; | ------- ^ - {integer} From 0ca63ec1f7f46c2784cc2e6059a90cce0ceccd01 Mon Sep 17 00:00:00 2001 From: Henry Boisdequin <65845077+henryboisdequin@users.noreply.github.com> Date: Sat, 6 Mar 2021 09:16:29 +0530 Subject: [PATCH 61/61] Fix typo we need to actually -> we actually need to @rustbot label +C-cleanup --- compiler/rustc/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc/src/main.rs b/compiler/rustc/src/main.rs index 6bc5aa6382c4c..0151973eccfda 100644 --- a/compiler/rustc/src/main.rs +++ b/compiler/rustc/src/main.rs @@ -4,7 +4,7 @@ fn main() { // Note that we're pulling in a static copy of jemalloc which means that to // pull it in we need to actually reference its symbols for it to get // linked. The two crates we link to here, std and rustc_driver, are both - // dynamic libraries. That means to pull in jemalloc we need to actually + // dynamic libraries. That means to pull in jemalloc we actually need to // reference allocation symbols one way or another (as this file is the only // object code in the rustc executable). #[cfg(feature = "jemalloc-sys")]