From 67b711af7fe4d00af20eca55518d0e8f3d3e39a1 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 12 Dec 2022 15:47:32 +0100 Subject: [PATCH 001/101] Rename `assert_uninit_valid` intrinsic It's not about "uninit" anymore but about "filling with 0x01 bytes" so the name should at least try to reflect that. --- src/intrinsics/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 0302b843aa226..e4a27f1bb6d40 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -713,7 +713,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let res = CValue::by_val(swap(&mut fx.bcx, val), arg.layout()); ret.write_cvalue(fx, res); } - sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => { + sym::assert_inhabited | sym::assert_zero_valid | sym::assert_mem_uninitialized_valid => { intrinsic_args!(fx, args => (); intrinsic); let layout = fx.layout_of(substs.type_at(0)); @@ -742,7 +742,9 @@ fn codegen_regular_intrinsic_call<'tcx>( return; } - if intrinsic == sym::assert_uninit_valid && !fx.tcx.permits_uninit_init(layout) { + if intrinsic == sym::assert_mem_uninitialized_valid + && !fx.tcx.permits_uninit_init(layout) + { with_no_trimmed_paths!({ crate::base::codegen_panic( fx, From 98a276b5895697a862b48bcaf07b423de0b0deef Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 14 Dec 2022 19:30:46 +0100 Subject: [PATCH 002/101] Merge commit '2bb3996244cf1b89878da9e39841e9f6bf061602' into sync_cg_clif-2022-12-14 --- .cirrus.yml | 2 - .github/workflows/main.yml | 52 ++- .github/workflows/nightly-cranelift.yml | 2 +- .github/workflows/rustc.yml | 16 +- .gitignore | 1 + .vscode/settings.json | 35 +- Cargo.lock | 125 ++++-- Cargo.toml | 20 +- Readme.md | 2 +- build_sysroot/Cargo.lock | 57 +-- build_system/abi_cafe.rs | 20 +- build_system/build_backend.rs | 13 +- build_system/build_sysroot.rs | 98 +++-- build_system/mod.rs | 91 ++-- build_system/path.rs | 70 +++ build_system/prepare.rs | 154 +++---- build_system/rustc_info.rs | 20 + build_system/tests.rs | 553 +++++++++++++----------- build_system/utils.rs | 145 ++++++- clean_all.sh | 2 +- config.txt | 1 + docs/usage.md | 12 +- example/issue-72793.rs | 24 + example/mini_core.rs | 7 +- example/mini_core_hello_world.rs | 2 - example/std_example.rs | 2 + rust-toolchain | 2 +- rustfmt.toml | 2 + scripts/filter_profile.rs | 2 +- scripts/rustdoc-clif.rs | 36 ++ scripts/setup_rust_fork.sh | 22 +- scripts/test_rustc_tests.sh | 14 +- src/abi/mod.rs | 29 +- src/allocator.rs | 4 +- src/base.rs | 12 +- src/cast.rs | 2 +- src/common.rs | 11 +- src/constant.rs | 82 ++-- src/debuginfo/unwind.rs | 4 +- src/discriminant.rs | 207 +++++++-- src/driver/jit.rs | 12 +- src/driver/mod.rs | 3 +- src/intrinsics/llvm.rs | 176 +------- src/intrinsics/llvm_aarch64.rs | 222 ++++++++++ src/intrinsics/llvm_x86.rs | 197 +++++++++ src/intrinsics/mod.rs | 96 +--- src/intrinsics/simd.rs | 7 +- src/main_shim.rs | 6 +- src/num.rs | 6 +- src/optimize/peephole.rs | 20 - src/value_and_place.rs | 15 +- test.sh | 2 +- y.rs | 2 +- 53 files changed, 1691 insertions(+), 1028 deletions(-) create mode 100644 build_system/path.rs create mode 100644 example/issue-72793.rs create mode 100644 scripts/rustdoc-clif.rs create mode 100644 src/intrinsics/llvm_aarch64.rs create mode 100644 src/intrinsics/llvm_x86.rs diff --git a/.cirrus.yml b/.cirrus.yml index 732edd66196d7..d627c2ee09c4e 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -12,8 +12,6 @@ task: folder: target prepare_script: - . $HOME/.cargo/env - - git config --global user.email "user@example.com" - - git config --global user.name "User" - ./y.rs prepare test_script: - . $HOME/.cargo/env diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5061010c86cd3..a6bb12a66a247 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,6 +19,7 @@ jobs: - name: Rustfmt run: | cargo fmt --check + rustfmt --check build_system/mod.rs build: runs-on: ${{ matrix.os }} @@ -28,7 +29,7 @@ jobs: fail-fast: false matrix: include: - - os: ubuntu-latest + - os: ubuntu-20.04 # FIXME switch to ubuntu-22.04 once #1303 is fixed env: TARGET_TRIPLE: x86_64-unknown-linux-gnu - os: macos-latest @@ -41,18 +42,22 @@ jobs: - os: ubuntu-latest env: TARGET_TRIPLE: aarch64-unknown-linux-gnu + # s390x requires QEMU 6.1 or greater, we could build it from source, but ubuntu 22.04 comes with 6.2 by default + - os: ubuntu-latest + env: + TARGET_TRIPLE: s390x-unknown-linux-gnu steps: - uses: actions/checkout@v3 - name: Cache cargo installed crates - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: ${{ runner.os }}-cargo-installed-crates - name: Cache cargo registry and index - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -60,9 +65,9 @@ jobs: key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo target dir - uses: actions/cache@v2 + uses: actions/cache@v3 with: - path: target + path: build/cg_clif key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Install MinGW toolchain and wine @@ -78,11 +83,14 @@ jobs: sudo apt-get update sudo apt-get install -y gcc-aarch64-linux-gnu qemu-user - - name: Prepare dependencies + - name: Install s390x toolchain and qemu + if: matrix.env.TARGET_TRIPLE == 's390x-unknown-linux-gnu' run: | - git config --global user.email "user@example.com" - git config --global user.name "User" - ./y.rs prepare + sudo apt-get update + sudo apt-get install -y gcc-s390x-linux-gnu qemu-user + + - name: Prepare dependencies + run: ./y.rs prepare - name: Build without unstable features env: @@ -110,7 +118,7 @@ jobs: ./y.rs test - name: Package prebuilt cg_clif - run: tar cvfJ cg_clif.tar.xz build + run: tar cvfJ cg_clif.tar.xz dist - name: Upload prebuilt cg_clif if: matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu' @@ -121,7 +129,7 @@ jobs: - name: Upload prebuilt cg_clif (cross compile) if: matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: cg_clif-${{ runner.os }}-cross-x86_64-mingw path: cg_clif.tar.xz @@ -147,13 +155,13 @@ jobs: - uses: actions/checkout@v3 - name: Cache cargo installed crates - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-installed-crates - name: Cache cargo registry and index - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -161,9 +169,9 @@ jobs: key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo target dir - uses: actions/cache@v2 + uses: actions/cache@v3 with: - path: target + path: build/cg_clif key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Set MinGW as the default toolchain @@ -172,8 +180,6 @@ jobs: - name: Prepare dependencies run: | - git config --global user.email "user@example.com" - git config --global user.name "User" git config --global core.autocrlf false rustc y.rs -o y.exe -g ./y.exe prepare @@ -198,24 +204,24 @@ jobs: # Enable extra checks $Env:CG_CLIF_ENABLE_VERIFIER=1 - + # WIP Disable some tests - + # This fails due to some weird argument handling by hyperfine, not an actual regression # more of a build system issue (Get-Content config.txt) -replace '(bench.simple-raytracer)', '# $1' | Out-File config.txt - - # This fails with a different output than expected + + # This fails with a different output than expected (Get-Content config.txt) -replace '(test.regex-shootout-regex-dna)', '# $1' | Out-File config.txt ./y.exe test - name: Package prebuilt cg_clif # don't use compression as xzip isn't supported by tar on windows and bzip2 hangs - run: tar cvf cg_clif.tar build + run: tar cvf cg_clif.tar dist - name: Upload prebuilt cg_clif - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: cg_clif-${{ matrix.env.TARGET_TRIPLE }} path: cg_clif.tar diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index 0a3e7ca073b45..d0d58d2a7eacb 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -14,7 +14,7 @@ jobs: - uses: actions/checkout@v3 - name: Cache cargo installed crates - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: ubuntu-latest-cargo-installed-crates diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index b8a98b83ebe5e..bef806318efa8 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -11,13 +11,13 @@ jobs: - uses: actions/checkout@v3 - name: Cache cargo installed crates - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: ${{ runner.os }}-cargo-installed-crates - name: Cache cargo registry and index - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -25,9 +25,9 @@ jobs: key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo target dir - uses: actions/cache@v2 + uses: actions/cache@v3 with: - path: target + path: build/cg_clif key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Prepare dependencies @@ -49,13 +49,13 @@ jobs: - uses: actions/checkout@v3 - name: Cache cargo installed crates - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: ${{ runner.os }}-cargo-installed-crates - name: Cache cargo registry and index - uses: actions/cache@v2 + uses: actions/cache@v3 with: path: | ~/.cargo/registry @@ -63,9 +63,9 @@ jobs: key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo target dir - uses: actions/cache@v2 + uses: actions/cache@v3 with: - path: target + path: build/cg_clif key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - name: Prepare dependencies diff --git a/.gitignore b/.gitignore index fae09592c6ac0..b443fd58a1b98 100644 --- a/.gitignore +++ b/.gitignore @@ -14,5 +14,6 @@ perf.data.old /build_sysroot/sysroot_src /build_sysroot/compiler-builtins /build_sysroot/rustc_version +/dist /rust /download diff --git a/.vscode/settings.json b/.vscode/settings.json index 13301bf20a5ed..bc914e37d2b51 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,16 +4,10 @@ "rust-analyzer.imports.granularity.enforce": true, "rust-analyzer.imports.granularity.group": "module", "rust-analyzer.imports.prefix": "crate", - "rust-analyzer.cargo.features": ["unstable-features"], + "rust-analyzer.cargo.features": ["unstable-features", "__check_build_system_using_ra"], "rust-analyzer.linkedProjects": [ "./Cargo.toml", - //"./build_sysroot/sysroot_src/library/std/Cargo.toml", { - "roots": [ - "./example/mini_core.rs", - "./example/mini_core_hello_world.rs", - "./example/mod_bench.rs" - ], "crates": [ { "root_module": "./example/mini_core.rs", @@ -36,34 +30,11 @@ ] }, { - "roots": ["./example/std_example.rs"], + "sysroot_src": "./build_sysroot/sysroot_src/library", "crates": [ { "root_module": "./example/std_example.rs", - "edition": "2018", - "deps": [{ "crate": 1, "name": "std" }], - "cfg": [], - }, - { - "root_module": "./build_sysroot/sysroot_src/library/std/src/lib.rs", - "edition": "2018", - "deps": [], - "cfg": [], - }, - ] - }, - { - "roots": ["./y.rs"], - "crates": [ - { - "root_module": "./y.rs", - "edition": "2018", - "deps": [{ "crate": 1, "name": "std" }], - "cfg": [], - }, - { - "root_module": "./build_sysroot/sysroot_src/library/std/src/lib.rs", - "edition": "2018", + "edition": "2015", "deps": [], "cfg": [], }, diff --git a/Cargo.lock b/Cargo.lock index 3b406036c356e..e4d3e9ca5ae0a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,9 +15,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.60" +version = "1.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c794e162a5eff65c72ef524dfe393eb923c354e350bb78b9c7383df13f3bc142" +checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6" [[package]] name = "arrayvec" @@ -39,9 +39,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bumpalo" -version = "3.11.0" +version = "3.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1ad822118d20d2c234f427000d5acc36eabe1e29a348c89b63dd60b13f28e5d" +checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" [[package]] name = "byteorder" @@ -57,24 +57,25 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44409ccf2d0f663920cab563d2b79fcd6b2e9a2bcc6e929fef76c8f82ad6c17a" +checksum = "b62c772976416112fa4484cbd688cb6fb35fd430005c1c586224fc014018abad" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98de2018ad96eb97f621f7d6b900a0cc661aec8d02ea4a50e56ecb48e5a2fcaf" +checksum = "9b40ed2dd13c2ac7e24f88a3090c68ad3414eb1d066a95f8f1f7b3b819cb4e46" dependencies = [ "arrayvec", "bumpalo", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", + "cranelift-egraph", "cranelift-entity", "cranelift-isle", "gimli", @@ -86,30 +87,44 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5287ce36e6c4758fbaf298bd1a8697ad97a4f2375a3d1b61142ea538db4877e5" +checksum = "bb927a8f1c27c34ee3759b6b0ffa528d2330405d5cc4511f0cab33fe2279f4b5" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2855c24219e2f08827f3f4ffb2da92e134ae8d8ecc185b11ec8f9878cf5f588e" +checksum = "43dfa417b884a9ab488d95fd6b93b25e959321fe7bfd7a0a960ba5d7fb7ab927" + +[[package]] +name = "cranelift-egraph" +version = "0.90.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a66b39785efd8513d2cca967ede56d6cc57c8d7986a595c7c47d0c78de8dce" +dependencies = [ + "cranelift-entity", + "fxhash", + "hashbrown", + "indexmap", + "log", + "smallvec", +] [[package]] name = "cranelift-entity" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b65673279d75d34bf11af9660ae2dbd1c22e6d28f163f5c72f4e1dc56d56103" +checksum = "0637ffde963cb5d759bc4d454cfa364b6509e6c74cdaa21298add0ed9276f346" [[package]] name = "cranelift-frontend" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ed2b3d7a4751163f6c4a349205ab1b7d9c00eecf19dcea48592ef1f7688eefc" +checksum = "fb72b8342685e850cb037350418f62cc4fc55d6c2eb9c7ca01b82f9f1a6f3d56" dependencies = [ "cranelift-codegen", "log", @@ -119,15 +134,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3be64cecea9d90105fc6a2ba2d003e98c867c1d6c4c86cc878f97ad9fb916293" +checksum = "850579cb9e4b448f7c301f1e6e6cbad99abe3f1f1d878a4994cb66e33c6db8cd" [[package]] name = "cranelift-jit" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f98ed42a70a0c9c388e34ec9477f57fc7300f541b1e5136a0e2ea02b1fac6015" +checksum = "9add822ad66dcbe152b5ab57de10240a2df4505099f2f6c27159acb711890bd4" dependencies = [ "anyhow", "cranelift-codegen", @@ -138,14 +153,15 @@ dependencies = [ "log", "region", "target-lexicon", + "wasmtime-jit-icache-coherence", "windows-sys", ] [[package]] name = "cranelift-module" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d658ac7f156708bfccb647216cc8b9387469f50d352ba4ad80150541e4ae2d49" +checksum = "406b772626fc2664864cf947f3895a23b619895c7fff635f3622e2d857f4492f" dependencies = [ "anyhow", "cranelift-codegen", @@ -153,9 +169,9 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4a03a6ac1b063e416ca4b93f6247978c991475e8271465340caa6f92f3c16a4" +checksum = "2d0a279e5bcba3e0466c734d8d8eb6bfc1ad29e95c37f3e4955b492b5616335e" dependencies = [ "cranelift-codegen", "libc", @@ -164,9 +180,9 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.88.1" +version = "0.90.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eef0b4119b645b870a43a036d76c0ada3a076b1f82e8b8487659304c8b09049b" +checksum = "39793c550f0c1d7db96c2fc1324583670c8143befe6edbfbaf1c68aba53be983" dependencies = [ "anyhow", "cranelift-codegen", @@ -185,6 +201,12 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "fxhash" version = "0.2.1" @@ -196,9 +218,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.7" +version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ "cfg-if", "libc", @@ -211,7 +233,9 @@ version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" dependencies = [ + "fallible-iterator", "indexmap", + "stable_deref_trait", ] [[package]] @@ -225,9 +249,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "1.9.1" +version = "1.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e" +checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399" dependencies = [ "autocfg", "hashbrown", @@ -235,15 +259,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.127" +version = "0.2.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b" +checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" [[package]] name = "libloading" -version = "0.7.3" +version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd" +checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" dependencies = [ "cfg-if", "winapi", @@ -287,15 +311,15 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.13.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" +checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" [[package]] name = "regalloc2" -version = "0.3.2" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d43a209257d978ef079f3d446331d0f1794f5e0fc19b306a199983857833a779" +checksum = "91b2eab54204ea0117fe9a060537e0b07a4e72f7c7d182361ecc346cab2240e5" dependencies = [ "fxhash", "log", @@ -342,15 +366,21 @@ checksum = "03b634d87b960ab1a38c4fe143b508576f075e7c978bfad18217645ebfdfa2ec" [[package]] name = "smallvec" -version = "1.9.0" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" +checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" [[package]] name = "target-lexicon" -version = "0.12.4" +version = "0.12.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c02424087780c9b71cc96799eaeddff35af2bc513278cda5c99fc1f5d026d3c1" +checksum = "9410d0f6853b1d94f0e519fb95df60f29d2c1eff2d921ffdf01a4c8a3b54f12d" [[package]] name = "version_check" @@ -364,6 +394,17 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasmtime-jit-icache-coherence" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6bbabb309c06cc238ee91b1455b748c45f0bdcab0dda2c2db85b0a1e69fcb66" +dependencies = [ + "cfg-if", + "libc", + "windows-sys", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 0fdd5de118ccb..2b216ca072f00 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,17 +3,24 @@ name = "rustc_codegen_cranelift" version = "0.1.0" edition = "2021" +[[bin]] +# This is used just to teach rust-analyzer how to check the build system. required-features is used +# to disable it for regular builds. +name = "y" +path = "./y.rs" +required-features = ["__check_build_system_using_ra"] + [lib] crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { version = "0.88.1", features = ["unwind", "all-arch"] } -cranelift-frontend = "0.88.1" -cranelift-module = "0.88.1" -cranelift-native = "0.88.1" -cranelift-jit = { version = "0.88.1", optional = true } -cranelift-object = "0.88.1" +cranelift-codegen = { version = "0.90.1", features = ["unwind", "all-arch"] } +cranelift-frontend = "0.90.1" +cranelift-module = "0.90.1" +cranelift-native = "0.90.1" +cranelift-jit = { version = "0.90.1", optional = true } +cranelift-object = "0.90.1" target-lexicon = "0.12.0" gimli = { version = "0.26.0", default-features = false, features = ["write"]} object = { version = "0.29.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] } @@ -39,6 +46,7 @@ smallvec = "1.8.1" unstable-features = ["jit", "inline_asm"] jit = ["cranelift-jit", "libloading"] inline_asm = [] +__check_build_system_using_ra = [] [package.metadata.rust-analyzer] rustc_private = true diff --git a/Readme.md b/Readme.md index 1e84c7fa3657b..0e9c77244d4cc 100644 --- a/Readme.md +++ b/Readme.md @@ -37,7 +37,7 @@ Assuming `$cg_clif_dir` is the directory you cloned this repo into and you follo In the directory with your project (where you can do the usual `cargo build`), run: ```bash -$ $cg_clif_dir/build/cargo-clif build +$ $cg_clif_dir/dist/cargo-clif build ``` This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend. diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index f6a9cb67290c7..bba3210536ef7 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -4,9 +4,9 @@ version = 3 [[package]] name = "addr2line" -version = "0.16.0" +version = "0.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd" +checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" dependencies = [ "compiler_builtins", "gimli", @@ -32,27 +32,11 @@ dependencies = [ "core", ] -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - [[package]] name = "cc" -version = "1.0.73" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2fff2a6927b3bb87f9595d67196a70493f627687a71d87a0d692242c33f58c11" - -[[package]] -name = "cfg-if" -version = "0.1.10" +version = "1.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -dependencies = [ - "compiler_builtins", - "rustc-std-workspace-core", -] +checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" [[package]] name = "cfg-if" @@ -66,9 +50,9 @@ dependencies = [ [[package]] name = "compiler_builtins" -version = "0.1.82" +version = "0.1.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18cd7635fea7bb481ea543b392789844c1ad581299da70184c7175ce3af76603" +checksum = "13e81c6cd7ab79f51a0c927d22858d61ad12bd0b3865f0b13ece02a4486aeabb" dependencies = [ "rustc-std-workspace-core", ] @@ -111,9 +95,9 @@ dependencies = [ [[package]] name = "gimli" -version = "0.25.0" +version = "0.26.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7" +checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d" dependencies = [ "compiler_builtins", "rustc-std-workspace-alloc", @@ -145,9 +129,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.135" +version = "0.2.138" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" +checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" dependencies = [ "rustc-std-workspace-core", ] @@ -164,12 +148,11 @@ dependencies = [ [[package]] name = "miniz_oxide" -version = "0.4.4" +version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b" +checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34" dependencies = [ "adler", - "autocfg", "compiler_builtins", "rustc-std-workspace-alloc", "rustc-std-workspace-core", @@ -177,9 +160,9 @@ dependencies = [ [[package]] name = "object" -version = "0.26.2" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39f37e50073ccad23b6d09bcb5b263f4e76d3bb6038e4a3c08e52162ffa8abc2" +checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53" dependencies = [ "compiler_builtins", "memchr", @@ -192,7 +175,7 @@ name = "panic_abort" version = "0.0.0" dependencies = [ "alloc", - "cfg-if 0.1.10", + "cfg-if", "compiler_builtins", "core", "libc", @@ -203,7 +186,7 @@ name = "panic_unwind" version = "0.0.0" dependencies = [ "alloc", - "cfg-if 0.1.10", + "cfg-if", "compiler_builtins", "core", "libc", @@ -255,7 +238,7 @@ version = "0.0.0" dependencies = [ "addr2line", "alloc", - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "core", "dlmalloc", @@ -277,7 +260,7 @@ dependencies = [ name = "std_detect" version = "0.1.5" dependencies = [ - "cfg-if 1.0.0", + "cfg-if", "compiler_builtins", "libc", "rustc-std-workspace-alloc", @@ -299,7 +282,7 @@ dependencies = [ name = "test" version = "0.0.0" dependencies = [ - "cfg-if 0.1.10", + "cfg-if", "core", "getopts", "libc", @@ -325,7 +308,7 @@ name = "unwind" version = "0.0.0" dependencies = [ "cc", - "cfg-if 0.1.10", + "cfg-if", "compiler_builtins", "core", "libc", diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index fae5b27163680..a081fdaa1c7e6 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -1,16 +1,21 @@ -use std::env; use std::path::Path; use super::build_sysroot; use super::config; -use super::prepare; -use super::utils::{cargo_command, spawn_and_wait}; +use super::path::Dirs; +use super::prepare::GitRepo; +use super::utils::{spawn_and_wait, CargoProject, Compiler}; use super::SysrootKind; +pub(crate) static ABI_CAFE_REPO: GitRepo = + GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); + +static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); + pub(crate) fn run( channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, + dirs: &Dirs, cg_clif_dylib: &Path, host_triple: &str, target_triple: &str, @@ -27,26 +32,25 @@ pub(crate) fn run( eprintln!("Building sysroot for abi-cafe"); build_sysroot::build_sysroot( + dirs, channel, sysroot_kind, - target_dir, cg_clif_dylib, host_triple, target_triple, ); eprintln!("Running abi-cafe"); - let abi_cafe_path = prepare::ABI_CAFE.source_dir(); - env::set_current_dir(abi_cafe_path.clone()).unwrap(); let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"]; - let mut cmd = cargo_command("cargo", "run", Some(target_triple), &abi_cafe_path); + let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs); cmd.arg("--"); cmd.arg("--pairs"); cmd.args(pairs); cmd.arg("--add-rustc-codegen-backend"); cmd.arg(format!("cgclif:{}", cg_clif_dylib.display())); + cmd.current_dir(ABI_CAFE.source_dir(dirs)); spawn_and_wait(cmd); } diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index cda468bcfa2df..fde8ef424ccc5 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -1,16 +1,19 @@ use std::env; use std::path::PathBuf; +use super::path::{Dirs, RelPath}; use super::rustc_info::get_file_name; -use super::utils::{cargo_command, is_ci}; +use super::utils::{is_ci, CargoProject, Compiler}; + +static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif"); pub(crate) fn build_backend( + dirs: &Dirs, channel: &str, host_triple: &str, use_unstable_features: bool, ) -> PathBuf { - let source_dir = std::env::current_dir().unwrap(); - let mut cmd = cargo_command("cargo", "build", Some(host_triple), &source_dir); + let mut cmd = CG_CLIF.build(&Compiler::host(), dirs); cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode @@ -41,8 +44,8 @@ pub(crate) fn build_backend( eprintln!("[BUILD] rustc_codegen_cranelift"); super::utils::spawn_and_wait(cmd); - source_dir - .join("target") + CG_CLIF + .target_dir(dirs) .join(host_triple) .join(channel) .join(get_file_name("rustc_codegen_cranelift", "dylib")) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 856aecc49fd1c..cbbf09b9b97b8 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -1,57 +1,60 @@ use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::{self, Command}; +use super::path::{Dirs, RelPath}; use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name}; -use super::utils::{cargo_command, spawn_and_wait, try_hard_link}; +use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; +static DIST_DIR: RelPath = RelPath::DIST; +static BIN_DIR: RelPath = RelPath::DIST.join("bin"); +static LIB_DIR: RelPath = RelPath::DIST.join("lib"); +static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib"); + pub(crate) fn build_sysroot( + dirs: &Dirs, channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, cg_clif_dylib_src: &Path, host_triple: &str, target_triple: &str, ) { eprintln!("[BUILD] sysroot {:?}", sysroot_kind); - if target_dir.exists() { - fs::remove_dir_all(target_dir).unwrap(); - } - fs::create_dir_all(target_dir.join("bin")).unwrap(); - fs::create_dir_all(target_dir.join("lib")).unwrap(); + DIST_DIR.ensure_fresh(dirs); + BIN_DIR.ensure_exists(dirs); + LIB_DIR.ensure_exists(dirs); // Copy the backend - let cg_clif_dylib_path = target_dir - .join(if cfg!(windows) { - // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the - // binaries. - "bin" - } else { - "lib" - }) - .join(get_file_name("rustc_codegen_cranelift", "dylib")); + let cg_clif_dylib_path = if cfg!(windows) { + // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the + // binaries. + BIN_DIR + } else { + LIB_DIR + } + .to_path(dirs) + .join(get_file_name("rustc_codegen_cranelift", "dylib")); try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path); // Build and copy rustc and cargo wrappers - for wrapper in ["rustc-clif", "cargo-clif"] { + for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] { let wrapper_name = get_wrapper_file_name(wrapper, "bin"); let mut build_cargo_wrapper_cmd = Command::new("rustc"); build_cargo_wrapper_cmd - .arg(PathBuf::from("scripts").join(format!("{wrapper}.rs"))) + .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs"))) .arg("-o") - .arg(target_dir.join(wrapper_name)) + .arg(DIST_DIR.to_path(dirs).join(wrapper_name)) .arg("-g"); spawn_and_wait(build_cargo_wrapper_cmd); } let default_sysroot = super::rustc_info::get_default_sysroot(); - let rustlib = target_dir.join("lib").join("rustlib"); - let host_rustlib_lib = rustlib.join(host_triple).join("lib"); - let target_rustlib_lib = rustlib.join(target_triple).join("lib"); + let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib"); + let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); @@ -112,24 +115,18 @@ pub(crate) fn build_sysroot( } } SysrootKind::Clif => { - build_clif_sysroot_for_triple( - channel, - target_dir, - host_triple, - &cg_clif_dylib_path, - None, - ); + build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None); if host_triple != target_triple { // When cross-compiling it is often necessary to manually pick the right linker - let linker = if target_triple == "aarch64-unknown-linux-gnu" { - Some("aarch64-linux-gnu-gcc") - } else { - None + let linker = match target_triple { + "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), + "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"), + _ => None, }; build_clif_sysroot_for_triple( + dirs, channel, - target_dir, target_triple, &cg_clif_dylib_path, linker, @@ -142,21 +139,26 @@ pub(crate) fn build_sysroot( let file = file.unwrap().path(); let filename = file.file_name().unwrap().to_str().unwrap(); if filename.contains("std-") && !filename.contains(".rlib") { - try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap())); + try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap())); } } } } } +// FIXME move to download/ or dist/ +pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version"); +pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src"); +static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot"); + fn build_clif_sysroot_for_triple( + dirs: &Dirs, channel: &str, - target_dir: &Path, triple: &str, cg_clif_dylib_path: &Path, linker: Option<&str>, ) { - match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) { + match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { eprintln!("Failed to get rustc version for patched sysroot source: {}", e); eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source"); @@ -174,7 +176,7 @@ fn build_clif_sysroot_for_triple( } } - let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel); + let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel); if !super::config::get_bool("keep_sysroot") { // Cleanup the deps dir, but keep build scripts and the incremental cache for faster @@ -185,27 +187,27 @@ fn build_clif_sysroot_for_triple( } // Build sysroot - let mut build_cmd = cargo_command("cargo", "build", Some(triple), Path::new("build_sysroot")); let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap())); - rustflags.push_str(&format!(" --sysroot={}", target_dir.to_str().unwrap())); + rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap())); if channel == "release" { - build_cmd.arg("--release"); rustflags.push_str(" -Zmir-opt-level=3"); } if let Some(linker) = linker { use std::fmt::Write; write!(rustflags, " -Clinker={}", linker).unwrap(); } - build_cmd.env("RUSTFLAGS", rustflags); + let mut compiler = Compiler::with_triple(triple.to_owned()); + compiler.rustflags = rustflags; + let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); + if channel == "release" { + build_cmd.arg("--release"); + } build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); // Copy all relevant files to the sysroot - for entry in - fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps")) - .unwrap() - { + for entry in fs::read_dir(build_dir.join("deps")).unwrap() { let entry = entry.unwrap(); if let Some(ext) = entry.path().extension() { if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" { @@ -216,7 +218,7 @@ fn build_clif_sysroot_for_triple( }; try_hard_link( entry.path(), - target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()), + RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()), ); } } diff --git a/build_system/mod.rs b/build_system/mod.rs index b25270d832ceb..1afc9a55c73b5 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -8,20 +8,37 @@ mod abi_cafe; mod build_backend; mod build_sysroot; mod config; +mod path; mod prepare; mod rustc_info; mod tests; mod utils; +const USAGE: &str = r#"The build system of cg_clif. + +USAGE: + ./y.rs prepare [--out-dir DIR] + ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + +OPTIONS: + --sysroot none|clif|llvm + Which sysroot libraries to use: + `none` will not include any standard library in the sysroot. + `clif` will build the standard library using Cranelift. + `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM. + + --out-dir DIR + Specify the directory in which the download, build and dist directories are stored. + By default this is the working directory. + + --no-unstable-features + fSome features are not yet ready for production usage. This option will disable these + features. This includes the JIT mode and inline assembly support. +"#; + fn usage() { - eprintln!("Usage:"); - eprintln!(" ./y.rs prepare"); - eprintln!( - " ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" - ); - eprintln!( - " ./y.rs test [--debug] [--sysroot none|clif|llvm] [--target-dir DIR] [--no-unstable-features]" - ); + eprintln!("{USAGE}"); } macro_rules! arg_error { @@ -34,6 +51,7 @@ macro_rules! arg_error { #[derive(PartialEq, Debug)] enum Command { + Prepare, Build, Test, } @@ -48,8 +66,6 @@ pub(crate) enum SysrootKind { pub fn main() { env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); - // The target dir is expected in the default location. Guard against the user changing it. - env::set_var("CARGO_TARGET_DIR", "target"); if is_ci() { // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway @@ -58,13 +74,7 @@ pub fn main() { let mut args = env::args().skip(1); let command = match args.next().as_deref() { - Some("prepare") => { - if args.next().is_some() { - arg_error!("./y.rs prepare doesn't expect arguments"); - } - prepare::prepare(); - process::exit(0); - } + Some("prepare") => Command::Prepare, Some("build") => Command::Build, Some("test") => Command::Test, Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), @@ -75,15 +85,15 @@ pub fn main() { } }; - let mut target_dir = PathBuf::from("build"); + let mut out_dir = PathBuf::from("."); let mut channel = "release"; let mut sysroot_kind = SysrootKind::Clif; let mut use_unstable_features = true; while let Some(arg) = args.next().as_deref() { match arg { - "--target-dir" => { - target_dir = PathBuf::from(args.next().unwrap_or_else(|| { - arg_error!("--target-dir requires argument"); + "--out-dir" => { + out_dir = PathBuf::from(args.next().unwrap_or_else(|| { + arg_error!("--out-dir requires argument"); })) } "--debug" => channel = "debug", @@ -101,7 +111,6 @@ pub fn main() { arg => arg_error!("Unexpected argument {}", arg), } } - target_dir = std::env::current_dir().unwrap().join(target_dir); let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { host_triple @@ -122,13 +131,43 @@ pub fn main() { host_triple.clone() }; - let cg_clif_dylib = build_backend::build_backend(channel, &host_triple, use_unstable_features); + // FIXME allow changing the location of these dirs using cli arguments + let current_dir = std::env::current_dir().unwrap(); + out_dir = current_dir.join(out_dir); + let dirs = path::Dirs { + source_dir: current_dir.clone(), + download_dir: out_dir.join("download"), + build_dir: out_dir.join("build"), + dist_dir: out_dir.join("dist"), + }; + + path::RelPath::BUILD.ensure_exists(&dirs); + + { + // Make sure we always explicitly specify the target dir + let target = + path::RelPath::BUILD.join("target_dir_should_be_set_explicitly").to_path(&dirs); + env::set_var("CARGO_TARGET_DIR", &target); + let _ = std::fs::remove_file(&target); + std::fs::File::create(target).unwrap(); + } + + if command == Command::Prepare { + prepare::prepare(&dirs); + process::exit(0); + } + + let cg_clif_dylib = + build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features); match command { + Command::Prepare => { + // Handled above + } Command::Test => { tests::run_tests( + &dirs, channel, sysroot_kind, - &target_dir, &cg_clif_dylib, &host_triple, &target_triple, @@ -137,7 +176,7 @@ pub fn main() { abi_cafe::run( channel, sysroot_kind, - &target_dir, + &dirs, &cg_clif_dylib, &host_triple, &target_triple, @@ -145,9 +184,9 @@ pub fn main() { } Command::Build => { build_sysroot::build_sysroot( + &dirs, channel, sysroot_kind, - &target_dir, &cg_clif_dylib, &host_triple, &target_triple, diff --git a/build_system/path.rs b/build_system/path.rs new file mode 100644 index 0000000000000..e93981f1d64d3 --- /dev/null +++ b/build_system/path.rs @@ -0,0 +1,70 @@ +use std::fs; +use std::path::PathBuf; + +#[derive(Debug, Clone)] +pub(crate) struct Dirs { + pub(crate) source_dir: PathBuf, + pub(crate) download_dir: PathBuf, + pub(crate) build_dir: PathBuf, + pub(crate) dist_dir: PathBuf, +} + +#[doc(hidden)] +#[derive(Debug, Copy, Clone)] +pub(crate) enum PathBase { + Source, + Download, + Build, + Dist, +} + +impl PathBase { + fn to_path(self, dirs: &Dirs) -> PathBuf { + match self { + PathBase::Source => dirs.source_dir.clone(), + PathBase::Download => dirs.download_dir.clone(), + PathBase::Build => dirs.build_dir.clone(), + PathBase::Dist => dirs.dist_dir.clone(), + } + } +} + +#[derive(Debug, Copy, Clone)] +pub(crate) enum RelPath { + Base(PathBase), + Join(&'static RelPath, &'static str), +} + +impl RelPath { + pub(crate) const SOURCE: RelPath = RelPath::Base(PathBase::Source); + pub(crate) const DOWNLOAD: RelPath = RelPath::Base(PathBase::Download); + pub(crate) const BUILD: RelPath = RelPath::Base(PathBase::Build); + pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist); + + pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts"); + pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); + pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches"); + + pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath { + RelPath::Join(self, suffix) + } + + pub(crate) fn to_path(&self, dirs: &Dirs) -> PathBuf { + match self { + RelPath::Base(base) => base.to_path(dirs), + RelPath::Join(base, suffix) => base.to_path(dirs).join(suffix), + } + } + + pub(crate) fn ensure_exists(&self, dirs: &Dirs) { + fs::create_dir_all(self.to_path(dirs)).unwrap(); + } + + pub(crate) fn ensure_fresh(&self, dirs: &Dirs) { + let path = self.to_path(dirs); + if path.exists() { + fs::remove_dir_all(&path).unwrap(); + } + fs::create_dir_all(path).unwrap(); + } +} diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 3111f62f6c215..8ac67e8f94228 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -1,92 +1,75 @@ -use std::env; use std::ffi::OsStr; use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; +use super::build_sysroot::{SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; +use super::path::{Dirs, RelPath}; use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; -use super::utils::{cargo_command, copy_dir_recursively, spawn_and_wait}; - -pub(crate) const ABI_CAFE: GitRepo = - GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); - -pub(crate) const RAND: GitRepo = - GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); - -pub(crate) const REGEX: GitRepo = - GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); - -pub(crate) const PORTABLE_SIMD: GitRepo = GitRepo::github( - "rust-lang", - "portable-simd", - "d5cd4a8112d958bd3a252327e0d069a6363249bd", - "portable-simd", -); - -pub(crate) const SIMPLE_RAYTRACER: GitRepo = GitRepo::github( - "ebobby", - "simple-raytracer", - "804a7a21b9e673a482797aa289a18ed480e4d813", - "", -); - -pub(crate) fn prepare() { - if Path::new("download").exists() { - std::fs::remove_dir_all(Path::new("download")).unwrap(); +use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler}; + +pub(crate) fn prepare(dirs: &Dirs) { + if RelPath::DOWNLOAD.to_path(dirs).exists() { + std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); } - std::fs::create_dir_all(Path::new("download")).unwrap(); + std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); - prepare_sysroot(); + prepare_sysroot(dirs); // FIXME maybe install this only locally? eprintln!("[INSTALL] hyperfine"); - Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap(); + Command::new("cargo") + .arg("install") + .arg("hyperfine") + .env_remove("CARGO_TARGET_DIR") + .spawn() + .unwrap() + .wait() + .unwrap(); - ABI_CAFE.fetch(); - RAND.fetch(); - REGEX.fetch(); - PORTABLE_SIMD.fetch(); - SIMPLE_RAYTRACER.fetch(); + super::abi_cafe::ABI_CAFE_REPO.fetch(dirs); + super::tests::RAND_REPO.fetch(dirs); + super::tests::REGEX_REPO.fetch(dirs); + super::tests::PORTABLE_SIMD_REPO.fetch(dirs); + super::tests::SIMPLE_RAYTRACER_REPO.fetch(dirs); eprintln!("[LLVM BUILD] simple-raytracer"); - let build_cmd = cargo_command("cargo", "build", None, &SIMPLE_RAYTRACER.source_dir()); + let host_compiler = Compiler::host(); + let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler, dirs); spawn_and_wait(build_cmd); fs::copy( - SIMPLE_RAYTRACER - .source_dir() - .join("target") + super::tests::SIMPLE_RAYTRACER + .target_dir(dirs) + .join(&host_compiler.triple) .join("debug") .join(get_file_name("main", "bin")), - SIMPLE_RAYTRACER.source_dir().join(get_file_name("raytracer_cg_llvm", "bin")), + RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")), ) .unwrap(); } -fn prepare_sysroot() { +fn prepare_sysroot(dirs: &Dirs) { let rustc_path = get_rustc_path(); let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust"); - let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src"); + let sysroot_src = SYSROOT_SRC; assert!(sysroot_src_orig.exists()); - if sysroot_src.exists() { - fs::remove_dir_all(&sysroot_src).unwrap(); - } - fs::create_dir_all(sysroot_src.join("library")).unwrap(); + sysroot_src.ensure_fresh(dirs); + fs::create_dir_all(sysroot_src.to_path(dirs).join("library")).unwrap(); eprintln!("[COPY] sysroot src"); - copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library")); + copy_dir_recursively( + &sysroot_src_orig.join("library"), + &sysroot_src.to_path(dirs).join("library"), + ); let rustc_version = get_rustc_version(); - fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap(); + fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap(); eprintln!("[GIT] init"); - let mut git_init_cmd = Command::new("git"); - git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src); - spawn_and_wait(git_init_cmd); - - init_git_repo(&sysroot_src); + init_git_repo(&sysroot_src.to_path(dirs)); - apply_patches("sysroot", &sysroot_src); + apply_patches(dirs, "sysroot", &sysroot_src.to_path(dirs)); } pub(crate) struct GitRepo { @@ -100,7 +83,7 @@ enum GitRepoUrl { } impl GitRepo { - const fn github( + pub(crate) const fn github( user: &'static str, repo: &'static str, rev: &'static str, @@ -109,21 +92,25 @@ impl GitRepo { GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name } } - pub(crate) fn source_dir(&self) -> PathBuf { + pub(crate) const fn source_dir(&self) -> RelPath { match self.url { - GitRepoUrl::Github { user: _, repo } => { - std::env::current_dir().unwrap().join("download").join(repo) - } + GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo), } } - fn fetch(&self) { + fn fetch(&self, dirs: &Dirs) { match self.url { GitRepoUrl::Github { user, repo } => { - clone_repo_shallow_github(&self.source_dir(), user, repo, self.rev); + clone_repo_shallow_github( + dirs, + &self.source_dir().to_path(dirs), + user, + repo, + self.rev, + ); } } - apply_patches(self.patch_name, &self.source_dir()); + apply_patches(dirs, self.patch_name, &self.source_dir().to_path(dirs)); } } @@ -142,18 +129,16 @@ fn clone_repo(download_dir: &Path, repo: &str, rev: &str) { spawn_and_wait(checkout_cmd); } -fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: &str) { +fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: &str, rev: &str) { if cfg!(windows) { // Older windows doesn't have tar or curl by default. Fall back to using git. clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev); return; } - let downloads_dir = std::env::current_dir().unwrap().join("download"); - let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev); - let archive_file = downloads_dir.join(format!("{}.tar.gz", rev)); - let archive_dir = downloads_dir.join(format!("{}-{}", repo, rev)); + let archive_file = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}.tar.gz", rev)); + let archive_dir = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}-{}", repo, rev)); eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url); @@ -169,7 +154,7 @@ fn clone_repo_shallow_github(download_dir: &Path, user: &str, repo: &str, rev: & // Unpack tar archive let mut unpack_cmd = Command::new("tar"); - unpack_cmd.arg("xf").arg(&archive_file).current_dir(downloads_dir); + unpack_cmd.arg("xf").arg(&archive_file).current_dir(RelPath::DOWNLOAD.to_path(dirs)); spawn_and_wait(unpack_cmd); // Rename unpacked dir to the expected name @@ -191,12 +176,21 @@ fn init_git_repo(repo_dir: &Path) { spawn_and_wait(git_add_cmd); let mut git_commit_cmd = Command::new("git"); - git_commit_cmd.arg("commit").arg("-m").arg("Initial commit").arg("-q").current_dir(repo_dir); + git_commit_cmd + .arg("-c") + .arg("user.name=Dummy") + .arg("-c") + .arg("user.email=dummy@example.com") + .arg("commit") + .arg("-m") + .arg("Initial commit") + .arg("-q") + .current_dir(repo_dir); spawn_and_wait(git_commit_cmd); } -fn get_patches(source_dir: &Path, crate_name: &str) -> Vec { - let mut patches: Vec<_> = fs::read_dir(source_dir.join("patches")) +fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec { + let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs)) .unwrap() .map(|entry| entry.unwrap().path()) .filter(|path| path.extension() == Some(OsStr::new("patch"))) @@ -215,19 +209,27 @@ fn get_patches(source_dir: &Path, crate_name: &str) -> Vec { patches } -fn apply_patches(crate_name: &str, target_dir: &Path) { +fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) { if crate_name == "" { return; } - for patch in get_patches(&std::env::current_dir().unwrap(), crate_name) { + for patch in get_patches(dirs, crate_name) { eprintln!( "[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch.file_name().unwrap() ); let mut apply_patch_cmd = Command::new("git"); - apply_patch_cmd.arg("am").arg(patch).arg("-q").current_dir(target_dir); + apply_patch_cmd + .arg("-c") + .arg("user.name=Dummy") + .arg("-c") + .arg("user.email=dummy@example.com") + .arg("am") + .arg(patch) + .arg("-q") + .current_dir(target_dir); spawn_and_wait(apply_patch_cmd); } } diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs index 3c08b6fa3894d..8e5ab688e131b 100644 --- a/build_system/rustc_info.rs +++ b/build_system/rustc_info.rs @@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String { .to_owned() } +pub(crate) fn get_cargo_path() -> PathBuf { + let cargo_path = Command::new("rustup") + .stderr(Stdio::inherit()) + .args(&["which", "cargo"]) + .output() + .unwrap() + .stdout; + Path::new(String::from_utf8(cargo_path).unwrap().trim()).to_owned() +} + pub(crate) fn get_rustc_path() -> PathBuf { let rustc_path = Command::new("rustup") .stderr(Stdio::inherit()) @@ -33,6 +43,16 @@ pub(crate) fn get_rustc_path() -> PathBuf { Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned() } +pub(crate) fn get_rustdoc_path() -> PathBuf { + let rustc_path = Command::new("rustup") + .stderr(Stdio::inherit()) + .args(&["which", "rustdoc"]) + .output() + .unwrap() + .stdout; + Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned() +} + pub(crate) fn get_default_sysroot() -> PathBuf { let default_sysroot = Command::new("rustc") .stderr(Stdio::inherit()) diff --git a/build_system/tests.rs b/build_system/tests.rs index a414b60f4e06b..1c372736ed65d 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -1,15 +1,20 @@ use super::build_sysroot; use super::config; -use super::prepare; -use super::rustc_info::get_wrapper_file_name; -use super::utils::{cargo_command, hyperfine_command, spawn_and_wait, spawn_and_wait_with_input}; -use build_system::SysrootKind; +use super::path::{Dirs, RelPath}; +use super::prepare::GitRepo; +use super::rustc_info::{get_cargo_path, get_wrapper_file_name}; +use super::utils::{ + hyperfine_command, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, +}; +use super::SysrootKind; use std::env; use std::ffi::OsStr; use std::fs; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::process::Command; +static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example"); + struct TestCase { config: &'static str, func: &'static dyn Fn(&TestRunner), @@ -30,7 +35,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib,dylib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("build.example", &|runner| { @@ -39,7 +44,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("jit.mini_core_hello_world", &|runner| { @@ -51,7 +56,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--cfg", "jit", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); @@ -65,7 +70,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "--cfg", "jit", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); @@ -79,7 +84,7 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-g", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]); }), @@ -94,7 +99,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []); }), @@ -106,7 +111,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("issue_91827_extern_types", []); }), @@ -116,7 +121,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "lib", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("aot.alloc_example", &|runner| { @@ -125,7 +130,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("alloc_example", []); }), @@ -136,7 +141,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cprefer-dynamic", "example/std_example.rs", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); eprintln!("[JIT-lazy] std_example"); @@ -146,7 +151,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cprefer-dynamic", "example/std_example.rs", "--target", - &runner.host_triple, + &runner.target_compiler.triple, ]); }), TestCase::new("aot.std_example", &|runner| { @@ -155,7 +160,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("std_example", ["arg"]); }), @@ -167,7 +172,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("dst_field_align", []); }), @@ -178,7 +183,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("subslice-patterns-const-eval", []); }), @@ -189,7 +194,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("track-caller-attribute", []); }), @@ -200,7 +205,7 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "bin", "-Cpanic=abort", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("float-minmax-pass", []); }), @@ -210,205 +215,252 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "--crate-type", "bin", "--target", - &runner.target_triple, + &runner.target_compiler.triple, ]); runner.run_out_command("mod_bench", []); }), + TestCase::new("aot.issue-72793", &|runner| { + runner.run_rustc([ + "example/issue-72793.rs", + "--crate-type", + "bin", + "--target", + &runner.target_compiler.triple, + ]); + runner.run_out_command("issue-72793", []); + }), ]; +pub(crate) static RAND_REPO: GitRepo = + GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); + +static RAND: CargoProject = CargoProject::new(&RAND_REPO.source_dir(), "rand"); + +pub(crate) static REGEX_REPO: GitRepo = + GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); + +static REGEX: CargoProject = CargoProject::new(®EX_REPO.source_dir(), "regex"); + +pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( + "rust-lang", + "portable-simd", + "d5cd4a8112d958bd3a252327e0d069a6363249bd", + "portable-simd", +); + +static PORTABLE_SIMD: CargoProject = + CargoProject::new(&PORTABLE_SIMD_REPO.source_dir(), "portable_simd"); + +pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( + "ebobby", + "simple-raytracer", + "804a7a21b9e673a482797aa289a18ed480e4d813", + "", +); + +pub(crate) static SIMPLE_RAYTRACER: CargoProject = + CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); + +static LIBCORE_TESTS: CargoProject = + CargoProject::new(&RelPath::BUILD_SYSROOT.join("sysroot_src/library/core/tests"), "core_tests"); + const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ TestCase::new("test.rust-random/rand", &|runner| { - runner.in_dir(prepare::RAND.source_dir(), |runner| { - runner.run_cargo("clean", []); - - if runner.host_triple == runner.target_triple { - eprintln!("[TEST] rust-random/rand"); - runner.run_cargo("test", ["--workspace"]); - } else { - eprintln!("[AOT] rust-random/rand"); - runner.run_cargo("build", ["--workspace", "--tests"]); - } - }); + spawn_and_wait(RAND.clean(&runner.target_compiler.cargo, &runner.dirs)); + + if runner.is_native { + eprintln!("[TEST] rust-random/rand"); + let mut test_cmd = RAND.test(&runner.target_compiler, &runner.dirs); + test_cmd.arg("--workspace"); + spawn_and_wait(test_cmd); + } else { + eprintln!("[AOT] rust-random/rand"); + let mut build_cmd = RAND.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--workspace").arg("--tests"); + spawn_and_wait(build_cmd); + } }), TestCase::new("bench.simple-raytracer", &|runner| { - runner.in_dir(prepare::SIMPLE_RAYTRACER.source_dir(), |runner| { - let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); - - if runner.host_triple == runner.target_triple { - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); - let prepare = runner.cargo_command("clean", []); - - let llvm_build_cmd = cargo_command("cargo", "build", None, Path::new(".")); - - let cargo_clif = runner - .root_dir - .clone() - .join("build") - .join(get_wrapper_file_name("cargo-clif", "bin")); - let clif_build_cmd = cargo_command(cargo_clif, "build", None, Path::new(".")); - - let bench_compile = - hyperfine_command(1, run_runs, Some(prepare), llvm_build_cmd, clif_build_cmd); - - spawn_and_wait(bench_compile); - - eprintln!("[BENCH RUN] ebobby/simple-raytracer"); - fs::copy(PathBuf::from("./target/debug/main"), PathBuf::from("raytracer_cg_clif")) - .unwrap(); - - let bench_run = hyperfine_command( - 0, - run_runs, - None, - Command::new("./raytracer_cg_llvm"), - Command::new("./raytracer_cg_clif"), - ); - spawn_and_wait(bench_run); - } else { - runner.run_cargo("clean", []); - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); - eprintln!("[COMPILE] ebobby/simple-raytracer"); - runner.run_cargo("build", []); - eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)"); - } - }); + let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); + + if runner.is_native { + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); + let cargo_clif = RelPath::DIST + .to_path(&runner.dirs) + .join(get_wrapper_file_name("cargo-clif", "bin")); + let manifest_path = SIMPLE_RAYTRACER.manifest_path(&runner.dirs); + let target_dir = SIMPLE_RAYTRACER.target_dir(&runner.dirs); + + let clean_cmd = format!( + "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let llvm_build_cmd = format!( + "cargo build --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let clif_build_cmd = format!( + "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}", + cargo_clif = cargo_clif.display(), + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + + let bench_compile = + hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); + + spawn_and_wait(bench_compile); + + eprintln!("[BENCH RUN] ebobby/simple-raytracer"); + fs::copy( + target_dir.join("debug").join("main"), + RelPath::BUILD.to_path(&runner.dirs).join("raytracer_cg_clif"), + ) + .unwrap(); + + let mut bench_run = + hyperfine_command(0, run_runs, None, "./raytracer_cg_llvm", "./raytracer_cg_clif"); + bench_run.current_dir(RelPath::BUILD.to_path(&runner.dirs)); + spawn_and_wait(bench_run); + } else { + spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.target_compiler.cargo, &runner.dirs)); + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); + eprintln!("[COMPILE] ebobby/simple-raytracer"); + spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); + eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)"); + } }), TestCase::new("test.libcore", &|runner| { - runner.in_dir( - std::env::current_dir() - .unwrap() - .join("build_sysroot") - .join("sysroot_src") - .join("library") - .join("core") - .join("tests"), - |runner| { - runner.run_cargo("clean", []); - - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", []); - } else { - eprintln!("Cross-Compiling: Not running tests"); - runner.run_cargo("build", ["--tests"]); - } - }, - ); + spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs)); + + if runner.is_native { + spawn_and_wait(LIBCORE_TESTS.test(&runner.target_compiler, &runner.dirs)); + } else { + eprintln!("Cross-Compiling: Not running tests"); + let mut build_cmd = LIBCORE_TESTS.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--tests"); + spawn_and_wait(build_cmd); + } }), TestCase::new("test.regex-shootout-regex-dna", &|runner| { - runner.in_dir(prepare::REGEX.source_dir(), |runner| { - runner.run_cargo("clean", []); - - // newer aho_corasick versions throw a deprecation warning - let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags); - - let mut build_cmd = runner.cargo_command("build", ["--example", "shootout-regex-dna"]); - build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); - spawn_and_wait(build_cmd); - - if runner.host_triple == runner.target_triple { - let mut run_cmd = runner.cargo_command("run", ["--example", "shootout-regex-dna"]); - run_cmd.env("RUSTFLAGS", lint_rust_flags); - - let input = - fs::read_to_string(PathBuf::from("examples/regexdna-input.txt")).unwrap(); - let expected_path = PathBuf::from("examples/regexdna-output.txt"); - let expected = fs::read_to_string(&expected_path).unwrap(); - - let output = spawn_and_wait_with_input(run_cmd, input); - // Make sure `[codegen mono items] start` doesn't poison the diff - let output = output - .lines() - .filter(|line| !line.contains("codegen mono items")) - .chain(Some("")) // This just adds the trailing newline - .collect::>() - .join("\r\n"); - - let output_matches = expected.lines().eq(output.lines()); - if !output_matches { - let res_path = PathBuf::from("res.txt"); - fs::write(&res_path, &output).unwrap(); - - if cfg!(windows) { - println!("Output files don't match!"); - println!("Expected Output:\n{}", expected); - println!("Actual Output:\n{}", output); - } else { - let mut diff = Command::new("diff"); - diff.arg("-u"); - diff.arg(res_path); - diff.arg(expected_path); - spawn_and_wait(diff); - } - - std::process::exit(1); + spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo, &runner.dirs)); + + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); + + let mut build_cmd = REGEX.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--example").arg("shootout-regex-dna"); + build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); + spawn_and_wait(build_cmd); + + if runner.is_native { + let mut run_cmd = REGEX.run(&runner.target_compiler, &runner.dirs); + run_cmd.arg("--example").arg("shootout-regex-dna"); + run_cmd.env("RUSTFLAGS", lint_rust_flags); + + let input = fs::read_to_string( + REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-input.txt"), + ) + .unwrap(); + let expected_path = + REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-output.txt"); + let expected = fs::read_to_string(&expected_path).unwrap(); + + let output = spawn_and_wait_with_input(run_cmd, input); + // Make sure `[codegen mono items] start` doesn't poison the diff + let output = output + .lines() + .filter(|line| !line.contains("codegen mono items")) + .chain(Some("")) // This just adds the trailing newline + .collect::>() + .join("\r\n"); + + let output_matches = expected.lines().eq(output.lines()); + if !output_matches { + let res_path = REGEX.source_dir(&runner.dirs).join("res.txt"); + fs::write(&res_path, &output).unwrap(); + + if cfg!(windows) { + println!("Output files don't match!"); + println!("Expected Output:\n{}", expected); + println!("Actual Output:\n{}", output); + } else { + let mut diff = Command::new("diff"); + diff.arg("-u"); + diff.arg(res_path); + diff.arg(expected_path); + spawn_and_wait(diff); } + + std::process::exit(1); } - }); + } }), TestCase::new("test.regex", &|runner| { - runner.in_dir(prepare::REGEX.source_dir(), |runner| { - runner.run_cargo("clean", []); - - // newer aho_corasick versions throw a deprecation warning - let lint_rust_flags = format!("{} --cap-lints warn", runner.rust_flags); - - if runner.host_triple == runner.target_triple { - let mut run_cmd = runner.cargo_command( - "test", - [ - "--tests", - "--", - "--exclude-should-panic", - "--test-threads", - "1", - "-Zunstable-options", - "-q", - ], - ); - run_cmd.env("RUSTFLAGS", lint_rust_flags); - spawn_and_wait(run_cmd); - } else { - eprintln!("Cross-Compiling: Not running tests"); - let mut build_cmd = - runner.cargo_command("build", ["--tests", "--target", &runner.target_triple]); - build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); - spawn_and_wait(build_cmd); - } - }); + spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo, &runner.dirs)); + + // newer aho_corasick versions throw a deprecation warning + let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); + + if runner.is_native { + let mut run_cmd = REGEX.test(&runner.target_compiler, &runner.dirs); + run_cmd.args([ + "--tests", + "--", + "--exclude-should-panic", + "--test-threads", + "1", + "-Zunstable-options", + "-q", + ]); + run_cmd.env("RUSTFLAGS", lint_rust_flags); + spawn_and_wait(run_cmd); + } else { + eprintln!("Cross-Compiling: Not running tests"); + let mut build_cmd = REGEX.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--tests"); + build_cmd.env("RUSTFLAGS", lint_rust_flags.clone()); + spawn_and_wait(build_cmd); + } }), TestCase::new("test.portable-simd", &|runner| { - runner.in_dir(prepare::PORTABLE_SIMD.source_dir(), |runner| { - runner.run_cargo("clean", []); - runner.run_cargo("build", ["--all-targets", "--target", &runner.target_triple]); + spawn_and_wait(PORTABLE_SIMD.clean(&runner.host_compiler.cargo, &runner.dirs)); - if runner.host_triple == runner.target_triple { - runner.run_cargo("test", ["-q"]); - } - }); + let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs); + build_cmd.arg("--all-targets"); + spawn_and_wait(build_cmd); + + if runner.is_native { + let mut test_cmd = PORTABLE_SIMD.test(&runner.target_compiler, &runner.dirs); + test_cmd.arg("-q"); + spawn_and_wait(test_cmd); + } }), ]; pub(crate) fn run_tests( + dirs: &Dirs, channel: &str, sysroot_kind: SysrootKind, - target_dir: &Path, cg_clif_dylib: &Path, host_triple: &str, target_triple: &str, ) { - let runner = TestRunner::new(host_triple.to_string(), target_triple.to_string()); + let runner = TestRunner::new(dirs.clone(), host_triple.to_string(), target_triple.to_string()); if config::get_bool("testsuite.no_sysroot") { build_sysroot::build_sysroot( + dirs, channel, SysrootKind::None, - &target_dir, cg_clif_dylib, &host_triple, &target_triple, ); - let _ = fs::remove_dir_all(Path::new("target").join("out")); + BUILD_EXAMPLE_OUT_DIR.ensure_fresh(dirs); runner.run_testsuite(NO_SYSROOT_SUITE); } else { eprintln!("[SKIP] no_sysroot tests"); @@ -419,9 +471,9 @@ pub(crate) fn run_tests( if run_base_sysroot || run_extended_sysroot { build_sysroot::build_sysroot( + dirs, channel, sysroot_kind, - &target_dir, cg_clif_dylib, &host_triple, &target_triple, @@ -442,40 +494,50 @@ pub(crate) fn run_tests( } struct TestRunner { - root_dir: PathBuf, - out_dir: PathBuf, + is_native: bool, jit_supported: bool, - rust_flags: String, - run_wrapper: Vec, - host_triple: String, - target_triple: String, + dirs: Dirs, + host_compiler: Compiler, + target_compiler: Compiler, } impl TestRunner { - pub fn new(host_triple: String, target_triple: String) -> Self { - let root_dir = env::current_dir().unwrap(); - - let mut out_dir = root_dir.clone(); - out_dir.push("target"); - out_dir.push("out"); - + pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self { let is_native = host_triple == target_triple; let jit_supported = target_triple.contains("x86_64") && is_native && !host_triple.contains("windows"); - let mut rust_flags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); - let mut run_wrapper = Vec::new(); + let rustc_clif = + RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustc-clif", "bin")); + let rustdoc_clif = + RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustdoc-clif", "bin")); + + let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); + let mut runner = vec![]; if !is_native { match target_triple.as_str() { "aarch64-unknown-linux-gnu" => { // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - rust_flags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rust_flags); - run_wrapper = vec!["qemu-aarch64", "-L", "/usr/aarch64-linux-gnu"]; + rustflags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rustflags); + runner = vec![ + "qemu-aarch64".to_owned(), + "-L".to_owned(), + "/usr/aarch64-linux-gnu".to_owned(), + ]; + } + "s390x-unknown-linux-gnu" => { + // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. + rustflags = format!("-Clinker=s390x-linux-gnu-gcc{}", rustflags); + runner = vec![ + "qemu-s390x".to_owned(), + "-L".to_owned(), + "/usr/s390x-linux-gnu".to_owned(), + ]; } "x86_64-pc-windows-gnu" => { // We are cross-compiling for Windows. Run tests in wine. - run_wrapper = vec!["wine"]; + runner = vec!["wine".to_owned()]; } _ => { println!("Unknown non-native platform"); @@ -484,19 +546,31 @@ impl TestRunner { } // FIXME fix `#[linkage = "extern_weak"]` without this - if host_triple.contains("darwin") { - rust_flags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rust_flags); + if target_triple.contains("darwin") { + rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags); } - Self { - root_dir, - out_dir, - jit_supported, - rust_flags, - run_wrapper: run_wrapper.iter().map(|s| s.to_string()).collect(), - host_triple, - target_triple, - } + let host_compiler = Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif.clone(), + rustdoc: rustdoc_clif.clone(), + rustflags: String::new(), + rustdocflags: String::new(), + triple: host_triple, + runner: vec![], + }; + + let target_compiler = Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif, + rustdoc: rustdoc_clif, + rustflags: rustflags.clone(), + rustdocflags: rustflags, + triple: target_triple, + runner, + }; + + Self { is_native, jit_supported, dirs, host_compiler, target_compiler } } pub fn run_testsuite(&self, tests: &[TestCase]) { @@ -516,29 +590,18 @@ impl TestRunner { } } - fn in_dir(&self, new: impl AsRef, callback: impl FnOnce(&TestRunner)) { - let current = env::current_dir().unwrap(); - - env::set_current_dir(new).unwrap(); - callback(self); - env::set_current_dir(current).unwrap(); - } - + #[must_use] fn rustc_command(&self, args: I) -> Command where I: IntoIterator, S: AsRef, { - let mut rustc_clif = self.root_dir.clone(); - rustc_clif.push("build"); - rustc_clif.push(get_wrapper_file_name("rustc-clif", "bin")); - - let mut cmd = Command::new(rustc_clif); - cmd.args(self.rust_flags.split_whitespace()); + let mut cmd = Command::new(&self.target_compiler.rustc); + cmd.args(self.target_compiler.rustflags.split_whitespace()); cmd.arg("-L"); - cmd.arg(format!("crate={}", self.out_dir.display())); + cmd.arg(format!("crate={}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display())); cmd.arg("--out-dir"); - cmd.arg(format!("{}", self.out_dir.display())); + cmd.arg(format!("{}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display())); cmd.arg("-Cdebuginfo=2"); cmd.args(args); cmd @@ -559,15 +622,13 @@ impl TestRunner { let mut full_cmd = vec![]; // Prepend the RUN_WRAPPER's - if !self.run_wrapper.is_empty() { - full_cmd.extend(self.run_wrapper.iter().cloned()); + if !self.target_compiler.runner.is_empty() { + full_cmd.extend(self.target_compiler.runner.iter().cloned()); } - full_cmd.push({ - let mut out_path = self.out_dir.clone(); - out_path.push(name); - out_path.to_str().unwrap().to_string() - }); + full_cmd.push( + BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).join(name).to_str().unwrap().to_string(), + ); for arg in args.into_iter() { full_cmd.push(arg.to_string()); @@ -581,30 +642,4 @@ impl TestRunner { spawn_and_wait(cmd); } - - fn cargo_command<'a, I>(&self, subcommand: &str, args: I) -> Command - where - I: IntoIterator, - { - let mut cargo_clif = self.root_dir.clone(); - cargo_clif.push("build"); - cargo_clif.push(get_wrapper_file_name("cargo-clif", "bin")); - - let mut cmd = cargo_command( - cargo_clif, - subcommand, - if subcommand == "clean" { None } else { Some(&self.target_triple) }, - Path::new("."), - ); - cmd.args(args); - cmd.env("RUSTFLAGS", &self.rust_flags); - cmd - } - - fn run_cargo<'a, I>(&self, subcommand: &str, args: I) - where - I: IntoIterator, - { - spawn_and_wait(self.cargo_command(subcommand, args)); - } } diff --git a/build_system/utils.rs b/build_system/utils.rs index c627af4e62fe1..2be70e8e421b2 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -1,35 +1,138 @@ use std::env; use std::fs; use std::io::Write; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; -pub(crate) fn cargo_command( - cargo: impl AsRef, - subcommand: &str, - triple: Option<&str>, - source_dir: &Path, -) -> Command { - let mut cmd = Command::new(cargo.as_ref()); - cmd.arg(subcommand) - .arg("--manifest-path") - .arg(source_dir.join("Cargo.toml")) - .arg("--target-dir") - .arg(source_dir.join("target")); +use super::path::{Dirs, RelPath}; +use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path}; + +pub(crate) struct Compiler { + pub(crate) cargo: PathBuf, + pub(crate) rustc: PathBuf, + pub(crate) rustdoc: PathBuf, + pub(crate) rustflags: String, + pub(crate) rustdocflags: String, + pub(crate) triple: String, + pub(crate) runner: Vec, +} + +impl Compiler { + pub(crate) fn host() -> Compiler { + Compiler { + cargo: get_cargo_path(), + rustc: get_rustc_path(), + rustdoc: get_rustdoc_path(), + rustflags: String::new(), + rustdocflags: String::new(), + triple: get_host_triple(), + runner: vec![], + } + } + + pub(crate) fn with_triple(triple: String) -> Compiler { + Compiler { + cargo: get_cargo_path(), + rustc: get_rustc_path(), + rustdoc: get_rustdoc_path(), + rustflags: String::new(), + rustdocflags: String::new(), + triple, + runner: vec![], + } + } +} + +pub(crate) struct CargoProject { + source: &'static RelPath, + target: &'static str, +} + +impl CargoProject { + pub(crate) const fn new(path: &'static RelPath, target: &'static str) -> CargoProject { + CargoProject { source: path, target } + } + + pub(crate) fn source_dir(&self, dirs: &Dirs) -> PathBuf { + self.source.to_path(dirs) + } + + pub(crate) fn manifest_path(&self, dirs: &Dirs) -> PathBuf { + self.source_dir(dirs).join("Cargo.toml") + } + + pub(crate) fn target_dir(&self, dirs: &Dirs) -> PathBuf { + RelPath::BUILD.join(self.target).to_path(dirs) + } - if let Some(triple) = triple { - cmd.arg("--target").arg(triple); + fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command { + let mut cmd = Command::new(cargo); + + cmd.arg(command) + .arg("--manifest-path") + .arg(self.manifest_path(dirs)) + .arg("--target-dir") + .arg(self.target_dir(dirs)); + + cmd + } + + fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command { + let mut cmd = self.base_cmd(command, &compiler.cargo, dirs); + + cmd.arg("--target").arg(&compiler.triple); + + cmd.env("RUSTC", &compiler.rustc); + cmd.env("RUSTDOC", &compiler.rustdoc); + cmd.env("RUSTFLAGS", &compiler.rustflags); + cmd.env("RUSTDOCFLAGS", &compiler.rustdocflags); + if !compiler.runner.is_empty() { + cmd.env( + format!("CARGO_TARGET_{}_RUNNER", compiler.triple.to_uppercase().replace('-', "_")), + compiler.runner.join(" "), + ); + } + + cmd } - cmd + #[must_use] + pub(crate) fn fetch(&self, cargo: impl AsRef, dirs: &Dirs) -> Command { + let mut cmd = Command::new(cargo.as_ref()); + + cmd.arg("fetch").arg("--manifest-path").arg(self.manifest_path(dirs)); + + cmd + } + + #[must_use] + pub(crate) fn clean(&self, cargo: &Path, dirs: &Dirs) -> Command { + self.base_cmd("clean", cargo, dirs) + } + + #[must_use] + pub(crate) fn build(&self, compiler: &Compiler, dirs: &Dirs) -> Command { + self.build_cmd("build", compiler, dirs) + } + + #[must_use] + pub(crate) fn test(&self, compiler: &Compiler, dirs: &Dirs) -> Command { + self.build_cmd("test", compiler, dirs) + } + + #[must_use] + pub(crate) fn run(&self, compiler: &Compiler, dirs: &Dirs) -> Command { + self.build_cmd("run", compiler, dirs) + } } +#[must_use] pub(crate) fn hyperfine_command( warmup: u64, runs: u64, - prepare: Option, - a: Command, - b: Command, + prepare: Option<&str>, + a: &str, + b: &str, ) -> Command { let mut bench = Command::new("hyperfine"); @@ -42,10 +145,10 @@ pub(crate) fn hyperfine_command( } if let Some(prepare) = prepare { - bench.arg("--prepare").arg(format!("{:?}", prepare)); + bench.arg("--prepare").arg(prepare); } - bench.arg(format!("{:?}", a)).arg(format!("{:?}", b)); + bench.arg(a).arg(b); bench } diff --git a/clean_all.sh b/clean_all.sh index fedab2433aa05..1760e5836ecce 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -2,7 +2,7 @@ set -e rm -rf build_sysroot/{sysroot_src/,target/,compiler-builtins/,rustc_version} -rm -rf target/ build/ perf.data{,.old} y.bin +rm -rf target/ build/ dist/ perf.data{,.old} y.bin rm -rf download/ # Kept for now in case someone updates their checkout of cg_clif before running clean_all.sh diff --git a/config.txt b/config.txt index 0d539191b12f9..258b67e931476 100644 --- a/config.txt +++ b/config.txt @@ -40,6 +40,7 @@ aot.subslice-patterns-const-eval aot.track-caller-attribute aot.float-minmax-pass aot.mod_bench +aot.issue-72793 testsuite.extended_sysroot test.rust-random/rand diff --git a/docs/usage.md b/docs/usage.md index 33f146e7ba27a..4c2b0fa170498 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -9,7 +9,7 @@ Assuming `$cg_clif_dir` is the directory you cloned this repo into and you follo In the directory with your project (where you can do the usual `cargo build`), run: ```bash -$ $cg_clif_dir/build/cargo-clif build +$ $cg_clif_dir/dist/cargo-clif build ``` This will build your project with rustc_codegen_cranelift instead of the usual LLVM backend. @@ -19,7 +19,7 @@ This will build your project with rustc_codegen_cranelift instead of the usual L > You should prefer using the Cargo method. ```bash -$ $cg_clif_dir/build/rustc-clif my_crate.rs +$ $cg_clif_dir/dist/rustc-clif my_crate.rs ``` ## Jit mode @@ -32,20 +32,20 @@ In jit mode cg_clif will immediately execute your code without creating an execu > The jit mode will probably need cargo integration to make this possible. ```bash -$ $cg_clif_dir/build/cargo-clif jit +$ $cg_clif_dir/dist/cargo-clif jit ``` or ```bash -$ $cg_clif_dir/build/rustc-clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs +$ $cg_clif_dir/dist/rustc-clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs ``` There is also an experimental lazy jit mode. In this mode functions are only compiled once they are first called. ```bash -$ $cg_clif_dir/build/cargo-clif lazy-jit +$ $cg_clif_dir/dist/cargo-clif lazy-jit ``` ## Shell @@ -54,7 +54,7 @@ These are a few functions that allow you to easily run rust code from the shell ```bash function jit_naked() { - echo "$@" | $cg_clif_dir/build/rustc-clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic + echo "$@" | $cg_clif_dir/dist/rustc-clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic } function jit() { diff --git a/example/issue-72793.rs b/example/issue-72793.rs new file mode 100644 index 0000000000000..b1bb9b8e1e730 --- /dev/null +++ b/example/issue-72793.rs @@ -0,0 +1,24 @@ +// Adapted from rustc ui test suite (ui/type-alias-impl-trait/issue-72793.rs) + +#![feature(type_alias_impl_trait)] + +trait T { type Item; } + +type Alias<'a> = impl T; + +struct S; +impl<'a> T for &'a S { + type Item = &'a (); +} + +fn filter_positive<'a>() -> Alias<'a> { + &S +} + +fn with_positive(fun: impl Fn(Alias<'_>)) { + fun(filter_positive()); +} + +fn main() { + with_positive(|_| ()); +} diff --git a/example/mini_core.rs b/example/mini_core.rs index 7f85b52f083a7..1f9db1eb2a97a 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -19,6 +19,9 @@ pub trait Sized {} #[lang = "destruct"] pub trait Destruct {} +#[lang = "tuple_trait"] +pub trait Tuple {} + #[lang = "unsize"] pub trait Unsize {} @@ -443,7 +446,7 @@ pub struct PhantomData; #[lang = "fn_once"] #[rustc_paren_sugar] -pub trait FnOnce { +pub trait FnOnce { #[lang = "fn_once_output"] type Output; @@ -452,7 +455,7 @@ pub trait FnOnce { #[lang = "fn_mut"] #[rustc_paren_sugar] -pub trait FnMut: FnOnce { +pub trait FnMut: FnOnce { extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output; } diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index 215d3556a17ca..c00f8a2e0cdad 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -171,8 +171,6 @@ fn main() { assert_eq!(slice_ptr as usize % 4, 0); - //return; - unsafe { printf("Hello %s\n\0" as *const str as *const i8, "printf\0" as *const str as *const i8); diff --git a/example/std_example.rs b/example/std_example.rs index ad108c34992e3..8481d9c39a3cf 100644 --- a/example/std_example.rs +++ b/example/std_example.rs @@ -164,6 +164,8 @@ unsafe fn test_simd() { let cmp_eq = _mm_cmpeq_epi8(y, y); let cmp_lt = _mm_cmplt_epi8(y, y); + let (zero0, zero1) = std::mem::transmute::<_, (u64, u64)>(x); + assert_eq!((zero0, zero1), (0, 0)); assert_eq!(std::mem::transmute::<_, [u16; 8]>(or), [7, 7, 7, 7, 7, 7, 7, 7]); assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_eq), [0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff]); assert_eq!(std::mem::transmute::<_, [u16; 8]>(cmp_lt), [0, 0, 0, 0, 0, 0, 0, 0]); diff --git a/rust-toolchain b/rust-toolchain index c0a2e7a7883fc..d8f28dbcc15c8 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-10-23" +channel = "nightly-2022-12-13" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] diff --git a/rustfmt.toml b/rustfmt.toml index 2bd8f7d1bc15d..ebeca8662a519 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,3 +1,5 @@ +ignore = ["y.rs"] + # Matches rustfmt.toml of rustc version = "Two" use_small_heuristics = "Max" diff --git a/scripts/filter_profile.rs b/scripts/filter_profile.rs index e6f60d1c0cb23..f782671fe36f9 100755 --- a/scripts/filter_profile.rs +++ b/scripts/filter_profile.rs @@ -2,7 +2,7 @@ #![forbid(unsafe_code)]/* This line is ignored by bash # This block is ignored by rustc pushd $(dirname "$0")/../ -RUSTC="$(pwd)/build/rustc-clif" +RUSTC="$(pwd)/dist/rustc-clif" popd PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0 #*/ diff --git a/scripts/rustdoc-clif.rs b/scripts/rustdoc-clif.rs new file mode 100644 index 0000000000000..a19d72acfa83e --- /dev/null +++ b/scripts/rustdoc-clif.rs @@ -0,0 +1,36 @@ +use std::env; +use std::ffi::OsString; +#[cfg(unix)] +use std::os::unix::process::CommandExt; +use std::path::PathBuf; +use std::process::Command; + +fn main() { + let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap()); + + let cg_clif_dylib_path = sysroot.join(if cfg!(windows) { "bin" } else { "lib" }).join( + env::consts::DLL_PREFIX.to_string() + "rustc_codegen_cranelift" + env::consts::DLL_SUFFIX, + ); + + let mut args = std::env::args_os().skip(1).collect::>(); + args.push(OsString::from("-Cpanic=abort")); + args.push(OsString::from("-Zpanic-abort-tests")); + let mut codegen_backend_arg = OsString::from("-Zcodegen-backend="); + codegen_backend_arg.push(cg_clif_dylib_path); + args.push(codegen_backend_arg); + if !args.contains(&OsString::from("--sysroot")) { + args.push(OsString::from("--sysroot")); + args.push(OsString::from(sysroot.to_str().unwrap())); + } + + // Ensure that the right toolchain is used + env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN")); + + #[cfg(unix)] + Command::new("rustdoc").args(args).exec(); + + #[cfg(not(unix))] + std::process::exit( + Command::new("rustdoc").args(args).spawn().unwrap().wait().unwrap().code().unwrap_or(1), + ); +} diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index d6a37789599fe..6c64b7de7daa1 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -27,24 +27,6 @@ index d95b5b7f17f..00b6f0e3635 100644 [dev-dependencies] rand = "0.7" rand_xorshift = "0.2" -diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs -index 8431aa7b818..a3ff7e68ce5 100644 ---- a/src/tools/compiletest/src/runtest.rs -+++ b/src/tools/compiletest/src/runtest.rs -@@ -3489,12 +3489,7 @@ fn normalize_output(&self, output: &str, custom_rules: &[(String, String)]) -> S - let compiler_src_dir = base_dir.join("compiler"); - normalize_path(&compiler_src_dir, "$(echo '$COMPILER_DIR')"); - -- if let Some(virtual_rust_source_base_dir) = -- option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR").map(PathBuf::from) -- { -- normalize_path(&virtual_rust_source_base_dir.join("library"), "$(echo '$SRC_DIR')"); -- normalize_path(&virtual_rust_source_base_dir.join("compiler"), "$(echo '$COMPILER_DIR')"); -- } -+ normalize_path(&Path::new("$(cd ../build_sysroot/sysroot_src/library; pwd)"), "$(echo '$SRC_DIR')"); - - // Paths into the build directory - let test_build_dir = &self.config.build_base; EOF cat > config.toml < CallCon pub(crate) fn get_function_sig<'tcx>( tcx: TyCtxt<'tcx>, - triple: &target_lexicon::Triple, + default_call_conv: CallConv, inst: Instance<'tcx>, ) -> Signature { assert!(!inst.substs.needs_infer()); clif_sig_from_fn_abi( tcx, - CallConv::triple_default(triple), + default_call_conv, &RevealAllLayoutCx(tcx).fn_abi_of_instance(inst, ty::List::empty()), ) } @@ -74,7 +74,7 @@ pub(crate) fn import_function<'tcx>( inst: Instance<'tcx>, ) -> FuncId { let name = tcx.symbol_name(inst).name; - let sig = get_function_sig(tcx, module.isa().triple(), inst); + let sig = get_function_sig(tcx, module.target_config().default_call_conv, inst); match module.declare_function(name, Linkage::Import, &sig) { Ok(func_id) => func_id, Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!( @@ -341,14 +341,13 @@ pub(crate) fn codegen_terminator_call<'tcx>( destination: Place<'tcx>, target: Option, ) { - 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 func = codegen_operand(fx, func); + let fn_sig = func.layout().ty.fn_sig(fx.tcx); let ret_place = codegen_place(fx, destination); // Handle special calls like intrinsics and empty drop glue. - let instance = if let ty::FnDef(def_id, substs) = *fn_ty.kind() { + let instance = if let ty::FnDef(def_id, substs) = *func.layout().ty.kind() { let instance = ty::Instance::expect_resolve(fx.tcx, ty::ParamEnv::reveal_all(), def_id, substs) .polymorphize(fx.tcx); @@ -390,17 +389,17 @@ pub(crate) fn codegen_terminator_call<'tcx>( None }; - let extra_args = &args[fn_sig.inputs().len()..]; + let extra_args = &args[fn_sig.inputs().skip_binder().len()..]; let extra_args = fx .tcx .mk_type_list(extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.ty(fx.mir, fx.tcx)))); let fn_abi = if let Some(instance) = instance { RevealAllLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args) } else { - RevealAllLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_ty.fn_sig(fx.tcx), extra_args) + RevealAllLayoutCx(fx.tcx).fn_abi_of_fn_ptr(fn_sig, extra_args) }; - let is_cold = if fn_sig.abi == Abi::RustCold { + let is_cold = if fn_sig.abi() == Abi::RustCold { true } else { instance @@ -417,7 +416,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( } // Unpack arguments tuple for closures - let mut args = if fn_sig.abi == Abi::RustCall { + let mut args = if fn_sig.abi() == Abi::RustCall { assert_eq!(args.len(), 2, "rust-call abi requires two arguments"); let self_arg = codegen_call_argument_operand(fx, &args[0]); let pack_arg = codegen_call_argument_operand(fx, &args[1]); @@ -485,7 +484,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( fx.add_comment(nop_inst, "indirect call"); } - let func = codegen_operand(fx, func).load_scalar(fx); + let func = func.load_scalar(fx); let sig = clif_sig_from_fn_abi(fx.tcx, fx.target_config.default_call_conv, &fn_abi); let sig = fx.bcx.import_signature(sig); @@ -516,11 +515,11 @@ pub(crate) fn codegen_terminator_call<'tcx>( }; // FIXME find a cleaner way to support varargs - if fn_sig.c_variadic { - if !matches!(fn_sig.abi, Abi::C { .. }) { + if fn_sig.c_variadic() { + if !matches!(fn_sig.abi(), Abi::C { .. }) { fx.tcx.sess.span_fatal( source_info.span, - &format!("Variadic call for non-C abi {:?}", fn_sig.abi), + &format!("Variadic call for non-C abi {:?}", fn_sig.abi()), ); } let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap(); diff --git a/src/allocator.rs b/src/allocator.rs index 12bb00d346db4..8508227179ac6 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -66,7 +66,7 @@ fn codegen_inner( }; let sig = Signature { - call_conv: CallConv::triple_default(module.isa().triple()), + call_conv: module.target_config().default_call_conv, params: arg_tys.iter().cloned().map(AbiParam::new).collect(), returns: output.into_iter().map(AbiParam::new).collect(), }; @@ -104,7 +104,7 @@ fn codegen_inner( } let sig = Signature { - call_conv: CallConv::triple_default(module.isa().triple()), + call_conv: module.target_config().default_call_conv, params: vec![AbiParam::new(usize_ty), AbiParam::new(usize_ty)], returns: vec![], }; diff --git a/src/base.rs b/src/base.rs index 06813d7ec953f..89d955e8bf2e1 100644 --- a/src/base.rs +++ b/src/base.rs @@ -59,7 +59,7 @@ pub(crate) fn codegen_fn<'tcx>( // Declare function let symbol_name = tcx.symbol_name(instance).name.to_string(); - let sig = get_function_sig(tcx, module.isa().triple(), instance); + let sig = get_function_sig(tcx, module.target_config().default_call_conv, instance); let func_id = module.declare_function(&symbol_name, Linkage::Local, &sig).unwrap(); // Make the FunctionBuilder @@ -390,11 +390,9 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { _ => unreachable!("{:?}", targets), }; - let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr); let (discr, is_inverted) = crate::optimize::peephole::maybe_unwrap_bool_not(&mut fx.bcx, discr); let test_zero = if is_inverted { !test_zero } else { test_zero }; - let discr = crate::optimize::peephole::maybe_unwrap_bint(&mut fx.bcx, discr); if let Some(taken) = crate::optimize::peephole::maybe_known_branch_taken( &fx.bcx, discr, test_zero, ) { @@ -571,7 +569,7 @@ fn codegen_stmt<'tcx>( UnOp::Not => match layout.ty.kind() { ty::Bool => { let res = fx.bcx.ins().icmp_imm(IntCC::Equal, val, 0); - CValue::by_val(fx.bcx.ins().bint(types::I8, res), layout) + CValue::by_val(res, layout) } ty::Uint(_) | ty::Int(_) => { CValue::by_val(fx.bcx.ins().bnot(val), layout) @@ -579,12 +577,6 @@ fn codegen_stmt<'tcx>( _ => unreachable!("un op Not for {:?}", layout.ty), }, UnOp::Neg => match layout.ty.kind() { - ty::Int(IntTy::I128) => { - // FIXME remove this case once ineg.i128 works - let zero = - CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size)); - crate::num::codegen_int_binop(fx, BinOp::Sub, zero, operand) - } ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout), ty::Float(_) => CValue::by_val(fx.bcx.ins().fneg(val), layout), _ => unreachable!("un op Neg for {:?}", layout.ty), diff --git a/src/cast.rs b/src/cast.rs index bad5d1f08a9cf..5091c5a9fedac 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -149,7 +149,7 @@ pub(crate) fn clif_int_or_float_cast( } let is_not_nan = fx.bcx.ins().fcmp(FloatCC::Equal, from, from); - let zero = fx.bcx.ins().iconst(to_ty, 0); + let zero = type_zero_value(&mut fx.bcx, to_ty); fx.bcx.ins().select(is_not_nan, val, zero) } else if from_ty.is_float() && to_ty.is_float() { // float -> float diff --git a/src/common.rs b/src/common.rs index 589594465783e..2dcd42fbd8f43 100644 --- a/src/common.rs +++ b/src/common.rs @@ -162,11 +162,20 @@ pub(crate) fn codegen_icmp_imm( } } } else { - let rhs = i64::try_from(rhs).expect("codegen_icmp_imm rhs out of range for <128bit int"); + let rhs = rhs as i64; // Truncates on purpose in case rhs is actually an unsigned value fx.bcx.ins().icmp_imm(intcc, lhs, rhs) } } +pub(crate) fn type_zero_value(bcx: &mut FunctionBuilder<'_>, ty: Type) -> Value { + if ty == types::I128 { + let zero = bcx.ins().iconst(types::I64, 0); + bcx.ins().iconcat(zero, zero) + } else { + bcx.ins().iconst(ty, 0) + } +} + pub(crate) fn type_min_max_value( bcx: &mut FunctionBuilder<'_>, ty: Type, diff --git a/src/constant.rs b/src/constant.rs index a6bde88408497..dee6fb5b5130d 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -28,9 +28,7 @@ impl ConstantCx { } 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); self.done.clear(); } } @@ -268,16 +266,7 @@ fn data_id_for_static( def_id: DefId, definition: bool, ) -> DataId { - let rlinkage = tcx.codegen_fn_attrs(def_id).linkage; - let linkage = if definition { - crate::linkage::get_static_linkage(tcx, def_id) - } else if rlinkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak) - || rlinkage == Some(rustc_middle::mir::mono::Linkage::WeakAny) - { - Linkage::Preemptible - } else { - Linkage::Import - }; + let attrs = tcx.codegen_fn_attrs(def_id); let instance = Instance::mono(tcx, def_id).polymorphize(tcx); let symbol_name = tcx.symbol_name(instance).name; @@ -289,22 +278,30 @@ fn data_id_for_static( }; let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes(); - let attrs = tcx.codegen_fn_attrs(def_id); + if let Some(import_linkage) = attrs.import_linkage { + assert!(!definition); - let data_id = match module.declare_data( - &*symbol_name, - linkage, - is_mutable, - attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL), - ) { - Ok(data_id) => data_id, - Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!( - "attempt to declare `{symbol_name}` as static, but it was already declared as function" - )), - Err(err) => Err::<_, _>(err).unwrap(), - }; + let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak + || import_linkage == rustc_middle::mir::mono::Linkage::WeakAny + { + Linkage::Preemptible + } else { + Linkage::Import + }; + + let data_id = match module.declare_data( + &*symbol_name, + linkage, + is_mutable, + attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL), + ) { + Ok(data_id) => data_id, + Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!( + "attempt to declare `{symbol_name}` as static, but it was already declared as function" + )), + Err(err) => Err::<_, _>(err).unwrap(), + }; - if rlinkage.is_some() { // Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141 // Declare an internal global `extern_with_linkage_foo` which // is initialized with the address of `foo`. If `foo` is @@ -326,10 +323,34 @@ fn data_id_for_static( Err(ModuleError::DuplicateDefinition(_)) => {} res => res.unwrap(), } - ref_data_id - } else { - data_id + + return ref_data_id; } + + let linkage = if definition { + crate::linkage::get_static_linkage(tcx, def_id) + } else if attrs.linkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak) + || attrs.linkage == Some(rustc_middle::mir::mono::Linkage::WeakAny) + { + Linkage::Preemptible + } else { + Linkage::Import + }; + + let data_id = match module.declare_data( + &*symbol_name, + linkage, + is_mutable, + attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL), + ) { + Ok(data_id) => data_id, + Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!( + "attempt to declare `{symbol_name}` as static, but it was already declared as function" + )), + Err(err) => Err::<_, _>(err).unwrap(), + }; + + data_id } fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut ConstantCx) { @@ -348,8 +369,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant (data_id, alloc, None) } TodoItem::Static(def_id) => { - //println!("static {:?}", def_id); - let section_name = tcx.codegen_fn_attrs(def_id).link_section; let alloc = tcx.eval_static_initializer(def_id).unwrap(); @@ -359,7 +378,6 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant } }; - //("data_id {}", data_id); if cx.done.contains(&data_id) { continue; } diff --git a/src/debuginfo/unwind.rs b/src/debuginfo/unwind.rs index d26392c4913b5..493359c743f11 100644 --- a/src/debuginfo/unwind.rs +++ b/src/debuginfo/unwind.rs @@ -39,7 +39,9 @@ impl UnwindContext { } pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) { - let unwind_info = if let Some(unwind_info) = context.create_unwind_info(isa).unwrap() { + let unwind_info = if let Some(unwind_info) = + context.compiled_code().unwrap().create_unwind_info(isa).unwrap() + { unwind_info } else { return; diff --git a/src/discriminant.rs b/src/discriminant.rs index 97b395bcd0518..3cbf313adf0df 100644 --- a/src/discriminant.rs +++ b/src/discriminant.rs @@ -1,6 +1,7 @@ //! Handling of enum discriminants //! -//! Adapted from +//! Adapted from +//! () use rustc_target::abi::{Int, TagEncoding, Variants}; @@ -47,13 +48,19 @@ pub(crate) fn codegen_set_discriminant<'tcx>( } => { if variant_index != untagged_variant { let niche = place.place_field(fx, mir::Field::new(tag_field)); + let niche_type = fx.clif_type(niche.layout().ty).unwrap(); let niche_value = variant_index.as_u32() - niche_variants.start().as_u32(); - let niche_value = ty::ScalarInt::try_from_uint( - u128::from(niche_value).wrapping_add(niche_start), - niche.layout().size, - ) - .unwrap(); - let niche_llval = CValue::const_val(fx, niche.layout(), niche_value); + let niche_value = (niche_value as u128).wrapping_add(niche_start); + let niche_value = match niche_type { + types::I128 => { + let lsb = fx.bcx.ins().iconst(types::I64, niche_value as u64 as i64); + let msb = + fx.bcx.ins().iconst(types::I64, (niche_value >> 64) as u64 as i64); + fx.bcx.ins().iconcat(lsb, msb) + } + ty => fx.bcx.ins().iconst(ty, niche_value as i64), + }; + let niche_llval = CValue::by_val(niche_value, niche.layout()); niche.write_cvalue(fx, niche_llval); } } @@ -96,6 +103,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>( } }; + let cast_to_size = dest_layout.layout.size(); let cast_to = fx.clif_type(dest_layout.ty).unwrap(); // Read the tag/niche-encoded discriminant from memory. @@ -114,21 +122,128 @@ pub(crate) fn codegen_get_discriminant<'tcx>( dest.write_cvalue(fx, res); } TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start } => { - // Rebase from niche values to discriminants, and check - // whether the result is in range for the niche variants. - - // We first compute the "relative discriminant" (wrt `niche_variants`), - // that is, if `n = niche_variants.end() - niche_variants.start()`, - // we remap `niche_start..=niche_start + n` (which may wrap around) - // to (non-wrap-around) `0..=n`, to be able to check whether the - // discriminant corresponds to a niche variant with one comparison. - // We also can't go directly to the (variant index) discriminant - // and check that it is in the range `niche_variants`, because - // that might not fit in the same type, on top of needing an extra - // comparison (see also the comment on `let niche_discr`). - let relative_discr = if niche_start == 0 { - tag + let tag_size = tag_scalar.size(fx); + let max_unsigned = tag_size.unsigned_int_max(); + let max_signed = tag_size.signed_int_max() as u128; + let min_signed = max_signed + 1; + let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32(); + let niche_end = niche_start.wrapping_add(relative_max as u128) & max_unsigned; + let range = tag_scalar.valid_range(fx); + + let sle = |lhs: u128, rhs: u128| -> bool { + // Signed and unsigned comparisons give the same results, + // except that in signed comparisons an integer with the + // sign bit set is less than one with the sign bit clear. + // Toggle the sign bit to do a signed comparison. + (lhs ^ min_signed) <= (rhs ^ min_signed) + }; + + // We have a subrange `niche_start..=niche_end` inside `range`. + // If the value of the tag is inside this subrange, it's a + // "niche value", an increment of the discriminant. Otherwise it + // indicates the untagged variant. + // A general algorithm to extract the discriminant from the tag + // is: + // relative_tag = tag - niche_start + // is_niche = relative_tag <= (ule) relative_max + // discr = if is_niche { + // cast(relative_tag) + niche_variants.start() + // } else { + // untagged_variant + // } + // However, we will likely be able to emit simpler code. + + // Find the least and greatest values in `range`, considered + // both as signed and unsigned. + let (low_unsigned, high_unsigned) = + if range.start <= range.end { (range.start, range.end) } else { (0, max_unsigned) }; + let (low_signed, high_signed) = if sle(range.start, range.end) { + (range.start, range.end) } else { + (min_signed, max_signed) + }; + + let niches_ule = niche_start <= niche_end; + let niches_sle = sle(niche_start, niche_end); + let cast_smaller = cast_to_size <= tag_size; + + // In the algorithm above, we can change + // cast(relative_tag) + niche_variants.start() + // into + // cast(tag + (niche_variants.start() - niche_start)) + // if either the casted type is no larger than the original + // type, or if the niche values are contiguous (in either the + // signed or unsigned sense). + let can_incr = cast_smaller || niches_ule || niches_sle; + + let data_for_boundary_niche = || -> Option<(IntCC, u128)> { + if !can_incr { + None + } else if niche_start == low_unsigned { + Some((IntCC::UnsignedLessThanOrEqual, niche_end)) + } else if niche_end == high_unsigned { + Some((IntCC::UnsignedGreaterThanOrEqual, niche_start)) + } else if niche_start == low_signed { + Some((IntCC::SignedLessThanOrEqual, niche_end)) + } else if niche_end == high_signed { + Some((IntCC::SignedGreaterThanOrEqual, niche_start)) + } else { + None + } + }; + + let (is_niche, tagged_discr, delta) = if relative_max == 0 { + // Best case scenario: only one tagged variant. This will + // likely become just a comparison and a jump. + // The algorithm is: + // is_niche = tag == niche_start + // discr = if is_niche { + // niche_start + // } else { + // untagged_variant + // } + let is_niche = codegen_icmp_imm(fx, IntCC::Equal, tag, niche_start as i128); + let tagged_discr = + fx.bcx.ins().iconst(cast_to, niche_variants.start().as_u32() as i64); + (is_niche, tagged_discr, 0) + } else if let Some((predicate, constant)) = data_for_boundary_niche() { + // The niche values are either the lowest or the highest in + // `range`. We can avoid the first subtraction in the + // algorithm. + // The algorithm is now this: + // is_niche = tag <= niche_end + // discr = if is_niche { + // cast(tag + (niche_variants.start() - niche_start)) + // } else { + // untagged_variant + // } + // (the first line may instead be tag >= niche_start, + // and may be a signed or unsigned comparison) + // The arithmetic must be done before the cast, so we can + // have the correct wrapping behavior. See issue #104519 for + // the consequences of getting this wrong. + let is_niche = codegen_icmp_imm(fx, predicate, tag, constant as i128); + let delta = (niche_variants.start().as_u32() as u128).wrapping_sub(niche_start); + let incr_tag = if delta == 0 { + tag + } else { + let delta = match fx.bcx.func.dfg.value_type(tag) { + types::I128 => { + let lsb = fx.bcx.ins().iconst(types::I64, delta as u64 as i64); + let msb = fx.bcx.ins().iconst(types::I64, (delta >> 64) as u64 as i64); + fx.bcx.ins().iconcat(lsb, msb) + } + ty => fx.bcx.ins().iconst(ty, delta as i64), + }; + fx.bcx.ins().iadd(tag, delta) + }; + + let cast_tag = clif_intcast(fx, incr_tag, cast_to, !niches_ule); + + (is_niche, cast_tag, 0) + } else { + // The special cases don't apply, so we'll have to go with + // the general algorithm. let niche_start = match fx.bcx.func.dfg.value_type(tag) { types::I128 => { let lsb = fx.bcx.ins().iconst(types::I64, niche_start as u64 as i64); @@ -138,40 +253,40 @@ pub(crate) fn codegen_get_discriminant<'tcx>( } ty => fx.bcx.ins().iconst(ty, niche_start as i64), }; - fx.bcx.ins().isub(tag, niche_start) - }; - let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32(); - let is_niche = { - codegen_icmp_imm( + let relative_discr = fx.bcx.ins().isub(tag, niche_start); + let cast_tag = clif_intcast(fx, relative_discr, cast_to, false); + let is_niche = crate::common::codegen_icmp_imm( fx, IntCC::UnsignedLessThanOrEqual, relative_discr, i128::from(relative_max), - ) + ); + (is_niche, cast_tag, niche_variants.start().as_u32() as u128) }; - // NOTE(eddyb) this addition needs to be performed on the final - // type, in case the niche itself can't represent all variant - // indices (e.g. `u8` niche with more than `256` variants, - // but enough uninhabited variants so that the remaining variants - // fit in the niche). - // In other words, `niche_variants.end - niche_variants.start` - // is representable in the niche, but `niche_variants.end` - // might not be, in extreme cases. - let niche_discr = { - let relative_discr = if relative_max == 0 { - // HACK(eddyb) since we have only one niche, we know which - // one it is, and we can avoid having a dynamic value here. - fx.bcx.ins().iconst(cast_to, 0) - } else { - clif_intcast(fx, relative_discr, cast_to, false) + let tagged_discr = if delta == 0 { + tagged_discr + } else { + let delta = match cast_to { + types::I128 => { + let lsb = fx.bcx.ins().iconst(types::I64, delta as u64 as i64); + let msb = fx.bcx.ins().iconst(types::I64, (delta >> 64) as u64 as i64); + fx.bcx.ins().iconcat(lsb, msb) + } + ty => fx.bcx.ins().iconst(ty, delta as i64), }; - fx.bcx.ins().iadd_imm(relative_discr, i64::from(niche_variants.start().as_u32())) + fx.bcx.ins().iadd(tagged_discr, delta) }; - let untagged_variant = - fx.bcx.ins().iconst(cast_to, i64::from(untagged_variant.as_u32())); - let discr = fx.bcx.ins().select(is_niche, niche_discr, untagged_variant); + let untagged_variant = if cast_to == types::I128 { + let zero = fx.bcx.ins().iconst(types::I64, 0); + let untagged_variant = + fx.bcx.ins().iconst(types::I64, i64::from(untagged_variant.as_u32())); + fx.bcx.ins().iconcat(untagged_variant, zero) + } else { + fx.bcx.ins().iconst(cast_to, i64::from(untagged_variant.as_u32())) + }; + let discr = fx.bcx.ins().select(is_niche, tagged_discr, untagged_variant); let res = CValue::by_val(discr, dest_layout); dest.write_cvalue(fx, res); } diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 6a430b5215e36..be1b8c9ead3bf 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -159,7 +159,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { tcx.sess.abort_if_errors(); - jit_module.finalize_definitions(); + jit_module.finalize_definitions().unwrap(); unsafe { cx.unwind_context.register_jit(&jit_module) }; println!( @@ -245,7 +245,11 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) -> let backend_config = lazy_jit_state.backend_config.clone(); let name = tcx.symbol_name(instance).name; - let sig = crate::abi::get_function_sig(tcx, jit_module.isa().triple(), instance); + let sig = crate::abi::get_function_sig( + tcx, + jit_module.target_config().default_call_conv, + instance, + ); let func_id = jit_module.declare_function(name, Linkage::Export, &sig).unwrap(); let current_ptr = jit_module.read_got_entry(func_id); @@ -278,7 +282,7 @@ fn jit_fn(instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8) -> }); assert!(cx.global_asm.is_empty()); - jit_module.finalize_definitions(); + jit_module.finalize_definitions().unwrap(); unsafe { cx.unwind_context.register_jit(&jit_module) }; jit_module.get_finalized_function(func_id) }) @@ -344,7 +348,7 @@ fn codegen_shim<'tcx>( let pointer_type = module.target_config().pointer_type(); let name = tcx.symbol_name(inst).name; - let sig = crate::abi::get_function_sig(tcx, module.isa().triple(), inst); + let sig = crate::abi::get_function_sig(tcx, module.target_config().default_call_conv, inst); let func_id = module.declare_function(name, Linkage::Export, &sig).unwrap(); let instance_ptr = Box::into_raw(Box::new(inst)); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 8f5714ecb4177..6e925cea27707 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -24,7 +24,8 @@ fn predefine_mono_items<'tcx>( MonoItem::Fn(instance) => { let name = tcx.symbol_name(instance).name; let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); - let sig = get_function_sig(tcx, module.isa().triple(), instance); + let sig = + get_function_sig(tcx, module.target_config().default_call_conv, instance); let linkage = crate::linkage::get_clif_linkage( mono_item, linkage, diff --git a/src/intrinsics/llvm.rs b/src/intrinsics/llvm.rs index 783d426c30bcc..f722e52284fe8 100644 --- a/src/intrinsics/llvm.rs +++ b/src/intrinsics/llvm.rs @@ -8,135 +8,37 @@ use rustc_middle::ty::subst::SubstsRef; pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, intrinsic: &str, - _substs: SubstsRef<'tcx>, + substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], ret: CPlace<'tcx>, target: Option, ) { - match intrinsic { - "llvm.x86.sse2.pause" | "llvm.aarch64.isb" => { - // Spin loop hint - } + if intrinsic.starts_with("llvm.aarch64") { + return llvm_aarch64::codegen_aarch64_llvm_intrinsic_call( + fx, intrinsic, substs, args, ret, target, + ); + } + if intrinsic.starts_with("llvm.x86") { + return llvm_x86::codegen_x86_llvm_intrinsic_call(fx, intrinsic, substs, args, ret, target); + } - // Used by `_mm_movemask_epi8` and `_mm256_movemask_epi8` - "llvm.x86.sse2.pmovmskb.128" | "llvm.x86.avx2.pmovmskb" | "llvm.x86.sse2.movmsk.pd" => { + match intrinsic { + _ if intrinsic.starts_with("llvm.ctlz.v") => { intrinsic_args!(fx, args => (a); intrinsic); - let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx); - let lane_ty = fx.clif_type(lane_ty).unwrap(); - assert!(lane_count <= 32); - - let mut res = fx.bcx.ins().iconst(types::I32, 0); - - for lane in (0..lane_count).rev() { - let a_lane = a.value_lane(fx, lane).load_scalar(fx); - - // cast float to int - let a_lane = match lane_ty { - types::F32 => fx.bcx.ins().bitcast(types::I32, a_lane), - types::F64 => fx.bcx.ins().bitcast(types::I64, a_lane), - _ => a_lane, - }; - - // extract sign bit of an int - let a_lane_sign = fx.bcx.ins().ushr_imm(a_lane, i64::from(lane_ty.bits() - 1)); - - // shift sign bit into result - let a_lane_sign = clif_intcast(fx, a_lane_sign, types::I32, false); - res = fx.bcx.ins().ishl_imm(res, 1); - res = fx.bcx.ins().bor(res, a_lane_sign); - } - - let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.i32)); - ret.write_cvalue(fx, res); - } - "llvm.x86.sse2.cmp.ps" | "llvm.x86.sse2.cmp.pd" => { - let (x, y, kind) = match args { - [x, y, kind] => (x, y, kind), - _ => bug!("wrong number of args for intrinsic {intrinsic}"), - }; - let x = codegen_operand(fx, x); - let y = codegen_operand(fx, y); - let kind = crate::constant::mir_operand_get_const_val(fx, kind) - .expect("llvm.x86.sse2.cmp.* kind not const"); - - let flt_cc = match kind - .try_to_bits(Size::from_bytes(1)) - .unwrap_or_else(|| panic!("kind not scalar: {:?}", kind)) - { - 0 => FloatCC::Equal, - 1 => FloatCC::LessThan, - 2 => FloatCC::LessThanOrEqual, - 7 => FloatCC::Ordered, - 3 => FloatCC::Unordered, - 4 => FloatCC::NotEqual, - 5 => FloatCC::UnorderedOrGreaterThanOrEqual, - 6 => FloatCC::UnorderedOrGreaterThan, - kind => unreachable!("kind {:?}", kind), - }; - - simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, res_lane_ty, x_lane, y_lane| { - let res_lane = match lane_ty.kind() { - ty::Float(_) => fx.bcx.ins().fcmp(flt_cc, x_lane, y_lane), - _ => unreachable!("{:?}", lane_ty), - }; - bool_to_zero_or_max_uint(fx, res_lane_ty, res_lane) + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + fx.bcx.ins().clz(lane) }); } - "llvm.x86.sse2.psrli.d" => { - let (a, imm8) = match args { - [a, imm8] => (a, imm8), - _ => bug!("wrong number of args for intrinsic {intrinsic}"), - }; - let a = codegen_operand(fx, a); - let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8) - .expect("llvm.x86.sse2.psrli.d imm8 not const"); - simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm8 - .try_to_bits(Size::from_bytes(4)) - .unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) - { - imm8 if imm8 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm8 as u8)), - _ => fx.bcx.ins().iconst(types::I32, 0), - }); - } - "llvm.x86.sse2.pslli.d" => { - let (a, imm8) = match args { - [a, imm8] => (a, imm8), - _ => bug!("wrong number of args for intrinsic {intrinsic}"), - }; - let a = codegen_operand(fx, a); - let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8) - .expect("llvm.x86.sse2.psrli.d imm8 not const"); + _ if intrinsic.starts_with("llvm.ctpop.v") => { + intrinsic_args!(fx, args => (a); intrinsic); - simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm8 - .try_to_bits(Size::from_bytes(4)) - .unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) - { - imm8 if imm8 < 32 => fx.bcx.ins().ishl_imm(lane, i64::from(imm8 as u8)), - _ => fx.bcx.ins().iconst(types::I32, 0), + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + fx.bcx.ins().popcnt(lane) }); } - "llvm.x86.sse2.storeu.dq" => { - intrinsic_args!(fx, args => (mem_addr, a); intrinsic); - let mem_addr = mem_addr.load_scalar(fx); - - // FIXME correctly handle the unalignment - let dest = CPlace::for_ptr(Pointer::new(mem_addr), a.layout()); - dest.write_cvalue(fx, a); - } - "llvm.x86.addcarry.64" => { - intrinsic_args!(fx, args => (c_in, a, b); intrinsic); - let c_in = c_in.load_scalar(fx); - - llvm_add_sub(fx, BinOp::Add, ret, c_in, a, b); - } - "llvm.x86.subborrow.64" => { - intrinsic_args!(fx, args => (b_in, a, b); intrinsic); - let b_in = b_in.load_scalar(fx); - llvm_add_sub(fx, BinOp::Sub, ret, b_in, a, b); - } _ => { fx.tcx .sess @@ -150,47 +52,3 @@ pub(crate) fn codegen_llvm_intrinsic_call<'tcx>( let ret_block = fx.get_block(dest); fx.bcx.ins().jump(ret_block, &[]); } - -// llvm.x86.avx2.vperm2i128 -// llvm.x86.ssse3.pshuf.b.128 -// llvm.x86.avx2.pshuf.b -// llvm.x86.avx2.psrli.w -// llvm.x86.sse2.psrli.w - -fn llvm_add_sub<'tcx>( - fx: &mut FunctionCx<'_, '_, 'tcx>, - bin_op: BinOp, - ret: CPlace<'tcx>, - cb_in: Value, - a: CValue<'tcx>, - b: CValue<'tcx>, -) { - assert_eq!( - a.layout().ty, - fx.tcx.types.u64, - "llvm.x86.addcarry.64/llvm.x86.subborrow.64 second operand must be u64" - ); - assert_eq!( - b.layout().ty, - fx.tcx.types.u64, - "llvm.x86.addcarry.64/llvm.x86.subborrow.64 third operand must be u64" - ); - - // c + carry -> c + first intermediate carry or borrow respectively - let int0 = crate::num::codegen_checked_int_binop(fx, bin_op, a, b); - let c = int0.value_field(fx, mir::Field::new(0)); - let cb0 = int0.value_field(fx, mir::Field::new(1)).load_scalar(fx); - - // c + carry -> c + second intermediate carry or borrow respectively - let cb_in_as_u64 = fx.bcx.ins().uextend(types::I64, cb_in); - let cb_in_as_u64 = CValue::by_val(cb_in_as_u64, fx.layout_of(fx.tcx.types.u64)); - let int1 = crate::num::codegen_checked_int_binop(fx, bin_op, c, cb_in_as_u64); - let (c, cb1) = int1.load_scalar_pair(fx); - - // carry0 | carry1 -> carry or borrow respectively - let cb_out = fx.bcx.ins().bor(cb0, cb1); - - let layout = fx.layout_of(fx.tcx.mk_tup([fx.tcx.types.u8, fx.tcx.types.u64].iter())); - let val = CValue::by_val_pair(cb_out, c, layout); - ret.write_cvalue(fx, val); -} diff --git a/src/intrinsics/llvm_aarch64.rs b/src/intrinsics/llvm_aarch64.rs new file mode 100644 index 0000000000000..b431158d2690f --- /dev/null +++ b/src/intrinsics/llvm_aarch64.rs @@ -0,0 +1,222 @@ +//! Emulate AArch64 LLVM intrinsics + +use crate::intrinsics::*; +use crate::prelude::*; + +use rustc_middle::ty::subst::SubstsRef; + +pub(crate) fn codegen_aarch64_llvm_intrinsic_call<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + intrinsic: &str, + _substs: SubstsRef<'tcx>, + args: &[mir::Operand<'tcx>], + ret: CPlace<'tcx>, + target: Option, +) { + // llvm.aarch64.neon.sqshl.v*i* + + match intrinsic { + "llvm.aarch64.isb" => { + fx.bcx.ins().fence(); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.abs.v") => { + intrinsic_args!(fx, args => (a); intrinsic); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + fx.bcx.ins().iabs(lane) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.cls.v") => { + intrinsic_args!(fx, args => (a); intrinsic); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + fx.bcx.ins().cls(lane) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.rbit.v") => { + intrinsic_args!(fx, args => (a); intrinsic); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| { + fx.bcx.ins().bitrev(lane) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.sqadd.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane_typed(fx, x, y, ret, &|fx, x_lane, y_lane| { + crate::num::codegen_saturating_int_binop(fx, BinOp::Add, x_lane, y_lane) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.sqsub.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane_typed(fx, x, y, ret, &|fx, x_lane, y_lane| { + crate::num::codegen_saturating_int_binop(fx, BinOp::Sub, x_lane, y_lane) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.smax.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane( + fx, + x, + y, + ret, + &|fx, _lane_ty, _res_lane_ty, x_lane, y_lane| { + let gt = fx.bcx.ins().icmp(IntCC::SignedGreaterThan, x_lane, y_lane); + fx.bcx.ins().select(gt, x_lane, y_lane) + }, + ); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.umax.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane( + fx, + x, + y, + ret, + &|fx, _lane_ty, _res_lane_ty, x_lane, y_lane| { + let gt = fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, x_lane, y_lane); + fx.bcx.ins().select(gt, x_lane, y_lane) + }, + ); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.smaxv.i") => { + intrinsic_args!(fx, args => (v); intrinsic); + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| { + let gt = fx.bcx.ins().icmp(IntCC::SignedGreaterThan, a, b); + fx.bcx.ins().select(gt, a, b) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.umaxv.i") => { + intrinsic_args!(fx, args => (v); intrinsic); + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| { + let gt = fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, a, b); + fx.bcx.ins().select(gt, a, b) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.smin.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane( + fx, + x, + y, + ret, + &|fx, _lane_ty, _res_lane_ty, x_lane, y_lane| { + let gt = fx.bcx.ins().icmp(IntCC::SignedLessThan, x_lane, y_lane); + fx.bcx.ins().select(gt, x_lane, y_lane) + }, + ); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.umin.v") => { + intrinsic_args!(fx, args => (x, y); intrinsic); + + simd_pair_for_each_lane( + fx, + x, + y, + ret, + &|fx, _lane_ty, _res_lane_ty, x_lane, y_lane| { + let gt = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, x_lane, y_lane); + fx.bcx.ins().select(gt, x_lane, y_lane) + }, + ); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.sminv.i") => { + intrinsic_args!(fx, args => (v); intrinsic); + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| { + let gt = fx.bcx.ins().icmp(IntCC::SignedLessThan, a, b); + fx.bcx.ins().select(gt, a, b) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.uminv.i") => { + intrinsic_args!(fx, args => (v); intrinsic); + + simd_reduce(fx, v, None, ret, &|fx, _ty, a, b| { + let gt = fx.bcx.ins().icmp(IntCC::UnsignedLessThan, a, b); + fx.bcx.ins().select(gt, a, b) + }); + } + + /* + _ if intrinsic.starts_with("llvm.aarch64.neon.sshl.v") + || intrinsic.starts_with("llvm.aarch64.neon.sqshl.v") + // FIXME split this one out once saturating is implemented + || intrinsic.starts_with("llvm.aarch64.neon.sqshlu.v") => + { + intrinsic_args!(fx, args => (a, b); intrinsic); + + simd_pair_for_each_lane(fx, a, b, ret, &|fx, _lane_ty, _res_lane_ty, a, b| { + // FIXME saturate? + fx.bcx.ins().ishl(a, b) + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.sqshrn.v") => { + let (a, imm32) = match args { + [a, imm32] => (a, imm32), + _ => bug!("wrong number of args for intrinsic {intrinsic}"), + }; + let a = codegen_operand(fx, a); + let imm32 = crate::constant::mir_operand_get_const_val(fx, imm32) + .expect("llvm.aarch64.neon.sqshrn.v* imm32 not const"); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm32 + .try_to_bits(Size::from_bytes(4)) + .unwrap_or_else(|| panic!("imm32 not scalar: {:?}", imm32)) + { + imm32 if imm32 < 32 => fx.bcx.ins().sshr_imm(lane, i64::from(imm32 as u8)), + _ => fx.bcx.ins().iconst(types::I32, 0), + }); + } + + _ if intrinsic.starts_with("llvm.aarch64.neon.sqshrun.v") => { + let (a, imm32) = match args { + [a, imm32] => (a, imm32), + _ => bug!("wrong number of args for intrinsic {intrinsic}"), + }; + let a = codegen_operand(fx, a); + let imm32 = crate::constant::mir_operand_get_const_val(fx, imm32) + .expect("llvm.aarch64.neon.sqshrn.v* imm32 not const"); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm32 + .try_to_bits(Size::from_bytes(4)) + .unwrap_or_else(|| panic!("imm32 not scalar: {:?}", imm32)) + { + imm32 if imm32 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm32 as u8)), + _ => fx.bcx.ins().iconst(types::I32, 0), + }); + } + */ + _ => { + fx.tcx.sess.warn(&format!( + "unsupported AArch64 llvm intrinsic {}; replacing with trap", + intrinsic + )); + crate::trap::trap_unimplemented(fx, intrinsic); + return; + } + } + + let dest = target.expect("all llvm intrinsics used by stdlib should return"); + let ret_block = fx.get_block(dest); + fx.bcx.ins().jump(ret_block, &[]); +} diff --git a/src/intrinsics/llvm_x86.rs b/src/intrinsics/llvm_x86.rs new file mode 100644 index 0000000000000..7bc161fbe5523 --- /dev/null +++ b/src/intrinsics/llvm_x86.rs @@ -0,0 +1,197 @@ +//! Emulate x86 LLVM intrinsics + +use crate::intrinsics::*; +use crate::prelude::*; + +use rustc_middle::ty::subst::SubstsRef; + +pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + intrinsic: &str, + _substs: SubstsRef<'tcx>, + args: &[mir::Operand<'tcx>], + ret: CPlace<'tcx>, + target: Option, +) { + match intrinsic { + "llvm.x86.sse2.pause" | "llvm.aarch64.isb" => { + // Spin loop hint + } + + // Used by `_mm_movemask_epi8` and `_mm256_movemask_epi8` + "llvm.x86.sse2.pmovmskb.128" | "llvm.x86.avx2.pmovmskb" | "llvm.x86.sse2.movmsk.pd" => { + intrinsic_args!(fx, args => (a); intrinsic); + + let (lane_count, lane_ty) = a.layout().ty.simd_size_and_type(fx.tcx); + let lane_ty = fx.clif_type(lane_ty).unwrap(); + assert!(lane_count <= 32); + + let mut res = fx.bcx.ins().iconst(types::I32, 0); + + for lane in (0..lane_count).rev() { + let a_lane = a.value_lane(fx, lane).load_scalar(fx); + + // cast float to int + let a_lane = match lane_ty { + types::F32 => fx.bcx.ins().bitcast(types::I32, a_lane), + types::F64 => fx.bcx.ins().bitcast(types::I64, a_lane), + _ => a_lane, + }; + + // extract sign bit of an int + let a_lane_sign = fx.bcx.ins().ushr_imm(a_lane, i64::from(lane_ty.bits() - 1)); + + // shift sign bit into result + let a_lane_sign = clif_intcast(fx, a_lane_sign, types::I32, false); + res = fx.bcx.ins().ishl_imm(res, 1); + res = fx.bcx.ins().bor(res, a_lane_sign); + } + + let res = CValue::by_val(res, fx.layout_of(fx.tcx.types.i32)); + ret.write_cvalue(fx, res); + } + "llvm.x86.sse2.cmp.ps" | "llvm.x86.sse2.cmp.pd" => { + let (x, y, kind) = match args { + [x, y, kind] => (x, y, kind), + _ => bug!("wrong number of args for intrinsic {intrinsic}"), + }; + let x = codegen_operand(fx, x); + let y = codegen_operand(fx, y); + let kind = crate::constant::mir_operand_get_const_val(fx, kind) + .expect("llvm.x86.sse2.cmp.* kind not const"); + + let flt_cc = match kind + .try_to_bits(Size::from_bytes(1)) + .unwrap_or_else(|| panic!("kind not scalar: {:?}", kind)) + { + 0 => FloatCC::Equal, + 1 => FloatCC::LessThan, + 2 => FloatCC::LessThanOrEqual, + 7 => FloatCC::Ordered, + 3 => FloatCC::Unordered, + 4 => FloatCC::NotEqual, + 5 => FloatCC::UnorderedOrGreaterThanOrEqual, + 6 => FloatCC::UnorderedOrGreaterThan, + kind => unreachable!("kind {:?}", kind), + }; + + simd_pair_for_each_lane(fx, x, y, ret, &|fx, lane_ty, res_lane_ty, x_lane, y_lane| { + let res_lane = match lane_ty.kind() { + ty::Float(_) => fx.bcx.ins().fcmp(flt_cc, x_lane, y_lane), + _ => unreachable!("{:?}", lane_ty), + }; + bool_to_zero_or_max_uint(fx, res_lane_ty, res_lane) + }); + } + "llvm.x86.sse2.psrli.d" => { + let (a, imm8) = match args { + [a, imm8] => (a, imm8), + _ => bug!("wrong number of args for intrinsic {intrinsic}"), + }; + let a = codegen_operand(fx, a); + let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8) + .expect("llvm.x86.sse2.psrli.d imm8 not const"); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm8 + .try_to_bits(Size::from_bytes(4)) + .unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) + { + imm8 if imm8 < 32 => fx.bcx.ins().ushr_imm(lane, i64::from(imm8 as u8)), + _ => fx.bcx.ins().iconst(types::I32, 0), + }); + } + "llvm.x86.sse2.pslli.d" => { + let (a, imm8) = match args { + [a, imm8] => (a, imm8), + _ => bug!("wrong number of args for intrinsic {intrinsic}"), + }; + let a = codegen_operand(fx, a); + let imm8 = crate::constant::mir_operand_get_const_val(fx, imm8) + .expect("llvm.x86.sse2.psrli.d imm8 not const"); + + simd_for_each_lane(fx, a, ret, &|fx, _lane_ty, _res_lane_ty, lane| match imm8 + .try_to_bits(Size::from_bytes(4)) + .unwrap_or_else(|| panic!("imm8 not scalar: {:?}", imm8)) + { + imm8 if imm8 < 32 => fx.bcx.ins().ishl_imm(lane, i64::from(imm8 as u8)), + _ => fx.bcx.ins().iconst(types::I32, 0), + }); + } + "llvm.x86.sse2.storeu.dq" => { + intrinsic_args!(fx, args => (mem_addr, a); intrinsic); + let mem_addr = mem_addr.load_scalar(fx); + + // FIXME correctly handle the unalignment + let dest = CPlace::for_ptr(Pointer::new(mem_addr), a.layout()); + dest.write_cvalue(fx, a); + } + "llvm.x86.addcarry.64" => { + intrinsic_args!(fx, args => (c_in, a, b); intrinsic); + let c_in = c_in.load_scalar(fx); + + llvm_add_sub(fx, BinOp::Add, ret, c_in, a, b); + } + "llvm.x86.subborrow.64" => { + intrinsic_args!(fx, args => (b_in, a, b); intrinsic); + let b_in = b_in.load_scalar(fx); + + llvm_add_sub(fx, BinOp::Sub, ret, b_in, a, b); + } + _ => { + fx.tcx.sess.warn(&format!( + "unsupported x86 llvm intrinsic {}; replacing with trap", + intrinsic + )); + crate::trap::trap_unimplemented(fx, intrinsic); + return; + } + } + + let dest = target.expect("all llvm intrinsics used by stdlib should return"); + let ret_block = fx.get_block(dest); + fx.bcx.ins().jump(ret_block, &[]); +} + +// llvm.x86.avx2.vperm2i128 +// llvm.x86.ssse3.pshuf.b.128 +// llvm.x86.avx2.pshuf.b +// llvm.x86.avx2.psrli.w +// llvm.x86.sse2.psrli.w + +fn llvm_add_sub<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + bin_op: BinOp, + ret: CPlace<'tcx>, + cb_in: Value, + a: CValue<'tcx>, + b: CValue<'tcx>, +) { + assert_eq!( + a.layout().ty, + fx.tcx.types.u64, + "llvm.x86.addcarry.64/llvm.x86.subborrow.64 second operand must be u64" + ); + assert_eq!( + b.layout().ty, + fx.tcx.types.u64, + "llvm.x86.addcarry.64/llvm.x86.subborrow.64 third operand must be u64" + ); + + // c + carry -> c + first intermediate carry or borrow respectively + let int0 = crate::num::codegen_checked_int_binop(fx, bin_op, a, b); + let c = int0.value_field(fx, mir::Field::new(0)); + let cb0 = int0.value_field(fx, mir::Field::new(1)).load_scalar(fx); + + // c + carry -> c + second intermediate carry or borrow respectively + let cb_in_as_u64 = fx.bcx.ins().uextend(types::I64, cb_in); + let cb_in_as_u64 = CValue::by_val(cb_in_as_u64, fx.layout_of(fx.tcx.types.u64)); + let int1 = crate::num::codegen_checked_int_binop(fx, bin_op, c, cb_in_as_u64); + let (c, cb1) = int1.load_scalar_pair(fx); + + // carry0 | carry1 -> carry or borrow respectively + let cb_out = fx.bcx.ins().bor(cb0, cb1); + + let layout = fx.layout_of(fx.tcx.mk_tup([fx.tcx.types.u8, fx.tcx.types.u64].iter())); + let val = CValue::by_val_pair(cb_out, c, layout); + ret.write_cvalue(fx, val); +} diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 0302b843aa226..7a380acf79857 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -14,6 +14,8 @@ macro_rules! intrinsic_args { mod cpuid; mod llvm; +mod llvm_aarch64; +mod llvm_x86; mod simd; pub(crate) use cpuid::codegen_cpuid_call; @@ -195,8 +197,7 @@ fn bool_to_zero_or_max_uint<'tcx>( ty => ty, }; - let val = fx.bcx.ins().bint(int_ty, val); - let mut res = fx.bcx.ins().ineg(val); + let mut res = fx.bcx.ins().bmask(int_ty, val); if ty.is_float() { res = fx.bcx.ins().bitcast(ty, res); @@ -632,85 +633,15 @@ fn codegen_regular_intrinsic_call<'tcx>( ret.write_cvalue(fx, res); } sym::bswap => { - // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift - fn swap(bcx: &mut FunctionBuilder<'_>, v: Value) -> Value { - match bcx.func.dfg.value_type(v) { - types::I8 => v, - - // https://code.woboq.org/gcc/include/bits/byteswap.h.html - types::I16 => { - let tmp1 = bcx.ins().ishl_imm(v, 8); - let n1 = bcx.ins().band_imm(tmp1, 0xFF00); - - let tmp2 = bcx.ins().ushr_imm(v, 8); - let n2 = bcx.ins().band_imm(tmp2, 0x00FF); - - bcx.ins().bor(n1, n2) - } - types::I32 => { - let tmp1 = bcx.ins().ishl_imm(v, 24); - let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000); - - let tmp2 = bcx.ins().ishl_imm(v, 8); - let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000); - - let tmp3 = bcx.ins().ushr_imm(v, 8); - let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00); - - let tmp4 = bcx.ins().ushr_imm(v, 24); - let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF); - - let or_tmp1 = bcx.ins().bor(n1, n2); - let or_tmp2 = bcx.ins().bor(n3, n4); - bcx.ins().bor(or_tmp1, or_tmp2) - } - types::I64 => { - let tmp1 = bcx.ins().ishl_imm(v, 56); - let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64); - - let tmp2 = bcx.ins().ishl_imm(v, 40); - let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64); - - let tmp3 = bcx.ins().ishl_imm(v, 24); - let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64); - - let tmp4 = bcx.ins().ishl_imm(v, 8); - let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64); - - let tmp5 = bcx.ins().ushr_imm(v, 8); - let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64); - - let tmp6 = bcx.ins().ushr_imm(v, 24); - let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64); - - let tmp7 = bcx.ins().ushr_imm(v, 40); - let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64); - - let tmp8 = bcx.ins().ushr_imm(v, 56); - let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64); - - let or_tmp1 = bcx.ins().bor(n1, n2); - let or_tmp2 = bcx.ins().bor(n3, n4); - let or_tmp3 = bcx.ins().bor(n5, n6); - let or_tmp4 = bcx.ins().bor(n7, n8); - - let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2); - let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4); - bcx.ins().bor(or_tmp5, or_tmp6) - } - types::I128 => { - let (lo, hi) = bcx.ins().isplit(v); - let lo = swap(bcx, lo); - let hi = swap(bcx, hi); - bcx.ins().iconcat(hi, lo) - } - ty => unreachable!("bswap {}", ty), - } - } intrinsic_args!(fx, args => (arg); intrinsic); let val = arg.load_scalar(fx); - let res = CValue::by_val(swap(&mut fx.bcx, val), arg.layout()); + let res = if fx.bcx.func.dfg.value_type(val) == types::I8 { + val + } else { + fx.bcx.ins().bswap(val) + }; + let res = CValue::by_val(res, arg.layout()); ret.write_cvalue(fx, res); } sym::assert_inhabited | sym::assert_zero_valid | sym::assert_uninit_valid => { @@ -936,8 +867,7 @@ fn codegen_regular_intrinsic_call<'tcx>( 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 ret_val = - CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout()); + let ret_val = CValue::by_val_pair(old, is_eq, ret.layout()); ret.write_cvalue(fx, ret_val) } @@ -1259,8 +1189,7 @@ fn codegen_regular_intrinsic_call<'tcx>( flags.set_notrap(); let lhs_val = fx.bcx.ins().load(clty, flags, lhs_ref, 0); let rhs_val = fx.bcx.ins().load(clty, flags, rhs_ref, 0); - let eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val); - fx.bcx.ins().bint(types::I8, eq) + fx.bcx.ins().icmp(IntCC::Equal, lhs_val, rhs_val) } else { // Just call `memcmp` (like slices do in core) when the // size is too large or it's not a power-of-two. @@ -1270,8 +1199,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let returns = vec![AbiParam::new(types::I32)]; let args = &[lhs_ref, rhs_ref, bytes_val]; let cmp = fx.lib_call("memcmp", params, returns, args)[0]; - let eq = fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0); - fx.bcx.ins().bint(types::I8, eq) + fx.bcx.ins().icmp_imm(IntCC::Equal, cmp, 0) }; ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout())); } diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 51fce8c854bdb..14f5e9187399f 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -112,10 +112,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( _ => unreachable!(), }; - let ty = fx.clif_type(res_lane_ty).unwrap(); - - let res_lane = fx.bcx.ins().bint(ty, res_lane); - fx.bcx.ins().ineg(res_lane) + bool_to_zero_or_max_uint(fx, res_lane_ty, res_lane) }); } @@ -716,7 +713,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( let res_type = Type::int_with_byte_size(u16::try_from(expected_bytes).unwrap()).unwrap(); - let mut res = fx.bcx.ins().iconst(res_type, 0); + let mut res = type_zero_value(&mut fx.bcx, res_type); let lanes = match fx.tcx.sess.target.endian { Endian::Big => Box::new(0..lane_count) as Box>, diff --git a/src/main_shim.rs b/src/main_shim.rs index f7434633ea442..c10054e7f0d2c 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -65,7 +65,7 @@ pub(crate) fn maybe_create_entry_wrapper( returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)], call_conv: crate::conv_to_call_conv( tcx.sess.target.options.entry_abi, - CallConv::triple_default(m.isa().triple()), + m.target_config().default_call_conv, ), }; @@ -75,7 +75,7 @@ pub(crate) fn maybe_create_entry_wrapper( let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx); let main_name = tcx.symbol_name(instance).name; - let main_sig = get_function_sig(tcx, m.isa().triple(), instance); + let main_sig = get_function_sig(tcx, m.target_config().default_call_conv, instance); let main_func_id = m.declare_function(main_name, Linkage::Import, &main_sig).unwrap(); let mut ctx = Context::new(); @@ -119,7 +119,7 @@ pub(crate) fn maybe_create_entry_wrapper( .polymorphize(tcx); let report_name = tcx.symbol_name(report).name; - let report_sig = get_function_sig(tcx, m.isa().triple(), report); + let report_sig = get_function_sig(tcx, m.target_config().default_call_conv, report); let report_func_id = m.declare_function(report_name, Linkage::Import, &report_sig).unwrap(); let report_func_ref = m.declare_func_in_func(report_func_id, &mut bcx.func); diff --git a/src/num.rs b/src/num.rs index ecbab408ded97..afacbec644582 100644 --- a/src/num.rs +++ b/src/num.rs @@ -49,7 +49,6 @@ fn codegen_compare_bin_op<'tcx>( ) -> CValue<'tcx> { let intcc = crate::num::bin_op_to_intcc(bin_op, signed).unwrap(); let val = fx.bcx.ins().icmp(intcc, lhs, rhs); - let val = fx.bcx.ins().bint(types::I8, val); CValue::by_val(val, fx.layout_of(fx.tcx.types.bool)) } @@ -290,8 +289,6 @@ pub(crate) fn codegen_checked_int_binop<'tcx>( _ => 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())); CValue::by_val_pair(res, has_overflow, out_layout) } @@ -368,7 +365,6 @@ pub(crate) fn codegen_float_binop<'tcx>( _ => unreachable!(), }; let val = fx.bcx.ins().fcmp(fltcc, lhs, rhs); - let val = fx.bcx.ins().bint(types::I8, val); return CValue::by_val(val, fx.layout_of(fx.tcx.types.bool)); } _ => unreachable!("{:?}({:?}, {:?})", bin_op, in_lhs, in_rhs), @@ -440,7 +436,7 @@ 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(res, fx.layout_of(fx.tcx.types.bool)) } } diff --git a/src/optimize/peephole.rs b/src/optimize/peephole.rs index d637b4d89293c..7f45bbd8f2813 100644 --- a/src/optimize/peephole.rs +++ b/src/optimize/peephole.rs @@ -3,19 +3,6 @@ use cranelift_codegen::ir::{condcodes::IntCC, InstructionData, Opcode, Value, ValueDef}; use cranelift_frontend::FunctionBuilder; -/// If the given value was produced by a `bint` instruction, return it's input, otherwise return the -/// given value. -pub(crate) fn maybe_unwrap_bint(bcx: &mut FunctionBuilder<'_>, 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, - _ => arg, - } - } else { - arg - } -} - /// If the given value was produced by the lowering of `Rvalue::Not` return the input and true, /// otherwise return the given value and false. pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -> (Value, bool) { @@ -48,13 +35,6 @@ pub(crate) fn maybe_known_branch_taken( }; match bcx.func.dfg[arg_inst] { - InstructionData::UnaryBool { opcode: Opcode::Bconst, imm } => { - if test_zero { - Some(!imm) - } else { - Some(imm) - } - } InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => { if test_zero { Some(imm.bits() == 0) diff --git a/src/value_and_place.rs b/src/value_and_place.rs index 34746ff6b6645..fe8af21ac6de5 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -392,7 +392,7 @@ impl<'tcx> CPlace<'tcx> { local: Local, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { - let var = Variable::with_u32(fx.next_ssa_var); + let var = Variable::from_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 } @@ -403,9 +403,9 @@ impl<'tcx> CPlace<'tcx> { local: Local, layout: TyAndLayout<'tcx>, ) -> CPlace<'tcx> { - let var1 = Variable::with_u32(fx.next_ssa_var); + let var1 = Variable::from_u32(fx.next_ssa_var); fx.next_ssa_var += 1; - let var2 = Variable::with_u32(fx.next_ssa_var); + let var2 = Variable::from_u32(fx.next_ssa_var); fx.next_ssa_var += 1; let (ty1, ty2) = fx.clif_pair_type(layout.ty).unwrap(); @@ -515,9 +515,7 @@ impl<'tcx> CPlace<'tcx> { | (types::F32, types::I32) | (types::I64, types::F64) | (types::F64, types::I64) => fx.bcx.ins().bitcast(dst_ty, data), - _ if src_ty.is_vector() && dst_ty.is_vector() => { - fx.bcx.ins().raw_bitcast(dst_ty, data) - } + _ if src_ty.is_vector() && dst_ty.is_vector() => fx.bcx.ins().bitcast(dst_ty, data), _ if src_ty.is_vector() || dst_ty.is_vector() => { // FIXME do something more efficient for transmutes between vectors and integers. let stack_slot = fx.bcx.create_sized_stack_slot(StackSlotData { @@ -590,7 +588,10 @@ impl<'tcx> CPlace<'tcx> { return; } CPlaceInner::VarPair(_local, var1, var2) => { - let (data1, data2) = CValue(from.0, dst_layout).load_scalar_pair(fx); + let (ptr, meta) = from.force_stack(fx); + assert!(meta.is_none()); + let (data1, data2) = + CValue(CValueInner::ByRef(ptr, None), dst_layout).load_scalar_pair(fx); let (dst_ty1, dst_ty2) = fx.clif_pair_type(self.layout().ty).unwrap(); transmute_value(fx, var1, data1, dst_ty1); transmute_value(fx, var2, data2, dst_ty2); diff --git a/test.sh b/test.sh index 3d929a1d50ce2..13e7784539d5a 100755 --- a/test.sh +++ b/test.sh @@ -1,2 +1,2 @@ #!/usr/bin/env bash -exec ./y.rs test +exec ./y.rs test "$@" diff --git a/y.rs b/y.rs index f177b91c2c487..02e1e21ade1de 100755 --- a/y.rs +++ b/y.rs @@ -3,7 +3,7 @@ # This block is ignored by rustc set -e echo "[BUILD] y.rs" 1>&2 -rustc $0 -o ${0/.rs/.bin} -Cdebuginfo=1 +rustc $0 -o ${0/.rs/.bin} -Cdebuginfo=1 --edition 2021 exec ${0/.rs/.bin} $@ */ From 808cba2251aba868a97b67ad7d5d7cecc9024f93 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 10:28:53 +0000 Subject: [PATCH 003/101] Extract Compiler creation from tests.rs --- build_system/tests.rs | 31 ++++++------------------------- build_system/utils.rs | 21 ++++++++++++++++++++- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 1c372736ed65d..7a12c1a54d46c 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -2,7 +2,7 @@ use super::build_sysroot; use super::config; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; -use super::rustc_info::{get_cargo_path, get_wrapper_file_name}; +use super::rustc_info::get_wrapper_file_name; use super::utils::{ hyperfine_command, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, }; @@ -507,11 +507,6 @@ impl TestRunner { let jit_supported = target_triple.contains("x86_64") && is_native && !host_triple.contains("windows"); - let rustc_clif = - RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustc-clif", "bin")); - let rustdoc_clif = - RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustdoc-clif", "bin")); - let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); let mut runner = vec![]; @@ -550,25 +545,11 @@ impl TestRunner { rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags); } - let host_compiler = Compiler { - cargo: get_cargo_path(), - rustc: rustc_clif.clone(), - rustdoc: rustdoc_clif.clone(), - rustflags: String::new(), - rustdocflags: String::new(), - triple: host_triple, - runner: vec![], - }; - - let target_compiler = Compiler { - cargo: get_cargo_path(), - rustc: rustc_clif, - rustdoc: rustdoc_clif, - rustflags: rustflags.clone(), - rustdocflags: rustflags, - triple: target_triple, - runner, - }; + let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); + + let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); + target_compiler.rustflags = rustflags; + target_compiler.runner = runner; Self { is_native, jit_supported, dirs, host_compiler, target_compiler } } diff --git a/build_system/utils.rs b/build_system/utils.rs index 2be70e8e421b2..995918ee1430b 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -5,7 +5,9 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path}; +use super::rustc_info::{ + get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path, get_wrapper_file_name, +}; pub(crate) struct Compiler { pub(crate) cargo: PathBuf, @@ -41,6 +43,23 @@ impl Compiler { runner: vec![], } } + + pub(crate) fn clif_with_triple(dirs: &Dirs, triple: String) -> Compiler { + let rustc_clif = + RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustc-clif", "bin")); + let rustdoc_clif = + RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustdoc-clif", "bin")); + + Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif.clone(), + rustdoc: rustdoc_clif.clone(), + rustflags: String::new(), + rustdocflags: String::new(), + triple, + runner: vec![], + } + } } pub(crate) struct CargoProject { From f626185a5b792c171297d1e9a6b8d166348a5e53 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 11:09:45 +0000 Subject: [PATCH 004/101] Update portable-simd to 582239ac3b32007613df04d7ffa78dc30f4c5645 --- build_system/tests.rs | 2 +- ...table-simd-Disable-unsupported-tests.patch | 4 +-- src/intrinsics/simd.rs | 32 ++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 7a12c1a54d46c..2131b88c23286 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -244,7 +244,7 @@ static REGEX: CargoProject = CargoProject::new(®EX_REPO.source_dir(), "regex" pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( "rust-lang", "portable-simd", - "d5cd4a8112d958bd3a252327e0d069a6363249bd", + "582239ac3b32007613df04d7ffa78dc30f4c5645", "portable-simd", ); diff --git a/patches/0001-portable-simd-Disable-unsupported-tests.patch b/patches/0001-portable-simd-Disable-unsupported-tests.patch index 89e2b61c1fc85..bdf727666bedf 100644 --- a/patches/0001-portable-simd-Disable-unsupported-tests.patch +++ b/patches/0001-portable-simd-Disable-unsupported-tests.patch @@ -24,8 +24,8 @@ index e8e8f68..7173c24 100644 /// If an index is out-of-bounds, the lane is instead selected from the `or` vector. /// @@ -473,6 +474,7 @@ where - // Cleared ☢️ *mut T Zone - } + // Safety: The caller is responsible for upholding all invariants + unsafe { intrinsics::simd_scatter(self, dest, enable.to_int()) } } + */ } diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 14f5e9187399f..a6f5f70dc4cad 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -770,7 +770,37 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( }); } - // simd_arith_offset + sym::simd_expose_addr | sym::simd_from_exposed_addr | sym::simd_cast_ptr => { + intrinsic_args!(fx, args => (arg); intrinsic); + ret.write_cvalue_transmute(fx, arg); + } + + sym::simd_arith_offset => { + intrinsic_args!(fx, args => (ptr, offset); intrinsic); + + let (lane_count, ptr_lane_ty) = ptr.layout().ty.simd_size_and_type(fx.tcx); + let pointee_ty = ptr_lane_ty.builtin_deref(true).unwrap().ty; + let pointee_size = fx.layout_of(pointee_ty).size.bytes(); + let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); + let ret_lane_layout = fx.layout_of(ret_lane_ty); + assert_eq!(lane_count, ret_lane_count); + + for lane_idx in 0..lane_count { + let ptr_lane = ptr.value_lane(fx, lane_idx).load_scalar(fx); + let offset_lane = offset.value_lane(fx, lane_idx).load_scalar(fx); + + let ptr_diff = if pointee_size != 1 { + fx.bcx.ins().imul_imm(offset_lane, pointee_size as i64) + } else { + offset_lane + }; + let res_lane = fx.bcx.ins().iadd(ptr_lane, ptr_diff); + let res_lane = CValue::by_val(res_lane, ret_lane_layout); + + ret.place_lane(fx, lane_idx).write_cvalue(fx, res_lane); + } + } + // simd_scatter // simd_gather _ => { From 0865e5a45295b149757da230e7e8d5d4b7b7fc9c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 11:41:06 +0000 Subject: [PATCH 005/101] Set RUSTDOCFLAGS again Was accidentally removed in 808cba2 --- build_system/tests.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 2131b88c23286..738a76ef4c55a 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -548,7 +548,8 @@ impl TestRunner { let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); - target_compiler.rustflags = rustflags; + target_compiler.rustflags = rustflags.clone(); + target_compiler.rustdocflags = rustflags; target_compiler.runner = runner; Self { is_native, jit_supported, dirs, host_compiler, target_compiler } From 06be70e3d6dc5daea0c92f770028ef19a748f290 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 13:09:37 +0100 Subject: [PATCH 006/101] Implement simd_gather and simd_scatter (#1309) These are the last remaining platform intrinsics necessary for portable-simd. --- ...table-simd-Disable-unsupported-tests.patch | 35 --------- src/intrinsics/simd.rs | 76 ++++++++++++++++++- 2 files changed, 74 insertions(+), 37 deletions(-) delete mode 100644 patches/0001-portable-simd-Disable-unsupported-tests.patch diff --git a/patches/0001-portable-simd-Disable-unsupported-tests.patch b/patches/0001-portable-simd-Disable-unsupported-tests.patch deleted file mode 100644 index bdf727666bedf..0000000000000 --- a/patches/0001-portable-simd-Disable-unsupported-tests.patch +++ /dev/null @@ -1,35 +0,0 @@ -From b742f03694b920cc14400727d54424e8e1b60928 Mon Sep 17 00:00:00 2001 -From: bjorn3 -Date: Thu, 18 Nov 2021 19:28:40 +0100 -Subject: [PATCH] Disable unsupported tests - ---- - crates/core_simd/src/elements/int.rs | 8 ++++++++ - crates/core_simd/src/elements/uint.rs | 4 ++++ - crates/core_simd/src/masks/full_masks.rs | 6 ++++++ - crates/core_simd/src/vector.rs | 2 ++ - crates/core_simd/tests/masks.rs | 3 --- - 5 files changed, 20 insertions(+), 3 deletions(-) - -diff --git a/crates/core_simd/src/vector.rs b/crates/core_simd/src/vector.rs -index e8e8f68..7173c24 100644 ---- a/crates/core_simd/src/vector.rs -+++ b/crates/core_simd/src/vector.rs -@@ -250,6 +250,7 @@ where - unsafe { intrinsics::simd_cast(self) } - } - -+ /* - /// Reads from potentially discontiguous indices in `slice` to construct a SIMD vector. - /// If an index is out-of-bounds, the lane is instead selected from the `or` vector. - /// -@@ -473,6 +474,7 @@ where - // Safety: The caller is responsible for upholding all invariants - unsafe { intrinsics::simd_scatter(self, dest, enable.to_int()) } - } -+ */ - } - - impl Copy for Simd --- -2.25.1 diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index a6f5f70dc4cad..62ea2214ab44e 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -801,8 +801,80 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( } } - // simd_scatter - // simd_gather + sym::simd_gather => { + intrinsic_args!(fx, args => (val, ptr, mask); intrinsic); + + let (val_lane_count, val_lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); + let (ptr_lane_count, _ptr_lane_ty) = ptr.layout().ty.simd_size_and_type(fx.tcx); + let (mask_lane_count, _mask_lane_ty) = mask.layout().ty.simd_size_and_type(fx.tcx); + let (ret_lane_count, ret_lane_ty) = ret.layout().ty.simd_size_and_type(fx.tcx); + assert_eq!(val_lane_count, ptr_lane_count); + assert_eq!(val_lane_count, mask_lane_count); + assert_eq!(val_lane_count, ret_lane_count); + + let lane_clif_ty = fx.clif_type(val_lane_ty).unwrap(); + let ret_lane_layout = fx.layout_of(ret_lane_ty); + + for lane_idx in 0..ptr_lane_count { + let val_lane = val.value_lane(fx, lane_idx).load_scalar(fx); + let ptr_lane = ptr.value_lane(fx, lane_idx).load_scalar(fx); + let mask_lane = mask.value_lane(fx, lane_idx).load_scalar(fx); + + let if_enabled = fx.bcx.create_block(); + let if_disabled = fx.bcx.create_block(); + let next = fx.bcx.create_block(); + let res_lane = fx.bcx.append_block_param(next, lane_clif_ty); + + fx.bcx.ins().brnz(mask_lane, if_enabled, &[]); + fx.bcx.ins().jump(if_disabled, &[]); + fx.bcx.seal_block(if_enabled); + fx.bcx.seal_block(if_disabled); + + fx.bcx.switch_to_block(if_enabled); + let res = fx.bcx.ins().load(lane_clif_ty, MemFlags::trusted(), ptr_lane, 0); + fx.bcx.ins().jump(next, &[res]); + + fx.bcx.switch_to_block(if_disabled); + fx.bcx.ins().jump(next, &[val_lane]); + + fx.bcx.seal_block(next); + fx.bcx.switch_to_block(next); + + ret.place_lane(fx, lane_idx) + .write_cvalue(fx, CValue::by_val(res_lane, ret_lane_layout)); + } + } + + sym::simd_scatter => { + intrinsic_args!(fx, args => (val, ptr, mask); intrinsic); + + let (val_lane_count, _val_lane_ty) = val.layout().ty.simd_size_and_type(fx.tcx); + let (ptr_lane_count, _ptr_lane_ty) = ptr.layout().ty.simd_size_and_type(fx.tcx); + let (mask_lane_count, _mask_lane_ty) = mask.layout().ty.simd_size_and_type(fx.tcx); + assert_eq!(val_lane_count, ptr_lane_count); + assert_eq!(val_lane_count, mask_lane_count); + + for lane_idx in 0..ptr_lane_count { + let val_lane = val.value_lane(fx, lane_idx).load_scalar(fx); + let ptr_lane = ptr.value_lane(fx, lane_idx).load_scalar(fx); + let mask_lane = mask.value_lane(fx, lane_idx).load_scalar(fx); + + let if_enabled = fx.bcx.create_block(); + let next = fx.bcx.create_block(); + + fx.bcx.ins().brnz(mask_lane, if_enabled, &[]); + fx.bcx.ins().jump(next, &[]); + fx.bcx.seal_block(if_enabled); + + fx.bcx.switch_to_block(if_enabled); + fx.bcx.ins().store(MemFlags::trusted(), val_lane, ptr_lane, 0); + fx.bcx.ins().jump(next, &[]); + + fx.bcx.seal_block(next); + fx.bcx.switch_to_block(next); + } + } + _ => { fx.tcx.sess.span_fatal(span, &format!("Unknown SIMD intrinsic {}", intrinsic)); } From e2a273934183becd362ec7ce6a86ec944db04636 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 12:20:39 +0000 Subject: [PATCH 007/101] Update not yet supported section of the readme --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 0e9c77244d4cc..55b107e4efbca 100644 --- a/Readme.md +++ b/Readme.md @@ -53,7 +53,8 @@ configuration options. * Inline assembly ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1041)) * On UNIX there is support for invoking an external assembler for `global_asm!` and `asm!`. -* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), some basic things work) +* SIMD ([tracked here](https://github.com/bjorn3/rustc_codegen_cranelift/issues/171), `std::simd` fully works, `std::arch` is partially supported) +* Unwinding on panics ([no cranelift support](https://github.com/bytecodealliance/wasmtime/issues/1677), `-Cpanic=abort` is enabled by default) ## License From d74e5b6cc7d7b015362e68ae2be88cfc3415ce3a Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 12:22:57 +0000 Subject: [PATCH 008/101] Update actions/upload-artifact to v3 v2 depends on Node.js 12 which will be removed from GHA in the near future --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a6bb12a66a247..010979c9c33e4 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -122,7 +122,7 @@ jobs: - name: Upload prebuilt cg_clif if: matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu' - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v3 with: name: cg_clif-${{ matrix.env.TARGET_TRIPLE }} path: cg_clif.tar.xz From d841f93855f94cafaf3c1a5933b6a0b1c88fa9d3 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 13:57:13 +0000 Subject: [PATCH 009/101] Add .comment section with producer name Fixes #1211 --- src/debuginfo/mod.rs | 14 +++++++++----- src/driver/aot.rs | 14 +++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 2ba012a77b0a9..4cb556844b7e8 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -20,6 +20,14 @@ use indexmap::IndexSet; pub(crate) use emit::{DebugReloc, DebugRelocName}; pub(crate) use unwind::UnwindContext; +pub(crate) fn producer() -> String { + format!( + "cg_clif (rustc {}, cranelift {})", + rustc_interface::util::rustc_version_str().unwrap_or("unknown version"), + cranelift_codegen::VERSION, + ) +} + pub(crate) struct DebugContext { endian: RunTimeEndian, @@ -57,11 +65,7 @@ impl DebugContext { let mut dwarf = DwarfUnit::new(encoding); - let producer = format!( - "cg_clif (rustc {}, cranelift {})", - rustc_interface::util::rustc_version_str().unwrap_or("unknown version"), - cranelift_codegen::VERSION, - ); + let producer = producer(); let comp_dir = tcx .sess .opts diff --git a/src/driver/aot.rs b/src/driver/aot.rs index f873561c1713f..27cce7c15e1f7 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -169,10 +169,22 @@ fn emit_cgu( fn emit_module( output_filenames: &OutputFilenames, prof: &SelfProfilerRef, - object: cranelift_object::object::write::Object<'_>, + mut object: cranelift_object::object::write::Object<'_>, kind: ModuleKind, name: String, ) -> Result { + if object.format() == cranelift_object::object::BinaryFormat::Elf { + let comment_section = object.add_section( + Vec::new(), + b".comment".to_vec(), + cranelift_object::object::SectionKind::OtherString, + ); + let mut producer = vec![0]; + producer.extend(crate::debuginfo::producer().as_bytes()); + producer.push(0); + object.set_section_data(comment_section, producer, 1); + } + let tmp_file = output_filenames.temp_path(OutputType::Object, Some(&name)); let mut file = match File::create(&tmp_file) { Ok(file) => file, From e082eebb5f3fceac83b5cca666e601e5ce0a964b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 13:59:29 +0000 Subject: [PATCH 010/101] Run verifier checks during rustc tests too Fixes #1219 --- .github/workflows/rustc.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index bef806318efa8..af34e10c7596d 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -41,6 +41,9 @@ jobs: # Enable backtraces for easier debugging export RUST_BACKTRACE=1 + # Enable extra checks + export CG_CLIF_ENABLE_VERIFIER=1 + ./scripts/test_bootstrap.sh rustc_test_suite: runs-on: ubuntu-latest @@ -79,4 +82,7 @@ jobs: # Enable backtraces for easier debugging export RUST_BACKTRACE=1 + # Enable extra checks + export CG_CLIF_ENABLE_VERIFIER=1 + ./scripts/test_rustc_tests.sh From cc3ac006a2fecc9a6543244798c9f13c73c3db8d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 14:12:46 +0000 Subject: [PATCH 011/101] Move many env vars from CI configuration to the build system --- .cirrus.yml | 5 ----- .github/workflows/main.yml | 23 +---------------------- .github/workflows/nightly-cranelift.yml | 13 +------------ .github/workflows/rustc.yml | 18 ++---------------- build_system/mod.rs | 6 ++++++ build_system/tests.rs | 7 +++++-- 6 files changed, 15 insertions(+), 57 deletions(-) diff --git a/.cirrus.yml b/.cirrus.yml index d627c2ee09c4e..76b48d70ab7c7 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -15,9 +15,4 @@ task: - ./y.rs prepare 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 - ./y.rs test diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 010979c9c33e4..3492df72720bc 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -104,18 +104,7 @@ jobs: - name: Test env: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - run: | - # 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 - - # Enable extra checks - export CG_CLIF_ENABLE_VERIFIER=1 - - ./y.rs test + run: ./y.rs test - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz dist @@ -195,16 +184,6 @@ jobs: - name: Test run: | - # Enable backtraces for easier debugging - $Env:RUST_BACKTRACE=1 - - # Reduce amount of benchmark runs as they are slow - $Env:COMPILE_RUNS=2 - $Env:RUN_RUNS=2 - - # Enable extra checks - $Env:CG_CLIF_ENABLE_VERIFIER=1 - # WIP Disable some tests # This fails due to some weird argument handling by hyperfine, not an actual regression diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index d0d58d2a7eacb..0565938ee3531 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -45,15 +45,4 @@ jobs: - name: Build run: ./y.rs build --sysroot none - name: Test - run: | - # 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 - - # Enable extra checks - export CG_CLIF_ENABLE_VERIFIER=1 - - ./test.sh + run: ./test.sh diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index af34e10c7596d..bab81b9dc2a81 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -37,14 +37,7 @@ jobs: ./y.rs prepare - name: Test - run: | - # Enable backtraces for easier debugging - export RUST_BACKTRACE=1 - - # Enable extra checks - export CG_CLIF_ENABLE_VERIFIER=1 - - ./scripts/test_bootstrap.sh + run: ./scripts/test_bootstrap.sh rustc_test_suite: runs-on: ubuntu-latest @@ -78,11 +71,4 @@ jobs: ./y.rs prepare - name: Test - run: | - # Enable backtraces for easier debugging - export RUST_BACKTRACE=1 - - # Enable extra checks - export CG_CLIF_ENABLE_VERIFIER=1 - - ./scripts/test_rustc_tests.sh + run: ./scripts/test_rustc_tests.sh diff --git a/build_system/mod.rs b/build_system/mod.rs index 1afc9a55c73b5..2f311aed7c8db 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -64,12 +64,18 @@ pub(crate) enum SysrootKind { } pub fn main() { + if env::var("RUST_BACKTRACE").is_err() { + env::set_var("RUST_BACKTRACE", "1"); + } env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); if is_ci() { // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway env::set_var("CARGO_BUILD_INCREMENTAL", "false"); + + // Enable the Cranelift verifier + env::set_var("CG_CLIF_ENABLE_VERIFIER", "1"); } let mut args = env::args().skip(1); diff --git a/build_system/tests.rs b/build_system/tests.rs index 738a76ef4c55a..81993b9d337dd 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -4,7 +4,7 @@ use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; use super::rustc_info::get_wrapper_file_name; use super::utils::{ - hyperfine_command, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, + hyperfine_command, is_ci, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, }; use super::SysrootKind; use std::env; @@ -281,7 +281,10 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } }), TestCase::new("bench.simple-raytracer", &|runner| { - let run_runs = env::var("RUN_RUNS").unwrap_or("10".to_string()).parse().unwrap(); + let run_runs = env::var("RUN_RUNS") + .unwrap_or(if is_ci() { "2" } else { "10" }.to_string()) + .parse() + .unwrap(); if runner.is_native { eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); From 6c88b08adcda8761e91c17d3b3bee9b7b36c9a8c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 15:09:17 +0000 Subject: [PATCH 012/101] Fix some build steps on Windows --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3492df72720bc..3f9e779dfc164 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -177,10 +177,10 @@ jobs: env: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} # This is the config rust-lang/rust uses for builds - run: ./y.rs build --no-unstable-features + run: ./y.exe build --no-unstable-features - name: Build - run: ./y.rs build --sysroot none + run: ./y.exe build --sysroot none - name: Test run: | From 1ca07b9a44a6de9c769a66ed98a5b165a42aee15 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 14:16:39 +0000 Subject: [PATCH 013/101] Fix running simple-raytracer benchmark on Windows --- .github/workflows/main.yml | 4 ---- build_system/tests.rs | 17 ++++++++++++----- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3f9e779dfc164..d181bd1604e02 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -186,10 +186,6 @@ jobs: run: | # WIP Disable some tests - # This fails due to some weird argument handling by hyperfine, not an actual regression - # more of a build system issue - (Get-Content config.txt) -replace '(bench.simple-raytracer)', '# $1' | Out-File config.txt - # This fails with a different output than expected (Get-Content config.txt) -replace '(test.regex-shootout-regex-dna)', '# $1' | Out-File config.txt diff --git a/build_system/tests.rs b/build_system/tests.rs index 81993b9d337dd..37c35106af6d3 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -2,7 +2,7 @@ use super::build_sysroot; use super::config; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; -use super::rustc_info::get_wrapper_file_name; +use super::rustc_info::{get_file_name, get_wrapper_file_name}; use super::utils::{ hyperfine_command, is_ci, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, }; @@ -318,13 +318,20 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ eprintln!("[BENCH RUN] ebobby/simple-raytracer"); fs::copy( - target_dir.join("debug").join("main"), - RelPath::BUILD.to_path(&runner.dirs).join("raytracer_cg_clif"), + target_dir.join("debug").join(get_file_name("main", "bin")), + RelPath::BUILD + .to_path(&runner.dirs) + .join(get_file_name("raytracer_cg_clif", "bin")), ) .unwrap(); - let mut bench_run = - hyperfine_command(0, run_runs, None, "./raytracer_cg_llvm", "./raytracer_cg_clif"); + let mut bench_run = hyperfine_command( + 0, + run_runs, + None, + Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(), + Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(), + ); bench_run.current_dir(RelPath::BUILD.to_path(&runner.dirs)); spawn_and_wait(bench_run); } else { From 280fb2935b2bc4f4eb56671d21d7bc2b36faa81e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:30:18 +0000 Subject: [PATCH 014/101] Start running regex-shootout-regex-dna on Windows again --- .github/workflows/main.yml | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d181bd1604e02..e381f7e6adaab 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -183,13 +183,7 @@ jobs: run: ./y.exe build --sysroot none - name: Test - run: | - # WIP Disable some tests - - # This fails with a different output than expected - (Get-Content config.txt) -replace '(test.regex-shootout-regex-dna)', '# $1' | Out-File config.txt - - ./y.exe test + run: ./y.exe test - name: Package prebuilt cg_clif # don't use compression as xzip isn't supported by tar on windows and bzip2 hangs From e735206f50af9754ab21171e4472cfbe1eecd92f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 15 Dec 2022 17:58:27 +0000 Subject: [PATCH 015/101] Unify Windows and non-Windows CI --- .github/workflows/main.yml | 102 +++++++++---------------------------- 1 file changed, 25 insertions(+), 77 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e381f7e6adaab..b79406879ff32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,6 +25,10 @@ jobs: runs-on: ${{ matrix.os }} timeout-minutes: 60 + defaults: + run: + shell: bash + strategy: fail-fast: false matrix: @@ -46,6 +50,14 @@ jobs: - os: ubuntu-latest env: TARGET_TRIPLE: s390x-unknown-linux-gnu + # Native Windows build with MSVC + - os: windows-latest + env: + TARGET_TRIPLE: x86_64-pc-windows-msvc + # cross-compile from Windows to Windows MinGW + - os: windows-latest + env: + TARGET_TRIPLE: x86_64-pc-windows-gnu steps: - uses: actions/checkout@v3 @@ -54,7 +66,7 @@ jobs: uses: actions/cache@v3 with: path: ~/.cargo/bin - key: ${{ runner.os }}-cargo-installed-crates + key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-installed-crates - name: Cache cargo registry and index uses: actions/cache@v3 @@ -62,13 +74,17 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - key: ${{ runner.os }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} + key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - name: Cache cargo target dir uses: actions/cache@v3 with: path: build/cg_clif - key: ${{ runner.os }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} + key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} + + - name: Set MinGW as the default toolchain + if: matrix.os == 'windows-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' + run: rustup set default-host x86_64-pc-windows-gnu - name: Install MinGW toolchain and wine if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' @@ -89,6 +105,10 @@ jobs: sudo apt-get update sudo apt-get install -y gcc-s390x-linux-gnu qemu-user + - name: Windows setup + if: matrix.os == 'windows-latest' + run: git config --global core.autocrlf false + - name: Prepare dependencies run: ./y.rs prepare @@ -110,87 +130,15 @@ jobs: run: tar cvfJ cg_clif.tar.xz dist - name: Upload prebuilt cg_clif - if: matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu' + if: matrix.os == 'windows-latest' || matrix.env.TARGET_TRIPLE != 'x86_64-pc-windows-gnu' uses: actions/upload-artifact@v3 with: name: cg_clif-${{ matrix.env.TARGET_TRIPLE }} path: cg_clif.tar.xz - name: Upload prebuilt cg_clif (cross compile) - if: matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' + if: matrix.os != 'windows-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' uses: actions/upload-artifact@v3 with: name: cg_clif-${{ runner.os }}-cross-x86_64-mingw path: cg_clif.tar.xz - - windows: - runs-on: ${{ matrix.os }} - timeout-minutes: 60 - - strategy: - fail-fast: false - matrix: - include: - # Native Windows build with MSVC - - os: windows-latest - env: - TARGET_TRIPLE: x86_64-pc-windows-msvc - # cross-compile from Windows to Windows MinGW - - os: windows-latest - env: - TARGET_TRIPLE: x86_64-pc-windows-gnu - - steps: - - uses: actions/checkout@v3 - - - name: Cache cargo installed crates - uses: actions/cache@v3 - with: - path: ~/.cargo/bin - key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-installed-crates - - - name: Cache cargo registry and index - uses: actions/cache@v3 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - - - name: Cache cargo target dir - uses: actions/cache@v3 - with: - path: build/cg_clif - key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} - - - name: Set MinGW as the default toolchain - if: matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' - run: rustup set default-host x86_64-pc-windows-gnu - - - name: Prepare dependencies - run: | - git config --global core.autocrlf false - rustc y.rs -o y.exe -g - ./y.exe prepare - - - name: Build without unstable features - env: - TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - # This is the config rust-lang/rust uses for builds - run: ./y.exe build --no-unstable-features - - - name: Build - run: ./y.exe build --sysroot none - - - name: Test - run: ./y.exe test - - - name: Package prebuilt cg_clif - # don't use compression as xzip isn't supported by tar on windows and bzip2 hangs - run: tar cvf cg_clif.tar dist - - - name: Upload prebuilt cg_clif - uses: actions/upload-artifact@v3 - with: - name: cg_clif-${{ matrix.env.TARGET_TRIPLE }} - path: cg_clif.tar From 9ca82a9a3d987985aea5c87b3d4d1546cb5d04c9 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 09:41:36 +0000 Subject: [PATCH 016/101] Fix ICE on unsized locals Fixes #1312 --- src/abi/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 65cc6b4376713..416bce4849824 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -161,6 +161,12 @@ fn make_local_place<'tcx>( layout: TyAndLayout<'tcx>, is_ssa: bool, ) -> CPlace<'tcx> { + if layout.is_unsized() { + fx.tcx.sess.span_fatal( + fx.mir.local_decls[local].source_info.span, + "unsized locals are not yet supported", + ); + } let place = if is_ssa { if let rustc_target::abi::Abi::ScalarPair(_, _) = layout.abi { CPlace::new_var_pair(fx, local, layout) From b5ac64b4cf33809b182a6f7c63a7c4e874fb3056 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 09:44:28 +0000 Subject: [PATCH 017/101] Don't PrintOnPanic on fatal errors --- src/base.rs | 11 ++++++----- src/driver/mod.rs | 3 ++- src/lib.rs | 8 ++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/base.rs b/src/base.rs index 89d955e8bf2e1..7df11cf4aede1 100644 --- a/src/base.rs +++ b/src/base.rs @@ -29,8 +29,9 @@ pub(crate) fn codegen_and_compile_fn<'tcx>( module: &mut dyn Module, instance: Instance<'tcx>, ) { - let _inst_guard = - crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name)); + let _inst_guard = crate::PrintOnPanic(Some(tcx.sess), || { + format!("{:?} {}", instance, tcx.symbol_name(instance).name) + }); let cached_func = std::mem::replace(&mut cached_context.func, Function::new()); let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance); @@ -48,7 +49,7 @@ pub(crate) fn codegen_fn<'tcx>( debug_assert!(!instance.substs.needs_infer()); let mir = tcx.instance_mir(instance.def); - let _mir_guard = crate::PrintOnPanic(|| { + let _mir_guard = crate::PrintOnPanic(Some(tcx.sess), || { let mut buf = Vec::new(); with_no_trimmed_paths!({ rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf) @@ -176,7 +177,7 @@ pub(crate) fn compile_fn( write!(clif, " {}", isa_flag).unwrap(); } writeln!(clif, "\n").unwrap(); - crate::PrintOnPanic(move || { + crate::PrintOnPanic(None, move || { let mut clif = clif.clone(); ::cranelift_codegen::write::decorate_function( &mut &clif_comments_clone, @@ -497,7 +498,7 @@ fn codegen_stmt<'tcx>( #[allow(unused_variables)] cur_block: Block, stmt: &Statement<'tcx>, ) { - let _print_guard = crate::PrintOnPanic(|| format!("stmt {:?}", stmt)); + let _print_guard = crate::PrintOnPanic(Some(fx.tcx.sess), || format!("stmt {:?}", stmt)); fx.set_debug_loc(stmt.source_info); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 6e925cea27707..1ab4fdf61551b 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -23,7 +23,8 @@ fn predefine_mono_items<'tcx>( match mono_item { MonoItem::Fn(instance) => { let name = tcx.symbol_name(instance).name; - let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); + let _inst_guard = + crate::PrintOnPanic(Some(tcx.sess), || format!("{:?} {}", instance, name)); let sig = get_function_sig(tcx, module.target_config().default_call_conv, instance); let linkage = crate::linkage::get_clif_linkage( diff --git a/src/lib.rs b/src/lib.rs index 629d79d501240..49b5dc32144d2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,11 +113,11 @@ mod prelude { pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue}; } -struct PrintOnPanic String>(F); -impl String> Drop for PrintOnPanic { +struct PrintOnPanic<'a, F: Fn() -> String>(Option<&'a Session>, F); +impl<'a, F: Fn() -> String> Drop for PrintOnPanic<'a, F> { fn drop(&mut self) { - if ::std::thread::panicking() { - println!("{}", (self.0)()); + if ::std::thread::panicking() && self.0.map_or(true, |sess| sess.has_errors().is_none()) { + println!("{}", (self.1)()); } } } From ec92c3e5dca69cf0ed02d11fb7a3bfae9f41c0ca Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 09:55:27 +0000 Subject: [PATCH 018/101] Fix ICE on incompatible declarations of entry symbol Fixes #1313 --- src/main_shim.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/main_shim.rs b/src/main_shim.rs index c10054e7f0d2c..556d7b8e51a67 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -70,7 +70,13 @@ pub(crate) fn maybe_create_entry_wrapper( }; let entry_name = tcx.sess.target.options.entry_name.as_ref(); - let cmain_func_id = m.declare_function(entry_name, Linkage::Export, &cmain_sig).unwrap(); + let cmain_func_id = match m.declare_function(entry_name, Linkage::Export, &cmain_sig) { + Ok(func_id) => func_id, + Err(err) => { + tcx.sess + .fatal(&format!("entry symbol `{entry_name}` declared multiple times: {err}")); + } + }; let instance = Instance::mono(tcx, rust_main_def_id).polymorphize(tcx); @@ -162,7 +168,11 @@ pub(crate) fn maybe_create_entry_wrapper( bcx.seal_all_blocks(); bcx.finalize(); } - m.define_function(cmain_func_id, &mut ctx).unwrap(); + + if let Err(err) = m.define_function(cmain_func_id, &mut ctx) { + tcx.sess.fatal(&format!("entry symbol `{entry_name}` defined multiple times: {err}")); + } + unwind_context.add_function(cmain_func_id, &ctx, m.isa()); } } From 91655428f905b35c4a14d38562113eb768252fdf Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:04:40 +0000 Subject: [PATCH 019/101] Ensure Cranelift errors are reported deterministically This may also have been the root cause of #1310. --- src/driver/aot.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/driver/aot.rs b/src/driver/aot.rs index 27cce7c15e1f7..d4494a9e45de4 100644 --- a/src/driver/aot.rs +++ b/src/driver/aot.rs @@ -108,6 +108,8 @@ impl OngoingCodegen { self.concurrency_limiter.finished(); + sess.abort_if_errors(); + ( CodegenResults { modules, @@ -411,8 +413,6 @@ pub(crate) fn run_aot( .collect::>() }); - tcx.sess.abort_if_errors(); - let mut allocator_module = make_module(tcx.sess, &backend_config, "allocator_shim".to_string()); let mut allocator_unwind_context = UnwindContext::new(allocator_module.isa(), true); let created_alloc_shim = From 14ca1df478ac9c0a7899530eb581362f37519ad3 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 10:38:46 +0000 Subject: [PATCH 020/101] Enable debug assertions on CI Fixes #1314 --- build_system/build_backend.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index fde8ef424ccc5..16b43c5fc852f 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -25,6 +25,8 @@ pub(crate) fn build_backend( // Disabling incr comp reduces cache size and incr comp doesn't save as much on CI anyway cmd.env("CARGO_BUILD_INCREMENTAL", "false"); + + cmd.env("CARGO_PROFILE_RELEASE_DEBUG_ASSERTIONS", "true"); } if use_unstable_features { From ab0e2aaed2098cecf5eb9e2c2ee196e794d79db9 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 13:00:36 +0000 Subject: [PATCH 021/101] Revert "Don't PrintOnPanic on fatal errors" This reverts commit b5ac64b4cf33809b182a6f7c63a7c4e874fb3056. It entirely breaks PrintOnPanic as ICE seems to be considered a fatal error too. --- src/base.rs | 11 +++++------ src/driver/mod.rs | 3 +-- src/lib.rs | 8 ++++---- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/base.rs b/src/base.rs index 7df11cf4aede1..89d955e8bf2e1 100644 --- a/src/base.rs +++ b/src/base.rs @@ -29,9 +29,8 @@ pub(crate) fn codegen_and_compile_fn<'tcx>( module: &mut dyn Module, instance: Instance<'tcx>, ) { - let _inst_guard = crate::PrintOnPanic(Some(tcx.sess), || { - format!("{:?} {}", instance, tcx.symbol_name(instance).name) - }); + let _inst_guard = + crate::PrintOnPanic(|| format!("{:?} {}", instance, tcx.symbol_name(instance).name)); let cached_func = std::mem::replace(&mut cached_context.func, Function::new()); let codegened_func = codegen_fn(tcx, cx, cached_func, module, instance); @@ -49,7 +48,7 @@ pub(crate) fn codegen_fn<'tcx>( debug_assert!(!instance.substs.needs_infer()); let mir = tcx.instance_mir(instance.def); - let _mir_guard = crate::PrintOnPanic(Some(tcx.sess), || { + let _mir_guard = crate::PrintOnPanic(|| { let mut buf = Vec::new(); with_no_trimmed_paths!({ rustc_middle::mir::pretty::write_mir_fn(tcx, mir, &mut |_, _| Ok(()), &mut buf) @@ -177,7 +176,7 @@ pub(crate) fn compile_fn( write!(clif, " {}", isa_flag).unwrap(); } writeln!(clif, "\n").unwrap(); - crate::PrintOnPanic(None, move || { + crate::PrintOnPanic(move || { let mut clif = clif.clone(); ::cranelift_codegen::write::decorate_function( &mut &clif_comments_clone, @@ -498,7 +497,7 @@ fn codegen_stmt<'tcx>( #[allow(unused_variables)] cur_block: Block, stmt: &Statement<'tcx>, ) { - let _print_guard = crate::PrintOnPanic(Some(fx.tcx.sess), || format!("stmt {:?}", stmt)); + let _print_guard = crate::PrintOnPanic(|| format!("stmt {:?}", stmt)); fx.set_debug_loc(stmt.source_info); diff --git a/src/driver/mod.rs b/src/driver/mod.rs index 1ab4fdf61551b..6e925cea27707 100644 --- a/src/driver/mod.rs +++ b/src/driver/mod.rs @@ -23,8 +23,7 @@ fn predefine_mono_items<'tcx>( match mono_item { MonoItem::Fn(instance) => { let name = tcx.symbol_name(instance).name; - let _inst_guard = - crate::PrintOnPanic(Some(tcx.sess), || format!("{:?} {}", instance, name)); + let _inst_guard = crate::PrintOnPanic(|| format!("{:?} {}", instance, name)); let sig = get_function_sig(tcx, module.target_config().default_call_conv, instance); let linkage = crate::linkage::get_clif_linkage( diff --git a/src/lib.rs b/src/lib.rs index 49b5dc32144d2..629d79d501240 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -113,11 +113,11 @@ mod prelude { pub(crate) use crate::value_and_place::{CPlace, CPlaceInner, CValue}; } -struct PrintOnPanic<'a, F: Fn() -> String>(Option<&'a Session>, F); -impl<'a, F: Fn() -> String> Drop for PrintOnPanic<'a, F> { +struct PrintOnPanic String>(F); +impl String> Drop for PrintOnPanic { fn drop(&mut self) { - if ::std::thread::panicking() && self.0.map_or(true, |sess| sess.has_errors().is_none()) { - println!("{}", (self.1)()); + if ::std::thread::panicking() { + println!("{}", (self.0)()); } } } From 1c724ee6d0e1023a5cd9b8b5c0bf324b45d429c5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 13:29:26 +0000 Subject: [PATCH 022/101] Re-enable some rustc tests --- scripts/test_rustc_tests.sh | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 04ad77ec97eac..b3d6dae515359 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -37,15 +37,7 @@ rm -r src/test/run-make/test-benches rm src/test/ui/sse2.rs # cpuid not supported, so sse2 not detected rm src/test/ui/intrinsics/const-eval-select-x86_64.rs # requires x86_64 vendor intrinsics rm src/test/ui/simd/array-type.rs # "Index argument for `simd_insert` is not a constant" -rm src/test/ui/simd/intrinsic/generic-bitmask-pass.rs # simd_bitmask unimplemented -rm src/test/ui/simd/intrinsic/generic-as.rs # simd_as unimplemented -rm src/test/ui/simd/intrinsic/generic-arithmetic-saturating-pass.rs # simd_saturating_add unimplemented rm src/test/ui/simd/intrinsic/float-math-pass.rs # simd_fcos unimplemented -rm src/test/ui/simd/intrinsic/generic-gather-pass.rs # simd_gather unimplemented -rm src/test/ui/simd/intrinsic/generic-select-pass.rs # simd_select_bitmask unimplemented -rm src/test/ui/simd/issue-85915-simd-ptrs.rs # simd_gather unimplemented -rm src/test/ui/simd/issue-89193.rs # simd_gather unimplemented -rm src/test/ui/simd/simd-bitmask.rs # simd_bitmask unimplemented # exotic linkages rm src/test/ui/issues/issue-33992.rs # unsupported linkages @@ -102,13 +94,11 @@ rm -r src/test/ui/consts/missing_span_in_backtrace.rs # expects sysroot source t # ============ rm src/test/incremental/spike-neg1.rs # errors out for some reason rm src/test/incremental/spike-neg2.rs # same -rm src/test/ui/issues/issue-74564-if-expr-stack-overflow.rs # gives a stackoverflow before the backend runs -rm src/test/ui/mir/ssa-analysis-regression-50041.rs # produces ICE -rm src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs # produces ICE rm src/test/ui/simd/intrinsic/generic-reduction-pass.rs # simd_reduce_add_unordered doesn't accept an accumulator for integer vectors -rm src/test/ui/runtime/out-of-stack.rs # SIGSEGV instead of SIGABRT for some reason (#1301) +rm src/test/ui/simd/intrinsic/generic-as.rs # crash when accessing vector type filed (#1318) +rm src/test/ui/simd/simd-bitmask.rs # crash # bugs in the test suite # ====================== From dffa6acf731c6019a39d0175e242b821a3423915 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 14:15:59 +0000 Subject: [PATCH 023/101] Fix crash after simd type validation error Fixes part of #1319 --- src/intrinsics/mod.rs | 3 +-- src/intrinsics/simd.rs | 12 ++++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 7a380acf79857..548a6bf45842b 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -240,10 +240,9 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( substs, args, destination, + target, source_info.span, ); - let ret_block = fx.get_block(target); - fx.bcx.ins().jump(ret_block, &[]); } else if codegen_float_intrinsic_call(fx, intrinsic, args, destination) { let ret_block = fx.get_block(target); fx.bcx.ins().jump(ret_block, &[]); diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 62ea2214ab44e..791ff5cfcf3c6 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -24,6 +24,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( _substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], ret: CPlace<'tcx>, + target: BasicBlock, span: Span, ) { match intrinsic { @@ -277,16 +278,15 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( } else { fx.tcx.sess.span_warn(span, "Index argument for `simd_extract` is not a constant"); let trap_block = fx.bcx.create_block(); - let dummy_block = fx.bcx.create_block(); let true_ = fx.bcx.ins().iconst(types::I8, 1); fx.bcx.ins().brnz(true_, trap_block, &[]); - fx.bcx.ins().jump(dummy_block, &[]); + let ret_block = fx.get_block(target); + fx.bcx.ins().jump(ret_block, &[]); fx.bcx.switch_to_block(trap_block); crate::trap::trap_unimplemented( fx, "Index argument for `simd_extract` is not a constant", ); - fx.bcx.switch_to_block(dummy_block); return; }; @@ -876,7 +876,11 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( } _ => { - fx.tcx.sess.span_fatal(span, &format!("Unknown SIMD intrinsic {}", intrinsic)); + fx.tcx.sess.span_err(span, &format!("Unknown SIMD intrinsic {}", intrinsic)); + // Prevent verifier error + fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } } + let ret_block = fx.get_block(target); + fx.bcx.ins().jump(ret_block, &[]); } From ee05e4d5ab76a94f46705e9afb01357d138fd519 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 15:41:52 +0000 Subject: [PATCH 024/101] Don't ICE on unimplemented call convs --- src/abi/mod.rs | 27 +++++++++++++++++---------- src/main_shim.rs | 1 + 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 416bce4849824..90ec8ec3685e3 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -7,6 +7,7 @@ mod returning; use cranelift_module::ModuleError; use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; use rustc_middle::ty::layout::FnAbiOf; +use rustc_session::Session; use rustc_target::abi::call::{Conv, FnAbi}; use rustc_target::spec::abi::Abi; @@ -22,7 +23,7 @@ fn clif_sig_from_fn_abi<'tcx>( default_call_conv: CallConv, fn_abi: &FnAbi<'tcx, Ty<'tcx>>, ) -> Signature { - let call_conv = conv_to_call_conv(fn_abi.conv, default_call_conv); + let call_conv = conv_to_call_conv(tcx.sess, fn_abi.conv, default_call_conv); let inputs = fn_abi.args.iter().map(|arg_abi| arg_abi.get_abi_param(tcx).into_iter()).flatten(); @@ -33,24 +34,30 @@ fn clif_sig_from_fn_abi<'tcx>( Signature { params, returns, call_conv } } -pub(crate) fn conv_to_call_conv(c: Conv, default_call_conv: CallConv) -> CallConv { +pub(crate) fn conv_to_call_conv(sess: &Session, c: Conv, default_call_conv: CallConv) -> CallConv { match c { Conv::Rust | Conv::C => default_call_conv, Conv::RustCold => CallConv::Cold, Conv::X86_64SysV => CallConv::SystemV, Conv::X86_64Win64 => CallConv::WindowsFastcall, - Conv::ArmAapcs - | Conv::CCmseNonSecureCall + + // Should already get a back compat warning + Conv::X86Fastcall | Conv::X86Stdcall | Conv::X86ThisCall | Conv::X86VectorCall => { + default_call_conv + } + + Conv::X86Intr => sess.fatal("x86-interrupt call conv not yet implemented"), + + Conv::ArmAapcs => sess.fatal("aapcs call conv not yet implemented"), + + Conv::CCmseNonSecureCall | Conv::Msp430Intr | Conv::PtxKernel - | Conv::X86Fastcall - | Conv::X86Intr - | Conv::X86Stdcall - | Conv::X86ThisCall - | Conv::X86VectorCall | Conv::AmdGpuKernel | Conv::AvrInterrupt - | Conv::AvrNonBlockingInterrupt => todo!("{:?}", c), + | Conv::AvrNonBlockingInterrupt => { + unreachable!("tried to use {c:?} call conv which only exists on an unsupported target"); + } } } diff --git a/src/main_shim.rs b/src/main_shim.rs index 556d7b8e51a67..fd45362548c0d 100644 --- a/src/main_shim.rs +++ b/src/main_shim.rs @@ -64,6 +64,7 @@ pub(crate) fn maybe_create_entry_wrapper( ], returns: vec![AbiParam::new(m.target_config().pointer_type() /*isize*/)], call_conv: crate::conv_to_call_conv( + tcx.sess, tcx.sess.target.options.entry_abi, m.target_config().default_call_conv, ), From 8b478012086f4df5c21e4ec0016631fac163133f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 16 Dec 2022 15:45:07 +0000 Subject: [PATCH 025/101] Don't ICE on C-cmse-nonsecure-call either --- src/abi/mod.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/abi/mod.rs b/src/abi/mod.rs index 90ec8ec3685e3..3c34585d4191e 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -49,9 +49,11 @@ pub(crate) fn conv_to_call_conv(sess: &Session, c: Conv, default_call_conv: Call Conv::X86Intr => sess.fatal("x86-interrupt call conv not yet implemented"), Conv::ArmAapcs => sess.fatal("aapcs call conv not yet implemented"), + Conv::CCmseNonSecureCall => { + sess.fatal("C-cmse-nonsecure-call call conv is not yet implemented"); + } - Conv::CCmseNonSecureCall - | Conv::Msp430Intr + Conv::Msp430Intr | Conv::PtxKernel | Conv::AmdGpuKernel | Conv::AvrInterrupt From debd45cc7be97c8a7c50c2822593471d04d87366 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 13 Dec 2022 18:34:08 +0100 Subject: [PATCH 026/101] Update to cranelift 0.91 Closes #1307 --- Cargo.lock | 94 ++++++++++++++++++++++---------------- Cargo.toml | 12 ++--- src/base.rs | 5 +- src/common.rs | 9 ++++ src/intrinsics/llvm_x86.rs | 4 +- src/intrinsics/mod.rs | 2 +- src/value_and_place.rs | 4 +- 7 files changed, 76 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4d3e9ca5ae0a..3bc2bf31d666d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,18 +57,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62c772976416112fa4484cbd688cb6fb35fd430005c1c586224fc014018abad" +checksum = "fc952b310b24444fc14ab8b9cbe3fafd7e7329e3eec84c3a9b11d2b5cf6f3be1" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b40ed2dd13c2ac7e24f88a3090c68ad3414eb1d066a95f8f1f7b3b819cb4e46" +checksum = "e73470419b33011e50dbf0f6439cbccbaabe9381de172da4e1b6efcda4bb8fa7" dependencies = [ "arrayvec", "bumpalo", @@ -87,24 +87,24 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bb927a8f1c27c34ee3759b6b0ffa528d2330405d5cc4511f0cab33fe2279f4b5" +checksum = "911a1872464108a11ac9965c2b079e61bbdf1bc2e0b9001264264add2e12a38f" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "43dfa417b884a9ab488d95fd6b93b25e959321fe7bfd7a0a960ba5d7fb7ab927" +checksum = "e036f3f07adb24a86fb46e977e8fe03b18bb16b1eada949cf2c48283e5f8a862" [[package]] name = "cranelift-egraph" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0a66b39785efd8513d2cca967ede56d6cc57c8d7986a595c7c47d0c78de8dce" +checksum = "2d6c623f4b5d2a6bad32c403f03765d4484a827eb93ee78f8cb6219ef118fd59" dependencies = [ "cranelift-entity", "fxhash", @@ -116,15 +116,15 @@ dependencies = [ [[package]] name = "cranelift-entity" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0637ffde963cb5d759bc4d454cfa364b6509e6c74cdaa21298add0ed9276f346" +checksum = "74385eb5e405b3562f0caa7bcc4ab9a93c7958dd5bcd0e910bffb7765eacd6fc" [[package]] name = "cranelift-frontend" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb72b8342685e850cb037350418f62cc4fc55d6c2eb9c7ca01b82f9f1a6f3d56" +checksum = "8a4ac920422ee36bff2c66257fec861765e3d95a125cdf58d8c0f3bba7e40e61" dependencies = [ "cranelift-codegen", "log", @@ -134,15 +134,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "850579cb9e4b448f7c301f1e6e6cbad99abe3f1f1d878a4994cb66e33c6db8cd" +checksum = "c541263fb37ad2baa53ec8c37218ee5d02fa0984670d9419dedd8002ea68ff08" [[package]] name = "cranelift-jit" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9add822ad66dcbe152b5ab57de10240a2df4505099f2f6c27159acb711890bd4" +checksum = "48a844e3500d313b69f3eec4b4e15bf9cdbd529756add06a468e0e281c0f6bee" dependencies = [ "anyhow", "cranelift-codegen", @@ -159,9 +159,9 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "406b772626fc2664864cf947f3895a23b619895c7fff635f3622e2d857f4492f" +checksum = "0699ea5fc6ca943456ba80ad49f80212bd6e2b846b992ec59f0f2b912a1d25fa" dependencies = [ "anyhow", "cranelift-codegen", @@ -169,9 +169,9 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d0a279e5bcba3e0466c734d8d8eb6bfc1ad29e95c37f3e4955b492b5616335e" +checksum = "1de5d7a063e8563d670aaca38de16591a9b70dc66cbad4d49a7b4ae8395fd1ce" dependencies = [ "cranelift-codegen", "libc", @@ -180,9 +180,9 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.90.1" +version = "0.91.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39793c550f0c1d7db96c2fc1324583670c8143befe6edbfbaf1c68aba53be983" +checksum = "307735148f6a556388aabf1ea31f46ccd378ed0739f3e9bdda2029639d701ab7" dependencies = [ "anyhow", "cranelift-codegen", @@ -317,9 +317,9 @@ checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" [[package]] name = "regalloc2" -version = "0.4.2" +version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91b2eab54204ea0117fe9a060537e0b07a4e72f7c7d182361ecc346cab2240e5" +checksum = "300d4fbfb40c1c66a78ba3ddd41c1110247cf52f97b87d0f2fc9209bd49b030c" dependencies = [ "fxhash", "log", @@ -396,9 +396,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasmtime-jit-icache-coherence" -version = "2.0.1" +version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6bbabb309c06cc238ee91b1455b748c45f0bdcab0dda2c2db85b0a1e69fcb66" +checksum = "22d9c2e92b0fc124d2cad6cb497a4c840580a7dd2414a37109e8c7cfe699c0ea" dependencies = [ "cfg-if", "libc", @@ -429,43 +429,57 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "windows-sys" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04155a16a59f9eab786fe12a4a450e75cdb175f9e0d80da1e17db09f55b8d2" +checksum = "5a3e1820f08b8513f676f7ab6c1f99ff312fb97b553d30ff4dd86f9f15728aa7" dependencies = [ + "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", + "windows_x86_64_gnullvm", "windows_x86_64_msvc", ] +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d2aa71f6f0cbe00ae5167d90ef3cfe66527d6f613ca78ac8024c3ccab9a19e" + [[package]] name = "windows_aarch64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9bb8c3fd39ade2d67e9874ac4f3db21f0d710bee00fe7cab16949ec184eeaa47" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" [[package]] name = "windows_i686_gnu" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "180e6ccf01daf4c426b846dfc66db1fc518f074baa793aa7d9b9aaeffad6a3b6" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" [[package]] name = "windows_i686_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2e7917148b2812d1eeafaeb22a97e4813dfa60a3f8f78ebe204bcc88f12f024" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" [[package]] name = "windows_x86_64_gnu" -version = "0.36.1" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dcd171b8776c41b97521e5da127a2d86ad280114807d0b2ab1e462bc764d9e1" +checksum = "09d525d2ba30eeb3297665bd434a54297e4170c7f1a44cad4ef58095b4cd2028" [[package]] name = "windows_x86_64_msvc" -version = "0.36.1" +version = "0.42.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c811ca4a8c853ef420abd8592ba53ddbbac90410fab6903b3e79972a631f7680" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" diff --git a/Cargo.toml b/Cargo.toml index 2b216ca072f00..f03cc34a81081 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,12 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { version = "0.90.1", features = ["unwind", "all-arch"] } -cranelift-frontend = "0.90.1" -cranelift-module = "0.90.1" -cranelift-native = "0.90.1" -cranelift-jit = { version = "0.90.1", optional = true } -cranelift-object = "0.90.1" +cranelift-codegen = { version = "0.91", features = ["unwind", "all-arch"] } +cranelift-frontend = "0.91" +cranelift-module = "0.91" +cranelift-native = "0.91" +cranelift-jit = { version = "0.91", optional = true } +cranelift-object = "0.91" target-lexicon = "0.12.0" gimli = { version = "0.26.0", default-features = false, features = ["write"]} object = { version = "0.29.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] } diff --git a/src/base.rs b/src/base.rs index 89d955e8bf2e1..0df3ffc4bd890 100644 --- a/src/base.rs +++ b/src/base.rs @@ -113,6 +113,8 @@ pub(crate) fn codegen_fn<'tcx>( }; tcx.sess.time("codegen clif ir", || codegen_fn_body(&mut fx, start_block)); + fx.bcx.seal_all_blocks(); + fx.bcx.finalize(); // Recover all necessary data from fx, before accessing func will prevent future access to it. let symbol_name = fx.symbol_name; @@ -487,9 +489,6 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { } }; } - - fx.bcx.seal_all_blocks(); - fx.bcx.finalize(); } fn codegen_stmt<'tcx>( diff --git a/src/common.rs b/src/common.rs index 2dcd42fbd8f43..869977104e361 100644 --- a/src/common.rs +++ b/src/common.rs @@ -167,6 +167,15 @@ pub(crate) fn codegen_icmp_imm( } } +pub(crate) fn codegen_bitcast(fx: &mut FunctionCx<'_, '_, '_>, dst_ty: Type, val: Value) -> Value { + let mut flags = MemFlags::new(); + flags.set_endianness(match fx.tcx.data_layout.endian { + rustc_target::abi::Endian::Big => cranelift_codegen::ir::Endianness::Big, + rustc_target::abi::Endian::Little => cranelift_codegen::ir::Endianness::Little, + }); + fx.bcx.ins().bitcast(dst_ty, flags, val) +} + pub(crate) fn type_zero_value(bcx: &mut FunctionBuilder<'_>, ty: Type) -> Value { if ty == types::I128 { let zero = bcx.ins().iconst(types::I64, 0); diff --git a/src/intrinsics/llvm_x86.rs b/src/intrinsics/llvm_x86.rs index 7bc161fbe5523..d2ae6978ca2a8 100644 --- a/src/intrinsics/llvm_x86.rs +++ b/src/intrinsics/llvm_x86.rs @@ -33,8 +33,8 @@ pub(crate) fn codegen_x86_llvm_intrinsic_call<'tcx>( // cast float to int let a_lane = match lane_ty { - types::F32 => fx.bcx.ins().bitcast(types::I32, a_lane), - types::F64 => fx.bcx.ins().bitcast(types::I64, a_lane), + types::F32 => codegen_bitcast(fx, types::I32, a_lane), + types::F64 => codegen_bitcast(fx, types::I64, a_lane), _ => a_lane, }; diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 548a6bf45842b..3681154e7888f 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -200,7 +200,7 @@ fn bool_to_zero_or_max_uint<'tcx>( let mut res = fx.bcx.ins().bmask(int_ty, val); if ty.is_float() { - res = fx.bcx.ins().bitcast(ty, res); + res = codegen_bitcast(fx, ty, res); } res diff --git a/src/value_and_place.rs b/src/value_and_place.rs index fe8af21ac6de5..fa06d6c3ba7f3 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -514,8 +514,8 @@ impl<'tcx> CPlace<'tcx> { (types::I32, types::F32) | (types::F32, types::I32) | (types::I64, types::F64) - | (types::F64, types::I64) => fx.bcx.ins().bitcast(dst_ty, data), - _ if src_ty.is_vector() && dst_ty.is_vector() => fx.bcx.ins().bitcast(dst_ty, data), + | (types::F64, types::I64) => codegen_bitcast(fx, dst_ty, data), + _ if src_ty.is_vector() && dst_ty.is_vector() => codegen_bitcast(fx, dst_ty, data), _ if src_ty.is_vector() || dst_ty.is_vector() => { // FIXME do something more efficient for transmutes between vectors and integers. let stack_slot = fx.bcx.create_sized_stack_slot(StackSlotData { From 97e2213c3c31909de388943ac3b1cb024decd6c2 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 22 Dec 2022 14:02:41 +0000 Subject: [PATCH 027/101] Enable inline stack probes on AArch64 Fixes #661 --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 629d79d501240..70d0cc339a80c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -278,12 +278,14 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Box Date: Wed, 4 Jan 2023 14:45:19 +0000 Subject: [PATCH 028/101] Enable some fixed rustc tests cc #381 --- scripts/test_rustc_tests.sh | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index b3d6dae515359..ca07c59c3925b 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -11,7 +11,7 @@ pushd rust command -v rg >/dev/null 2>&1 || cargo install ripgrep rm -r src/test/ui/{extern/,unsized-locals/,lto/,linkage*} || true -for test in $(rg --files-with-matches "lto|// needs-asm-support|// needs-unwind" src/test/{ui,incremental}); do +for test in $(rg --files-with-matches "lto|// needs-asm-support|// needs-unwind" src/test/{codegen-units,ui,incremental}); do rm $test done @@ -32,6 +32,7 @@ rm src/test/incremental/issue-80691-bad-eval-cache.rs # -Cpanic=abort causes abo # requires compiling with -Cpanic=unwind rm -r src/test/ui/macros/rfc-2011-nicer-assert-messages/ rm -r src/test/run-make/test-benches +rm src/test/ui/test-attrs/test-type.rs # vendor intrinsics rm src/test/ui/sse2.rs # cpuid not supported, so sse2 not detected @@ -56,10 +57,7 @@ rm src/test/ui/intrinsics/intrinsic-nearby.rs # unimplemented nearbyintf32 and n rm src/test/ui/target-feature/missing-plusminus.rs # error not implemented rm src/test/ui/fn/dyn-fn-alignment.rs # wants a 256 byte alignment rm -r src/test/run-make/emit-named-files # requires full --emit support -rm src/test/ui/abi/stack-probes.rs # stack probes not yet implemented -rm src/test/ui/simd/intrinsic/ptr-cast.rs # simd_expose_addr intrinsic unimplemented rm -r src/test/run-make/repr128-dwarf # debuginfo test -rm src/test/codegen-units/item-collection/asm-sym.rs # requires support for sym in asm!() # optimization tests # ================== @@ -104,9 +102,6 @@ rm src/test/ui/simd/simd-bitmask.rs # crash # ====================== rm src/test/ui/backtrace.rs # TODO warning rm src/test/ui/simple_global_asm.rs # TODO add needs-asm-support -rm src/test/ui/test-attrs/test-type.rs # TODO panic message on stderr. correct stdout -# not sure if this is actually a bug in the test suite, but the symbol list shows the function without leading _ for some reason -rm -r src/test/run-make/native-link-modifier-bundle rm src/test/ui/process/nofile-limit.rs # TODO some AArch64 linking issue rm src/test/ui/dyn-star/dispatch-on-pin-mut.rs # TODO failed assertion in vtable::get_ptr_and_method_ref From 4c97569a546c1f1d9bbfbb6f2e6a75466cd9faad Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 5 Jan 2023 17:26:54 +0000 Subject: [PATCH 029/101] Move patched sysroot from build_sysroot/ to download/ --- .gitignore | 5 +---- build_system/build_sysroot.rs | 9 +++++---- build_system/path.rs | 1 - build_system/prepare.rs | 24 +++++++++++++----------- build_system/tests.rs | 4 +++- clean_all.sh | 5 ++--- scripts/rustup.sh | 2 +- scripts/setup_rust_fork.sh | 2 +- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index b443fd58a1b98..8012e93f6a90e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -target +/target **/*.rs.bk *.rlib *.o @@ -11,9 +11,6 @@ perf.data.old /y.exe /y.pdb /build -/build_sysroot/sysroot_src -/build_sysroot/compiler-builtins -/build_sysroot/rustc_version /dist /rust /download diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index cbbf09b9b97b8..711d4ccc55bfb 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -146,10 +146,11 @@ pub(crate) fn build_sysroot( } } -// FIXME move to download/ or dist/ -pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version"); -pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src"); -static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot"); +pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); +pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot"); +pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version"); +pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src"); +static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); fn build_clif_sysroot_for_triple( dirs: &Dirs, diff --git a/build_system/path.rs b/build_system/path.rs index e93981f1d64d3..35ab6f111fef4 100644 --- a/build_system/path.rs +++ b/build_system/path.rs @@ -42,7 +42,6 @@ impl RelPath { pub(crate) const DIST: RelPath = RelPath::Base(PathBase::Dist); pub(crate) const SCRIPTS: RelPath = RelPath::SOURCE.join("scripts"); - pub(crate) const BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); pub(crate) const PATCHES: RelPath = RelPath::SOURCE.join("patches"); pub(crate) const fn join(&'static self, suffix: &'static str) -> RelPath { diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 8ac67e8f94228..f9ef29849eb7e 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -3,9 +3,11 @@ use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; -use super::build_sysroot::{SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; +use crate::build_system::rustc_info::get_default_sysroot; + +use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version}; +use super::rustc_info::{get_file_name, get_rustc_version}; use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler}; pub(crate) fn prepare(dirs: &Dirs) { @@ -49,27 +51,27 @@ pub(crate) fn prepare(dirs: &Dirs) { } fn prepare_sysroot(dirs: &Dirs) { - let rustc_path = get_rustc_path(); - let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust"); - let sysroot_src = SYSROOT_SRC; - + let sysroot_src_orig = get_default_sysroot().join("lib/rustlib/src/rust"); assert!(sysroot_src_orig.exists()); - sysroot_src.ensure_fresh(dirs); - fs::create_dir_all(sysroot_src.to_path(dirs).join("library")).unwrap(); eprintln!("[COPY] sysroot src"); + + BUILD_SYSROOT.ensure_fresh(dirs); + copy_dir_recursively(&ORIG_BUILD_SYSROOT.to_path(dirs), &BUILD_SYSROOT.to_path(dirs)); + + fs::create_dir_all(SYSROOT_SRC.to_path(dirs).join("library")).unwrap(); copy_dir_recursively( &sysroot_src_orig.join("library"), - &sysroot_src.to_path(dirs).join("library"), + &SYSROOT_SRC.to_path(dirs).join("library"), ); let rustc_version = get_rustc_version(); fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap(); eprintln!("[GIT] init"); - init_git_repo(&sysroot_src.to_path(dirs)); + init_git_repo(&SYSROOT_SRC.to_path(dirs)); - apply_patches(dirs, "sysroot", &sysroot_src.to_path(dirs)); + apply_patches(dirs, "sysroot", &SYSROOT_SRC.to_path(dirs)); } pub(crate) struct GitRepo { diff --git a/build_system/tests.rs b/build_system/tests.rs index 37c35106af6d3..bc112910ce6be 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -1,3 +1,5 @@ +use crate::build_system::build_sysroot::SYSROOT_SRC; + use super::build_sysroot; use super::config; use super::path::{Dirs, RelPath}; @@ -262,7 +264,7 @@ pub(crate) static SIMPLE_RAYTRACER: CargoProject = CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); static LIBCORE_TESTS: CargoProject = - CargoProject::new(&RelPath::BUILD_SYSROOT.join("sysroot_src/library/core/tests"), "core_tests"); + CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests"); const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ TestCase::new("test.rust-random/rand", &|runner| { diff --git a/clean_all.sh b/clean_all.sh index 1760e5836ecce..34cb081e37537 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -1,10 +1,9 @@ #!/usr/bin/env bash set -e -rm -rf build_sysroot/{sysroot_src/,target/,compiler-builtins/,rustc_version} -rm -rf target/ build/ dist/ perf.data{,.old} y.bin -rm -rf download/ +rm -rf target/ download/ build/ dist/ y.bin y.exe # Kept for now in case someone updates their checkout of cg_clif before running clean_all.sh # FIXME remove at some point in the future rm -rf rand/ regex/ simple-raytracer/ portable-simd/ abi-checker/ abi-cafe/ +rm -rf build_sysroot/{sysroot_src/,target/,compiler-builtins/,rustc_version} diff --git a/scripts/rustup.sh b/scripts/rustup.sh index bc4c06ed7d298..6111c20544463 100755 --- a/scripts/rustup.sh +++ b/scripts/rustup.sh @@ -17,10 +17,10 @@ case $1 in done ./clean_all.sh - ./y.rs prepare (cd build_sysroot && cargo update) + ./y.rs prepare ;; "commit") git add rust-toolchain build_sysroot/Cargo.lock diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index 6c64b7de7daa1..88bc64455030e 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -51,7 +51,7 @@ popd # FIXME remove once inline asm is fully supported export RUSTFLAGS="$RUSTFLAGS --cfg=rustix_use_libc" -export CFG_VIRTUAL_RUST_SOURCE_BASE_DIR="$(cd build_sysroot/sysroot_src; pwd)" +export CFG_VIRTUAL_RUST_SOURCE_BASE_DIR="$(cd download/sysroot/sysroot_src; pwd)" # Allow the testsuite to use llvm tools host_triple=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") From da37e16c2366a47b6599600d6b69d0f89480857d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 5 Jan 2023 17:28:23 +0000 Subject: [PATCH 030/101] Remove debuginfo files for compiled y.rs in clean_all.sh --- clean_all.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clean_all.sh b/clean_all.sh index 34cb081e37537..cdfc2e143e674 100755 --- a/clean_all.sh +++ b/clean_all.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash set -e -rm -rf target/ download/ build/ dist/ y.bin y.exe +rm -rf target/ download/ build/ dist/ y.bin y.bin.dSYM y.exe y.pdb # Kept for now in case someone updates their checkout of cg_clif before running clean_all.sh # FIXME remove at some point in the future From e14e5c2af1a028a2ad23508ee3d6d5ffda0ebdf0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 5 Jan 2023 17:57:35 +0000 Subject: [PATCH 031/101] Don't use hyperfine during testing A new command ./y.rs bench is introduced for benchmarking. This change allows skipping hyperfine installation in ./y.rs prepare and thus avoids writing to ~/.cargo/bin. --- .cirrus.yml | 2 - .github/workflows/main.yml | 6 -- .github/workflows/nightly-cranelift.yml | 6 -- .github/workflows/rustc.yml | 12 ---- .vscode/settings.json | 2 +- build_system/bench.rs | 79 +++++++++++++++++++++++ build_system/mod.rs | 17 ++++- build_system/prepare.rs | 6 +- build_system/tests.rs | 84 ++----------------------- config.txt | 2 +- 10 files changed, 106 insertions(+), 110 deletions(-) create mode 100644 build_system/bench.rs diff --git a/.cirrus.yml b/.cirrus.yml index 76b48d70ab7c7..7c966aa1ab9a9 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -6,8 +6,6 @@ task: - 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: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b79406879ff32..cc9ae19afded6 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,12 +62,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Cache cargo installed crates - uses: actions/cache@v3 - with: - path: ~/.cargo/bin - key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-installed-crates - - name: Cache cargo registry and index uses: actions/cache@v3 with: diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index 0565938ee3531..968cd43efd15d 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -13,12 +13,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Cache cargo installed crates - uses: actions/cache@v3 - with: - path: ~/.cargo/bin - key: ubuntu-latest-cargo-installed-crates - - name: Prepare dependencies run: | git config --global user.email "user@example.com" diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index bab81b9dc2a81..2c7de86f9b907 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -10,12 +10,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Cache cargo installed crates - uses: actions/cache@v3 - with: - path: ~/.cargo/bin - key: ${{ runner.os }}-cargo-installed-crates - - name: Cache cargo registry and index uses: actions/cache@v3 with: @@ -44,12 +38,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Cache cargo installed crates - uses: actions/cache@v3 - with: - path: ~/.cargo/bin - key: ${{ runner.os }}-cargo-installed-crates - - name: Cache cargo registry and index uses: actions/cache@v3 with: diff --git a/.vscode/settings.json b/.vscode/settings.json index bc914e37d2b51..d8650d1e387d2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -30,7 +30,7 @@ ] }, { - "sysroot_src": "./build_sysroot/sysroot_src/library", + "sysroot_src": "./download/sysroot/sysroot_src/library", "crates": [ { "root_module": "./example/std_example.rs", diff --git a/build_system/bench.rs b/build_system/bench.rs new file mode 100644 index 0000000000000..0ad8863223de6 --- /dev/null +++ b/build_system/bench.rs @@ -0,0 +1,79 @@ +use std::env; +use std::fs; +use std::path::Path; + +use super::path::{Dirs, RelPath}; +use super::prepare::GitRepo; +use super::rustc_info::{get_file_name, get_wrapper_file_name}; +use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject}; + +pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( + "ebobby", + "simple-raytracer", + "804a7a21b9e673a482797aa289a18ed480e4d813", + "", +); + +pub(crate) static SIMPLE_RAYTRACER: CargoProject = + CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); + +pub(crate) fn benchmark(dirs: &Dirs) { + benchmark_simple_raytracer(dirs); +} + +fn benchmark_simple_raytracer(dirs: &Dirs) { + if std::process::Command::new("hyperfine").output().is_err() { + eprintln!("Hyperfine not installed"); + eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine"); + std::process::exit(1); + } + + let run_runs = env::var("RUN_RUNS") + .unwrap_or(if is_ci() { "2" } else { "10" }.to_string()) + .parse() + .unwrap(); + + eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); + let cargo_clif = RelPath::DIST.to_path(dirs).join(get_wrapper_file_name("cargo-clif", "bin")); + let manifest_path = SIMPLE_RAYTRACER.manifest_path(dirs); + let target_dir = SIMPLE_RAYTRACER.target_dir(dirs); + + let clean_cmd = format!( + "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let llvm_build_cmd = format!( + "cargo build --manifest-path {manifest_path} --target-dir {target_dir}", + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + let clif_build_cmd = format!( + "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}", + cargo_clif = cargo_clif.display(), + manifest_path = manifest_path.display(), + target_dir = target_dir.display(), + ); + + let bench_compile = + hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); + + spawn_and_wait(bench_compile); + + eprintln!("[BENCH RUN] ebobby/simple-raytracer"); + fs::copy( + target_dir.join("debug").join(get_file_name("main", "bin")), + RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_clif", "bin")), + ) + .unwrap(); + + let mut bench_run = hyperfine_command( + 0, + run_runs, + None, + Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(), + Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(), + ); + bench_run.current_dir(RelPath::BUILD.to_path(dirs)); + spawn_and_wait(bench_run); +} diff --git a/build_system/mod.rs b/build_system/mod.rs index 2f311aed7c8db..76d1d013b0dbc 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -5,6 +5,7 @@ use std::process; use self::utils::is_ci; mod abi_cafe; +mod bench; mod build_backend; mod build_sysroot; mod config; @@ -20,6 +21,7 @@ USAGE: ./y.rs prepare [--out-dir DIR] ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] OPTIONS: --sysroot none|clif|llvm @@ -54,6 +56,7 @@ enum Command { Prepare, Build, Test, + Bench, } #[derive(Copy, Clone, Debug)] @@ -67,7 +70,7 @@ pub fn main() { if env::var("RUST_BACKTRACE").is_err() { env::set_var("RUST_BACKTRACE", "1"); } - env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); + env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); // FIXME disable this by default env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); if is_ci() { @@ -83,6 +86,7 @@ pub fn main() { Some("prepare") => Command::Prepare, Some("build") => Command::Build, Some("test") => Command::Test, + Some("bench") => Command::Bench, Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), Some(command) => arg_error!("Unknown command {}", command), None => { @@ -198,5 +202,16 @@ pub fn main() { &target_triple, ); } + Command::Bench => { + build_sysroot::build_sysroot( + &dirs, + channel, + sysroot_kind, + &cg_clif_dylib, + &host_triple, + &target_triple, + ); + bench::benchmark(&dirs); + } } } diff --git a/build_system/prepare.rs b/build_system/prepare.rs index f9ef29849eb7e..9ad4ddc92c5af 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -33,14 +33,14 @@ pub(crate) fn prepare(dirs: &Dirs) { super::tests::RAND_REPO.fetch(dirs); super::tests::REGEX_REPO.fetch(dirs); super::tests::PORTABLE_SIMD_REPO.fetch(dirs); - super::tests::SIMPLE_RAYTRACER_REPO.fetch(dirs); + super::bench::SIMPLE_RAYTRACER_REPO.fetch(dirs); eprintln!("[LLVM BUILD] simple-raytracer"); let host_compiler = Compiler::host(); - let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler, dirs); + let build_cmd = super::bench::SIMPLE_RAYTRACER.build(&host_compiler, dirs); spawn_and_wait(build_cmd); fs::copy( - super::tests::SIMPLE_RAYTRACER + super::bench::SIMPLE_RAYTRACER .target_dir(dirs) .join(&host_compiler.triple) .join("debug") diff --git a/build_system/tests.rs b/build_system/tests.rs index bc112910ce6be..5b8b6f2df1066 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -1,13 +1,9 @@ -use crate::build_system::build_sysroot::SYSROOT_SRC; - -use super::build_sysroot; +use super::bench::SIMPLE_RAYTRACER; +use super::build_sysroot::{self, SYSROOT_SRC}; use super::config; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; -use super::rustc_info::{get_file_name, get_wrapper_file_name}; -use super::utils::{ - hyperfine_command, is_ci, spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler, -}; +use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler}; use super::SysrootKind; use std::env; use std::ffi::OsStr; @@ -253,16 +249,6 @@ pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( static PORTABLE_SIMD: CargoProject = CargoProject::new(&PORTABLE_SIMD_REPO.source_dir(), "portable_simd"); -pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( - "ebobby", - "simple-raytracer", - "804a7a21b9e673a482797aa289a18ed480e4d813", - "", -); - -pub(crate) static SIMPLE_RAYTRACER: CargoProject = - CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); - static LIBCORE_TESTS: CargoProject = CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests"); @@ -282,67 +268,9 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ spawn_and_wait(build_cmd); } }), - TestCase::new("bench.simple-raytracer", &|runner| { - let run_runs = env::var("RUN_RUNS") - .unwrap_or(if is_ci() { "2" } else { "10" }.to_string()) - .parse() - .unwrap(); - - if runner.is_native { - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); - let cargo_clif = RelPath::DIST - .to_path(&runner.dirs) - .join(get_wrapper_file_name("cargo-clif", "bin")); - let manifest_path = SIMPLE_RAYTRACER.manifest_path(&runner.dirs); - let target_dir = SIMPLE_RAYTRACER.target_dir(&runner.dirs); - - let clean_cmd = format!( - "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}", - manifest_path = manifest_path.display(), - target_dir = target_dir.display(), - ); - let llvm_build_cmd = format!( - "cargo build --manifest-path {manifest_path} --target-dir {target_dir}", - manifest_path = manifest_path.display(), - target_dir = target_dir.display(), - ); - let clif_build_cmd = format!( - "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}", - cargo_clif = cargo_clif.display(), - manifest_path = manifest_path.display(), - target_dir = target_dir.display(), - ); - - let bench_compile = - hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd); - - spawn_and_wait(bench_compile); - - eprintln!("[BENCH RUN] ebobby/simple-raytracer"); - fs::copy( - target_dir.join("debug").join(get_file_name("main", "bin")), - RelPath::BUILD - .to_path(&runner.dirs) - .join(get_file_name("raytracer_cg_clif", "bin")), - ) - .unwrap(); - - let mut bench_run = hyperfine_command( - 0, - run_runs, - None, - Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(), - Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(), - ); - bench_run.current_dir(RelPath::BUILD.to_path(&runner.dirs)); - spawn_and_wait(bench_run); - } else { - spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.target_compiler.cargo, &runner.dirs)); - eprintln!("[BENCH COMPILE] ebobby/simple-raytracer (skipped)"); - eprintln!("[COMPILE] ebobby/simple-raytracer"); - spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); - eprintln!("[BENCH RUN] ebobby/simple-raytracer (skipped)"); - } + TestCase::new("test.simple-raytracer", &|runner| { + spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.host_compiler.cargo, &runner.dirs)); + spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); }), TestCase::new("test.libcore", &|runner| { spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs)); diff --git a/config.txt b/config.txt index 258b67e931476..d9912a8158f67 100644 --- a/config.txt +++ b/config.txt @@ -44,7 +44,7 @@ aot.issue-72793 testsuite.extended_sysroot test.rust-random/rand -bench.simple-raytracer +test.simple-raytracer test.libcore test.regex-shootout-regex-dna test.regex From 4e47bd0464ce119a903e95b2dc20260258776d8e Mon Sep 17 00:00:00 2001 From: Albert Larsan <74931857+albertlarsan68@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:45:44 +0100 Subject: [PATCH 032/101] Change `src/test` to `tests` in source files, fix tidy and tests --- scripts/test_rustc_tests.sh | 132 ++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 04ad77ec97eac..12ecb8cf4e17d 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -10,118 +10,118 @@ pushd rust command -v rg >/dev/null 2>&1 || cargo install ripgrep -rm -r src/test/ui/{extern/,unsized-locals/,lto/,linkage*} || true -for test in $(rg --files-with-matches "lto|// needs-asm-support|// needs-unwind" src/test/{ui,incremental}); do +rm -r tests/ui/{extern/,unsized-locals/,lto/,linkage*} || true +for test in $(rg --files-with-matches "lto|// needs-asm-support|// needs-unwind" tests/{ui,incremental}); do rm $test done -for test in $(rg -i --files-with-matches "//(\[\w+\])?~[^\|]*\s*ERR|// error-pattern:|// build-fail|// run-fail|-Cllvm-args" src/test/ui); do +for test in $(rg -i --files-with-matches "//(\[\w+\])?~[^\|]*\s*ERR|// error-pattern:|// build-fail|// run-fail|-Cllvm-args" tests/ui); do rm $test done -git checkout -- src/test/ui/issues/auxiliary/issue-3136-a.rs # contains //~ERROR, but shouldn't be removed -git checkout -- src/test/ui/proc-macro/pretty-print-hack/ +git checkout -- tests/ui/issues/auxiliary/issue-3136-a.rs # contains //~ERROR, but shouldn't be removed +git checkout -- tests/ui/proc-macro/pretty-print-hack/ # missing features # ================ # requires stack unwinding -rm src/test/incremental/change_crate_dep_kind.rs -rm src/test/incremental/issue-80691-bad-eval-cache.rs # -Cpanic=abort causes abort instead of exit(101) +rm tests/incremental/change_crate_dep_kind.rs +rm tests/incremental/issue-80691-bad-eval-cache.rs # -Cpanic=abort causes abort instead of exit(101) # requires compiling with -Cpanic=unwind -rm -r src/test/ui/macros/rfc-2011-nicer-assert-messages/ -rm -r src/test/run-make/test-benches +rm -r tests/ui/macros/rfc-2011-nicer-assert-messages/ +rm -r tests/run-make/test-benches # vendor intrinsics -rm src/test/ui/sse2.rs # cpuid not supported, so sse2 not detected -rm src/test/ui/intrinsics/const-eval-select-x86_64.rs # requires x86_64 vendor intrinsics -rm src/test/ui/simd/array-type.rs # "Index argument for `simd_insert` is not a constant" -rm src/test/ui/simd/intrinsic/generic-bitmask-pass.rs # simd_bitmask unimplemented -rm src/test/ui/simd/intrinsic/generic-as.rs # simd_as unimplemented -rm src/test/ui/simd/intrinsic/generic-arithmetic-saturating-pass.rs # simd_saturating_add unimplemented -rm src/test/ui/simd/intrinsic/float-math-pass.rs # simd_fcos unimplemented -rm src/test/ui/simd/intrinsic/generic-gather-pass.rs # simd_gather unimplemented -rm src/test/ui/simd/intrinsic/generic-select-pass.rs # simd_select_bitmask unimplemented -rm src/test/ui/simd/issue-85915-simd-ptrs.rs # simd_gather unimplemented -rm src/test/ui/simd/issue-89193.rs # simd_gather unimplemented -rm src/test/ui/simd/simd-bitmask.rs # simd_bitmask unimplemented +rm tests/ui/sse2.rs # cpuid not supported, so sse2 not detected +rm tests/ui/intrinsics/const-eval-select-x86_64.rs # requires x86_64 vendor intrinsics +rm tests/ui/simd/array-type.rs # "Index argument for `simd_insert` is not a constant" +rm tests/ui/simd/intrinsic/generic-bitmask-pass.rs # simd_bitmask unimplemented +rm tests/ui/simd/intrinsic/generic-as.rs # simd_as unimplemented +rm tests/ui/simd/intrinsic/generic-arithmetic-saturating-pass.rs # simd_saturating_add unimplemented +rm tests/ui/simd/intrinsic/float-math-pass.rs # simd_fcos unimplemented +rm tests/ui/simd/intrinsic/generic-gather-pass.rs # simd_gather unimplemented +rm tests/ui/simd/intrinsic/generic-select-pass.rs # simd_select_bitmask unimplemented +rm tests/ui/simd/issue-85915-simd-ptrs.rs # simd_gather unimplemented +rm tests/ui/simd/issue-89193.rs # simd_gather unimplemented +rm tests/ui/simd/simd-bitmask.rs # simd_bitmask unimplemented # exotic linkages -rm src/test/ui/issues/issue-33992.rs # unsupported linkages -rm src/test/incremental/hashes/function_interfaces.rs # same -rm src/test/incremental/hashes/statics.rs # same +rm tests/ui/issues/issue-33992.rs # unsupported linkages +rm tests/incremental/hashes/function_interfaces.rs # same +rm tests/incremental/hashes/statics.rs # same # variadic arguments -rm src/test/ui/abi/mir/mir_codegen_calls_variadic.rs # requires float varargs -rm src/test/ui/abi/variadic-ffi.rs # requires callee side vararg support +rm tests/ui/abi/mir/mir_codegen_calls_variadic.rs # requires float varargs +rm tests/ui/abi/variadic-ffi.rs # requires callee side vararg support # unsized locals -rm -r src/test/run-pass-valgrind/unsized-locals +rm -r tests/run-pass-valgrind/unsized-locals # misc unimplemented things -rm src/test/ui/intrinsics/intrinsic-nearby.rs # unimplemented nearbyintf32 and nearbyintf64 intrinsics -rm src/test/ui/target-feature/missing-plusminus.rs # error not implemented -rm src/test/ui/fn/dyn-fn-alignment.rs # wants a 256 byte alignment -rm -r src/test/run-make/emit-named-files # requires full --emit support -rm src/test/ui/abi/stack-probes.rs # stack probes not yet implemented -rm src/test/ui/simd/intrinsic/ptr-cast.rs # simd_expose_addr intrinsic unimplemented -rm -r src/test/run-make/repr128-dwarf # debuginfo test -rm src/test/codegen-units/item-collection/asm-sym.rs # requires support for sym in asm!() +rm tests/ui/intrinsics/intrinsic-nearby.rs # unimplemented nearbyintf32 and nearbyintf64 intrinsics +rm tests/ui/target-feature/missing-plusminus.rs # error not implemented +rm tests/ui/fn/dyn-fn-alignment.rs # wants a 256 byte alignment +rm -r tests/run-make/emit-named-files # requires full --emit support +rm tests/ui/abi/stack-probes.rs # stack probes not yet implemented +rm tests/ui/simd/intrinsic/ptr-cast.rs # simd_expose_addr intrinsic unimplemented +rm -r tests/run-make/repr128-dwarf # debuginfo test +rm tests/codegen-units/item-collection/asm-sym.rs # requires support for sym in asm!() # optimization tests # ================== -rm src/test/ui/codegen/issue-28950.rs # depends on stack size optimizations -rm src/test/ui/codegen/init-large-type.rs # same -rm src/test/ui/issues/issue-40883.rs # same -rm -r src/test/run-make/fmt-write-bloat/ # tests an optimization +rm tests/ui/codegen/issue-28950.rs # depends on stack size optimizations +rm tests/ui/codegen/init-large-type.rs # same +rm tests/ui/issues/issue-40883.rs # same +rm -r tests/run-make/fmt-write-bloat/ # tests an optimization # backend specific tests # ====================== -rm src/test/incremental/thinlto/cgu_invalidated_when_import_{added,removed}.rs # requires LLVM -rm src/test/ui/abi/stack-protector.rs # requires stack protector support +rm tests/incremental/thinlto/cgu_invalidated_when_import_{added,removed}.rs # requires LLVM +rm tests/ui/abi/stack-protector.rs # requires stack protector support # giving different but possibly correct results # ============================================= -rm src/test/ui/mir/mir_misc_casts.rs # depends on deduplication of constants -rm src/test/ui/mir/mir_raw_fat_ptr.rs # same -rm src/test/ui/consts/issue-33537.rs # same -rm src/test/ui/layout/valid_range_oob.rs # different ICE message +rm tests/ui/mir/mir_misc_casts.rs # depends on deduplication of constants +rm tests/ui/mir/mir_raw_fat_ptr.rs # same +rm tests/ui/consts/issue-33537.rs # same +rm tests/ui/layout/valid_range_oob.rs # different ICE message # doesn't work due to the way the rustc test suite is invoked. # should work when using ./x.py test the way it is intended # ============================================================ -rm -r src/test/run-make/emit-shared-files # requires the rustdoc executable in dist/bin/ -rm -r src/test/run-make/unstable-flag-required # same -rm -r src/test/run-make/rustdoc-* # same -rm -r src/test/run-make/issue-88756-default-output # same -rm -r src/test/run-make/remap-path-prefix-dwarf # requires llvm-dwarfdump -rm -r src/test/ui/consts/missing_span_in_backtrace.rs # expects sysroot source to be elsewhere +rm -r tests/run-make/emit-shared-files # requires the rustdoc executable in dist/bin/ +rm -r tests/run-make/unstable-flag-required # same +rm -r tests/run-make/rustdoc-* # same +rm -r tests/run-make/issue-88756-default-output # same +rm -r tests/run-make/remap-path-prefix-dwarf # requires llvm-dwarfdump +rm -r tests/ui/consts/missing_span_in_backtrace.rs # expects sysroot source to be elsewhere # genuine bugs # ============ -rm src/test/incremental/spike-neg1.rs # errors out for some reason -rm src/test/incremental/spike-neg2.rs # same -rm src/test/ui/issues/issue-74564-if-expr-stack-overflow.rs # gives a stackoverflow before the backend runs -rm src/test/ui/mir/ssa-analysis-regression-50041.rs # produces ICE -rm src/test/ui/type-alias-impl-trait/assoc-projection-ice.rs # produces ICE +rm tests/incremental/spike-neg1.rs # errors out for some reason +rm tests/incremental/spike-neg2.rs # same +rm tests/ui/issues/issue-74564-if-expr-stack-overflow.rs # gives a stackoverflow before the backend runs +rm tests/ui/mir/ssa-analysis-regression-50041.rs # produces ICE +rm tests/ui/type-alias-impl-trait/assoc-projection-ice.rs # produces ICE -rm src/test/ui/simd/intrinsic/generic-reduction-pass.rs # simd_reduce_add_unordered doesn't accept an accumulator for integer vectors +rm tests/ui/simd/intrinsic/generic-reduction-pass.rs # simd_reduce_add_unordered doesn't accept an accumulator for integer vectors -rm src/test/ui/runtime/out-of-stack.rs # SIGSEGV instead of SIGABRT for some reason (#1301) +rm tests/ui/runtime/out-of-stack.rs # SIGSEGV instead of SIGABRT for some reason (#1301) # bugs in the test suite # ====================== -rm src/test/ui/backtrace.rs # TODO warning -rm src/test/ui/simple_global_asm.rs # TODO add needs-asm-support -rm src/test/ui/test-attrs/test-type.rs # TODO panic message on stderr. correct stdout +rm tests/ui/backtrace.rs # TODO warning +rm tests/ui/simple_global_asm.rs # TODO add needs-asm-support +rm tests/ui/test-attrs/test-type.rs # TODO panic message on stderr. correct stdout # not sure if this is actually a bug in the test suite, but the symbol list shows the function without leading _ for some reason -rm -r src/test/run-make/native-link-modifier-bundle -rm src/test/ui/process/nofile-limit.rs # TODO some AArch64 linking issue -rm src/test/ui/dyn-star/dispatch-on-pin-mut.rs # TODO failed assertion in vtable::get_ptr_and_method_ref +rm -r tests/run-make/native-link-modifier-bundle +rm tests/ui/process/nofile-limit.rs # TODO some AArch64 linking issue +rm tests/ui/dyn-star/dispatch-on-pin-mut.rs # TODO failed assertion in vtable::get_ptr_and_method_ref -rm src/test/ui/stdio-is-blocking.rs # really slow with unoptimized libstd +rm tests/ui/stdio-is-blocking.rs # really slow with unoptimized libstd echo "[TEST] rustc test suite" -RUST_TEST_NOCAPTURE=1 COMPILETEST_FORCE_STAGE0=1 ./x.py test --stage 0 src/test/{codegen-units,run-make,run-pass-valgrind,ui,incremental} +RUST_TEST_NOCAPTURE=1 COMPILETEST_FORCE_STAGE0=1 ./x.py test --stage 0 tests/{codegen-units,run-make,run-pass-valgrind,ui,incremental} popd From fb788e375ad110fdcf6b0333745bf2cab29054ee Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 11 Jan 2023 13:59:14 +0000 Subject: [PATCH 033/101] Don't install hyperfine in y.rs prepare Missed in #1338 Part of #1290 --- build_system/prepare.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 9ad4ddc92c5af..7c10b9b0c0f7f 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -18,17 +18,6 @@ pub(crate) fn prepare(dirs: &Dirs) { prepare_sysroot(dirs); - // FIXME maybe install this only locally? - eprintln!("[INSTALL] hyperfine"); - Command::new("cargo") - .arg("install") - .arg("hyperfine") - .env_remove("CARGO_TARGET_DIR") - .spawn() - .unwrap() - .wait() - .unwrap(); - super::abi_cafe::ABI_CAFE_REPO.fetch(dirs); super::tests::RAND_REPO.fetch(dirs); super::tests::REGEX_REPO.fetch(dirs); From caacef29e6ad63d6b588715e809e78286d29db3f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 11 Jan 2023 14:14:14 +0000 Subject: [PATCH 034/101] Retry downloads on network failure Fixes #1280 --- build_system/prepare.rs | 20 +++++++++++++++++--- build_system/utils.rs | 16 ++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 7c10b9b0c0f7f..106b06296b4b9 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -8,7 +8,7 @@ use crate::build_system::rustc_info::get_default_sysroot; use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; use super::path::{Dirs, RelPath}; use super::rustc_info::{get_file_name, get_rustc_version}; -use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler}; +use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait, Compiler}; pub(crate) fn prepare(dirs: &Dirs) { if RelPath::DOWNLOAD.to_path(dirs).exists() { @@ -140,8 +140,22 @@ fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: // Download zip archive let mut download_cmd = Command::new("curl"); - download_cmd.arg("--location").arg("--output").arg(&archive_file).arg(archive_url); - spawn_and_wait(download_cmd); + download_cmd + .arg("--max-time") + .arg("600") + .arg("-y") + .arg("30") + .arg("-Y") + .arg("10") + .arg("--connect-timeout") + .arg("30") + .arg("--continue-at") + .arg("-") + .arg("--location") + .arg("--output") + .arg(&archive_file) + .arg(archive_url); + retry_spawn_and_wait(5, download_cmd); // Unpack tar archive let mut unpack_cmd = Command::new("tar"); diff --git a/build_system/utils.rs b/build_system/utils.rs index 995918ee1430b..3c27af0196e7f 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -188,6 +188,22 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) { } } +// Based on the retry function in rust's src/ci/shared.sh +#[track_caller] +pub(crate) fn retry_spawn_and_wait(tries: u64, mut cmd: Command) { + for i in 1..tries+1 { + if i != 1 { + println!("Command failed. Attempt {i}/{tries}:"); + } + if cmd.spawn().unwrap().wait().unwrap().success() { + return; + } + std::thread::sleep(std::time::Duration::from_secs(i * 5)); + } + println!("The command has failed after {tries} attempts."); + process::exit(1); +} + #[track_caller] pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> String { let mut child = cmd From 868638393eb79bf53ba00be6000930f9ecf8751e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 11 Jan 2023 15:23:15 +0000 Subject: [PATCH 035/101] Rustfmt --- build_system/utils.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build_system/utils.rs b/build_system/utils.rs index 3c27af0196e7f..3d6617d445843 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -191,7 +191,7 @@ pub(crate) fn spawn_and_wait(mut cmd: Command) { // Based on the retry function in rust's src/ci/shared.sh #[track_caller] pub(crate) fn retry_spawn_and_wait(tries: u64, mut cmd: Command) { - for i in 1..tries+1 { + for i in 1..tries + 1 { if i != 1 { println!("Command failed. Attempt {i}/{tries}:"); } From dbfbb29717dbaf94b3e83a30e4b9104241471dca Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 11:41:40 +0000 Subject: [PATCH 036/101] Move LLVM simple-raytracer build to ./y.rs bench cc #1290 --- build_system/bench.rs | 20 +++++++++++++++++++- build_system/prepare.rs | 18 ++---------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/build_system/bench.rs b/build_system/bench.rs index 0ad8863223de6..e0956cb44bad3 100644 --- a/build_system/bench.rs +++ b/build_system/bench.rs @@ -5,7 +5,7 @@ use std::path::Path; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; use super::rustc_info::{get_file_name, get_wrapper_file_name}; -use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject}; +use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject, Compiler}; pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( "ebobby", @@ -14,6 +14,10 @@ pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( "", ); +// Use a separate target dir for the initial LLVM build to reduce unnecessary recompiles +pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject = + CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer_llvm"); + pub(crate) static SIMPLE_RAYTRACER: CargoProject = CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); @@ -28,6 +32,20 @@ fn benchmark_simple_raytracer(dirs: &Dirs) { std::process::exit(1); } + eprintln!("[LLVM BUILD] simple-raytracer"); + let host_compiler = Compiler::host(); + let build_cmd = SIMPLE_RAYTRACER_LLVM.build(&host_compiler, dirs); + spawn_and_wait(build_cmd); + fs::copy( + SIMPLE_RAYTRACER_LLVM + .target_dir(dirs) + .join(&host_compiler.triple) + .join("debug") + .join(get_file_name("main", "bin")), + RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")), + ) + .unwrap(); + let run_runs = env::var("RUN_RUNS") .unwrap_or(if is_ci() { "2" } else { "10" }.to_string()) .parse() diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 106b06296b4b9..4c92987ba5bf1 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -7,8 +7,8 @@ use crate::build_system::rustc_info::get_default_sysroot; use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_file_name, get_rustc_version}; -use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait, Compiler}; +use super::rustc_info::get_rustc_version; +use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait}; pub(crate) fn prepare(dirs: &Dirs) { if RelPath::DOWNLOAD.to_path(dirs).exists() { @@ -23,20 +23,6 @@ pub(crate) fn prepare(dirs: &Dirs) { super::tests::REGEX_REPO.fetch(dirs); super::tests::PORTABLE_SIMD_REPO.fetch(dirs); super::bench::SIMPLE_RAYTRACER_REPO.fetch(dirs); - - eprintln!("[LLVM BUILD] simple-raytracer"); - let host_compiler = Compiler::host(); - let build_cmd = super::bench::SIMPLE_RAYTRACER.build(&host_compiler, dirs); - spawn_and_wait(build_cmd); - fs::copy( - super::bench::SIMPLE_RAYTRACER - .target_dir(dirs) - .join(&host_compiler.triple) - .join("debug") - .join(get_file_name("main", "bin")), - RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")), - ) - .unwrap(); } fn prepare_sysroot(dirs: &Dirs) { From cf22470de71180e54defc35e71edfd8558685fb4 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 11:42:02 +0000 Subject: [PATCH 037/101] Disable CG_CLIF_DISPLAY_CG_TIME by default It is a lot of noise without much benefit --- build_system/mod.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/build_system/mod.rs b/build_system/mod.rs index 76d1d013b0dbc..1c25c515e6b34 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -70,7 +70,6 @@ pub fn main() { if env::var("RUST_BACKTRACE").is_err() { env::set_var("RUST_BACKTRACE", "1"); } - env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1"); // FIXME disable this by default env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1"); if is_ci() { From 70a1cb9e62df17fbba0d30c884c7d98edf2ef780 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:16:11 +0000 Subject: [PATCH 038/101] Pass around Compiler instead of target triples --- build_system/abi_cafe.rs | 14 +++------ build_system/bench.rs | 9 +++--- build_system/build_backend.rs | 6 ++-- build_system/build_sysroot.rs | 35 ++++++++++++++--------- build_system/mod.rs | 54 ++++++++++++++--------------------- build_system/tests.rs | 11 +++---- build_system/utils.rs | 19 ++---------- 7 files changed, 64 insertions(+), 84 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index a081fdaa1c7e6..5f20a377329f5 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -17,34 +17,28 @@ pub(crate) fn run( sysroot_kind: SysrootKind, dirs: &Dirs, cg_clif_dylib: &Path, - host_triple: &str, - target_triple: &str, + host_compiler: &Compiler, ) { if !config::get_bool("testsuite.abi-cafe") { eprintln!("[SKIP] abi-cafe"); return; } - if host_triple != target_triple { - eprintln!("[SKIP] abi-cafe (cross-compilation not supported)"); - return; - } - eprintln!("Building sysroot for abi-cafe"); build_sysroot::build_sysroot( dirs, channel, sysroot_kind, cg_clif_dylib, - host_triple, - target_triple, + host_compiler, + &host_compiler.triple, ); eprintln!("Running abi-cafe"); let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"]; - let mut cmd = ABI_CAFE.run(&Compiler::host(), dirs); + let mut cmd = ABI_CAFE.run(host_compiler, dirs); cmd.arg("--"); cmd.arg("--pairs"); cmd.args(pairs); diff --git a/build_system/bench.rs b/build_system/bench.rs index e0956cb44bad3..f5c5d92cb3286 100644 --- a/build_system/bench.rs +++ b/build_system/bench.rs @@ -21,11 +21,11 @@ pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject = pub(crate) static SIMPLE_RAYTRACER: CargoProject = CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); -pub(crate) fn benchmark(dirs: &Dirs) { - benchmark_simple_raytracer(dirs); +pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) { + benchmark_simple_raytracer(dirs, host_compiler); } -fn benchmark_simple_raytracer(dirs: &Dirs) { +fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) { if std::process::Command::new("hyperfine").output().is_err() { eprintln!("Hyperfine not installed"); eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine"); @@ -33,8 +33,7 @@ fn benchmark_simple_raytracer(dirs: &Dirs) { } eprintln!("[LLVM BUILD] simple-raytracer"); - let host_compiler = Compiler::host(); - let build_cmd = SIMPLE_RAYTRACER_LLVM.build(&host_compiler, dirs); + let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs); spawn_and_wait(build_cmd); fs::copy( SIMPLE_RAYTRACER_LLVM diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index 16b43c5fc852f..00d9a6ddea8ab 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -10,10 +10,10 @@ static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif"); pub(crate) fn build_backend( dirs: &Dirs, channel: &str, - host_triple: &str, + host_compiler: &Compiler, use_unstable_features: bool, ) -> PathBuf { - let mut cmd = CG_CLIF.build(&Compiler::host(), dirs); + let mut cmd = CG_CLIF.build(&host_compiler, dirs); cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode @@ -48,7 +48,7 @@ pub(crate) fn build_backend( CG_CLIF .target_dir(dirs) - .join(host_triple) + .join(&host_compiler.triple) .join(channel) .join(get_file_name("rustc_codegen_cranelift", "dylib")) } diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 711d4ccc55bfb..f7bdf517976a3 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -17,7 +17,7 @@ pub(crate) fn build_sysroot( channel: &str, sysroot_kind: SysrootKind, cg_clif_dylib_src: &Path, - host_triple: &str, + host_compiler: &Compiler, target_triple: &str, ) { eprintln!("[BUILD] sysroot {:?}", sysroot_kind); @@ -53,7 +53,7 @@ pub(crate) fn build_sysroot( let default_sysroot = super::rustc_info::get_default_sysroot(); - let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib"); + let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&host_compiler.triple).join("lib"); let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); @@ -83,7 +83,7 @@ pub(crate) fn build_sysroot( SysrootKind::None => {} // Nothing to do SysrootKind::Llvm => { for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"), + default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"), ) .unwrap() { @@ -103,7 +103,7 @@ pub(crate) fn build_sysroot( try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap())); } - if target_triple != host_triple { + if target_triple != host_compiler.triple { for file in fs::read_dir( default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"), ) @@ -115,9 +115,15 @@ pub(crate) fn build_sysroot( } } SysrootKind::Clif => { - build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None); + build_clif_sysroot_for_triple( + dirs, + channel, + host_compiler.clone(), + &cg_clif_dylib_path, + None, + ); - if host_triple != target_triple { + if host_compiler.triple != target_triple { // When cross-compiling it is often necessary to manually pick the right linker let linker = match target_triple { "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), @@ -127,7 +133,11 @@ pub(crate) fn build_sysroot( build_clif_sysroot_for_triple( dirs, channel, - target_triple, + { + let mut target_compiler = host_compiler.clone(); + target_compiler.triple = target_triple.to_owned(); + target_compiler + }, &cg_clif_dylib_path, linker, ); @@ -155,7 +165,7 @@ static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build fn build_clif_sysroot_for_triple( dirs: &Dirs, channel: &str, - triple: &str, + mut compiler: Compiler, cg_clif_dylib_path: &Path, linker: Option<&str>, ) { @@ -177,7 +187,7 @@ fn build_clif_sysroot_for_triple( } } - let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel); + let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel); if !super::config::get_bool("keep_sysroot") { // Cleanup the deps dir, but keep build scripts and the incremental cache for faster @@ -188,7 +198,7 @@ fn build_clif_sysroot_for_triple( } // Build sysroot - let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); + let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap())); rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap())); if channel == "release" { @@ -198,8 +208,7 @@ fn build_clif_sysroot_for_triple( use std::fmt::Write; write!(rustflags, " -Clinker={}", linker).unwrap(); } - let mut compiler = Compiler::with_triple(triple.to_owned()); - compiler.rustflags = rustflags; + compiler.rustflags += &rustflags; let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); if channel == "release" { build_cmd.arg("--release"); @@ -219,7 +228,7 @@ fn build_clif_sysroot_for_triple( }; try_hard_link( entry.path(), - RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()), + RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()), ); } } diff --git a/build_system/mod.rs b/build_system/mod.rs index 1c25c515e6b34..f2de07cf5eb85 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -2,7 +2,7 @@ use std::env; use std::path::PathBuf; use std::process; -use self::utils::is_ci; +use self::utils::{is_ci, Compiler}; mod abi_cafe; mod bench; @@ -121,24 +121,16 @@ pub fn main() { } } - let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") { - host_triple - } else if let Some(host_triple) = config::get_value("host") { - host_triple - } else { - rustc_info::get_host_triple() - }; - let target_triple = if let Ok(target_triple) = std::env::var("TARGET_TRIPLE") { - if target_triple != "" { - target_triple - } else { - host_triple.clone() // Empty target triple can happen on GHA - } - } else if let Some(target_triple) = config::get_value("target") { - target_triple - } else { - host_triple.clone() - }; + let host_compiler = Compiler::llvm_with_triple( + std::env::var("HOST_TRIPLE") + .ok() + .or_else(|| config::get_value("host")) + .unwrap_or_else(|| rustc_info::get_host_triple()), + ); + let target_triple = std::env::var("TARGET_TRIPLE") + .ok() + .or_else(|| config::get_value("target")) + .unwrap_or_else(|| host_compiler.triple.clone()); // FIXME allow changing the location of these dirs using cli arguments let current_dir = std::env::current_dir().unwrap(); @@ -167,7 +159,7 @@ pub fn main() { } let cg_clif_dylib = - build_backend::build_backend(&dirs, channel, &host_triple, use_unstable_features); + build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features); match command { Command::Prepare => { // Handled above @@ -178,18 +170,16 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_triple, + &host_compiler, &target_triple, ); - abi_cafe::run( - channel, - sysroot_kind, - &dirs, - &cg_clif_dylib, - &host_triple, - &target_triple, - ); + if host_compiler.triple == target_triple { + abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler); + } else { + eprintln!("[SKIP] abi-cafe (cross-compilation not supported)"); + return; + } } Command::Build => { build_sysroot::build_sysroot( @@ -197,7 +187,7 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_triple, + &host_compiler, &target_triple, ); } @@ -207,10 +197,10 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_triple, + &host_compiler, &target_triple, ); - bench::benchmark(&dirs); + bench::benchmark(&dirs, &host_compiler); } } } diff --git a/build_system/tests.rs b/build_system/tests.rs index 5b8b6f2df1066..9139b3ccecc1c 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -385,10 +385,11 @@ pub(crate) fn run_tests( channel: &str, sysroot_kind: SysrootKind, cg_clif_dylib: &Path, - host_triple: &str, + host_compiler: &Compiler, target_triple: &str, ) { - let runner = TestRunner::new(dirs.clone(), host_triple.to_string(), target_triple.to_string()); + let runner = + TestRunner::new(dirs.clone(), host_compiler.triple.clone(), target_triple.to_string()); if config::get_bool("testsuite.no_sysroot") { build_sysroot::build_sysroot( @@ -396,7 +397,7 @@ pub(crate) fn run_tests( channel, SysrootKind::None, cg_clif_dylib, - &host_triple, + host_compiler, &target_triple, ); @@ -415,7 +416,7 @@ pub(crate) fn run_tests( channel, sysroot_kind, cg_clif_dylib, - &host_triple, + host_compiler, &target_triple, ); } @@ -445,7 +446,7 @@ impl TestRunner { pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self { let is_native = host_triple == target_triple; let jit_supported = - target_triple.contains("x86_64") && is_native && !host_triple.contains("windows"); + is_native && host_triple.contains("x86_64") && !host_triple.contains("windows"); let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); let mut runner = vec![]; diff --git a/build_system/utils.rs b/build_system/utils.rs index 3d6617d445843..d244da1b2e04e 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -5,10 +5,9 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{ - get_cargo_path, get_host_triple, get_rustc_path, get_rustdoc_path, get_wrapper_file_name, -}; +use super::rustc_info::{get_cargo_path, get_rustc_path, get_rustdoc_path, get_wrapper_file_name}; +#[derive(Clone, Debug)] pub(crate) struct Compiler { pub(crate) cargo: PathBuf, pub(crate) rustc: PathBuf, @@ -20,19 +19,7 @@ pub(crate) struct Compiler { } impl Compiler { - pub(crate) fn host() -> Compiler { - Compiler { - cargo: get_cargo_path(), - rustc: get_rustc_path(), - rustdoc: get_rustdoc_path(), - rustflags: String::new(), - rustdocflags: String::new(), - triple: get_host_triple(), - runner: vec![], - } - } - - pub(crate) fn with_triple(triple: String) -> Compiler { + pub(crate) fn llvm_with_triple(triple: String) -> Compiler { Compiler { cargo: get_cargo_path(), rustc: get_rustc_path(), From f311ef5a2ebbb520da3fb1333a1df70b31b9a66a Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:32:20 +0000 Subject: [PATCH 039/101] Share cross-compilation code between building and testing --- build_system/build_sysroot.rs | 14 +--------- build_system/tests.rs | 51 +++++++++-------------------------- build_system/utils.rs | 32 ++++++++++++++++++++++ 3 files changed, 46 insertions(+), 51 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index f7bdf517976a3..3ec00e08ef797 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -120,26 +120,19 @@ pub(crate) fn build_sysroot( channel, host_compiler.clone(), &cg_clif_dylib_path, - None, ); if host_compiler.triple != target_triple { - // When cross-compiling it is often necessary to manually pick the right linker - let linker = match target_triple { - "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"), - "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu-gcc"), - _ => None, - }; build_clif_sysroot_for_triple( dirs, channel, { let mut target_compiler = host_compiler.clone(); target_compiler.triple = target_triple.to_owned(); + target_compiler.set_cross_linker_and_runner(); target_compiler }, &cg_clif_dylib_path, - linker, ); } @@ -167,7 +160,6 @@ fn build_clif_sysroot_for_triple( channel: &str, mut compiler: Compiler, cg_clif_dylib_path: &Path, - linker: Option<&str>, ) { match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { @@ -204,10 +196,6 @@ fn build_clif_sysroot_for_triple( if channel == "release" { rustflags.push_str(" -Zmir-opt-level=3"); } - if let Some(linker) = linker { - use std::fmt::Write; - write!(rustflags, " -Clinker={}", linker).unwrap(); - } compiler.rustflags += &rustflags; let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs); if channel == "release" { diff --git a/build_system/tests.rs b/build_system/tests.rs index 9139b3ccecc1c..6993fdee40524 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -448,51 +448,26 @@ impl TestRunner { let jit_supported = is_native && host_triple.contains("x86_64") && !host_triple.contains("windows"); - let mut rustflags = env::var("RUSTFLAGS").ok().unwrap_or("".to_string()); - let mut runner = vec![]; + let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); + let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); if !is_native { - match target_triple.as_str() { - "aarch64-unknown-linux-gnu" => { - // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - rustflags = format!("-Clinker=aarch64-linux-gnu-gcc{}", rustflags); - runner = vec![ - "qemu-aarch64".to_owned(), - "-L".to_owned(), - "/usr/aarch64-linux-gnu".to_owned(), - ]; - } - "s390x-unknown-linux-gnu" => { - // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. - rustflags = format!("-Clinker=s390x-linux-gnu-gcc{}", rustflags); - runner = vec![ - "qemu-s390x".to_owned(), - "-L".to_owned(), - "/usr/s390x-linux-gnu".to_owned(), - ]; - } - "x86_64-pc-windows-gnu" => { - // We are cross-compiling for Windows. Run tests in wine. - runner = vec!["wine".to_owned()]; - } - _ => { - println!("Unknown non-native platform"); - } - } + target_compiler.set_cross_linker_and_runner(); + } + if let Ok(rustflags) = env::var("RUSTFLAGS") { + target_compiler.rustflags.push(' '); + target_compiler.rustflags.push_str(&rustflags); + } + if let Ok(rustdocflags) = env::var("RUSTDOCFLAGS") { + target_compiler.rustdocflags.push(' '); + target_compiler.rustdocflags.push_str(&rustdocflags); } // FIXME fix `#[linkage = "extern_weak"]` without this - if target_triple.contains("darwin") { - rustflags = format!("{} -Clink-arg=-undefined -Clink-arg=dynamic_lookup", rustflags); + if target_compiler.triple.contains("darwin") { + target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup"); } - let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); - - let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); - target_compiler.rustflags = rustflags.clone(); - target_compiler.rustdocflags = rustflags; - target_compiler.runner = runner; - Self { is_native, jit_supported, dirs, host_compiler, target_compiler } } diff --git a/build_system/utils.rs b/build_system/utils.rs index d244da1b2e04e..77cbf9b26fdc0 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -47,6 +47,38 @@ impl Compiler { runner: vec![], } } + + pub(crate) fn set_cross_linker_and_runner(&mut self) { + match self.triple.as_str() { + "aarch64-unknown-linux-gnu" => { + // We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. + self.rustflags += " -Clinker=aarch64-linux-gnu-gcc"; + self.rustdocflags += " -Clinker=aarch64-linux-gnu-gcc"; + self.runner = vec![ + "qemu-aarch64".to_owned(), + "-L".to_owned(), + "/usr/aarch64-linux-gnu".to_owned(), + ]; + } + "s390x-unknown-linux-gnu" => { + // We are cross-compiling for s390x. Use the correct linker and run tests in qemu. + self.rustflags += " -Clinker=s390x-linux-gnu-gcc"; + self.rustdocflags += " -Clinker=s390x-linux-gnu-gcc"; + self.runner = vec![ + "qemu-s390x".to_owned(), + "-L".to_owned(), + "/usr/s390x-linux-gnu".to_owned(), + ]; + } + "x86_64-pc-windows-gnu" => { + // We are cross-compiling for Windows. Run tests in wine. + self.runner = vec!["wine".to_owned()]; + } + _ => { + println!("Unknown non-native platform"); + } + } + } } pub(crate) struct CargoProject { From bb7ab8242a2b17d90ab7735efbbf2c5b905e196e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:52:29 +0000 Subject: [PATCH 040/101] Remove a lot of redundant rustc arguments for tests --- build_system/tests.rs | 141 ++++++------------------------------------ 1 file changed, 19 insertions(+), 122 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 6993fdee40524..364c4d93e4f3c 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -26,24 +26,10 @@ impl TestCase { const NO_SYSROOT_SUITE: &[TestCase] = &[ TestCase::new("build.mini_core", &|runner| { - runner.run_rustc([ - "example/mini_core.rs", - "--crate-name", - "mini_core", - "--crate-type", - "lib,dylib", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/mini_core.rs", "--crate-type", "lib,dylib"]); }), TestCase::new("build.example", &|runner| { - runner.run_rustc([ - "example/example.rs", - "--crate-type", - "lib", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/example.rs", "--crate-type", "lib"]); }), TestCase::new("jit.mini_core_hello_world", &|runner| { let mut jit_cmd = runner.rustc_command([ @@ -53,8 +39,6 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "example/mini_core_hello_world.rs", "--cfg", "jit", - "--target", - &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); @@ -67,69 +51,30 @@ const NO_SYSROOT_SUITE: &[TestCase] = &[ "example/mini_core_hello_world.rs", "--cfg", "jit", - "--target", - &runner.target_compiler.triple, ]); jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); spawn_and_wait(jit_cmd); }), TestCase::new("aot.mini_core_hello_world", &|runner| { - runner.run_rustc([ - "example/mini_core_hello_world.rs", - "--crate-name", - "mini_core_hello_world", - "--crate-type", - "bin", - "-g", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/mini_core_hello_world.rs"]); runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]); }), ]; const BASE_SYSROOT_SUITE: &[TestCase] = &[ TestCase::new("aot.arbitrary_self_types_pointers_and_wrappers", &|runner| { - runner.run_rustc([ - "example/arbitrary_self_types_pointers_and_wrappers.rs", - "--crate-name", - "arbitrary_self_types_pointers_and_wrappers", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/arbitrary_self_types_pointers_and_wrappers.rs"]); runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []); }), TestCase::new("aot.issue_91827_extern_types", &|runner| { - runner.run_rustc([ - "example/issue-91827-extern-types.rs", - "--crate-name", - "issue_91827_extern_types", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); - runner.run_out_command("issue_91827_extern_types", []); + runner.run_rustc(["example/issue-91827-extern-types.rs"]); + runner.run_out_command("issue-91827-extern-types", []); }), TestCase::new("build.alloc_system", &|runner| { - runner.run_rustc([ - "example/alloc_system.rs", - "--crate-type", - "lib", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/alloc_system.rs", "--crate-type", "lib"]); }), TestCase::new("aot.alloc_example", &|runner| { - runner.run_rustc([ - "example/alloc_example.rs", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/alloc_example.rs"]); runner.run_out_command("alloc_example", []); }), TestCase::new("jit.std_example", &|runner| { @@ -138,8 +83,6 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cllvm-args=mode=jit", "-Cprefer-dynamic", "example/std_example.rs", - "--target", - &runner.target_compiler.triple, ]); eprintln!("[JIT-lazy] std_example"); @@ -148,83 +91,34 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ "-Cllvm-args=mode=jit-lazy", "-Cprefer-dynamic", "example/std_example.rs", - "--target", - &runner.target_compiler.triple, ]); }), TestCase::new("aot.std_example", &|runner| { - runner.run_rustc([ - "example/std_example.rs", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/std_example.rs"]); runner.run_out_command("std_example", ["arg"]); }), TestCase::new("aot.dst_field_align", &|runner| { - runner.run_rustc([ - "example/dst-field-align.rs", - "--crate-name", - "dst_field_align", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); - runner.run_out_command("dst_field_align", []); + runner.run_rustc(["example/dst-field-align.rs"]); + runner.run_out_command("dst-field-align", []); }), TestCase::new("aot.subslice-patterns-const-eval", &|runner| { - runner.run_rustc([ - "example/subslice-patterns-const-eval.rs", - "--crate-type", - "bin", - "-Cpanic=abort", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/subslice-patterns-const-eval.rs"]); runner.run_out_command("subslice-patterns-const-eval", []); }), TestCase::new("aot.track-caller-attribute", &|runner| { - runner.run_rustc([ - "example/track-caller-attribute.rs", - "--crate-type", - "bin", - "-Cpanic=abort", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/track-caller-attribute.rs"]); runner.run_out_command("track-caller-attribute", []); }), TestCase::new("aot.float-minmax-pass", &|runner| { - runner.run_rustc([ - "example/float-minmax-pass.rs", - "--crate-type", - "bin", - "-Cpanic=abort", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/float-minmax-pass.rs"]); runner.run_out_command("float-minmax-pass", []); }), TestCase::new("aot.mod_bench", &|runner| { - runner.run_rustc([ - "example/mod_bench.rs", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/mod_bench.rs"]); runner.run_out_command("mod_bench", []); }), TestCase::new("aot.issue-72793", &|runner| { - runner.run_rustc([ - "example/issue-72793.rs", - "--crate-type", - "bin", - "--target", - &runner.target_compiler.triple, - ]); + runner.run_rustc(["example/issue-72793.rs"]); runner.run_out_command("issue-72793", []); }), ]; @@ -501,6 +395,9 @@ impl TestRunner { cmd.arg("--out-dir"); cmd.arg(format!("{}", BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).display())); cmd.arg("-Cdebuginfo=2"); + cmd.arg("--target"); + cmd.arg(&self.target_compiler.triple); + cmd.arg("-Cpanic=abort"); cmd.args(args); cmd } From af99a369c448c4b81c1a75437dc17779f6301020 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:44:40 +0000 Subject: [PATCH 041/101] Use sparse cargo registry --- .github/workflows/main.yml | 15 ++++++------- .github/workflows/nightly-cranelift.yml | 7 ++++++ .github/workflows/rustc.yml | 30 ++++++++++++------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cc9ae19afded6..7dbc8e76efdff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -62,14 +62,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Cache cargo registry and index - uses: actions/cache@v3 - with: - path: | - ~/.cargo/registry - ~/.cargo/git - key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-registry-and-index-${{ hashFiles('**/Cargo.lock') }} - - name: Cache cargo target dir uses: actions/cache@v3 with: @@ -103,6 +95,13 @@ jobs: if: matrix.os == 'windows-latest' run: git config --global core.autocrlf false + - name: Use sparse cargo registry + run: | + cat >> ~/.cargo/config.toml <> ~/.cargo/config.toml <> ~/.cargo/config.toml <> ~/.cargo/config.toml < Date: Fri, 13 Jan 2023 13:51:51 +0000 Subject: [PATCH 042/101] Don't require git user to be configured for testing rust --- .github/workflows/nightly-cranelift.yml | 5 +---- .github/workflows/rustc.yml | 10 ++-------- scripts/setup_rust_fork.sh | 2 +- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index 32a8ba9bcc992..8b3bfab7071ce 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -21,10 +21,7 @@ jobs: EOF - name: Prepare dependencies - run: | - git config --global user.email "user@example.com" - git config --global user.name "User" - ./y.rs prepare + run: ./y.rs prepare - name: Patch Cranelift run: | diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index 8844874ecb01f..5faa8f0540451 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -24,10 +24,7 @@ jobs: EOF - name: Prepare dependencies - run: | - git config --global user.email "user@example.com" - git config --global user.name "User" - ./y.rs prepare + run: ./y.rs prepare - name: Test run: ./scripts/test_bootstrap.sh @@ -51,10 +48,7 @@ jobs: EOF - name: Prepare dependencies - run: | - git config --global user.email "user@example.com" - git config --global user.name "User" - ./y.rs prepare + run: ./y.rs prepare - name: Test run: ./scripts/test_rustc_tests.sh diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index 88bc64455030e..ce0d7e9fe07da 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -10,7 +10,7 @@ git fetch git checkout -- . git checkout "$(rustc -V | cut -d' ' -f3 | tr -d '(')" -git am ../patches/*-sysroot-*.patch +git -c user.name=Dummy -c user.email=dummy@example.com am ../patches/*-sysroot-*.patch git apply - < Date: Fri, 13 Jan 2023 14:19:54 +0000 Subject: [PATCH 043/101] Introduce helpers for common test case commands --- build_system/tests.rs | 226 ++++++++++++++++++++++-------------------- 1 file changed, 117 insertions(+), 109 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 364c4d93e4f3c..8bef6f733800a 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -15,112 +15,83 @@ static BUILD_EXAMPLE_OUT_DIR: RelPath = RelPath::BUILD.join("example"); struct TestCase { config: &'static str, - func: &'static dyn Fn(&TestRunner), + cmd: TestCaseCmd, +} + +enum TestCaseCmd { + Custom { func: &'static dyn Fn(&TestRunner) }, + BuildLib { source: &'static str, crate_types: &'static str }, + BuildBinAndRun { source: &'static str, args: &'static [&'static str] }, + JitBin { source: &'static str, args: &'static str }, } impl TestCase { - const fn new(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self { - Self { config, func } + // FIXME reduce usage of custom test case commands + const fn custom(config: &'static str, func: &'static dyn Fn(&TestRunner)) -> Self { + Self { config, cmd: TestCaseCmd::Custom { func } } + } + + const fn build_lib( + config: &'static str, + source: &'static str, + crate_types: &'static str, + ) -> Self { + Self { config, cmd: TestCaseCmd::BuildLib { source, crate_types } } + } + + const fn build_bin_and_run( + config: &'static str, + source: &'static str, + args: &'static [&'static str], + ) -> Self { + Self { config, cmd: TestCaseCmd::BuildBinAndRun { source, args } } + } + + const fn jit_bin(config: &'static str, source: &'static str, args: &'static str) -> Self { + Self { config, cmd: TestCaseCmd::JitBin { source, args } } } } const NO_SYSROOT_SUITE: &[TestCase] = &[ - TestCase::new("build.mini_core", &|runner| { - runner.run_rustc(["example/mini_core.rs", "--crate-type", "lib,dylib"]); - }), - TestCase::new("build.example", &|runner| { - runner.run_rustc(["example/example.rs", "--crate-type", "lib"]); - }), - TestCase::new("jit.mini_core_hello_world", &|runner| { - let mut jit_cmd = runner.rustc_command([ - "-Zunstable-options", - "-Cllvm-args=mode=jit", - "-Cprefer-dynamic", - "example/mini_core_hello_world.rs", - "--cfg", - "jit", - ]); - jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); - spawn_and_wait(jit_cmd); - - eprintln!("[JIT-lazy] mini_core_hello_world"); - let mut jit_cmd = runner.rustc_command([ - "-Zunstable-options", - "-Cllvm-args=mode=jit-lazy", - "-Cprefer-dynamic", - "example/mini_core_hello_world.rs", - "--cfg", - "jit", - ]); - jit_cmd.env("CG_CLIF_JIT_ARGS", "abc bcd"); - spawn_and_wait(jit_cmd); - }), - TestCase::new("aot.mini_core_hello_world", &|runner| { - runner.run_rustc(["example/mini_core_hello_world.rs"]); - runner.run_out_command("mini_core_hello_world", ["abc", "bcd"]); - }), + TestCase::build_lib("build.mini_core", "example/mini_core.rs", "lib,dylib"), + TestCase::build_lib("build.example", "example/example.rs", "lib"), + TestCase::jit_bin("jit.mini_core_hello_world", "example/mini_core_hello_world.rs", "abc bcd"), + TestCase::build_bin_and_run( + "aot.mini_core_hello_world", + "example/mini_core_hello_world.rs", + &["abc", "bcd"], + ), ]; const BASE_SYSROOT_SUITE: &[TestCase] = &[ - TestCase::new("aot.arbitrary_self_types_pointers_and_wrappers", &|runner| { - runner.run_rustc(["example/arbitrary_self_types_pointers_and_wrappers.rs"]); - runner.run_out_command("arbitrary_self_types_pointers_and_wrappers", []); - }), - TestCase::new("aot.issue_91827_extern_types", &|runner| { - runner.run_rustc(["example/issue-91827-extern-types.rs"]); - runner.run_out_command("issue-91827-extern-types", []); - }), - TestCase::new("build.alloc_system", &|runner| { - runner.run_rustc(["example/alloc_system.rs", "--crate-type", "lib"]); - }), - TestCase::new("aot.alloc_example", &|runner| { - runner.run_rustc(["example/alloc_example.rs"]); - runner.run_out_command("alloc_example", []); - }), - TestCase::new("jit.std_example", &|runner| { - runner.run_rustc([ - "-Zunstable-options", - "-Cllvm-args=mode=jit", - "-Cprefer-dynamic", - "example/std_example.rs", - ]); - - eprintln!("[JIT-lazy] std_example"); - runner.run_rustc([ - "-Zunstable-options", - "-Cllvm-args=mode=jit-lazy", - "-Cprefer-dynamic", - "example/std_example.rs", - ]); - }), - TestCase::new("aot.std_example", &|runner| { - runner.run_rustc(["example/std_example.rs"]); - runner.run_out_command("std_example", ["arg"]); - }), - TestCase::new("aot.dst_field_align", &|runner| { - runner.run_rustc(["example/dst-field-align.rs"]); - runner.run_out_command("dst-field-align", []); - }), - TestCase::new("aot.subslice-patterns-const-eval", &|runner| { - runner.run_rustc(["example/subslice-patterns-const-eval.rs"]); - runner.run_out_command("subslice-patterns-const-eval", []); - }), - TestCase::new("aot.track-caller-attribute", &|runner| { - runner.run_rustc(["example/track-caller-attribute.rs"]); - runner.run_out_command("track-caller-attribute", []); - }), - TestCase::new("aot.float-minmax-pass", &|runner| { - runner.run_rustc(["example/float-minmax-pass.rs"]); - runner.run_out_command("float-minmax-pass", []); - }), - TestCase::new("aot.mod_bench", &|runner| { - runner.run_rustc(["example/mod_bench.rs"]); - runner.run_out_command("mod_bench", []); - }), - TestCase::new("aot.issue-72793", &|runner| { - runner.run_rustc(["example/issue-72793.rs"]); - runner.run_out_command("issue-72793", []); - }), + TestCase::build_bin_and_run( + "aot.arbitrary_self_types_pointers_and_wrappers", + "example/arbitrary_self_types_pointers_and_wrappers.rs", + &[], + ), + TestCase::build_bin_and_run( + "aot.issue_91827_extern_types", + "example/issue-91827-extern-types.rs", + &[], + ), + TestCase::build_lib("build.alloc_system", "example/alloc_system.rs", "lib"), + TestCase::build_bin_and_run("aot.alloc_example", "example/alloc_example.rs", &[]), + TestCase::jit_bin("jit.std_example", "example/std_example.rs", ""), + TestCase::build_bin_and_run("aot.std_example", "example/std_example.rs", &["arg"]), + TestCase::build_bin_and_run("aot.dst_field_align", "example/dst-field-align.rs", &[]), + TestCase::build_bin_and_run( + "aot.subslice-patterns-const-eval", + "example/subslice-patterns-const-eval.rs", + &[], + ), + TestCase::build_bin_and_run( + "aot.track-caller-attribute", + "example/track-caller-attribute.rs", + &[], + ), + TestCase::build_bin_and_run("aot.float-minmax-pass", "example/float-minmax-pass.rs", &[]), + TestCase::build_bin_and_run("aot.mod_bench", "example/mod_bench.rs", &[]), + TestCase::build_bin_and_run("aot.issue-72793", "example/issue-72793.rs", &[]), ]; pub(crate) static RAND_REPO: GitRepo = @@ -147,7 +118,7 @@ static LIBCORE_TESTS: CargoProject = CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests"); const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ - TestCase::new("test.rust-random/rand", &|runner| { + TestCase::custom("test.rust-random/rand", &|runner| { spawn_and_wait(RAND.clean(&runner.target_compiler.cargo, &runner.dirs)); if runner.is_native { @@ -162,11 +133,11 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ spawn_and_wait(build_cmd); } }), - TestCase::new("test.simple-raytracer", &|runner| { + TestCase::custom("test.simple-raytracer", &|runner| { spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.host_compiler.cargo, &runner.dirs)); spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); }), - TestCase::new("test.libcore", &|runner| { + TestCase::custom("test.libcore", &|runner| { spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs)); if runner.is_native { @@ -178,7 +149,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ spawn_and_wait(build_cmd); } }), - TestCase::new("test.regex-shootout-regex-dna", &|runner| { + TestCase::custom("test.regex-shootout-regex-dna", &|runner| { spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo, &runner.dirs)); // newer aho_corasick versions throw a deprecation warning @@ -232,7 +203,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } } }), - TestCase::new("test.regex", &|runner| { + TestCase::custom("test.regex", &|runner| { spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo, &runner.dirs)); // newer aho_corasick versions throw a deprecation warning @@ -259,7 +230,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ spawn_and_wait(build_cmd); } }), - TestCase::new("test.portable-simd", &|runner| { + TestCase::custom("test.portable-simd", &|runner| { spawn_and_wait(PORTABLE_SIMD.clean(&runner.host_compiler.cargo, &runner.dirs)); let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs); @@ -366,7 +337,7 @@ impl TestRunner { } pub fn run_testsuite(&self, tests: &[TestCase]) { - for &TestCase { config, func } in tests { + for TestCase { config, cmd } in tests { let (tag, testname) = config.split_once('.').unwrap(); let tag = tag.to_uppercase(); let is_jit_test = tag == "JIT"; @@ -378,7 +349,47 @@ impl TestRunner { eprintln!("[{tag}] {testname}"); } - func(self); + match *cmd { + TestCaseCmd::Custom { func } => func(self), + TestCaseCmd::BuildLib { source, crate_types } => { + self.run_rustc([source, "--crate-type", crate_types]); + } + TestCaseCmd::BuildBinAndRun { source, args } => { + self.run_rustc([source]); + self.run_out_command( + source.split('/').last().unwrap().split('.').next().unwrap(), + args, + ); + } + TestCaseCmd::JitBin { source, args } => { + let mut jit_cmd = self.rustc_command([ + "-Zunstable-options", + "-Cllvm-args=mode=jit", + "-Cprefer-dynamic", + source, + "--cfg", + "jit", + ]); + if !args.is_empty() { + jit_cmd.env("CG_CLIF_JIT_ARGS", args); + } + spawn_and_wait(jit_cmd); + + eprintln!("[JIT-lazy] {testname}"); + let mut jit_cmd = self.rustc_command([ + "-Zunstable-options", + "-Cllvm-args=mode=jit-lazy", + "-Cprefer-dynamic", + source, + "--cfg", + "jit", + ]); + if !args.is_empty() { + jit_cmd.env("CG_CLIF_JIT_ARGS", args); + } + spawn_and_wait(jit_cmd); + } + } } } @@ -410,10 +421,7 @@ impl TestRunner { spawn_and_wait(self.rustc_command(args)); } - fn run_out_command<'a, I>(&self, name: &str, args: I) - where - I: IntoIterator, - { + fn run_out_command<'a>(&self, name: &str, args: &[&str]) { let mut full_cmd = vec![]; // Prepend the RUN_WRAPPER's @@ -425,7 +433,7 @@ impl TestRunner { BUILD_EXAMPLE_OUT_DIR.to_path(&self.dirs).join(name).to_str().unwrap().to_string(), ); - for arg in args.into_iter() { + for arg in args { full_cmd.push(arg.to_string()); } From 40745694a014bf59fa90f92ba097015c550da38f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 14:33:14 +0000 Subject: [PATCH 044/101] Add fixme --- build_system/prepare.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 4c92987ba5bf1..21ef599cec9d2 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -31,6 +31,7 @@ fn prepare_sysroot(dirs: &Dirs) { eprintln!("[COPY] sysroot src"); + // FIXME ensure builds error out or update the copy if any of the files copied here change BUILD_SYSROOT.ensure_fresh(dirs); copy_dir_recursively(&ORIG_BUILD_SYSROOT.to_path(dirs), &BUILD_SYSROOT.to_path(dirs)); From 9a15db6dd0baba5423455012cf170d0f3ca89c26 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 14:39:12 +0000 Subject: [PATCH 045/101] Add git_command helper --- build_system/prepare.rs | 45 +++++++++++++---------------------------- build_system/utils.rs | 12 +++++++++++ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 21ef599cec9d2..c3faacd9244c8 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -8,7 +8,7 @@ use crate::build_system::rustc_info::get_default_sysroot; use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; use super::path::{Dirs, RelPath}; use super::rustc_info::get_rustc_version; -use super::utils::{copy_dir_recursively, retry_spawn_and_wait, spawn_and_wait}; +use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait}; pub(crate) fn prepare(dirs: &Dirs) { if RelPath::DOWNLOAD.to_path(dirs).exists() { @@ -96,14 +96,14 @@ impl GitRepo { fn clone_repo(download_dir: &Path, repo: &str, rev: &str) { eprintln!("[CLONE] {}", repo); // Ignore exit code as the repo may already have been checked out - Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap(); + git_command(None, "clone").arg(repo).arg(download_dir).spawn().unwrap().wait().unwrap(); - let mut clean_cmd = Command::new("git"); - clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir); + let mut clean_cmd = git_command(download_dir, "checkout"); + clean_cmd.arg("--").arg("."); spawn_and_wait(clean_cmd); - let mut checkout_cmd = Command::new("git"); - checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir); + let mut checkout_cmd = git_command(download_dir, "checkout"); + checkout_cmd.arg("-q").arg(rev); spawn_and_wait(checkout_cmd); } @@ -159,25 +159,16 @@ fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: } fn init_git_repo(repo_dir: &Path) { - let mut git_init_cmd = Command::new("git"); - git_init_cmd.arg("init").arg("-q").current_dir(repo_dir); + let mut git_init_cmd = git_command(repo_dir, "init"); + git_init_cmd.arg("-q"); spawn_and_wait(git_init_cmd); - let mut git_add_cmd = Command::new("git"); - git_add_cmd.arg("add").arg(".").current_dir(repo_dir); + let mut git_add_cmd = git_command(repo_dir, "add"); + git_add_cmd.arg("."); spawn_and_wait(git_add_cmd); - let mut git_commit_cmd = Command::new("git"); - git_commit_cmd - .arg("-c") - .arg("user.name=Dummy") - .arg("-c") - .arg("user.email=dummy@example.com") - .arg("commit") - .arg("-m") - .arg("Initial commit") - .arg("-q") - .current_dir(repo_dir); + let mut git_commit_cmd = git_command(repo_dir, "commit"); + git_commit_cmd.arg("-m").arg("Initial commit").arg("-q"); spawn_and_wait(git_commit_cmd); } @@ -212,16 +203,8 @@ fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) { target_dir.file_name().unwrap(), patch.file_name().unwrap() ); - let mut apply_patch_cmd = Command::new("git"); - apply_patch_cmd - .arg("-c") - .arg("user.name=Dummy") - .arg("-c") - .arg("user.email=dummy@example.com") - .arg("am") - .arg(patch) - .arg("-q") - .current_dir(target_dir); + let mut apply_patch_cmd = git_command(target_dir, "am"); + apply_patch_cmd.arg(patch).arg("-q"); spawn_and_wait(apply_patch_cmd); } } diff --git a/build_system/utils.rs b/build_system/utils.rs index 77cbf9b26fdc0..6d0d9b56514ae 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -103,6 +103,7 @@ impl CargoProject { RelPath::BUILD.join(self.target).to_path(dirs) } + #[must_use] fn base_cmd(&self, command: &str, cargo: &Path, dirs: &Dirs) -> Command { let mut cmd = Command::new(cargo); @@ -115,6 +116,7 @@ impl CargoProject { cmd } + #[must_use] fn build_cmd(&self, command: &str, compiler: &Compiler, dirs: &Dirs) -> Command { let mut cmd = self.base_cmd(command, &compiler.cargo, dirs); @@ -191,6 +193,16 @@ pub(crate) fn hyperfine_command( bench } +#[must_use] +pub(crate) fn git_command<'a>(repo_dir: impl Into>, cmd: &str) -> Command { + let mut git_cmd = Command::new("git"); + git_cmd.arg("-c").arg("user.name=Dummy").arg("-c").arg("user.email=dummy@example.com").arg(cmd); + if let Some(repo_dir) = repo_dir.into() { + git_cmd.current_dir(repo_dir); + } + git_cmd +} + #[track_caller] pub(crate) fn try_hard_link(src: impl AsRef, dst: impl AsRef) { let src = src.as_ref(); From 890c61216a726a6ecad41e1f8146c169aa91812d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 14:41:07 +0000 Subject: [PATCH 046/101] Set core.autocrlf=false in the build system instead of CI config --- .github/workflows/main.yml | 4 ---- build_system/utils.rs | 9 ++++++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7dbc8e76efdff..f1badb792cd13 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -91,10 +91,6 @@ jobs: sudo apt-get update sudo apt-get install -y gcc-s390x-linux-gnu qemu-user - - name: Windows setup - if: matrix.os == 'windows-latest' - run: git config --global core.autocrlf false - - name: Use sparse cargo registry run: | cat >> ~/.cargo/config.toml <(repo_dir: impl Into>, cmd: &str) -> Command { let mut git_cmd = Command::new("git"); - git_cmd.arg("-c").arg("user.name=Dummy").arg("-c").arg("user.email=dummy@example.com").arg(cmd); + git_cmd + .arg("-c") + .arg("user.name=Dummy") + .arg("-c") + .arg("user.email=dummy@example.com") + .arg("-c") + .arg("core.autocrlf=false") + .arg(cmd); if let Some(repo_dir) = repo_dir.into() { git_cmd.current_dir(repo_dir); } From 1a89507d7c4fa7d8cfa87872578cc833d52f88f8 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:14:04 +0000 Subject: [PATCH 047/101] Don't use the diff command in regex test The output is small enough that getting a pretty diff isn't important. In addition this reduces the amount of commands the build system depends on. --- build_system/tests.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 8bef6f733800a..25c2457785e30 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -169,9 +169,10 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-input.txt"), ) .unwrap(); - let expected_path = - REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-output.txt"); - let expected = fs::read_to_string(&expected_path).unwrap(); + let expected = fs::read_to_string( + REGEX.source_dir(&runner.dirs).join("examples").join("regexdna-output.txt"), + ) + .unwrap(); let output = spawn_and_wait_with_input(run_cmd, input); // Make sure `[codegen mono items] start` doesn't poison the diff @@ -184,20 +185,9 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ let output_matches = expected.lines().eq(output.lines()); if !output_matches { - let res_path = REGEX.source_dir(&runner.dirs).join("res.txt"); - fs::write(&res_path, &output).unwrap(); - - if cfg!(windows) { - println!("Output files don't match!"); - println!("Expected Output:\n{}", expected); - println!("Actual Output:\n{}", output); - } else { - let mut diff = Command::new("diff"); - diff.arg("-u"); - diff.arg(res_path); - diff.arg(expected_path); - spawn_and_wait(diff); - } + println!("Output files don't match!"); + println!("Expected Output:\n{}", expected); + println!("Actual Output:\n{}", output); std::process::exit(1); } From 957d78c47936a82164803470b97bf7a50efd9fc5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:40:22 +0000 Subject: [PATCH 048/101] Fetch all cargo dependencies in ./y.rs prepare --- build_system/abi_cafe.rs | 2 +- build_system/build_backend.rs | 2 +- build_system/build_sysroot.rs | 2 +- build_system/prepare.rs | 9 +++++++++ build_system/tests.rs | 8 ++++---- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 5f20a377329f5..3f93c14a0fb49 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -10,7 +10,7 @@ use super::SysrootKind; pub(crate) static ABI_CAFE_REPO: GitRepo = GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); -static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); +pub(crate) static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); pub(crate) fn run( channel: &str, diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index 00d9a6ddea8ab..6ab39e48f214f 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -5,7 +5,7 @@ use super::path::{Dirs, RelPath}; use super::rustc_info::get_file_name; use super::utils::{is_ci, CargoProject, Compiler}; -static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif"); +pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "cg_clif"); pub(crate) fn build_backend( dirs: &Dirs, diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 3ec00e08ef797..c7d80789f8a2e 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -153,7 +153,7 @@ pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysr pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot"); pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version"); pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src"); -static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); +pub(crate) static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); fn build_clif_sysroot_for_triple( dirs: &Dirs, diff --git a/build_system/prepare.rs b/build_system/prepare.rs index c3faacd9244c8..4e898b30b7cce 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -16,13 +16,22 @@ pub(crate) fn prepare(dirs: &Dirs) { } std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); + spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", dirs)); + prepare_sysroot(dirs); + spawn_and_wait(super::build_sysroot::STANDARD_LIBRARY.fetch("cargo", dirs)); + spawn_and_wait(super::tests::LIBCORE_TESTS.fetch("cargo", dirs)); super::abi_cafe::ABI_CAFE_REPO.fetch(dirs); + spawn_and_wait(super::abi_cafe::ABI_CAFE.fetch("cargo", dirs)); super::tests::RAND_REPO.fetch(dirs); + spawn_and_wait(super::tests::RAND.fetch("cargo", dirs)); super::tests::REGEX_REPO.fetch(dirs); + spawn_and_wait(super::tests::REGEX.fetch("cargo", dirs)); super::tests::PORTABLE_SIMD_REPO.fetch(dirs); + spawn_and_wait(super::tests::PORTABLE_SIMD.fetch("cargo", dirs)); super::bench::SIMPLE_RAYTRACER_REPO.fetch(dirs); + spawn_and_wait(super::bench::SIMPLE_RAYTRACER.fetch("cargo", dirs)); } fn prepare_sysroot(dirs: &Dirs) { diff --git a/build_system/tests.rs b/build_system/tests.rs index 25c2457785e30..4d638a4eced10 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -97,12 +97,12 @@ const BASE_SYSROOT_SUITE: &[TestCase] = &[ pub(crate) static RAND_REPO: GitRepo = GitRepo::github("rust-random", "rand", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", "rand"); -static RAND: CargoProject = CargoProject::new(&RAND_REPO.source_dir(), "rand"); +pub(crate) static RAND: CargoProject = CargoProject::new(&RAND_REPO.source_dir(), "rand"); pub(crate) static REGEX_REPO: GitRepo = GitRepo::github("rust-lang", "regex", "341f207c1071f7290e3f228c710817c280c8dca1", "regex"); -static REGEX: CargoProject = CargoProject::new(®EX_REPO.source_dir(), "regex"); +pub(crate) static REGEX: CargoProject = CargoProject::new(®EX_REPO.source_dir(), "regex"); pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( "rust-lang", @@ -111,10 +111,10 @@ pub(crate) static PORTABLE_SIMD_REPO: GitRepo = GitRepo::github( "portable-simd", ); -static PORTABLE_SIMD: CargoProject = +pub(crate) static PORTABLE_SIMD: CargoProject = CargoProject::new(&PORTABLE_SIMD_REPO.source_dir(), "portable_simd"); -static LIBCORE_TESTS: CargoProject = +pub(crate) static LIBCORE_TESTS: CargoProject = CargoProject::new(&SYSROOT_SRC.join("library/core/tests"), "core_tests"); const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ From be8f656fac6e2ec8325707b88952c913883b2867 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:44:33 +0000 Subject: [PATCH 049/101] Pass --frozen to cargo to ensure ./y.rs prepare fetches all deps --- .github/workflows/nightly-cranelift.yml | 2 ++ build_system/utils.rs | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index 8b3bfab7071ce..c3dd7445fd8b9 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -36,6 +36,8 @@ jobs: cat Cargo.toml + cargo fetch + - name: Build without unstable features # This is the config rust-lang/rust uses for builds run: ./y.rs build --no-unstable-features diff --git a/build_system/utils.rs b/build_system/utils.rs index afbb1b0e5be4d..f2b1fecedc16b 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -111,7 +111,8 @@ impl CargoProject { .arg("--manifest-path") .arg(self.manifest_path(dirs)) .arg("--target-dir") - .arg(self.target_dir(dirs)); + .arg(self.target_dir(dirs)) + .arg("--frozen"); cmd } From 288c0678632b3a36e595c1a21f546b2559e028f5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:55:17 +0000 Subject: [PATCH 050/101] Set panic=abort for the build system This saves about 60ms of build time --- y.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/y.rs b/y.rs index 02e1e21ade1de..fd825d02e355c 100755 --- a/y.rs +++ b/y.rs @@ -3,7 +3,7 @@ # This block is ignored by rustc set -e echo "[BUILD] y.rs" 1>&2 -rustc $0 -o ${0/.rs/.bin} -Cdebuginfo=1 --edition 2021 +rustc $0 -o ${0/.rs/.bin} -Cdebuginfo=1 --edition 2021 -Cpanic=abort exec ${0/.rs/.bin} $@ */ From a3468770e3ee0b357df1c0f5ec194b0fea425da1 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:58:39 +0000 Subject: [PATCH 051/101] Rustfmt --- build_system/abi_cafe.rs | 3 ++- build_system/build_sysroot.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 3f93c14a0fb49..63f2efd8e1ef7 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -10,7 +10,8 @@ use super::SysrootKind; pub(crate) static ABI_CAFE_REPO: GitRepo = GitRepo::github("Gankra", "abi-cafe", "4c6dc8c9c687e2b3a760ff2176ce236872b37212", "abi-cafe"); -pub(crate) static ABI_CAFE: CargoProject = CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); +pub(crate) static ABI_CAFE: CargoProject = + CargoProject::new(&ABI_CAFE_REPO.source_dir(), "abi_cafe"); pub(crate) fn run( channel: &str, diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index c7d80789f8a2e..b7228968f6313 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -153,7 +153,8 @@ pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysr pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot"); pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version"); pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src"); -pub(crate) static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); +pub(crate) static STANDARD_LIBRARY: CargoProject = + CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); fn build_clif_sysroot_for_triple( dirs: &Dirs, From 3a27253289b4340a464206ea52534ef07e017efe Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 15:59:43 +0000 Subject: [PATCH 052/101] Enable VSCode formatOnSave --- .vscode/settings.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index d8650d1e387d2..7c8703cba505c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,6 @@ { + "editor.formatOnSave": true, + // source for rustc_* is not included in the rust-src component; disable the errors about this "rust-analyzer.diagnostics.disabled": ["unresolved-extern-crate", "unresolved-macro-call"], "rust-analyzer.imports.granularity.enforce": true, From bdcbf47df3b2f4e7dc12930a861c689b75a66c3f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 16:34:43 +0000 Subject: [PATCH 053/101] Improve readme and build system help message --- Readme.md | 11 +++++------ build_system/mod.rs | 26 +------------------------- build_system/usage.txt | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 31 deletions(-) create mode 100644 build_system/usage.txt diff --git a/Readme.md b/Readme.md index 55b107e4efbca..b87a9dc51e8d0 100644 --- a/Readme.md +++ b/Readme.md @@ -8,9 +8,9 @@ If not please open an issue. ## Building and testing ```bash -$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git +$ git clone https://github.com/bjorn3/rustc_codegen_cranelift $ cd rustc_codegen_cranelift -$ ./y.rs prepare # download and patch sysroot src and install hyperfine for benchmarking +$ ./y.rs prepare $ ./y.rs build ``` @@ -20,13 +20,12 @@ To run the test suite replace the last command with: $ ./test.sh ``` -This will implicitly build cg_clif too. Both `y.rs build` and `test.sh` accept a `--debug` argument to -build in debug mode. +For more docs on how to build and test see [build_system/usage.txt](build_system/usage.txt) or the help message of `./y.rs`. -Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section +Alternatively you can download a pre built version from [Github Actions]. It is listed in the artifacts section of workflow runs. Unfortunately due to GHA restrictions you need to be logged in to access it. -[GHA]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess +[Github Actions]: https://github.com/bjorn3/rustc_codegen_cranelift/actions?query=branch%3Amaster+event%3Apush+is%3Asuccess ## Usage diff --git a/build_system/mod.rs b/build_system/mod.rs index f2de07cf5eb85..d1932549ee675 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -15,32 +15,8 @@ mod rustc_info; mod tests; mod utils; -const USAGE: &str = r#"The build system of cg_clif. - -USAGE: - ./y.rs prepare [--out-dir DIR] - ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] - ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] - ./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] - -OPTIONS: - --sysroot none|clif|llvm - Which sysroot libraries to use: - `none` will not include any standard library in the sysroot. - `clif` will build the standard library using Cranelift. - `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM. - - --out-dir DIR - Specify the directory in which the download, build and dist directories are stored. - By default this is the working directory. - - --no-unstable-features - fSome features are not yet ready for production usage. This option will disable these - features. This includes the JIT mode and inline assembly support. -"#; - fn usage() { - eprintln!("{USAGE}"); + eprintln!("{}", include_str!("usage.txt")); } macro_rules! arg_error { diff --git a/build_system/usage.txt b/build_system/usage.txt new file mode 100644 index 0000000000000..9185255155426 --- /dev/null +++ b/build_system/usage.txt @@ -0,0 +1,34 @@ +The build system of cg_clif. + +USAGE: + ./y.rs prepare [--out-dir DIR] + ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + +OPTIONS: + --debug + Build cg_clif and the standard library in debug mode rather than release mode. + Warning: An unoptimized cg_clif is very slow. + + --sysroot none|clif|llvm + Which sysroot libraries to use: + `none` will not include any standard library in the sysroot. + `clif` will build the standard library using Cranelift. + `llvm` will use the pre-compiled standard library of rustc which is compiled with LLVM. + + --out-dir DIR + Specify the directory in which the download, build and dist directories are stored. + By default this is the working directory. + + --no-unstable-features + Some features are not yet ready for production usage. This option will disable these + features. This includes the JIT mode and inline assembly support. + +REQUIREMENTS: + * Rustup: The build system has a hard coded dependency on rustup to install the right nightly + version and make sure it is used where necessary. + * Git: `./y.rs prepare` uses git for applying patches and on Windows for downloading test repos. + * Curl and tar (non-Windows only): Used by `./y.rs prepare` to download a single commit for + repos. Git will be used to clone the whole repo when using Windows. + * [Hyperfine](https://github.com/sharkdp/hyperfine/): Used for benchmarking with `./y.rs bench`. From 8af4eacc8c6e8daad185cffef90ecad6919e34c1 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:16:47 +0000 Subject: [PATCH 054/101] Update FreeBSD to 13.1 This fixes the certificate error --- .cirrus.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.cirrus.yml b/.cirrus.yml index 7c966aa1ab9a9..7886cae42a15a 100644 --- a/.cirrus.yml +++ b/.cirrus.yml @@ -1,7 +1,7 @@ task: name: freebsd freebsd_instance: - image: freebsd-12-1-release-amd64 + image: freebsd-13-1-release-amd64 setup_rust_script: - pkg install -y curl git bash - curl https://sh.rustup.rs -sSf --output rustup.sh From 395eaa1538a18aff0c433b683625b7decf3c9235 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 14:17:43 +0000 Subject: [PATCH 055/101] Remove Test nightly Cranelift workflow Cranelift makes api breaking changes often enough that this workflow fails half of the time. As such in practice I completely ignore it's result and push an update to a branch after Cranelift branches to test everything on CI. In this update I immediately fix the fallout of api breaking changes. --- .github/workflows/nightly-cranelift.yml | 48 ------------------------- 1 file changed, 48 deletions(-) delete mode 100644 .github/workflows/nightly-cranelift.yml diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml deleted file mode 100644 index c3dd7445fd8b9..0000000000000 --- a/.github/workflows/nightly-cranelift.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: Test nightly Cranelift - -on: - push: - schedule: - - cron: '17 1 * * *' # At 01:17 UTC every day. - -jobs: - build: - runs-on: ubuntu-latest - timeout-minutes: 60 - - steps: - - uses: actions/checkout@v3 - - - name: Use sparse cargo registry - run: | - cat >> ~/.cargo/config.toml < Date: Sat, 14 Jan 2023 12:14:19 +0000 Subject: [PATCH 056/101] Minor consistency improvement --- build_system/abi_cafe.rs | 2 +- build_system/mod.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 63f2efd8e1ef7..1b4c7c9da2140 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -21,7 +21,7 @@ pub(crate) fn run( host_compiler: &Compiler, ) { if !config::get_bool("testsuite.abi-cafe") { - eprintln!("[SKIP] abi-cafe"); + eprintln!("[RUN] abi-cafe (skipped)"); return; } diff --git a/build_system/mod.rs b/build_system/mod.rs index d1932549ee675..f0cde968ae7b8 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -153,7 +153,7 @@ pub fn main() { if host_compiler.triple == target_triple { abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler); } else { - eprintln!("[SKIP] abi-cafe (cross-compilation not supported)"); + eprintln!("[RUN] abi-cafe (skipped, cross-compilation not supported)"); return; } } From a09712e0d2e2a17c6d11377e9d14e080b75ac952 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 12:36:56 +0000 Subject: [PATCH 057/101] Use fs::remove_dir_all instead of cargo clean --- build_system/tests.rs | 17 +++++++---------- build_system/utils.rs | 5 ++--- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 4d638a4eced10..4203a3859e1a1 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -119,7 +119,7 @@ pub(crate) static LIBCORE_TESTS: CargoProject = const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ TestCase::custom("test.rust-random/rand", &|runner| { - spawn_and_wait(RAND.clean(&runner.target_compiler.cargo, &runner.dirs)); + RAND.clean(&runner.dirs); if runner.is_native { eprintln!("[TEST] rust-random/rand"); @@ -134,11 +134,11 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } }), TestCase::custom("test.simple-raytracer", &|runner| { - spawn_and_wait(SIMPLE_RAYTRACER.clean(&runner.host_compiler.cargo, &runner.dirs)); + SIMPLE_RAYTRACER.clean(&runner.dirs); spawn_and_wait(SIMPLE_RAYTRACER.build(&runner.target_compiler, &runner.dirs)); }), TestCase::custom("test.libcore", &|runner| { - spawn_and_wait(LIBCORE_TESTS.clean(&runner.host_compiler.cargo, &runner.dirs)); + LIBCORE_TESTS.clean(&runner.dirs); if runner.is_native { spawn_and_wait(LIBCORE_TESTS.test(&runner.target_compiler, &runner.dirs)); @@ -150,7 +150,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } }), TestCase::custom("test.regex-shootout-regex-dna", &|runner| { - spawn_and_wait(REGEX.clean(&runner.target_compiler.cargo, &runner.dirs)); + REGEX.clean(&runner.dirs); // newer aho_corasick versions throw a deprecation warning let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); @@ -194,7 +194,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } }), TestCase::custom("test.regex", &|runner| { - spawn_and_wait(REGEX.clean(&runner.host_compiler.cargo, &runner.dirs)); + REGEX.clean(&runner.dirs); // newer aho_corasick versions throw a deprecation warning let lint_rust_flags = format!("{} --cap-lints warn", runner.target_compiler.rustflags); @@ -221,7 +221,7 @@ const EXTENDED_SYSROOT_SUITE: &[TestCase] = &[ } }), TestCase::custom("test.portable-simd", &|runner| { - spawn_and_wait(PORTABLE_SIMD.clean(&runner.host_compiler.cargo, &runner.dirs)); + PORTABLE_SIMD.clean(&runner.dirs); let mut build_cmd = PORTABLE_SIMD.build(&runner.target_compiler, &runner.dirs); build_cmd.arg("--all-targets"); @@ -293,7 +293,6 @@ struct TestRunner { is_native: bool, jit_supported: bool, dirs: Dirs, - host_compiler: Compiler, target_compiler: Compiler, } @@ -303,8 +302,6 @@ impl TestRunner { let jit_supported = is_native && host_triple.contains("x86_64") && !host_triple.contains("windows"); - let host_compiler = Compiler::clif_with_triple(&dirs, host_triple); - let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); if !is_native { target_compiler.set_cross_linker_and_runner(); @@ -323,7 +320,7 @@ impl TestRunner { target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup"); } - Self { is_native, jit_supported, dirs, host_compiler, target_compiler } + Self { is_native, jit_supported, dirs, target_compiler } } pub fn run_testsuite(&self, tests: &[TestCase]) { diff --git a/build_system/utils.rs b/build_system/utils.rs index f2b1fecedc16b..935177a00ba87 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -146,9 +146,8 @@ impl CargoProject { cmd } - #[must_use] - pub(crate) fn clean(&self, cargo: &Path, dirs: &Dirs) -> Command { - self.base_cmd("clean", cargo, dirs) + pub(crate) fn clean(&self, dirs: &Dirs) { + let _ = fs::remove_dir_all(self.target_dir(dirs)); } #[must_use] From 5e452ba616ab416e1444882ffe52648ae1fbac09 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 12:41:07 +0000 Subject: [PATCH 058/101] Set RUSTC and RUSTDOC env vars to invalid values to catch forgetting to set them --- build_system/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build_system/mod.rs b/build_system/mod.rs index f0cde968ae7b8..265416c48fcca 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -134,6 +134,9 @@ pub fn main() { process::exit(0); } + env::set_var("RUSTC", "rustc_should_be_set_explicitly"); + env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly"); + let cg_clif_dylib = build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features); match command { From fd1e824d88e9927326e5a2b4242c53e83b264d07 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 12:44:39 +0000 Subject: [PATCH 059/101] Minor changes to the TestRunner::new signature --- build_system/tests.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/build_system/tests.rs b/build_system/tests.rs index 4203a3859e1a1..6c4d0e2e35e7d 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -243,8 +243,11 @@ pub(crate) fn run_tests( host_compiler: &Compiler, target_triple: &str, ) { - let runner = - TestRunner::new(dirs.clone(), host_compiler.triple.clone(), target_triple.to_string()); + let runner = TestRunner::new( + dirs.clone(), + target_triple.to_owned(), + host_compiler.triple == target_triple, + ); if config::get_bool("testsuite.no_sysroot") { build_sysroot::build_sysroot( @@ -297,11 +300,7 @@ struct TestRunner { } impl TestRunner { - pub fn new(dirs: Dirs, host_triple: String, target_triple: String) -> Self { - let is_native = host_triple == target_triple; - let jit_supported = - is_native && host_triple.contains("x86_64") && !host_triple.contains("windows"); - + pub fn new(dirs: Dirs, target_triple: String, is_native: bool) -> Self { let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); if !is_native { target_compiler.set_cross_linker_and_runner(); @@ -320,6 +319,10 @@ impl TestRunner { target_compiler.rustflags.push_str(" -Clink-arg=-undefined -Clink-arg=dynamic_lookup"); } + let jit_supported = is_native + && target_compiler.triple.contains("x86_64") + && !target_compiler.triple.contains("windows"); + Self { is_native, jit_supported, dirs, target_compiler } } From bbb7a3b9b9790d3abc62157f3f6daae9af94d841 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 12:53:33 +0000 Subject: [PATCH 060/101] Rename Compiler variables for clarity --- build_system/abi_cafe.rs | 8 ++++---- build_system/bench.rs | 10 +++++----- build_system/build_backend.rs | 6 +++--- build_system/build_sysroot.rs | 25 +++++++++++++++---------- build_system/mod.rs | 30 ++++++++++++++++++++---------- build_system/tests.rs | 14 ++++++-------- build_system/utils.rs | 2 +- 7 files changed, 54 insertions(+), 41 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 1b4c7c9da2140..8352c1a965a75 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -18,7 +18,7 @@ pub(crate) fn run( sysroot_kind: SysrootKind, dirs: &Dirs, cg_clif_dylib: &Path, - host_compiler: &Compiler, + bootstrap_host_compiler: &Compiler, ) { if !config::get_bool("testsuite.abi-cafe") { eprintln!("[RUN] abi-cafe (skipped)"); @@ -31,15 +31,15 @@ pub(crate) fn run( channel, sysroot_kind, cg_clif_dylib, - host_compiler, - &host_compiler.triple, + bootstrap_host_compiler, + &bootstrap_host_compiler.triple, ); eprintln!("Running abi-cafe"); let pairs = ["rustc_calls_cgclif", "cgclif_calls_rustc", "cgclif_calls_cc", "cc_calls_cgclif"]; - let mut cmd = ABI_CAFE.run(host_compiler, dirs); + let mut cmd = ABI_CAFE.run(bootstrap_host_compiler, dirs); cmd.arg("--"); cmd.arg("--pairs"); cmd.args(pairs); diff --git a/build_system/bench.rs b/build_system/bench.rs index f5c5d92cb3286..1e83f21ba577b 100644 --- a/build_system/bench.rs +++ b/build_system/bench.rs @@ -21,11 +21,11 @@ pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject = pub(crate) static SIMPLE_RAYTRACER: CargoProject = CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer"); -pub(crate) fn benchmark(dirs: &Dirs, host_compiler: &Compiler) { - benchmark_simple_raytracer(dirs, host_compiler); +pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) { + benchmark_simple_raytracer(dirs, bootstrap_host_compiler); } -fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) { +fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) { if std::process::Command::new("hyperfine").output().is_err() { eprintln!("Hyperfine not installed"); eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine"); @@ -33,12 +33,12 @@ fn benchmark_simple_raytracer(dirs: &Dirs, host_compiler: &Compiler) { } eprintln!("[LLVM BUILD] simple-raytracer"); - let build_cmd = SIMPLE_RAYTRACER_LLVM.build(host_compiler, dirs); + let build_cmd = SIMPLE_RAYTRACER_LLVM.build(bootstrap_host_compiler, dirs); spawn_and_wait(build_cmd); fs::copy( SIMPLE_RAYTRACER_LLVM .target_dir(dirs) - .join(&host_compiler.triple) + .join(&bootstrap_host_compiler.triple) .join("debug") .join(get_file_name("main", "bin")), RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")), diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index 6ab39e48f214f..514404305a3fa 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -10,10 +10,10 @@ pub(crate) static CG_CLIF: CargoProject = CargoProject::new(&RelPath::SOURCE, "c pub(crate) fn build_backend( dirs: &Dirs, channel: &str, - host_compiler: &Compiler, + bootstrap_host_compiler: &Compiler, use_unstable_features: bool, ) -> PathBuf { - let mut cmd = CG_CLIF.build(&host_compiler, dirs); + let mut cmd = CG_CLIF.build(&bootstrap_host_compiler, dirs); cmd.env("CARGO_BUILD_INCREMENTAL", "true"); // Force incr comp even in release mode @@ -48,7 +48,7 @@ pub(crate) fn build_backend( CG_CLIF .target_dir(dirs) - .join(&host_compiler.triple) + .join(&bootstrap_host_compiler.triple) .join(channel) .join(get_file_name("rustc_codegen_cranelift", "dylib")) } diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index b7228968f6313..92d01750fab00 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -17,7 +17,7 @@ pub(crate) fn build_sysroot( channel: &str, sysroot_kind: SysrootKind, cg_clif_dylib_src: &Path, - host_compiler: &Compiler, + bootstrap_host_compiler: &Compiler, target_triple: &str, ) { eprintln!("[BUILD] sysroot {:?}", sysroot_kind); @@ -53,7 +53,8 @@ pub(crate) fn build_sysroot( let default_sysroot = super::rustc_info::get_default_sysroot(); - let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&host_compiler.triple).join("lib"); + let host_rustlib_lib = + RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); @@ -83,7 +84,11 @@ pub(crate) fn build_sysroot( SysrootKind::None => {} // Nothing to do SysrootKind::Llvm => { for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(&host_compiler.triple).join("lib"), + default_sysroot + .join("lib") + .join("rustlib") + .join(&bootstrap_host_compiler.triple) + .join("lib"), ) .unwrap() { @@ -103,7 +108,7 @@ pub(crate) fn build_sysroot( try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap())); } - if target_triple != host_compiler.triple { + if target_triple != bootstrap_host_compiler.triple { for file in fs::read_dir( default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"), ) @@ -118,19 +123,19 @@ pub(crate) fn build_sysroot( build_clif_sysroot_for_triple( dirs, channel, - host_compiler.clone(), + bootstrap_host_compiler.clone(), &cg_clif_dylib_path, ); - if host_compiler.triple != target_triple { + if bootstrap_host_compiler.triple != target_triple { build_clif_sysroot_for_triple( dirs, channel, { - let mut target_compiler = host_compiler.clone(); - target_compiler.triple = target_triple.to_owned(); - target_compiler.set_cross_linker_and_runner(); - target_compiler + let mut bootstrap_target_compiler = bootstrap_host_compiler.clone(); + bootstrap_target_compiler.triple = target_triple.to_owned(); + bootstrap_target_compiler.set_cross_linker_and_runner(); + bootstrap_target_compiler }, &cg_clif_dylib_path, ); diff --git a/build_system/mod.rs b/build_system/mod.rs index 265416c48fcca..910213be85e2c 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -97,7 +97,7 @@ pub fn main() { } } - let host_compiler = Compiler::llvm_with_triple( + let bootstrap_host_compiler = Compiler::bootstrap_with_triple( std::env::var("HOST_TRIPLE") .ok() .or_else(|| config::get_value("host")) @@ -106,7 +106,7 @@ pub fn main() { let target_triple = std::env::var("TARGET_TRIPLE") .ok() .or_else(|| config::get_value("target")) - .unwrap_or_else(|| host_compiler.triple.clone()); + .unwrap_or_else(|| bootstrap_host_compiler.triple.clone()); // FIXME allow changing the location of these dirs using cli arguments let current_dir = std::env::current_dir().unwrap(); @@ -137,8 +137,12 @@ pub fn main() { env::set_var("RUSTC", "rustc_should_be_set_explicitly"); env::set_var("RUSTDOC", "rustdoc_should_be_set_explicitly"); - let cg_clif_dylib = - build_backend::build_backend(&dirs, channel, &host_compiler, use_unstable_features); + let cg_clif_dylib = build_backend::build_backend( + &dirs, + channel, + &bootstrap_host_compiler, + use_unstable_features, + ); match command { Command::Prepare => { // Handled above @@ -149,12 +153,18 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_compiler, + &bootstrap_host_compiler, &target_triple, ); - if host_compiler.triple == target_triple { - abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &host_compiler); + if bootstrap_host_compiler.triple == target_triple { + abi_cafe::run( + channel, + sysroot_kind, + &dirs, + &cg_clif_dylib, + &bootstrap_host_compiler, + ); } else { eprintln!("[RUN] abi-cafe (skipped, cross-compilation not supported)"); return; @@ -166,7 +176,7 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_compiler, + &bootstrap_host_compiler, &target_triple, ); } @@ -176,10 +186,10 @@ pub fn main() { channel, sysroot_kind, &cg_clif_dylib, - &host_compiler, + &bootstrap_host_compiler, &target_triple, ); - bench::benchmark(&dirs, &host_compiler); + bench::benchmark(&dirs, &bootstrap_host_compiler); } } } diff --git a/build_system/tests.rs b/build_system/tests.rs index 6c4d0e2e35e7d..e915ed2a381d5 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -3,6 +3,7 @@ use super::build_sysroot::{self, SYSROOT_SRC}; use super::config; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; +use super::rustc_info::get_host_triple; use super::utils::{spawn_and_wait, spawn_and_wait_with_input, CargoProject, Compiler}; use super::SysrootKind; use std::env; @@ -240,14 +241,11 @@ pub(crate) fn run_tests( channel: &str, sysroot_kind: SysrootKind, cg_clif_dylib: &Path, - host_compiler: &Compiler, + bootstrap_host_compiler: &Compiler, target_triple: &str, ) { - let runner = TestRunner::new( - dirs.clone(), - target_triple.to_owned(), - host_compiler.triple == target_triple, - ); + let runner = + TestRunner::new(dirs.clone(), target_triple.to_owned(), get_host_triple() == target_triple); if config::get_bool("testsuite.no_sysroot") { build_sysroot::build_sysroot( @@ -255,7 +253,7 @@ pub(crate) fn run_tests( channel, SysrootKind::None, cg_clif_dylib, - host_compiler, + bootstrap_host_compiler, &target_triple, ); @@ -274,7 +272,7 @@ pub(crate) fn run_tests( channel, sysroot_kind, cg_clif_dylib, - host_compiler, + bootstrap_host_compiler, &target_triple, ); } diff --git a/build_system/utils.rs b/build_system/utils.rs index 935177a00ba87..07ea482fbbeaa 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -19,7 +19,7 @@ pub(crate) struct Compiler { } impl Compiler { - pub(crate) fn llvm_with_triple(triple: String) -> Compiler { + pub(crate) fn bootstrap_with_triple(triple: String) -> Compiler { Compiler { cargo: get_cargo_path(), rustc: get_rustc_path(), From 4dbafefe74f32ca7f76009e6ce3136348a017b3a Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 13:04:40 +0000 Subject: [PATCH 061/101] Return Compiler from build_sysroot --- build_system/abi_cafe.rs | 2 +- build_system/build_sysroot.rs | 26 +++++++++++++------- build_system/mod.rs | 6 ++--- build_system/tests.rs | 45 +++++++++++++++++------------------ 4 files changed, 43 insertions(+), 36 deletions(-) diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 8352c1a965a75..8742389f33227 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -32,7 +32,7 @@ pub(crate) fn run( sysroot_kind, cg_clif_dylib, bootstrap_host_compiler, - &bootstrap_host_compiler.triple, + bootstrap_host_compiler.triple.clone(), ); eprintln!("Running abi-cafe"); diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 92d01750fab00..7902c7005e017 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -18,14 +18,16 @@ pub(crate) fn build_sysroot( sysroot_kind: SysrootKind, cg_clif_dylib_src: &Path, bootstrap_host_compiler: &Compiler, - target_triple: &str, -) { + target_triple: String, +) -> Compiler { eprintln!("[BUILD] sysroot {:?}", sysroot_kind); DIST_DIR.ensure_fresh(dirs); BIN_DIR.ensure_exists(dirs); LIB_DIR.ensure_exists(dirs); + let is_native = bootstrap_host_compiler.triple == target_triple; + // Copy the backend let cg_clif_dylib_path = if cfg!(windows) { // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the @@ -55,12 +57,12 @@ pub(crate) fn build_sysroot( let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); - let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib"); + let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib"); fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); if target_triple == "x86_64-pc-windows-gnu" { - if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() { + if !default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib").exists() { eprintln!( "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \ to compile a sysroot for it.", @@ -68,7 +70,7 @@ pub(crate) fn build_sysroot( process::exit(1); } for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"), + default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"), ) .unwrap() { @@ -108,9 +110,9 @@ pub(crate) fn build_sysroot( try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap())); } - if target_triple != bootstrap_host_compiler.triple { + if !is_native { for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"), + default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"), ) .unwrap() { @@ -127,13 +129,13 @@ pub(crate) fn build_sysroot( &cg_clif_dylib_path, ); - if bootstrap_host_compiler.triple != target_triple { + if !is_native { build_clif_sysroot_for_triple( dirs, channel, { let mut bootstrap_target_compiler = bootstrap_host_compiler.clone(); - bootstrap_target_compiler.triple = target_triple.to_owned(); + bootstrap_target_compiler.triple = target_triple.clone(); bootstrap_target_compiler.set_cross_linker_and_runner(); bootstrap_target_compiler }, @@ -152,6 +154,12 @@ pub(crate) fn build_sysroot( } } } + + let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); + if !is_native { + target_compiler.set_cross_linker_and_runner(); + } + target_compiler } pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); diff --git a/build_system/mod.rs b/build_system/mod.rs index 910213be85e2c..6f388cd605fce 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -154,7 +154,7 @@ pub fn main() { sysroot_kind, &cg_clif_dylib, &bootstrap_host_compiler, - &target_triple, + target_triple.clone(), ); if bootstrap_host_compiler.triple == target_triple { @@ -177,7 +177,7 @@ pub fn main() { sysroot_kind, &cg_clif_dylib, &bootstrap_host_compiler, - &target_triple, + target_triple, ); } Command::Bench => { @@ -187,7 +187,7 @@ pub fn main() { sysroot_kind, &cg_clif_dylib, &bootstrap_host_compiler, - &target_triple, + target_triple, ); bench::benchmark(&dirs, &bootstrap_host_compiler); } diff --git a/build_system/tests.rs b/build_system/tests.rs index e915ed2a381d5..dcfadd737566e 100644 --- a/build_system/tests.rs +++ b/build_system/tests.rs @@ -242,21 +242,21 @@ pub(crate) fn run_tests( sysroot_kind: SysrootKind, cg_clif_dylib: &Path, bootstrap_host_compiler: &Compiler, - target_triple: &str, + target_triple: String, ) { - let runner = - TestRunner::new(dirs.clone(), target_triple.to_owned(), get_host_triple() == target_triple); - if config::get_bool("testsuite.no_sysroot") { - build_sysroot::build_sysroot( + let target_compiler = build_sysroot::build_sysroot( dirs, channel, SysrootKind::None, cg_clif_dylib, bootstrap_host_compiler, - &target_triple, + target_triple.clone(), ); + let runner = + TestRunner::new(dirs.clone(), target_compiler, get_host_triple() == target_triple); + BUILD_EXAMPLE_OUT_DIR.ensure_fresh(dirs); runner.run_testsuite(NO_SYSROOT_SUITE); } else { @@ -267,26 +267,29 @@ pub(crate) fn run_tests( let run_extended_sysroot = config::get_bool("testsuite.extended_sysroot"); if run_base_sysroot || run_extended_sysroot { - build_sysroot::build_sysroot( + let target_compiler = build_sysroot::build_sysroot( dirs, channel, sysroot_kind, cg_clif_dylib, bootstrap_host_compiler, - &target_triple, + target_triple.clone(), ); - } - if run_base_sysroot { - runner.run_testsuite(BASE_SYSROOT_SUITE); - } else { - eprintln!("[SKIP] base_sysroot tests"); - } + let runner = + TestRunner::new(dirs.clone(), target_compiler, get_host_triple() == target_triple); - if run_extended_sysroot { - runner.run_testsuite(EXTENDED_SYSROOT_SUITE); - } else { - eprintln!("[SKIP] extended_sysroot tests"); + if run_base_sysroot { + runner.run_testsuite(BASE_SYSROOT_SUITE); + } else { + eprintln!("[SKIP] base_sysroot tests"); + } + + if run_extended_sysroot { + runner.run_testsuite(EXTENDED_SYSROOT_SUITE); + } else { + eprintln!("[SKIP] extended_sysroot tests"); + } } } @@ -298,11 +301,7 @@ struct TestRunner { } impl TestRunner { - pub fn new(dirs: Dirs, target_triple: String, is_native: bool) -> Self { - let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); - if !is_native { - target_compiler.set_cross_linker_and_runner(); - } + pub fn new(dirs: Dirs, mut target_compiler: Compiler, is_native: bool) -> Self { if let Ok(rustflags) = env::var("RUSTFLAGS") { target_compiler.rustflags.push(' '); target_compiler.rustflags.push_str(&rustflags); From 22c5249f68676f7ba8852fd59cd222c49a1523a2 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 13:08:55 +0000 Subject: [PATCH 062/101] Don't hard-code rustc path in get_rustc_version and get_default_sysroot --- build_system/build_sysroot.rs | 4 ++-- build_system/prepare.rs | 4 ++-- build_system/rustc_info.rs | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 7902c7005e017..15a5e030193db 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -53,7 +53,7 @@ pub(crate) fn build_sysroot( spawn_and_wait(build_cargo_wrapper_cmd); } - let default_sysroot = super::rustc_info::get_default_sysroot(); + let default_sysroot = super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc); let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); @@ -182,7 +182,7 @@ fn build_clif_sysroot_for_triple( process::exit(1); } Ok(source_version) => { - let rustc_version = get_rustc_version(); + let rustc_version = get_rustc_version(&compiler.rustc); if source_version != rustc_version { eprintln!("The patched sysroot source is outdated"); eprintln!("Source version: {}", source_version.trim()); diff --git a/build_system/prepare.rs b/build_system/prepare.rs index 4e898b30b7cce..bc6c3223dc234 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -35,7 +35,7 @@ pub(crate) fn prepare(dirs: &Dirs) { } fn prepare_sysroot(dirs: &Dirs) { - let sysroot_src_orig = get_default_sysroot().join("lib/rustlib/src/rust"); + let sysroot_src_orig = get_default_sysroot(Path::new("rustc")).join("lib/rustlib/src/rust"); assert!(sysroot_src_orig.exists()); eprintln!("[COPY] sysroot src"); @@ -50,7 +50,7 @@ fn prepare_sysroot(dirs: &Dirs) { &SYSROOT_SRC.to_path(dirs).join("library"), ); - let rustc_version = get_rustc_version(); + let rustc_version = get_rustc_version(Path::new("rustc")); fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap(); eprintln!("[GIT] init"); diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs index 8e5ab688e131b..6959d1e323cd6 100644 --- a/build_system/rustc_info.rs +++ b/build_system/rustc_info.rs @@ -1,9 +1,9 @@ use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; -pub(crate) fn get_rustc_version() -> String { +pub(crate) fn get_rustc_version(rustc: &Path) -> String { let version_info = - Command::new("rustc").stderr(Stdio::inherit()).args(&["-V"]).output().unwrap().stdout; + Command::new(rustc).stderr(Stdio::inherit()).args(&["-V"]).output().unwrap().stdout; String::from_utf8(version_info).unwrap() } @@ -53,8 +53,8 @@ pub(crate) fn get_rustdoc_path() -> PathBuf { Path::new(String::from_utf8(rustc_path).unwrap().trim()).to_owned() } -pub(crate) fn get_default_sysroot() -> PathBuf { - let default_sysroot = Command::new("rustc") +pub(crate) fn get_default_sysroot(rustc: &Path) -> PathBuf { + let default_sysroot = Command::new(rustc) .stderr(Stdio::inherit()) .args(&["--print", "sysroot"]) .output() From 629eab79c16a6659a241c0b50ea986780934a65d Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 14:08:23 +0000 Subject: [PATCH 063/101] Avoid hard-coded rustc when building wrappers --- build_system/build_sysroot.rs | 7 +++++-- build_system/rustc_info.rs | 10 ++++++++++ scripts/cargo-clif.rs | 2 +- scripts/rustc-clif.rs | 2 +- scripts/rustdoc-clif.rs | 2 +- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 15a5e030193db..9eebcf95505d9 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -3,7 +3,9 @@ use std::path::Path; use std::process::{self, Command}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name}; +use super::rustc_info::{ + get_file_name, get_rustc_version, get_toolchain_name, get_wrapper_file_name, +}; use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; @@ -44,8 +46,9 @@ pub(crate) fn build_sysroot( for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] { let wrapper_name = get_wrapper_file_name(wrapper, "bin"); - let mut build_cargo_wrapper_cmd = Command::new("rustc"); + let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc); build_cargo_wrapper_cmd + .env("TOOLCHAIN_NAME", get_toolchain_name()) .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs"))) .arg("-o") .arg(DIST_DIR.to_path(dirs).join(wrapper_name)) diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs index 6959d1e323cd6..8a7e1c472dd10 100644 --- a/build_system/rustc_info.rs +++ b/build_system/rustc_info.rs @@ -23,6 +23,16 @@ pub(crate) fn get_host_triple() -> String { .to_owned() } +pub(crate) fn get_toolchain_name() -> String { + let active_toolchain = Command::new("rustup") + .stderr(Stdio::inherit()) + .args(&["show", "active-toolchain"]) + .output() + .unwrap() + .stdout; + String::from_utf8(active_toolchain).unwrap().trim().split_once(' ').unwrap().0.to_owned() +} + pub(crate) fn get_cargo_path() -> PathBuf { let cargo_path = Command::new("rustup") .stderr(Stdio::inherit()) diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs index 9362b47fa6d83..c993430b830b6 100644 --- a/scripts/cargo-clif.rs +++ b/scripts/cargo-clif.rs @@ -26,7 +26,7 @@ fn main() { env::set_var("RUSTDOCFLAGS", env::var("RUSTDOCFLAGS").unwrap_or(String::new()) + &rustflags); // Ensure that the right toolchain is used - env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN")); + env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME")); let args: Vec<_> = match env::args().nth(1).as_deref() { Some("jit") => { diff --git a/scripts/rustc-clif.rs b/scripts/rustc-clif.rs index 3abfcd8ddc824..c187f54a60e77 100644 --- a/scripts/rustc-clif.rs +++ b/scripts/rustc-clif.rs @@ -24,7 +24,7 @@ fn main() { } // Ensure that the right toolchain is used - env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN")); + env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME")); #[cfg(unix)] Command::new("rustc").args(args).exec(); diff --git a/scripts/rustdoc-clif.rs b/scripts/rustdoc-clif.rs index a19d72acfa83e..a6528ac41aee0 100644 --- a/scripts/rustdoc-clif.rs +++ b/scripts/rustdoc-clif.rs @@ -24,7 +24,7 @@ fn main() { } // Ensure that the right toolchain is used - env::set_var("RUSTUP_TOOLCHAIN", env!("RUSTUP_TOOLCHAIN")); + env::set_var("RUSTUP_TOOLCHAIN", env!("TOOLCHAIN_NAME")); #[cfg(unix)] Command::new("rustdoc").args(args).exec(); From c950f2265ef898ff760219d16298bd96fecb7155 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 17:02:21 +0000 Subject: [PATCH 064/101] Build rtstartup for MinGW from scratch Rather than copying it from an existing sysroot --- .github/workflows/main.yml | 1 - build_system/build_sysroot.rs | 34 ++++++++++++++++------------------ 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f1badb792cd13..1181c935b8389 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -77,7 +77,6 @@ jobs: run: | sudo apt-get update sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable - rustup target add x86_64-pc-windows-gnu - name: Install AArch64 toolchain and qemu if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'aarch64-unknown-linux-gnu' diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 9eebcf95505d9..413aa3444a54b 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -64,24 +64,21 @@ pub(crate) fn build_sysroot( fs::create_dir_all(&host_rustlib_lib).unwrap(); fs::create_dir_all(&target_rustlib_lib).unwrap(); - if target_triple == "x86_64-pc-windows-gnu" { - if !default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib").exists() { - eprintln!( - "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \ - to compile a sysroot for it.", - ); - process::exit(1); - } - for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"), - ) - .unwrap() - { - let file = file.unwrap().path(); - if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") { - continue; // only copy object files - } - try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap())); + if target_triple.ends_with("windows-gnu") { + eprintln!("[BUILD] rtstartup for {target_triple}"); + + let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); + + for file in ["rsbegin", "rsend"] { + let mut build_rtstartup_cmd = Command::new(&bootstrap_host_compiler.rustc); + build_rtstartup_cmd + .arg("--target") + .arg(&target_triple) + .arg("--emit=obj") + .arg("-o") + .arg(target_rustlib_lib.join(format!("{file}.o"))) + .arg(rtstartup_src.join(format!("{file}.rs"))); + spawn_and_wait(build_rtstartup_cmd); } } @@ -209,6 +206,7 @@ fn build_clif_sysroot_for_triple( // Build sysroot let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap())); + // Necessary for MinGW to find rsbegin.o and rsend.o rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap())); if channel == "release" { rustflags.push_str(" -Zmir-opt-level=3"); From 6f1e1775d3586d0f82bd202de461e4de27a0fe1c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 17:40:16 +0000 Subject: [PATCH 065/101] Introduce SysrootTarget --- build_system/build_sysroot.rs | 69 +++++++++++++++++++++++------------ 1 file changed, 46 insertions(+), 23 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 413aa3444a54b..218db67a0eced 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -1,5 +1,5 @@ use std::fs; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::{self, Command}; use super::path::{Dirs, RelPath}; @@ -56,35 +56,40 @@ pub(crate) fn build_sysroot( spawn_and_wait(build_cargo_wrapper_cmd); } - let default_sysroot = super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc); - - let host_rustlib_lib = - RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); - let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib"); - fs::create_dir_all(&host_rustlib_lib).unwrap(); - fs::create_dir_all(&target_rustlib_lib).unwrap(); - if target_triple.ends_with("windows-gnu") { eprintln!("[BUILD] rtstartup for {target_triple}"); let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); + let mut target_libs = SysrootTarget { triple: target_triple.clone(), libs: vec![] }; for file in ["rsbegin", "rsend"] { + let obj = RelPath::BUILD.to_path(dirs).join(format!("{file}.o")); let mut build_rtstartup_cmd = Command::new(&bootstrap_host_compiler.rustc); build_rtstartup_cmd .arg("--target") .arg(&target_triple) .arg("--emit=obj") .arg("-o") - .arg(target_rustlib_lib.join(format!("{file}.o"))) + .arg(&obj) .arg(rtstartup_src.join(format!("{file}.rs"))); spawn_and_wait(build_rtstartup_cmd); + target_libs.libs.push(obj); } - } + target_libs.install_into_sysroot(&DIST_DIR.to_path(dirs)) + } match sysroot_kind { SysrootKind::None => {} // Nothing to do SysrootKind::Llvm => { + let default_sysroot = + super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc); + + let host_rustlib_lib = + RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); + let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib"); + fs::create_dir_all(&host_rustlib_lib).unwrap(); + fs::create_dir_all(&target_rustlib_lib).unwrap(); + for file in fs::read_dir( default_sysroot .join("lib") @@ -122,12 +127,13 @@ pub(crate) fn build_sysroot( } } SysrootKind::Clif => { - build_clif_sysroot_for_triple( + let host = build_clif_sysroot_for_triple( dirs, channel, bootstrap_host_compiler.clone(), &cg_clif_dylib_path, ); + host.install_into_sysroot(&DIST_DIR.to_path(dirs)); if !is_native { build_clif_sysroot_for_triple( @@ -140,16 +146,16 @@ pub(crate) fn build_sysroot( bootstrap_target_compiler }, &cg_clif_dylib_path, - ); + ) + .install_into_sysroot(&DIST_DIR.to_path(dirs)); } // Copy std for the host to the lib dir. This is necessary for the jit mode to find // libstd. - for file in fs::read_dir(host_rustlib_lib).unwrap() { - let file = file.unwrap().path(); - let filename = file.file_name().unwrap().to_str().unwrap(); + for lib in host.libs { + let filename = lib.file_name().unwrap().to_str().unwrap(); if filename.contains("std-") && !filename.contains(".rlib") { - try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap())); + try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap())); } } } @@ -162,6 +168,22 @@ pub(crate) fn build_sysroot( target_compiler } +struct SysrootTarget { + triple: String, + libs: Vec, +} + +impl SysrootTarget { + fn install_into_sysroot(&self, sysroot: &Path) { + let target_rustlib_lib = sysroot.join("lib").join("rustlib").join(&self.triple).join("lib"); + fs::create_dir_all(&target_rustlib_lib).unwrap(); + + for lib in &self.libs { + try_hard_link(lib, target_rustlib_lib.join(lib.file_name().unwrap())); + } + } +} + pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot"); pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot"); pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version"); @@ -169,12 +191,13 @@ pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src"); pub(crate) static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); +#[must_use] fn build_clif_sysroot_for_triple( dirs: &Dirs, channel: &str, mut compiler: Compiler, cg_clif_dylib_path: &Path, -) { +) -> SysrootTarget { match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) { Err(e) => { eprintln!("Failed to get rustc version for patched sysroot source: {}", e); @@ -219,7 +242,8 @@ fn build_clif_sysroot_for_triple( build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); - // Copy all relevant files to the sysroot + let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] }; + for entry in fs::read_dir(build_dir.join("deps")).unwrap() { let entry = entry.unwrap(); if let Some(ext) = entry.path().extension() { @@ -229,9 +253,8 @@ fn build_clif_sysroot_for_triple( } else { continue; }; - try_hard_link( - entry.path(), - RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()), - ); + target_libs.libs.push(entry.path()); } + + target_libs } From 0ac4456351b3767c0bd46af312cf6635cc98f6ef Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 17:51:54 +0000 Subject: [PATCH 066/101] Move rtstartup build to build_clif_sysroot_for_triple Also pass build/rtstartup as sysroot when building the standard library --- build_system/build_sysroot.rs | 57 ++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 218db67a0eced..e8c45643765f4 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -56,28 +56,6 @@ pub(crate) fn build_sysroot( spawn_and_wait(build_cargo_wrapper_cmd); } - if target_triple.ends_with("windows-gnu") { - eprintln!("[BUILD] rtstartup for {target_triple}"); - - let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); - let mut target_libs = SysrootTarget { triple: target_triple.clone(), libs: vec![] }; - - for file in ["rsbegin", "rsend"] { - let obj = RelPath::BUILD.to_path(dirs).join(format!("{file}.o")); - let mut build_rtstartup_cmd = Command::new(&bootstrap_host_compiler.rustc); - build_rtstartup_cmd - .arg("--target") - .arg(&target_triple) - .arg("--emit=obj") - .arg("-o") - .arg(&obj) - .arg(rtstartup_src.join(format!("{file}.rs"))); - spawn_and_wait(build_rtstartup_cmd); - target_libs.libs.push(obj); - } - - target_libs.install_into_sysroot(&DIST_DIR.to_path(dirs)) - } match sysroot_kind { SysrootKind::None => {} // Nothing to do SysrootKind::Llvm => { @@ -190,6 +168,7 @@ pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_ver pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src"); pub(crate) static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); +pub(crate) static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup"); #[must_use] fn build_clif_sysroot_for_triple( @@ -216,6 +195,35 @@ fn build_clif_sysroot_for_triple( } } + let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] }; + + if compiler.triple.ends_with("windows-gnu") { + eprintln!("[BUILD] rtstartup for {}", compiler.triple); + + RTSTARTUP_SYSROOT.ensure_fresh(dirs); + + let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); + let mut rtstartup_target_libs = + SysrootTarget { triple: compiler.triple.clone(), libs: vec![] }; + + for file in ["rsbegin", "rsend"] { + let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o")); + let mut build_rtstartup_cmd = Command::new(&compiler.rustc); + build_rtstartup_cmd + .arg("--target") + .arg(&compiler.triple) + .arg("--emit=obj") + .arg("-o") + .arg(&obj) + .arg(rtstartup_src.join(format!("{file}.rs"))); + spawn_and_wait(build_rtstartup_cmd); + rtstartup_target_libs.libs.push(obj.clone()); + target_libs.libs.push(obj); + } + + rtstartup_target_libs.install_into_sysroot(&RTSTARTUP_SYSROOT.to_path(dirs)); + } + let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel); if !super::config::get_bool("keep_sysroot") { @@ -230,7 +238,8 @@ fn build_clif_sysroot_for_triple( let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string(); rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap())); // Necessary for MinGW to find rsbegin.o and rsend.o - rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap())); + rustflags + .push_str(&format!(" --sysroot={}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap())); if channel == "release" { rustflags.push_str(" -Zmir-opt-level=3"); } @@ -242,8 +251,6 @@ fn build_clif_sysroot_for_triple( build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); - let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] }; - for entry in fs::read_dir(build_dir.join("deps")).unwrap() { let entry = entry.unwrap(); if let Some(ext) = entry.path().extension() { From fd83b2794536514ba5fb46f51ba76ea2a82c6841 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 18:25:43 +0000 Subject: [PATCH 067/101] Port llvm sysroot building to SysrootTarget too and dedup some code --- build_system/build_sysroot.rs | 159 +++++++++++++++++----------------- 1 file changed, 80 insertions(+), 79 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index e8c45643765f4..3f52721a4698e 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -12,7 +12,6 @@ use super::SysrootKind; static DIST_DIR: RelPath = RelPath::DIST; static BIN_DIR: RelPath = RelPath::DIST.join("bin"); static LIB_DIR: RelPath = RelPath::DIST.join("lib"); -static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib"); pub(crate) fn build_sysroot( dirs: &Dirs, @@ -56,86 +55,37 @@ pub(crate) fn build_sysroot( spawn_and_wait(build_cargo_wrapper_cmd); } - match sysroot_kind { - SysrootKind::None => {} // Nothing to do - SysrootKind::Llvm => { - let default_sysroot = - super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc); - - let host_rustlib_lib = - RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib"); - let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib"); - fs::create_dir_all(&host_rustlib_lib).unwrap(); - fs::create_dir_all(&target_rustlib_lib).unwrap(); - - for file in fs::read_dir( - default_sysroot - .join("lib") - .join("rustlib") - .join(&bootstrap_host_compiler.triple) - .join("lib"), - ) - .unwrap() - { - let file = file.unwrap().path(); - let file_name_str = file.file_name().unwrap().to_str().unwrap(); - if (file_name_str.contains("rustc_") - && !file_name_str.contains("rustc_std_workspace_") - && !file_name_str.contains("rustc_demangle")) - || file_name_str.contains("chalk") - || file_name_str.contains("tracing") - || file_name_str.contains("regex") - { - // These are large crates that are part of the rustc-dev component and are not - // necessary to run regular programs. - continue; - } - try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap())); - } + let host = build_sysroot_for_triple( + dirs, + channel, + bootstrap_host_compiler.clone(), + &cg_clif_dylib_path, + sysroot_kind, + ); + host.install_into_sysroot(&DIST_DIR.to_path(dirs)); - if !is_native { - for file in fs::read_dir( - default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"), - ) - .unwrap() - { - let file = file.unwrap().path(); - try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap())); - } - } - } - SysrootKind::Clif => { - let host = build_clif_sysroot_for_triple( - dirs, - channel, - bootstrap_host_compiler.clone(), - &cg_clif_dylib_path, - ); - host.install_into_sysroot(&DIST_DIR.to_path(dirs)); - - if !is_native { - build_clif_sysroot_for_triple( - dirs, - channel, - { - let mut bootstrap_target_compiler = bootstrap_host_compiler.clone(); - bootstrap_target_compiler.triple = target_triple.clone(); - bootstrap_target_compiler.set_cross_linker_and_runner(); - bootstrap_target_compiler - }, - &cg_clif_dylib_path, - ) - .install_into_sysroot(&DIST_DIR.to_path(dirs)); - } + if !is_native { + build_sysroot_for_triple( + dirs, + channel, + { + let mut bootstrap_target_compiler = bootstrap_host_compiler.clone(); + bootstrap_target_compiler.triple = target_triple.clone(); + bootstrap_target_compiler.set_cross_linker_and_runner(); + bootstrap_target_compiler + }, + &cg_clif_dylib_path, + sysroot_kind, + ) + .install_into_sysroot(&DIST_DIR.to_path(dirs)); + } - // Copy std for the host to the lib dir. This is necessary for the jit mode to find - // libstd. - for lib in host.libs { - let filename = lib.file_name().unwrap().to_str().unwrap(); - if filename.contains("std-") && !filename.contains(".rlib") { - try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap())); - } - } + // Copy std for the host to the lib dir. This is necessary for the jit mode to find + // libstd. + for lib in host.libs { + let filename = lib.file_name().unwrap().to_str().unwrap(); + if filename.contains("std-") && !filename.contains(".rlib") { + try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap())); } } @@ -170,6 +120,57 @@ pub(crate) static STANDARD_LIBRARY: CargoProject = CargoProject::new(&BUILD_SYSROOT, "build_sysroot"); pub(crate) static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup"); +#[must_use] +fn build_sysroot_for_triple( + dirs: &Dirs, + channel: &str, + compiler: Compiler, + cg_clif_dylib_path: &Path, + sysroot_kind: SysrootKind, +) -> SysrootTarget { + match sysroot_kind { + SysrootKind::None => SysrootTarget { triple: compiler.triple, libs: vec![] }, + SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler), + SysrootKind::Clif => { + build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path) + } + } +} + +#[must_use] +fn build_llvm_sysroot_for_triple(compiler: Compiler) -> SysrootTarget { + let default_sysroot = super::rustc_info::get_default_sysroot(&compiler.rustc); + + let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] }; + + for entry in fs::read_dir( + default_sysroot.join("lib").join("rustlib").join(&target_libs.triple).join("lib"), + ) + .unwrap() + { + let entry = entry.unwrap(); + if entry.file_type().unwrap().is_dir() { + continue; + } + let file = entry.path(); + let file_name_str = file.file_name().unwrap().to_str().unwrap(); + if (file_name_str.contains("rustc_") + && !file_name_str.contains("rustc_std_workspace_") + && !file_name_str.contains("rustc_demangle")) + || file_name_str.contains("chalk") + || file_name_str.contains("tracing") + || file_name_str.contains("regex") + { + // These are large crates that are part of the rustc-dev component and are not + // necessary to run regular programs. + continue; + } + target_libs.libs.push(file); + } + + target_libs +} + #[must_use] fn build_clif_sysroot_for_triple( dirs: &Dirs, From 280dffd82cba287bdac74cc23a0ea8359d7cd4e8 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 18:32:19 +0000 Subject: [PATCH 068/101] Build rtstartup for none sysroot too Even mini_core needs it --- build_system/build_sysroot.rs | 57 ++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 3f52721a4698e..f92841ab993d1 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -129,7 +129,8 @@ fn build_sysroot_for_triple( sysroot_kind: SysrootKind, ) -> SysrootTarget { match sysroot_kind { - SysrootKind::None => SysrootTarget { triple: compiler.triple, libs: vec![] }, + SysrootKind::None => build_rtstartup(dirs, &compiler) + .unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }), SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler), SysrootKind::Clif => { build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path) @@ -198,31 +199,10 @@ fn build_clif_sysroot_for_triple( let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] }; - if compiler.triple.ends_with("windows-gnu") { - eprintln!("[BUILD] rtstartup for {}", compiler.triple); - - RTSTARTUP_SYSROOT.ensure_fresh(dirs); - - let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); - let mut rtstartup_target_libs = - SysrootTarget { triple: compiler.triple.clone(), libs: vec![] }; - - for file in ["rsbegin", "rsend"] { - let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o")); - let mut build_rtstartup_cmd = Command::new(&compiler.rustc); - build_rtstartup_cmd - .arg("--target") - .arg(&compiler.triple) - .arg("--emit=obj") - .arg("-o") - .arg(&obj) - .arg(rtstartup_src.join(format!("{file}.rs"))); - spawn_and_wait(build_rtstartup_cmd); - rtstartup_target_libs.libs.push(obj.clone()); - target_libs.libs.push(obj); - } - + if let Some(rtstartup_target_libs) = build_rtstartup(dirs, &compiler) { rtstartup_target_libs.install_into_sysroot(&RTSTARTUP_SYSROOT.to_path(dirs)); + + target_libs.libs.extend(rtstartup_target_libs.libs); } let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel); @@ -266,3 +246,30 @@ fn build_clif_sysroot_for_triple( target_libs } + +fn build_rtstartup(dirs: &Dirs, compiler: &Compiler) -> Option { + if !compiler.triple.ends_with("windows-gnu") { + return None; + } + + RTSTARTUP_SYSROOT.ensure_fresh(dirs); + + let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup"); + let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] }; + + for file in ["rsbegin", "rsend"] { + let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o")); + let mut build_rtstartup_cmd = Command::new(&compiler.rustc); + build_rtstartup_cmd + .arg("--target") + .arg(&compiler.triple) + .arg("--emit=obj") + .arg("-o") + .arg(&obj) + .arg(rtstartup_src.join(format!("{file}.rs"))); + spawn_and_wait(build_rtstartup_cmd); + target_libs.libs.push(obj.clone()); + } + + Some(target_libs) +} From 13197322ec78820cdd3214d8001f81fa4773918b Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 14 Jan 2023 18:45:47 +0000 Subject: [PATCH 069/101] Skip creating sysroot target dir if it will be empty --- build_system/build_sysroot.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index f92841ab993d1..d3bc7ec01286a 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -103,6 +103,10 @@ struct SysrootTarget { impl SysrootTarget { fn install_into_sysroot(&self, sysroot: &Path) { + if self.libs.is_empty() { + return; + } + let target_rustlib_lib = sysroot.join("lib").join("rustlib").join(&self.triple).join("lib"); fs::create_dir_all(&target_rustlib_lib).unwrap(); From abcff71bec1ef1614041e59ea4f947bb0a13267c Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 15 Jan 2023 14:14:13 +0000 Subject: [PATCH 070/101] Significantly speed up assembling of sysroots MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit By avoiding some redundant rustc calls and stripping debuginfo for wrappers. ./y.rs build --sysroot none now runs 44% faster. Benchmark 1: ./y_before.bin build --sysroot none Time (mean ± σ): 2.200 s ± 0.038 s [User: 2.140 s, System: 0.653 s] Range (min … max): 2.171 s … 2.303 s 10 runs Benchmark 2: ./y_after.bin build --sysroot none Time (mean ± σ): 1.528 s ± 0.020 s [User: 1.388 s, System: 0.490 s] Range (min … max): 1.508 s … 1.580 s 10 runs Summary './y_after.bin build --sysroot none' ran 1.44 ± 0.03 times faster than './y_before.bin build --sysroot none' --- build_system/bench.rs | 5 +++-- build_system/build_sysroot.rs | 27 +++++++++++++++++++++------ build_system/rustc_info.rs | 9 --------- build_system/utils.rs | 19 +------------------ 4 files changed, 25 insertions(+), 35 deletions(-) diff --git a/build_system/bench.rs b/build_system/bench.rs index 1e83f21ba577b..01d44dafbdd17 100644 --- a/build_system/bench.rs +++ b/build_system/bench.rs @@ -4,7 +4,7 @@ use std::path::Path; use super::path::{Dirs, RelPath}; use super::prepare::GitRepo; -use super::rustc_info::{get_file_name, get_wrapper_file_name}; +use super::rustc_info::get_file_name; use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject, Compiler}; pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github( @@ -51,7 +51,8 @@ fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) { .unwrap(); eprintln!("[BENCH COMPILE] ebobby/simple-raytracer"); - let cargo_clif = RelPath::DIST.to_path(dirs).join(get_wrapper_file_name("cargo-clif", "bin")); + let cargo_clif = + RelPath::DIST.to_path(dirs).join(get_file_name("cargo_clif", "bin").replace('_', "-")); let manifest_path = SIMPLE_RAYTRACER.manifest_path(dirs); let target_dir = SIMPLE_RAYTRACER.target_dir(dirs); diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index d3bc7ec01286a..cbc58365e6974 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -3,9 +3,7 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{ - get_file_name, get_rustc_version, get_toolchain_name, get_wrapper_file_name, -}; +use super::rustc_info::{get_cargo_path, get_file_name, get_rustc_version, get_toolchain_name}; use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; @@ -42,8 +40,9 @@ pub(crate) fn build_sysroot( try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path); // Build and copy rustc and cargo wrappers + let wrapper_base_name = get_file_name("____", "bin"); for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] { - let wrapper_name = get_wrapper_file_name(wrapper, "bin"); + let wrapper_name = wrapper_base_name.replace("____", wrapper); let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc); build_cargo_wrapper_cmd @@ -51,7 +50,7 @@ pub(crate) fn build_sysroot( .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs"))) .arg("-o") .arg(DIST_DIR.to_path(dirs).join(wrapper_name)) - .arg("-g"); + .arg("-Cstrip=debuginfo"); spawn_and_wait(build_cargo_wrapper_cmd); } @@ -89,7 +88,23 @@ pub(crate) fn build_sysroot( } } - let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple); + let mut target_compiler = { + let dirs: &Dirs = &dirs; + let rustc_clif = + RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustc-clif")); + let rustdoc_clif = + RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif")); + + Compiler { + cargo: get_cargo_path(), + rustc: rustc_clif.clone(), + rustdoc: rustdoc_clif.clone(), + rustflags: String::new(), + rustdocflags: String::new(), + triple: target_triple, + runner: vec![], + } + }; if !is_native { target_compiler.set_cross_linker_and_runner(); } diff --git a/build_system/rustc_info.rs b/build_system/rustc_info.rs index 8a7e1c472dd10..a70453b442289 100644 --- a/build_system/rustc_info.rs +++ b/build_system/rustc_info.rs @@ -93,12 +93,3 @@ pub(crate) fn get_file_name(crate_name: &str, crate_type: &str) -> String { assert!(file_name.contains(crate_name)); file_name } - -/// Similar to `get_file_name`, but converts any dashes (`-`) in the `crate_name` to -/// underscores (`_`). This is specially made for the rustc and cargo wrappers -/// which have a dash in the name, and that is not allowed in a crate name. -pub(crate) fn get_wrapper_file_name(crate_name: &str, crate_type: &str) -> String { - let crate_name = crate_name.replace('-', "_"); - let wrapper_name = get_file_name(&crate_name, crate_type); - wrapper_name.replace('_', "-") -} diff --git a/build_system/utils.rs b/build_system/utils.rs index 07ea482fbbeaa..21bfb1b1f00f5 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -5,7 +5,7 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_cargo_path, get_rustc_path, get_rustdoc_path, get_wrapper_file_name}; +use super::rustc_info::{get_cargo_path, get_rustc_path, get_rustdoc_path}; #[derive(Clone, Debug)] pub(crate) struct Compiler { @@ -31,23 +31,6 @@ impl Compiler { } } - pub(crate) fn clif_with_triple(dirs: &Dirs, triple: String) -> Compiler { - let rustc_clif = - RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustc-clif", "bin")); - let rustdoc_clif = - RelPath::DIST.to_path(&dirs).join(get_wrapper_file_name("rustdoc-clif", "bin")); - - Compiler { - cargo: get_cargo_path(), - rustc: rustc_clif.clone(), - rustdoc: rustdoc_clif.clone(), - rustflags: String::new(), - rustdocflags: String::new(), - triple, - runner: vec![], - } - } - pub(crate) fn set_cross_linker_and_runner(&mut self) { match self.triple.as_str() { "aarch64-unknown-linux-gnu" => { From 0f4df8fb0e56f7ac157fba4c4209f520ba7fba79 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 15 Jan 2023 14:38:22 +0000 Subject: [PATCH 071/101] Eliminate a couple of extra calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is an additional 17% improvement on ./y.rs compile --sysroot none Benchmark 1: ./y_before.bin build --sysroot none Time (mean ± σ): 1.533 s ± 0.022 s [User: 1.411 s, System: 0.471 s] Range (min … max): 1.517 s … 1.589 s 10 runs Benchmark 2: ./y_after.bin build --sysroot none Time (mean ± σ): 1.311 s ± 0.020 s [User: 1.232 s, System: 0.428 s] Range (min … max): 1.298 s … 1.366 s 10 runs Summary './y_after.bin build --sysroot none' ran 1.17 ± 0.02 times faster than './y_before.bin build --sysroot none' --- build_system/build_sysroot.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index cbc58365e6974..f52d34ffcd63f 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -3,7 +3,7 @@ use std::path::{Path, PathBuf}; use std::process::{self, Command}; use super::path::{Dirs, RelPath}; -use super::rustc_info::{get_cargo_path, get_file_name, get_rustc_version, get_toolchain_name}; +use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name}; use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; @@ -36,17 +36,18 @@ pub(crate) fn build_sysroot( LIB_DIR } .to_path(dirs) - .join(get_file_name("rustc_codegen_cranelift", "dylib")); + .join(cg_clif_dylib_src.file_name().unwrap()); try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path); // Build and copy rustc and cargo wrappers let wrapper_base_name = get_file_name("____", "bin"); + let toolchain_name = get_toolchain_name(); for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] { let wrapper_name = wrapper_base_name.replace("____", wrapper); let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc); build_cargo_wrapper_cmd - .env("TOOLCHAIN_NAME", get_toolchain_name()) + .env("TOOLCHAIN_NAME", toolchain_name.clone()) .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs"))) .arg("-o") .arg(DIST_DIR.to_path(dirs).join(wrapper_name)) @@ -96,7 +97,7 @@ pub(crate) fn build_sysroot( RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif")); Compiler { - cargo: get_cargo_path(), + cargo: bootstrap_host_compiler.cargo.clone(), rustc: rustc_clif.clone(), rustdoc: rustdoc_clif.clone(), rustflags: String::new(), From 2b99b9fd25df59eb8b483e9809185f28d28199ed Mon Sep 17 00:00:00 2001 From: Kyle Huey Date: Sun, 15 Jan 2023 09:40:46 -0800 Subject: [PATCH 072/101] Preserve split DWARF files when building archives. The optimization that removes artifacts when building libraries is correct from the compiler's perspective but not from a debugger's perspective. Unpacked split debuginfo is referred to by filename and debuggers need the artifact that contains debuginfo to continue to exist at that path. Ironically the test expects the correct behavior but it was not running. --- compiler/rustc_codegen_ssa/src/back/link.rs | 6 ------ compiler/rustc_session/src/config.rs | 12 ------------ tests/run-make-fulldeps/split-debuginfo/Makefile | 4 ++-- 3 files changed, 2 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 342abf81f6a7c..5715bcf1f11d5 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -1300,12 +1300,6 @@ fn preserve_objects_for_their_debuginfo(sess: &Session) -> (bool, bool) { return (false, false); } - // If we're only producing artifacts that are archives, no need to preserve - // the objects as they're losslessly contained inside the archives. - if sess.crate_types().iter().all(|&x| x.is_archive()) { - return (false, false); - } - match (sess.split_debuginfo(), sess.opts.unstable_opts.split_dwarf_kind) { // If there is no split debuginfo then do not preserve objects. (SplitDebuginfo::Off, _) => (false, false), diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 55576b4e0d19d..46ae9b56a48f9 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -846,18 +846,6 @@ pub enum CrateType { ProcMacro, } -impl CrateType { - /// When generated, is this crate type an archive? - pub fn is_archive(&self) -> bool { - match *self { - CrateType::Rlib | CrateType::Staticlib => true, - CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::ProcMacro => { - false - } - } - } -} - #[derive(Clone, Hash, Debug, PartialEq, Eq)] pub enum Passes { Some(Vec), diff --git a/tests/run-make-fulldeps/split-debuginfo/Makefile b/tests/run-make-fulldeps/split-debuginfo/Makefile index 1831ab38fab49..44cda0d3d117f 100644 --- a/tests/run-make-fulldeps/split-debuginfo/Makefile +++ b/tests/run-make-fulldeps/split-debuginfo/Makefile @@ -254,12 +254,12 @@ unpacked-remapped-single: $(RUSTC) $(UNSTABLEOPTS) -C split-debuginfo=unpacked -C debuginfo=2 \ -Z split-dwarf-kind=single --remap-path-prefix $(TMPDIR)=/a foo.rs -g objdump -Wi $(TMPDIR)/foo | grep DW_AT_GNU_dwo_name | (! grep $(TMPDIR)) || exit 1 - ls $(TMPDIR)/*.o + rm $(TMPDIR)/*.o ls $(TMPDIR)/*.dwo && exit 1 || exit 0 ls $(TMPDIR)/*.dwp && exit 1 || exit 0 rm $(TMPDIR)/$(call BIN,foo) -unpacked-crosscrate: packed-crosscrate-split packed-crosscrate-single +unpacked-crosscrate: unpacked-crosscrate-split unpacked-crosscrate-single # - Debuginfo in `.dwo` files # - (bar) `.rlib` file created, contains `.dwo` From 70957832684155dbe74a313d576028a5af5f89e9 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 16 Jan 2023 11:21:37 +0000 Subject: [PATCH 073/101] Only run abi-cafe on cg_clif's CI --- .github/workflows/main.yml | 8 ++++++++ build_system/abi_cafe.rs | 6 ------ build_system/mod.rs | 20 ++++++++------------ build_system/usage.txt | 1 + config.txt | 2 -- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1181c935b8389..90004b408c014 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -114,6 +114,14 @@ jobs: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} run: ./y.rs test + - name: Test abi-cafe + env: + TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} + run: | + if [[ "$(rustc -vV | grep host | cut -d' ' -f2)" == "$TARGET_TRIPLE" ]]; then + ./y.rs abi-cafe + fi + - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz dist diff --git a/build_system/abi_cafe.rs b/build_system/abi_cafe.rs index 8742389f33227..dbee9be04eea6 100644 --- a/build_system/abi_cafe.rs +++ b/build_system/abi_cafe.rs @@ -1,7 +1,6 @@ use std::path::Path; use super::build_sysroot; -use super::config; use super::path::Dirs; use super::prepare::GitRepo; use super::utils::{spawn_and_wait, CargoProject, Compiler}; @@ -20,11 +19,6 @@ pub(crate) fn run( cg_clif_dylib: &Path, bootstrap_host_compiler: &Compiler, ) { - if !config::get_bool("testsuite.abi-cafe") { - eprintln!("[RUN] abi-cafe (skipped)"); - return; - } - eprintln!("Building sysroot for abi-cafe"); build_sysroot::build_sysroot( dirs, diff --git a/build_system/mod.rs b/build_system/mod.rs index 6f388cd605fce..8dcbe8de189b2 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -32,6 +32,7 @@ enum Command { Prepare, Build, Test, + AbiCafe, Bench, } @@ -61,6 +62,7 @@ pub fn main() { Some("prepare") => Command::Prepare, Some("build") => Command::Build, Some("test") => Command::Test, + Some("abi-cafe") => Command::AbiCafe, Some("bench") => Command::Bench, Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag), Some(command) => arg_error!("Unknown command {}", command), @@ -156,19 +158,13 @@ pub fn main() { &bootstrap_host_compiler, target_triple.clone(), ); - - if bootstrap_host_compiler.triple == target_triple { - abi_cafe::run( - channel, - sysroot_kind, - &dirs, - &cg_clif_dylib, - &bootstrap_host_compiler, - ); - } else { - eprintln!("[RUN] abi-cafe (skipped, cross-compilation not supported)"); - return; + } + Command::AbiCafe => { + if bootstrap_host_compiler.triple != target_triple { + eprintln!("Abi-cafe doesn't support cross-compilation"); + process::exit(1); } + abi_cafe::run(channel, sysroot_kind, &dirs, &cg_clif_dylib, &bootstrap_host_compiler); } Command::Build => { build_sysroot::build_sysroot( diff --git a/build_system/usage.txt b/build_system/usage.txt index 9185255155426..ab98ccc35a58a 100644 --- a/build_system/usage.txt +++ b/build_system/usage.txt @@ -4,6 +4,7 @@ USAGE: ./y.rs prepare [--out-dir DIR] ./y.rs build [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] ./y.rs test [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] + ./y.rs abi-cafe [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] ./y.rs bench [--debug] [--sysroot none|clif|llvm] [--out-dir DIR] [--no-unstable-features] OPTIONS: diff --git a/config.txt b/config.txt index d9912a8158f67..d49cc90791a5d 100644 --- a/config.txt +++ b/config.txt @@ -49,5 +49,3 @@ test.libcore test.regex-shootout-regex-dna test.regex test.portable-simd - -testsuite.abi-cafe From 08c7230989ff02471aeaf93aa4325a8ef6e9c30d Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Wed, 7 Dec 2022 09:24:00 +0000 Subject: [PATCH 074/101] Move compiler input and ouput paths into session --- src/debuginfo/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/debuginfo/mod.rs b/src/debuginfo/mod.rs index 2ba012a77b0a9..28fbcb15b2b58 100644 --- a/src/debuginfo/mod.rs +++ b/src/debuginfo/mod.rs @@ -68,7 +68,7 @@ impl DebugContext { .working_dir .to_string_lossy(FileNameDisplayPreference::Remapped) .into_owned(); - let (name, file_info) = match tcx.sess.local_crate_source_file.clone() { + let (name, file_info) = match tcx.sess.local_crate_source_file() { Some(path) => { let name = path.to_string_lossy().into_owned(); (name, None) From 1a6ab6c16a4902aef1e6d49ca188e1e112c677b7 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 17 Jan 2023 08:00:01 +0000 Subject: [PATCH 075/101] Empty regions don't exist anymore, remove them from fluent --- compiler/rustc_error_messages/locales/en-US/infer.ftl | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/infer.ftl b/compiler/rustc_error_messages/locales/en-US/infer.ftl index ae0091b03736f..03a51142a3dfb 100644 --- a/compiler/rustc_error_messages/locales/en-US/infer.ftl +++ b/compiler/rustc_error_messages/locales/en-US/infer.ftl @@ -147,8 +147,6 @@ infer_region_explanation = {$pref_kind -> }{$desc_kind -> *[should_not_happen] [{$desc_kind}] [restatic] the static lifetime - [reempty] the empty lifetime - [reemptyuni] the empty lifetime in universe {$desc_arg} [revar] lifetime {$desc_arg} [as_defined] the lifetime `{$desc_arg}` as defined here From 49a9438681639ad9ca30bc2f69a4fd231cd35e09 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Wed, 16 Nov 2022 20:34:16 +0000 Subject: [PATCH 076/101] Remove double spaces after dots in comments --- src/constant.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/constant.rs b/src/constant.rs index dee6fb5b5130d..51450897bfc11 100644 --- a/src/constant.rs +++ b/src/constant.rs @@ -304,7 +304,7 @@ fn data_id_for_static( // Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141 // Declare an internal global `extern_with_linkage_foo` which - // is initialized with the address of `foo`. If `foo` is + // is initialized with the address of `foo`. If `foo` is // discarded during linking (for example, if `foo` has weak // linkage and there are no definitions), then // `extern_with_linkage_foo` will instead be initialized to From 64e5f9129f6ffb382b458a255eed36f2581271c4 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 17 Jan 2023 08:21:34 +0000 Subject: [PATCH 077/101] Handle diagnostics customization on the fluent side --- .../src/region_infer/opaque_types.rs | 12 ------------ .../locales/en-US/borrowck.ftl | 5 ++++- compiler/rustc_error_messages/src/lib.rs | 15 +++++++++++++++ .../type-alias-impl-trait/bounds-are-checked.rs | 2 +- .../bounds-are-checked.stderr | 3 ++- .../generic_nondefining_use.rs | 2 +- .../generic_nondefining_use.stderr | 2 +- 7 files changed, 24 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs index 9d8812b7eeaac..f4d31cf1c34d7 100644 --- a/compiler/rustc_borrowck/src/region_infer/opaque_types.rs +++ b/compiler/rustc_borrowck/src/region_infer/opaque_types.rs @@ -368,18 +368,6 @@ fn check_opaque_type_parameter_valid( for (i, arg) in opaque_type_key.substs.iter().enumerate() { let arg_is_param = match arg.unpack() { GenericArgKind::Type(ty) => matches!(ty.kind(), ty::Param(_)), - GenericArgKind::Lifetime(lt) if lt.is_static() => { - tcx.sess - .struct_span_err(span, "non-defining opaque type use in defining scope") - .span_label( - tcx.def_span(opaque_generics.param_at(i, tcx).def_id), - "cannot use static lifetime; use a bound lifetime \ - instead or remove the lifetime parameter from the \ - opaque type", - ) - .emit(); - return false; - } GenericArgKind::Lifetime(lt) => { matches!(*lt, ty::ReEarlyBound(_) | ty::ReFree(_)) } diff --git a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl index 9e4332c428386..0021638c10268 100644 --- a/compiler/rustc_error_messages/locales/en-US/borrowck.ftl +++ b/compiler/rustc_error_messages/locales/en-US/borrowck.ftl @@ -123,4 +123,7 @@ borrowck_cannot_move_when_borrowed = borrowck_opaque_type_non_generic_param = expected generic {$kind} parameter, found `{$ty}` - .label = this generic parameter must be used with a generic {$kind} parameter + .label = {STREQ($ty, "'static") -> + [true] cannot use static lifetime; use a bound lifetime instead or remove the lifetime parameter from the opaque type + *[other] this generic parameter must be used with a generic {$kind} parameter + } diff --git a/compiler/rustc_error_messages/src/lib.rs b/compiler/rustc_error_messages/src/lib.rs index 37a51980a0888..f053bdc3809be 100644 --- a/compiler/rustc_error_messages/src/lib.rs +++ b/compiler/rustc_error_messages/src/lib.rs @@ -182,6 +182,9 @@ pub fn fluent_bundle( trace!(?locale); let mut bundle = new_bundle(vec![locale]); + // Add convenience functions available to ftl authors. + register_functions(&mut bundle); + // Fluent diagnostics can insert directionality isolation markers around interpolated variables // indicating that there may be a shift from right-to-left to left-to-right text (or // vice-versa). These are disabled because they are sometimes visible in the error output, but @@ -244,6 +247,15 @@ pub fn fluent_bundle( Ok(Some(bundle)) } +fn register_functions(bundle: &mut FluentBundle) { + bundle + .add_function("STREQ", |positional, _named| match positional { + [FluentValue::String(a), FluentValue::String(b)] => format!("{}", (a == b)).into(), + _ => FluentValue::Error, + }) + .expect("Failed to add a function to the bundle."); +} + /// Type alias for the result of `fallback_fluent_bundle` - a reference-counted pointer to a lazily /// evaluated fluent bundle. pub type LazyFallbackBundle = Lrc FluentBundle>>; @@ -256,6 +268,9 @@ pub fn fallback_fluent_bundle( ) -> LazyFallbackBundle { Lrc::new(Lazy::new(move || { let mut fallback_bundle = new_bundle(vec![langid!("en-US")]); + + register_functions(&mut fallback_bundle); + // See comment in `fluent_bundle`. fallback_bundle.set_use_isolating(with_directionality_markers); diff --git a/tests/ui/type-alias-impl-trait/bounds-are-checked.rs b/tests/ui/type-alias-impl-trait/bounds-are-checked.rs index 83d22161e4e75..eeb5dca07f06a 100644 --- a/tests/ui/type-alias-impl-trait/bounds-are-checked.rs +++ b/tests/ui/type-alias-impl-trait/bounds-are-checked.rs @@ -8,7 +8,7 @@ type X<'a> = impl Into<&'static str> + From<&'a str>; fn f<'a: 'static>(t: &'a str) -> X<'a> { //~^ WARNING unnecessary lifetime parameter t - //~^ ERROR non-defining opaque type use + //~^ ERROR expected generic lifetime parameter, found `'static` } fn extend_lt<'a>(o: &'a str) -> &'static str { diff --git a/tests/ui/type-alias-impl-trait/bounds-are-checked.stderr b/tests/ui/type-alias-impl-trait/bounds-are-checked.stderr index 920eef11da4b9..94882597a62e6 100644 --- a/tests/ui/type-alias-impl-trait/bounds-are-checked.stderr +++ b/tests/ui/type-alias-impl-trait/bounds-are-checked.stderr @@ -6,7 +6,7 @@ LL | fn f<'a: 'static>(t: &'a str) -> X<'a> { | = help: you can use the `'static` lifetime directly, in place of `'a` -error: non-defining opaque type use in defining scope +error[E0792]: expected generic lifetime parameter, found `'static` --> $DIR/bounds-are-checked.rs:10:5 | LL | type X<'a> = impl Into<&'static str> + From<&'a str>; @@ -17,3 +17,4 @@ LL | t error: aborting due to previous error; 1 warning emitted +For more information about this error, try `rustc --explain E0792`. diff --git a/tests/ui/type-alias-impl-trait/generic_nondefining_use.rs b/tests/ui/type-alias-impl-trait/generic_nondefining_use.rs index f5045d382aac4..e7b8567b9a217 100644 --- a/tests/ui/type-alias-impl-trait/generic_nondefining_use.rs +++ b/tests/ui/type-alias-impl-trait/generic_nondefining_use.rs @@ -19,7 +19,7 @@ fn concrete_ty() -> OneTy { fn concrete_lifetime() -> OneLifetime<'static> { 6u32 - //~^ ERROR non-defining opaque type use in defining scope + //~^ ERROR expected generic lifetime parameter, found `'static` } fn concrete_const() -> OneConst<{ 123 }> { diff --git a/tests/ui/type-alias-impl-trait/generic_nondefining_use.stderr b/tests/ui/type-alias-impl-trait/generic_nondefining_use.stderr index 564648630b161..966fe823f024d 100644 --- a/tests/ui/type-alias-impl-trait/generic_nondefining_use.stderr +++ b/tests/ui/type-alias-impl-trait/generic_nondefining_use.stderr @@ -7,7 +7,7 @@ LL | type OneTy = impl Debug; LL | 5u32 | ^^^^ -error: non-defining opaque type use in defining scope +error[E0792]: expected generic lifetime parameter, found `'static` --> $DIR/generic_nondefining_use.rs:21:5 | LL | type OneLifetime<'a> = impl Debug; From 6fe4cf795bb50e2168f9bc3f12391febc83a54c7 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Tue, 17 Jan 2023 13:48:43 +0100 Subject: [PATCH 078/101] Migrate mir_build's borrow conflicts --- .../locales/en-US/mir_build.ftl | 16 +- compiler/rustc_mir_build/src/errors.rs | 50 ++++-- .../src/thir/pattern/check_match.rs | 77 +++++----- ...atterns-slice-patterns-box-patterns.stderr | 8 +- ...can-live-while-the-other-survives-1.stderr | 8 +- .../borrowck-pat-at-and-box.stderr | 32 ++-- .../borrowck-pat-by-move-and-ref.stderr | 120 +++++++-------- .../borrowck-pat-ref-mut-and-ref.stderr | 144 +++++++++--------- .../borrowck-pat-ref-mut-twice.stderr | 96 ++++++------ ...inding-modes-both-sides-independent.stderr | 16 +- .../ui/suggestions/ref-pattern-binding.stderr | 8 +- 11 files changed, 302 insertions(+), 273 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl index a082c0b61fa7e..ec216de31d324 100644 --- a/compiler/rustc_error_messages/locales/en-US/mir_build.ftl +++ b/compiler/rustc_error_messages/locales/en-US/mir_build.ftl @@ -299,10 +299,18 @@ mir_build_borrow_of_moved_value = borrow of moved value .suggestion = borrow this binding in the pattern to avoid moving the value mir_build_multiple_mut_borrows = cannot borrow value as mutable more than once at a time - .label = first mutable borrow, by `{$name}`, occurs here - .mutable_borrow = another mutable borrow, by `{$name_mut}`, occurs here - .immutable_borrow = also borrowed as immutable, by `{$name_immut}`, here - .moved = also moved into `{$name_moved}` here + +mir_build_already_borrowed = cannot borrow value as mutable because it is also borrowed as immutable + +mir_build_already_mut_borrowed = cannot borrow value as immutable because it is also borrowed as mutable + +mir_build_moved_while_borrowed = cannot move out of value because it is borrowed + +mir_build_mutable_borrow = value is mutably borrowed by `{$name}` here + +mir_build_borrow = value is borrowed by `{$name}` here + +mir_build_moved = value is moved into `{$name}` here mir_build_union_pattern = cannot use unions in constant patterns diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 06523b0a1de84..d3cd5f080ec9f 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -600,32 +600,56 @@ pub struct BorrowOfMovedValue<'tcx> { pub struct MultipleMutBorrows { #[primary_span] pub span: Span, - #[label] - pub binding_span: Span, #[subdiagnostic] - pub occurences: Vec, - pub name: Ident, + pub occurences: Vec, +} + +#[derive(Diagnostic)] +#[diag(mir_build_already_borrowed)] +pub struct AlreadyBorrowed { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub occurences: Vec, +} + +#[derive(Diagnostic)] +#[diag(mir_build_already_mut_borrowed)] +pub struct AlreadyMutBorrowed { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub occurences: Vec, +} + +#[derive(Diagnostic)] +#[diag(mir_build_moved_while_borrowed)] +pub struct MovedWhileBorrowed { + #[primary_span] + pub span: Span, + #[subdiagnostic] + pub occurences: Vec, } #[derive(Subdiagnostic)] -pub enum MultipleMutBorrowOccurence { - #[label(mutable_borrow)] - Mutable { +pub enum Conflict { + #[label(mir_build_mutable_borrow)] + Mut { #[primary_span] span: Span, - name_mut: Ident, + name: Ident, }, - #[label(immutable_borrow)] - Immutable { + #[label(mir_build_borrow)] + Ref { #[primary_span] span: Span, - name_immut: Ident, + name: Ident, }, - #[label(moved)] + #[label(mir_build_moved)] Moved { #[primary_span] span: Span, - name_moved: Ident, + name: Ident, }, } diff --git a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs index e13c0662ef85f..1881eec174a17 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/check_match.rs @@ -914,58 +914,55 @@ fn check_borrow_conflicts_in_at_patterns(cx: &MatchVisitor<'_, '_, '_>, pat: &Pa sub.each_binding(|_, hir_id, span, name| { match typeck_results.extract_binding_mode(sess, hir_id, span) { Some(ty::BindByReference(mut_inner)) => match (mut_outer, mut_inner) { - (Mutability::Not, Mutability::Not) => {} // Both sides are `ref`. - (Mutability::Mut, Mutability::Mut) => conflicts_mut_mut.push((span, name)), // 2x `ref mut`. - _ => conflicts_mut_ref.push((span, name)), // `ref` + `ref mut` in either direction. + // Both sides are `ref`. + (Mutability::Not, Mutability::Not) => {} + // 2x `ref mut`. + (Mutability::Mut, Mutability::Mut) => { + conflicts_mut_mut.push(Conflict::Mut { span, name }) + } + (Mutability::Not, Mutability::Mut) => { + conflicts_mut_ref.push(Conflict::Mut { span, name }) + } + (Mutability::Mut, Mutability::Not) => { + conflicts_mut_ref.push(Conflict::Ref { span, name }) + } }, Some(ty::BindByValue(_)) if is_binding_by_move(cx, hir_id) => { - conflicts_move.push((span, name)) // `ref mut?` + by-move conflict. + conflicts_move.push(Conflict::Moved { span, name }) // `ref mut?` + by-move conflict. } Some(ty::BindByValue(_)) | None => {} // `ref mut?` + by-copy is fine. } }); + let report_mut_mut = !conflicts_mut_mut.is_empty(); + let report_mut_ref = !conflicts_mut_ref.is_empty(); + let report_move_conflict = !conflicts_move.is_empty(); + + let mut occurences = match mut_outer { + Mutability::Mut => vec![Conflict::Mut { span: binding_span, name }], + Mutability::Not => vec![Conflict::Ref { span: binding_span, name }], + }; + occurences.extend(conflicts_mut_mut); + occurences.extend(conflicts_mut_ref); + occurences.extend(conflicts_move); + // Report errors if any. - if !conflicts_mut_mut.is_empty() { + if report_mut_mut { // Report mutability conflicts for e.g. `ref mut x @ Some(ref mut y)`. - let mut occurences = vec![]; - - for (span, name_mut) in conflicts_mut_mut { - occurences.push(MultipleMutBorrowOccurence::Mutable { span, name_mut }); - } - for (span, name_immut) in conflicts_mut_ref { - occurences.push(MultipleMutBorrowOccurence::Immutable { span, name_immut }); - } - for (span, name_moved) in conflicts_move { - occurences.push(MultipleMutBorrowOccurence::Moved { span, name_moved }); - } - sess.emit_err(MultipleMutBorrows { span: pat.span, binding_span, occurences, name }); - } else if !conflicts_mut_ref.is_empty() { + sess.emit_err(MultipleMutBorrows { span: pat.span, occurences }); + } else if report_mut_ref { // Report mutability conflicts for e.g. `ref x @ Some(ref mut y)` or the converse. - let (primary, also) = match mut_outer { - Mutability::Mut => ("mutable", "immutable"), - Mutability::Not => ("immutable", "mutable"), + match mut_outer { + Mutability::Mut => { + sess.emit_err(AlreadyMutBorrowed { span: pat.span, occurences }); + } + Mutability::Not => { + sess.emit_err(AlreadyBorrowed { span: pat.span, occurences }); + } }; - let msg = - format!("cannot borrow value as {} because it is also borrowed as {}", also, primary); - let mut err = sess.struct_span_err(pat.span, &msg); - err.span_label(binding_span, format!("{} borrow, by `{}`, occurs here", primary, name)); - for (span, name) in conflicts_mut_ref { - err.span_label(span, format!("{} borrow, by `{}`, occurs here", also, name)); - } - for (span, name) in conflicts_move { - err.span_label(span, format!("also moved into `{}` here", name)); - } - err.emit(); - } else if !conflicts_move.is_empty() { + } else if report_move_conflict { // Report by-ref and by-move conflicts, e.g. `ref x @ y`. - let mut err = - sess.struct_span_err(pat.span, "cannot move out of value because it is borrowed"); - err.span_label(binding_span, format!("value borrowed, by `{}`, here", name)); - for (span, name) in conflicts_move { - err.span_label(span, format!("value moved into `{}` here", name)); - } - err.emit(); + sess.emit_err(MovedWhileBorrowed { span: pat.span, occurences }); } } diff --git a/tests/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr b/tests/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr index 50eee1049db6e..5835f06753bb1 100644 --- a/tests/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr +++ b/tests/ui/borrowck/bindings-after-at-or-patterns-slice-patterns-box-patterns.stderr @@ -4,8 +4,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref foo @ [.., ref mut bar] => (), | -------^^^^^^^^-----------^ | | | - | | mutable borrow, by `bar`, occurs here - | immutable borrow, by `foo`, occurs here + | | value is mutably borrowed by `bar` here + | value is borrowed by `foo` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:120:9 @@ -13,8 +13,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref foo @ Some(box ref mut s) => (), | -------^^^^^^^^^^^^---------^ | | | - | | mutable borrow, by `s`, occurs here - | immutable borrow, by `foo`, occurs here + | | value is mutably borrowed by `s` here + | value is borrowed by `foo` here error[E0382]: borrow of moved value: `x` --> $DIR/bindings-after-at-or-patterns-slice-patterns-box-patterns.rs:18:5 diff --git a/tests/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr b/tests/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr index c8b45fd24d98c..29cd6c45c34b5 100644 --- a/tests/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr +++ b/tests/ui/pattern/bindings-after-at/bind-by-move-neither-can-live-while-the-other-survives-1.stderr @@ -4,8 +4,8 @@ error: cannot move out of value because it is borrowed LL | Some(ref _y @ _z) => {} | ------^^^-- | | | - | | value moved into `_z` here - | value borrowed, by `_y`, here + | | value is moved into `_z` here + | value is borrowed by `_y` here error: borrow of moved value --> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:19:14 @@ -28,8 +28,8 @@ error: cannot move out of value because it is borrowed LL | Some(ref mut _y @ _z) => {} | ----------^^^-- | | | - | | value moved into `_z` here - | value borrowed, by `_y`, here + | | value is moved into `_z` here + | value is mutably borrowed by `_y` here error: borrow of moved value --> $DIR/bind-by-move-neither-can-live-while-the-other-survives-1.rs:33:14 diff --git a/tests/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr b/tests/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr index f27df32ccfa5c..2c123b01e173e 100644 --- a/tests/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr +++ b/tests/ui/pattern/bindings-after-at/borrowck-pat-at-and-box.stderr @@ -4,8 +4,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ box b = Box::new(NC); | -----^^^^^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-at-and-box.rs:34:9 @@ -13,8 +13,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ box ref mut b = Box::new(nc()); | -----^^^^^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-at-and-box.rs:36:9 @@ -22,8 +22,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ box ref mut b = Box::new(NC); | -----^^^^^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-at-and-box.rs:38:9 @@ -31,8 +31,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ box ref mut b = Box::new(NC); | -----^^^^^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-at-and-box.rs:42:9 @@ -40,8 +40,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ box ref mut b = Box::new(NC); | -----^^^^^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-at-and-box.rs:48:9 @@ -49,8 +49,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ box ref b = Box::new(NC); | ---------^^^^^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-at-and-box.rs:62:9 @@ -58,8 +58,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ box ref b => { | ---------^^^^^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-at-and-box.rs:54:11 @@ -67,8 +67,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | fn f5(ref mut a @ box ref b: Box) { | ---------^^^^^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error[E0382]: borrow of moved value --> $DIR/borrowck-pat-at-and-box.rs:31:9 diff --git a/tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr b/tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr index 770bb89530cca..4f7fbc9e04b04 100644 --- a/tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr +++ b/tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref.stderr @@ -4,8 +4,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ b = U; | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:26:9 @@ -13,9 +13,9 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (U, U); | -----^^^^^^^^^^^^-----^^^^^^^^^^-^ | | | | - | | | value moved into `e` here - | | value moved into `c` here - | value borrowed, by `a`, here + | | | value is moved into `e` here + | | value is moved into `c` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:26:18 @@ -23,8 +23,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (U, U); | -----^^^----- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is borrowed by `b` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:26:33 @@ -32,8 +32,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (U, U); | -----^^^- | | | - | | value moved into `e` here - | value borrowed, by `d`, here + | | value is moved into `e` here + | value is borrowed by `d` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:30:9 @@ -41,9 +41,9 @@ error: cannot move out of value because it is borrowed LL | let ref mut a @ [b, mut c] = [U, U]; | ---------^^^^-^^-----^ | | | | - | | | value moved into `c` here - | | value moved into `b` here - | value borrowed, by `a`, here + | | | value is moved into `c` here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:33:9 @@ -51,8 +51,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ b = u(); | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:36:9 @@ -60,9 +60,9 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (u(), u()); | -----^^^^^^^^^^^^-----^^^^^^^^^^-^ | | | | - | | | value moved into `e` here - | | value moved into `c` here - | value borrowed, by `a`, here + | | | value is moved into `e` here + | | value is moved into `c` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:36:18 @@ -70,8 +70,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (u(), u()); | -----^^^----- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is borrowed by `b` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:36:33 @@ -79,8 +79,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ (ref b @ mut c, ref d @ e) = (u(), u()); | -----^^^- | | | - | | value moved into `e` here - | value borrowed, by `d`, here + | | value is moved into `e` here + | value is borrowed by `d` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:42:9 @@ -88,9 +88,9 @@ error: cannot move out of value because it is borrowed LL | let ref mut a @ [b, mut c] = [u(), u()]; | ---------^^^^-^^-----^ | | | | - | | | value moved into `c` here - | | value moved into `b` here - | value borrowed, by `a`, here + | | | value is moved into `c` here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:47:9 @@ -98,8 +98,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some(b) => {} | -----^^^^^^^^-^ | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:52:9 @@ -107,9 +107,9 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^^^^^^^^^^^^^^^-----^^^^^^^^^^-^^ | | | | - | | | value moved into `e` here - | | value moved into `c` here - | value borrowed, by `a`, here + | | | value is moved into `e` here + | | value is moved into `c` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:52:23 @@ -117,8 +117,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^----- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is borrowed by `b` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:52:38 @@ -126,8 +126,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^- | | | - | | value moved into `e` here - | value borrowed, by `d`, here + | | value is moved into `e` here + | value is borrowed by `d` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:59:9 @@ -135,9 +135,9 @@ error: cannot move out of value because it is borrowed LL | ref mut a @ Some([b, mut c]) => {} | ---------^^^^^^^^^-^^-----^^ | | | | - | | | value moved into `c` here - | | value moved into `b` here - | value borrowed, by `a`, here + | | | value is moved into `c` here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:64:9 @@ -145,8 +145,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some(b) => {} | -----^^^^^^^^-^ | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:69:9 @@ -154,9 +154,9 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^^^^^^^^^^^^^^^-----^^^^^^^^^^-^^ | | | | - | | | value moved into `e` here - | | value moved into `c` here - | value borrowed, by `a`, here + | | | value is moved into `e` here + | | value is moved into `c` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:69:23 @@ -164,8 +164,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^----- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is borrowed by `b` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:69:38 @@ -173,8 +173,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ Some((ref b @ mut c, ref d @ e)) => {} | -----^^^- | | | - | | value moved into `e` here - | value borrowed, by `d`, here + | | value is moved into `e` here + | value is borrowed by `d` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:78:9 @@ -182,9 +182,9 @@ error: cannot move out of value because it is borrowed LL | ref mut a @ Some([b, mut c]) => {} | ---------^^^^^^^^^-^^-----^^ | | | | - | | | value moved into `c` here - | | value moved into `b` here - | value borrowed, by `a`, here + | | | value is moved into `c` here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:11:11 @@ -192,8 +192,8 @@ error: cannot move out of value because it is borrowed LL | fn f1(ref a @ b: U) {} | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:14:11 @@ -201,9 +201,9 @@ error: cannot move out of value because it is borrowed LL | fn f2(ref a @ (ref b @ mut c, ref d @ e): (U, U)) {} | -----^^^^^^^^^^^^-----^^^^^^^^^^-^ | | | | - | | | value moved into `e` here - | | value moved into `c` here - | value borrowed, by `a`, here + | | | value is moved into `e` here + | | value is moved into `c` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:14:20 @@ -211,8 +211,8 @@ error: cannot move out of value because it is borrowed LL | fn f2(ref a @ (ref b @ mut c, ref d @ e): (U, U)) {} | -----^^^----- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is borrowed by `b` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:14:35 @@ -220,8 +220,8 @@ error: cannot move out of value because it is borrowed LL | fn f2(ref a @ (ref b @ mut c, ref d @ e): (U, U)) {} | -----^^^- | | | - | | value moved into `e` here - | value borrowed, by `d`, here + | | value is moved into `e` here + | value is borrowed by `d` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-by-move-and-ref.rs:20:11 @@ -229,9 +229,9 @@ error: cannot move out of value because it is borrowed LL | fn f3(ref mut a @ [b, mut c]: [U; 2]) {} | ---------^^^^-^^-----^ | | | | - | | | value moved into `c` here - | | value moved into `b` here - | value borrowed, by `a`, here + | | | value is moved into `c` here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error[E0382]: borrow of partially moved value --> $DIR/borrowck-pat-by-move-and-ref.rs:30:9 diff --git a/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr index 8546b4bb47734..f51b5041858c6 100644 --- a/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr +++ b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-and-ref.stderr @@ -4,8 +4,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut z @ &mut Some(ref a) => { | ---------^^^^^^^^^^^^^-----^ | | | - | | immutable borrow, by `a`, occurs here - | mutable borrow, by `z`, occurs here + | | value is borrowed by `a` here + | value is mutably borrowed by `z` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-and-ref.rs:33:9 @@ -13,9 +13,9 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub | ---------^^^^-----------------^ | | | | - | | | another mutable borrow, by `c`, occurs here - | | also borrowed as immutable, by `b`, here - | first mutable borrow, by `a`, occurs here + | | | value is mutably borrowed by `c` here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:33:22 @@ -23,8 +23,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref mut a @ (ref b @ ref mut c) = u(); // sub-in-sub | -----^^^--------- | | | - | | mutable borrow, by `c`, occurs here - | immutable borrow, by `b`, occurs here + | | value is mutably borrowed by `c` here + | value is borrowed by `b` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:37:9 @@ -32,8 +32,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ ref mut b = U; | -----^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:39:9 @@ -41,8 +41,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ ref b = U; | ---------^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:41:9 @@ -50,9 +50,9 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ (ref mut b, ref mut c) = (U, U); | -----^^^^---------^^---------^ | | | | - | | | mutable borrow, by `c`, occurs here - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | | value is mutably borrowed by `c` here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:43:9 @@ -60,9 +60,9 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ (ref b, ref c) = (U, U); | ---------^^^^-----^^-----^ | | | | - | | | immutable borrow, by `c`, occurs here - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | | value is borrowed by `c` here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:46:9 @@ -70,8 +70,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ ref b = u(); | ---------^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:51:9 @@ -79,8 +79,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ ref mut b = u(); | -----^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:57:9 @@ -88,8 +88,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ ref b = U; | ---------^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:61:9 @@ -97,8 +97,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ ref mut b = U; | -----^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:67:9 @@ -106,8 +106,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) => { | ---------^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:67:33 @@ -115,8 +115,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) => { | ---------^^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:76:9 @@ -124,8 +124,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) => { | -----^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:76:33 @@ -133,8 +133,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) => { | -----^^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:87:9 @@ -142,8 +142,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { *b = U; false } => {} | -----^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:87:33 @@ -151,8 +151,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { *b = U; false } => {} | -----^^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:94:9 @@ -160,8 +160,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { *a = Err(U); false } => {} | ---------^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:94:33 @@ -169,8 +169,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { *a = Err(U); false } => {} | ---------^^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:101:9 @@ -178,8 +178,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { drop(b); false } => {} | -----^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:101:33 @@ -187,8 +187,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | ref a @ Ok(ref mut b) | ref a @ Err(ref mut b) if { drop(b); false } => {} | -----^^^^^^^---------^ | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:109:9 @@ -196,8 +196,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { drop(a); false } => {} | ---------^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:109:33 @@ -205,8 +205,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | ref mut a @ Ok(ref b) | ref mut a @ Err(ref b) if { drop(a); false } => {} | ---------^^^^^^^-----^ | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:117:9 @@ -214,9 +214,9 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ (ref mut b, ref mut c) = (U, U); | -----^^^^---------^^---------^ | | | | - | | | mutable borrow, by `c`, occurs here - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | | value is mutably borrowed by `c` here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:123:9 @@ -224,9 +224,9 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ (ref mut b, ref mut c) = (U, U); | -----^^^^---------^^---------^ | | | | - | | | mutable borrow, by `c`, occurs here - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | | value is mutably borrowed by `c` here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:129:9 @@ -234,9 +234,9 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | let ref a @ (ref mut b, ref mut c) = (U, U); | -----^^^^---------^^---------^ | | | | - | | | mutable borrow, by `c`, occurs here - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | | value is mutably borrowed by `c` here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:134:9 @@ -244,9 +244,9 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | let ref mut a @ (ref b, ref c) = (U, U); | ---------^^^^-----^^-----^ | | | | - | | | immutable borrow, by `c`, occurs here - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | | value is borrowed by `c` here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:22:11 @@ -254,8 +254,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | fn f1(ref a @ ref mut b: U) {} | -----^^^--------- | | | - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:24:11 @@ -263,8 +263,8 @@ error: cannot borrow value as immutable because it is also borrowed as mutable LL | fn f2(ref mut a @ ref b: U) {} | ---------^^^----- | | | - | | immutable borrow, by `b`, occurs here - | mutable borrow, by `a`, occurs here + | | value is borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:26:11 @@ -272,8 +272,8 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | fn f3(ref a @ [ref b, ref mut mid @ .., ref c]: [U; 4]) {} | -----^^^^^^^^^^^----------------^^^^^^^^ | | | - | | mutable borrow, by `mid`, occurs here - | immutable borrow, by `a`, occurs here + | | value is mutably borrowed by `mid` here + | value is borrowed by `a` here error: cannot borrow value as mutable because it is also borrowed as immutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:28:22 @@ -281,9 +281,9 @@ error: cannot borrow value as mutable because it is also borrowed as immutable LL | fn f4_also_moved(ref a @ ref mut b @ c: U) {} | -----^^^------------- | | | | - | | | also moved into `c` here - | | mutable borrow, by `b`, occurs here - | immutable borrow, by `a`, occurs here + | | | value is moved into `c` here + | | value is mutably borrowed by `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-ref-mut-and-ref.rs:28:30 @@ -291,8 +291,8 @@ error: cannot move out of value because it is borrowed LL | fn f4_also_moved(ref a @ ref mut b @ c: U) {} | ---------^^^- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is mutably borrowed by `b` here error[E0502]: cannot borrow value as immutable because it is also borrowed as mutable --> $DIR/borrowck-pat-ref-mut-and-ref.rs:8:31 diff --git a/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-twice.stderr b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-twice.stderr index ad4ce7952ca74..a0cb04a064e06 100644 --- a/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-twice.stderr +++ b/tests/ui/pattern/bindings-after-at/borrowck-pat-ref-mut-twice.stderr @@ -4,8 +4,8 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ref mut b = U; | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:29:9 @@ -13,8 +13,8 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ref mut b = U; | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:33:9 @@ -22,8 +22,8 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ref mut b = U; | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:36:9 @@ -31,8 +31,8 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ref mut b = U; | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:39:9 @@ -40,8 +40,8 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ref mut b = U; | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:44:9 @@ -49,18 +49,18 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ( | ^-------- | | - | _________first mutable borrow, by `a`, occurs here + | _________value is mutably borrowed by `a` here | | LL | | LL | | ref mut b, - | | --------- another mutable borrow, by `b`, occurs here + | | --------- value is mutably borrowed by `b` here LL | | [ LL | | ref mut c, - | | --------- another mutable borrow, by `c`, occurs here + | | --------- value is mutably borrowed by `c` here LL | | ref mut d, - | | --------- another mutable borrow, by `d`, occurs here + | | --------- value is mutably borrowed by `d` here LL | | ref e, - | | ----- also borrowed as immutable, by `e`, here + | | ----- value is borrowed by `e` here LL | | ] LL | | ) = (U, [U, U, U]); | |_____^ @@ -71,18 +71,18 @@ error: cannot borrow value as mutable more than once at a time LL | let ref mut a @ ( | ^-------- | | - | _________first mutable borrow, by `a`, occurs here + | _________value is mutably borrowed by `a` here | | LL | | LL | | ref mut b, - | | --------- another mutable borrow, by `b`, occurs here + | | --------- value is mutably borrowed by `b` here LL | | [ LL | | ref mut c, - | | --------- another mutable borrow, by `c`, occurs here + | | --------- value is mutably borrowed by `c` here LL | | ref mut d, - | | --------- another mutable borrow, by `d`, occurs here + | | --------- value is mutably borrowed by `d` here LL | | ref e, - | | ----- also borrowed as immutable, by `e`, here + | | ----- value is borrowed by `e` here LL | | ] LL | | ) = (u(), [u(), u(), u()]); | |_________^ @@ -157,8 +157,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:76:37 @@ -166,8 +166,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:82:9 @@ -175,8 +175,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:82:37 @@ -184,8 +184,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:89:9 @@ -193,8 +193,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:89:37 @@ -202,8 +202,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:101:9 @@ -211,8 +211,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:101:37 @@ -220,8 +220,8 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ Ok(ref mut b) | ref mut a @ Err(ref mut b) => { | ---------^^^^^^^---------^ | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:8:11 @@ -229,8 +229,8 @@ error: cannot borrow value as mutable more than once at a time LL | fn f1(ref mut a @ ref mut b: U) {} | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:10:11 @@ -238,8 +238,8 @@ error: cannot borrow value as mutable more than once at a time LL | fn f2(ref mut a @ ref mut b: U) {} | ---------^^^--------- | | | - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:13:9 @@ -247,13 +247,13 @@ error: cannot borrow value as mutable more than once at a time LL | ref mut a @ [ | ^-------- | | - | _________first mutable borrow, by `a`, occurs here + | _________value is mutably borrowed by `a` here | | LL | | LL | | [ref b @ .., _], - | | ---------- also borrowed as immutable, by `b`, here + | | ---------- value is borrowed by `b` here LL | | [_, ref mut mid @ ..], - | | ---------------- another mutable borrow, by `mid`, occurs here + | | ---------------- value is mutably borrowed by `mid` here LL | | .., LL | | [..], LL | | ] : [[U; 4]; 5] @@ -265,9 +265,9 @@ error: cannot borrow value as mutable more than once at a time LL | fn f4_also_moved(ref mut a @ ref mut b @ c: U) {} | ---------^^^------------- | | | | - | | | also moved into `c` here - | | another mutable borrow, by `b`, occurs here - | first mutable borrow, by `a`, occurs here + | | | value is moved into `c` here + | | value is mutably borrowed by `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/borrowck-pat-ref-mut-twice.rs:21:34 @@ -275,8 +275,8 @@ error: cannot move out of value because it is borrowed LL | fn f4_also_moved(ref mut a @ ref mut b @ c: U) {} | ---------^^^- | | | - | | value moved into `c` here - | value borrowed, by `b`, here + | | value is moved into `c` here + | value is mutably borrowed by `b` here error[E0499]: cannot borrow value as mutable more than once at a time --> $DIR/borrowck-pat-ref-mut-twice.rs:29:9 diff --git a/tests/ui/pattern/bindings-after-at/default-binding-modes-both-sides-independent.stderr b/tests/ui/pattern/bindings-after-at/default-binding-modes-both-sides-independent.stderr index 638bdd6db7606..73ebbf48118da 100644 --- a/tests/ui/pattern/bindings-after-at/default-binding-modes-both-sides-independent.stderr +++ b/tests/ui/pattern/bindings-after-at/default-binding-modes-both-sides-independent.stderr @@ -4,8 +4,8 @@ error: cannot move out of value because it is borrowed LL | let ref a @ b = NotCopy; | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/default-binding-modes-both-sides-independent.rs:29:9 @@ -13,8 +13,8 @@ error: cannot move out of value because it is borrowed LL | let ref mut a @ b = NotCopy; | ---------^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is mutably borrowed by `a` here error: cannot move out of value because it is borrowed --> $DIR/default-binding-modes-both-sides-independent.rs:34:12 @@ -22,8 +22,8 @@ error: cannot move out of value because it is borrowed LL | Ok(ref a @ b) | Err(b @ ref a) => { | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error: borrow of moved value --> $DIR/default-binding-modes-both-sides-independent.rs:34:29 @@ -46,8 +46,8 @@ error: cannot move out of value because it is borrowed LL | ref a @ b => { | -----^^^- | | | - | | value moved into `b` here - | value borrowed, by `a`, here + | | value is moved into `b` here + | value is borrowed by `a` here error[E0382]: borrow of moved value --> $DIR/default-binding-modes-both-sides-independent.rs:29:9 diff --git a/tests/ui/suggestions/ref-pattern-binding.stderr b/tests/ui/suggestions/ref-pattern-binding.stderr index 10447ba7089ca..7b194259349b8 100644 --- a/tests/ui/suggestions/ref-pattern-binding.stderr +++ b/tests/ui/suggestions/ref-pattern-binding.stderr @@ -19,8 +19,8 @@ error: cannot move out of value because it is borrowed LL | let ref _moved @ _from = String::from("foo"); | ----------^^^----- | | | - | | value moved into `_from` here - | value borrowed, by `_moved`, here + | | value is moved into `_from` here + | value is borrowed by `_moved` here error: cannot move out of value because it is borrowed --> $DIR/ref-pattern-binding.rs:15:9 @@ -28,8 +28,8 @@ error: cannot move out of value because it is borrowed LL | let ref _moved @ S { f } = S { f: String::from("foo") }; | ----------^^^^^^^-^^ | | | - | | value moved into `f` here - | value borrowed, by `_moved`, here + | | value is moved into `f` here + | value is borrowed by `_moved` here error: borrow of moved value --> $DIR/ref-pattern-binding.rs:18:9 From 4e87f13054fa3cd9103b1b5ad9a2ed499fc542d5 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 19 Jan 2023 14:51:43 +0000 Subject: [PATCH 079/101] Fix a couple of TOCTOU occurences --- build_system/build_sysroot.rs | 6 ++---- build_system/path.rs | 6 +++--- build_system/prepare.rs | 9 ++------- build_system/utils.rs | 10 +++++++++- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index f52d34ffcd63f..bd04fdbe304a3 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -4,7 +4,7 @@ use std::process::{self, Command}; use super::path::{Dirs, RelPath}; use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name}; -use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler}; +use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler}; use super::SysrootKind; static DIST_DIR: RelPath = RelPath::DIST; @@ -230,9 +230,7 @@ fn build_clif_sysroot_for_triple( if !super::config::get_bool("keep_sysroot") { // Cleanup the deps dir, but keep build scripts and the incremental cache for faster // recompilation as they are not affected by changes in cg_clif. - if build_dir.join("deps").exists() { - fs::remove_dir_all(build_dir.join("deps")).unwrap(); - } + remove_dir_if_exists(&build_dir.join("deps")); } // Build sysroot diff --git a/build_system/path.rs b/build_system/path.rs index 35ab6f111fef4..3290723005dd9 100644 --- a/build_system/path.rs +++ b/build_system/path.rs @@ -1,6 +1,8 @@ use std::fs; use std::path::PathBuf; +use super::utils::remove_dir_if_exists; + #[derive(Debug, Clone)] pub(crate) struct Dirs { pub(crate) source_dir: PathBuf, @@ -61,9 +63,7 @@ impl RelPath { pub(crate) fn ensure_fresh(&self, dirs: &Dirs) { let path = self.to_path(dirs); - if path.exists() { - fs::remove_dir_all(&path).unwrap(); - } + remove_dir_if_exists(&path); fs::create_dir_all(path).unwrap(); } } diff --git a/build_system/prepare.rs b/build_system/prepare.rs index bc6c3223dc234..f25a81dc23459 100644 --- a/build_system/prepare.rs +++ b/build_system/prepare.rs @@ -3,18 +3,13 @@ use std::fs; use std::path::{Path, PathBuf}; use std::process::Command; -use crate::build_system::rustc_info::get_default_sysroot; - use super::build_sysroot::{BUILD_SYSROOT, ORIG_BUILD_SYSROOT, SYSROOT_RUSTC_VERSION, SYSROOT_SRC}; use super::path::{Dirs, RelPath}; -use super::rustc_info::get_rustc_version; +use super::rustc_info::{get_default_sysroot, get_rustc_version}; use super::utils::{copy_dir_recursively, git_command, retry_spawn_and_wait, spawn_and_wait}; pub(crate) fn prepare(dirs: &Dirs) { - if RelPath::DOWNLOAD.to_path(dirs).exists() { - std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); - } - std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap(); + RelPath::DOWNLOAD.ensure_fresh(dirs); spawn_and_wait(super::build_backend::CG_CLIF.fetch("cargo", dirs)); diff --git a/build_system/utils.rs b/build_system/utils.rs index 21bfb1b1f00f5..da2a94a0a4ff8 100644 --- a/build_system/utils.rs +++ b/build_system/utils.rs @@ -1,6 +1,6 @@ use std::env; use std::fs; -use std::io::Write; +use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::process::{self, Command, Stdio}; @@ -246,6 +246,14 @@ pub(crate) fn spawn_and_wait_with_input(mut cmd: Command, input: String) -> Stri String::from_utf8(output.stdout).unwrap() } +pub(crate) fn remove_dir_if_exists(path: &Path) { + match fs::remove_dir_all(&path) { + Ok(()) => {} + Err(err) if err.kind() == io::ErrorKind::NotFound => {} + Err(err) => panic!("Failed to remove {path}: {err}", path = path.display()), + } +} + pub(crate) fn copy_dir_recursively(from: &Path, to: &Path) { for entry in fs::read_dir(from).unwrap() { let entry = entry.unwrap(); From 7d14e606bef1732c1f42f8598b0ca9d435cfdcaf Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:40:54 +0100 Subject: [PATCH 080/101] Rustup to rustc 1.68.0-nightly (4c83bd03a 2023-01-19) --- build_sysroot/Cargo.lock | 12 ++++++------ .../0022-sysroot-Disable-not-compiling-tests.patch | 5 +++-- rust-toolchain | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index bba3210536ef7..24f15fc8521fe 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -34,9 +34,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.0.77" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" +checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" [[package]] name = "cfg-if" @@ -50,9 +50,9 @@ dependencies = [ [[package]] name = "compiler_builtins" -version = "0.1.85" +version = "0.1.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e81c6cd7ab79f51a0c927d22858d61ad12bd0b3865f0b13ece02a4486aeabb" +checksum = "5dae98c88e576098d7ab13ebcb40cc43e5114b2beafe61a87cda9200649ff205" dependencies = [ "rustc-std-workspace-core", ] @@ -129,9 +129,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.138" +version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db6d7e329c562c5dfab7a46a2afabc8b987ab9a4834c9d1ca04dc54c1546cef8" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" dependencies = [ "rustc-std-workspace-core", ] diff --git a/patches/0022-sysroot-Disable-not-compiling-tests.patch b/patches/0022-sysroot-Disable-not-compiling-tests.patch index 8d9ee3f25c49d..865aa833a5eef 100644 --- a/patches/0022-sysroot-Disable-not-compiling-tests.patch +++ b/patches/0022-sysroot-Disable-not-compiling-tests.patch @@ -18,7 +18,7 @@ new file mode 100644 index 0000000..46fd999 --- /dev/null +++ b/library/core/tests/Cargo.toml -@@ -0,0 +1,11 @@ +@@ -0,0 +1,12 @@ +[package] +name = "core" +version = "0.0.0" @@ -29,6 +29,7 @@ index 0000000..46fd999 +path = "lib.rs" + +[dependencies] -+rand = "0.7" ++rand = { version = "0.8.5", default-features = false } ++rand_xorshift = { version = "0.3.0", default-features = false } -- 2.21.0 (Apple Git-122) diff --git a/rust-toolchain b/rust-toolchain index d8f28dbcc15c8..77345b9a17c6e 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-12-13" +channel = "nightly-2023-01-20" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] From 92b4c76652bb79dd767a1f117ecbd88b85dcd807 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:41:27 +0100 Subject: [PATCH 081/101] Update rustup.sh for the moved dir of the sysroot source --- scripts/rustup.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/rustup.sh b/scripts/rustup.sh index 6111c20544463..34e3981b5381f 100755 --- a/scripts/rustup.sh +++ b/scripts/rustup.sh @@ -18,9 +18,9 @@ case $1 in ./clean_all.sh - (cd build_sysroot && cargo update) - ./y.rs prepare + + (cd download/sysroot && cargo update && cargo fetch && cp Cargo.lock ../../build_sysroot/) ;; "commit") git add rust-toolchain build_sysroot/Cargo.lock From fb6d048c4d3f266d7e6b0664dd1365acb34e36e3 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 12:49:27 +0100 Subject: [PATCH 082/101] Update patch --- scripts/setup_rust_fork.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index ce0d7e9fe07da..a08e80dd19abc 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -25,8 +25,8 @@ index d95b5b7f17f..00b6f0e3635 100644 +compiler_builtins = { version = "0.1.66", features = ['rustc-dep-of-std', 'no-asm'] } [dev-dependencies] - rand = "0.7" - rand_xorshift = "0.2" + rand = { version = "0.8.5", default-features = false, features = ["alloc"] } + rand_xorshift = "0.3.0" EOF cat > config.toml < Date: Fri, 20 Jan 2023 13:08:45 +0100 Subject: [PATCH 083/101] Fix rustc test suite --- scripts/test_rustc_tests.sh | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 81e287f1ddebf..07c9ae6ee9ff2 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -78,6 +78,20 @@ rm tests/ui/mir/mir_raw_fat_ptr.rs # same rm tests/ui/consts/issue-33537.rs # same rm tests/ui/layout/valid_range_oob.rs # different ICE message +rm tests/ui/consts/issue-miri-1910.rs # different error message +rm tests/ui/consts/offset_ub.rs # same +rm tests/ui/intrinsics/panic-uninitialized-zeroed.rs # same +rm tests/ui/lint/lint-const-item-mutation.rs # same +rm tests/ui/pattern/usefulness/doc-hidden-non-exhaustive.rs # same +rm tests/ui/suggestions/derive-trait-for-method-call.rs # same +rm tests/ui/typeck/issue-46112.rs # same + +rm tests/ui/proc-macro/crt-static.rs # extra warning about -Cpanic=abort for proc macros +rm tests/ui/proc-macro/proc-macro-deprecated-attr.rs # same +rm tests/ui/proc-macro/quote-debug.rs # same +rm tests/ui/proc-macro/no-missing-docs.rs # same +rm tests/ui/rust-2018/proc-macro-crate-in-paths.rs # same + # doesn't work due to the way the rustc test suite is invoked. # should work when using ./x.py test the way it is intended # ============================================================ @@ -98,12 +112,14 @@ rm tests/ui/simd/intrinsic/generic-reduction-pass.rs # simd_reduce_add_unordered rm tests/ui/simd/intrinsic/generic-as.rs # crash when accessing vector type filed (#1318) rm tests/ui/simd/simd-bitmask.rs # crash +rm tests/ui/dyn-star/dyn-star-to-dyn.rs +rm tests/ui/dyn-star/dispatch-on-pin-mut.rs + # bugs in the test suite # ====================== rm tests/ui/backtrace.rs # TODO warning rm tests/ui/simple_global_asm.rs # TODO add needs-asm-support rm tests/ui/process/nofile-limit.rs # TODO some AArch64 linking issue -rm tests/ui/dyn-star/dispatch-on-pin-mut.rs # TODO failed assertion in vtable::get_ptr_and_method_ref rm tests/ui/stdio-is-blocking.rs # really slow with unoptimized libstd From c6ad186298f6e556607df32c7e02e91435fb84e7 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:27:39 +0000 Subject: [PATCH 084/101] Use panic_nounwind and panic_cannot_unwind where necessary These were either regular unwinding panics or aborts in the past but got changed somewhat recently. --- src/base.rs | 34 +++++++++++++++++++++++++++------- src/intrinsics/mod.rs | 6 +++--- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/base.rs b/src/base.rs index 0df3ffc4bd890..53fbab9fe2931 100644 --- a/src/base.rs +++ b/src/base.rs @@ -466,7 +466,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { *destination, ); } - TerminatorKind::Resume | TerminatorKind::Abort => { + TerminatorKind::Abort => { + codegen_panic_cannot_unwind(fx, source_info); + } + TerminatorKind::Resume => { // FIXME implement unwinding fx.bcx.ins().trap(TrapCode::UnreachableCodeReached); } @@ -931,7 +934,28 @@ pub(crate) fn codegen_panic<'tcx>( codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, source_info.span); } -pub(crate) fn codegen_panic_inner<'tcx>( +pub(crate) fn codegen_panic_nounwind<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + msg_str: &str, + source_info: mir::SourceInfo, +) { + let msg_ptr = fx.anonymous_str(msg_str); + let msg_len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(msg_str.len()).unwrap()); + let args = [msg_ptr, msg_len]; + + codegen_panic_inner(fx, rustc_hir::LangItem::PanicNounwind, &args, source_info.span); +} + +pub(crate) fn codegen_panic_cannot_unwind<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + source_info: mir::SourceInfo, +) { + let args = []; + + codegen_panic_inner(fx, rustc_hir::LangItem::PanicCannotUnwind, &args, source_info.span); +} + +fn codegen_panic_inner<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, lang_item: rustc_hir::LangItem, args: &[Value], @@ -948,11 +972,7 @@ pub(crate) fn codegen_panic_inner<'tcx>( fx.lib_call( &*symbol_name, - vec![ - AbiParam::new(fx.pointer_type), - AbiParam::new(fx.pointer_type), - AbiParam::new(fx.pointer_type), - ], + args.iter().map(|&arg| AbiParam::new(fx.bcx.func.dfg.value_type(arg))).collect(), vec![], args, ); diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 9cce8e9b9cdc0..52720daac6ffc 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -649,7 +649,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let layout = fx.layout_of(substs.type_at(0)); if layout.abi.is_uninhabited() { with_no_trimmed_paths!({ - crate::base::codegen_panic( + crate::base::codegen_panic_nounwind( fx, &format!("attempted to instantiate uninhabited type `{}`", layout.ty), source_info, @@ -660,7 +660,7 @@ fn codegen_regular_intrinsic_call<'tcx>( if intrinsic == sym::assert_zero_valid && !fx.tcx.permits_zero_init(layout) { with_no_trimmed_paths!({ - crate::base::codegen_panic( + crate::base::codegen_panic_nounwind( fx, &format!( "attempted to zero-initialize type `{}`, which is invalid", @@ -676,7 +676,7 @@ fn codegen_regular_intrinsic_call<'tcx>( && !fx.tcx.permits_uninit_init(layout) { with_no_trimmed_paths!({ - crate::base::codegen_panic( + crate::base::codegen_panic_nounwind( fx, &format!( "attempted to leave type `{}` uninitialized, which is invalid", From 219cdbaac02480b661e4632115c97496d1d4a3ba Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:40:53 +0000 Subject: [PATCH 085/101] Fix clif ir writing for simd_gather --- src/base.rs | 3 +++ src/intrinsics/simd.rs | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/base.rs b/src/base.rs index 53fbab9fe2931..d3a8c10657e8d 100644 --- a/src/base.rs +++ b/src/base.rs @@ -305,6 +305,9 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) { let source_info = bb_data.terminator().source_info; fx.set_debug_loc(source_info); + let _print_guard = + crate::PrintOnPanic(|| format!("terminator {:?}", bb_data.terminator().kind)); + match &bb_data.terminator().kind { TerminatorKind::Goto { target } => { if let TerminatorKind::Return = fx.mir[*target].terminator().kind { diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index 791ff5cfcf3c6..b33eb29754ab7 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -840,6 +840,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( fx.bcx.seal_block(next); fx.bcx.switch_to_block(next); + fx.bcx.ins().nop(); + ret.place_lane(fx, lane_idx) .write_cvalue(fx, CValue::by_val(res_lane, ret_lane_layout)); } From 6eef214e46454dd508821759092253c86d269204 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 20 Jan 2023 17:49:59 +0000 Subject: [PATCH 086/101] Separate out abi-cafe runs into separate CI jobs This increases build parallelism --- .github/workflows/main.yml | 67 ++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 10 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 90004b408c014..c0daf69e98e91 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -50,11 +50,9 @@ jobs: - os: ubuntu-latest env: TARGET_TRIPLE: s390x-unknown-linux-gnu - # Native Windows build with MSVC - os: windows-latest env: TARGET_TRIPLE: x86_64-pc-windows-msvc - # cross-compile from Windows to Windows MinGW - os: windows-latest env: TARGET_TRIPLE: x86_64-pc-windows-gnu @@ -114,14 +112,6 @@ jobs: TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} run: ./y.rs test - - name: Test abi-cafe - env: - TARGET_TRIPLE: ${{ matrix.env.TARGET_TRIPLE }} - run: | - if [[ "$(rustc -vV | grep host | cut -d' ' -f2)" == "$TARGET_TRIPLE" ]]; then - ./y.rs abi-cafe - fi - - name: Package prebuilt cg_clif run: tar cvfJ cg_clif.tar.xz dist @@ -138,3 +128,60 @@ jobs: with: name: cg_clif-${{ runner.os }}-cross-x86_64-mingw path: cg_clif.tar.xz + + + abi_cafe: + runs-on: ${{ matrix.os }} + timeout-minutes: 60 + + defaults: + run: + shell: bash + + strategy: + fail-fast: true + matrix: + include: + - os: ubuntu-latest + env: + TARGET_TRIPLE: x86_64-unknown-linux-gnu + - os: macos-latest + env: + TARGET_TRIPLE: x86_64-apple-darwin + - os: windows-latest + env: + TARGET_TRIPLE: x86_64-pc-windows-msvc + - os: windows-latest + env: + TARGET_TRIPLE: x86_64-pc-windows-gnu + + steps: + - uses: actions/checkout@v3 + + - name: Cache cargo target dir + uses: actions/cache@v3 + with: + path: build/cg_clif + key: ${{ runner.os }}-${{ matrix.env.TARGET_TRIPLE }}-cargo-build-target-${{ hashFiles('rust-toolchain', '**/Cargo.lock') }} + + - name: Set MinGW as the default toolchain + if: matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu' + run: rustup set default-host x86_64-pc-windows-gnu + + - name: Use sparse cargo registry + run: | + cat >> ~/.cargo/config.toml < Date: Sat, 7 Jan 2023 17:06:13 +0000 Subject: [PATCH 087/101] Update to Cranelift 0.92 --- Cargo.lock | 64 ++++++++++++++++------------------------ Cargo.toml | 12 ++++---- src/optimize/peephole.rs | 4 +-- 3 files changed, 33 insertions(+), 47 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3bc2bf31d666d..48800725d2c13 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -57,28 +57,28 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc952b310b24444fc14ab8b9cbe3fafd7e7329e3eec84c3a9b11d2b5cf6f3be1" +checksum = "2f3d54eab028f5805ae3b26fd60eca3f3a9cfb76b989d9bab173be3f61356cc3" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e73470419b33011e50dbf0f6439cbccbaabe9381de172da4e1b6efcda4bb8fa7" +checksum = "2be1d5f2c3cca1efb691844bc1988b89c77291f13f778499a3f3c0cf49c0ed61" dependencies = [ "arrayvec", "bumpalo", "cranelift-bforest", "cranelift-codegen-meta", "cranelift-codegen-shared", - "cranelift-egraph", "cranelift-entity", "cranelift-isle", "gimli", + "hashbrown", "log", "regalloc2", "smallvec", @@ -87,44 +87,30 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "911a1872464108a11ac9965c2b079e61bbdf1bc2e0b9001264264add2e12a38f" +checksum = "3f9b1b1089750ce4005893af7ee00bb08a2cf1c9779999c0f7164cbc8ad2e0d2" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.91.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e036f3f07adb24a86fb46e977e8fe03b18bb16b1eada949cf2c48283e5f8a862" - -[[package]] -name = "cranelift-egraph" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d6c623f4b5d2a6bad32c403f03765d4484a827eb93ee78f8cb6219ef118fd59" -dependencies = [ - "cranelift-entity", - "fxhash", - "hashbrown", - "indexmap", - "log", - "smallvec", -] +checksum = "cc5fbaec51de47297fd7304986fd53c8c0030abbe69728a60d72e1c63559318d" [[package]] name = "cranelift-entity" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74385eb5e405b3562f0caa7bcc4ab9a93c7958dd5bcd0e910bffb7765eacd6fc" +checksum = "dab984c94593f876090fae92e984bdcc74d9b1acf740ab5f79036001c65cba13" [[package]] name = "cranelift-frontend" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a4ac920422ee36bff2c66257fec861765e3d95a125cdf58d8c0f3bba7e40e61" +checksum = "6e0cb3102d21a2fe5f3210af608748ddd0cd09825ac12d42dc56ed5ed8725fe0" dependencies = [ "cranelift-codegen", "log", @@ -134,15 +120,15 @@ dependencies = [ [[package]] name = "cranelift-isle" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c541263fb37ad2baa53ec8c37218ee5d02fa0984670d9419dedd8002ea68ff08" +checksum = "72101dd1f441d629735143c41e00b3428f9267738176983ef588ff43382af0a0" [[package]] name = "cranelift-jit" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48a844e3500d313b69f3eec4b4e15bf9cdbd529756add06a468e0e281c0f6bee" +checksum = "6557f8ce44d498777f2495aa58d9692a4a37d6f84aa445750d666cef770b6a5c" dependencies = [ "anyhow", "cranelift-codegen", @@ -159,9 +145,9 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0699ea5fc6ca943456ba80ad49f80212bd6e2b846b992ec59f0f2b912a1d25fa" +checksum = "88807e1c0c47ec02fe433333ccbe56b480425418b1470e333205e11650697d72" dependencies = [ "anyhow", "cranelift-codegen", @@ -169,9 +155,9 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1de5d7a063e8563d670aaca38de16591a9b70dc66cbad4d49a7b4ae8395fd1ce" +checksum = "c22b0d9fcbe3fc5a1af9e7021b44ce42b930bcefac446ce22e02e8f9a0d67120" dependencies = [ "cranelift-codegen", "libc", @@ -180,9 +166,9 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.91.0" +version = "0.92.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307735148f6a556388aabf1ea31f46ccd378ed0739f3e9bdda2029639d701ab7" +checksum = "341375758d7c3fedc0b5315f552e6f0feac46baf87c450a15e9455ef47c2b261" dependencies = [ "anyhow", "cranelift-codegen", @@ -396,9 +382,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasmtime-jit-icache-coherence" -version = "3.0.0" +version = "5.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22d9c2e92b0fc124d2cad6cb497a4c840580a7dd2414a37109e8c7cfe699c0ea" +checksum = "08fcba5ebd96da2a9f0747ab6337fe9788adfb3f63fa2c180520d665562d257e" dependencies = [ "cfg-if", "libc", diff --git a/Cargo.toml b/Cargo.toml index f03cc34a81081..eadb4438bc992 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,12 +15,12 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { version = "0.91", features = ["unwind", "all-arch"] } -cranelift-frontend = "0.91" -cranelift-module = "0.91" -cranelift-native = "0.91" -cranelift-jit = { version = "0.91", optional = true } -cranelift-object = "0.91" +cranelift-codegen = { version = "0.92", features = ["unwind", "all-arch"] } +cranelift-frontend = { version = "0.92" } +cranelift-module = { version = "0.92" } +cranelift-native = { version = "0.92" } +cranelift-jit = { version = "0.92", optional = true } +cranelift-object = { version = "0.92" } target-lexicon = "0.12.0" gimli = { version = "0.26.0", default-features = false, features = ["write"]} object = { version = "0.29.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] } diff --git a/src/optimize/peephole.rs b/src/optimize/peephole.rs index 7f45bbd8f2813..26327dca299b9 100644 --- a/src/optimize/peephole.rs +++ b/src/optimize/peephole.rs @@ -7,7 +7,7 @@ use cranelift_frontend::FunctionBuilder; /// otherwise return the given value and false. pub(crate) fn maybe_unwrap_bool_not(bcx: &mut FunctionBuilder<'_>, arg: Value) -> (Value, bool) { if let ValueDef::Result(arg_inst, 0) = bcx.func.dfg.value_def(arg) { - match bcx.func.dfg[arg_inst] { + match bcx.func.dfg.insts[arg_inst] { // This is the lowering of `Rvalue::Not` InstructionData::IntCompareImm { opcode: Opcode::IcmpImm, @@ -34,7 +34,7 @@ pub(crate) fn maybe_known_branch_taken( return None; }; - match bcx.func.dfg[arg_inst] { + match bcx.func.dfg.insts[arg_inst] { InstructionData::UnaryImm { opcode: Opcode::Iconst, imm } => { if test_zero { Some(imm.bits() == 0) From afd530793454a42951dcc0fe50b1cd66f397c438 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:00:41 +0100 Subject: [PATCH 088/101] Move `ty::tls` to seperate file --- compiler/rustc_middle/src/ty/context.rs | 174 +------------------- compiler/rustc_middle/src/ty/context/tls.rs | 169 +++++++++++++++++++ 2 files changed, 171 insertions(+), 172 deletions(-) create mode 100644 compiler/rustc_middle/src/ty/context/tls.rs diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index ce04d8d21f4cd..ed23297dba78c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2,6 +2,8 @@ #![allow(rustc::usage_of_ty_tykind)] +pub mod tls; + use crate::arena::Arena; use crate::dep_graph::{DepGraph, DepKindStruct}; use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos}; @@ -1188,178 +1190,6 @@ CloneLiftImpls! { for<'tcx> { Constness, traits::WellFormedLoc, ImplPolarity, crate::mir::ReturnConstraint, } } -pub mod tls { - use super::{ptr_eq, GlobalCtxt, TyCtxt}; - - use crate::dep_graph::TaskDepsRef; - use crate::ty::query; - use rustc_data_structures::sync::{self, Lock}; - use rustc_errors::Diagnostic; - use std::mem; - use thin_vec::ThinVec; - - #[cfg(not(parallel_compiler))] - use std::cell::Cell; - - #[cfg(parallel_compiler)] - use rustc_rayon_core as rayon_core; - - /// This is the implicit state of rustc. It contains the current - /// `TyCtxt` and query. It is updated when creating a local interner or - /// executing a new query. Whenever there's a `TyCtxt` value available - /// you should also have access to an `ImplicitCtxt` through the functions - /// in this module. - #[derive(Clone)] - pub struct ImplicitCtxt<'a, 'tcx> { - /// The current `TyCtxt`. - pub tcx: TyCtxt<'tcx>, - - /// The current query job, if any. This is updated by `JobOwner::start` in - /// `ty::query::plumbing` when executing a query. - pub query: Option, - - /// Where to store diagnostics for the current query job, if any. - /// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query. - pub diagnostics: Option<&'a Lock>>, - - /// Used to prevent queries from calling too deeply. - pub query_depth: usize, - - /// The current dep graph task. This is used to add dependencies to queries - /// when executing them. - pub task_deps: TaskDepsRef<'a>, - } - - impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { - pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self { - let tcx = TyCtxt { gcx }; - ImplicitCtxt { - tcx, - query: None, - diagnostics: None, - query_depth: 0, - task_deps: TaskDepsRef::Ignore, - } - } - } - - /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs - /// to `value` during the call to `f`. It is restored to its previous value after. - /// This is used to set the pointer to the new `ImplicitCtxt`. - #[cfg(parallel_compiler)] - #[inline] - fn set_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) - } - - /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. - /// This is used to get the pointer to the current `ImplicitCtxt`. - #[cfg(parallel_compiler)] - #[inline] - pub fn get_tlv() -> usize { - rayon_core::tlv::get() - } - - #[cfg(not(parallel_compiler))] - thread_local! { - /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; - } - - /// Sets TLV to `value` during the call to `f`. - /// It is restored to its previous value after. - /// This is used to set the pointer to the new `ImplicitCtxt`. - #[cfg(not(parallel_compiler))] - #[inline] - fn set_tlv R, R>(value: usize, f: F) -> R { - let old = get_tlv(); - let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); - TLV.with(|tlv| tlv.set(value)); - f() - } - - /// Gets the pointer to the current `ImplicitCtxt`. - #[cfg(not(parallel_compiler))] - #[inline] - fn get_tlv() -> usize { - TLV.with(|tlv| tlv.get()) - } - - /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. - #[inline] - pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R - where - F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, - { - set_tlv(context as *const _ as usize, || f(&context)) - } - - /// Allows access to the current `ImplicitCtxt` in a closure if one is available. - #[inline] - pub fn with_context_opt(f: F) -> R - where - F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, - { - let context = get_tlv(); - if context == 0 { - f(None) - } else { - // We could get an `ImplicitCtxt` pointer from another thread. - // Ensure that `ImplicitCtxt` is `Sync`. - sync::assert_sync::>(); - - unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } - } - } - - /// Allows access to the current `ImplicitCtxt`. - /// Panics if there is no `ImplicitCtxt` available. - #[inline] - pub fn with_context(f: F) -> R - where - F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, - { - with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls"))) - } - - /// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument - /// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime - /// as the `TyCtxt` passed in. - /// This will panic if you pass it a `TyCtxt` which is different from the current - /// `ImplicitCtxt`'s `tcx` field. - #[inline] - pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R - where - F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, - { - with_context(|context| unsafe { - assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); - let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); - f(context) - }) - } - - /// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. - /// Panics if there is no `ImplicitCtxt` available. - #[inline] - pub fn with(f: F) -> R - where - F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R, - { - with_context(|context| f(context.tcx)) - } - - /// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. - /// The closure is passed None if there is no `ImplicitCtxt` available. - #[inline] - pub fn with_opt(f: F) -> R - where - F: for<'tcx> FnOnce(Option>) -> R, - { - with_context_opt(|opt_context| f(opt_context.map(|context| context.tcx))) - } -} - macro_rules! sty_debug_print { ($fmt: expr, $ctxt: expr, $($variant: ident),*) => {{ // Curious inner module to allow variant names to be used as diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs new file mode 100644 index 0000000000000..0737131f1794e --- /dev/null +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -0,0 +1,169 @@ +use super::{ptr_eq, GlobalCtxt, TyCtxt}; + +use crate::dep_graph::TaskDepsRef; +use crate::ty::query; +use rustc_data_structures::sync::{self, Lock}; +use rustc_errors::Diagnostic; +use std::mem; +use thin_vec::ThinVec; + +#[cfg(not(parallel_compiler))] +use std::cell::Cell; + +#[cfg(parallel_compiler)] +use rustc_rayon_core as rayon_core; + +/// This is the implicit state of rustc. It contains the current +/// `TyCtxt` and query. It is updated when creating a local interner or +/// executing a new query. Whenever there's a `TyCtxt` value available +/// you should also have access to an `ImplicitCtxt` through the functions +/// in this module. +#[derive(Clone)] +pub struct ImplicitCtxt<'a, 'tcx> { + /// The current `TyCtxt`. + pub tcx: TyCtxt<'tcx>, + + /// The current query job, if any. This is updated by `JobOwner::start` in + /// `ty::query::plumbing` when executing a query. + pub query: Option, + + /// Where to store diagnostics for the current query job, if any. + /// This is updated by `JobOwner::start` in `ty::query::plumbing` when executing a query. + pub diagnostics: Option<&'a Lock>>, + + /// Used to prevent queries from calling too deeply. + pub query_depth: usize, + + /// The current dep graph task. This is used to add dependencies to queries + /// when executing them. + pub task_deps: TaskDepsRef<'a>, +} + +impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { + pub fn new(gcx: &'tcx GlobalCtxt<'tcx>) -> Self { + let tcx = TyCtxt { gcx }; + ImplicitCtxt { + tcx, + query: None, + diagnostics: None, + query_depth: 0, + task_deps: TaskDepsRef::Ignore, + } + } +} + +/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs +/// to `value` during the call to `f`. It is restored to its previous value after. +/// This is used to set the pointer to the new `ImplicitCtxt`. +#[cfg(parallel_compiler)] +#[inline] +fn set_tlv R, R>(value: usize, f: F) -> R { + rayon_core::tlv::with(value, f) +} + +/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. +/// This is used to get the pointer to the current `ImplicitCtxt`. +#[cfg(parallel_compiler)] +#[inline] +pub fn get_tlv() -> usize { + rayon_core::tlv::get() +} + +#[cfg(not(parallel_compiler))] +thread_local! { + /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. + static TLV: Cell = const { Cell::new(0) }; +} + +/// Sets TLV to `value` during the call to `f`. +/// It is restored to its previous value after. +/// This is used to set the pointer to the new `ImplicitCtxt`. +#[cfg(not(parallel_compiler))] +#[inline] +fn set_tlv R, R>(value: usize, f: F) -> R { + let old = get_tlv(); + let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); + TLV.with(|tlv| tlv.set(value)); + f() +} + +/// Gets the pointer to the current `ImplicitCtxt`. +#[cfg(not(parallel_compiler))] +#[inline] +fn get_tlv() -> usize { + TLV.with(|tlv| tlv.get()) +} + +/// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. +#[inline] +pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R +where + F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, +{ + set_tlv(context as *const _ as usize, || f(&context)) +} + +/// Allows access to the current `ImplicitCtxt` in a closure if one is available. +#[inline] +pub fn with_context_opt(f: F) -> R +where + F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, +{ + let context = get_tlv(); + if context == 0 { + f(None) + } else { + // We could get an `ImplicitCtxt` pointer from another thread. + // Ensure that `ImplicitCtxt` is `Sync`. + sync::assert_sync::>(); + + unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } + } +} + +/// Allows access to the current `ImplicitCtxt`. +/// Panics if there is no `ImplicitCtxt` available. +#[inline] +pub fn with_context(f: F) -> R +where + F: for<'a, 'tcx> FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, +{ + with_context_opt(|opt_context| f(opt_context.expect("no ImplicitCtxt stored in tls"))) +} + +/// Allows access to the current `ImplicitCtxt` whose tcx field is the same as the tcx argument +/// passed in. This means the closure is given an `ImplicitCtxt` with the same `'tcx` lifetime +/// as the `TyCtxt` passed in. +/// This will panic if you pass it a `TyCtxt` which is different from the current +/// `ImplicitCtxt`'s `tcx` field. +#[inline] +pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R +where + F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, +{ + with_context(|context| unsafe { + assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); + let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); + f(context) + }) +} + +/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. +/// Panics if there is no `ImplicitCtxt` available. +#[inline] +pub fn with(f: F) -> R +where + F: for<'tcx> FnOnce(TyCtxt<'tcx>) -> R, +{ + with_context(|context| f(context.tcx)) +} + +/// Allows access to the `TyCtxt` in the current `ImplicitCtxt`. +/// The closure is passed None if there is no `ImplicitCtxt` available. +#[inline] +pub fn with_opt(f: F) -> R +where + F: for<'tcx> FnOnce(Option>) -> R, +{ + with_context_opt(|opt_context| f(opt_context.map(|context| context.tcx))) +} From 0d11b77005c9304e45db7b1321d8d4b324366e2f Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:14:29 +0100 Subject: [PATCH 089/101] Some ty::tls cleanups Putting the cfged functions into a seperate module and giving them better names. --- compiler/rustc_middle/src/ty/context.rs | 6 -- compiler/rustc_middle/src/ty/context/tls.rs | 98 +++++++++++---------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index ed23297dba78c..bae2c863a813d 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -2222,12 +2222,6 @@ pub struct DeducedParamAttrs { pub read_only: bool, } -// We are comparing types with different invariant lifetimes, so `ptr::eq` -// won't work for us. -fn ptr_eq(t: *const T, u: *const U) -> bool { - t as *const () == u as *const () -} - pub fn provide(providers: &mut ty::query::Providers) { providers.module_reexports = |tcx, id| tcx.resolutions(()).reexport_map.get(&id).map(|v| &v[..]); diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs index 0737131f1794e..f1fe47f6ba6ed 100644 --- a/compiler/rustc_middle/src/ty/context/tls.rs +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -1,18 +1,13 @@ -use super::{ptr_eq, GlobalCtxt, TyCtxt}; +use super::{GlobalCtxt, TyCtxt}; use crate::dep_graph::TaskDepsRef; use crate::ty::query; use rustc_data_structures::sync::{self, Lock}; use rustc_errors::Diagnostic; use std::mem; +use std::ptr; use thin_vec::ThinVec; -#[cfg(not(parallel_compiler))] -use std::cell::Cell; - -#[cfg(parallel_compiler)] -use rustc_rayon_core as rayon_core; - /// This is the implicit state of rustc. It contains the current /// `TyCtxt` and query. It is updated when creating a local interner or /// executing a new query. Whenever there's a `TyCtxt` value available @@ -52,46 +47,53 @@ impl<'a, 'tcx> ImplicitCtxt<'a, 'tcx> { } } -/// Sets Rayon's thread-local variable, which is preserved for Rayon jobs -/// to `value` during the call to `f`. It is restored to its previous value after. -/// This is used to set the pointer to the new `ImplicitCtxt`. #[cfg(parallel_compiler)] -#[inline] -fn set_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) -} +mod tlv { + use rustc_rayon_core as rayon_core; + use std::ptr; + + /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. + /// This is used to get the pointer to the current `ImplicitCtxt`. + #[inline] + pub(super) fn get_tlv() -> usize { + rayon_core::tlv::get() + } -/// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. -/// This is used to get the pointer to the current `ImplicitCtxt`. -#[cfg(parallel_compiler)] -#[inline] -pub fn get_tlv() -> usize { - rayon_core::tlv::get() + /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs + /// to `value` during the call to `f`. It is restored to its previous value after. + /// This is used to set the pointer to the new `ImplicitCtxt`. + #[inline] + pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + rayon_core::tlv::with(value, f) + } } #[cfg(not(parallel_compiler))] -thread_local! { - /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; -} +mod tlv { + use std::cell::Cell; + use std::ptr; -/// Sets TLV to `value` during the call to `f`. -/// It is restored to its previous value after. -/// This is used to set the pointer to the new `ImplicitCtxt`. -#[cfg(not(parallel_compiler))] -#[inline] -fn set_tlv R, R>(value: usize, f: F) -> R { - let old = get_tlv(); - let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); - TLV.with(|tlv| tlv.set(value)); - f() -} + thread_local! { + /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. + static TLV: Cell = const { Cell::new(0) }; + } -/// Gets the pointer to the current `ImplicitCtxt`. -#[cfg(not(parallel_compiler))] -#[inline] -fn get_tlv() -> usize { - TLV.with(|tlv| tlv.get()) + /// Gets the pointer to the current `ImplicitCtxt`. + #[inline] + pub(super) fn get_tlv() -> usize { + TLV.with(|tlv| tlv.get()) + } + + /// Sets TLV to `value` during the call to `f`. + /// It is restored to its previous value after. + /// This is used to set the pointer to the new `ImplicitCtxt`. + #[inline] + pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + let old = get_tlv(); + let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); + TLV.with(|tlv| tlv.set(value)); + f() + } } /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. @@ -100,7 +102,7 @@ pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> where F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, { - set_tlv(context as *const _ as usize, || f(&context)) + tlv::with_tlv(context as *const _ as usize, || f(&context)) } /// Allows access to the current `ImplicitCtxt` in a closure if one is available. @@ -109,7 +111,7 @@ pub fn with_context_opt(f: F) -> R where F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, { - let context = get_tlv(); + let context = tlv::get_tlv(); if context == 0 { f(None) } else { @@ -141,9 +143,15 @@ pub fn with_related_context<'tcx, F, R>(tcx: TyCtxt<'tcx>, f: F) -> R where F: FnOnce(&ImplicitCtxt<'_, 'tcx>) -> R, { - with_context(|context| unsafe { - assert!(ptr_eq(context.tcx.gcx, tcx.gcx)); - let context: &ImplicitCtxt<'_, '_> = mem::transmute(context); + with_context(|context| { + // The two gcx have different invariant lifetimes, so we need to erase them for the comparison. + assert!(ptr::eq( + context.tcx.gcx as *const _ as *const (), + tcx.gcx as *const _ as *const () + )); + + let context: &ImplicitCtxt<'_, '_> = unsafe { mem::transmute(context) }; + f(context) }) } From db305d0ca8b03492877a467d061f0c65eb194b2a Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 31 Dec 2022 11:43:40 +0100 Subject: [PATCH 090/101] Use strict provenance APIs in ty::tls --- compiler/rustc_middle/src/lib.rs | 1 + compiler/rustc_middle/src/ty/context/tls.rs | 30 ++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 7e4063c2ffd78..95148de251824 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -43,6 +43,7 @@ #![feature(min_specialization)] #![feature(trusted_len)] #![feature(type_alias_impl_trait)] +#![feature(strict_provenance)] #![feature(associated_type_bounds)] #![feature(rustc_attrs)] #![feature(control_flow_enum)] diff --git a/compiler/rustc_middle/src/ty/context/tls.rs b/compiler/rustc_middle/src/ty/context/tls.rs index f1fe47f6ba6ed..71b025dc1be4b 100644 --- a/compiler/rustc_middle/src/ty/context/tls.rs +++ b/compiler/rustc_middle/src/ty/context/tls.rs @@ -55,16 +55,16 @@ mod tlv { /// Gets Rayon's thread-local variable, which is preserved for Rayon jobs. /// This is used to get the pointer to the current `ImplicitCtxt`. #[inline] - pub(super) fn get_tlv() -> usize { - rayon_core::tlv::get() + pub(super) fn get_tlv() -> *const () { + ptr::from_exposed_addr(rayon_core::tlv::get()) } /// Sets Rayon's thread-local variable, which is preserved for Rayon jobs /// to `value` during the call to `f`. It is restored to its previous value after. /// This is used to set the pointer to the new `ImplicitCtxt`. #[inline] - pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { - rayon_core::tlv::with(value, f) + pub(super) fn with_tlv R, R>(value: *const (), f: F) -> R { + rayon_core::tlv::with(value.expose_addr(), f) } } @@ -75,12 +75,12 @@ mod tlv { thread_local! { /// A thread local variable that stores a pointer to the current `ImplicitCtxt`. - static TLV: Cell = const { Cell::new(0) }; + static TLV: Cell<*const ()> = const { Cell::new(ptr::null()) }; } /// Gets the pointer to the current `ImplicitCtxt`. #[inline] - pub(super) fn get_tlv() -> usize { + pub(super) fn get_tlv() -> *const () { TLV.with(|tlv| tlv.get()) } @@ -88,7 +88,7 @@ mod tlv { /// It is restored to its previous value after. /// This is used to set the pointer to the new `ImplicitCtxt`. #[inline] - pub(super) fn with_tlv R, R>(value: usize, f: F) -> R { + pub(super) fn with_tlv R, R>(value: *const (), f: F) -> R { let old = get_tlv(); let _reset = rustc_data_structures::OnDrop(move || TLV.with(|tlv| tlv.set(old))); TLV.with(|tlv| tlv.set(value)); @@ -96,13 +96,23 @@ mod tlv { } } +#[inline] +fn erase(context: &ImplicitCtxt<'_, '_>) -> *const () { + context as *const _ as *const () +} + +#[inline] +unsafe fn downcast<'a, 'tcx>(context: *const ()) -> &'a ImplicitCtxt<'a, 'tcx> { + &*(context as *const ImplicitCtxt<'a, 'tcx>) +} + /// Sets `context` as the new current `ImplicitCtxt` for the duration of the function `f`. #[inline] pub fn enter_context<'a, 'tcx, F, R>(context: &ImplicitCtxt<'a, 'tcx>, f: F) -> R where F: FnOnce(&ImplicitCtxt<'a, 'tcx>) -> R, { - tlv::with_tlv(context as *const _ as usize, || f(&context)) + tlv::with_tlv(erase(context), || f(&context)) } /// Allows access to the current `ImplicitCtxt` in a closure if one is available. @@ -112,14 +122,14 @@ where F: for<'a, 'tcx> FnOnce(Option<&ImplicitCtxt<'a, 'tcx>>) -> R, { let context = tlv::get_tlv(); - if context == 0 { + if context.is_null() { f(None) } else { // We could get an `ImplicitCtxt` pointer from another thread. // Ensure that `ImplicitCtxt` is `Sync`. sync::assert_sync::>(); - unsafe { f(Some(&*(context as *const ImplicitCtxt<'_, '_>))) } + unsafe { f(Some(downcast(context))) } } } From f74ca88384cbce7e318a7a0142a9ac3eda6b8641 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 21 Jan 2023 20:12:40 +0100 Subject: [PATCH 091/101] Use a type-alias-impl-trait in `ObligationForest` --- compiler/rustc_data_structures/src/obligation_forest/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_data_structures/src/obligation_forest/mod.rs b/compiler/rustc_data_structures/src/obligation_forest/mod.rs index 10e673cd9297b..dda422c6dd07e 100644 --- a/compiler/rustc_data_structures/src/obligation_forest/mod.rs +++ b/compiler/rustc_data_structures/src/obligation_forest/mod.rs @@ -139,8 +139,7 @@ pub enum ProcessResult { #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)] struct ObligationTreeId(usize); -type ObligationTreeIdGenerator = - std::iter::Map, fn(usize) -> ObligationTreeId>; +type ObligationTreeIdGenerator = impl Iterator; pub struct ObligationForest { /// The list of obligations. In between calls to [Self::process_obligations], From d7f6564fdd05c9bb0168be0554626de2886f1314 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 3 Jul 2022 22:08:01 +0200 Subject: [PATCH 092/101] Encode AdtDef in the def-id loop. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 141 ++++++------------- 1 file changed, 41 insertions(+), 100 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index ab2ad79b876d4..772c99a07bc35 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -38,7 +38,6 @@ use rustc_span::symbol::{sym, Symbol}; use rustc_span::{ self, DebuggerVisualizerFile, ExternalSource, FileName, SourceFile, Span, SyntaxContext, }; -use rustc_target::abi::VariantIdx; use std::borrow::Borrow; use std::collections::hash_map::Entry; use std::hash::Hash; @@ -1178,8 +1177,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id)); } if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind { - let params_in_repr = self.tcx.params_in_repr(def_id); - record!(self.tables.params_in_repr[def_id] <- params_in_repr); + self.encode_info_for_adt(def_id); } if should_encode_trait_impl_trait_tys(tcx, def_id) && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id) @@ -1199,9 +1197,38 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_enum_variant_info(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) { + #[instrument(level = "trace", skip(self))] + fn encode_info_for_adt(&mut self, def_id: DefId) { + let tcx = self.tcx; + let adt_def = tcx.adt_def(def_id); + record!(self.tables.repr_options[def_id] <- adt_def.repr()); + + let params_in_repr = self.tcx.params_in_repr(def_id); + record!(self.tables.params_in_repr[def_id] <- params_in_repr); + + if adt_def.is_enum() { + record_array!(self.tables.children[def_id] <- iter::from_generator(|| + for variant in tcx.adt_def(def_id).variants() { + yield variant.def_id.index; + // Encode constructors which take a separate slot in value namespace. + if let Some(ctor_def_id) = variant.ctor_def_id() { + yield ctor_def_id.index; + } + } + )); + } + + // In some cases, along with the item itself, we also + // encode some sub-items. Usually we want some info from the item + // so it's easier to do that here then to wait until we would encounter + // normally in the visitor walk. + for variant in adt_def.variants().iter() { + self.encode_enum_variant_info(variant); + } + } + + fn encode_enum_variant_info(&mut self, variant: &ty::VariantDef) { let tcx = self.tcx; - let variant = &def.variant(index); let def_id = variant.def_id; debug!("EncodeContext::encode_enum_variant_info({:?})", def_id); @@ -1218,27 +1245,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { f.did.index })); if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { - // FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`. - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id)); - } - } - - fn encode_enum_variant_ctor(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) { - let variant = &def.variant(index); - let Some((ctor_kind, def_id)) = variant.ctor else { return }; - debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id); + debug!("EncodeContext::encode_enum_variant_ctor({:?})", ctor_def_id); - // FIXME(eddyb) encode only the `CtorKind` for constructors. - let data = VariantData { - discr: variant.discr, - ctor: Some((ctor_kind, def_id.index)), - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }; + self.tables.constness.set(ctor_def_id.index, hir::Constness::Const); - record!(self.tables.variant_data[def_id] <- data); - self.tables.constness.set(def_id.index, hir::Constness::Const); - if ctor_kind == CtorKind::Fn { - record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); + let fn_sig = tcx.fn_sig(ctor_def_id); + record!(self.tables.fn_sig[ctor_def_id] <- fn_sig); + // FIXME(eddyb) encode signature only for `ctor_def_id`. + record!(self.tables.fn_sig[def_id] <- fn_sig); } } @@ -1291,25 +1305,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>) { - let variant = adt_def.non_enum_variant(); - let Some((ctor_kind, def_id)) = variant.ctor else { return }; - debug!("EncodeContext::encode_struct_ctor({:?})", def_id); - - let data = VariantData { - discr: variant.discr, - ctor: Some((ctor_kind, def_id.index)), - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }; - - record!(self.tables.repr_options[def_id] <- adt_def.repr()); - record!(self.tables.variant_data[def_id] <- data); - self.tables.constness.set(def_id.index, hir::Constness::Const); - if ctor_kind == CtorKind::Fn { - record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); - } - } - fn encode_explicit_item_bounds(&mut self, def_id: DefId) { debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id); let bounds = self.tcx.explicit_item_bounds(def_id); @@ -1518,33 +1513,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.tables.is_type_alias_impl_trait.set(def_id.index, ()); } } - hir::ItemKind::Enum(..) => { - let adt_def = self.tcx.adt_def(def_id); - record!(self.tables.repr_options[def_id] <- adt_def.repr()); - } - hir::ItemKind::Struct(..) => { - let adt_def = self.tcx.adt_def(def_id); - record!(self.tables.repr_options[def_id] <- adt_def.repr()); - self.tables.constness.set(def_id.index, hir::Constness::Const); - - let variant = adt_def.non_enum_variant(); - record!(self.tables.variant_data[def_id] <- VariantData { - discr: variant.discr, - ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }); - } - hir::ItemKind::Union(..) => { - let adt_def = self.tcx.adt_def(def_id); - record!(self.tables.repr_options[def_id] <- adt_def.repr()); - - let variant = adt_def.non_enum_variant(); - record!(self.tables.variant_data[def_id] <- VariantData { - discr: variant.discr, - ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }); - } hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { self.tables.impl_defaultness.set(def_id.index, *defaultness); self.tables.constness.set(def_id.index, *constness); @@ -1583,31 +1551,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } hir::ItemKind::Static(..) | hir::ItemKind::Const(..) + | hir::ItemKind::Enum(..) + | hir::ItemKind::Struct(..) + | hir::ItemKind::Union(..) | hir::ItemKind::ForeignMod { .. } | hir::ItemKind::GlobalAsm(..) | hir::ItemKind::TyAlias(..) => {} }; // FIXME(eddyb) there should be a nicer way to do this. match item.kind { - hir::ItemKind::Enum(..) => { - record_array!(self.tables.children[def_id] <- iter::from_generator(|| - for variant in tcx.adt_def(def_id).variants() { - yield variant.def_id.index; - // Encode constructors which take a separate slot in value namespace. - if let Some(ctor_def_id) = variant.ctor_def_id() { - yield ctor_def_id.index; - } - } - )) - } - hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => { - record_array!(self.tables.children[def_id] <- - self.tcx.adt_def(def_id).non_enum_variant().fields.iter().map(|f| { - assert!(f.did.is_local()); - f.did.index - }) - ) - } hir::ItemKind::Impl { .. } | hir::ItemKind::Trait(..) => { let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id); record_array!(self.tables.children[def_id] <- @@ -1635,17 +1587,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // so it's easier to do that here then to wait until we would encounter // normally in the visitor walk. match item.kind { - hir::ItemKind::Enum(..) => { - let def = self.tcx.adt_def(item.owner_id.to_def_id()); - for (i, _) in def.variants().iter_enumerated() { - self.encode_enum_variant_info(def, i); - self.encode_enum_variant_ctor(def, i); - } - } - hir::ItemKind::Struct(..) => { - let def = self.tcx.adt_def(item.owner_id.to_def_id()); - self.encode_struct_ctor(def); - } hir::ItemKind::Impl { .. } => { for &trait_item_def_id in self.tcx.associated_item_def_ids(item.owner_id.to_def_id()).iter() From 6ecf30d67d7e82722ac7aff232378739e92f33e6 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 3 Jul 2022 22:53:57 +0200 Subject: [PATCH 093/101] Inline encode_enum_variant_info. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 46 ++++++++------------ 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 772c99a07bc35..6227d01bcfae9 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1223,36 +1223,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // so it's easier to do that here then to wait until we would encounter // normally in the visitor walk. for variant in adt_def.variants().iter() { - self.encode_enum_variant_info(variant); - } - } - - fn encode_enum_variant_info(&mut self, variant: &ty::VariantDef) { - let tcx = self.tcx; - let def_id = variant.def_id; - debug!("EncodeContext::encode_enum_variant_info({:?})", def_id); + let data = VariantData { + discr: variant.discr, + ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), + is_non_exhaustive: variant.is_field_list_non_exhaustive(), + }; + record!(self.tables.variant_data[variant.def_id] <- data); - let data = VariantData { - discr: variant.discr, - ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), - is_non_exhaustive: variant.is_field_list_non_exhaustive(), - }; + self.tables.constness.set(variant.def_id.index, hir::Constness::Const); + record_array!(self.tables.children[variant.def_id] <- variant.fields.iter().map(|f| { + assert!(f.did.is_local()); + f.did.index + })); - record!(self.tables.variant_data[def_id] <- data); - self.tables.constness.set(def_id.index, hir::Constness::Const); - record_array!(self.tables.children[def_id] <- variant.fields.iter().map(|f| { - assert!(f.did.is_local()); - f.did.index - })); - if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { - debug!("EncodeContext::encode_enum_variant_ctor({:?})", ctor_def_id); - - self.tables.constness.set(ctor_def_id.index, hir::Constness::Const); - - let fn_sig = tcx.fn_sig(ctor_def_id); - record!(self.tables.fn_sig[ctor_def_id] <- fn_sig); - // FIXME(eddyb) encode signature only for `ctor_def_id`. - record!(self.tables.fn_sig[def_id] <- fn_sig); + if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { + self.tables.constness.set(ctor_def_id.index, hir::Constness::Const); + let fn_sig = tcx.fn_sig(ctor_def_id); + record!(self.tables.fn_sig[ctor_def_id] <- fn_sig); + // FIXME only encode signature for ctor_def_id + record!(self.tables.fn_sig[variant.def_id] <- fn_sig); + } } } From 4d11206ee78cd53bfd104c6073f3c8169da8828d Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 22 Jan 2023 11:08:33 +0000 Subject: [PATCH 094/101] Tweak comments. --- compiler/rustc_metadata/src/rmeta/encoder.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 6227d01bcfae9..0dba9a32f9cee 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -1216,12 +1216,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } )); + } else { + // For non-enum, there is only one variant, and its def_id is the adt's. + debug_assert_eq!(adt_def.variants().len(), 1); + debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id); + // Therefore, the loop over variants will encode its fields as the adt's children. } - // In some cases, along with the item itself, we also - // encode some sub-items. Usually we want some info from the item - // so it's easier to do that here then to wait until we would encounter - // normally in the visitor walk. for variant in adt_def.variants().iter() { let data = VariantData { discr: variant.discr, From ae3aa718a4d70f560b4fe5fb6297f8a8461a1e9e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Tue, 24 Jan 2023 19:03:13 +0100 Subject: [PATCH 095/101] Update tidy for cranelift-egraph removal and new windows-sys targets --- src/tools/tidy/src/deps.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tools/tidy/src/deps.rs b/src/tools/tidy/src/deps.rs index bc2edf634de2b..8ce19c8b5145b 100644 --- a/src/tools/tidy/src/deps.rs +++ b/src/tools/tidy/src/deps.rs @@ -59,7 +59,6 @@ const EXCEPTIONS_CRANELIFT: &[(&str, &str)] = &[ ("cranelift-codegen", "Apache-2.0 WITH LLVM-exception"), ("cranelift-codegen-meta", "Apache-2.0 WITH LLVM-exception"), ("cranelift-codegen-shared", "Apache-2.0 WITH LLVM-exception"), - ("cranelift-egraph", "Apache-2.0 WITH LLVM-exception"), ("cranelift-entity", "Apache-2.0 WITH LLVM-exception"), ("cranelift-frontend", "Apache-2.0 WITH LLVM-exception"), ("cranelift-isle", "Apache-2.0 WITH LLVM-exception"), @@ -286,7 +285,6 @@ const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[ "cranelift-codegen", "cranelift-codegen-meta", "cranelift-codegen-shared", - "cranelift-egraph", "cranelift-entity", "cranelift-frontend", "cranelift-isle", @@ -321,10 +319,12 @@ const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[ "winapi-i686-pc-windows-gnu", "winapi-x86_64-pc-windows-gnu", "windows-sys", + "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", "windows_i686_msvc", "windows_x86_64_gnu", + "windows_x86_64_gnullvm", "windows_x86_64_msvc", ]; From 1578b1c73fa2c2ae1b78b5d66f36734f41b968b8 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 25 Jan 2023 15:19:40 +0100 Subject: [PATCH 096/101] Vendor newer version of cranelift-native It fixes a bug that caused compilation on 32bit x86 to fail --- compiler/rustc_codegen_cranelift/Cargo.lock | 1 - compiler/rustc_codegen_cranelift/Cargo.toml | 4 +- .../src/cranelift_native.rs | 268 ++++++++++++++++++ compiler/rustc_codegen_cranelift/src/lib.rs | 2 + 4 files changed, 273 insertions(+), 2 deletions(-) create mode 100644 compiler/rustc_codegen_cranelift/src/cranelift_native.rs diff --git a/compiler/rustc_codegen_cranelift/Cargo.lock b/compiler/rustc_codegen_cranelift/Cargo.lock index 48800725d2c13..50249ea1bdb49 100644 --- a/compiler/rustc_codegen_cranelift/Cargo.lock +++ b/compiler/rustc_codegen_cranelift/Cargo.lock @@ -333,7 +333,6 @@ dependencies = [ "cranelift-frontend", "cranelift-jit", "cranelift-module", - "cranelift-native", "cranelift-object", "gimli", "indexmap", diff --git a/compiler/rustc_codegen_cranelift/Cargo.toml b/compiler/rustc_codegen_cranelift/Cargo.toml index eadb4438bc992..34117c2886feb 100644 --- a/compiler/rustc_codegen_cranelift/Cargo.toml +++ b/compiler/rustc_codegen_cranelift/Cargo.toml @@ -18,7 +18,9 @@ crate-type = ["dylib"] cranelift-codegen = { version = "0.92", features = ["unwind", "all-arch"] } cranelift-frontend = { version = "0.92" } cranelift-module = { version = "0.92" } -cranelift-native = { version = "0.92" } +# NOTE vendored as src/cranelift_native.rs +# FIXME revert back to the external crate with Cranelift 0.93 +#cranelift-native = { version = "0.92" } cranelift-jit = { version = "0.92", optional = true } cranelift-object = { version = "0.92" } target-lexicon = "0.12.0" diff --git a/compiler/rustc_codegen_cranelift/src/cranelift_native.rs b/compiler/rustc_codegen_cranelift/src/cranelift_native.rs new file mode 100644 index 0000000000000..7c0ca1adc2d48 --- /dev/null +++ b/compiler/rustc_codegen_cranelift/src/cranelift_native.rs @@ -0,0 +1,268 @@ +// Vendored from https://github.com/bytecodealliance/wasmtime/blob/b58a197d33f044193c3d608010f5e6ec394ac07e/cranelift/native/src/lib.rs +// which is licensed as +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// unlike rustc_codegen_cranelift itself. +// FIXME revert back to the external crate with Cranelift 0.93 +#![allow(warnings)] + +//! Performs autodetection of the host for the purposes of running +//! Cranelift to generate code to run on the same machine. + +#![deny( + missing_docs, + trivial_numeric_casts, + unused_extern_crates, + unstable_features +)] +#![warn(unused_import_braces)] +#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))] +#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))] +#![cfg_attr( + feature = "cargo-clippy", + warn( + clippy::float_arithmetic, + clippy::mut_mut, + clippy::nonminimal_bool, + clippy::map_unwrap_or, + clippy::clippy::print_stdout, + clippy::unicode_not_nfc, + clippy::use_self + ) +)] + +use cranelift_codegen::isa; +use target_lexicon::Triple; + +/// Return an `isa` builder configured for the current host +/// machine, or `Err(())` if the host machine is not supported +/// in the current configuration. +pub fn builder() -> Result { + builder_with_options(true) +} + +/// Return an `isa` builder configured for the current host +/// machine, or `Err(())` if the host machine is not supported +/// in the current configuration. +/// +/// Selects the given backend variant specifically; this is +/// useful when more than oen backend exists for a given target +/// (e.g., on x86-64). +pub fn builder_with_options(infer_native_flags: bool) -> Result { + let mut isa_builder = isa::lookup(Triple::host()).map_err(|err| match err { + isa::LookupError::SupportDisabled => "support for architecture disabled at compile time", + isa::LookupError::Unsupported => "unsupported architecture", + })?; + + #[cfg(target_arch = "x86_64")] + { + use cranelift_codegen::settings::Configurable; + + if !std::is_x86_feature_detected!("sse2") { + return Err("x86 support requires SSE2"); + } + + if !infer_native_flags { + return Ok(isa_builder); + } + + // These are temporarily enabled by default (see #3810 for + // more) so that a default-constructed `Flags` can work with + // default Wasmtime features. Otherwise, the user must + // explicitly use native flags or turn these on when on x86-64 + // platforms to avoid a configuration panic. In order for the + // "enable if detected" logic below to work, we must turn them + // *off* (differing from the default) and then re-enable below + // if present. + isa_builder.set("has_sse3", "false").unwrap(); + isa_builder.set("has_ssse3", "false").unwrap(); + isa_builder.set("has_sse41", "false").unwrap(); + isa_builder.set("has_sse42", "false").unwrap(); + + if std::is_x86_feature_detected!("sse3") { + isa_builder.enable("has_sse3").unwrap(); + } + if std::is_x86_feature_detected!("ssse3") { + isa_builder.enable("has_ssse3").unwrap(); + } + if std::is_x86_feature_detected!("sse4.1") { + isa_builder.enable("has_sse41").unwrap(); + } + if std::is_x86_feature_detected!("sse4.2") { + isa_builder.enable("has_sse42").unwrap(); + } + if std::is_x86_feature_detected!("popcnt") { + isa_builder.enable("has_popcnt").unwrap(); + } + if std::is_x86_feature_detected!("avx") { + isa_builder.enable("has_avx").unwrap(); + } + if std::is_x86_feature_detected!("avx2") { + isa_builder.enable("has_avx2").unwrap(); + } + if std::is_x86_feature_detected!("fma") { + isa_builder.enable("has_fma").unwrap(); + } + if std::is_x86_feature_detected!("bmi1") { + isa_builder.enable("has_bmi1").unwrap(); + } + if std::is_x86_feature_detected!("bmi2") { + isa_builder.enable("has_bmi2").unwrap(); + } + if std::is_x86_feature_detected!("avx512bitalg") { + isa_builder.enable("has_avx512bitalg").unwrap(); + } + if std::is_x86_feature_detected!("avx512dq") { + isa_builder.enable("has_avx512dq").unwrap(); + } + if std::is_x86_feature_detected!("avx512f") { + isa_builder.enable("has_avx512f").unwrap(); + } + if std::is_x86_feature_detected!("avx512vl") { + isa_builder.enable("has_avx512vl").unwrap(); + } + if std::is_x86_feature_detected!("avx512vbmi") { + isa_builder.enable("has_avx512vbmi").unwrap(); + } + if std::is_x86_feature_detected!("lzcnt") { + isa_builder.enable("has_lzcnt").unwrap(); + } + } + + #[cfg(target_arch = "aarch64")] + { + use cranelift_codegen::settings::Configurable; + + if !infer_native_flags { + return Ok(isa_builder); + } + + if std::arch::is_aarch64_feature_detected!("lse") { + isa_builder.enable("has_lse").unwrap(); + } + + if std::arch::is_aarch64_feature_detected!("paca") { + isa_builder.enable("has_pauth").unwrap(); + } + + if cfg!(target_os = "macos") { + // Pointer authentication is always available on Apple Silicon. + isa_builder.enable("sign_return_address").unwrap(); + // macOS enforces the use of the B key for return addresses. + isa_builder.enable("sign_return_address_with_bkey").unwrap(); + } + } + + // There is no is_s390x_feature_detected macro yet, so for now + // we use getauxval from the libc crate directly. + #[cfg(all(target_arch = "s390x", target_os = "linux"))] + { + use cranelift_codegen::settings::Configurable; + + if !infer_native_flags { + return Ok(isa_builder); + } + + let v = unsafe { libc::getauxval(libc::AT_HWCAP) }; + const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768; + if (v & HWCAP_S390X_VXRS_EXT2) != 0 { + isa_builder.enable("has_vxrs_ext2").unwrap(); + // There is no separate HWCAP bit for mie2, so assume + // that any machine with vxrs_ext2 also has mie2. + isa_builder.enable("has_mie2").unwrap(); + } + } + + // `is_riscv_feature_detected` is nightly only for now, use + // getauxval from the libc crate directly as a temporary measure. + #[cfg(all(target_arch = "riscv64", target_os = "linux"))] + { + use cranelift_codegen::settings::Configurable; + + if !infer_native_flags { + return Ok(isa_builder); + } + + let v = unsafe { libc::getauxval(libc::AT_HWCAP) }; + + const HWCAP_RISCV_EXT_A: libc::c_ulong = 1 << (b'a' - b'a'); + const HWCAP_RISCV_EXT_C: libc::c_ulong = 1 << (b'c' - b'a'); + const HWCAP_RISCV_EXT_D: libc::c_ulong = 1 << (b'd' - b'a'); + const HWCAP_RISCV_EXT_F: libc::c_ulong = 1 << (b'f' - b'a'); + const HWCAP_RISCV_EXT_M: libc::c_ulong = 1 << (b'm' - b'a'); + const HWCAP_RISCV_EXT_V: libc::c_ulong = 1 << (b'v' - b'a'); + + if (v & HWCAP_RISCV_EXT_A) != 0 { + isa_builder.enable("has_a").unwrap(); + } + + if (v & HWCAP_RISCV_EXT_C) != 0 { + isa_builder.enable("has_c").unwrap(); + } + + if (v & HWCAP_RISCV_EXT_D) != 0 { + isa_builder.enable("has_d").unwrap(); + } + + if (v & HWCAP_RISCV_EXT_F) != 0 { + isa_builder.enable("has_f").unwrap(); + + // TODO: There doesn't seem to be a bit associated with this extension + // rust enables it with the `f` extension: + // https://github.com/rust-lang/stdarch/blob/790411f93c4b5eada3c23abb4c9a063fb0b24d99/crates/std_detect/src/detect/os/linux/riscv.rs#L43 + isa_builder.enable("has_zicsr").unwrap(); + } + + if (v & HWCAP_RISCV_EXT_M) != 0 { + isa_builder.enable("has_m").unwrap(); + } + + if (v & HWCAP_RISCV_EXT_V) != 0 { + isa_builder.enable("has_v").unwrap(); + } + + // TODO: ZiFencei does not have a bit associated with it + // TODO: Zbkb does not have a bit associated with it + } + + // squelch warnings about unused mut/variables on some platforms. + drop(&mut isa_builder); + drop(infer_native_flags); + + Ok(isa_builder) +} + +#[cfg(test)] +mod tests { + use super::builder; + use cranelift_codegen::isa::CallConv; + use cranelift_codegen::settings; + + #[test] + fn test() { + if let Ok(isa_builder) = builder() { + let flag_builder = settings::builder(); + let isa = isa_builder + .finish(settings::Flags::new(flag_builder)) + .unwrap(); + + if cfg!(all(target_os = "macos", target_arch = "aarch64")) { + assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64); + } else if cfg!(any(unix, target_os = "nebulet")) { + assert_eq!(isa.default_call_conv(), CallConv::SystemV); + } else if cfg!(windows) { + assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall); + } + + if cfg!(target_pointer_width = "64") { + assert_eq!(isa.pointer_bits(), 64); + } else if cfg!(target_pointer_width = "32") { + assert_eq!(isa.pointer_bits(), 32); + } else if cfg!(target_pointer_width = "16") { + assert_eq!(isa.pointer_bits(), 16); + } + } + } +} + +/// Version number of this crate. +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); diff --git a/compiler/rustc_codegen_cranelift/src/lib.rs b/compiler/rustc_codegen_cranelift/src/lib.rs index 70d0cc339a80c..d3868730557b7 100644 --- a/compiler/rustc_codegen_cranelift/src/lib.rs +++ b/compiler/rustc_codegen_cranelift/src/lib.rs @@ -57,6 +57,8 @@ mod compiler_builtins; mod concurrency_limiter; mod config; mod constant; +// FIXME revert back to the external crate with Cranelift 0.93 +mod cranelift_native; mod debuginfo; mod discriminant; mod driver; From 3808bc4639468018b1e5c30e1cd2e6905485ce67 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Wed, 25 Jan 2023 15:56:22 +0100 Subject: [PATCH 097/101] Fix CI --- .../src/cranelift_native.rs | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/cranelift_native.rs b/compiler/rustc_codegen_cranelift/src/cranelift_native.rs index 7c0ca1adc2d48..6c4efca442448 100644 --- a/compiler/rustc_codegen_cranelift/src/cranelift_native.rs +++ b/compiler/rustc_codegen_cranelift/src/cranelift_native.rs @@ -1,34 +1,16 @@ // Vendored from https://github.com/bytecodealliance/wasmtime/blob/b58a197d33f044193c3d608010f5e6ec394ac07e/cranelift/native/src/lib.rs // which is licensed as // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// unlike rustc_codegen_cranelift itself. +// unlike rustc_codegen_cranelift itself. Also applies a small change to remove #![cfg_attr] that +// rust's CI complains about and to fix formatting to match rustc. // FIXME revert back to the external crate with Cranelift 0.93 #![allow(warnings)] //! Performs autodetection of the host for the purposes of running //! Cranelift to generate code to run on the same machine. -#![deny( - missing_docs, - trivial_numeric_casts, - unused_extern_crates, - unstable_features -)] +#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)] #![warn(unused_import_braces)] -#![cfg_attr(feature = "clippy", plugin(clippy(conf_file = "../../clippy.toml")))] -#![cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))] -#![cfg_attr( - feature = "cargo-clippy", - warn( - clippy::float_arithmetic, - clippy::mut_mut, - clippy::nonminimal_bool, - clippy::map_unwrap_or, - clippy::clippy::print_stdout, - clippy::unicode_not_nfc, - clippy::use_self - ) -)] use cranelift_codegen::isa; use target_lexicon::Triple; @@ -241,9 +223,7 @@ mod tests { fn test() { if let Ok(isa_builder) = builder() { let flag_builder = settings::builder(); - let isa = isa_builder - .finish(settings::Flags::new(flag_builder)) - .unwrap(); + let isa = isa_builder.finish(settings::Flags::new(flag_builder)).unwrap(); if cfg!(all(target_os = "macos", target_arch = "aarch64")) { assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64); From 114eb2d0e255a49b759412b645799fc52c38968a Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Mon, 23 Jan 2023 22:17:36 +0000 Subject: [PATCH 098/101] Replace ZST operands and debuginfo by constants. --- .../rustc_mir_transform/src/remove_zsts.rs | 121 ++++++++++++++---- .../invalid_constant.main.ConstProp.diff | 14 +- .../invalid_constant.main.RemoveZsts.diff | 73 +++++++++++ tests/mir-opt/const_prop/invalid_constant.rs | 1 + .../inline/asm_unwind.main.Inline.diff | 2 +- tests/mir-opt/inline/cycle.g.Inline.diff | 5 +- tests/mir-opt/inline/cycle.main.Inline.diff | 5 +- .../inline/inline_cycle.two.Inline.diff | 5 +- .../inline/inline_diverging.h.Inline.diff | 38 +++--- ...ine_scopes_parenting.main.Inline.after.mir | 7 +- ...intrinsics_e2e.f_unit.PreCodegen.after.mir | 13 +- ..._option_map_e2e.ezmap.PreCodegen.after.mir | 35 ++--- 12 files changed, 220 insertions(+), 99 deletions(-) create mode 100644 tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff diff --git a/compiler/rustc_mir_transform/src/remove_zsts.rs b/compiler/rustc_mir_transform/src/remove_zsts.rs index 6cabef92d8c21..6d19f8a94ea43 100644 --- a/compiler/rustc_mir_transform/src/remove_zsts.rs +++ b/compiler/rustc_mir_transform/src/remove_zsts.rs @@ -1,7 +1,9 @@ //! Removes assignments to ZST places. use crate::MirPass; -use rustc_middle::mir::{Body, StatementKind}; +use rustc_middle::mir::interpret::ConstValue; +use rustc_middle::mir::visit::*; +use rustc_middle::mir::*; use rustc_middle::ty::{self, Ty, TyCtxt}; pub struct RemoveZsts; @@ -16,38 +18,24 @@ impl<'tcx> MirPass<'tcx> for RemoveZsts { if tcx.type_of(body.source.def_id()).is_generator() { return; } - let param_env = tcx.param_env(body.source.def_id()); - let basic_blocks = body.basic_blocks.as_mut_preserves_cfg(); + let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id()); let local_decls = &body.local_decls; - for block in basic_blocks { - for statement in block.statements.iter_mut() { - if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) = - statement.kind - { - let place_ty = place.ty(local_decls, tcx).ty; - if !maybe_zst(place_ty) { - continue; - } - let Ok(layout) = tcx.layout_of(param_env.and(place_ty)) else { - continue; - }; - if !layout.is_zst() { - continue; - } - if tcx.consider_optimizing(|| { - format!( - "RemoveZsts - Place: {:?} SourceInfo: {:?}", - place, statement.source_info - ) - }) { - statement.make_nop(); - } - } - } + let mut replacer = Replacer { tcx, param_env, local_decls }; + for var_debug_info in &mut body.var_debug_info { + replacer.visit_var_debug_info(var_debug_info); + } + for (bb, data) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() { + replacer.visit_basic_block_data(bb, data); } } } +struct Replacer<'a, 'tcx> { + tcx: TyCtxt<'tcx>, + param_env: ty::ParamEnv<'tcx>, + local_decls: &'a LocalDecls<'tcx>, +} + /// A cheap, approximate check to avoid unnecessary `layout_of` calls. fn maybe_zst(ty: Ty<'_>) -> bool { match ty.kind() { @@ -63,3 +51,80 @@ fn maybe_zst(ty: Ty<'_>) -> bool { _ => false, } } + +impl<'tcx> Replacer<'_, 'tcx> { + fn is_zst(&self, ty: Ty<'tcx>) -> bool { + if !maybe_zst(ty) { + return false; + } + let Ok(layout) = self.tcx.layout_of(self.param_env.and(ty)) else { + return false; + }; + layout.is_zst() + } + + fn make_zst(&self, ty: Ty<'tcx>) -> Constant<'tcx> { + debug_assert!(self.is_zst(ty)); + Constant { + span: rustc_span::DUMMY_SP, + user_ty: None, + literal: ConstantKind::Val(ConstValue::ZeroSized, ty), + } + } +} + +impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { + fn tcx(&self) -> TyCtxt<'tcx> { + self.tcx + } + + fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) { + match var_debug_info.value { + VarDebugInfoContents::Const(_) => {} + VarDebugInfoContents::Place(place) => { + let place_ty = place.ty(self.local_decls, self.tcx).ty; + if self.is_zst(place_ty) { + var_debug_info.value = VarDebugInfoContents::Const(self.make_zst(place_ty)) + } + } + VarDebugInfoContents::Composite { ty, fragments: _ } => { + if self.is_zst(ty) { + var_debug_info.value = VarDebugInfoContents::Const(self.make_zst(ty)) + } + } + } + } + + fn visit_operand(&mut self, operand: &mut Operand<'tcx>, loc: Location) { + if let Operand::Constant(_) = operand { + return; + } + let op_ty = operand.ty(self.local_decls, self.tcx); + if self.is_zst(op_ty) + && self.tcx.consider_optimizing(|| { + format!("RemoveZsts - Operand: {:?} Location: {:?}", operand, loc) + }) + { + *operand = Operand::Constant(Box::new(self.make_zst(op_ty))) + } + } + + fn visit_statement(&mut self, statement: &mut Statement<'tcx>, loc: Location) { + if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) = + statement.kind + { + let place_ty = place.ty(self.local_decls, self.tcx).ty; + if self.is_zst(place_ty) + && self.tcx.consider_optimizing(|| { + format!( + "RemoveZsts - Place: {:?} SourceInfo: {:?}", + place, statement.source_info + ) + }) + { + statement.make_nop(); + } + } + self.super_statement(statement, loc); + } +} diff --git a/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff b/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff index 6c4757c1a8103..b20e571a4ccb2 100644 --- a/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/invalid_constant.main.ConstProp.diff @@ -12,12 +12,10 @@ let _3: [E; 1]; // in scope 1 at $DIR/invalid_constant.rs:+13:9: +13:21 scope 3 { debug _invalid_tag => _3; // in scope 3 at $DIR/invalid_constant.rs:+13:9: +13:21 - let _6: [Empty; 1]; // in scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 scope 5 { - debug _enum_without_variants => _6; // in scope 5 at $DIR/invalid_constant.rs:+20:9: +20:31 - let _7: main::Str<"���">; // in scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 + debug _enum_without_variants => const [ZeroSized: Empty]; // in scope 5 at $DIR/invalid_constant.rs:+20:9: +20:31 scope 7 { - debug _non_utf8_str => _7; // in scope 7 at $DIR/invalid_constant.rs:+24:9: +24:22 + debug _non_utf8_str => const Str::<"���">; // in scope 7 at $DIR/invalid_constant.rs:+24:9: +24:22 } } scope 6 { @@ -46,18 +44,14 @@ - _3 = [move _4]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 + _4 = const Scalar(0x00000004): E; // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:57 + // mir::Constant -+ // + span: $DIR/invalid_constant.rs:28:34: 28:57 ++ // + span: $DIR/invalid_constant.rs:29:34: 29:57 + // + literal: Const { ty: E, val: Value(Scalar(0x00000004)) } + _3 = [const Scalar(0x00000004): E]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 + // mir::Constant -+ // + span: $DIR/invalid_constant.rs:28:24: 28:60 ++ // + span: $DIR/invalid_constant.rs:29:24: 29:60 + // + literal: Const { ty: E, val: Value(Scalar(0x00000004)) } StorageDead(_4); // scope 1 at $DIR/invalid_constant.rs:+13:59: +13:60 StorageDead(_5); // scope 1 at $DIR/invalid_constant.rs:+13:60: +13:61 - StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 - StorageLive(_7); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 - StorageDead(_7); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2 - StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2 StorageDead(_3); // scope 1 at $DIR/invalid_constant.rs:+27:1: +27:2 StorageDead(_1); // scope 0 at $DIR/invalid_constant.rs:+27:1: +27:2 return; // scope 0 at $DIR/invalid_constant.rs:+27:2: +27:2 diff --git a/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff b/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff new file mode 100644 index 0000000000000..2d63101173719 --- /dev/null +++ b/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff @@ -0,0 +1,73 @@ +- // MIR for `main` before RemoveZsts ++ // MIR for `main` after RemoveZsts + + fn main() -> () { + let mut _0: (); // return place in scope 0 at $DIR/invalid_constant.rs:+0:11: +0:11 + let _1: char; // in scope 0 at $DIR/invalid_constant.rs:+6:9: +6:22 + let mut _2: main::InvalidChar; // in scope 0 at $DIR/invalid_constant.rs:+6:34: +6:63 + let mut _4: E; // in scope 0 at $DIR/invalid_constant.rs:+13:25: +13:59 + let mut _5: main::InvalidTag; // in scope 0 at $DIR/invalid_constant.rs:+13:34: +13:55 + let mut _7: Empty; // in scope 0 at $DIR/invalid_constant.rs:+20:35: +20:73 + let mut _8: main::NoVariants; // in scope 0 at $DIR/invalid_constant.rs:+20:44: +20:65 + scope 1 { + debug _invalid_char => _1; // in scope 1 at $DIR/invalid_constant.rs:+6:9: +6:22 + let _3: [E; 1]; // in scope 1 at $DIR/invalid_constant.rs:+13:9: +13:21 + scope 3 { + debug _invalid_tag => _3; // in scope 3 at $DIR/invalid_constant.rs:+13:9: +13:21 + let _6: [Empty; 1]; // in scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 + scope 5 { +- debug _enum_without_variants => _6; // in scope 5 at $DIR/invalid_constant.rs:+20:9: +20:31 ++ debug _enum_without_variants => const [ZeroSized: Empty]; // in scope 5 at $DIR/invalid_constant.rs:+20:9: +20:31 + let _9: main::Str<"���">; // in scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 + scope 7 { +- debug _non_utf8_str => _9; // in scope 7 at $DIR/invalid_constant.rs:+24:9: +24:22 ++ debug _non_utf8_str => const Str::<"���">; // in scope 7 at $DIR/invalid_constant.rs:+24:9: +24:22 + } + } + scope 6 { + } + } + scope 4 { + } + } + scope 2 { + } + + bb0: { + StorageLive(_1); // scope 0 at $DIR/invalid_constant.rs:+6:9: +6:22 + StorageLive(_2); // scope 2 at $DIR/invalid_constant.rs:+6:34: +6:63 + Deinit(_2); // scope 2 at $DIR/invalid_constant.rs:+6:34: +6:63 + (_2.0: u32) = const 1114113_u32; // scope 2 at $DIR/invalid_constant.rs:+6:34: +6:63 + _1 = (_2.1: char); // scope 2 at $DIR/invalid_constant.rs:+6:34: +6:67 + StorageDead(_2); // scope 0 at $DIR/invalid_constant.rs:+6:69: +6:70 + StorageLive(_3); // scope 1 at $DIR/invalid_constant.rs:+13:9: +13:21 + StorageLive(_4); // scope 1 at $DIR/invalid_constant.rs:+13:25: +13:59 + StorageLive(_5); // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:55 + Deinit(_5); // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:55 + (_5.0: u32) = const 4_u32; // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:55 + _4 = (_5.1: E); // scope 4 at $DIR/invalid_constant.rs:+13:34: +13:57 + _3 = [move _4]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 + StorageDead(_4); // scope 1 at $DIR/invalid_constant.rs:+13:59: +13:60 + StorageDead(_5); // scope 1 at $DIR/invalid_constant.rs:+13:60: +13:61 + StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 + StorageLive(_7); // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73 + StorageLive(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 + Deinit(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 + (_8.0: u32) = const 0_u32; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 +- _7 = (_8.1: Empty); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71 +- _6 = [move _7]; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74 ++ nop; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71 ++ nop; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74 + StorageDead(_7); // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74 + StorageDead(_8); // scope 3 at $DIR/invalid_constant.rs:+20:74: +20:75 + StorageLive(_9); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 +- _0 = const (); // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2 ++ nop; // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2 + StorageDead(_9); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2 + StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2 + StorageDead(_3); // scope 1 at $DIR/invalid_constant.rs:+27:1: +27:2 + StorageDead(_1); // scope 0 at $DIR/invalid_constant.rs:+27:1: +27:2 + return; // scope 0 at $DIR/invalid_constant.rs:+27:2: +27:2 + } + } + diff --git a/tests/mir-opt/const_prop/invalid_constant.rs b/tests/mir-opt/const_prop/invalid_constant.rs index 0337a7ca85118..eb6172cdff90f 100644 --- a/tests/mir-opt/const_prop/invalid_constant.rs +++ b/tests/mir-opt/const_prop/invalid_constant.rs @@ -11,6 +11,7 @@ enum E { A, B, C } #[derive(Copy, Clone)] enum Empty {} +// EMIT_MIR invalid_constant.main.RemoveZsts.diff // EMIT_MIR invalid_constant.main.ConstProp.diff fn main() { // An invalid char. diff --git a/tests/mir-opt/inline/asm_unwind.main.Inline.diff b/tests/mir-opt/inline/asm_unwind.main.Inline.diff index f1b62ac38ba4b..a4193290a9521 100644 --- a/tests/mir-opt/inline/asm_unwind.main.Inline.diff +++ b/tests/mir-opt/inline/asm_unwind.main.Inline.diff @@ -7,7 +7,7 @@ + scope 1 (inlined foo) { // at $DIR/asm_unwind.rs:21:5: 21:10 + let _2: D; // in scope 1 at $DIR/asm_unwind.rs:15:9: 15:11 + scope 2 { -+ debug _d => _2; // in scope 2 at $DIR/asm_unwind.rs:15:9: 15:11 ++ debug _d => const D; // in scope 2 at $DIR/asm_unwind.rs:15:9: 15:11 + scope 3 { + } + } diff --git a/tests/mir-opt/inline/cycle.g.Inline.diff b/tests/mir-opt/inline/cycle.g.Inline.diff index 5f3ee467c88cb..d1c9f2ff37945 100644 --- a/tests/mir-opt/inline/cycle.g.Inline.diff +++ b/tests/mir-opt/inline/cycle.g.Inline.diff @@ -5,11 +5,11 @@ let mut _0: (); // return place in scope 0 at $DIR/cycle.rs:+0:8: +0:8 let _1: (); // in scope 0 at $DIR/cycle.rs:+1:5: +1:12 + let mut _2: fn() {main}; // in scope 0 at $DIR/cycle.rs:+1:5: +1:12 ++ let mut _5: (); // in scope 0 at $DIR/cycle.rs:6:5: 6:8 + scope 1 (inlined f::) { // at $DIR/cycle.rs:12:5: 12:12 + debug g => _2; // in scope 1 at $DIR/cycle.rs:5:6: 5:7 + let _3: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8 + let mut _4: &fn() {main}; // in scope 1 at $DIR/cycle.rs:6:5: 6:6 -+ let mut _5: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8 + scope 2 (inlined >::call - shim(fn() {main})) { // at $DIR/cycle.rs:6:5: 6:8 + } + } @@ -29,6 +29,7 @@ + StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6 + _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6 + StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 ++ _5 = const (); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + _3 = move (*_4)() -> [return: bb4, unwind: bb2]; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL } @@ -48,7 +49,7 @@ + } + + bb4: { -+ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:7: 6:8 ++ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8 + StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9 + drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2 diff --git a/tests/mir-opt/inline/cycle.main.Inline.diff b/tests/mir-opt/inline/cycle.main.Inline.diff index 6b4c63bbd917f..03035c1e73f20 100644 --- a/tests/mir-opt/inline/cycle.main.Inline.diff +++ b/tests/mir-opt/inline/cycle.main.Inline.diff @@ -5,11 +5,11 @@ let mut _0: (); // return place in scope 0 at $DIR/cycle.rs:+0:11: +0:11 let _1: (); // in scope 0 at $DIR/cycle.rs:+1:5: +1:9 + let mut _2: fn() {g}; // in scope 0 at $DIR/cycle.rs:+1:5: +1:9 ++ let mut _5: (); // in scope 0 at $DIR/cycle.rs:6:5: 6:8 + scope 1 (inlined f::) { // at $DIR/cycle.rs:17:5: 17:9 + debug g => _2; // in scope 1 at $DIR/cycle.rs:5:6: 5:7 + let _3: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8 + let mut _4: &fn() {g}; // in scope 1 at $DIR/cycle.rs:6:5: 6:6 -+ let mut _5: (); // in scope 1 at $DIR/cycle.rs:6:5: 6:8 + scope 2 (inlined >::call - shim(fn() {g})) { // at $DIR/cycle.rs:6:5: 6:8 + } + } @@ -29,6 +29,7 @@ + StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6 + _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6 + StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 ++ _5 = const (); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + _3 = move (*_4)() -> [return: bb4, unwind: bb2]; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL } @@ -48,7 +49,7 @@ + } + + bb4: { -+ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:7: 6:8 ++ StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8 + StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9 + drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2 diff --git a/tests/mir-opt/inline/inline_cycle.two.Inline.diff b/tests/mir-opt/inline/inline_cycle.two.Inline.diff index 64c0065b5436d..5c395a3b24c65 100644 --- a/tests/mir-opt/inline/inline_cycle.two.Inline.diff +++ b/tests/mir-opt/inline/inline_cycle.two.Inline.diff @@ -5,10 +5,10 @@ let mut _0: (); // return place in scope 0 at $DIR/inline_cycle.rs:+0:10: +0:10 let _1: (); // in scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 + let mut _2: fn() {f}; // in scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 ++ let mut _4: (); // in scope 0 at $DIR/inline_cycle.rs:54:5: 54:8 + scope 1 (inlined call::) { // at $DIR/inline_cycle.rs:49:5: 49:12 + debug f => _2; // in scope 1 at $DIR/inline_cycle.rs:53:22: 53:23 + let _3: (); // in scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 -+ let mut _4: (); // in scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + scope 2 (inlined >::call_once - shim(fn() {f})) { // at $DIR/inline_cycle.rs:54:5: 54:8 + } + } @@ -26,11 +26,12 @@ // + literal: Const { ty: fn() {f}, val: Value() } + StorageLive(_3); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + StorageLive(_4); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 ++ _4 = const (); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + _3 = move _2() -> bb1; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL } bb1: { -+ StorageDead(_4); // scope 1 at $DIR/inline_cycle.rs:54:7: 54:8 ++ StorageDead(_4); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + StorageDead(_3); // scope 1 at $DIR/inline_cycle.rs:54:8: 54:9 + StorageDead(_2); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 StorageDead(_1); // scope 0 at $DIR/inline_cycle.rs:+1:12: +1:13 diff --git a/tests/mir-opt/inline/inline_diverging.h.Inline.diff b/tests/mir-opt/inline/inline_diverging.h.Inline.diff index f82fcf4c82129..2a36549ca354c 100644 --- a/tests/mir-opt/inline/inline_diverging.h.Inline.diff +++ b/tests/mir-opt/inline/inline_diverging.h.Inline.diff @@ -5,19 +5,18 @@ let mut _0: (); // return place in scope 0 at $DIR/inline_diverging.rs:+0:12: +0:12 let _1: (!, !); // in scope 0 at $DIR/inline_diverging.rs:+1:5: +1:22 + let mut _2: fn() -> ! {sleep}; // in scope 0 at $DIR/inline_diverging.rs:+1:5: +1:22 ++ let mut _8: (); // in scope 0 at $DIR/inline_diverging.rs:27:13: 27:16 + scope 1 (inlined call_twice:: ! {sleep}>) { // at $DIR/inline_diverging.rs:22:5: 22:22 + debug f => _2; // in scope 1 at $DIR/inline_diverging.rs:26:36: 26:37 + let _3: !; // in scope 1 at $DIR/inline_diverging.rs:27:9: 27:10 + let mut _4: &fn() -> ! {sleep}; // in scope 1 at $DIR/inline_diverging.rs:27:13: 27:14 -+ let mut _5: (); // in scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 -+ let mut _6: &fn() -> ! {sleep}; // in scope 1 at $DIR/inline_diverging.rs:28:13: 28:14 -+ let mut _7: (); // in scope 1 at $DIR/inline_diverging.rs:28:13: 28:16 -+ let mut _8: !; // in scope 1 at $DIR/inline_diverging.rs:29:6: 29:7 -+ let mut _9: !; // in scope 1 at $DIR/inline_diverging.rs:29:9: 29:10 ++ let mut _5: &fn() -> ! {sleep}; // in scope 1 at $DIR/inline_diverging.rs:28:13: 28:14 ++ let mut _6: !; // in scope 1 at $DIR/inline_diverging.rs:29:6: 29:7 ++ let mut _7: !; // in scope 1 at $DIR/inline_diverging.rs:29:9: 29:10 + scope 2 { + debug a => _3; // in scope 2 at $DIR/inline_diverging.rs:27:9: 27:10 + scope 3 { -+ debug b => _9; // in scope 3 at $DIR/inline_diverging.rs:28:9: 28:10 ++ debug b => _7; // in scope 3 at $DIR/inline_diverging.rs:28:9: 28:10 + } + } + scope 4 (inlined ! {sleep} as Fn<()>>::call - shim(fn() -> ! {sleep})) { // at $DIR/inline_diverging.rs:27:13: 27:16 @@ -35,23 +34,23 @@ - // mir::Constant // + span: $DIR/inline_diverging.rs:22:16: 22:21 // + literal: Const { ty: fn() -> ! {sleep}, val: Value() } -+ StorageLive(_9); // scope 0 at $DIR/inline_diverging.rs:+1:5: +1:22 ++ StorageLive(_7); // scope 0 at $DIR/inline_diverging.rs:+1:5: +1:22 + StorageLive(_3); // scope 1 at $DIR/inline_diverging.rs:27:9: 27:10 + StorageLive(_4); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:14 + _4 = &_2; // scope 1 at $DIR/inline_diverging.rs:27:13: 27:14 -+ StorageLive(_5); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 ++ StorageLive(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 ++ _8 = const (); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 + _3 = move (*_4)() -> [return: bb6, unwind: bb4]; // scope 4 at $SRC_DIR/core/src/ops/function.rs:LL:COL + } + + bb1: { -+ StorageDead(_7); // scope 2 at $DIR/inline_diverging.rs:28:15: 28:16 -+ StorageDead(_6); // scope 2 at $DIR/inline_diverging.rs:28:15: 28:16 -+ StorageLive(_8); // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 -+ _8 = move _3; // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 ++ StorageDead(_5); // scope 2 at $DIR/inline_diverging.rs:28:15: 28:16 ++ StorageLive(_6); // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 ++ _6 = move _3; // scope 3 at $DIR/inline_diverging.rs:29:6: 29:7 + Deinit(_1); // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 -+ (_1.0: !) = move _8; // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 -+ (_1.1: !) = move _9; // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 -+ StorageDead(_8); // scope 3 at $DIR/inline_diverging.rs:29:10: 29:11 ++ (_1.0: !) = move _6; // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 ++ (_1.1: !) = move _7; // scope 3 at $DIR/inline_diverging.rs:29:5: 29:11 ++ StorageDead(_6); // scope 3 at $DIR/inline_diverging.rs:29:10: 29:11 + StorageDead(_3); // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 + drop(_2) -> bb2; // scope 1 at $DIR/inline_diverging.rs:30:1: 30:2 + } @@ -73,12 +72,11 @@ + } + + bb6: { -+ StorageDead(_5); // scope 1 at $DIR/inline_diverging.rs:27:15: 27:16 ++ StorageDead(_8); // scope 1 at $DIR/inline_diverging.rs:27:13: 27:16 + StorageDead(_4); // scope 1 at $DIR/inline_diverging.rs:27:15: 27:16 -+ StorageLive(_6); // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 -+ _6 = &_2; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 -+ StorageLive(_7); // scope 2 at $DIR/inline_diverging.rs:28:13: 28:16 -+ _9 = ! {sleep} as Fn<()>>::call(move _6, move _7) -> [return: bb1, unwind: bb3]; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:16 ++ StorageLive(_5); // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 ++ _5 = &_2; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:14 ++ _7 = ! {sleep} as Fn<()>>::call(move _5, const ()) -> [return: bb1, unwind: bb3]; // scope 2 at $DIR/inline_diverging.rs:28:13: 28:16 + // mir::Constant + // + span: $DIR/inline_diverging.rs:28:13: 28:14 + // + literal: Const { ty: for<'a> extern "rust-call" fn(&'a fn() -> ! {sleep}, ()) -> ! {sleep} as FnOnce<()>>::Output { ! {sleep} as Fn<()>>::call}, val: Value() } diff --git a/tests/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir b/tests/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir index d99ae1a6c7c83..97081effdd0bc 100644 --- a/tests/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir +++ b/tests/mir-opt/inline/issue_76997_inline_scopes_parenting.main.Inline.after.mir @@ -10,10 +10,9 @@ fn main() -> () { scope 1 { debug f => _1; // in scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:9: +1:10 scope 2 (inlined main::{closure#0}) { // at $DIR/issue_76997_inline_scopes_parenting.rs:6:5: 6:10 - debug x => _5; // in scope 2 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:14: +1:15 - let _6: (); // in scope 2 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:23: +1:24 + debug x => const (); // in scope 2 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:14: +1:15 scope 3 { - debug y => _6; // in scope 3 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:23: +1:24 + debug y => const (); // in scope 3 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:23: +1:24 } } } @@ -30,8 +29,6 @@ fn main() -> () { (_3.0: ()) = move _4; // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:5: +2:10 StorageLive(_5); // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:5: +2:10 _5 = move (_3.0: ()); // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:5: +2:10 - StorageLive(_6); // scope 2 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:23: +1:24 - StorageDead(_6); // scope 2 at $DIR/issue_76997_inline_scopes_parenting.rs:+1:32: +1:33 StorageDead(_5); // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:5: +2:10 StorageDead(_4); // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:9: +2:10 StorageDead(_3); // scope 1 at $DIR/issue_76997_inline_scopes_parenting.rs:+2:9: +2:10 diff --git a/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir b/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir index b672e1a6e6397..9d6e71ab78a9a 100644 --- a/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir +++ b/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir @@ -2,26 +2,23 @@ fn f_unit() -> () { let mut _0: (); // return place in scope 0 at $DIR/lower_intrinsics_e2e.rs:+0:17: +0:17 - let mut _1: (); // in scope 0 at $DIR/lower_intrinsics_e2e.rs:+1:16: +1:18 scope 1 (inlined f_dispatch::<()>) { // at $DIR/lower_intrinsics_e2e.rs:9:5: 9:19 - debug t => _1; // in scope 1 at $DIR/lower_intrinsics_e2e.rs:19:22: 19:23 - let _2: (); // in scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 + debug t => const (); // in scope 1 at $DIR/lower_intrinsics_e2e.rs:19:22: 19:23 + let _1: (); // in scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 scope 2 (inlined std::mem::size_of::<()>) { // at $DIR/lower_intrinsics_e2e.rs:20:8: 20:32 } } bb0: { - StorageLive(_1); // scope 0 at $DIR/lower_intrinsics_e2e.rs:+1:16: +1:18 - StorageLive(_2); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 - _2 = f_zst::<()>(move _1) -> bb1; // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 + StorageLive(_1); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 + _1 = f_zst::<()>(const ()) -> bb1; // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 // mir::Constant // + span: $DIR/lower_intrinsics_e2e.rs:21:9: 21:14 // + literal: Const { ty: fn(()) {f_zst::<()>}, val: Value() } } bb1: { - StorageDead(_2); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:17: 21:18 - StorageDead(_1); // scope 0 at $DIR/lower_intrinsics_e2e.rs:+1:18: +1:19 + StorageDead(_1); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:17: 21:18 return; // scope 0 at $DIR/lower_intrinsics_e2e.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/simple_option_map_e2e.ezmap.PreCodegen.after.mir b/tests/mir-opt/simple_option_map_e2e.ezmap.PreCodegen.after.mir index 916f99049c60a..5ead390e9ffc3 100644 --- a/tests/mir-opt/simple_option_map_e2e.ezmap.PreCodegen.after.mir +++ b/tests/mir-opt/simple_option_map_e2e.ezmap.PreCodegen.after.mir @@ -3,31 +3,29 @@ fn ezmap(_1: Option) -> Option { debug x => _1; // in scope 0 at $DIR/simple_option_map_e2e.rs:+0:14: +0:15 let mut _0: std::option::Option; // return place in scope 0 at $DIR/simple_option_map_e2e.rs:+0:33: +0:44 - let mut _2: [closure@$DIR/simple_option_map_e2e.rs:14:12: 14:15]; // in scope 0 at $DIR/simple_option_map_e2e.rs:+1:12: +1:21 scope 1 (inlined map::) { // at $DIR/simple_option_map_e2e.rs:14:5: 14:22 debug slf => _1; // in scope 1 at $DIR/simple_option_map_e2e.rs:2:17: 2:20 - debug f => _2; // in scope 1 at $DIR/simple_option_map_e2e.rs:2:33: 2:34 - let mut _3: isize; // in scope 1 at $DIR/simple_option_map_e2e.rs:7:9: 7:16 + debug f => const ZeroSized: [closure@$DIR/simple_option_map_e2e.rs:14:12: 14:15]; // in scope 1 at $DIR/simple_option_map_e2e.rs:2:33: 2:34 + let mut _2: isize; // in scope 1 at $DIR/simple_option_map_e2e.rs:7:9: 7:16 + let mut _3: i32; // in scope 1 at $DIR/simple_option_map_e2e.rs:7:25: 7:29 let mut _4: i32; // in scope 1 at $DIR/simple_option_map_e2e.rs:7:25: 7:29 - let mut _5: i32; // in scope 1 at $DIR/simple_option_map_e2e.rs:7:25: 7:29 scope 2 { - debug x => _5; // in scope 2 at $DIR/simple_option_map_e2e.rs:7:14: 7:15 + debug x => _4; // in scope 2 at $DIR/simple_option_map_e2e.rs:7:14: 7:15 scope 3 (inlined ezmap::{closure#0}) { // at $DIR/simple_option_map_e2e.rs:7:25: 7:29 - debug n => _5; // in scope 3 at $DIR/simple_option_map_e2e.rs:+1:13: +1:14 + debug n => _4; // in scope 3 at $DIR/simple_option_map_e2e.rs:+1:13: +1:14 } } } bb0: { - StorageLive(_2); // scope 0 at $DIR/simple_option_map_e2e.rs:+1:12: +1:21 - _3 = discriminant(_1); // scope 1 at $DIR/simple_option_map_e2e.rs:6:11: 6:14 - switchInt(move _3) -> [0: bb1, 1: bb3, otherwise: bb2]; // scope 1 at $DIR/simple_option_map_e2e.rs:6:5: 6:14 + _2 = discriminant(_1); // scope 1 at $DIR/simple_option_map_e2e.rs:6:11: 6:14 + switchInt(move _2) -> [0: bb1, 1: bb3, otherwise: bb2]; // scope 1 at $DIR/simple_option_map_e2e.rs:6:5: 6:14 } bb1: { Deinit(_0); // scope 1 at $DIR/simple_option_map_e2e.rs:8:17: 8:21 discriminant(_0) = 0; // scope 1 at $DIR/simple_option_map_e2e.rs:8:17: 8:21 - goto -> bb4; // scope 1 at $DIR/simple_option_map_e2e.rs:8:17: 8:21 + return; // scope 1 at $DIR/simple_option_map_e2e.rs:8:17: 8:21 } bb2: { @@ -35,18 +33,13 @@ fn ezmap(_1: Option) -> Option { } bb3: { - _5 = move ((_1 as Some).0: i32); // scope 1 at $DIR/simple_option_map_e2e.rs:7:14: 7:15 - StorageLive(_4); // scope 2 at $DIR/simple_option_map_e2e.rs:7:25: 7:29 - _4 = Add(move _5, const 1_i32); // scope 3 at $DIR/simple_option_map_e2e.rs:+1:16: +1:21 + _4 = move ((_1 as Some).0: i32); // scope 1 at $DIR/simple_option_map_e2e.rs:7:14: 7:15 + StorageLive(_3); // scope 2 at $DIR/simple_option_map_e2e.rs:7:25: 7:29 + _3 = Add(move _4, const 1_i32); // scope 3 at $DIR/simple_option_map_e2e.rs:+1:16: +1:21 Deinit(_0); // scope 2 at $DIR/simple_option_map_e2e.rs:7:20: 7:30 - ((_0 as Some).0: i32) = move _4; // scope 2 at $DIR/simple_option_map_e2e.rs:7:20: 7:30 + ((_0 as Some).0: i32) = move _3; // scope 2 at $DIR/simple_option_map_e2e.rs:7:20: 7:30 discriminant(_0) = 1; // scope 2 at $DIR/simple_option_map_e2e.rs:7:20: 7:30 - StorageDead(_4); // scope 2 at $DIR/simple_option_map_e2e.rs:7:29: 7:30 - goto -> bb4; // scope 1 at $DIR/simple_option_map_e2e.rs:10:1: 10:2 - } - - bb4: { - StorageDead(_2); // scope 0 at $DIR/simple_option_map_e2e.rs:+1:21: +1:22 - return; // scope 0 at $DIR/simple_option_map_e2e.rs:+2:2: +2:2 + StorageDead(_3); // scope 2 at $DIR/simple_option_map_e2e.rs:7:29: 7:30 + return; // scope 1 at $DIR/simple_option_map_e2e.rs:10:1: 10:2 } } From 2a3b0e35a5726021183b915a9fb049c0331b6cc8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 25 Jan 2023 16:54:45 +0000 Subject: [PATCH 099/101] Rename method. --- compiler/rustc_mir_transform/src/remove_zsts.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/rustc_mir_transform/src/remove_zsts.rs b/compiler/rustc_mir_transform/src/remove_zsts.rs index 6d19f8a94ea43..f0a1e08d2bc29 100644 --- a/compiler/rustc_mir_transform/src/remove_zsts.rs +++ b/compiler/rustc_mir_transform/src/remove_zsts.rs @@ -1,4 +1,4 @@ -//! Removes assignments to ZST places. +//! Removes operations on ZST places, and convert ZST operands to constants. use crate::MirPass; use rustc_middle::mir::interpret::ConstValue; @@ -53,7 +53,7 @@ fn maybe_zst(ty: Ty<'_>) -> bool { } impl<'tcx> Replacer<'_, 'tcx> { - fn is_zst(&self, ty: Ty<'tcx>) -> bool { + fn known_to_be_zst(&self, ty: Ty<'tcx>) -> bool { if !maybe_zst(ty) { return false; } @@ -64,7 +64,7 @@ impl<'tcx> Replacer<'_, 'tcx> { } fn make_zst(&self, ty: Ty<'tcx>) -> Constant<'tcx> { - debug_assert!(self.is_zst(ty)); + debug_assert!(self.known_to_be_zst(ty)); Constant { span: rustc_span::DUMMY_SP, user_ty: None, @@ -83,12 +83,12 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { VarDebugInfoContents::Const(_) => {} VarDebugInfoContents::Place(place) => { let place_ty = place.ty(self.local_decls, self.tcx).ty; - if self.is_zst(place_ty) { + if self.known_to_be_zst(place_ty) { var_debug_info.value = VarDebugInfoContents::Const(self.make_zst(place_ty)) } } VarDebugInfoContents::Composite { ty, fragments: _ } => { - if self.is_zst(ty) { + if self.known_to_be_zst(ty) { var_debug_info.value = VarDebugInfoContents::Const(self.make_zst(ty)) } } @@ -100,7 +100,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { return; } let op_ty = operand.ty(self.local_decls, self.tcx); - if self.is_zst(op_ty) + if self.known_to_be_zst(op_ty) && self.tcx.consider_optimizing(|| { format!("RemoveZsts - Operand: {:?} Location: {:?}", operand, loc) }) @@ -114,7 +114,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { statement.kind { let place_ty = place.ty(self.local_decls, self.tcx).ty; - if self.is_zst(place_ty) + if self.known_to_be_zst(place_ty) && self.tcx.consider_optimizing(|| { format!( "RemoveZsts - Place: {:?} SourceInfo: {:?}", From 49f4399c9b29d399a14c33e2c9ab8ec05442928c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 25 Jan 2023 16:55:30 +0000 Subject: [PATCH 100/101] Generalize operation. --- .../rustc_mir_transform/src/remove_zsts.rs | 37 +++++++----- ...l_flow_simplification.hello.ConstProp.diff | 1 - .../invalid_constant.main.RemoveZsts.diff | 18 ++++-- .../issue_66971.main.ConstProp.diff | 2 - .../issue_67019.main.ConstProp.diff | 2 - ...ar_literal_propagation.main.ConstProp.diff | 2 - ...le_literal_propagation.main.ConstProp.diff | 2 - .../inline/asm_unwind.main.Inline.diff | 14 ++--- tests/mir-opt/inline/cycle.g.Inline.diff | 4 +- tests/mir-opt/inline/cycle.main.Inline.diff | 4 +- .../exponential_runtime.main.Inline.diff | 56 +++++++++---------- .../inline/inline_cycle.two.Inline.diff | 4 +- .../inline/inline_diverging.g.Inline.diff | 2 +- .../inline_options.main.Inline.after.mir | 32 +++++------ ...76432.test.SimplifyComparisonIntegral.diff | 1 - ...ue_59352.num_to_digit.PreCodegen.after.mir | 1 - ..._intrinsics_e2e.f_u64.PreCodegen.after.mir | 2 - ...intrinsics_e2e.f_unit.PreCodegen.after.mir | 2 - ...annot_opt_generic.RemoveUnneededDrops.diff | 4 +- ...ed_drops.dont_opt.RemoveUnneededDrops.diff | 4 +- ...nneeded_drops.opt.RemoveUnneededDrops.diff | 4 +- ....opt_generic_copy.RemoveUnneededDrops.diff | 4 +- .../remove_zsts.get_union.RemoveZsts.diff | 6 +- ...mplifyConstCondition-after-const-prop.diff | 2 - 24 files changed, 105 insertions(+), 105 deletions(-) diff --git a/compiler/rustc_mir_transform/src/remove_zsts.rs b/compiler/rustc_mir_transform/src/remove_zsts.rs index f0a1e08d2bc29..86706c5a7c7f1 100644 --- a/compiler/rustc_mir_transform/src/remove_zsts.rs +++ b/compiler/rustc_mir_transform/src/remove_zsts.rs @@ -110,21 +110,30 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'_, 'tcx> { } fn visit_statement(&mut self, statement: &mut Statement<'tcx>, loc: Location) { - if let StatementKind::Assign(box (place, _)) | StatementKind::Deinit(box place) = - statement.kind - { - let place_ty = place.ty(self.local_decls, self.tcx).ty; - if self.known_to_be_zst(place_ty) - && self.tcx.consider_optimizing(|| { - format!( - "RemoveZsts - Place: {:?} SourceInfo: {:?}", - place, statement.source_info - ) - }) - { - statement.make_nop(); + let place_for_ty = match statement.kind { + StatementKind::Assign(box (place, ref rvalue)) => { + rvalue.is_safe_to_remove().then_some(place) + } + StatementKind::Deinit(box place) + | StatementKind::SetDiscriminant { box place, variant_index: _ } + | StatementKind::AscribeUserType(box (place, _), _) + | StatementKind::Retag(_, box place) + | StatementKind::FakeRead(box (_, place)) => Some(place), + StatementKind::StorageLive(local) | StatementKind::StorageDead(local) => { + Some(local.into()) } + StatementKind::Coverage(_) | StatementKind::Intrinsic(_) | StatementKind::Nop => None, + }; + if let Some(place_for_ty) = place_for_ty + && let ty = place_for_ty.ty(self.local_decls, self.tcx).ty + && self.known_to_be_zst(ty) + && self.tcx.consider_optimizing(|| { + format!("RemoveZsts - Place: {:?} SourceInfo: {:?}", place_for_ty, statement.source_info) + }) + { + statement.make_nop(); + } else { + self.super_statement(statement, loc); } - self.super_statement(statement, loc); } } diff --git a/tests/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff b/tests/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff index f270ab8b69f0e..7e77c18d5758e 100644 --- a/tests/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff +++ b/tests/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff @@ -14,7 +14,6 @@ } bb1: { - StorageLive(_2); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL _2 = begin_panic::<&str>(const "explicit panic"); // scope 0 at $SRC_DIR/std/src/panic.rs:LL:COL // mir::Constant // + span: $SRC_DIR/std/src/panic.rs:LL:COL diff --git a/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff b/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff index 2d63101173719..ef9e33bbb062f 100644 --- a/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff +++ b/tests/mir-opt/const_prop/invalid_constant.main.RemoveZsts.diff @@ -49,22 +49,28 @@ _3 = [move _4]; // scope 1 at $DIR/invalid_constant.rs:+13:24: +13:60 StorageDead(_4); // scope 1 at $DIR/invalid_constant.rs:+13:59: +13:60 StorageDead(_5); // scope 1 at $DIR/invalid_constant.rs:+13:60: +13:61 - StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 - StorageLive(_7); // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73 +- StorageLive(_6); // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 +- StorageLive(_7); // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73 ++ nop; // scope 3 at $DIR/invalid_constant.rs:+20:9: +20:31 ++ nop; // scope 3 at $DIR/invalid_constant.rs:+20:35: +20:73 StorageLive(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 Deinit(_8); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 (_8.0: u32) = const 0_u32; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:65 - _7 = (_8.1: Empty); // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71 - _6 = [move _7]; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74 +- StorageDead(_7); // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74 + nop; // scope 6 at $DIR/invalid_constant.rs:+20:44: +20:71 + nop; // scope 3 at $DIR/invalid_constant.rs:+20:34: +20:74 - StorageDead(_7); // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74 ++ nop; // scope 3 at $DIR/invalid_constant.rs:+20:73: +20:74 StorageDead(_8); // scope 3 at $DIR/invalid_constant.rs:+20:74: +20:75 - StorageLive(_9); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 +- StorageLive(_9); // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 - _0 = const (); // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2 +- StorageDead(_9); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2 +- StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2 ++ nop; // scope 5 at $DIR/invalid_constant.rs:+24:9: +24:22 + nop; // scope 0 at $DIR/invalid_constant.rs:+0:11: +27:2 - StorageDead(_9); // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2 - StorageDead(_6); // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2 ++ nop; // scope 5 at $DIR/invalid_constant.rs:+27:1: +27:2 ++ nop; // scope 3 at $DIR/invalid_constant.rs:+27:1: +27:2 StorageDead(_3); // scope 1 at $DIR/invalid_constant.rs:+27:1: +27:2 StorageDead(_1); // scope 0 at $DIR/invalid_constant.rs:+27:1: +27:2 return; // scope 0 at $DIR/invalid_constant.rs:+27:2: +27:2 diff --git a/tests/mir-opt/const_prop/issue_66971.main.ConstProp.diff b/tests/mir-opt/const_prop/issue_66971.main.ConstProp.diff index 488e772d0ea8b..ef0f8ca706143 100644 --- a/tests/mir-opt/const_prop/issue_66971.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/issue_66971.main.ConstProp.diff @@ -7,7 +7,6 @@ let mut _2: ((), u8, u8); // in scope 0 at $DIR/issue_66971.rs:+1:12: +1:22 bb0: { - StorageLive(_1); // scope 0 at $DIR/issue_66971.rs:+1:5: +1:23 StorageLive(_2); // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22 Deinit(_2); // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22 (_2.1: u8) = const 0_u8; // scope 0 at $DIR/issue_66971.rs:+1:12: +1:22 @@ -20,7 +19,6 @@ bb1: { StorageDead(_2); // scope 0 at $DIR/issue_66971.rs:+1:22: +1:23 - StorageDead(_1); // scope 0 at $DIR/issue_66971.rs:+1:23: +1:24 return; // scope 0 at $DIR/issue_66971.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff b/tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff index cd53048597b82..f45b0eac82794 100644 --- a/tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/issue_67019.main.ConstProp.diff @@ -8,7 +8,6 @@ let mut _3: (u8, u8); // in scope 0 at $DIR/issue_67019.rs:+1:11: +1:17 bb0: { - StorageLive(_1); // scope 0 at $DIR/issue_67019.rs:+1:5: +1:20 StorageLive(_2); // scope 0 at $DIR/issue_67019.rs:+1:10: +1:19 StorageLive(_3); // scope 0 at $DIR/issue_67019.rs:+1:11: +1:17 Deinit(_3); // scope 0 at $DIR/issue_67019.rs:+1:11: +1:17 @@ -26,7 +25,6 @@ bb1: { StorageDead(_2); // scope 0 at $DIR/issue_67019.rs:+1:19: +1:20 - StorageDead(_1); // scope 0 at $DIR/issue_67019.rs:+1:20: +1:21 return; // scope 0 at $DIR/issue_67019.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff b/tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff index d518eff04eba2..d118b02127b1a 100644 --- a/tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/scalar_literal_propagation.main.ConstProp.diff @@ -13,7 +13,6 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/scalar_literal_propagation.rs:+1:9: +1:10 _1 = const 1_u32; // scope 0 at $DIR/scalar_literal_propagation.rs:+1:13: +1:14 - StorageLive(_2); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:5: +2:15 StorageLive(_3); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:13: +2:14 - _3 = _1; // scope 1 at $DIR/scalar_literal_propagation.rs:+2:13: +2:14 - _2 = consume(move _3) -> bb1; // scope 1 at $DIR/scalar_literal_propagation.rs:+2:5: +2:15 @@ -26,7 +25,6 @@ bb1: { StorageDead(_3); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:14: +2:15 - StorageDead(_2); // scope 1 at $DIR/scalar_literal_propagation.rs:+2:15: +2:16 StorageDead(_1); // scope 0 at $DIR/scalar_literal_propagation.rs:+3:1: +3:2 return; // scope 0 at $DIR/scalar_literal_propagation.rs:+3:2: +3:2 } diff --git a/tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff b/tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff index e4c92b617c6a3..83af9841e0651 100644 --- a/tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff +++ b/tests/mir-opt/const_prop/tuple_literal_propagation.main.ConstProp.diff @@ -15,7 +15,6 @@ Deinit(_1); // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19 (_1.0: u32) = const 1_u32; // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19 (_1.1: u32) = const 2_u32; // scope 0 at $DIR/tuple_literal_propagation.rs:+1:13: +1:19 - StorageLive(_2); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:5: +3:15 StorageLive(_3); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14 - _3 = _1; // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14 + _3 = const (1_u32, 2_u32); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:13: +3:14 @@ -27,7 +26,6 @@ bb1: { StorageDead(_3); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:14: +3:15 - StorageDead(_2); // scope 1 at $DIR/tuple_literal_propagation.rs:+3:15: +3:16 StorageDead(_1); // scope 0 at $DIR/tuple_literal_propagation.rs:+4:1: +4:2 return; // scope 0 at $DIR/tuple_literal_propagation.rs:+4:2: +4:2 } diff --git a/tests/mir-opt/inline/asm_unwind.main.Inline.diff b/tests/mir-opt/inline/asm_unwind.main.Inline.diff index a4193290a9521..ed290063a9305 100644 --- a/tests/mir-opt/inline/asm_unwind.main.Inline.diff +++ b/tests/mir-opt/inline/asm_unwind.main.Inline.diff @@ -19,21 +19,21 @@ - // mir::Constant - // + span: $DIR/asm_unwind.rs:21:5: 21:8 - // + literal: Const { ty: fn() {foo}, val: Value() } -+ StorageLive(_2); // scope 1 at $DIR/asm_unwind.rs:15:9: 15:11 -+ asm!("", options(MAY_UNWIND)) -> [return: bb1, unwind: bb3]; // scope 3 at $DIR/asm_unwind.rs:16:14: 16:54 ++ StorageLive(_2); // scope 0 at $DIR/asm_unwind.rs:+1:5: +1:10 ++ asm!("", options(MAY_UNWIND)) -> [return: bb2, unwind: bb3]; // scope 3 at $DIR/asm_unwind.rs:16:14: 16:54 } bb1: { -+ drop(_2) -> bb2; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2 -+ } -+ -+ bb2: { -+ StorageDead(_2); // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2 ++ StorageDead(_2); // scope 0 at $DIR/asm_unwind.rs:+1:5: +1:10 StorageDead(_1); // scope 0 at $DIR/asm_unwind.rs:+1:10: +1:11 _0 = const (); // scope 0 at $DIR/asm_unwind.rs:+0:15: +2:2 return; // scope 0 at $DIR/asm_unwind.rs:+2:2: +2:2 + } + ++ bb2: { ++ drop(_2) -> bb1; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2 ++ } ++ + bb3 (cleanup): { + drop(_2) -> bb4; // scope 1 at $DIR/asm_unwind.rs:17:1: 17:2 + } diff --git a/tests/mir-opt/inline/cycle.g.Inline.diff b/tests/mir-opt/inline/cycle.g.Inline.diff index d1c9f2ff37945..20d313aecf588 100644 --- a/tests/mir-opt/inline/cycle.g.Inline.diff +++ b/tests/mir-opt/inline/cycle.g.Inline.diff @@ -25,7 +25,7 @@ - // mir::Constant // + span: $DIR/cycle.rs:12:7: 12:11 // + literal: Const { ty: fn() {main}, val: Value() } -+ StorageLive(_3); // scope 1 at $DIR/cycle.rs:6:5: 6:8 ++ StorageLive(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:12 + StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6 + _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6 + StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 @@ -34,6 +34,7 @@ } bb1: { ++ StorageDead(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:12 + StorageDead(_2); // scope 0 at $DIR/cycle.rs:+1:5: +1:12 StorageDead(_1); // scope 0 at $DIR/cycle.rs:+1:12: +1:13 _0 = const (); // scope 0 at $DIR/cycle.rs:+0:8: +2:2 @@ -51,7 +52,6 @@ + bb4: { + StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8 -+ StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9 + drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2 } } diff --git a/tests/mir-opt/inline/cycle.main.Inline.diff b/tests/mir-opt/inline/cycle.main.Inline.diff index 03035c1e73f20..dacc5f4be9d7f 100644 --- a/tests/mir-opt/inline/cycle.main.Inline.diff +++ b/tests/mir-opt/inline/cycle.main.Inline.diff @@ -25,7 +25,7 @@ - // mir::Constant // + span: $DIR/cycle.rs:17:7: 17:8 // + literal: Const { ty: fn() {g}, val: Value() } -+ StorageLive(_3); // scope 1 at $DIR/cycle.rs:6:5: 6:8 ++ StorageLive(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:9 + StorageLive(_4); // scope 1 at $DIR/cycle.rs:6:5: 6:6 + _4 = &_2; // scope 1 at $DIR/cycle.rs:6:5: 6:6 + StorageLive(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 @@ -34,6 +34,7 @@ } bb1: { ++ StorageDead(_3); // scope 0 at $DIR/cycle.rs:+1:5: +1:9 + StorageDead(_2); // scope 0 at $DIR/cycle.rs:+1:5: +1:9 StorageDead(_1); // scope 0 at $DIR/cycle.rs:+1:9: +1:10 _0 = const (); // scope 0 at $DIR/cycle.rs:+0:11: +2:2 @@ -51,7 +52,6 @@ + bb4: { + StorageDead(_5); // scope 1 at $DIR/cycle.rs:6:5: 6:8 + StorageDead(_4); // scope 1 at $DIR/cycle.rs:6:7: 6:8 -+ StorageDead(_3); // scope 1 at $DIR/cycle.rs:6:8: 6:9 + drop(_2) -> bb1; // scope 1 at $DIR/cycle.rs:7:1: 7:2 } } diff --git a/tests/mir-opt/inline/exponential_runtime.main.Inline.diff b/tests/mir-opt/inline/exponential_runtime.main.Inline.diff index 7fd62be7ab9c3..dd1f253cb47dd 100644 --- a/tests/mir-opt/inline/exponential_runtime.main.Inline.diff +++ b/tests/mir-opt/inline/exponential_runtime.main.Inline.diff @@ -18,9 +18,13 @@ bb0: { StorageLive(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 - _1 = <() as G>::call() -> bb1; // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 -+ StorageLive(_2); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 -+ StorageLive(_5); // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25 -+ _5 = <() as E>::call() -> bb3; // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25 ++ StorageLive(_2); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 ++ StorageLive(_3); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 ++ StorageLive(_4); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 ++ StorageLive(_5); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ StorageLive(_6); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ StorageLive(_7); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ _5 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:61:9: 61:25 // mir::Constant - // + span: $DIR/exponential_runtime.rs:86:5: 86:20 - // + literal: Const { ty: fn() {<() as G>::call}, val: Value() } @@ -29,47 +33,43 @@ } bb1: { -+ StorageDead(_3); // scope 1 at $DIR/exponential_runtime.rs:74:25: 74:26 -+ StorageLive(_4); // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 -+ _4 = <() as F>::call() -> bb2; // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 -+ // mir::Constant -+ // + span: $DIR/exponential_runtime.rs:75:9: 75:23 -+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value() } -+ } -+ -+ bb2: { -+ StorageDead(_4); // scope 1 at $DIR/exponential_runtime.rs:75:25: 75:26 ++ StorageDead(_4); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 ++ StorageDead(_3); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 ++ StorageDead(_2); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 StorageDead(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:22: +1:23 _0 = const (); // scope 0 at $DIR/exponential_runtime.rs:+0:11: +2:2 return; // scope 0 at $DIR/exponential_runtime.rs:+2:2: +2:2 + } + ++ bb2: { ++ StorageDead(_7); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ StorageDead(_6); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ StorageDead(_5); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 ++ _3 = <() as F>::call() -> bb3; // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 ++ // mir::Constant ++ // + span: $DIR/exponential_runtime.rs:74:9: 74:23 ++ // + literal: Const { ty: fn() {<() as F>::call}, val: Value() } ++ } ++ + bb3: { -+ StorageDead(_5); // scope 2 at $DIR/exponential_runtime.rs:61:25: 61:26 -+ StorageLive(_6); // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 -+ _6 = <() as E>::call() -> bb4; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 ++ _4 = <() as F>::call() -> bb1; // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 + // mir::Constant -+ // + span: $DIR/exponential_runtime.rs:62:9: 62:23 -+ // + literal: Const { ty: fn() {<() as E>::call}, val: Value() } ++ // + span: $DIR/exponential_runtime.rs:75:9: 75:23 ++ // + literal: Const { ty: fn() {<() as F>::call}, val: Value() } + } + + bb4: { -+ StorageDead(_6); // scope 2 at $DIR/exponential_runtime.rs:62:25: 62:26 -+ StorageLive(_7); // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25 -+ _7 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25 ++ _6 = <() as E>::call() -> bb5; // scope 2 at $DIR/exponential_runtime.rs:62:9: 62:25 + // mir::Constant -+ // + span: $DIR/exponential_runtime.rs:63:9: 63:23 ++ // + span: $DIR/exponential_runtime.rs:62:9: 62:23 + // + literal: Const { ty: fn() {<() as E>::call}, val: Value() } + } + + bb5: { -+ StorageDead(_7); // scope 2 at $DIR/exponential_runtime.rs:63:25: 63:26 -+ StorageDead(_2); // scope 1 at $DIR/exponential_runtime.rs:73:25: 73:26 -+ StorageLive(_3); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 -+ _3 = <() as F>::call() -> bb1; // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 ++ _7 = <() as E>::call() -> bb2; // scope 2 at $DIR/exponential_runtime.rs:63:9: 63:25 + // mir::Constant -+ // + span: $DIR/exponential_runtime.rs:74:9: 74:23 -+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value() } ++ // + span: $DIR/exponential_runtime.rs:63:9: 63:23 ++ // + literal: Const { ty: fn() {<() as E>::call}, val: Value() } } } diff --git a/tests/mir-opt/inline/inline_cycle.two.Inline.diff b/tests/mir-opt/inline/inline_cycle.two.Inline.diff index 5c395a3b24c65..0215b3d93f9e3 100644 --- a/tests/mir-opt/inline/inline_cycle.two.Inline.diff +++ b/tests/mir-opt/inline/inline_cycle.two.Inline.diff @@ -24,7 +24,7 @@ - // mir::Constant // + span: $DIR/inline_cycle.rs:49:10: 49:11 // + literal: Const { ty: fn() {f}, val: Value() } -+ StorageLive(_3); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 ++ StorageLive(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 + StorageLive(_4); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + _4 = const (); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 + _3 = move _2() -> bb1; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL @@ -32,7 +32,7 @@ bb1: { + StorageDead(_4); // scope 1 at $DIR/inline_cycle.rs:54:5: 54:8 -+ StorageDead(_3); // scope 1 at $DIR/inline_cycle.rs:54:8: 54:9 ++ StorageDead(_3); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 + StorageDead(_2); // scope 0 at $DIR/inline_cycle.rs:+1:5: +1:12 StorageDead(_1); // scope 0 at $DIR/inline_cycle.rs:+1:12: +1:13 _0 = const (); // scope 0 at $DIR/inline_cycle.rs:+0:10: +2:2 diff --git a/tests/mir-opt/inline/inline_diverging.g.Inline.diff b/tests/mir-opt/inline/inline_diverging.g.Inline.diff index b787a19f4b21c..4f22ad43700ac 100644 --- a/tests/mir-opt/inline/inline_diverging.g.Inline.diff +++ b/tests/mir-opt/inline/inline_diverging.g.Inline.diff @@ -34,7 +34,7 @@ bb2: { StorageLive(_6); // scope 0 at $DIR/inline_diverging.rs:+4:9: +4:16 - _6 = panic(); // scope 0 at $DIR/inline_diverging.rs:+4:9: +4:16 -+ StorageLive(_7); // scope 1 at $SRC_DIR/std/src/panic.rs:LL:COL ++ StorageLive(_7); // scope 0 at $DIR/inline_diverging.rs:+4:9: +4:16 + _7 = begin_panic::<&str>(const "explicit panic"); // scope 1 at $SRC_DIR/std/src/panic.rs:LL:COL // mir::Constant - // + span: $DIR/inline_diverging.rs:16:9: 16:14 diff --git a/tests/mir-opt/inline/inline_options.main.Inline.after.mir b/tests/mir-opt/inline/inline_options.main.Inline.after.mir index 1c590be945c9e..abe26bd8ce363 100644 --- a/tests/mir-opt/inline/inline_options.main.Inline.after.mir +++ b/tests/mir-opt/inline/inline_options.main.Inline.after.mir @@ -21,35 +21,35 @@ fn main() -> () { bb1: { StorageDead(_1); // scope 0 at $DIR/inline_options.rs:+1:18: +1:19 StorageLive(_2); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 - StorageLive(_3); // scope 1 at $DIR/inline_options.rs:16:23: 16:26 - _3 = g() -> bb2; // scope 1 at $DIR/inline_options.rs:16:23: 16:26 + StorageLive(_3); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + StorageLive(_4); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + StorageLive(_5); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + _3 = g() -> bb3; // scope 1 at $DIR/inline_options.rs:16:23: 16:26 // mir::Constant // + span: $DIR/inline_options.rs:16:23: 16:24 // + literal: Const { ty: fn() {g}, val: Value() } } bb2: { - StorageDead(_3); // scope 1 at $DIR/inline_options.rs:16:26: 16:27 - StorageLive(_4); // scope 1 at $DIR/inline_options.rs:16:28: 16:31 - _4 = g() -> bb3; // scope 1 at $DIR/inline_options.rs:16:28: 16:31 - // mir::Constant - // + span: $DIR/inline_options.rs:16:28: 16:29 - // + literal: Const { ty: fn() {g}, val: Value() } + StorageDead(_5); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + StorageDead(_4); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + StorageDead(_3); // scope 0 at $DIR/inline_options.rs:+2:5: +2:21 + StorageDead(_2); // scope 0 at $DIR/inline_options.rs:+2:21: +2:22 + _0 = const (); // scope 0 at $DIR/inline_options.rs:+0:11: +3:2 + return; // scope 0 at $DIR/inline_options.rs:+3:2: +3:2 } bb3: { - StorageDead(_4); // scope 1 at $DIR/inline_options.rs:16:31: 16:32 - StorageLive(_5); // scope 1 at $DIR/inline_options.rs:16:33: 16:36 - _5 = g() -> bb4; // scope 1 at $DIR/inline_options.rs:16:33: 16:36 + _4 = g() -> bb4; // scope 1 at $DIR/inline_options.rs:16:28: 16:31 // mir::Constant - // + span: $DIR/inline_options.rs:16:33: 16:34 + // + span: $DIR/inline_options.rs:16:28: 16:29 // + literal: Const { ty: fn() {g}, val: Value() } } bb4: { - StorageDead(_5); // scope 1 at $DIR/inline_options.rs:16:36: 16:37 - StorageDead(_2); // scope 0 at $DIR/inline_options.rs:+2:21: +2:22 - _0 = const (); // scope 0 at $DIR/inline_options.rs:+0:11: +3:2 - return; // scope 0 at $DIR/inline_options.rs:+3:2: +3:2 + _5 = g() -> bb2; // scope 1 at $DIR/inline_options.rs:16:33: 16:36 + // mir::Constant + // + span: $DIR/inline_options.rs:16:33: 16:34 + // + literal: Const { ty: fn() {g}, val: Value() } } } diff --git a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff index c24543daeacb7..5721237d48536 100644 --- a/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff +++ b/tests/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff @@ -56,7 +56,6 @@ } bb1: { - StorageLive(_15); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL _15 = core::panicking::panic(const "internal error: entered unreachable code"); // scope 1 at $SRC_DIR/core/src/panic.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/panic.rs:LL:COL diff --git a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir index c17d221f86a84..9df508cc71fa0 100644 --- a/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir +++ b/tests/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir @@ -70,7 +70,6 @@ fn num_to_digit(_1: char) -> u32 { } bb6: { - StorageLive(_8); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL _8 = core::panicking::panic(const "called `Option::unwrap()` on a `None` value"); // scope 3 at $SRC_DIR/core/src/option.rs:LL:COL // mir::Constant // + span: $SRC_DIR/core/src/option.rs:LL:COL diff --git a/tests/mir-opt/lower_intrinsics_e2e.f_u64.PreCodegen.after.mir b/tests/mir-opt/lower_intrinsics_e2e.f_u64.PreCodegen.after.mir index f6d8bdd742289..adfc6b2731c1a 100644 --- a/tests/mir-opt/lower_intrinsics_e2e.f_u64.PreCodegen.after.mir +++ b/tests/mir-opt/lower_intrinsics_e2e.f_u64.PreCodegen.after.mir @@ -12,7 +12,6 @@ fn f_u64() -> () { bb0: { StorageLive(_1); // scope 0 at $DIR/lower_intrinsics_e2e.rs:+1:5: +1:21 - StorageLive(_2); // scope 1 at $DIR/lower_intrinsics_e2e.rs:23:9: 23:21 _2 = f_non_zst::(const 0_u64) -> bb1; // scope 1 at $DIR/lower_intrinsics_e2e.rs:23:9: 23:21 // mir::Constant // + span: $DIR/lower_intrinsics_e2e.rs:23:9: 23:18 @@ -20,7 +19,6 @@ fn f_u64() -> () { } bb1: { - StorageDead(_2); // scope 1 at $DIR/lower_intrinsics_e2e.rs:23:21: 23:22 StorageDead(_1); // scope 0 at $DIR/lower_intrinsics_e2e.rs:+1:5: +1:21 return; // scope 0 at $DIR/lower_intrinsics_e2e.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir b/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir index 9d6e71ab78a9a..302ca09aac4b2 100644 --- a/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir +++ b/tests/mir-opt/lower_intrinsics_e2e.f_unit.PreCodegen.after.mir @@ -10,7 +10,6 @@ fn f_unit() -> () { } bb0: { - StorageLive(_1); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 _1 = f_zst::<()>(const ()) -> bb1; // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:9: 21:17 // mir::Constant // + span: $DIR/lower_intrinsics_e2e.rs:21:9: 21:14 @@ -18,7 +17,6 @@ fn f_unit() -> () { } bb1: { - StorageDead(_1); // scope 1 at $DIR/lower_intrinsics_e2e.rs:21:17: 21:18 return; // scope 0 at $DIR/lower_intrinsics_e2e.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff index 07e4dd4181324..7713649c5b96b 100644 --- a/tests/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff +++ b/tests/mir-opt/remove_unneeded_drops.cannot_opt_generic.RemoveUnneededDrops.diff @@ -11,7 +11,7 @@ } bb0: { - StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 + nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 _3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL @@ -23,7 +23,7 @@ bb2: { StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:11: +1:12 - StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 + nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+0:32: +2:2 return; // scope 0 at $DIR/remove_unneeded_drops.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff index e809ca4e90066..533db4051ef1f 100644 --- a/tests/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff +++ b/tests/mir-opt/remove_unneeded_drops.dont_opt.RemoveUnneededDrops.diff @@ -11,7 +11,7 @@ } bb0: { - StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 + nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 _3 = move _1; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 drop(_3) -> [return: bb2, unwind: bb1]; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL @@ -23,7 +23,7 @@ bb2: { StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:11: +1:12 - StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 + nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+0:27: +2:2 return; // scope 0 at $DIR/remove_unneeded_drops.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff index 087f76dbda83b..04a2d54e9a19b 100644 --- a/tests/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff +++ b/tests/mir-opt/remove_unneeded_drops.opt.RemoveUnneededDrops.diff @@ -11,7 +11,7 @@ } bb0: { - StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 +- nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 _3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 - drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL @@ -19,7 +19,7 @@ - - bb1: { StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:11: +1:12 - StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 +- nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 - nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+0:17: +2:2 return; // scope 0 at $DIR/remove_unneeded_drops.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff b/tests/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff index 933d6895f4594..782d0c6c5f267 100644 --- a/tests/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff +++ b/tests/mir-opt/remove_unneeded_drops.opt_generic_copy.RemoveUnneededDrops.diff @@ -11,7 +11,7 @@ } bb0: { - StorageLive(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 +- nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:5: +1:12 StorageLive(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 _3 = _1; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:10: +1:11 - drop(_3) -> bb1; // scope 1 at $SRC_DIR/core/src/mem/mod.rs:LL:COL @@ -19,7 +19,7 @@ - - bb1: { StorageDead(_3); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:11: +1:12 - StorageDead(_2); // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 +- nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+1:12: +1:13 - nop; // scope 0 at $DIR/remove_unneeded_drops.rs:+0:36: +2:2 return; // scope 0 at $DIR/remove_unneeded_drops.rs:+2:2: +2:2 } diff --git a/tests/mir-opt/remove_zsts.get_union.RemoveZsts.diff b/tests/mir-opt/remove_zsts.get_union.RemoveZsts.diff index 169b7b1054b47..18b612faaacaa 100644 --- a/tests/mir-opt/remove_zsts.get_union.RemoveZsts.diff +++ b/tests/mir-opt/remove_zsts.get_union.RemoveZsts.diff @@ -6,13 +6,15 @@ let mut _1: (); // in scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 bb0: { - StorageLive(_1); // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 +- StorageLive(_1); // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 - Deinit(_1); // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 ++ nop; // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 + nop; // scope 0 at $DIR/remove_zsts.rs:+1:14: +1:16 Deinit(_0); // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18 - (_0.0: ()) = move _1; // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18 +- StorageDead(_1); // scope 0 at $DIR/remove_zsts.rs:+1:17: +1:18 + nop; // scope 0 at $DIR/remove_zsts.rs:+1:5: +1:18 - StorageDead(_1); // scope 0 at $DIR/remove_zsts.rs:+1:17: +1:18 ++ nop; // scope 0 at $DIR/remove_zsts.rs:+1:17: +1:18 return; // scope 0 at $DIR/remove_zsts.rs:+2:2: +2:2 } } diff --git a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff index f9e22866bee99..ac79e72701344 100644 --- a/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff +++ b/tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.diff @@ -14,7 +14,6 @@ } bb1: { - StorageLive(_2); // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15 _2 = noop() -> bb2; // scope 0 at $DIR/simplify_if.rs:+2:9: +2:15 // mir::Constant // + span: $DIR/simplify_if.rs:7:9: 7:13 @@ -22,7 +21,6 @@ } bb2: { - StorageDead(_2); // scope 0 at $DIR/simplify_if.rs:+2:15: +2:16 goto -> bb4; // scope 0 at $DIR/simplify_if.rs:+1:5: +3:6 } From d7f59e91e09037c3a0b6721f4f50081ee15e405a Mon Sep 17 00:00:00 2001 From: Jakob Degen Date: Thu, 26 Jan 2023 03:29:28 -0800 Subject: [PATCH 101/101] Custom mir: Add support for some remaining, easy to support constructs --- .../src/build/custom/parse/instruction.rs | 14 ++++++++++++++ library/core/src/intrinsics/mir.rs | 10 ++++++++-- .../custom/arrays.arrays.built.after.mir | 14 ++++++++++++++ tests/mir-opt/building/custom/arrays.rs | 19 +++++++++++++++++++ tests/mir-opt/building/custom/enums.rs | 1 + .../custom/enums.set_discr.built.after.mir | 5 +++-- .../custom/operators.f.built.after.mir | 8 ++++++-- tests/mir-opt/building/custom/operators.rs | 3 +++ 8 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 tests/mir-opt/building/custom/arrays.arrays.built.after.mir create mode 100644 tests/mir-opt/building/custom/arrays.rs diff --git a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs index 9840b95feefde..dbba529aef7a5 100644 --- a/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs +++ b/compiler/rustc_mir_build/src/build/custom/parse/instruction.rs @@ -18,6 +18,9 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { @call("mir_storage_dead", args) => { Ok(StatementKind::StorageDead(self.parse_local(args[0])?)) }, + @call("mir_deinit", args) => { + Ok(StatementKind::Deinit(Box::new(self.parse_place(args[0])?))) + }, @call("mir_retag", args) => { Ok(StatementKind::Retag(RetagKind::Default, Box::new(self.parse_place(args[0])?))) }, @@ -141,6 +144,14 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { fn parse_rvalue(&self, expr_id: ExprId) -> PResult> { parse_by_kind!(self, expr_id, _, "rvalue", @call("mir_discriminant", args) => self.parse_place(args[0]).map(Rvalue::Discriminant), + @call("mir_checked", args) => { + parse_by_kind!(self, args[0], _, "binary op", + ExprKind::Binary { op, lhs, rhs } => Ok(Rvalue::CheckedBinaryOp( + *op, Box::new((self.parse_operand(*lhs)?, self.parse_operand(*rhs)?)) + )), + ) + }, + @call("mir_len", args) => Ok(Rvalue::Len(self.parse_place(args[0])?)), ExprKind::Borrow { borrow_kind, arg } => Ok( Rvalue::Ref(self.tcx.lifetimes.re_erased, *borrow_kind, self.parse_place(*arg)?) ), @@ -153,6 +164,9 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> { ExprKind::Unary { op, arg } => Ok( Rvalue::UnaryOp(*op, self.parse_operand(*arg)?) ), + ExprKind::Repeat { value, count } => Ok( + Rvalue::Repeat(self.parse_operand(*value)?, *count) + ), _ => self.parse_operand(expr_id).map(Rvalue::Use), ) } diff --git a/library/core/src/intrinsics/mir.rs b/library/core/src/intrinsics/mir.rs index e3157b66902eb..3d7ccffa1735c 100644 --- a/library/core/src/intrinsics/mir.rs +++ b/library/core/src/intrinsics/mir.rs @@ -211,13 +211,16 @@ //! //! #### Statements //! - Assign statements work via normal Rust assignment. -//! - [`Retag`] statements have an associated function. +//! - [`Retag`], [`StorageLive`], [`StorageDead`], [`Deinit`] statements have an associated function. //! //! #### Rvalues //! //! - Operands implicitly convert to `Use` rvalues. //! - `&`, `&mut`, `addr_of!`, and `addr_of_mut!` all work to create their associated rvalue. -//! - [`Discriminant`] has an associated function. +//! - [`Discriminant`] and [`Len`] have associated functions. +//! - Unary and binary operations use their normal Rust syntax - `a * b`, `!c`, etc. +//! - Checked binary operations are represented by wrapping the associated binop in [`Checked`]. +//! - Array repetition syntax (`[foo; 10]`) creates the associated rvalue. //! //! #### Terminators //! @@ -261,6 +264,9 @@ define!("mir_drop_and_replace", fn DropAndReplace(place: T, value: T, goto: B define!("mir_call", fn Call(place: T, goto: BasicBlock, call: T)); define!("mir_storage_live", fn StorageLive(local: T)); define!("mir_storage_dead", fn StorageDead(local: T)); +define!("mir_deinit", fn Deinit(place: T)); +define!("mir_checked", fn Checked(binop: T) -> (T, bool)); +define!("mir_len", fn Len(place: T) -> usize); define!("mir_retag", fn Retag(place: T)); define!("mir_move", fn Move(place: T) -> T); define!("mir_static", fn Static(s: T) -> &'static T); diff --git a/tests/mir-opt/building/custom/arrays.arrays.built.after.mir b/tests/mir-opt/building/custom/arrays.arrays.built.after.mir new file mode 100644 index 0000000000000..4c92127288596 --- /dev/null +++ b/tests/mir-opt/building/custom/arrays.arrays.built.after.mir @@ -0,0 +1,14 @@ +// MIR for `arrays` after built + +fn arrays() -> usize { + let mut _0: usize; // return place in scope 0 at $DIR/arrays.rs:+0:32: +0:37 + let mut _1: [i32; C]; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + let mut _2: usize; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + + bb0: { + _1 = [const 5_i32; C]; // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _2 = Len(_1); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _0 = _2; // scope 0 at $DIR/arrays.rs:+4:9: +4:16 + return; // scope 0 at $DIR/arrays.rs:+5:9: +5:17 + } +} diff --git a/tests/mir-opt/building/custom/arrays.rs b/tests/mir-opt/building/custom/arrays.rs new file mode 100644 index 0000000000000..8e0a1fd7a4390 --- /dev/null +++ b/tests/mir-opt/building/custom/arrays.rs @@ -0,0 +1,19 @@ +#![feature(custom_mir, core_intrinsics, inline_const)] + +extern crate core; +use core::intrinsics::mir::*; + +// EMIT_MIR arrays.arrays.built.after.mir +#[custom_mir(dialect = "built")] +fn arrays() -> usize { + mir!({ + let x = [5_i32; C]; + let c = Len(x); + RET = c; + Return() + }) +} + +fn main() { + assert_eq!(arrays::<20>(), 20); +} diff --git a/tests/mir-opt/building/custom/enums.rs b/tests/mir-opt/building/custom/enums.rs index e5cd456377844..eca5b792ec0a2 100644 --- a/tests/mir-opt/building/custom/enums.rs +++ b/tests/mir-opt/building/custom/enums.rs @@ -86,6 +86,7 @@ fn switch_option_repr(option: Bool) -> bool { #[custom_mir(dialect = "runtime", phase = "initial")] fn set_discr(option: &mut Option<()>) { mir!({ + Deinit(*option); SetDiscriminant(*option, 0); Return() }) diff --git a/tests/mir-opt/building/custom/enums.set_discr.built.after.mir b/tests/mir-opt/building/custom/enums.set_discr.built.after.mir index 7de9ed0983fe8..6d07473658ace 100644 --- a/tests/mir-opt/building/custom/enums.set_discr.built.after.mir +++ b/tests/mir-opt/building/custom/enums.set_discr.built.after.mir @@ -4,7 +4,8 @@ fn set_discr(_1: &mut Option<()>) -> () { let mut _0: (); // return place in scope 0 at $DIR/enums.rs:+0:39: +0:39 bb0: { - discriminant((*_1)) = 0; // scope 0 at $DIR/enums.rs:+2:9: +2:36 - return; // scope 0 at $DIR/enums.rs:+3:9: +3:17 + Deinit((*_1)); // scope 0 at $DIR/enums.rs:+2:9: +2:24 + discriminant((*_1)) = 0; // scope 0 at $DIR/enums.rs:+3:9: +3:36 + return; // scope 0 at $DIR/enums.rs:+4:9: +4:17 } } diff --git a/tests/mir-opt/building/custom/operators.f.built.after.mir b/tests/mir-opt/building/custom/operators.f.built.after.mir index a0c5f1b40dbb7..cb43d5e6ed7c7 100644 --- a/tests/mir-opt/building/custom/operators.f.built.after.mir +++ b/tests/mir-opt/building/custom/operators.f.built.after.mir @@ -2,6 +2,7 @@ fn f(_1: i32, _2: bool) -> i32 { let mut _0: i32; // return place in scope 0 at $DIR/operators.rs:+0:30: +0:33 + let mut _3: (i32, bool); // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL bb0: { _1 = Neg(_1); // scope 0 at $DIR/operators.rs:+2:9: +2:15 @@ -20,7 +21,10 @@ fn f(_1: i32, _2: bool) -> i32 { _2 = Le(_1, _1); // scope 0 at $DIR/operators.rs:+15:9: +15:19 _2 = Ge(_1, _1); // scope 0 at $DIR/operators.rs:+16:9: +16:19 _2 = Gt(_1, _1); // scope 0 at $DIR/operators.rs:+17:9: +17:18 - _0 = _1; // scope 0 at $DIR/operators.rs:+18:9: +18:16 - return; // scope 0 at $DIR/operators.rs:+19:9: +19:17 + _3 = CheckedAdd(_1, _1); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL + _2 = (_3.1: bool); // scope 0 at $DIR/operators.rs:+19:9: +19:18 + _1 = (_3.0: i32); // scope 0 at $DIR/operators.rs:+20:9: +20:18 + _0 = _1; // scope 0 at $DIR/operators.rs:+21:9: +21:16 + return; // scope 0 at $DIR/operators.rs:+22:9: +22:17 } } diff --git a/tests/mir-opt/building/custom/operators.rs b/tests/mir-opt/building/custom/operators.rs index 51f80c66392a1..db7a48317d928 100644 --- a/tests/mir-opt/building/custom/operators.rs +++ b/tests/mir-opt/building/custom/operators.rs @@ -22,6 +22,9 @@ pub fn f(a: i32, b: bool) -> i32 { b = a <= a; b = a >= a; b = a > a; + let res = Checked(a + a); + b = res.1; + a = res.0; RET = a; Return() })