Skip to content

Commit 694284d

Browse files
committed
Set target features depending on the target architecture (#4127)
Updates the `target_config` method to set the target features depending on the target architecture. Resolves #4119 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent a7028d2 commit 694284d

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

kani-compiler/src/codegen_cprover_gotoc/compiler_interface.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use rustc_session::Session;
4141
use rustc_session::config::{CrateType, OutputFilenames, OutputType};
4242
use rustc_session::output::out_filename;
4343
use rustc_smir::rustc_internal;
44-
use rustc_span::symbol::Symbol;
44+
use rustc_span::{Symbol, sym};
4545
use rustc_target::spec::PanicStrategy;
4646
use stable_mir::CrateDef;
4747
use stable_mir::mir::mono::{Instance, MonoItem};
@@ -252,21 +252,37 @@ impl CodegenBackend for GotocCodegenBackend {
252252
DEFAULT_LOCALE_RESOURCE
253253
}
254254

255-
fn target_config(&self, _sess: &Session) -> TargetConfig {
255+
fn target_config(&self, sess: &Session) -> TargetConfig {
256+
// This code is adapted from the cranelift backend:
257+
// https://github.com/rust-lang/rust/blob/a124fb3cb7291d75872934f411d81fe298379ace/compiler/rustc_codegen_cranelift/src/lib.rs#L184
258+
let target_features = if sess.target.arch == "x86_64" && sess.target.os != "none" {
259+
// x86_64 mandates SSE2 support and rustc requires the x87 feature to be enabled
260+
vec![sym::sse, sym::sse2, Symbol::intern("x87")]
261+
} else if sess.target.arch == "aarch64" {
262+
match &*sess.target.os {
263+
"none" => vec![],
264+
// On macOS the aes, sha2 and sha3 features are enabled by default and ring
265+
// fails to compile on macOS when they are not present.
266+
"macos" => vec![sym::neon, sym::aes, sym::sha2, sym::sha3],
267+
// AArch64 mandates Neon support
268+
_ => vec![sym::neon],
269+
}
270+
} else {
271+
vec![]
272+
};
273+
// FIXME do `unstable_target_features` properly
274+
let unstable_target_features = target_features.clone();
275+
276+
let has_reliable_f128 = true;
277+
let has_reliable_f16 = true;
278+
256279
TargetConfig {
257-
target_features: vec![],
258-
unstable_target_features: vec![
259-
Symbol::intern("sse"),
260-
Symbol::intern("neon"),
261-
Symbol::intern("x87"),
262-
Symbol::intern("sse2"),
263-
],
264-
// `true` is used as a default so backends need to acknowledge when they do not
265-
// support the float types, rather than accidentally quietly skipping all tests.
266-
has_reliable_f16: true,
267-
has_reliable_f16_math: true,
268-
has_reliable_f128: true,
269-
has_reliable_f128_math: true,
280+
target_features,
281+
unstable_target_features,
282+
has_reliable_f16,
283+
has_reliable_f16_math: has_reliable_f16,
284+
has_reliable_f128,
285+
has_reliable_f128_math: has_reliable_f128,
270286
}
271287
}
272288

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright Kani Contributors
2+
// SPDX-License-Identifier: Apache-2.0 OR MIT
3+
4+
/// This test checks that the Kani compiler propertly enables the
5+
/// architecture-specific target features (e.g. `neon` on `aarch64` and
6+
/// `sse`/`sse2` on `x86_64`)
7+
8+
#[kani::proof]
9+
fn check_expected_target_features() {
10+
#[cfg(target_arch = "aarch64")]
11+
{
12+
assert!(cfg!(target_feature = "neon"));
13+
}
14+
15+
#[cfg(target_arch = "x86_64")]
16+
{
17+
assert!(cfg!(target_feature = "sse"));
18+
assert!(cfg!(target_feature = "sse2"));
19+
assert!(cfg!(target_feature = "x87"));
20+
}
21+
}

0 commit comments

Comments
 (0)