From 32202f20cdfb1e01d88d2b61edb492daf3f33e5e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 22 Apr 2022 21:11:38 +0200 Subject: [PATCH 01/25] Merge commit 'f2cdd4a78d89c009342197cf5844a21f8aa813df' into sync_cg_clif-2022-04-22 --- Cargo.lock | 40 +++++++------- Cargo.toml | 12 ++--- build_sysroot/Cargo.lock | 9 ++-- example/float-minmax-pass.rs | 53 +++++++++++++++++++ example/mini_core.rs | 16 ++++-- example/mini_core_hello_world.rs | 4 +- example/std_example.rs | 6 ++- ...table-simd-Disable-unsupported-tests.patch | 36 ------------- rust-toolchain | 2 +- scripts/filter_profile.rs | 2 +- scripts/setup_rust_fork.sh | 2 +- scripts/test_rustc_tests.sh | 19 +++---- scripts/tests.sh | 4 ++ src/base.rs | 3 +- src/cast.rs | 29 +++++++--- src/config.rs | 9 ---- src/discriminant.rs | 12 ++++- src/intrinsics/mod.rs | 24 ++------- src/intrinsics/simd.rs | 29 +++++----- src/lib.rs | 2 - src/num.rs | 18 +++++++ 21 files changed, 189 insertions(+), 142 deletions(-) create mode 100644 example/float-minmax-pass.rs diff --git a/Cargo.lock b/Cargo.lock index 30e9627c48d..7b8e43b639f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,18 +33,18 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "cranelift-bforest" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d16922317bd7dd104d509a373887822caa0242fc1def00de66abb538db221db4" +checksum = "ed44413e7e2fe3260d0ed73e6956ab188b69c10ee92b892e401e0f4f6808c68b" dependencies = [ "cranelift-entity", ] [[package]] name = "cranelift-codegen" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b80bf40380256307b68a3dcbe1b91cac92a533e212b5b635abc3e4525781a0a" +checksum = "0b5d83f0f26bf213f971f45589d17e5b65e4861f9ed22392b0cbb6eaa5bd329c" dependencies = [ "cranelift-bforest", "cranelift-codegen-meta", @@ -59,30 +59,30 @@ dependencies = [ [[package]] name = "cranelift-codegen-meta" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "703d0ed7d3bc6c7a814ca12858175bf4e93167a3584127858c686e4b5dd6e432" +checksum = "6800dc386177df6ecc5a32680607ed8ba1fa0d31a2a59c8c61fbf44826b8191d" dependencies = [ "cranelift-codegen-shared", ] [[package]] name = "cranelift-codegen-shared" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80f52311e1c90de12dcf8c4b9999c6ebfd1ed360373e88c357160936844511f6" +checksum = "c961f85070985ebc8fcdb81b838a5cf842294d1e6ed4852446161c7e246fd455" [[package]] name = "cranelift-entity" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "66bc82ef522c1f643baf7d4d40b7c52643ee4549d8960b0e6a047daacb83f897" +checksum = "2347b2b8d1d5429213668f2a8e36c85ee3c73984a2f6a79007e365d3e575e7ed" [[package]] name = "cranelift-frontend" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3cc35e4251864b17515845ba47447bca88fec9ca1a4186b19fe42526e36140e8" +checksum = "4cbcdbf7bed29e363568b778649b69dabc3d727256d5d25236096ef693757654" dependencies = [ "cranelift-codegen", "log", @@ -92,9 +92,9 @@ dependencies = [ [[package]] name = "cranelift-jit" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93c66d594ad3bfe4e58b1fbd8d17877a7c6564a5f2d6f78cbbf4b0182af1927f" +checksum = "7c769d4e0d76f59c8b2a3bf0477d89ee149bb0731b53fbb245ee081d49063095" dependencies = [ "anyhow", "cranelift-codegen", @@ -110,9 +110,9 @@ dependencies = [ [[package]] name = "cranelift-module" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf356697c40232aa09e1e3fb8a350ee894e849ccecc4eac56ff0570a4575c325" +checksum = "0ab57d399a2401074bb0cc40b3031e420f3d66d46ec0cf21eeae53ac04bd73e2" dependencies = [ "anyhow", "cranelift-codegen", @@ -120,9 +120,9 @@ dependencies = [ [[package]] name = "cranelift-native" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b882b2251c9845d509d92aebfdb6c8bb3b3b48e207ac951f21fbd20cfe7f90b3" +checksum = "8f4cdf93552e5ceb2e3c042829ebb4de4378492705f769eadc6a7c6c5251624c" dependencies = [ "cranelift-codegen", "libc", @@ -131,9 +131,9 @@ dependencies = [ [[package]] name = "cranelift-object" -version = "0.82.1" +version = "0.83.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d3f1a88e654e567d2591169239ed157ab290811a729a6468f53999c01001263" +checksum = "cf8e65f4839c26e6237fc0744911d79b0a2ac5e76b4e4eebd14db2b8d849fd31" dependencies = [ "anyhow", "cranelift-codegen", diff --git a/Cargo.toml b/Cargo.toml index 70c03da3f29..74f50808a98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,12 +8,12 @@ crate-type = ["dylib"] [dependencies] # These have to be in sync with each other -cranelift-codegen = { version = "0.82.1", features = ["unwind", "all-arch"] } -cranelift-frontend = "0.82.1" -cranelift-module = "0.82.1" -cranelift-native = "0.82.1" -cranelift-jit = { version = "0.82.1", optional = true } -cranelift-object = "0.82.1" +cranelift-codegen = { version = "0.83.0", features = ["unwind", "all-arch"] } +cranelift-frontend = "0.83.0" +cranelift-module = "0.83.0" +cranelift-native = "0.83.0" +cranelift-jit = { version = "0.83.0", optional = true } +cranelift-object = "0.83.0" target-lexicon = "0.12.0" gimli = { version = "0.26.0", default-features = false, features = ["write"]} object = { version = "0.27.0", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] } diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index f584f54e106..51ba0dbfcc7 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -56,9 +56,9 @@ dependencies = [ [[package]] name = "compiler_builtins" -version = "0.1.71" +version = "0.1.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "163437f05ca8f29d7e9128ea728dedf5eb620e445fbca273641d3a3050305f23" +checksum = "afdbb35d279238cf77f0c9e8d90ad50d6c7bff476ab342baafa29440f0f10bff" dependencies = [ "rustc-std-workspace-core", ] @@ -134,9 +134,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.121" +version = "0.2.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efaa7b300f3b5fe8eb6bf21ce3895e1751d9665086af2d64b42f19701015ff4f" +checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" dependencies = [ "rustc-std-workspace-core", ] @@ -203,6 +203,7 @@ dependencies = [ name = "proc_macro" version = "0.0.0" dependencies = [ + "core", "std", ] diff --git a/example/float-minmax-pass.rs b/example/float-minmax-pass.rs new file mode 100644 index 00000000000..b8f901d1ba1 --- /dev/null +++ b/example/float-minmax-pass.rs @@ -0,0 +1,53 @@ +// Copied from https://github.com/rust-lang/rust/blob/3fe3b89cd57229343eeca753fdd8c63d9b03c65c/src/test/ui/simd/intrinsic/float-minmax-pass.rs +// run-pass +// ignore-emscripten + +// Test that the simd_f{min,max} intrinsics produce the correct results. + +#![feature(repr_simd, platform_intrinsics)] +#![allow(non_camel_case_types)] + +#[repr(simd)] +#[derive(Copy, Clone, PartialEq, Debug)] +struct f32x4(pub f32, pub f32, pub f32, pub f32); + +extern "platform-intrinsic" { + fn simd_fmin(x: T, y: T) -> T; + fn simd_fmax(x: T, y: T) -> T; +} + +fn main() { + let x = f32x4(1.0, 2.0, 3.0, 4.0); + let y = f32x4(2.0, 1.0, 4.0, 3.0); + + #[cfg(not(any(target_arch = "mips", target_arch = "mips64")))] + let nan = f32::NAN; + // MIPS hardware treats f32::NAN as SNAN. Clear the signaling bit. + // See https://github.com/rust-lang/rust/issues/52746. + #[cfg(any(target_arch = "mips", target_arch = "mips64"))] + let nan = f32::from_bits(f32::NAN.to_bits() - 1); + + let n = f32x4(nan, nan, nan, nan); + + unsafe { + let min0 = simd_fmin(x, y); + let min1 = simd_fmin(y, x); + assert_eq!(min0, min1); + let e = f32x4(1.0, 1.0, 3.0, 3.0); + assert_eq!(min0, e); + let minn = simd_fmin(x, n); + assert_eq!(minn, x); + let minn = simd_fmin(y, n); + assert_eq!(minn, y); + + let max0 = simd_fmax(x, y); + let max1 = simd_fmax(y, x); + assert_eq!(max0, max1); + let e = f32x4(2.0, 2.0, 4.0, 4.0); + assert_eq!(max0, e); + let maxn = simd_fmax(x, n); + assert_eq!(maxn, x); + let maxn = simd_fmax(y, n); + assert_eq!(maxn, y); + } +} diff --git a/example/mini_core.rs b/example/mini_core.rs index 7efc8dc785a..8da705e0cb0 100644 --- a/example/mini_core.rs +++ b/example/mini_core.rs @@ -16,6 +16,9 @@ #[lang = "sized"] pub trait Sized {} +#[lang = "destruct"] +pub trait Destruct {} + #[lang = "unsize"] pub trait Unsize {} @@ -491,13 +494,20 @@ pub trait Deref { fn deref(&self) -> &Self::Target; } +#[repr(transparent)] +#[rustc_layout_scalar_valid_range_start(1)] +#[rustc_nonnull_optimization_guaranteed] +pub struct NonNull(pub *mut T); + +impl CoerceUnsized> for NonNull where T: Unsize {} +impl DispatchFromDyn> for NonNull where T: Unsize {} + pub struct Unique { - pub pointer: *const T, + pub pointer: NonNull, pub _marker: PhantomData, } impl CoerceUnsized> for Unique where T: Unsize {} - impl DispatchFromDyn> for Unique where T: Unsize {} #[lang = "owned_box"] @@ -526,7 +536,7 @@ unsafe fn allocate(size: usize, _align: usize) -> *mut u8 { #[lang = "box_free"] unsafe fn box_free(ptr: Unique, alloc: ()) { - libc::free(ptr.pointer as *mut u8); + libc::free(ptr.pointer.0 as *mut u8); } #[lang = "drop"] diff --git a/example/mini_core_hello_world.rs b/example/mini_core_hello_world.rs index c4730581335..85ca908d0a2 100644 --- a/example/mini_core_hello_world.rs +++ b/example/mini_core_hello_world.rs @@ -122,7 +122,7 @@ fn call_return_u128_pair() { #[allow(unreachable_code)] // FIXME false positive fn main() { take_unique(Unique { - pointer: 0 as *const (), + pointer: unsafe { NonNull(1 as *mut ()) }, _marker: PhantomData, }); take_f32(0.1); @@ -173,7 +173,7 @@ fn main() { assert!(intrinsics::needs_drop::()); Unique { - pointer: 0 as *const &str, + pointer: NonNull(1 as *mut &str), _marker: PhantomData, } as Unique; diff --git a/example/std_example.rs b/example/std_example.rs index 5bc51a541b5..0a2bce2621d 100644 --- a/example/std_example.rs +++ b/example/std_example.rs @@ -1,7 +1,8 @@ -#![feature(core_intrinsics, generators, generator_trait, is_sorted)] +#![feature(core_intrinsics, generators, generator_trait, is_sorted, bench_black_box)] #[cfg(target_arch = "x86_64")] use std::arch::x86_64::*; +use std::hint::black_box; use std::io::Write; use std::ops::Generator; @@ -86,6 +87,9 @@ fn main() { assert_eq!(houndred_f64 as i128, 100); assert_eq!(1u128.rotate_left(2), 4); + assert_eq!(black_box(f32::NAN) as i128, 0); + assert_eq!(black_box(f32::NAN) as u128, 0); + // Test signed 128bit comparing let max = usize::MAX as i128; if 100i128 < 0i128 || 100i128 > max { diff --git a/patches/0001-portable-simd-Disable-unsupported-tests.patch b/patches/0001-portable-simd-Disable-unsupported-tests.patch index c1325908691..54e13b090ab 100644 --- a/patches/0001-portable-simd-Disable-unsupported-tests.patch +++ b/patches/0001-portable-simd-Disable-unsupported-tests.patch @@ -102,42 +102,6 @@ index 6a8ecd3..68fcb49 100644 } } } -diff --git a/crates/core_simd/tests/ops_macros.rs b/crates/core_simd/tests/ops_macros.rs -index 31b7ee2..bd04b3c 100644 ---- a/crates/core_simd/tests/ops_macros.rs -+++ b/crates/core_simd/tests/ops_macros.rs -@@ -567,6 +567,7 @@ macro_rules! impl_float_tests { - }); - } - -+ /* - fn horizontal_max() { - test_helpers::test_1(&|x| { - let vmax = Vector::::from_array(x).horizontal_max(); -@@ -590,6 +591,7 @@ macro_rules! impl_float_tests { - Ok(()) - }); - } -+ */ - } - - #[cfg(feature = "std")] -@@ -604,6 +606,7 @@ macro_rules! impl_float_tests { - ) - } - -+ /* - fn mul_add() { - test_helpers::test_ternary_elementwise( - &Vector::::mul_add, -@@ -611,6 +614,7 @@ macro_rules! impl_float_tests { - &|_, _, _| true, - ) - } -+ */ - } - } - } -- 2.26.2.7.g19db9cfb68 diff --git a/rust-toolchain b/rust-toolchain index 84d90e5db02..966097c248b 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-03-19" +channel = "nightly-2022-04-21" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] diff --git a/scripts/filter_profile.rs b/scripts/filter_profile.rs index a0e99267c2b..f4e863e5494 100755 --- a/scripts/filter_profile.rs +++ b/scripts/filter_profile.rs @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash #![forbid(unsafe_code)]/* This line is ignored by bash # This block is ignored by rustc pushd $(dirname "$0")/../ diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index 85c0109c6f6..cabbaaa8922 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e ./y.rs build --no-unstable-features diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index a32e6df2208..4cf24c02235 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash set -e cd $(dirname "$0")/../ @@ -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 "asm!|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/{ui,incremental}); do rm $test done @@ -25,14 +25,8 @@ git checkout -- src/test/ui/issues/auxiliary/issue-3136-a.rs # contains //~ERROR # ================ # requires stack unwinding -rm src/test/ui/backtrace.rs -rm src/test/ui/process/multi-panic.rs -rm src/test/ui/numbers-arithmetic/issue-8460.rs 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 src/test/ui/panic-while-printing.rs -rm src/test/ui/test-attrs/test-panic-while-printing.rs -rm src/test/ui/test-attrs/test-type.rs # requires compiling with -Cpanic=unwind rm src/test/ui/test-attrs/test-fn-signature-verification-for-explicit-return-type.rs # "Cannot run dynamic test fn out-of-process" @@ -85,8 +79,6 @@ rm src/test/ui/abi/stack-protector.rs # requires stack protector support # giving different but possibly correct results # ============================================= -rm src/test/ui/numbers-arithmetic/saturating-float-casts.rs # intrinsic gives different but valid result -rm src/test/ui/simd/intrinsic/float-minmax-pass.rs # same 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 @@ -112,9 +104,14 @@ rm src/test/ui/mir/ssa-analysis-regression-50041.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/rfc-2091-track-caller/intrinsic-wrapper.rs # wrong result from `Location::caller()` + # bugs in the test suite # ====================== -rm src/test/ui/unsafe/union.rs # has UB caught by cg_clif. see rust-lang/rust#95075 +rm src/test/ui/backtrace.rs # TODO warning +rm src/test/ui/empty_global_asm.rs # TODO add needs-asm-support +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 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} diff --git a/scripts/tests.sh b/scripts/tests.sh index fee1012c8f1..aae626081f6 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -72,6 +72,10 @@ function base_sysroot_tests() { $MY_RUSTC example/track-caller-attribute.rs --crate-type bin -Cpanic=abort --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/track-caller-attribute + echo "[AOT] float-minmax-pass" + $MY_RUSTC example/float-minmax-pass.rs --crate-type bin -Cpanic=abort --target "$TARGET_TRIPLE" + $RUN_WRAPPER ./target/out/float-minmax-pass + echo "[AOT] mod_bench" $MY_RUSTC example/mod_bench.rs --crate-type bin --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/mod_bench diff --git a/src/base.rs b/src/base.rs index 8c45993a8b7..65346cb3962 100644 --- a/src/base.rs +++ b/src/base.rs @@ -821,7 +821,8 @@ pub(crate) fn codegen_place<'tcx>( if cplace.layout().ty.is_box() { cplace = cplace .place_field(fx, Field::new(0)) // Box -> Unique - .place_field(fx, Field::new(0)) // Unique -> *const T + .place_field(fx, Field::new(0)) // Unique -> NonNull + .place_field(fx, Field::new(0)) // NonNull -> *mut T .place_deref(fx); } else { cplace = cplace.place_deref(fx); diff --git a/src/cast.rs b/src/cast.rs index e7e6afeb865..e19070774c6 100644 --- a/src/cast.rs +++ b/src/cast.rs @@ -84,7 +84,7 @@ pub(crate) fn clif_int_or_float_cast( fx.bcx.ins().fcvt_from_uint(to_ty, from) } } else if from_ty.is_float() && to_ty.is_int() { - if to_ty == types::I128 { + let val = if to_ty == types::I128 { // _____sssf___ // __fix sfti: f32 -> i128 // __fix dfti: f64 -> i128 @@ -109,13 +109,9 @@ pub(crate) fn clif_int_or_float_cast( let to_rust_ty = if to_signed { fx.tcx.types.i128 } else { fx.tcx.types.u128 }; - return fx - .easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty) - .load_scalar(fx); - } - - // float -> int-like - if to_ty == types::I8 || to_ty == types::I16 { + fx.easy_call(&name, &[CValue::by_val(from, fx.layout_of(from_rust_ty))], to_rust_ty) + .load_scalar(fx) + } else if to_ty == types::I8 || to_ty == types::I16 { // FIXME implement fcvt_to_*int_sat.i8/i16 let val = if to_signed { fx.bcx.ins().fcvt_to_sint_sat(types::I32, from) @@ -146,6 +142,23 @@ pub(crate) fn clif_int_or_float_cast( fx.bcx.ins().fcvt_to_sint_sat(to_ty, from) } else { fx.bcx.ins().fcvt_to_uint_sat(to_ty, from) + }; + + if let Some(false) = fx.tcx.sess.opts.debugging_opts.saturating_float_casts { + return val; + } + + let is_not_nan = fx.bcx.ins().fcmp(FloatCC::Equal, from, from); + if to_ty == types::I128 { + // FIXME(bytecodealliance/wasmtime#3963): select.i128 on fcmp eq miscompiles + let (lsb, msb) = fx.bcx.ins().isplit(val); + let zero = fx.bcx.ins().iconst(types::I64, 0); + let lsb = fx.bcx.ins().select(is_not_nan, lsb, zero); + let msb = fx.bcx.ins().select(is_not_nan, msb, zero); + fx.bcx.ins().iconcat(lsb, msb) + } else { + let zero = fx.bcx.ins().iconst(to_ty, 0); + 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/config.rs b/src/config.rs index eef3c8c8d6e..e59a0cb0a23 100644 --- a/src/config.rs +++ b/src/config.rs @@ -48,12 +48,6 @@ pub struct BackendConfig { /// Can be set using `-Cllvm-args=display_cg_time=...`. pub display_cg_time: bool, - /// The register allocator to use. - /// - /// Defaults to the value of `CG_CLIF_REGALLOC` or `backtracking` otherwise. Can be set using - /// `-Cllvm-args=regalloc=...`. - pub regalloc: String, - /// Enable the Cranelift ir verifier for all compilation passes. If not set it will only run /// once before passing the clif ir to Cranelift for compilation. /// @@ -80,8 +74,6 @@ impl Default for BackendConfig { args.split(' ').map(|arg| arg.to_string()).collect() }, display_cg_time: bool_env_var("CG_CLIF_DISPLAY_CG_TIME"), - regalloc: std::env::var("CG_CLIF_REGALLOC") - .unwrap_or_else(|_| "backtracking".to_string()), enable_verifier: cfg!(debug_assertions) || bool_env_var("CG_CLIF_ENABLE_VERIFIER"), disable_incr_cache: bool_env_var("CG_CLIF_DISABLE_INCR_CACHE"), } @@ -101,7 +93,6 @@ impl BackendConfig { match name { "mode" => config.codegen_mode = value.parse()?, "display_cg_time" => config.display_cg_time = parse_bool(name, value)?, - "regalloc" => config.regalloc = value.to_string(), "enable_verifier" => config.enable_verifier = parse_bool(name, value)?, "disable_incr_cache" => config.disable_incr_cache = parse_bool(name, value)?, _ => return Err(format!("Unknown option `{}`", name)), diff --git a/src/discriminant.rs b/src/discriminant.rs index 357cb4a6d24..f619bb5ed5e 100644 --- a/src/discriminant.rs +++ b/src/discriminant.rs @@ -128,8 +128,16 @@ pub(crate) fn codegen_get_discriminant<'tcx>( let relative_discr = if niche_start == 0 { tag } else { - // FIXME handle niche_start > i64::MAX - fx.bcx.ins().iadd_imm(tag, -i64::try_from(niche_start).unwrap()) + 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); + let msb = + fx.bcx.ins().iconst(types::I64, (niche_start >> 64) as u64 as i64); + fx.bcx.ins().iconcat(lsb, msb) + } + 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 = { diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 310d27c6dec..d76dfca7960 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -1019,39 +1019,23 @@ fn codegen_regular_intrinsic_call<'tcx>( ret.write_cvalue(fx, old); }; - // In Rust floating point min and max don't propagate NaN. In Cranelift they do however. - // For this reason it is necessary to use `a.is_nan() ? b : (a >= b ? b : a)` for `minnumf*` - // and `a.is_nan() ? b : (a <= b ? b : a)` for `maxnumf*`. NaN checks are done by comparing - // a float against itself. Only in case of NaN is it not equal to itself. minnumf32, (v a, v b) { - let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); - let a_ge_b = fx.bcx.ins().fcmp(FloatCC::GreaterThanOrEqual, a, b); - let temp = fx.bcx.ins().select(a_ge_b, b, a); - let val = fx.bcx.ins().select(a_is_nan, b, temp); + let val = crate::num::codegen_float_min(fx, a, b); let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32)); ret.write_cvalue(fx, val); }; minnumf64, (v a, v b) { - let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); - let a_ge_b = fx.bcx.ins().fcmp(FloatCC::GreaterThanOrEqual, a, b); - let temp = fx.bcx.ins().select(a_ge_b, b, a); - let val = fx.bcx.ins().select(a_is_nan, b, temp); + let val = crate::num::codegen_float_min(fx, a, b); let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64)); ret.write_cvalue(fx, val); }; maxnumf32, (v a, v b) { - let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); - let a_le_b = fx.bcx.ins().fcmp(FloatCC::LessThanOrEqual, a, b); - let temp = fx.bcx.ins().select(a_le_b, b, a); - let val = fx.bcx.ins().select(a_is_nan, b, temp); + let val = crate::num::codegen_float_max(fx, a, b); let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f32)); ret.write_cvalue(fx, val); }; maxnumf64, (v a, v b) { - let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); - let a_le_b = fx.bcx.ins().fcmp(FloatCC::LessThanOrEqual, a, b); - let temp = fx.bcx.ins().select(a_le_b, b, a); - let val = fx.bcx.ins().select(a_is_nan, b, temp); + let val = crate::num::codegen_float_max(fx, a, b); let val = CValue::by_val(val, fx.layout_of(fx.tcx.types.f64)); ret.write_cvalue(fx, val); }; diff --git a/src/intrinsics/simd.rs b/src/intrinsics/simd.rs index bc21d736166..d1ca9edf2e0 100644 --- a/src/intrinsics/simd.rs +++ b/src/intrinsics/simd.rs @@ -322,20 +322,21 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( } assert_eq!(a.layout(), b.layout()); assert_eq!(a.layout(), c.layout()); - let layout = a.layout(); + assert_eq!(a.layout(), ret.layout()); - let (lane_count, _lane_ty) = 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!(lane_count, ret_lane_count); - let ret_lane_layout = fx.layout_of(ret_lane_ty); + let layout = a.layout(); + let (lane_count, lane_ty) = layout.ty.simd_size_and_type(fx.tcx); for lane in 0..lane_count { - let a_lane = a.value_lane(fx, lane).load_scalar(fx); - let b_lane = b.value_lane(fx, lane).load_scalar(fx); - let c_lane = c.value_lane(fx, lane).load_scalar(fx); + let a_lane = a.value_lane(fx, lane); + let b_lane = b.value_lane(fx, lane); + let c_lane = c.value_lane(fx, lane); - let mul_lane = fx.bcx.ins().fmul(a_lane, b_lane); - let res_lane = CValue::by_val(fx.bcx.ins().fadd(mul_lane, c_lane), ret_lane_layout); + let res_lane = match lane_ty.kind() { + ty::Float(FloatTy::F32) => fx.easy_call("fmaf", &[a_lane, b_lane, c_lane], lane_ty), + ty::Float(FloatTy::F64) => fx.easy_call("fma", &[a_lane, b_lane, c_lane], lane_ty), + _ => unreachable!(), + }; ret.place_lane(fx, lane).write_cvalue(fx, res_lane); } @@ -354,8 +355,8 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( _ => unreachable!("{:?}", lane_ty), } match intrinsic { - sym::simd_fmin => fx.bcx.ins().fmin(x_lane, y_lane), - sym::simd_fmax => fx.bcx.ins().fmax(x_lane, y_lane), + sym::simd_fmin => crate::num::codegen_float_min(fx, x_lane, y_lane), + sym::simd_fmax => crate::num::codegen_float_max(fx, x_lane, y_lane), _ => unreachable!(), } }); @@ -495,7 +496,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( let lt = match ty.kind() { ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedLessThan, a, b), ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedLessThan, a, b), - ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::LessThan, a, b), + ty::Float(_) => return crate::num::codegen_float_min(fx, a, b), _ => unreachable!(), }; fx.bcx.ins().select(lt, a, b) @@ -512,7 +513,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>( let gt = match ty.kind() { ty::Int(_) => fx.bcx.ins().icmp(IntCC::SignedGreaterThan, a, b), ty::Uint(_) => fx.bcx.ins().icmp(IntCC::UnsignedGreaterThan, a, b), - ty::Float(_) => fx.bcx.ins().fcmp(FloatCC::GreaterThan, a, b), + ty::Float(_) => return crate::num::codegen_float_max(fx, a, b), _ => unreachable!(), }; fx.bcx.ins().select(gt, a, b) diff --git a/src/lib.rs b/src/lib.rs index 878b9390e13..9d2e12f9898 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -256,8 +256,6 @@ fn build_isa(sess: &Session, backend_config: &BackendConfig) -> Box { diff --git a/src/num.rs b/src/num.rs index 545d390e269..4ce8adb182e 100644 --- a/src/num.rs +++ b/src/num.rs @@ -420,3 +420,21 @@ pub(crate) fn codegen_ptr_binop<'tcx>( CValue::by_val(fx.bcx.ins().bint(types::I8, res), fx.layout_of(fx.tcx.types.bool)) } } + +// In Rust floating point min and max don't propagate NaN. In Cranelift they do however. +// For this reason it is necessary to use `a.is_nan() ? b : (a >= b ? b : a)` for `minnumf*` +// and `a.is_nan() ? b : (a <= b ? b : a)` for `maxnumf*`. NaN checks are done by comparing +// a float against itself. Only in case of NaN is it not equal to itself. +pub(crate) fn codegen_float_min(fx: &mut FunctionCx<'_, '_, '_>, a: Value, b: Value) -> Value { + let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); + let a_ge_b = fx.bcx.ins().fcmp(FloatCC::GreaterThanOrEqual, a, b); + let temp = fx.bcx.ins().select(a_ge_b, b, a); + fx.bcx.ins().select(a_is_nan, b, temp) +} + +pub(crate) fn codegen_float_max(fx: &mut FunctionCx<'_, '_, '_>, a: Value, b: Value) -> Value { + let a_is_nan = fx.bcx.ins().fcmp(FloatCC::NotEqual, a, a); + let a_le_b = fx.bcx.ins().fcmp(FloatCC::LessThanOrEqual, a, b); + let temp = fx.bcx.ins().select(a_le_b, b, a); + fx.bcx.ins().select(a_is_nan, b, temp) +} From 7e45747a3d7c0877da20a9931cffc50e1b6d6aa4 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 27 Apr 2022 12:32:20 +0200 Subject: [PATCH 02/25] Rustup to rustc 1.62.0-nightly (082e4ca49 2022-04-26) --- rust-toolchain | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-toolchain b/rust-toolchain index 966097c248b..2fa0ac3f772 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-04-21" +channel = "nightly-2022-04-27" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] From f429b4093f905ff2239889daec21433deb198250 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 27 Apr 2022 14:09:41 +0200 Subject: [PATCH 03/25] Fix running the rustc test suite --- scripts/setup_rust_fork.sh | 15 --------------- scripts/test_rustc_tests.sh | 1 + 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/scripts/setup_rust_fork.sh b/scripts/setup_rust_fork.sh index cabbaaa8922..fa71dfeac07 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -26,21 +26,6 @@ index d95b5b7f17f..00b6f0e3635 100644 [dev-dependencies] rand = "0.7" rand_xorshift = "0.2" -diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs -index 887d27fd6dca4..2c2239f2b83d1 100644 ---- a/src/tools/compiletest/src/header.rs -+++ b/src/tools/compiletest/src/header.rs -@@ -806,8 +806,8 @@ pub fn make_test_description( - cfg: Option<&str>, - ) -> test::TestDesc { - let mut ignore = false; - #[cfg(not(bootstrap))] -- let ignore_message: Option = None; -+ let ignore_message: Option<&str> = None; - let mut should_fail = false; - - let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some(); - 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 diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 4cf24c02235..5afab7c5de5 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -101,6 +101,7 @@ 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 From 5e0c62372c77e44793bfb1609ab67ebb2af6a1f3 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 27 Apr 2022 16:54:35 +0200 Subject: [PATCH 04/25] Minor changes to tests.sh --- scripts/tests.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts/tests.sh b/scripts/tests.sh index aae626081f6..938553ceab0 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -39,7 +39,7 @@ function base_sysroot_tests() { $MY_RUSTC example/issue-91827-extern-types.rs --crate-name issue_91827_extern_types --crate-type bin --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/issue_91827_extern_types - echo "[AOT] alloc_system" + echo "[BUILD] alloc_system" $MY_RUSTC example/alloc_system.rs --crate-type lib --target "$TARGET_TRIPLE" echo "[AOT] alloc_example" @@ -56,14 +56,14 @@ function base_sysroot_tests() { echo "[JIT] std_example (skipped)" fi - echo "[AOT] dst_field_align" - $MY_RUSTC example/dst-field-align.rs --crate-name dst_field_align --crate-type bin --target "$TARGET_TRIPLE" - $RUN_WRAPPER ./target/out/dst_field_align || (echo $?; false) - echo "[AOT] std_example" $MY_RUSTC example/std_example.rs --crate-type bin --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/std_example arg + echo "[AOT] dst_field_align" + $MY_RUSTC example/dst-field-align.rs --crate-name dst_field_align --crate-type bin --target "$TARGET_TRIPLE" + $RUN_WRAPPER ./target/out/dst_field_align + echo "[AOT] subslice-patterns-const-eval" $MY_RUSTC example/subslice-patterns-const-eval.rs --crate-type bin -Cpanic=abort --target "$TARGET_TRIPLE" $RUN_WRAPPER ./target/out/subslice-patterns-const-eval From 88b5f5722a23800474d2c6b192dd5129590b4254 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 14 Feb 2022 15:24:14 +0100 Subject: [PATCH 05/25] Use jit_builder.symbol instead of #[no_mangle] for __clif_jit_fn #[no_mangle] doesn't resolve when the codegen backend is opened with RTLD_LOCAL --- src/driver/jit.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/driver/jit.rs b/src/driver/jit.rs index 7f15bc75fda..1b01f4edbb3 100644 --- a/src/driver/jit.rs +++ b/src/driver/jit.rs @@ -74,6 +74,7 @@ fn create_jit_module<'tcx>( jit_builder.hotswap(hotswap); crate::compiler_builtins::register_functions_for_jit(&mut jit_builder); jit_builder.symbols(imported_symbols); + jit_builder.symbol("__clif_jit_fn", clif_jit_fn as *const u8); let mut jit_module = JITModule::new(jit_builder); let mut cx = crate::CodegenCx::new( @@ -210,8 +211,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! { } } -#[no_mangle] -extern "C" fn __clif_jit_fn( +extern "C" fn clif_jit_fn( instance_ptr: *const Instance<'static>, trampoline_ptr: *const u8, ) -> *const u8 { From 944a48d3afb0fa27390ecbd2ceb0f1c666cf4c5b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 13 Feb 2022 18:46:53 +0100 Subject: [PATCH 06/25] Remove less of the sysroot build dir on recompilation --- build_system/build_sysroot.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index c9c003d4610..b301777e1e8 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -168,11 +168,10 @@ fn build_clif_sysroot_for_triple( let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel); if !super::config::get_bool("keep_sysroot") { - // Cleanup the target dir with the exception of build scripts and the incremental cache - for dir in ["build", "deps", "examples", "native"] { - if build_dir.join(dir).exists() { - fs::remove_dir_all(build_dir.join(dir)).unwrap(); - } + // 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(); } } From 377f44d38c0ee788860df1f8edf02d7eefbe0160 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 13 Feb 2022 19:36:36 +0100 Subject: [PATCH 07/25] Inline ext_config.sh --- scripts/ext_config.sh | 32 -------------------------------- scripts/tests.sh | 29 ++++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 33 deletions(-) delete mode 100644 scripts/ext_config.sh diff --git a/scripts/ext_config.sh b/scripts/ext_config.sh deleted file mode 100644 index 11d6c4c8318..00000000000 --- a/scripts/ext_config.sh +++ /dev/null @@ -1,32 +0,0 @@ -# Note to people running shellcheck: this file should only be sourced, not executed directly. - -# Various env vars that should only be set for the build system - -set -e - -export CG_CLIF_DISPLAY_CG_TIME=1 -export CG_CLIF_DISABLE_INCR_CACHE=1 - -export HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") -export TARGET_TRIPLE=${TARGET_TRIPLE:-$HOST_TRIPLE} - -export RUN_WRAPPER='' -export JIT_SUPPORTED=1 -if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then - export JIT_SUPPORTED=0 - if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then - # We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. - export RUSTFLAGS='-Clinker=aarch64-linux-gnu-gcc '$RUSTFLAGS - export RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu' - elif [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then - # We are cross-compiling for Windows. Run tests in wine. - export RUN_WRAPPER='wine' - else - echo "Unknown non-native platform" - fi -fi - -# FIXME fix `#[linkage = "extern_weak"]` without this -if [[ "$(uname)" == 'Darwin' ]]; then - export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup" -fi diff --git a/scripts/tests.sh b/scripts/tests.sh index 938553ceab0..cdde44ac67c 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -3,7 +3,34 @@ set -e source scripts/config.sh -source scripts/ext_config.sh + +export CG_CLIF_DISPLAY_CG_TIME=1 +export CG_CLIF_DISABLE_INCR_CACHE=1 + +export HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") +export TARGET_TRIPLE=${TARGET_TRIPLE:-$HOST_TRIPLE} + +export RUN_WRAPPER='' +export JIT_SUPPORTED=1 +if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then + export JIT_SUPPORTED=0 + if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then + # We are cross-compiling for aarch64. Use the correct linker and run tests in qemu. + export RUSTFLAGS='-Clinker=aarch64-linux-gnu-gcc '$RUSTFLAGS + export RUN_WRAPPER='qemu-aarch64 -L /usr/aarch64-linux-gnu' + elif [[ "$TARGET_TRIPLE" == "x86_64-pc-windows-gnu" ]]; then + # We are cross-compiling for Windows. Run tests in wine. + export RUN_WRAPPER='wine' + else + echo "Unknown non-native platform" + fi +fi + +# FIXME fix `#[linkage = "extern_weak"]` without this +if [[ "$(uname)" == 'Darwin' ]]; then + export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup" +fi + export RUSTC=false # ensure that cg_llvm isn't accidentally used MY_RUSTC="$(pwd)/build/bin/cg_clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2" From 617171e930cc282ceaa71fd6a6cd18615ad1bf59 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 13 Feb 2022 18:33:30 +0100 Subject: [PATCH 08/25] Remove cg_clif_build_sysroot --- build_system/build_sysroot.rs | 55 ++++++++++--------- build_system/mod.rs | 1 + src/bin/cg_clif_build_sysroot.rs | 93 -------------------------------- 3 files changed, 31 insertions(+), 118 deletions(-) delete mode 100644 src/bin/cg_clif_build_sysroot.rs diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index b301777e1e8..ac27e77caa7 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -1,4 +1,3 @@ -use std::env; use std::fs; use std::path::{Path, PathBuf}; use std::process::{self, Command}; @@ -22,25 +21,21 @@ pub(crate) fn build_sysroot( fs::create_dir_all(target_dir.join("lib")).unwrap(); // Copy the backend - for file in ["cg_clif", "cg_clif_build_sysroot"] { - try_hard_link( - cg_clif_build_dir.join(get_file_name(file, "bin")), - target_dir.join("bin").join(get_file_name(file, "bin")), - ); - } - let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib"); + 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(&cg_clif_dylib); + try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path); + try_hard_link( - cg_clif_build_dir.join(&cg_clif_dylib), - 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(cg_clif_dylib), + cg_clif_build_dir.join(get_file_name("cg_clif", "bin")), + target_dir.join("bin").join(get_file_name("cg_clif", "bin")), ); // Build and copy cargo wrapper @@ -117,7 +112,13 @@ pub(crate) fn build_sysroot( } } SysrootKind::Clif => { - build_clif_sysroot_for_triple(channel, target_dir, host_triple, None); + build_clif_sysroot_for_triple( + channel, + target_dir, + 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 @@ -126,7 +127,13 @@ pub(crate) fn build_sysroot( } else { None }; - build_clif_sysroot_for_triple(channel, target_dir, target_triple, linker); + build_clif_sysroot_for_triple( + channel, + target_dir, + target_triple, + &cg_clif_dylib_path, + linker, + ); } // Copy std for the host to the lib dir. This is necessary for the jit mode to find @@ -145,6 +152,7 @@ fn build_clif_sysroot_for_triple( 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")) { @@ -178,7 +186,8 @@ fn build_clif_sysroot_for_triple( // Build sysroot let mut build_cmd = Command::new("cargo"); build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot"); - let mut rustflags = "--clif -Zforce-unstable-if-unmarked".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())); if channel == "release" { build_cmd.arg("--release"); rustflags.push_str(" -Zmir-opt-level=3"); @@ -188,10 +197,6 @@ fn build_clif_sysroot_for_triple( write!(rustflags, " -Clinker={}", linker).unwrap(); } build_cmd.env("RUSTFLAGS", rustflags); - build_cmd.env( - "RUSTC", - env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"), - ); build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif"); spawn_and_wait(build_cmd); diff --git a/build_system/mod.rs b/build_system/mod.rs index b228da3981f..b897b7fbacf 100644 --- a/build_system/mod.rs +++ b/build_system/mod.rs @@ -86,6 +86,7 @@ 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 diff --git a/src/bin/cg_clif_build_sysroot.rs b/src/bin/cg_clif_build_sysroot.rs deleted file mode 100644 index bde4d71b9a3..00000000000 --- a/src/bin/cg_clif_build_sysroot.rs +++ /dev/null @@ -1,93 +0,0 @@ -//! The only difference between this and cg_clif.rs is that this binary defaults to using cg_llvm -//! instead of cg_clif and requires `--clif` to use cg_clif and that this binary doesn't have JIT -//! support. -//! This is necessary as with Cargo `RUSTC` applies to both target crates and host crates. The host -//! crates must be built with cg_llvm as we are currently building a sysroot for cg_clif. -//! `RUSTFLAGS` however is only applied to target crates, so `--clif` would only be passed to the -//! target crates. - -#![feature(rustc_private)] -#![warn(rust_2018_idioms)] -#![warn(unused_lifetimes)] -#![warn(unreachable_pub)] - -extern crate rustc_driver; -extern crate rustc_interface; -extern crate rustc_session; -extern crate rustc_target; - -use std::path::PathBuf; - -use rustc_interface::interface; -use rustc_session::config::ErrorOutputType; -use rustc_session::early_error; -use rustc_target::spec::PanicStrategy; - -fn find_sysroot() -> String { - // Taken from https://github.com/Manishearth/rust-clippy/pull/911. - let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME")); - let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN")); - match (home, toolchain) { - (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain), - _ => option_env!("RUST_SYSROOT") - .expect("need to specify RUST_SYSROOT env var or use rustup or multirust") - .to_owned(), - } -} - -pub struct CraneliftPassesCallbacks { - use_clif: bool, -} - -impl rustc_driver::Callbacks for CraneliftPassesCallbacks { - fn config(&mut self, config: &mut interface::Config) { - if !self.use_clif { - config.opts.maybe_sysroot = Some(PathBuf::from(find_sysroot())); - return; - } - - config.opts.cg.panic = Some(PanicStrategy::Abort); - config.opts.debugging_opts.panic_abort_tests = true; - config.opts.maybe_sysroot = - Some(std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned()); - } -} - -fn main() { - rustc_driver::init_rustc_env_logger(); - rustc_driver::install_ice_hook(); - let exit_code = rustc_driver::catch_with_exit_code(|| { - let mut use_clif = false; - - let args = std::env::args_os() - .enumerate() - .map(|(i, arg)| { - arg.into_string().unwrap_or_else(|arg| { - early_error( - ErrorOutputType::default(), - &format!("Argument {} is not valid Unicode: {:?}", i, arg), - ) - }) - }) - .filter(|arg| { - if arg == "--clif" { - use_clif = true; - false - } else { - true - } - }) - .collect::>(); - - let mut callbacks = CraneliftPassesCallbacks { use_clif }; - - let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks); - if use_clif { - run_compiler.set_make_codegen_backend(Some(Box::new(move |_| { - Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None }) - }))); - } - run_compiler.run() - }); - std::process::exit(exit_code) -} From 2e65a8f2ca9e2d7ffd42ef8b2c21436d4997c35d Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 13 Feb 2022 19:31:49 +0100 Subject: [PATCH 09/25] Use -Zcodegen-backend instead of a rustc replacement in cargo-clif --- build_system/build_backend.rs | 12 ----- build_system/build_sysroot.rs | 23 ++++----- docs/usage.md | 6 +-- scripts/cargo-clif.rs | 22 +++----- scripts/config.sh | 6 --- scripts/filter_profile.rs | 3 +- scripts/rustc-clif.rs | 36 ++++++++++++++ scripts/setup_rust_fork.sh | 3 +- scripts/tests.sh | 7 +-- src/bin/cg_clif.rs | 94 ----------------------------------- 10 files changed, 60 insertions(+), 152 deletions(-) delete mode 100644 scripts/config.sh create mode 100644 scripts/rustc-clif.rs delete mode 100644 src/bin/cg_clif.rs diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs index 0a56eb131ed..48faec8bc4b 100644 --- a/build_system/build_backend.rs +++ b/build_system/build_backend.rs @@ -34,18 +34,6 @@ pub(crate) fn build_backend( _ => unreachable!(), } - // Set the rpath to make the cg_clif executable find librustc_codegen_cranelift without changing - // LD_LIBRARY_PATH - if cfg!(unix) { - if cfg!(target_os = "macos") { - rustflags += " -Csplit-debuginfo=unpacked \ - -Clink-arg=-Wl,-rpath,@loader_path/../lib \ - -Zosx-rpath-install-name"; - } else { - rustflags += " -Clink-arg=-Wl,-rpath=$ORIGIN/../lib "; - } - } - cmd.env("RUSTFLAGS", rustflags); eprintln!("[BUILD] rustc_codegen_cranelift"); diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index ac27e77caa7..06feb7cfbcd 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -33,19 +33,16 @@ pub(crate) fn build_sysroot( .join(&cg_clif_dylib); try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path); - try_hard_link( - cg_clif_build_dir.join(get_file_name("cg_clif", "bin")), - target_dir.join("bin").join(get_file_name("cg_clif", "bin")), - ); - - // Build and copy cargo wrapper - let mut build_cargo_wrapper_cmd = Command::new("rustc"); - build_cargo_wrapper_cmd - .arg("scripts/cargo-clif.rs") - .arg("-o") - .arg(target_dir.join("cargo-clif")) - .arg("-g"); - spawn_and_wait(build_cargo_wrapper_cmd); + // Build and copy rustc and cargo wrappers + for wrapper in ["rustc-clif", "cargo-clif"] { + let mut build_cargo_wrapper_cmd = Command::new("rustc"); + build_cargo_wrapper_cmd + .arg(PathBuf::from("scripts").join(format!("{wrapper}.rs"))) + .arg("-o") + .arg(target_dir.join(wrapper)) + .arg("-g"); + spawn_and_wait(build_cargo_wrapper_cmd); + } let default_sysroot = super::rustc_info::get_default_sysroot(); diff --git a/docs/usage.md b/docs/usage.md index 785c7383783..33f146e7ba2 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -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/bin/cg_clif my_crate.rs +$ $cg_clif_dir/build/rustc-clif my_crate.rs ``` ## Jit mode @@ -38,7 +38,7 @@ $ $cg_clif_dir/build/cargo-clif jit or ```bash -$ $cg_clif_dir/build/bin/cg_clif -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic my_crate.rs +$ $cg_clif_dir/build/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 @@ -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/bin/cg_clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic + echo "$@" | $cg_clif_dir/build/rustc-clif - -Zunstable-features -Cllvm-args=mode=jit -Cprefer-dynamic } function jit() { diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs index 41d82b581cd..20aee5ff81b 100644 --- a/scripts/cargo-clif.rs +++ b/scripts/cargo-clif.rs @@ -5,20 +5,11 @@ use std::path::PathBuf; use std::process::Command; fn main() { - if env::var("RUSTC_WRAPPER").map_or(false, |wrapper| wrapper.contains("sccache")) { - eprintln!( - "\x1b[1;93m=== Warning: Unsetting RUSTC_WRAPPER to prevent interference with sccache ===\x1b[0m" - ); - env::remove_var("RUSTC_WRAPPER"); - } - let sysroot = PathBuf::from(env::current_exe().unwrap().parent().unwrap()); - env::set_var("RUSTC", sysroot.join("bin/cg_clif".to_string() + env::consts::EXE_SUFFIX)); - - let mut rustdoc_flags = env::var("RUSTDOCFLAGS").unwrap_or(String::new()); - rustdoc_flags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend="); - rustdoc_flags.push_str( + let mut rustflags = String::new(); + rustflags.push_str(" -Cpanic=abort -Zpanic-abort-tests -Zcodegen-backend="); + rustflags.push_str( sysroot .join(if cfg!(windows) { "bin" } else { "lib" }) .join( @@ -29,9 +20,10 @@ fn main() { .to_str() .unwrap(), ); - rustdoc_flags.push_str(" --sysroot "); - rustdoc_flags.push_str(sysroot.to_str().unwrap()); - env::set_var("RUSTDOCFLAGS", rustdoc_flags); + rustflags.push_str(" --sysroot "); + rustflags.push_str(sysroot.to_str().unwrap()); + env::set_var("RUSTFLAGS", env::var("RUSTFLAGS").unwrap_or(String::new()) + &rustflags); + 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")); diff --git a/scripts/config.sh b/scripts/config.sh deleted file mode 100644 index 53ada369b08..00000000000 --- a/scripts/config.sh +++ /dev/null @@ -1,6 +0,0 @@ -# Note to people running shellcheck: this file should only be sourced, not executed directly. - -set -e - -export LD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$LD_LIBRARY_PATH" -export DYLD_LIBRARY_PATH="$(rustc --print sysroot)/lib:$DYLD_LIBRARY_PATH" diff --git a/scripts/filter_profile.rs b/scripts/filter_profile.rs index f4e863e5494..e6f60d1c0cb 100755 --- a/scripts/filter_profile.rs +++ b/scripts/filter_profile.rs @@ -2,8 +2,7 @@ #![forbid(unsafe_code)]/* This line is ignored by bash # This block is ignored by rustc pushd $(dirname "$0")/../ -source scripts/config.sh -RUSTC="$(pwd)/build/bin/cg_clif" +RUSTC="$(pwd)/build/rustc-clif" popd PROFILE=$1 OUTPUT=$2 exec $RUSTC -Zunstable-options -Cllvm-args=mode=jit -Cprefer-dynamic $0 #*/ diff --git a/scripts/rustc-clif.rs b/scripts/rustc-clif.rs new file mode 100644 index 00000000000..3abfcd8ddc8 --- /dev/null +++ b/scripts/rustc-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("rustc").args(args).exec(); + + #[cfg(not(unix))] + std::process::exit( + Command::new("rustc").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 fa71dfeac07..4d0dfa16c5e 100644 --- a/scripts/setup_rust_fork.sh +++ b/scripts/setup_rust_fork.sh @@ -2,7 +2,6 @@ set -e ./y.rs build --no-unstable-features -source scripts/config.sh echo "[SETUP] Rust fork" git clone https://github.com/rust-lang/rust.git || true @@ -52,7 +51,7 @@ changelog-seen = 2 ninja = false [build] -rustc = "$(pwd)/../build/bin/cg_clif" +rustc = "$(pwd)/../build/rustc-clif" cargo = "$(rustup which cargo)" full-bootstrap = true local-rebuild = true diff --git a/scripts/tests.sh b/scripts/tests.sh index cdde44ac67c..cd6b2181ace 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -2,8 +2,6 @@ set -e -source scripts/config.sh - export CG_CLIF_DISPLAY_CG_TIME=1 export CG_CLIF_DISABLE_INCR_CACHE=1 @@ -31,8 +29,7 @@ if [[ "$(uname)" == 'Darwin' ]]; then export RUSTFLAGS="$RUSTFLAGS -Clink-arg=-undefined -Clink-arg=dynamic_lookup" fi -export RUSTC=false # ensure that cg_llvm isn't accidentally used -MY_RUSTC="$(pwd)/build/bin/cg_clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2" +MY_RUSTC="$(pwd)/build/rustc-clif $RUSTFLAGS -L crate=target/out --out-dir target/out -Cdebuginfo=2" function no_sysroot_tests() { echo "[BUILD] mini_core" @@ -124,7 +121,7 @@ function extended_sysroot_tests() { if [[ "$HOST_TRIPLE" = "$TARGET_TRIPLE" ]]; then echo "[BENCH COMPILE] ebobby/simple-raytracer" hyperfine --runs "${RUN_RUNS:-10}" --warmup 1 --prepare "../build/cargo-clif clean" \ - "RUSTC=rustc RUSTFLAGS='' cargo build" \ + "RUSTFLAGS='' cargo build" \ "../build/cargo-clif build" echo "[BENCH RUN] ebobby/simple-raytracer" diff --git a/src/bin/cg_clif.rs b/src/bin/cg_clif.rs deleted file mode 100644 index 5984ec8412a..00000000000 --- a/src/bin/cg_clif.rs +++ /dev/null @@ -1,94 +0,0 @@ -#![feature(rustc_private)] -#![warn(rust_2018_idioms)] -#![warn(unused_lifetimes)] -#![warn(unreachable_pub)] - -extern crate rustc_data_structures; -extern crate rustc_driver; -extern crate rustc_interface; -extern crate rustc_session; -extern crate rustc_target; - -use std::panic; - -use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry}; -use rustc_interface::interface; -use rustc_session::config::{ErrorOutputType, TrimmedDefPaths}; -use rustc_session::early_error; -use rustc_target::spec::PanicStrategy; - -// FIXME use std::lazy::SyncLazy once it stabilizes -use once_cell::sync::Lazy; - -const BUG_REPORT_URL: &str = "https://github.com/bjorn3/rustc_codegen_cranelift/issues/new"; - -static DEFAULT_HOOK: Lazy) + Sync + Send + 'static>> = - Lazy::new(|| { - let hook = panic::take_hook(); - panic::set_hook(Box::new(|info| { - // Invoke the default handler, which prints the actual panic message and optionally a backtrace - (*DEFAULT_HOOK)(info); - - // Separate the output with an empty line - eprintln!(); - - // Print the ICE message - rustc_driver::report_ice(info, BUG_REPORT_URL); - })); - hook - }); - -#[derive(Default)] -pub struct CraneliftPassesCallbacks { - time_passes: bool, -} - -impl rustc_driver::Callbacks for CraneliftPassesCallbacks { - fn config(&mut self, config: &mut interface::Config) { - // If a --prints=... option has been given, we don't print the "total" - // time because it will mess up the --prints output. See #64339. - self.time_passes = config.opts.prints.is_empty() - && (config.opts.debugging_opts.time_passes || config.opts.debugging_opts.time); - - config.opts.cg.panic = Some(PanicStrategy::Abort); - config.opts.debugging_opts.panic_abort_tests = true; - config.opts.maybe_sysroot = Some(config.opts.maybe_sysroot.clone().unwrap_or_else(|| { - std::env::current_exe().unwrap().parent().unwrap().parent().unwrap().to_owned() - })); - - config.opts.trimmed_def_paths = TrimmedDefPaths::GoodPath; - } -} - -fn main() { - let start_time = std::time::Instant::now(); - let start_rss = get_resident_set_size(); - rustc_driver::init_rustc_env_logger(); - let mut callbacks = CraneliftPassesCallbacks::default(); - Lazy::force(&DEFAULT_HOOK); // Install ice hook - let exit_code = rustc_driver::catch_with_exit_code(|| { - let args = std::env::args_os() - .enumerate() - .map(|(i, arg)| { - arg.into_string().unwrap_or_else(|arg| { - early_error( - ErrorOutputType::default(), - &format!("Argument {} is not valid Unicode: {:?}", i, arg), - ) - }) - }) - .collect::>(); - let mut run_compiler = rustc_driver::RunCompiler::new(&args, &mut callbacks); - run_compiler.set_make_codegen_backend(Some(Box::new(move |_| { - Box::new(rustc_codegen_cranelift::CraneliftCodegenBackend { config: None }) - }))); - run_compiler.run() - }); - - if callbacks.time_passes { - let end_rss = get_resident_set_size(); - print_time_passes_entry("total", start_time.elapsed(), start_rss, end_rss); - } - - std::process::exit(exit_code) -} From 88d058fef3a1b583edf86b326265ed59c614da2e Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 29 Apr 2022 19:47:43 +0200 Subject: [PATCH 10/25] Don't unnecessarily copy rustc_std_workspace_std into the sysroot --- build_system/build_sysroot.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs index 06feb7cfbcd..8682204f4fd 100644 --- a/build_system/build_sysroot.rs +++ b/build_system/build_sysroot.rs @@ -137,7 +137,8 @@ pub(crate) fn build_sysroot( // libstd. for file in fs::read_dir(host_rustlib_lib).unwrap() { let file = file.unwrap().path(); - if file.file_name().unwrap().to_str().unwrap().contains("std-") { + 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())); } } From b84daf82ebe9726623cc8b9e1562f4b8f71368da Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 13:40:16 +0200 Subject: [PATCH 11/25] Update actions/checkout to v3 --- .github/workflows/main.yml | 6 +++--- .github/workflows/nightly-cranelift.yml | 2 +- .github/workflows/rustc.yml | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3aba528abfd..aa556a21bf8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,7 @@ jobs: timeout-minutes: 10 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Install rustfmt run: | @@ -39,7 +39,7 @@ jobs: TARGET_TRIPLE: aarch64-unknown-linux-gnu steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Cache cargo installed crates uses: actions/cache@v2 @@ -127,7 +127,7 @@ jobs: timeout-minutes: 60 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 #- name: Cache cargo installed crates # uses: actions/cache@v2 diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index a019793edd8..11ef7c1ef5e 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -11,7 +11,7 @@ jobs: timeout-minutes: 60 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Cache cargo installed crates uses: actions/cache@v2 diff --git a/.github/workflows/rustc.yml b/.github/workflows/rustc.yml index 1c08e5ece33..b8a98b83ebe 100644 --- a/.github/workflows/rustc.yml +++ b/.github/workflows/rustc.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Cache cargo installed crates uses: actions/cache@v2 @@ -46,7 +46,7 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Cache cargo installed crates uses: actions/cache@v2 From 882c3900db9b65111272dc38d0df094e5b698639 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 13:44:18 +0200 Subject: [PATCH 12/25] Update object for the nightly Cranelift check --- .github/workflows/nightly-cranelift.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nightly-cranelift.yml b/.github/workflows/nightly-cranelift.yml index 11ef7c1ef5e..0a3e7ca073b 100644 --- a/.github/workflows/nightly-cranelift.yml +++ b/.github/workflows/nightly-cranelift.yml @@ -34,7 +34,7 @@ jobs: sed -i 's/cranelift-jit = { version = "\w*.\w*.\w*", optional = true }/cranelift-jit = { git = "https:\/\/github.com\/bytecodealliance\/wasmtime.git", optional = true }/' Cargo.toml sed -i 's/cranelift-object = "\w*.\w*.\w*"/cranelift-object = { git = "https:\/\/github.com\/bytecodealliance\/wasmtime.git" }/' Cargo.toml - sed -i 's/gimli = { version = "0.25.0", default-features = false, features = \["write"\]}/gimli = { version = "0.26.1", default-features = false, features = ["write"] }/' Cargo.toml + sed -i 's/object = { version = "0.27.0"/object = { version = "0.28.0"/' Cargo.toml cat Cargo.toml From f449c0d1756ad2b72240c1961d7d18fc11faba9b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 14:09:03 +0200 Subject: [PATCH 13/25] Remove profile overrides for build scripts and their dependencies Cargo now disables optimizations for build scripts by default anyway, so they aren't really useful anymore. --- Cargo.toml | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 74f50808a98..18d7f41cf40 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,15 +41,5 @@ unstable-features = ["jit", "inline_asm"] jit = ["cranelift-jit", "libloading"] inline_asm = [] -# Disable optimizations and debuginfo of build scripts and some of the heavy build deps, as the -# execution time of build scripts is so fast that optimizing them slows down the total build time. -[profile.release.build-override] -opt-level = 0 -debug = false - -[profile.release.package.cranelift-codegen-meta] -opt-level = 0 -debug = false - [package.metadata.rust-analyzer] rustc_private = true From f3fc94f2399e8244bb78af8e0e5f462b884083ac Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 15:01:57 +0200 Subject: [PATCH 14/25] Fix #[track_caller] with MIR inlining --- scripts/test_rustc_tests.sh | 2 -- src/abi/mod.rs | 30 +++++++++++++-------- src/base.rs | 20 ++++++++------ src/common.rs | 52 +++++++++++++++++++++++++++---------- src/intrinsics/mod.rs | 52 ++++++++++++++++++------------------- 5 files changed, 95 insertions(+), 61 deletions(-) diff --git a/scripts/test_rustc_tests.sh b/scripts/test_rustc_tests.sh index 5afab7c5de5..9bdb9f22c54 100755 --- a/scripts/test_rustc_tests.sh +++ b/scripts/test_rustc_tests.sh @@ -105,8 +105,6 @@ 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/rfc-2091-track-caller/intrinsic-wrapper.rs # wrong result from `Location::caller()` - # bugs in the test suite # ====================== rm src/test/ui/backtrace.rs # TODO warning diff --git a/src/abi/mod.rs b/src/abi/mod.rs index ef56fb191bf..b163a426191 100644 --- a/src/abi/mod.rs +++ b/src/abi/mod.rs @@ -309,7 +309,7 @@ fn codegen_call_argument_operand<'tcx>( pub(crate) fn codegen_terminator_call<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, - span: Span, + source_info: mir::SourceInfo, func: &Operand<'tcx>, args: &[Operand<'tcx>], mir_dest: Option<(Place<'tcx>, BasicBlock)>, @@ -340,7 +340,13 @@ pub(crate) fn codegen_terminator_call<'tcx>( match instance.def { InstanceDef::Intrinsic(_) => { - crate::intrinsics::codegen_intrinsic_call(fx, instance, args, destination, span); + crate::intrinsics::codegen_intrinsic_call( + fx, + instance, + args, + destination, + source_info, + ); return; } InstanceDef::DropGlue(_, None) => { @@ -402,7 +408,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( // Pass the caller location for `#[track_caller]`. if instance.map(|inst| inst.def.requires_caller_location(fx.tcx)).unwrap_or(false) { - let caller_location = fx.get_caller_location(span); + let caller_location = fx.get_caller_location(source_info); args.push(CallArgument { value: caller_location, is_owned: false }); } @@ -479,9 +485,10 @@ 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 { .. }) { - fx.tcx - .sess - .span_fatal(span, &format!("Variadic call for non-C abi {:?}", fn_sig.abi)); + fx.tcx.sess.span_fatal( + source_info.span, + &format!("Variadic call for non-C abi {:?}", fn_sig.abi), + ); } let sig_ref = fx.bcx.func.dfg.call_signature(call_inst).unwrap(); let abi_params = call_args @@ -490,9 +497,10 @@ pub(crate) fn codegen_terminator_call<'tcx>( let ty = fx.bcx.func.dfg.value_type(arg); if !ty.is_int() { // FIXME set %al to upperbound on float args once floats are supported - fx.tcx - .sess - .span_fatal(span, &format!("Non int ty {:?} for variadic call", ty)); + fx.tcx.sess.span_fatal( + source_info.span, + &format!("Non int ty {:?} for variadic call", ty), + ); } AbiParam::new(ty) }) @@ -513,7 +521,7 @@ pub(crate) fn codegen_terminator_call<'tcx>( pub(crate) fn codegen_drop<'tcx>( fx: &mut FunctionCx<'_, '_, 'tcx>, - span: Span, + source_info: mir::SourceInfo, drop_place: CPlace<'tcx>, ) { let ty = drop_place.layout().ty; @@ -560,7 +568,7 @@ pub(crate) fn codegen_drop<'tcx>( if drop_instance.def.requires_caller_location(fx.tcx) { // Pass the caller location for `#[track_caller]`. - let caller_location = fx.get_caller_location(span); + let caller_location = fx.get_caller_location(source_info); call_args.extend( adjust_arg_for_abi(fx, caller_location, &fn_abi.args[1], false).into_iter(), ); diff --git a/src/base.rs b/src/base.rs index 65346cb3962..e59998e4dfa 100644 --- a/src/base.rs +++ b/src/base.rs @@ -325,7 +325,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { AssertKind::BoundsCheck { ref len, ref index } => { let len = codegen_operand(fx, len).load_scalar(fx); let index = codegen_operand(fx, index).load_scalar(fx); - let location = fx.get_caller_location(source_info.span).load_scalar(fx); + let location = fx.get_caller_location(source_info).load_scalar(fx); codegen_panic_inner( fx, @@ -336,7 +336,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { } _ => { let msg_str = msg.description(); - codegen_panic(fx, msg_str, source_info.span); + codegen_panic(fx, msg_str, source_info); } } } @@ -393,12 +393,12 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { func, args, destination, - fn_span, + fn_span: _, cleanup: _, from_hir_call: _, } => { fx.tcx.sess.time("codegen call", || { - crate::abi::codegen_terminator_call(fx, *fn_span, func, args, *destination) + crate::abi::codegen_terminator_call(fx, source_info, func, args, *destination) }); } TerminatorKind::InlineAsm { @@ -450,7 +450,7 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { } TerminatorKind::Drop { place, target, unwind: _ } => { let drop_place = codegen_place(fx, *place); - crate::abi::codegen_drop(fx, source_info.span, drop_place); + crate::abi::codegen_drop(fx, source_info, drop_place); let target_block = fx.get_block(*target); fx.bcx.ins().jump(target_block, &[]); @@ -898,14 +898,18 @@ pub(crate) fn codegen_operand<'tcx>( } } -pub(crate) fn codegen_panic<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, msg_str: &str, span: Span) { - let location = fx.get_caller_location(span).load_scalar(fx); +pub(crate) fn codegen_panic<'tcx>( + fx: &mut FunctionCx<'_, '_, 'tcx>, + msg_str: &str, + source_info: mir::SourceInfo, +) { + let location = fx.get_caller_location(source_info).load_scalar(fx); 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, location]; - codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, span); + codegen_panic_inner(fx, rustc_hir::LangItem::Panic, &args, source_info.span); } pub(crate) fn codegen_panic_inner<'tcx>( diff --git a/src/common.rs b/src/common.rs index ffa629ca16c..f9dc1b5169e 100644 --- a/src/common.rs +++ b/src/common.rs @@ -340,22 +340,46 @@ impl<'tcx> FunctionCx<'_, '_, 'tcx> { self.bcx.set_srcloc(SourceLoc::new(index as u32)); } - pub(crate) fn get_caller_location(&mut self, span: Span) -> CValue<'tcx> { - if let Some(loc) = self.caller_location { - // `#[track_caller]` is used; return caller location instead of current location. - return loc; + // Note: must be kept in sync with get_caller_location from cg_ssa + pub(crate) fn get_caller_location(&mut self, mut source_info: mir::SourceInfo) -> CValue<'tcx> { + let span_to_caller_location = |fx: &mut FunctionCx<'_, '_, 'tcx>, span: Span| { + let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); + let caller = fx.tcx.sess.source_map().lookup_char_pos(topmost.lo()); + let const_loc = fx.tcx.const_caller_location(( + rustc_span::symbol::Symbol::intern( + &caller.file.name.prefer_remapped().to_string_lossy(), + ), + caller.line as u32, + caller.col_display as u32 + 1, + )); + crate::constant::codegen_const_value(fx, const_loc, fx.tcx.caller_location_ty()) + }; + + // Walk up the `SourceScope`s, in case some of them are from MIR inlining. + // If so, the starting `source_info.span` is in the innermost inlined + // function, and will be replaced with outer callsite spans as long + // as the inlined functions were `#[track_caller]`. + loop { + let scope_data = &self.mir.source_scopes[source_info.scope]; + + if let Some((callee, callsite_span)) = scope_data.inlined { + // Stop inside the most nested non-`#[track_caller]` function, + // before ever reaching its caller (which is irrelevant). + if !callee.def.requires_caller_location(self.tcx) { + return span_to_caller_location(self, source_info.span); + } + source_info.span = callsite_span; + } + + // Skip past all of the parents with `inlined: None`. + match scope_data.inlined_parent_scope { + Some(parent) => source_info.scope = parent, + None => break, + } } - let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span); - let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo()); - let const_loc = self.tcx.const_caller_location(( - rustc_span::symbol::Symbol::intern( - &caller.file.name.prefer_remapped().to_string_lossy(), - ), - caller.line as u32, - caller.col_display as u32 + 1, - )); - crate::constant::codegen_const_value(self, const_loc, self.tcx.caller_location_ty()) + // No inlined `SourceScope`s, or all of them were `#[track_caller]`. + self.caller_location.unwrap_or_else(|| span_to_caller_location(self, source_info.span)) } pub(crate) fn anonymous_str(&mut self, msg: &str) -> Value { diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index d76dfca7960..0468a051bcd 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -218,7 +218,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( instance: Instance<'tcx>, args: &[mir::Operand<'tcx>], destination: Option<(CPlace<'tcx>, BasicBlock)>, - span: Span, + source_info: mir::SourceInfo, ) { let intrinsic = fx.tcx.item_name(instance.def_id()); let substs = instance.substs; @@ -232,7 +232,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( fx.bcx.ins().trap(TrapCode::User(0)); } sym::transmute => { - crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", span); + crate::base::codegen_panic(fx, "Transmuting to uninhabited type.", source_info); } _ => unimplemented!("unsupported instrinsic {}", intrinsic), } @@ -241,7 +241,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( }; if intrinsic.as_str().starts_with("simd_") { - self::simd::codegen_simd_intrinsic_call(fx, intrinsic, substs, args, ret, span); + self::simd::codegen_simd_intrinsic_call(fx, intrinsic, substs, args, ret, source_info.span); let ret_block = fx.get_block(destination.expect("SIMD intrinsics don't diverge").1); fx.bcx.ins().jump(ret_block, &[]); } else if codegen_float_intrinsic_call(fx, intrinsic, args, ret) { @@ -255,7 +255,7 @@ pub(crate) fn codegen_intrinsic_call<'tcx>( substs, args, ret, - span, + source_info, destination, ); } @@ -339,7 +339,7 @@ fn codegen_regular_intrinsic_call<'tcx>( substs: SubstsRef<'tcx>, args: &[mir::Operand<'tcx>], ret: CPlace<'tcx>, - span: Span, + source_info: mir::SourceInfo, destination: Option<(CPlace<'tcx>, BasicBlock)>, ) { let usize_layout = fx.layout_of(fx.tcx.types.usize); @@ -347,7 +347,7 @@ fn codegen_regular_intrinsic_call<'tcx>( intrinsic_match! { fx, intrinsic, args, _ => { - fx.tcx.sess.span_fatal(span, &format!("unsupported intrinsic {}", intrinsic)); + fx.tcx.sess.span_fatal(source_info.span, &format!("unsupported intrinsic {}", intrinsic)); }; assume, (c _a) {}; @@ -658,7 +658,7 @@ fn codegen_regular_intrinsic_call<'tcx>( crate::base::codegen_panic( fx, &format!("attempted to instantiate uninhabited type `{}`", layout.ty), - span, + source_info, ) }); return; @@ -669,7 +669,7 @@ fn codegen_regular_intrinsic_call<'tcx>( crate::base::codegen_panic( fx, &format!("attempted to zero-initialize type `{}`, which is invalid", layout.ty), - span, + source_info, ); }); return; @@ -680,7 +680,7 @@ fn codegen_regular_intrinsic_call<'tcx>( crate::base::codegen_panic( fx, &format!("attempted to leave type `{}` uninitialized, which is invalid", layout.ty), - span, + source_info, ) }); return; @@ -735,7 +735,7 @@ fn codegen_regular_intrinsic_call<'tcx>( }; caller_location, () { - let caller_location = fx.get_caller_location(span); + let caller_location = fx.get_caller_location(source_info); ret.write_cvalue(fx, caller_location); }; @@ -758,12 +758,12 @@ fn codegen_regular_intrinsic_call<'tcx>( fx.bcx.ins().jump(ret_block, &[]); return; } else { - fx.tcx.sess.span_fatal(span, "128bit atomics not yet supported"); + fx.tcx.sess.span_fatal(source_info.span, "128bit atomics not yet supported"); } } ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty); return; } } @@ -786,12 +786,12 @@ fn codegen_regular_intrinsic_call<'tcx>( fx.bcx.ins().jump(ret_block, &[]); return; } else { - fx.tcx.sess.span_fatal(span, "128bit atomics not yet supported"); + fx.tcx.sess.span_fatal(source_info.span, "128bit atomics not yet supported"); } } ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, ty); return; } } @@ -805,7 +805,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -823,7 +823,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -843,7 +843,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -861,7 +861,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -879,7 +879,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -897,7 +897,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -915,7 +915,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -933,7 +933,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -951,7 +951,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -969,7 +969,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -987,7 +987,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } @@ -1005,7 +1005,7 @@ fn codegen_regular_intrinsic_call<'tcx>( match layout.ty.kind() { ty::Uint(_) | ty::Int(_) | ty::RawPtr(..) => {} _ => { - report_atomic_type_validation_error(fx, intrinsic, span, layout.ty); + report_atomic_type_validation_error(fx, intrinsic, source_info.span, layout.ty); return; } } From f52162f75c640618637e265d005f0f5f25811af5 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 30 Apr 2022 16:21:43 +0200 Subject: [PATCH 15/25] Fix #[track_caller] location for function chains --- src/base.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/base.rs b/src/base.rs index e59998e4dfa..c9626a16be4 100644 --- a/src/base.rs +++ b/src/base.rs @@ -393,12 +393,18 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, '_>) { func, args, destination, - fn_span: _, + fn_span, cleanup: _, from_hir_call: _, } => { fx.tcx.sess.time("codegen call", || { - crate::abi::codegen_terminator_call(fx, source_info, func, args, *destination) + crate::abi::codegen_terminator_call( + fx, + mir::SourceInfo { span: *fn_span, ..source_info }, + func, + args, + *destination, + ) }); } TerminatorKind::InlineAsm { From ddde38d1323f103c55710a8b9599feb0f76ea186 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 1 May 2022 15:54:12 +0200 Subject: [PATCH 16/25] Fix jit mode with cargo-clif wrapper --- scripts/cargo-clif.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/cargo-clif.rs b/scripts/cargo-clif.rs index 20aee5ff81b..9362b47fa6d 100644 --- a/scripts/cargo-clif.rs +++ b/scripts/cargo-clif.rs @@ -38,7 +38,7 @@ fn main() { .chain(env::args().skip(2)) .chain([ "--".to_string(), - "-Zunstable-features".to_string(), + "-Zunstable-options".to_string(), "-Cllvm-args=mode=jit".to_string(), ]) .collect() @@ -52,7 +52,7 @@ fn main() { .chain(env::args().skip(2)) .chain([ "--".to_string(), - "-Zunstable-features".to_string(), + "-Zunstable-options".to_string(), "-Cllvm-args=mode=jit-lazy".to_string(), ]) .collect() From eb881b486f7c71cea87fbcddf728c741e397a779 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 6 May 2022 18:12:01 +0200 Subject: [PATCH 17/25] Add missing comma in vscode settings --- .vscode/settings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 74fde9c27c0..ecb20f22d8c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,7 +5,7 @@ "rust-analyzer.assist.importEnforceGranularity": true, "rust-analyzer.assist.importPrefix": "crate", "rust-analyzer.cargo.runBuildScripts": true, - "rust-analyzer.cargo.features": ["unstable-features"] + "rust-analyzer.cargo.features": ["unstable-features"], "rust-analyzer.linkedProjects": [ "./Cargo.toml", //"./build_sysroot/sysroot_src/src/libstd/Cargo.toml", From 74b9232ee8001b6204a3c357a7793a6d152bd8ca Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 6 May 2022 18:26:20 +0200 Subject: [PATCH 18/25] Fix assert_assignable for array types Fixes #1226 --- src/value_and_place.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/value_and_place.rs b/src/value_and_place.rs index 8f80b02ae0d..f8da18ee3e2 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -828,6 +828,9 @@ pub(crate) fn assert_assignable<'tcx>( } } } + (ty::Array(a, _), ty::Array(b, _)) => { + assert_assignable(fx, *a, *b) + } _ => { assert_eq!( from_ty, to_ty, From acf93987bc43dc2f9aba0984ec60131f17dc40fb Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Fri, 6 May 2022 18:54:13 +0200 Subject: [PATCH 19/25] Rustfmt --- src/value_and_place.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/value_and_place.rs b/src/value_and_place.rs index f8da18ee3e2..a68225de58b 100644 --- a/src/value_and_place.rs +++ b/src/value_and_place.rs @@ -828,9 +828,7 @@ pub(crate) fn assert_assignable<'tcx>( } } } - (ty::Array(a, _), ty::Array(b, _)) => { - assert_assignable(fx, *a, *b) - } + (ty::Array(a, _), ty::Array(b, _)) => assert_assignable(fx, *a, *b), _ => { assert_eq!( from_ty, to_ty, From 25f7711fea4f83a82d54c17dff5cc5b9a2b2937b Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sat, 7 May 2022 14:56:36 +0200 Subject: [PATCH 20/25] Rustup to rustc 1.62.0-nightly (77652b9ef 2022-05-06) --- build_sysroot/Cargo.lock | 12 ++++++------ rust-toolchain | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/build_sysroot/Cargo.lock b/build_sysroot/Cargo.lock index 51ba0dbfcc7..efee6ef3f37 100644 --- a/build_sysroot/Cargo.lock +++ b/build_sysroot/Cargo.lock @@ -112,9 +112,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.12.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758" +checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3" dependencies = [ "compiler_builtins", "rustc-std-workspace-alloc", @@ -134,18 +134,18 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.124" +version = "0.2.125" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" +checksum = "5916d2ae698f6de9bfb891ad7a8d65c09d232dc58cc4ac433c7da3b2fd84bc2b" dependencies = [ "rustc-std-workspace-core", ] [[package]] name = "memchr" -version = "2.4.1" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" dependencies = [ "compiler_builtins", "rustc-std-workspace-core", diff --git a/rust-toolchain b/rust-toolchain index 2fa0ac3f772..30c81d76222 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-04-27" +channel = "nightly-2022-05-07" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] From e5f011aba629bf4fc94e7faa2a0e05fd9a8c2ea6 Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Mon, 9 May 2022 16:34:16 +0100 Subject: [PATCH 21/25] Only enable JIT tests on x86_64 Cranelift currently only supports JIT on x86_64 targets. Disable JIT tests on all other targets, so that failing tests are ignored. --- scripts/tests.sh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/scripts/tests.sh b/scripts/tests.sh index cd6b2181ace..9b5ffa40960 100755 --- a/scripts/tests.sh +++ b/scripts/tests.sh @@ -9,7 +9,16 @@ export HOST_TRIPLE=$(rustc -vV | grep host | cut -d: -f2 | tr -d " ") export TARGET_TRIPLE=${TARGET_TRIPLE:-$HOST_TRIPLE} export RUN_WRAPPER='' -export JIT_SUPPORTED=1 + +case "$TARGET_TRIPLE" in + x86_64*) + export JIT_SUPPORTED=1 + ;; + *) + export JIT_SUPPORTED=0 + ;; +esac + if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then export JIT_SUPPORTED=0 if [[ "$TARGET_TRIPLE" == "aarch64-unknown-linux-gnu" ]]; then From 343c532f4f8bda436bf75250f9eb06d5aee245b4 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Wed, 11 May 2022 10:49:31 +0000 Subject: [PATCH 22/25] Use cfg(any()) instead of cfg(disabled) Rustbuild uses --check-cfg without allowing disabled as cfg name --- src/base.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base.rs b/src/base.rs index c9626a16be4..65e5812a8a5 100644 --- a/src/base.rs +++ b/src/base.rs @@ -477,7 +477,7 @@ fn codegen_stmt<'tcx>( fx.set_debug_loc(stmt.source_info); - #[cfg(disabled)] + #[cfg(any())] // This is never true match &stmt.kind { StatementKind::StorageLive(..) | StatementKind::StorageDead(..) => {} // Those are not very useful _ => { From 06817d1bb34398718c501d67edd6ecc07ca1723e Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 9 Apr 2022 01:27:47 -0700 Subject: [PATCH 23/25] Add `unsigned_offset_from` on pointers Like we have `add`/`sub` which are the `usize` version of `offset`, this adds the `usize` equivalent of `offset_from`. Like how `.add(d)` replaced a whole bunch of `.offset(d as isize)`, you can see from the changes here that it's fairly common that code actually knows the order between the pointers and *wants* a `usize`, not an `isize`. As a bonus, this can do `sub nuw`+`udiv exact`, rather than `sub`+`sdiv exact`, which can be optimized slightly better because it doesn't have to worry about negatives. That's why the slice iterators weren't using `offset_from`, though I haven't updated that code in this PR because slices are so perf-critical that I'll do it as its own change. This is an intrinsic, like `offset_from`, so that it can eventually be allowed in CTFE. It also allows checking the extra safety condition -- see the test confirming that CTFE catches it if you pass the pointers in the wrong order. --- src/intrinsics/mod.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index d76dfca7960..b254ca3bec8 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -713,14 +713,21 @@ fn codegen_regular_intrinsic_call<'tcx>( ret.write_cvalue(fx, val); }; - ptr_offset_from, (v ptr, v base) { + ptr_offset_from | ptr_offset_from_unsigned, (v ptr, v base) { let ty = substs.type_at(0); let isize_layout = fx.layout_of(fx.tcx.types.isize); let pointee_size: u64 = fx.layout_of(ty).size.bytes(); - let diff = fx.bcx.ins().isub(ptr, base); + let diff_bytes = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. - let val = CValue::by_val(fx.bcx.ins().sdiv_imm(diff, pointee_size as i64), isize_layout); + let diff = if intrinsic == sym::ptr_offset_from_unsigned { + // Because diff_bytes ULT isize::MAX, this would be fine as signed, + // but unsigned is slightly easier to codegen, so might as well. + fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64) + } else { + fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64) + }; + let val = CValue::by_val(diff, isize_layout); ret.write_cvalue(fx, val); }; From bb7b844d7c47dfcac42a283140a60d2635d42178 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 9 Apr 2022 14:14:35 -0700 Subject: [PATCH 24/25] Rename `unsigned_offset_from` to `sub_ptr` --- src/intrinsics/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index b254ca3bec8..f7a83373e87 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -721,7 +721,7 @@ fn codegen_regular_intrinsic_call<'tcx>( let diff_bytes = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. let diff = if intrinsic == sym::ptr_offset_from_unsigned { - // Because diff_bytes ULT isize::MAX, this would be fine as signed, + // Because diff_bytes ULE isize::MAX, this would be fine as signed, // but unsigned is slightly easier to codegen, so might as well. fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64) } else { From 63734fcdd718cca089f84c42f3a42c0096cfd431 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Sun, 15 May 2022 12:22:00 +0200 Subject: [PATCH 25/25] Rustup to rustc 1.62.0-nightly (70b3681bf 2022-05-14) --- patches/0027-sysroot-128bit-atomic-operations.patch | 10 ++++++---- rust-toolchain | 2 +- src/intrinsics/mod.rs | 10 +++++----- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/patches/0027-sysroot-128bit-atomic-operations.patch b/patches/0027-sysroot-128bit-atomic-operations.patch index 8e6652af374..ce1c6c99b40 100644 --- a/patches/0027-sysroot-128bit-atomic-operations.patch +++ b/patches/0027-sysroot-128bit-atomic-operations.patch @@ -21,7 +21,7 @@ index 092b7cf..158cf71 100644 -#[cfg(target_has_atomic_load_store = "128")] -#[unstable(feature = "integer_atomics", issue = "32976")] -impl RefUnwindSafe for crate::sync::atomic::AtomicI128 {} - + #[cfg(target_has_atomic_load_store = "ptr")] #[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")] @@ -235,9 +232,6 @@ impl RefUnwindSafe for crate::sync::atomic::AtomicU32 {} @@ -31,14 +31,14 @@ index 092b7cf..158cf71 100644 -#[cfg(target_has_atomic_load_store = "128")] -#[unstable(feature = "integer_atomics", issue = "32976")] -impl RefUnwindSafe for crate::sync::atomic::AtomicU128 {} - + #[cfg(target_has_atomic_load_store = "8")] #[stable(feature = "unwind_safe_atomic_refs", since = "1.14.0")] diff --git a/library/core/src/sync/atomic.rs b/library/core/src/sync/atomic.rs index d9de37e..8293fce 100644 --- a/library/core/src/sync/atomic.rs +++ b/library/core/src/sync/atomic.rs -@@ -2234,44 +2234,6 @@ atomic_int! { +@@ -2234,46 +2234,6 @@ atomic_int! { "AtomicU64::new(0)", u64 AtomicU64 ATOMIC_U64_INIT } @@ -54,6 +54,7 @@ index d9de37e..8293fce 100644 - unstable(feature = "integer_atomics", issue = "32976"), - rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), - unstable(feature = "integer_atomics", issue = "32976"), +- cfg_attr(not(test), rustc_diagnostic_item = "AtomicI128"), - "i128", - "#![feature(integer_atomics)]\n\n", - atomic_min, atomic_max, @@ -73,6 +74,7 @@ index d9de37e..8293fce 100644 - unstable(feature = "integer_atomics", issue = "32976"), - rustc_const_stable(feature = "const_integer_atomics", since = "1.34.0"), - unstable(feature = "integer_atomics", issue = "32976"), +- cfg_attr(not(test), rustc_diagnostic_item = "AtomicU128"), - "u128", - "#![feature(integer_atomics)]\n\n", - atomic_umin, atomic_umax, @@ -98,6 +100,6 @@ index b735957..ea728b6 100644 #[cfg(target_has_atomic = "ptr")] assert_eq!(align_of::(), size_of::()); #[cfg(target_has_atomic = "ptr")] --- +-- 2.26.2.7.g19db9cfb68 diff --git a/rust-toolchain b/rust-toolchain index 30c81d76222..e98e92e468e 100644 --- a/rust-toolchain +++ b/rust-toolchain @@ -1,3 +1,3 @@ [toolchain] -channel = "nightly-2022-05-07" +channel = "nightly-2022-05-15" components = ["rust-src", "rustc-dev", "llvm-tools-preview"] diff --git a/src/intrinsics/mod.rs b/src/intrinsics/mod.rs index 458a2addb27..29b3f36b2be 100644 --- a/src/intrinsics/mod.rs +++ b/src/intrinsics/mod.rs @@ -715,19 +715,19 @@ fn codegen_regular_intrinsic_call<'tcx>( ptr_offset_from | ptr_offset_from_unsigned, (v ptr, v base) { let ty = substs.type_at(0); - let isize_layout = fx.layout_of(fx.tcx.types.isize); let pointee_size: u64 = fx.layout_of(ty).size.bytes(); let diff_bytes = fx.bcx.ins().isub(ptr, base); // FIXME this can be an exact division. - let diff = if intrinsic == sym::ptr_offset_from_unsigned { + let val = if intrinsic == sym::ptr_offset_from_unsigned { + let usize_layout = fx.layout_of(fx.tcx.types.usize); // Because diff_bytes ULE isize::MAX, this would be fine as signed, // but unsigned is slightly easier to codegen, so might as well. - fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64) + CValue::by_val(fx.bcx.ins().udiv_imm(diff_bytes, pointee_size as i64), usize_layout) } else { - fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64) + let isize_layout = fx.layout_of(fx.tcx.types.isize); + CValue::by_val(fx.bcx.ins().sdiv_imm(diff_bytes, pointee_size as i64), isize_layout) }; - let val = CValue::by_val(diff, isize_layout); ret.write_cvalue(fx, val); };