Skip to content

Commit 3f964f8

Browse files
committed
Auto merge of rust-lang#129027 - erikdesjardins:outofuandme, r=<try>
Stop generating assumes for validity ranges After rust-lang#128371, we represent validity ranges as parameter / return value attributes, so we no longer need to use assumes. r? `@ghost`
2 parents a2e1d15 + 0d514ae commit 3f964f8

20 files changed

+60
-333
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+4-65
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
242242
if let OperandValueKind::Immediate(to_scalar) = cast_kind
243243
&& from_scalar.size(self.cx) == to_scalar.size(self.cx)
244244
{
245-
let from_backend_ty = bx.backend_type(operand.layout);
246245
let to_backend_ty = bx.backend_type(cast);
247246
Some(OperandValue::Immediate(self.transmute_immediate(
248247
bx,
249248
imm,
250249
from_scalar,
251-
from_backend_ty,
252250
to_scalar,
253251
to_backend_ty,
254252
)))
@@ -264,13 +262,11 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
264262
&& in_a.size(self.cx) == out_a.size(self.cx)
265263
&& in_b.size(self.cx) == out_b.size(self.cx)
266264
{
267-
let in_a_ibty = bx.scalar_pair_element_backend_type(operand.layout, 0, false);
268-
let in_b_ibty = bx.scalar_pair_element_backend_type(operand.layout, 1, false);
269265
let out_a_ibty = bx.scalar_pair_element_backend_type(cast, 0, false);
270266
let out_b_ibty = bx.scalar_pair_element_backend_type(cast, 1, false);
271267
Some(OperandValue::Pair(
272-
self.transmute_immediate(bx, imm_a, in_a, in_a_ibty, out_a, out_a_ibty),
273-
self.transmute_immediate(bx, imm_b, in_b, in_b_ibty, out_b, out_b_ibty),
268+
self.transmute_immediate(bx, imm_a, in_a, out_a, out_a_ibty),
269+
self.transmute_immediate(bx, imm_b, in_b, out_b, out_b_ibty),
274270
))
275271
} else {
276272
None
@@ -294,12 +290,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
294290
) -> Option<Bx::Value> {
295291
use abi::Primitive::*;
296292

297-
// When scalars are passed by value, there's no metadata recording their
298-
// valid ranges. For example, `char`s are passed as just `i32`, with no
299-
// way for LLVM to know that they're 0x10FFFF at most. Thus we assume
300-
// the range of the input value too, not just the output range.
301-
self.assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
302-
303293
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
304294
(Int(_, is_signed), Int(..)) => bx.intcast(imm, to_backend_ty, is_signed),
305295
(Float(_), Float(_)) => {
@@ -341,21 +331,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
341331
bx: &mut Bx,
342332
mut imm: Bx::Value,
343333
from_scalar: abi::Scalar,
344-
from_backend_ty: Bx::Type,
345334
to_scalar: abi::Scalar,
346335
to_backend_ty: Bx::Type,
347336
) -> Bx::Value {
337+
use abi::Primitive::*;
338+
348339
assert_eq!(from_scalar.size(self.cx), to_scalar.size(self.cx));
349340

350-
use abi::Primitive::*;
351341
imm = bx.from_immediate(imm);
352342

353-
// When scalars are passed by value, there's no metadata recording their
354-
// valid ranges. For example, `char`s are passed as just `i32`, with no
355-
// way for LLVM to know that they're 0x10FFFF at most. Thus we assume
356-
// the range of the input value too, not just the output range.
357-
self.assume_scalar_range(bx, imm, from_scalar, from_backend_ty);
358-
359343
imm = match (from_scalar.primitive(), to_scalar.primitive()) {
360344
(Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
361345
(Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
@@ -370,55 +354,10 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
370354
bx.bitcast(int_imm, to_backend_ty)
371355
}
372356
};
373-
self.assume_scalar_range(bx, imm, to_scalar, to_backend_ty);
374357
imm = bx.to_immediate_scalar(imm, to_scalar);
375358
imm
376359
}
377360

378-
fn assume_scalar_range(
379-
&self,
380-
bx: &mut Bx,
381-
imm: Bx::Value,
382-
scalar: abi::Scalar,
383-
backend_ty: Bx::Type,
384-
) {
385-
if matches!(self.cx.sess().opts.optimize, OptLevel::No | OptLevel::Less)
386-
// For now, the critical niches are all over `Int`eger values.
387-
// Should floating-point values or pointers ever get more complex
388-
// niches, then this code will probably want to handle them too.
389-
|| !matches!(scalar.primitive(), abi::Primitive::Int(..))
390-
|| scalar.is_always_valid(self.cx)
391-
{
392-
return;
393-
}
394-
395-
let abi::WrappingRange { start, end } = scalar.valid_range(self.cx);
396-
397-
if start <= end {
398-
if start > 0 {
399-
let low = bx.const_uint_big(backend_ty, start);
400-
let cmp = bx.icmp(IntPredicate::IntUGE, imm, low);
401-
bx.assume(cmp);
402-
}
403-
404-
let type_max = scalar.size(self.cx).unsigned_int_max();
405-
if end < type_max {
406-
let high = bx.const_uint_big(backend_ty, end);
407-
let cmp = bx.icmp(IntPredicate::IntULE, imm, high);
408-
bx.assume(cmp);
409-
}
410-
} else {
411-
let low = bx.const_uint_big(backend_ty, start);
412-
let cmp_low = bx.icmp(IntPredicate::IntUGE, imm, low);
413-
414-
let high = bx.const_uint_big(backend_ty, end);
415-
let cmp_high = bx.icmp(IntPredicate::IntULE, imm, high);
416-
417-
let or = bx.or(cmp_low, cmp_high);
418-
bx.assume(or);
419-
}
420-
}
421-
422361
pub fn codegen_rvalue_unsized(
423362
&mut self,
424363
bx: &mut Bx,

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

+2-74
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ use rustc_middle::mir::interpret::Scalar;
88
use rustc_middle::mir::*;
99
use rustc_middle::thir::*;
1010
use rustc_middle::ty::cast::{mir_cast_kind, CastTy};
11-
use rustc_middle::ty::layout::IntegerExt;
1211
use rustc_middle::ty::util::IntTypeExt;
1312
use rustc_middle::ty::{self, Ty, UpvarArgs};
1413
use rustc_span::source_map::Spanned;
1514
use rustc_span::{Span, DUMMY_SP};
16-
use rustc_target::abi::{Abi, FieldIdx, Primitive};
15+
use rustc_target::abi::FieldIdx;
1716
use tracing::debug;
1817

1918
use crate::build::expr::as_place::PlaceBase;
@@ -201,85 +200,14 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
201200
{
202201
let discr_ty = adt_def.repr().discr_type().to_ty(this.tcx);
203202
let temp = unpack!(block = this.as_temp(block, scope, source, Mutability::Not));
204-
let layout = this.tcx.layout_of(this.param_env.and(source_expr.ty));
205203
let discr = this.temp(discr_ty, source_expr.span);
206204
this.cfg.push_assign(
207205
block,
208206
source_info,
209207
discr,
210208
Rvalue::Discriminant(temp.into()),
211209
);
212-
let (op, ty) = (Operand::Move(discr), discr_ty);
213-
214-
if let Abi::Scalar(scalar) = layout.unwrap().abi
215-
&& !scalar.is_always_valid(&this.tcx)
216-
&& let Primitive::Int(int_width, _signed) = scalar.primitive()
217-
{
218-
let unsigned_ty = int_width.to_ty(this.tcx, false);
219-
let unsigned_place = this.temp(unsigned_ty, expr_span);
220-
this.cfg.push_assign(
221-
block,
222-
source_info,
223-
unsigned_place,
224-
Rvalue::Cast(CastKind::IntToInt, Operand::Copy(discr), unsigned_ty),
225-
);
226-
227-
let bool_ty = this.tcx.types.bool;
228-
let range = scalar.valid_range(&this.tcx);
229-
let merge_op =
230-
if range.start <= range.end { BinOp::BitAnd } else { BinOp::BitOr };
231-
232-
let mut comparer = |range: u128, bin_op: BinOp| -> Place<'tcx> {
233-
let range_val = Const::from_bits(
234-
this.tcx,
235-
range,
236-
ty::ParamEnv::empty().and(unsigned_ty),
237-
);
238-
let lit_op = this.literal_operand(expr.span, range_val);
239-
let is_bin_op = this.temp(bool_ty, expr_span);
240-
this.cfg.push_assign(
241-
block,
242-
source_info,
243-
is_bin_op,
244-
Rvalue::BinaryOp(
245-
bin_op,
246-
Box::new((Operand::Copy(unsigned_place), lit_op)),
247-
),
248-
);
249-
is_bin_op
250-
};
251-
let assert_place = if range.start == 0 {
252-
comparer(range.end, BinOp::Le)
253-
} else {
254-
let start_place = comparer(range.start, BinOp::Ge);
255-
let end_place = comparer(range.end, BinOp::Le);
256-
let merge_place = this.temp(bool_ty, expr_span);
257-
this.cfg.push_assign(
258-
block,
259-
source_info,
260-
merge_place,
261-
Rvalue::BinaryOp(
262-
merge_op,
263-
Box::new((
264-
Operand::Move(start_place),
265-
Operand::Move(end_place),
266-
)),
267-
),
268-
);
269-
merge_place
270-
};
271-
this.cfg.push(
272-
block,
273-
Statement {
274-
source_info,
275-
kind: StatementKind::Intrinsic(Box::new(
276-
NonDivergingIntrinsic::Assume(Operand::Move(assert_place)),
277-
)),
278-
},
279-
);
280-
}
281-
282-
(op, ty)
210+
(Operand::Move(discr), discr_ty)
283211
} else {
284212
let ty = source_expr.ty;
285213
let source = unpack!(

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -3628,7 +3628,6 @@ ui/regions/issue-56537-closure-uses-region-from-container.rs
36283628
ui/regions/issue-6157.rs
36293629
ui/regions/issue-72051-member-region-hang.rs
36303630
ui/regions/issue-78262.rs
3631-
ui/repr/issue-83505-repr-simd.rs
36323631
ui/resolve/auxiliary/issue-112831-aux.rs
36333632
ui/resolve/auxiliary/issue-19452-aux.rs
36343633
ui/resolve/auxiliary/issue-21221-3.rs

tests/codegen/cast-optimized.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ compile-flags: -O -Z merge-functions=disabled
2+
//@ min-llvm-version: 19
23
#![crate_type = "lib"]
34

45
// This tests that LLVM can optimize based on the niches in the source or

tests/codegen/enum/enum-bounds-check-derived-idx.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This test checks an optimization that is not guaranteed to work. This test case should not block
22
// a future LLVM update.
33
//@ compile-flags: -O
4+
//@ min-llvm-version: 19
45

56
#![crate_type = "lib"]
67

tests/codegen/enum/enum-bounds-check-issue-13926.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This test checks an optimization that is not guaranteed to work. This test case should not block
22
// a future LLVM update.
33
//@ compile-flags: -O
4+
//@ min-llvm-version: 19
45

56
#![crate_type = "lib"]
67

tests/codegen/enum/enum-bounds-check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//@ compile-flags: -O
2+
//@ min-llvm-version: 19
23

34
#![crate_type = "lib"]
45

0 commit comments

Comments
 (0)