Skip to content

Commit

Permalink
Auto merge of rust-lang#122599 - bjorn3:sync_cg_clif-2024-03-16, r=bj…
Browse files Browse the repository at this point in the history
…orn3

Subtree sync for rustc_codegen_cranelift

The main highlight this time is a fix for a recently introduced ICE.

Fixes rust-lang#122399

r? `@ghost`

`@rustbot` label +A-codegen +A-cranelift +T-compiler
  • Loading branch information
bors committed Mar 16, 2024
2 parents 22e241e + 6697186 commit 2ffa3c8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 30 deletions.
30 changes: 19 additions & 11 deletions compiler/rustc_codegen_cranelift/.github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,9 @@ jobs:
env:
TARGET_TRIPLE: x86_64-apple-darwin
# cross-compile from Linux to Windows using mingw
# FIXME The wine version in Ubuntu 22.04 is missing ProcessPrng
#- os: ubuntu-latest
# env:
# TARGET_TRIPLE: x86_64-pc-windows-gnu
- os: ubuntu-latest
env:
TARGET_TRIPLE: x86_64-pc-windows-gnu
- os: ubuntu-latest
env:
TARGET_TRIPLE: aarch64-unknown-linux-gnu
Expand Down Expand Up @@ -81,11 +80,11 @@ jobs:
if: matrix.os == 'windows-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
run: rustup set default-host x86_64-pc-windows-gnu

#- name: Install MinGW toolchain and wine
# if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
# run: |
# sudo apt-get update
# sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable
- name: Install MinGW toolchain and wine
if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
run: |
sudo apt-get update
sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable
- name: Install AArch64 toolchain and qemu
if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'aarch64-unknown-linux-gnu'
Expand All @@ -108,6 +107,15 @@ jobs:
- name: Prepare dependencies
run: ./y.sh prepare

# The Wine version shipped with Ubuntu 22.04 doesn't implement bcryptprimitives.dll
- name: Build bcryptprimitives.dll shim for Wine
if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
run: |
rustup target add x86_64-pc-windows-gnu
mkdir wine_shims
rustc patches/bcryptprimitives.rs -Copt-level=3 -Clto=fat --out-dir wine_shims --target x86_64-pc-windows-gnu
echo "WINEPATH=$(pwd)/wine_shims" >> $GITHUB_ENV
- name: Build
run: ./y.sh build --sysroot none

Expand Down Expand Up @@ -234,11 +242,11 @@ jobs:
if: matrix.os == 'windows-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
run: rustup set default-host x86_64-pc-windows-gnu

- name: Install MinGW toolchain and wine
- name: Install MinGW toolchain
if: matrix.os == 'ubuntu-latest' && matrix.env.TARGET_TRIPLE == 'x86_64-pc-windows-gnu'
run: |
sudo apt-get update
sudo apt-get install -y gcc-mingw-w64-x86-64 wine-stable
sudo apt-get install -y gcc-mingw-w64-x86-64
- name: Prepare dependencies
run: ./y.sh prepare
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_codegen_cranelift/example/std_example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ fn main() {
transmute_fat_pointer();

rust_call_abi();

const fn no_str() -> Option<Box<str>> {
None
}

static STATIC_WITH_MAYBE_NESTED_BOX: &Option<Box<str>> = &no_str();

println!("{:?}", STATIC_WITH_MAYBE_NESTED_BOX);
}

fn panic(_: u128) {
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_codegen_cranelift/patches/bcryptprimitives.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Shim for bcryptprimitives.dll. The Wine version shipped with Ubuntu 22.04
// doesn't support it yet. Authored by @ChrisDenton

#![crate_type = "cdylib"]
#![allow(nonstandard_style)]

#[no_mangle]
pub unsafe extern "system" fn ProcessPrng(mut pbData: *mut u8, mut cbData: usize) -> i32 {
while cbData > 0 {
let size = core::cmp::min(cbData, u32::MAX as usize);
RtlGenRandom(pbData, size as u32);
cbData -= size;
pbData = pbData.add(size);
}
1
}

#[link(name = "advapi32")]
extern "system" {
#[link_name = "SystemFunction036"]
pub fn RtlGenRandom(RandomBuffer: *mut u8, RandomBufferLength: u32) -> u8;
}
7 changes: 2 additions & 5 deletions compiler/rustc_codegen_cranelift/patches/stdlib-lock.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,9 @@ checksum = "0942ffc6dcaadf03badf6e6a2d0228460359d5e34b57ccdc720b7382dfbd5ec5"

[[package]]
name = "cc"
version = "1.0.83"
version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"

[[package]]
name = "cfg-if"
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_cranelift/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-03-08"
channel = "nightly-2024-03-16"
components = ["rust-src", "rustc-dev", "llvm-tools"]
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_cranelift/scripts/test_rustc_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ rm -r tests/run-make/symbols-include-type-name # --emit=asm not supported
rm -r tests/run-make/target-specs # i686 not supported by Cranelift
rm -r tests/run-make/mismatching-target-triples # same
rm tests/ui/asm/x86_64/issue-96797.rs # const and sym inline asm operands don't work entirely correctly
rm tests/ui/asm/x86_64/goto.rs # inline asm labels not supported

# requires LTO
rm -r tests/run-make/cdylib
Expand Down Expand Up @@ -121,6 +122,7 @@ rm -r tests/run-make/optimization-remarks-dir # remarks are LLVM specific
rm tests/ui/mir/mir_misc_casts.rs # depends on deduplication of constants
rm tests/ui/mir/mir_raw_fat_ptr.rs # same
rm tests/ui/consts/issue-33537.rs # same
rm tests/ui/consts/const-mut-refs-crate.rs # same

# rustdoc-clif passes extra args, suppressing the help message when no args are passed
rm -r tests/run-make/issue-88756-default-output
Expand Down
42 changes: 29 additions & 13 deletions compiler/rustc_codegen_cranelift/src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ pub(crate) fn codegen_tls_ref<'tcx>(
let call = fx.bcx.ins().call(func_ref, &[]);
fx.bcx.func.dfg.first_result(call)
} else {
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let data_id = data_id_for_static(
fx.tcx, fx.module, def_id, false,
// For a declaration the stated mutability doesn't matter.
false,
);
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
Expand Down Expand Up @@ -164,7 +168,11 @@ pub(crate) fn codegen_const_value<'tcx>(
}
GlobalAlloc::Static(def_id) => {
assert!(fx.tcx.is_static(def_id));
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
let data_id = data_id_for_static(
fx.tcx, fx.module, def_id, false,
// For a declaration the stated mutability doesn't matter.
false,
);
let local_data_id =
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
if fx.clif_comments.enabled() {
Expand Down Expand Up @@ -232,21 +240,19 @@ fn data_id_for_static(
module: &mut dyn Module,
def_id: DefId,
definition: bool,
definition_writable: bool,
) -> DataId {
let attrs = tcx.codegen_fn_attrs(def_id);

let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
let symbol_name = tcx.symbol_name(instance).name;
let ty = instance.ty(tcx, ParamEnv::reveal_all());
let is_mutable = if tcx.is_mutable_static(def_id) {
true
} else {
!ty.is_freeze(tcx, ParamEnv::reveal_all())
};
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();

if let Some(import_linkage) = attrs.import_linkage {
assert!(!definition);
assert!(!tcx.is_mutable_static(def_id));

let ty = instance.ty(tcx, ParamEnv::reveal_all());
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();

let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak
|| import_linkage == rustc_middle::mir::mono::Linkage::WeakAny
Expand All @@ -259,7 +265,7 @@ fn data_id_for_static(
let data_id = match module.declare_data(
symbol_name,
linkage,
is_mutable,
false,
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
) {
Ok(data_id) => data_id,
Expand Down Expand Up @@ -307,7 +313,7 @@ fn data_id_for_static(
let data_id = match module.declare_data(
symbol_name,
linkage,
is_mutable,
definition_writable,
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
) {
Ok(data_id) => data_id,
Expand Down Expand Up @@ -341,7 +347,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant

let alloc = tcx.eval_static_initializer(def_id).unwrap();

let data_id = data_id_for_static(tcx, module, def_id, true);
let data_id = data_id_for_static(
tcx,
module,
def_id,
true,
alloc.inner().mutability == Mutability::Mut,
);
(data_id, alloc, section_name)
}
};
Expand Down Expand Up @@ -421,7 +433,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
// Don't push a `TodoItem::Static` here, as it will cause statics used by
// multiple crates to be duplicated between them. It isn't necessary anyway,
// as it will get pushed by `codegen_static` when necessary.
data_id_for_static(tcx, module, def_id, false)
data_id_for_static(
tcx, module, def_id, false,
// For a declaration the stated mutability doesn't matter.
false,
)
}
};

Expand Down

0 comments on commit 2ffa3c8

Please sign in to comment.