Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
tcx: TyCtxt<'_>,
target_cpu: String,
) -> OngoingCodegen<B> {
if tcx.sess.target.need_explicit_cpu && tcx.sess.opts.cg.target_cpu.is_none() {
if tcx.sess.target.requires_explicit_and_consistent_cpu && tcx.sess.opts.cg.target_cpu.is_none()
{
// The target has no default cpu, but none is set explicitly
tcx.dcx().emit_fatal(errors::CpuRequired);
}
Expand Down
20 changes: 16 additions & 4 deletions compiler/rustc_metadata/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,29 @@ metadata_incompatible_target_modifiers =
metadata_incompatible_target_modifiers_help_allow = if you are sure this will not cause problems, you may use `-Cunsafe-allow-abi-mismatch={$flag_name}` to silence this error
metadata_incompatible_target_modifiers_help_fix = set `{$flag_name_prefixed}={$extern_value}` in this crate or `{$flag_name_prefixed}={$local_value}` in `{$extern_crate}`

metadata_incompatible_target_modifiers_help_fix_l_missed = set `{$flag_name_prefixed}={$extern_value}` in this crate or unset `{$flag_name_prefixed}` in `{$extern_crate}`
metadata_incompatible_target_modifiers_help_fix_l_missed = set {$value_len ->
[0] `{$flag_name_prefixed}`
*[other] `{$flag_name_prefixed}={$extern_value}`
} in this crate or unset `{$flag_name_prefixed}` in `{$extern_crate}`

metadata_incompatible_target_modifiers_help_fix_r_missed = unset `{$flag_name_prefixed}` in this crate or set `{$flag_name_prefixed}={$local_value}` in `{$extern_crate}`
metadata_incompatible_target_modifiers_help_fix_r_missed = unset `{$flag_name_prefixed}` in this crate or set {$value_len ->
[0] `{$flag_name_prefixed}`
*[other] `{$flag_name_prefixed}={$local_value}`
} in `{$extern_crate}`

metadata_incompatible_target_modifiers_l_missed =
mixing `{$flag_name_prefixed}` will cause an ABI mismatch in crate `{$local_crate}`
.note = unset `{$flag_name_prefixed}` in this crate is incompatible with `{$flag_name_prefixed}={$extern_value}` in dependency `{$extern_crate}`
.note = `{$flag_name_prefixed}` is unset in this crate which is incompatible with {$value_len ->
[0] `{$flag_name_prefixed}` being set
*[other] `{$flag_name_prefixed}={$extern_value}`
} in dependency `{$extern_crate}`
.help = the `{$flag_name_prefixed}` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely
metadata_incompatible_target_modifiers_r_missed =
mixing `{$flag_name_prefixed}` will cause an ABI mismatch in crate `{$local_crate}`
.note = `{$flag_name_prefixed}={$local_value}` in this crate is incompatible with unset `{$flag_name_prefixed}` in dependency `{$extern_crate}`
.note = {$value_len ->
[0] `{$flag_name_prefixed}` being set
*[other] `{$flag_name_prefixed}={$local_value}`
} in this crate is incompatible with `{$flag_name_prefixed}` being unset in dependency `{$extern_crate}`
.help = the `{$flag_name_prefixed}` flag modifies the ABI so Rust crates compiled with different values of this flag cannot be used together safely

metadata_incompatible_with_immediate_abort =
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ impl CStore {
flag_name,
flag_name_prefixed,
extern_value: extern_value.to_string(),
value_len: extern_value.len(),
})
}
(Some(local_value), None) => {
Expand All @@ -401,6 +402,7 @@ impl CStore {
flag_name,
flag_name_prefixed,
local_value: local_value.to_string(),
value_len: local_value.len(),
})
}
(None, None) => panic!("Incorrect target modifiers report_diff(None, None)"),
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ pub struct IncompatibleTargetModifiersLMissed {
pub flag_name: String,
pub flag_name_prefixed: String,
pub extern_value: String,
pub value_len: usize,
}

#[derive(Diagnostic)]
Expand All @@ -592,6 +593,7 @@ pub struct IncompatibleTargetModifiersRMissed {
pub flag_name: String,
pub flag_name_prefixed: String,
pub local_value: String,
pub value_len: usize,
}

#[derive(Diagnostic)]
Expand Down
22 changes: 20 additions & 2 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,20 @@ mod target_modifier_consistency_check {
}
true
}
pub(super) fn target_cpu(
sess: &Session,
l: &TargetModifier,
r: Option<&TargetModifier>,
) -> bool {
if sess.target.requires_explicit_and_consistent_cpu {
if let Some(r) = r {
return l.extend().tech_value == r.extend().tech_value;
} else {
return false;
}
}
true
}
}

impl TargetModifier {
Expand All @@ -147,7 +161,11 @@ impl TargetModifier {
}
_ => {}
},
_ => {}
OptionsTargetModifiers::CodegenOptions(codegen) => match codegen {
CodegenOptionsTargetModifiers::target_cpu => {
return target_modifier_consistency_check::target_cpu(sess, self, other);
}
},
};
match other {
Some(other) => self.extend().tech_value == other.extend().tech_value,
Expand Down Expand Up @@ -2216,7 +2234,7 @@ options! {
symbol_mangling_version: Option<SymbolManglingVersion> = (None,
parse_symbol_mangling_version, [TRACKED],
"which mangling version to use for symbol names ('legacy' (default), 'v0', or 'hashed')"),
target_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
target_cpu: Option<String> = (None, parse_opt_string, [TRACKED TARGET_MODIFIER],
"select target processor (`rustc --print target-cpus` for details)"),
target_feature: String = (String::new(), parse_target_feature, [TRACKED],
"target specific attributes. (`rustc --print target-features` for details). \
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/spec/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Target {
forward!(link_env_remove);
forward!(asm_args);
forward!(cpu);
forward!(need_explicit_cpu);
forward!(requires_explicit_and_consistent_cpu);
forward!(features);
forward!(dynamic_linking);
forward_opt!(direct_access_external_data);
Expand Down Expand Up @@ -321,7 +321,7 @@ impl ToJson for Target {
target_option_val!(link_env_remove);
target_option_val!(asm_args);
target_option_val!(cpu);
target_option_val!(need_explicit_cpu);
target_option_val!(requires_explicit_and_consistent_cpu);
target_option_val!(features);
target_option_val!(dynamic_linking);
target_option_val!(direct_access_external_data);
Expand Down Expand Up @@ -544,7 +544,7 @@ struct TargetSpecJson {
link_env_remove: Option<StaticCow<[StaticCow<str>]>>,
asm_args: Option<StaticCow<[StaticCow<str>]>>,
cpu: Option<StaticCow<str>>,
need_explicit_cpu: Option<bool>,
requires_explicit_and_consistent_cpu: Option<bool>,
features: Option<StaticCow<str>>,
dynamic_linking: Option<bool>,
direct_access_external_data: Option<bool>,
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2262,9 +2262,10 @@ pub struct TargetOptions {
/// Default CPU to pass to LLVM. Corresponds to `llc -mcpu=$cpu`. Defaults
/// to "generic".
pub cpu: StaticCow<str>,
/// Whether a cpu needs to be explicitly set.
/// Set to true if there is no default cpu. Defaults to false.
pub need_explicit_cpu: bool,
/// Signals that the cpu needs to be explicitly set and be consistent when
/// multiple crates are linked together. Internally modifies `-Ctarget-cpu`
/// to act as a target modifier. Defaults to false.
pub requires_explicit_and_consistent_cpu: bool,
/// Default (Rust) target features to enable for this target. These features
/// overwrite `-Ctarget-cpu` but can be overwritten with `-Ctarget-features`.
/// Corresponds to `llc -mattr=$llvm_features` where `$llvm_features` is the
Expand Down Expand Up @@ -2722,7 +2723,7 @@ impl Default for TargetOptions {
link_script: None,
asm_args: cvs![],
cpu: "generic".into(),
need_explicit_cpu: false,
requires_explicit_and_consistent_cpu: false,
features: "".into(),
direct_access_external_data: None,
dynamic_linking: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn target() -> Target {

// There are many CPUs, one for each hardware generation.
// Require to set one explicitly as there is no good default.
need_explicit_cpu: true,
requires_explicit_and_consistent_cpu: true,

max_atomic_width: Some(64),

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_target/src/spec/targets/avr_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(crate) fn target() -> Target {
max_atomic_width: Some(16),
atomic_cas: false,
relocation_model: RelocModel::Static,
need_explicit_cpu: true,
requires_explicit_and_consistent_cpu: true,
..TargetOptions::default()
},
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_target/src/spec/targets/nvptx64_nvidia_cuda.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub(crate) fn target() -> Target {

// With `ptx-linker` approach, it can be later overridden via link flags.
cpu: "sm_30".into(),
// The target cpu for NVPTX must be set manually and be consistent
// accross crates linked together.
requires_explicit_and_consistent_cpu: true,

// FIXME: create tests for the atomics.
max_atomic_width: Some(64),
Expand Down
1 change: 0 additions & 1 deletion src/ci/docker/host-x86_64/dist-various-2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ ENV TARGETS=$TARGETS,wasm32-wasip2
ENV TARGETS=$TARGETS,wasm32v1-none
ENV TARGETS=$TARGETS,x86_64-unknown-linux-gnux32
ENV TARGETS=$TARGETS,x86_64-fortanix-unknown-sgx
ENV TARGETS=$TARGETS,nvptx64-nvidia-cuda
ENV TARGETS=$TARGETS,armv7-unknown-linux-gnueabi
ENV TARGETS=$TARGETS,armv7-unknown-linux-musleabi
ENV TARGETS=$TARGETS,i686-unknown-freebsd
Expand Down
6 changes: 0 additions & 6 deletions src/ci/docker/host-x86_64/test-various/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ ENV WASM_WASIP_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $
tests/assembly-llvm \
library/core

ENV NVPTX_TARGETS=nvptx64-nvidia-cuda
ENV NVPTX_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $NVPTX_TARGETS \
tests/run-make \
tests/run-make-cargo \
tests/assembly-llvm

ENV MUSL_TARGETS=x86_64-unknown-linux-musl \
CC_x86_64_unknown_linux_musl=x86_64-linux-musl-gcc \
CXX_x86_64_unknown_linux_musl=x86_64-linux-musl-g++
Expand Down
3 changes: 2 additions & 1 deletion src/doc/rustc/src/platform-support/nvptx64-nvidia-cuda.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ It is generally necessary to specify the target, such as `-C target-cpu=sm_89`,
One can use `-C target-feature=+ptx80` to choose a later PTX version without changing the target (the default in this case, `ptx78`, requires CUDA driver version 11.8, while `ptx80` would require driver version 12.0).
Later PTX versions may allow more efficient code generation.

Although Rust follows LLVM in representing `ptx*` and `sm_*` as target features, they should be thought of as having crate granularity, set via (either via `-Ctarget-cpu` and optionally `-Ctarget-feature`).
For this target, the compiler enforces that all crates built into a binary use the same value for `-C target-cpu`. Therefore, it is necessary to manually build `core` every time a crate is compiled. This can be done by adding the flag `-Z build-std=core` when invoking cargo.
Although Rust follows LLVM in representing `ptx*` and `sm_*` as target features, they should also be thought of as having binary granularity. However, this is not enforced by the compiler. When building crates together, the same value for all crates should be set via `-C target-feature`.
While the compiler accepts `#[target_feature(enable = "ptx80", enable = "sm_89")]`, it is not supported, may not behave as intended, and may become erroneous in the future.

## Building Rust kernels
Expand Down
2 changes: 1 addition & 1 deletion tests/assembly-llvm/asm/nvptx-types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ add-minicore
//@ assembly-output: emit-asm
//@ compile-flags: --target nvptx64-nvidia-cuda
//@ compile-flags: --target nvptx64-nvidia-cuda -Ctarget-cpu=sm_30
//@ needs-llvm-components: nvptx

#![feature(no_core, asm_experimental_arch)]
Expand Down
24 changes: 19 additions & 5 deletions tests/assembly-llvm/auxiliary/non-inline-dependency.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
#![no_std]
#![deny(warnings)]
//@ add-minicore
//@ no-prefer-dynamic
//@ compile-flags: --target nvptx64-nvidia-cuda -Ctarget-cpu=sm_30
//@ needs-llvm-components: nvptx
#![feature(no_core, intrinsics)]
#![crate_type = "rlib"]
#![no_core]

extern crate minicore;
use minicore::*;

#[rustc_intrinsic]
pub const fn wrapping_mul<T: Copy>(a: T, b: T) -> T;

#[rustc_intrinsic]
pub const fn mul_with_overflow<T: Copy>(x: T, y: T) -> (T, bool);

#[inline(never)]
#[no_mangle]
pub fn wrapping_external_fn(a: u32) -> u32 {
a.wrapping_mul(a)
wrapping_mul(a, a)
}

#[inline(never)]
#[no_mangle]
pub fn panicking_external_fn(a: u32) -> u32 {
a * a
pub fn overflowing_external_fn(a: u32) -> u32 {
mul_with_overflow(a, a).0
}
11 changes: 4 additions & 7 deletions tests/assembly-llvm/nvptx-arch-default.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib
//@ only-nvptx64

#![no_std]

//@ aux-build: breakpoint-panic-handler.rs
extern crate breakpoint_panic_handler;
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type cdylib -Ctarget-cpu=sm_30
//@ needs-llvm-components: nvptx
#![feature(no_core)]
#![no_core]

// Verify default target arch with ptx-linker.
// CHECK: .target sm_30
Expand Down
8 changes: 4 additions & 4 deletions tests/assembly-llvm/nvptx-arch-emit-asm.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//@ assembly-output: emit-asm
//@ compile-flags: --crate-type rlib
//@ only-nvptx64

#![no_std]
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type rlib -Ctarget-cpu=sm_30
//@ needs-llvm-components: nvptx
#![feature(no_core)]
#![no_core]

// Verify default arch without ptx-linker involved.
// CHECK: .target sm_30
Expand Down
2 changes: 1 addition & 1 deletion tests/assembly-llvm/nvptx-arch-link-arg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib -C link-arg=--arch=sm_60
//@ compile-flags: --crate-type cdylib -Ctarget-cpu=sm_60 -C link-arg=--arch=sm_60
//@ only-nvptx64
//@ ignore-nvptx64

Expand Down
11 changes: 4 additions & 7 deletions tests/assembly-llvm/nvptx-arch-target-cpu.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib -C target-cpu=sm_50
//@ only-nvptx64

#![no_std]

//@ aux-build: breakpoint-panic-handler.rs
extern crate breakpoint_panic_handler;
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type cdylib -C target-cpu=sm_50
//@ needs-llvm-components: nvptx
#![feature(no_core)]
#![no_core]

// Verify target arch override via `target-cpu`.
// CHECK: .target sm_50
Expand Down
2 changes: 1 addition & 1 deletion tests/assembly-llvm/nvptx-atomics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib
//@ compile-flags: --crate-type cdylib -Ctarget-cpu=sm_30
//@ only-nvptx64
//@ ignore-nvptx64

Expand Down
4 changes: 2 additions & 2 deletions tests/assembly-llvm/nvptx-c-abi-arg-v7.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib -C target-cpu=sm_86
//@ only-nvptx64
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type cdylib -C target-cpu=sm_86
//@ needs-llvm-components: nvptx

// The PTX ABI stability is tied to major versions of the PTX ISA
// These tests assume major version 7
Expand Down
4 changes: 2 additions & 2 deletions tests/assembly-llvm/nvptx-c-abi-ret-v7.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib -C target-cpu=sm_86
//@ only-nvptx64
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type cdylib -C target-cpu=sm_86
//@ needs-llvm-components: nvptx

// The PTX ABI stability is tied to major versions of the PTX ISA
// These tests assume major version 7
Expand Down
20 changes: 10 additions & 10 deletions tests/assembly-llvm/nvptx-internalizing.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
//@ add-minicore
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib
//@ only-nvptx64
//@ ignore-nvptx64
//@ compile-flags: --target nvptx64-nvidia-cuda --crate-type cdylib -Ctarget-cpu=sm_30
//@ needs-llvm-components: nvptx

#![feature(abi_ptx)]
#![no_std]
#![feature(abi_ptx, no_core, intrinsics)]
#![no_core]

//@ aux-build: breakpoint-panic-handler.rs
extern crate breakpoint_panic_handler;
extern crate minicore;
use minicore::*;

//@ aux-build: non-inline-dependency.rs
extern crate non_inline_dependency as dep;
#[rustc_intrinsic]
pub const unsafe fn unchecked_add<T: Copy>(x: T, y: T) -> T;

// Verify that no extra function declarations are present.
// CHECK-NOT: .func
Expand All @@ -19,7 +19,7 @@ extern crate non_inline_dependency as dep;
#[no_mangle]
pub unsafe extern "ptx-kernel" fn top_kernel(a: *const u32, b: *mut u32) {
// CHECK: add.s32 %{{r[0-9]+}}, %{{r[0-9]+}}, 5;
*b = *a + 5;
*b = unchecked_add(*a, 5);
}

// Verify that no extra function definitions are here.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@ assembly-output: ptx-linker
//@ compile-flags: --crate-type cdylib -C target-cpu=sm_86
//@ only-nvptx64
//@ compile-flags: --target=nvptx64-nvidia-cuda --crate-type cdylib -C target-cpu=sm_86
//@ needs-llvm-components: nvptx

// The following ABI tests are made with nvcc 11.6 does.
//
Expand Down
Loading
Loading