Skip to content

Commit c28641c

Browse files
authored
Unrolled build for rust-lang#122287
Rollup merge of rust-lang#122287 - RalfJung:simd-static-assert, r=pnkfelix add test ensuring simd codegen checks don't run when a static assertion failed stdarch relies on this to ensure that SIMD indices are in bounds. I would love to know why this works, but I can't figure out where codegen decides to not codegen a function if a required-const does not evaluate. `@oli-obk` `@bjorn3` do you have any idea?
2 parents fe61575 + d765fb8 commit c28641c

File tree

7 files changed

+56
-8
lines changed

7 files changed

+56
-8
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
7171
// This cannot fail because we checked all required_consts in advance.
7272
let val = cv
7373
.eval(fx.tcx, ty::ParamEnv::reveal_all(), Some(constant.span))
74-
.expect("erroneous constant not captured by required_consts");
74+
.expect("erroneous constant missed by mono item collection");
7575
(val, cv.ty())
7676
}
7777

compiler/rustc_codegen_ssa/src/mir/constant.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2121
}
2222

2323
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
24-
// `MirUsedCollector` visited all constants before codegen began, so if we got here there
25-
// can be no more constants that fail to evaluate.
24+
// `MirUsedCollector` visited all required_consts before codegen began, so if we got here
25+
// there can be no more constants that fail to evaluate.
2626
self.monomorphize(constant.const_)
2727
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), Some(constant.span))
28-
.expect("erroneous constant not captured by required_consts")
28+
.expect("erroneous constant missed by mono item collection")
2929
}
3030

3131
/// This is a convenience helper for `simd_shuffle_indices`. It has the precondition

compiler/rustc_codegen_ssa/src/mir/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
211211

212212
// It may seem like we should iterate over `required_consts` to ensure they all successfully
213213
// evaluate; however, the `MirUsedCollector` already did that during the collection phase of
214-
// monomorphization so we don't have to do it again.
214+
// monomorphization, and if there is an error during collection then codegen never starts -- so
215+
// we don't have to do it again.
215216

216217
fx.per_local_var_debug_info = fx.compute_per_local_var_debug_info(&mut start_bx);
217218

compiler/rustc_monomorphize/src/collector.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -818,13 +818,16 @@ impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
818818
self.super_rvalue(rvalue, location);
819819
}
820820

821-
/// This does not walk the constant, as it has been handled entirely here and trying
822-
/// to walk it would attempt to evaluate the `ty::Const` inside, which doesn't necessarily
823-
/// work, as some constants cannot be represented in the type system.
821+
/// This does not walk the MIR of the constant as that is not needed for codegen, all we need is
822+
/// to ensure that the constant evaluates successfully and walk the result.
824823
#[instrument(skip(self), level = "debug")]
825824
fn visit_constant(&mut self, constant: &mir::ConstOperand<'tcx>, location: Location) {
826825
let const_ = self.monomorphize(constant.const_);
827826
let param_env = ty::ParamEnv::reveal_all();
827+
// Evaluate the constant. This makes const eval failure a collection-time error (rather than
828+
// a codegen-time error). rustc stops after collection if there was an error, so this
829+
// ensures codegen never has to worry about failing consts.
830+
// (codegen relies on this and ICEs will happen if this is violated.)
828831
let val = match const_.eval(self.tcx, param_env, None) {
829832
Ok(v) => v,
830833
Err(ErrorHandled::Reported(..)) => return,

compiler/rustc_monomorphize/src/partitioning.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,9 @@ fn collect_and_partition_mono_items(tcx: TyCtxt<'_>, (): ()) -> (&DefIdSet, &[Co
11121112

11131113
let (items, usage_map) = collector::collect_crate_mono_items(tcx, collection_mode);
11141114

1115+
// If there was an error during collection (e.g. from one of the constants we evaluated),
1116+
// then we stop here. This way codegen does not have to worry about failing constants.
1117+
// (codegen relies on this and ICEs will happen if this is violated.)
11151118
tcx.dcx().abort_if_errors();
11161119

11171120
let (codegen_units, _) = tcx.sess.time("partition_and_assert_distinct_symbols", || {
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@build-fail
2+
//! Make sure that monomorphization-time const errors from `static_assert` take priority over the
3+
//! error from simd_extract. Basically this checks that if a const fails to evaluate in some
4+
//! function, we don't bother codegen'ing the function.
5+
#![feature(generic_arg_infer)]
6+
#![feature(core_intrinsics)]
7+
#![feature(repr_simd)]
8+
#![feature(inline_const)]
9+
use std::intrinsics::simd::*;
10+
11+
#[repr(simd)]
12+
#[allow(non_camel_case_types)]
13+
struct int8x4_t(u8,u8,u8,u8);
14+
15+
fn get_elem<const LANE: u32>(a: int8x4_t) -> u8 {
16+
const { assert!(LANE < 4); } // the error should be here...
17+
//~^ ERROR failed
18+
//~| assertion failed
19+
unsafe { simd_extract(a, LANE) } // ...not here
20+
}
21+
22+
fn main() {
23+
get_elem::<4>(int8x4_t(0,0,0,0));
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0080]: evaluation of `get_elem::<4>::{constant#0}` failed
2+
--> $DIR/const-err-trumps-simd-err.rs:16:13
3+
|
4+
LL | const { assert!(LANE < 4); } // the error should be here...
5+
| ^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: LANE < 4', $DIR/const-err-trumps-simd-err.rs:16:13
6+
|
7+
= note: this error originates in the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
note: the above error was encountered while instantiating `fn get_elem::<4>`
10+
--> $DIR/const-err-trumps-simd-err.rs:23:5
11+
|
12+
LL | get_elem::<4>(int8x4_t(0,0,0,0));
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)