Skip to content

Commit 40dcd79

Browse files
committed
Auto merge of #124302 - matthiaskrgr:rollup-2aya8n8, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - #124003 (Dellvmize some intrinsics (use `u32` instead of `Self` in some integer intrinsics)) - #124169 (Don't fatal when calling `expect_one_of` when recovering arg in `parse_seq`) - #124286 (Subtree sync for rustc_codegen_cranelift) r? `@ghost` `@rustbot` modify labels: rollup
2 parents c2f2db7 + a760954 commit 40dcd79

File tree

46 files changed

+482
-263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+482
-263
lines changed

compiler/rustc_codegen_cranelift/Cargo.lock

+90-149
Large diffs are not rendered by default.

compiler/rustc_codegen_cranelift/Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ crate-type = ["dylib"]
88

99
[dependencies]
1010
# These have to be in sync with each other
11-
cranelift-codegen = { version = "0.106.0", default-features = false, features = ["std", "unwind", "all-arch"] }
12-
cranelift-frontend = { version = "0.106.0" }
13-
cranelift-module = { version = "0.106.0" }
14-
cranelift-native = { version = "0.106.0" }
15-
cranelift-jit = { version = "0.106.0", optional = true }
16-
cranelift-object = { version = "0.106.0" }
11+
cranelift-codegen = { version = "0.107.0", default-features = false, features = ["std", "unwind", "all-arch"] }
12+
cranelift-frontend = { version = "0.107.0" }
13+
cranelift-module = { version = "0.107.0" }
14+
cranelift-native = { version = "0.107.0" }
15+
cranelift-jit = { version = "0.107.0", optional = true }
16+
cranelift-object = { version = "0.107.0" }
1717
target-lexicon = "0.12.0"
1818
gimli = { version = "0.28", default-features = false, features = ["write"]}
19-
object = { version = "0.32", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
19+
object = { version = "0.33", default-features = false, features = ["std", "read_core", "write", "archive", "coff", "elf", "macho", "pe"] }
2020

2121
indexmap = "2.0.0"
2222
libloading = { version = "0.8.0", optional = true }

compiler/rustc_codegen_cranelift/build_system/abi_cafe.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,13 @@ pub(crate) fn run(
4343
let mut cmd = ABI_CAFE.run(bootstrap_host_compiler, dirs);
4444
cmd.arg("--");
4545
cmd.arg("--pairs");
46-
cmd.args(pairs);
46+
cmd.args(
47+
if cfg!(not(any(target_os = "macos", all(target_os = "windows", target_env = "msvc")))) {
48+
&pairs[..]
49+
} else {
50+
&pairs[..2]
51+
},
52+
);
4753
cmd.arg("--add-rustc-codegen-backend");
4854
match cg_clif_dylib {
4955
CodegenBackend::Local(path) => {

compiler/rustc_codegen_cranelift/build_system/tests.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ pub(crate) fn run_tests(
290290
&& !skip_tests.contains(&"testsuite.extended_sysroot");
291291

292292
if run_base_sysroot || run_extended_sysroot {
293-
let mut target_compiler = build_sysroot::build_sysroot(
293+
let target_compiler = build_sysroot::build_sysroot(
294294
dirs,
295295
channel,
296296
sysroot_kind,
@@ -299,11 +299,8 @@ pub(crate) fn run_tests(
299299
rustup_toolchain_name,
300300
target_triple.clone(),
301301
);
302-
// Rust's build system denies a couple of lints that trigger on several of the test
303-
// projects. Changing the code to fix them is not worth it, so just silence all lints.
304-
target_compiler.rustflags.push("--cap-lints=allow".to_owned());
305302

306-
let runner = TestRunner::new(
303+
let mut runner = TestRunner::new(
307304
dirs.clone(),
308305
target_compiler,
309306
use_unstable_features,
@@ -319,6 +316,9 @@ pub(crate) fn run_tests(
319316
}
320317

321318
if run_extended_sysroot {
319+
// Rust's build system denies a couple of lints that trigger on several of the test
320+
// projects. Changing the code to fix them is not worth it, so just silence all lints.
321+
runner.target_compiler.rustflags.push("--cap-lints=allow".to_owned());
322322
runner.run_testsuite(EXTENDED_SYSROOT_SUITE);
323323
} else {
324324
eprintln!("[SKIP] extended_sysroot tests");

compiler/rustc_codegen_cranelift/example/alloc_example.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(start, core_intrinsics, alloc_error_handler, lang_items)]
2+
#![allow(internal_features)]
23
#![no_std]
34

45
extern crate alloc;

compiler/rustc_codegen_cranelift/example/alloc_system.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ mod platform {
8080
extern "system" {
8181
fn GetProcessHeap() -> HANDLE;
8282
fn HeapAlloc(hHeap: HANDLE, dwFlags: DWORD, dwBytes: SIZE_T) -> LPVOID;
83-
fn HeapReAlloc(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID, dwBytes: SIZE_T) -> LPVOID;
8483
fn HeapFree(hHeap: HANDLE, dwFlags: DWORD, lpMem: LPVOID) -> BOOL;
8584
fn GetLastError() -> DWORD;
8685
}
@@ -111,7 +110,7 @@ mod platform {
111110
allocate_with_flags(layout, HEAP_ZERO_MEMORY)
112111
}
113112
#[inline]
114-
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
113+
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
115114
let header = get_header(ptr);
116115
let err = HeapFree(GetProcessHeap(), 0, header.0 as LPVOID);
117116
debug_assert!(err != 0, "Failed to free heap memory: {}", GetLastError());

compiler/rustc_codegen_cranelift/example/example.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ pub fn array_as_slice(arr: &[u8; 3]) -> &[u8] {
149149
arr
150150
}
151151

152-
pub unsafe fn use_ctlz_nonzero(a: u16) -> u16 {
152+
pub unsafe fn use_ctlz_nonzero(a: u16) -> u32 {
153153
intrinsics::ctlz_nonzero(a)
154154
}
155155

compiler/rustc_codegen_cranelift/example/float-minmax-pass.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test that the simd_f{min,max} intrinsics produce the correct results.
66

77
#![feature(repr_simd, core_intrinsics)]
8-
#![allow(non_camel_case_types)]
8+
#![allow(internal_features, non_camel_case_types)]
99

1010
#[repr(simd)]
1111
#[derive(Copy, Clone, PartialEq, Debug)]

compiler/rustc_codegen_cranelift/example/mini_core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ pub mod intrinsics {
627627
pub fn min_align_of_val<T: ?::Sized>(val: *const T) -> usize;
628628
pub fn copy<T>(src: *const T, dst: *mut T, count: usize);
629629
pub fn transmute<T, U>(e: T) -> U;
630-
pub fn ctlz_nonzero<T>(x: T) -> T;
630+
pub fn ctlz_nonzero<T>(x: T) -> u32;
631631
#[rustc_safe_intrinsic]
632632
pub fn needs_drop<T: ?::Sized>() -> bool;
633633
#[rustc_safe_intrinsic]

compiler/rustc_codegen_cranelift/example/mod_bench.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(start, core_intrinsics, lang_items)]
2+
#![allow(internal_features)]
23
#![no_std]
34

45
#[cfg_attr(unix, link(name = "c"))]

compiler/rustc_codegen_cranelift/example/neon.rs

+2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
#[cfg(target_arch = "aarch64")]
66
use std::arch::aarch64::*;
7+
#[cfg(target_arch = "aarch64")]
78
use std::mem::transmute;
9+
#[cfg(target_arch = "aarch64")]
810
use std::simd::*;
911

1012
#[cfg(target_arch = "aarch64")]

compiler/rustc_codegen_cranelift/example/std_example.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
tuple_trait,
88
unboxed_closures
99
)]
10+
#![allow(internal_features)]
1011

1112
#[cfg(target_arch = "x86_64")]
1213
use std::arch::x86_64::*;

compiler/rustc_codegen_cranelift/patches/0001-abi-cafe-Disable-some-test-on-x86_64-pc-windows-gnu.patch

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ diff --git a/src/report.rs b/src/report.rs
1111
index eeec614..f582867 100644
1212
--- a/src/report.rs
1313
+++ b/src/report.rs
14-
@@ -48,6 +48,12 @@ pub fn get_test_rules(test: &TestKey, caller: &dyn AbiImpl, callee: &dyn AbiImpl
14+
@@ -48,6 +48,15 @@ pub fn get_test_rules(test: &TestKey, caller: &dyn AbiImpl, callee: &dyn AbiImpl
1515
//
1616
// THIS AREA RESERVED FOR VENDORS TO APPLY PATCHES
1717

1818
+ // x86_64-pc-windows-gnu has some broken i128 tests that aren't disabled by default
1919
+ if cfg!(all(target_os = "windows", target_env = "gnu")) && test.test_name == "ui128" {
2020
+ result.run = Link;
2121
+ result.check = Pass(Link);
22+
+ } else if test.test_name == "ui128" {
23+
+ result.run == Check;
24+
+ result.check = Pass(Check);
2225
+ }
2326
+
2427
// END OF VENDOR RESERVED AREA
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[toolchain]
2-
channel = "nightly-2024-04-11"
2+
channel = "nightly-2024-04-23"
33
components = ["rust-src", "rustc-dev", "llvm-tools"]

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ mod returning;
77
use std::borrow::Cow;
88

99
use cranelift_codegen::ir::SigRef;
10+
use cranelift_codegen::isa::CallConv;
1011
use cranelift_module::ModuleError;
1112
use rustc_codegen_ssa::errors::CompilerBuiltinsCannotCall;
1213
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1314
use rustc_middle::ty::layout::FnAbiOf;
1415
use rustc_middle::ty::print::with_no_trimmed_paths;
16+
use rustc_middle::ty::TypeVisitableExt;
1517
use rustc_monomorphize::is_call_from_compiler_builtins_to_upstream_monomorphization;
1618
use rustc_session::Session;
1719
use rustc_span::source_map::Spanned;

compiler/rustc_codegen_cranelift/src/base.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
33
use cranelift_codegen::ir::UserFuncName;
44
use cranelift_codegen::CodegenError;
5+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
56
use cranelift_module::ModuleError;
67
use rustc_ast::InlineAsmOptions;
78
use rustc_index::IndexVec;
89
use rustc_middle::ty::adjustment::PointerCoercion;
910
use rustc_middle::ty::layout::FnAbiOf;
1011
use rustc_middle::ty::print::with_no_trimmed_paths;
12+
use rustc_middle::ty::TypeVisitableExt;
1113
use rustc_monomorphize::is_call_from_compiler_builtins_to_upstream_monomorphization;
1214

1315
use crate::constant::ConstantCx;
@@ -823,7 +825,13 @@ fn codegen_stmt<'tcx>(
823825
};
824826
let data = codegen_operand(fx, data);
825827
let meta = codegen_operand(fx, meta);
826-
let ptr_val = CValue::pointer_from_data_and_meta(data, meta, layout);
828+
assert!(data.layout().ty.is_unsafe_ptr());
829+
assert!(layout.ty.is_unsafe_ptr());
830+
let ptr_val = if meta.layout().is_zst() {
831+
data.cast_pointer_to(layout)
832+
} else {
833+
CValue::by_val_pair(data.load_scalar(fx), meta.load_scalar(fx), layout)
834+
};
827835
lval.write_cvalue(fx, ptr_val);
828836
}
829837
Rvalue::Aggregate(ref kind, ref operands) => {

compiler/rustc_codegen_cranelift/src/common.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use cranelift_codegen::isa::TargetFrontendConfig;
2+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
23
use rustc_index::IndexVec;
34
use rustc_middle::ty::layout::{
4-
FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
5+
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
56
};
7+
use rustc_middle::ty::TypeFoldable;
68
use rustc_span::source_map::Spanned;
79
use rustc_target::abi::call::FnAbi;
810
use rustc_target::abi::{Integer, Primitive};

compiler/rustc_codegen_cranelift/src/constant.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,23 @@ pub(crate) fn codegen_const_value<'tcx>(
137137
let alloc_id = prov.alloc_id();
138138
let base_addr = match fx.tcx.global_alloc(alloc_id) {
139139
GlobalAlloc::Memory(alloc) => {
140-
let data_id = data_id_for_alloc_id(
141-
&mut fx.constants_cx,
142-
fx.module,
143-
alloc_id,
144-
alloc.inner().mutability,
145-
);
146-
let local_data_id =
147-
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
148-
if fx.clif_comments.enabled() {
149-
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
140+
if alloc.inner().len() == 0 {
141+
assert_eq!(offset, Size::ZERO);
142+
fx.bcx.ins().iconst(fx.pointer_type, alloc.inner().align.bytes() as i64)
143+
} else {
144+
let data_id = data_id_for_alloc_id(
145+
&mut fx.constants_cx,
146+
fx.module,
147+
alloc_id,
148+
alloc.inner().mutability,
149+
);
150+
let local_data_id =
151+
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
152+
if fx.clif_comments.enabled() {
153+
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
154+
}
155+
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
150156
}
151-
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
152157
}
153158
GlobalAlloc::Function(instance) => {
154159
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_codegen_ssa::debuginfo::type_names;
1919
use rustc_hir::def::DefKind;
2020
use rustc_hir::def_id::DefIdMap;
2121
use rustc_session::Session;
22-
use rustc_span::{SourceFileHash, StableSourceFileId};
22+
use rustc_span::{FileNameDisplayPreference, SourceFileHash, StableSourceFileId};
2323
use rustc_target::abi::call::FnAbi;
2424

2525
pub(crate) use self::emit::{DebugReloc, DebugRelocName};

compiler/rustc_codegen_cranelift/src/debuginfo/object.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use cranelift_module::{DataId, FuncId};
22
use cranelift_object::ObjectProduct;
33
use gimli::SectionId;
44
use object::write::{Relocation, StandardSegment};
5-
use object::{RelocationEncoding, SectionKind};
5+
use object::{RelocationEncoding, RelocationFlags, SectionKind};
66
use rustc_data_structures::fx::FxHashMap;
77

88
use crate::debuginfo::{DebugReloc, DebugRelocName};
@@ -72,9 +72,11 @@ impl WriteDebugInfo for ObjectProduct {
7272
Relocation {
7373
offset: u64::from(reloc.offset),
7474
symbol,
75-
kind: reloc.kind,
76-
encoding: RelocationEncoding::Generic,
77-
size: reloc.size * 8,
75+
flags: RelocationFlags::Generic {
76+
kind: reloc.kind,
77+
encoding: RelocationEncoding::Generic,
78+
size: reloc.size * 8,
79+
},
7880
addend: i64::try_from(symbol_offset).unwrap() + reloc.addend,
7981
},
8082
)

compiler/rustc_codegen_cranelift/src/driver/jit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::ffi::CString;
66
use std::os::raw::{c_char, c_int};
77
use std::sync::{mpsc, Mutex, OnceLock};
88

9+
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
910
use cranelift_jit::{JITBuilder, JITModule};
1011
use rustc_codegen_ssa::CrateInfo;
1112
use rustc_middle::mir::mono::MonoItem;

compiler/rustc_codegen_cranelift/src/inline_asm.rs

+24-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::fmt::Write;
44

5+
use cranelift_codegen::isa::CallConv;
56
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
67
use rustc_span::sym;
78
use rustc_target::asm::*;
@@ -785,9 +786,9 @@ fn call_inline_asm<'tcx>(
785786
for (offset, place) in outputs {
786787
let ty = if place.layout().ty.is_simd() {
787788
let (lane_count, lane_type) = place.layout().ty.simd_size_and_type(fx.tcx);
788-
fx.clif_type(lane_type).unwrap().by(lane_count.try_into().unwrap()).unwrap()
789+
asm_clif_type(fx, lane_type).unwrap().by(lane_count.try_into().unwrap()).unwrap()
789790
} else {
790-
fx.clif_type(place.layout().ty).unwrap()
791+
asm_clif_type(fx, place.layout().ty).unwrap()
791792
};
792793
let value = stack_slot.offset(fx, i32::try_from(offset.bytes()).unwrap().into()).load(
793794
fx,
@@ -797,3 +798,24 @@ fn call_inline_asm<'tcx>(
797798
place.write_cvalue(fx, CValue::by_val(value, place.layout()));
798799
}
799800
}
801+
802+
fn asm_clif_type<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
803+
match ty.kind() {
804+
// Adapted from https://github.com/rust-lang/rust/blob/f3c66088610c1b80110297c2d9a8b5f9265b013f/compiler/rustc_hir_analysis/src/check/intrinsicck.rs#L136-L151
805+
ty::Adt(adt, args) if Some(adt.did()) == fx.tcx.lang_items().maybe_uninit() => {
806+
let fields = &adt.non_enum_variant().fields;
807+
let ty = fields[FieldIdx::from_u32(1)].ty(fx.tcx, args);
808+
let ty::Adt(ty, args) = ty.kind() else {
809+
unreachable!("expected first field of `MaybeUninit` to be an ADT")
810+
};
811+
assert!(
812+
ty.is_manually_drop(),
813+
"expected first field of `MaybeUninit` to be `ManuallyDrop`"
814+
);
815+
let fields = &ty.non_enum_variant().fields;
816+
let ty = fields[FieldIdx::ZERO].ty(fx.tcx, args);
817+
fx.clif_type(ty)
818+
}
819+
_ => fx.clif_type(ty),
820+
}
821+
}

compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc_span::source_map::Spanned;
2626
use rustc_span::symbol::{sym, Symbol};
2727

2828
pub(crate) use self::llvm::codegen_llvm_intrinsic_call;
29+
use crate::cast::clif_intcast;
2930
use crate::prelude::*;
3031

3132
fn bug_on_incorrect_arg_count(intrinsic: impl std::fmt::Display) -> ! {
@@ -627,7 +628,8 @@ fn codegen_regular_intrinsic_call<'tcx>(
627628

628629
// FIXME trap on `ctlz_nonzero` with zero arg.
629630
let res = fx.bcx.ins().clz(val);
630-
let res = CValue::by_val(res, arg.layout());
631+
let res = clif_intcast(fx, res, types::I32, false);
632+
let res = CValue::by_val(res, ret.layout());
631633
ret.write_cvalue(fx, res);
632634
}
633635
sym::cttz | sym::cttz_nonzero => {
@@ -636,15 +638,17 @@ fn codegen_regular_intrinsic_call<'tcx>(
636638

637639
// FIXME trap on `cttz_nonzero` with zero arg.
638640
let res = fx.bcx.ins().ctz(val);
639-
let res = CValue::by_val(res, arg.layout());
641+
let res = clif_intcast(fx, res, types::I32, false);
642+
let res = CValue::by_val(res, ret.layout());
640643
ret.write_cvalue(fx, res);
641644
}
642645
sym::ctpop => {
643646
intrinsic_args!(fx, args => (arg); intrinsic);
644647
let val = arg.load_scalar(fx);
645648

646649
let res = fx.bcx.ins().popcnt(val);
647-
let res = CValue::by_val(res, arg.layout());
650+
let res = clif_intcast(fx, res, types::I32, false);
651+
let res = CValue::by_val(res, ret.layout());
648652
ret.write_cvalue(fx, res);
649653
}
650654
sym::bitreverse => {

0 commit comments

Comments
 (0)