Skip to content
Merged
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_cranelift/src/intrinsics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,11 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
};
ret.write_cvalue(fx, CValue::by_val(is_eq_value, ret.layout()));
};

black_box, (c a) {
// FIXME implement black_box semantics
ret.write_cvalue(fx, a);
};
}

if let Some((_, dest)) = destination {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_llvm/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ impl AsmMethods for CodegenCx<'ll, 'tcx> {
}
}

fn inline_asm_call(
pub(crate) fn inline_asm_call(
bx: &mut Builder<'a, 'll, 'tcx>,
asm: &str,
cons: &str,
Expand Down
26 changes: 26 additions & 0 deletions compiler/rustc_codegen_llvm/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::type_of::LayoutLlvmExt;
use crate::va_arg::emit_va_arg;
use crate::value::Value;

use rustc_ast as ast;
use rustc_codegen_ssa::base::{compare_simd_types, wants_msvc_seh};
use rustc_codegen_ssa::common::span_invalid_monomorphization_error;
use rustc_codegen_ssa::common::{IntPredicate, TypeKind};
Expand Down Expand Up @@ -327,6 +328,31 @@ impl IntrinsicCallMethods<'tcx> for Builder<'a, 'll, 'tcx> {
}
}

sym::black_box => {
args[0].val.store(self, result);

// We need to "use" the argument in some way LLVM can't introspect, and on
// targets that support it we can typically leverage inline assembly to do
// this. LLVM's interpretation of inline assembly is that it's, well, a black
// box. This isn't the greatest implementation since it probably deoptimizes
// more than we want, but it's so far good enough.
crate::asm::inline_asm_call(
self,
"",
"r,~{memory}",
&[result.llval],
self.type_void(),
true,
false,
ast::LlvmAsmDialect::Att,
&[span],
)
.unwrap_or_else(|| bug!("failed to generate inline asm call for `black_box`"));

// We have copied the value to `result` already.
return;
}

_ if name_str.starts_with("simd_") => {
match generic_simd_intrinsic(self, name, callee_ty, args, ret_ty, llret_ty, span) {
Ok(llval) => llval,
Expand Down
15 changes: 13 additions & 2 deletions compiler/rustc_lint/src/non_fmt_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{LateContext, LateLintPass, LintContext};
use rustc_ast as ast;
use rustc_errors::{pluralize, Applicability};
use rustc_hir as hir;
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty;
use rustc_parse_format::{ParseMode, Parser, Piece};
use rustc_session::lint::FutureIncompatibilityReason;
Expand Down Expand Up @@ -75,6 +76,11 @@ fn check_panic<'tcx>(cx: &LateContext<'tcx>, f: &'tcx hir::Expr<'tcx>, arg: &'tc

let (span, panic, symbol_str) = panic_call(cx, f);

if in_external_macro(cx.sess(), span) {
// Nothing that can be done about it in the current crate.
return;
}

// Find the span of the argument to `panic!()`, before expansion in the
// case of `panic!(some_macro!())`.
// We don't use source_callsite(), because this `panic!(..)` might itself
Expand Down Expand Up @@ -152,6 +158,13 @@ fn check_panic_str<'tcx>(
return;
}

let (span, _, _) = panic_call(cx, f);

if in_external_macro(cx.sess(), span) && in_external_macro(cx.sess(), arg.span) {
// Nothing that can be done about it in the current crate.
return;
}

let fmt_span = arg.span.source_callsite();

let (snippet, style) = match cx.sess().parse_sess.source_map().span_to_snippet(fmt_span) {
Expand All @@ -167,8 +180,6 @@ fn check_panic_str<'tcx>(
Parser::new(fmt.as_ref(), style, snippet.clone(), false, ParseMode::Format);
let n_arguments = (&mut fmt_parser).filter(|a| matches!(a, Piece::NextArgument(_))).count();

let (span, _, _) = panic_call(cx, f);

if n_arguments > 0 && fmt_parser.errors.is_empty() {
let arg_spans: Vec<_> = match &fmt_parser.arg_places[..] {
[] => vec![fmt_span],
Expand Down
24 changes: 8 additions & 16 deletions compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ impl IntegerExt for Integer {
let unsigned_fit = Integer::fit_unsigned(cmp::max(min as u128, max as u128));
let signed_fit = cmp::max(Integer::fit_signed(min), Integer::fit_signed(max));

let mut min_from_extern = None;
let min_default = I8;

if let Some(ity) = repr.int {
let discr = Integer::from_attr(&tcx, ity);
let fit = if ity.is_signed() { signed_fit } else { unsigned_fit };
Expand All @@ -128,19 +125,14 @@ impl IntegerExt for Integer {
return (discr, ity.is_signed());
}

if repr.c() {
match &tcx.sess.target.arch[..] {
"hexagon" => min_from_extern = Some(I8),
// WARNING: the ARM EABI has two variants; the one corresponding
// to `at_least == I32` appears to be used on Linux and NetBSD,
// but some systems may use the variant corresponding to no
// lower bound. However, we don't run on those yet...?
"arm" => min_from_extern = Some(I32),
_ => min_from_extern = Some(I32),
}
}

let at_least = min_from_extern.unwrap_or(min_default);
let at_least = if repr.c() {
// This is usually I32, however it can be different on some platforms,
// notably hexagon and arm-none/thumb-none
tcx.data_layout().c_enum_min_size
} else {
// repr(Rust) enums try to be as small as possible
I8
};

// If there are no negative values, we can use the unsigned fit.
if min >= 0 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir/src/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
);
self.copy_op(&self.operand_index(&args[0], index)?, dest)?;
}
sym::likely | sym::unlikely => {
sym::likely | sym::unlikely | sym::black_box => {
// These just return their argument
self.copy_op(&args[0], dest)?;
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ symbols! {
bitreverse,
bitxor,
bitxor_assign,
black_box,
block,
bool,
borrowck_graphviz_format,
Expand Down
17 changes: 17 additions & 0 deletions compiler/rustc_target/src/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct TargetDataLayout {
pub vector_align: Vec<(Size, AbiAndPrefAlign)>,

pub instruction_address_space: AddressSpace,

/// Minimum size of #[repr(C)] enums (default I32 bits)
pub c_enum_min_size: Integer,
}

impl Default for TargetDataLayout {
Expand All @@ -60,6 +63,7 @@ impl Default for TargetDataLayout {
(Size::from_bits(128), AbiAndPrefAlign::new(align(128))),
],
instruction_address_space: AddressSpace::DATA,
c_enum_min_size: Integer::I32,
}
}
}
Expand Down Expand Up @@ -173,6 +177,8 @@ impl TargetDataLayout {
));
}

dl.c_enum_min_size = Integer::from_size(Size::from_bits(target.c_enum_min_bits))?;

Ok(dl)
}

Expand Down Expand Up @@ -610,6 +616,17 @@ impl Integer {
}
I8
}

fn from_size(size: Size) -> Result<Self, String> {
match size.bits() {
8 => Ok(Integer::I8),
16 => Ok(Integer::I16),
32 => Ok(Integer::I32),
64 => Ok(Integer::I64),
128 => Ok(Integer::I128),
_ => Err(format!("rust does not support integers with {} bits", size.bits())),
}
}
}

/// Fundamental unit of memory access and layout.
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/armebv7r_none_eabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn target() -> Target {
panic_strategy: PanicStrategy::Abort,
max_atomic_width: Some(32),
emit_debug_gdb_scripts: false,
// GCC and Clang default to 8 for arm-none here
c_enum_min_bits: 8,
..Default::default()
},
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/armebv7r_none_eabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub fn target() -> Target {
features: "+vfp3,-d32,-fp16".to_string(),
max_atomic_width: Some(32),
emit_debug_gdb_scripts: false,
// GCC and Clang default to 8 for arm-none here
c_enum_min_bits: 8,
..Default::default()
},
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_target/src/spec/armv7a_none_eabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub fn target() -> Target {
max_atomic_width: Some(64),
panic_strategy: PanicStrategy::Abort,
emit_debug_gdb_scripts: false,
c_enum_min_bits: 8,
..Default::default()
};
Target {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/armv7a_none_eabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub fn target() -> Target {
max_atomic_width: Some(64),
panic_strategy: PanicStrategy::Abort,
emit_debug_gdb_scripts: false,
// GCC and Clang default to 8 for arm-none here
c_enum_min_bits: 8,
..Default::default()
};
Target {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/armv7r_none_eabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub fn target() -> Target {
panic_strategy: PanicStrategy::Abort,
max_atomic_width: Some(32),
emit_debug_gdb_scripts: false,
// GCC and Clang default to 8 for arm-none here
c_enum_min_bits: 8,
..Default::default()
},
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/armv7r_none_eabihf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub fn target() -> Target {
features: "+vfp3,-d32,-fp16".to_string(),
max_atomic_width: Some(32),
emit_debug_gdb_scripts: false,
// GCC and Clang default to 8 for arm-none here
c_enum_min_bits: 8,
..Default::default()
},
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_target/src/spec/hexagon_unknown_linux_musl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ pub fn target() -> Target {
base.dynamic_linking = true;
base.executables = true;

base.c_enum_min_bits = 8;

Target {
llvm_target: "hexagon-unknown-linux-musl".to_string(),
pointer_width: 32,
Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,9 @@ pub struct TargetOptions {

/// If present it's a default value to use for adjusting the C ABI.
pub default_adjusted_cabi: Option<Abi>,

/// Minimum number of bits in #[repr(C)] enum. Defaults to 32.
pub c_enum_min_bits: u64,
}

impl Default for TargetOptions {
Expand Down Expand Up @@ -1440,6 +1443,7 @@ impl Default for TargetOptions {
split_debuginfo: SplitDebuginfo::Off,
supported_sanitizers: SanitizerSet::empty(),
default_adjusted_cabi: None,
c_enum_min_bits: 32,
}
}
}
Expand Down Expand Up @@ -1604,6 +1608,12 @@ impl Target {
base.$key_name = s;
}
} );
($key_name:ident, u64) => ( {
let name = (stringify!($key_name)).replace("_", "-");
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
base.$key_name = s;
}
} );
($key_name:ident, Option<u32>) => ( {
let name = (stringify!($key_name)).replace("_", "-");
if let Some(s) = obj.remove_key(&name).and_then(|j| Json::as_u64(&j)) {
Expand Down Expand Up @@ -2017,6 +2027,7 @@ impl Target {
key!(split_debuginfo, SplitDebuginfo)?;
key!(supported_sanitizers, SanitizerSet)?;
key!(default_adjusted_cabi, Option<Abi>)?;
key!(c_enum_min_bits, u64);

if base.is_builtin {
// This can cause unfortunate ICEs later down the line.
Expand Down Expand Up @@ -2255,6 +2266,7 @@ impl ToJson for Target {
target_option_val!(has_thumb_interworking);
target_option_val!(split_debuginfo);
target_option_val!(supported_sanitizers);
target_option_val!(c_enum_min_bits);

if let Some(abi) = self.default_adjusted_cabi {
d.insert("default-adjusted-cabi".to_string(), Abi::name(abi).to_json());
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_target/src/spec/thumb_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub fn opts() -> TargetOptions {
// LLVM is eager to trash the link register when calling `noreturn` functions, which
// breaks debugging. Preserve LR by default to prevent that from happening.
frame_pointer: FramePointer::Always,
// ARM supports multiple ABIs for enums, the linux one matches the default of 32 here
// but any arm-none or thumb-none target will be defaulted to 8 on GCC and clang
c_enum_min_bits: 8,
..Default::default()
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_typeck/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ pub fn intrinsic_operation_unsafety(intrinsic: Symbol) -> hir::Unsafety {
| sym::maxnumf64
| sym::type_name
| sym::forget
| sym::black_box
| sym::variant_count => hir::Unsafety::Normal,
_ => hir::Unsafety::Unsafe,
}
Expand Down Expand Up @@ -387,6 +388,8 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
(1, vec![param_ty; 2], tcx.types.bool)
}

sym::black_box => (1, vec![param(0)], param(0)),

other => {
tcx.sess.emit_err(UnrecognizedIntrinsicFunction { span: it.span, name: other });
return;
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,11 +649,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match self.tcx.sess.source_map().span_to_snippet(closure_body_span) {
Ok(s) => {
let trimmed = s.trim_start();
let mut lines = trimmed.lines();
let line1 = lines.next().unwrap_or_default();

// If the closure contains a block then replace the opening brace
// with "{ let _ = (..); "
let sugg = if let Some('{') = trimmed.chars().next() {
format!("{{ {}; {}", migration_string, &trimmed[1..])
let sugg = if line1.trim_end() == "{" {
// This is a multi-line closure with just a `{` on the first line,
// so we put the `let` on its own line.
// We take the indentation from the next non-empty line.
let line2 = lines.filter(|line| !line.is_empty()).next().unwrap_or_default();
let indent = line2.split_once(|c: char| !c.is_whitespace()).unwrap_or_default().0;
format!("{{\n{}{};{}", indent, migration_string, &trimmed[line1.len()..])
} else if line1.starts_with('{') {
format!("{{ {}; {}", migration_string, &trimmed[1..].trim_start())
} else {
format!("{{ {}; {} }}", migration_string, s)
};
Expand Down
20 changes: 8 additions & 12 deletions library/core/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,19 @@ pub fn spin_loop() {
/// backend used. Programs cannot rely on `black_box` for *correctness* in any way.
///
/// [`std::convert::identity`]: crate::convert::identity
#[cfg_attr(not(miri), inline)]
#[cfg_attr(miri, inline(never))]
#[inline]
#[unstable(feature = "bench_black_box", issue = "64102")]
#[cfg_attr(miri, allow(unused_mut))]
#[cfg_attr(not(bootstrap), allow(unused_mut))]
pub fn black_box<T>(mut dummy: T) -> T {
// We need to "use" the argument in some way LLVM can't introspect, and on
// targets that support it we can typically leverage inline assembly to do
// this. LLVM's interpretation of inline assembly is that it's, well, a black
// box. This isn't the greatest implementation since it probably deoptimizes
// more than we want, but it's so far good enough.

#[cfg(not(miri))] // This is just a hint, so it is fine to skip in Miri.
#[cfg(bootstrap)]
// SAFETY: the inline assembly is a no-op.
unsafe {
// FIXME: Cannot use `asm!` because it doesn't support MIPS and other architectures.
llvm_asm!("" : : "r"(&mut dummy) : "memory" : "volatile");
dummy
}

dummy
#[cfg(not(bootstrap))]
{
crate::intrinsics::black_box(dummy)
}
}
Loading