Skip to content

Commit 56cb727

Browse files
committed
Sync from rust 18890f05f6cea40fd25bb4bb9aa6f7372b69f641
2 parents 598f090 + 02d5f7f commit 56cb727

File tree

6 files changed

+260
-5
lines changed

6 files changed

+260
-5
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ crate-type = ["dylib"]
1818
cranelift-codegen = { version = "0.92", features = ["unwind", "all-arch"] }
1919
cranelift-frontend = { version = "0.92" }
2020
cranelift-module = { version = "0.92" }
21-
cranelift-native = { version = "0.92" }
21+
# NOTE vendored as src/cranelift_native.rs
22+
# FIXME revert back to the external crate with Cranelift 0.93
23+
#cranelift-native = { version = "0.92" }
2224
cranelift-jit = { version = "0.92", optional = true }
2325
cranelift-object = { version = "0.92" }
2426
target-lexicon = "0.12.0"

src/common.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ pub(crate) fn scalar_to_clif_type(tcx: TyCtxt<'_>, scalar: Scalar) -> Type {
3535
},
3636
Primitive::F32 => types::F32,
3737
Primitive::F64 => types::F64,
38-
Primitive::Pointer => pointer_ty(tcx),
38+
// FIXME(erikdesjardins): handle non-default addrspace ptr sizes
39+
Primitive::Pointer(_) => pointer_ty(tcx),
3940
}
4041
}
4142

src/cranelift_native.rs

+248
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
// Vendored from https://github.com/bytecodealliance/wasmtime/blob/b58a197d33f044193c3d608010f5e6ec394ac07e/cranelift/native/src/lib.rs
2+
// which is licensed as
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
// unlike rustc_codegen_cranelift itself. Also applies a small change to remove #![cfg_attr] that
5+
// rust's CI complains about and to fix formatting to match rustc.
6+
// FIXME revert back to the external crate with Cranelift 0.93
7+
#![allow(warnings)]
8+
9+
//! Performs autodetection of the host for the purposes of running
10+
//! Cranelift to generate code to run on the same machine.
11+
12+
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates, unstable_features)]
13+
#![warn(unused_import_braces)]
14+
15+
use cranelift_codegen::isa;
16+
use target_lexicon::Triple;
17+
18+
/// Return an `isa` builder configured for the current host
19+
/// machine, or `Err(())` if the host machine is not supported
20+
/// in the current configuration.
21+
pub fn builder() -> Result<isa::Builder, &'static str> {
22+
builder_with_options(true)
23+
}
24+
25+
/// Return an `isa` builder configured for the current host
26+
/// machine, or `Err(())` if the host machine is not supported
27+
/// in the current configuration.
28+
///
29+
/// Selects the given backend variant specifically; this is
30+
/// useful when more than oen backend exists for a given target
31+
/// (e.g., on x86-64).
32+
pub fn builder_with_options(infer_native_flags: bool) -> Result<isa::Builder, &'static str> {
33+
let mut isa_builder = isa::lookup(Triple::host()).map_err(|err| match err {
34+
isa::LookupError::SupportDisabled => "support for architecture disabled at compile time",
35+
isa::LookupError::Unsupported => "unsupported architecture",
36+
})?;
37+
38+
#[cfg(target_arch = "x86_64")]
39+
{
40+
use cranelift_codegen::settings::Configurable;
41+
42+
if !std::is_x86_feature_detected!("sse2") {
43+
return Err("x86 support requires SSE2");
44+
}
45+
46+
if !infer_native_flags {
47+
return Ok(isa_builder);
48+
}
49+
50+
// These are temporarily enabled by default (see #3810 for
51+
// more) so that a default-constructed `Flags` can work with
52+
// default Wasmtime features. Otherwise, the user must
53+
// explicitly use native flags or turn these on when on x86-64
54+
// platforms to avoid a configuration panic. In order for the
55+
// "enable if detected" logic below to work, we must turn them
56+
// *off* (differing from the default) and then re-enable below
57+
// if present.
58+
isa_builder.set("has_sse3", "false").unwrap();
59+
isa_builder.set("has_ssse3", "false").unwrap();
60+
isa_builder.set("has_sse41", "false").unwrap();
61+
isa_builder.set("has_sse42", "false").unwrap();
62+
63+
if std::is_x86_feature_detected!("sse3") {
64+
isa_builder.enable("has_sse3").unwrap();
65+
}
66+
if std::is_x86_feature_detected!("ssse3") {
67+
isa_builder.enable("has_ssse3").unwrap();
68+
}
69+
if std::is_x86_feature_detected!("sse4.1") {
70+
isa_builder.enable("has_sse41").unwrap();
71+
}
72+
if std::is_x86_feature_detected!("sse4.2") {
73+
isa_builder.enable("has_sse42").unwrap();
74+
}
75+
if std::is_x86_feature_detected!("popcnt") {
76+
isa_builder.enable("has_popcnt").unwrap();
77+
}
78+
if std::is_x86_feature_detected!("avx") {
79+
isa_builder.enable("has_avx").unwrap();
80+
}
81+
if std::is_x86_feature_detected!("avx2") {
82+
isa_builder.enable("has_avx2").unwrap();
83+
}
84+
if std::is_x86_feature_detected!("fma") {
85+
isa_builder.enable("has_fma").unwrap();
86+
}
87+
if std::is_x86_feature_detected!("bmi1") {
88+
isa_builder.enable("has_bmi1").unwrap();
89+
}
90+
if std::is_x86_feature_detected!("bmi2") {
91+
isa_builder.enable("has_bmi2").unwrap();
92+
}
93+
if std::is_x86_feature_detected!("avx512bitalg") {
94+
isa_builder.enable("has_avx512bitalg").unwrap();
95+
}
96+
if std::is_x86_feature_detected!("avx512dq") {
97+
isa_builder.enable("has_avx512dq").unwrap();
98+
}
99+
if std::is_x86_feature_detected!("avx512f") {
100+
isa_builder.enable("has_avx512f").unwrap();
101+
}
102+
if std::is_x86_feature_detected!("avx512vl") {
103+
isa_builder.enable("has_avx512vl").unwrap();
104+
}
105+
if std::is_x86_feature_detected!("avx512vbmi") {
106+
isa_builder.enable("has_avx512vbmi").unwrap();
107+
}
108+
if std::is_x86_feature_detected!("lzcnt") {
109+
isa_builder.enable("has_lzcnt").unwrap();
110+
}
111+
}
112+
113+
#[cfg(target_arch = "aarch64")]
114+
{
115+
use cranelift_codegen::settings::Configurable;
116+
117+
if !infer_native_flags {
118+
return Ok(isa_builder);
119+
}
120+
121+
if std::arch::is_aarch64_feature_detected!("lse") {
122+
isa_builder.enable("has_lse").unwrap();
123+
}
124+
125+
if std::arch::is_aarch64_feature_detected!("paca") {
126+
isa_builder.enable("has_pauth").unwrap();
127+
}
128+
129+
if cfg!(target_os = "macos") {
130+
// Pointer authentication is always available on Apple Silicon.
131+
isa_builder.enable("sign_return_address").unwrap();
132+
// macOS enforces the use of the B key for return addresses.
133+
isa_builder.enable("sign_return_address_with_bkey").unwrap();
134+
}
135+
}
136+
137+
// There is no is_s390x_feature_detected macro yet, so for now
138+
// we use getauxval from the libc crate directly.
139+
#[cfg(all(target_arch = "s390x", target_os = "linux"))]
140+
{
141+
use cranelift_codegen::settings::Configurable;
142+
143+
if !infer_native_flags {
144+
return Ok(isa_builder);
145+
}
146+
147+
let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
148+
const HWCAP_S390X_VXRS_EXT2: libc::c_ulong = 32768;
149+
if (v & HWCAP_S390X_VXRS_EXT2) != 0 {
150+
isa_builder.enable("has_vxrs_ext2").unwrap();
151+
// There is no separate HWCAP bit for mie2, so assume
152+
// that any machine with vxrs_ext2 also has mie2.
153+
isa_builder.enable("has_mie2").unwrap();
154+
}
155+
}
156+
157+
// `is_riscv_feature_detected` is nightly only for now, use
158+
// getauxval from the libc crate directly as a temporary measure.
159+
#[cfg(all(target_arch = "riscv64", target_os = "linux"))]
160+
{
161+
use cranelift_codegen::settings::Configurable;
162+
163+
if !infer_native_flags {
164+
return Ok(isa_builder);
165+
}
166+
167+
let v = unsafe { libc::getauxval(libc::AT_HWCAP) };
168+
169+
const HWCAP_RISCV_EXT_A: libc::c_ulong = 1 << (b'a' - b'a');
170+
const HWCAP_RISCV_EXT_C: libc::c_ulong = 1 << (b'c' - b'a');
171+
const HWCAP_RISCV_EXT_D: libc::c_ulong = 1 << (b'd' - b'a');
172+
const HWCAP_RISCV_EXT_F: libc::c_ulong = 1 << (b'f' - b'a');
173+
const HWCAP_RISCV_EXT_M: libc::c_ulong = 1 << (b'm' - b'a');
174+
const HWCAP_RISCV_EXT_V: libc::c_ulong = 1 << (b'v' - b'a');
175+
176+
if (v & HWCAP_RISCV_EXT_A) != 0 {
177+
isa_builder.enable("has_a").unwrap();
178+
}
179+
180+
if (v & HWCAP_RISCV_EXT_C) != 0 {
181+
isa_builder.enable("has_c").unwrap();
182+
}
183+
184+
if (v & HWCAP_RISCV_EXT_D) != 0 {
185+
isa_builder.enable("has_d").unwrap();
186+
}
187+
188+
if (v & HWCAP_RISCV_EXT_F) != 0 {
189+
isa_builder.enable("has_f").unwrap();
190+
191+
// TODO: There doesn't seem to be a bit associated with this extension
192+
// rust enables it with the `f` extension:
193+
// https://github.com/rust-lang/stdarch/blob/790411f93c4b5eada3c23abb4c9a063fb0b24d99/crates/std_detect/src/detect/os/linux/riscv.rs#L43
194+
isa_builder.enable("has_zicsr").unwrap();
195+
}
196+
197+
if (v & HWCAP_RISCV_EXT_M) != 0 {
198+
isa_builder.enable("has_m").unwrap();
199+
}
200+
201+
if (v & HWCAP_RISCV_EXT_V) != 0 {
202+
isa_builder.enable("has_v").unwrap();
203+
}
204+
205+
// TODO: ZiFencei does not have a bit associated with it
206+
// TODO: Zbkb does not have a bit associated with it
207+
}
208+
209+
// squelch warnings about unused mut/variables on some platforms.
210+
drop(&mut isa_builder);
211+
drop(infer_native_flags);
212+
213+
Ok(isa_builder)
214+
}
215+
216+
#[cfg(test)]
217+
mod tests {
218+
use super::builder;
219+
use cranelift_codegen::isa::CallConv;
220+
use cranelift_codegen::settings;
221+
222+
#[test]
223+
fn test() {
224+
if let Ok(isa_builder) = builder() {
225+
let flag_builder = settings::builder();
226+
let isa = isa_builder.finish(settings::Flags::new(flag_builder)).unwrap();
227+
228+
if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
229+
assert_eq!(isa.default_call_conv(), CallConv::AppleAarch64);
230+
} else if cfg!(any(unix, target_os = "nebulet")) {
231+
assert_eq!(isa.default_call_conv(), CallConv::SystemV);
232+
} else if cfg!(windows) {
233+
assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall);
234+
}
235+
236+
if cfg!(target_pointer_width = "64") {
237+
assert_eq!(isa.pointer_bits(), 64);
238+
} else if cfg!(target_pointer_width = "32") {
239+
assert_eq!(isa.pointer_bits(), 32);
240+
} else if cfg!(target_pointer_width = "16") {
241+
assert_eq!(isa.pointer_bits(), 16);
242+
}
243+
}
244+
}
245+
}
246+
247+
/// Version number of this crate.
248+
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

src/intrinsics/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod simd;
2121
pub(crate) use cpuid::codegen_cpuid_call;
2222
pub(crate) use llvm::codegen_llvm_intrinsic_call;
2323

24+
use rustc_middle::ty::layout::HasParamEnv;
2425
use rustc_middle::ty::print::with_no_trimmed_paths;
2526
use rustc_middle::ty::subst::SubstsRef;
2627
use rustc_span::symbol::{kw, sym, Symbol};
@@ -658,7 +659,9 @@ fn codegen_regular_intrinsic_call<'tcx>(
658659
return;
659660
}
660661

661-
if intrinsic == sym::assert_zero_valid && !fx.tcx.permits_zero_init(layout) {
662+
if intrinsic == sym::assert_zero_valid
663+
&& !fx.tcx.permits_zero_init(fx.param_env().and(layout))
664+
{
662665
with_no_trimmed_paths!({
663666
crate::base::codegen_panic_nounwind(
664667
fx,
@@ -673,7 +676,7 @@ fn codegen_regular_intrinsic_call<'tcx>(
673676
}
674677

675678
if intrinsic == sym::assert_mem_uninitialized_valid
676-
&& !fx.tcx.permits_uninit_init(layout)
679+
&& !fx.tcx.permits_uninit_init(fx.param_env().and(layout))
677680
{
678681
with_no_trimmed_paths!({
679682
crate::base::codegen_panic_nounwind(

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ mod compiler_builtins;
5757
mod concurrency_limiter;
5858
mod config;
5959
mod constant;
60+
// FIXME revert back to the external crate with Cranelift 0.93
61+
mod cranelift_native;
6062
mod debuginfo;
6163
mod discriminant;
6264
mod driver;

0 commit comments

Comments
 (0)