Skip to content

Commit 1f5d8de

Browse files
Rollup merge of #80040 - tmiasko:always-lower-intrinsics, r=Dylan-DPC
Always run intrinsics lowering pass Move intrinsics lowering pass from the optimization phase (where it would not run if -Zmir-opt-level=0), to the drop lowering phase where it runs unconditionally. The implementation of those intrinsics in code generation and interpreter is unnecessary. Remove it.
2 parents 93f1c67 + a9ff4bd commit 1f5d8de

9 files changed

+38
-39
lines changed

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+1-15
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8383
return;
8484
}
8585

86-
sym::unreachable => {
87-
return;
88-
}
8986
sym::va_start => bx.va_start(args[0].immediate()),
9087
sym::va_end => bx.va_end(args[0].immediate()),
9188
sym::size_of_val => {
@@ -106,8 +103,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
106103
bx.const_usize(bx.layout_of(tp_ty).align.abi.bytes())
107104
}
108105
}
109-
sym::size_of
110-
| sym::pref_align_of
106+
sym::pref_align_of
111107
| sym::min_align_of
112108
| sym::needs_drop
113109
| sym::type_id
@@ -119,10 +115,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
119115
.unwrap();
120116
OperandRef::from_const(bx, value, ret_ty).immediate_or_packed_pair(bx)
121117
}
122-
// Effectively no-op
123-
sym::forget => {
124-
return;
125-
}
126118
sym::offset => {
127119
let ptr = args[0].immediate();
128120
let offset = args[1].immediate();
@@ -218,9 +210,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
218210
sym::add_with_overflow
219211
| sym::sub_with_overflow
220212
| sym::mul_with_overflow
221-
| sym::wrapping_add
222-
| sym::wrapping_sub
223-
| sym::wrapping_mul
224213
| sym::unchecked_div
225214
| sym::unchecked_rem
226215
| sym::unchecked_shl
@@ -254,9 +243,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
254243

255244
return;
256245
}
257-
sym::wrapping_add => bx.add(args[0].immediate(), args[1].immediate()),
258-
sym::wrapping_sub => bx.sub(args[0].immediate(), args[1].immediate()),
259-
sym::wrapping_mul => bx.mul(args[0].immediate(), args[1].immediate()),
260246
sym::exact_div => {
261247
if signed {
262248
bx.exactsdiv(args[0].immediate(), args[1].immediate())

compiler/rustc_mir/src/interpret/intrinsics.rs

+8-23
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,11 @@ crate fn eval_nullary_intrinsic<'tcx>(
6161
ConstValue::Slice { data: alloc, start: 0, end: alloc.len() }
6262
}
6363
sym::needs_drop => ConstValue::from_bool(tp_ty.needs_drop(tcx, param_env)),
64-
sym::size_of | sym::min_align_of | sym::pref_align_of => {
64+
sym::min_align_of | sym::pref_align_of => {
6565
let layout = tcx.layout_of(param_env.and(tp_ty)).map_err(|e| err_inval!(Layout(e)))?;
6666
let n = match name {
6767
sym::pref_align_of => layout.align.pref.bytes(),
6868
sym::min_align_of => layout.align.abi.bytes(),
69-
sym::size_of => layout.size.bytes(),
7069
_ => bug!(),
7170
};
7271
ConstValue::from_machine_usize(n, &tcx)
@@ -125,7 +124,6 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
125124
let (dest, ret) = match ret {
126125
None => match intrinsic_name {
127126
sym::transmute => throw_ub_format!("transmuting to uninhabited type"),
128-
sym::unreachable => throw_ub!(Unreachable),
129127
sym::abort => M::abort(self, "the program aborted execution".to_owned())?,
130128
// Unsupported diverging intrinsic.
131129
_ => return Ok(false),
@@ -160,13 +158,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
160158
sym::min_align_of
161159
| sym::pref_align_of
162160
| sym::needs_drop
163-
| sym::size_of
164161
| sym::type_id
165162
| sym::type_name
166163
| sym::variant_count => {
167164
let gid = GlobalId { instance, promoted: None };
168165
let ty = match intrinsic_name {
169-
sym::min_align_of | sym::pref_align_of | sym::size_of | sym::variant_count => {
166+
sym::min_align_of | sym::pref_align_of | sym::variant_count => {
170167
self.tcx.types.usize
171168
}
172169
sym::needs_drop => self.tcx.types.bool,
@@ -212,28 +209,16 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
212209
let out_val = numeric_intrinsic(intrinsic_name, bits, kind)?;
213210
self.write_scalar(out_val, dest)?;
214211
}
215-
sym::wrapping_add
216-
| sym::wrapping_sub
217-
| sym::wrapping_mul
218-
| sym::add_with_overflow
219-
| sym::sub_with_overflow
220-
| sym::mul_with_overflow => {
212+
sym::add_with_overflow | sym::sub_with_overflow | sym::mul_with_overflow => {
221213
let lhs = self.read_immediate(args[0])?;
222214
let rhs = self.read_immediate(args[1])?;
223-
let (bin_op, ignore_overflow) = match intrinsic_name {
224-
sym::wrapping_add => (BinOp::Add, true),
225-
sym::wrapping_sub => (BinOp::Sub, true),
226-
sym::wrapping_mul => (BinOp::Mul, true),
227-
sym::add_with_overflow => (BinOp::Add, false),
228-
sym::sub_with_overflow => (BinOp::Sub, false),
229-
sym::mul_with_overflow => (BinOp::Mul, false),
215+
let bin_op = match intrinsic_name {
216+
sym::add_with_overflow => BinOp::Add,
217+
sym::sub_with_overflow => BinOp::Sub,
218+
sym::mul_with_overflow => BinOp::Mul,
230219
_ => bug!("Already checked for int ops"),
231220
};
232-
if ignore_overflow {
233-
self.binop_ignore_overflow(bin_op, lhs, rhs, dest)?;
234-
} else {
235-
self.binop_with_overflow(bin_op, lhs, rhs, dest)?;
236-
}
221+
self.binop_with_overflow(bin_op, lhs, rhs, dest)?;
237222
}
238223
sym::saturating_add | sym::saturating_sub => {
239224
let l = self.read_immediate(args[0])?;

compiler/rustc_mir/src/transform/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ fn run_post_borrowck_cleanup_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tc
364364
// `AddRetag` needs to run after `ElaborateDrops`. Otherwise it should run fairly late,
365365
// but before optimizations begin.
366366
&add_retag::AddRetag,
367+
&lower_intrinsics::LowerIntrinsics,
367368
&simplify::SimplifyCfg::new("elaborate-drops"),
368369
// `Deaggregator` is conceptually part of MIR building, some backends rely on it happening
369370
// and it can help optimizations.
@@ -392,7 +393,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
392393

393394
// The main optimizations that we do on MIR.
394395
let optimizations: &[&dyn MirPass<'tcx>] = &[
395-
&lower_intrinsics::LowerIntrinsics,
396396
&remove_unneeded_drops::RemoveUnneededDrops,
397397
&match_branches::MatchBranchSimplification,
398398
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)

src/test/mir-opt/lower_intrinsics.discriminant.LowerIntrinsics.diff

+4
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,9 @@
120120
bb5: {
121121
return; // scope 0 at $DIR/lower_intrinsics.rs:73:2: 73:2
122122
}
123+
124+
bb6 (cleanup): {
125+
resume; // scope 0 at $DIR/lower_intrinsics.rs:68:1: 73:2
126+
}
123127
}
124128

src/test/mir-opt/lower_intrinsics.forget.LowerIntrinsics.diff

+8
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,15 @@
2525
StorageDead(_3); // scope 1 at $DIR/lower_intrinsics.rs:19:40: 19:41
2626
StorageDead(_2); // scope 0 at $DIR/lower_intrinsics.rs:19:43: 19:44
2727
_0 = const (); // scope 0 at $DIR/lower_intrinsics.rs:18:24: 20:2
28+
goto -> bb2; // scope 0 at $DIR/lower_intrinsics.rs:20:1: 20:2
29+
}
30+
31+
bb2: {
2832
return; // scope 0 at $DIR/lower_intrinsics.rs:20:2: 20:2
2933
}
34+
35+
bb3 (cleanup): {
36+
resume; // scope 0 at $DIR/lower_intrinsics.rs:18:1: 20:2
37+
}
3038
}
3139

src/test/mir-opt/lower_intrinsics.non_const.LowerIntrinsics.diff

+4
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,9 @@
2727
StorageDead(_1); // scope 0 at $DIR/lower_intrinsics.rs:59:1: 59:2
2828
return; // scope 0 at $DIR/lower_intrinsics.rs:59:2: 59:2
2929
}
30+
31+
bb2 (cleanup): {
32+
resume; // scope 0 at $DIR/lower_intrinsics.rs:55:1: 59:2
33+
}
3034
}
3135

src/test/mir-opt/lower_intrinsics.size_of.LowerIntrinsics.diff

+4
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@
1616
bb1: {
1717
return; // scope 0 at $DIR/lower_intrinsics.rs:15:2: 15:2
1818
}
19+
20+
bb2 (cleanup): {
21+
resume; // scope 0 at $DIR/lower_intrinsics.rs:13:1: 15:2
22+
}
1923
}
2024

src/test/mir-opt/lower_intrinsics.unreachable.LowerIntrinsics.diff

+4
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,9 @@
1818
- // + literal: Const { ty: unsafe extern "rust-intrinsic" fn() -> ! {std::intrinsics::unreachable}, val: Value(Scalar(<ZST>)) }
1919
+ unreachable; // scope 1 at $DIR/lower_intrinsics.rs:24:14: 24:45
2020
}
21+
22+
bb1 (cleanup): {
23+
resume; // scope 0 at $DIR/lower_intrinsics.rs:23:1: 25:2
24+
}
2125
}
2226

src/test/mir-opt/lower_intrinsics.wrapping.LowerIntrinsics.diff

+4
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,9 @@
7979
StorageDead(_3); // scope 0 at $DIR/lower_intrinsics.rs:10:1: 10:2
8080
return; // scope 0 at $DIR/lower_intrinsics.rs:10:2: 10:2
8181
}
82+
83+
bb4 (cleanup): {
84+
resume; // scope 0 at $DIR/lower_intrinsics.rs:6:1: 10:2
85+
}
8286
}
8387

0 commit comments

Comments
 (0)