Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f46a8e

Browse files
committedSep 26, 2024
Infer nounwind and use it in MIR opts
1 parent 648d024 commit 3f46a8e

20 files changed

+61
-30
lines changed
 

‎compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ provide! { tcx, def_id, other, cdata,
352352
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
353353
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
354354
cross_crate_inlinable => { table_direct }
355+
is_nounwind => { table_direct }
355356

356357
dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
357358
is_private_dep => { cdata.private_dep }

‎compiler/rustc_metadata/src/rmeta/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ define_tables! {
404404
// individually instead of `DefId`s.
405405
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
406406
cross_crate_inlinable: Table<DefIndex, bool>,
407+
is_nounwind: Table<DefIndex, bool>,
407408

408409
- optional:
409410
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,

‎compiler/rustc_middle/src/query/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1499,6 +1499,10 @@ rustc_queries! {
14991499
desc { |tcx| "checking if `{}` contains FFI-unwind calls", tcx.def_path_str(key) }
15001500
cache_on_disk_if { true }
15011501
}
1502+
query is_nounwind(key: DefId) -> bool {
1503+
desc { |tcx| "checking if `{}` contains unwinds", tcx.def_path_str(key) }
1504+
separate_provide_extern
1505+
}
15021506
query required_panic_strategy(_: CrateNum) -> Option<PanicStrategy> {
15031507
fatal_cycle
15041508
desc { "getting a crate's required panic strategy" }

‎compiler/rustc_mir_transform/src/instsimplify.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ impl<'tcx> InstSimplifyContext<'_, 'tcx> {
326326
_ => bug!("unexpected body ty: {:?}", body_ty),
327327
};
328328

329-
if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi) {
329+
if !layout::fn_can_unwind(self.tcx, Some(def_id), body_abi)
330+
|| (self.tcx.sess.opts.incremental.is_none() && self.tcx.is_nounwind(def_id))
331+
{
330332
*unwind = UnwindAction::Unreachable;
331333
}
332334
}

‎compiler/rustc_mir_transform/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ pub fn provide(providers: &mut Providers) {
134134
promoted_mir,
135135
deduced_param_attrs: deduce_param_attrs::deduced_param_attrs,
136136
coroutine_by_move_body_def_id: coroutine::coroutine_by_move_body_def_id,
137+
is_nounwind,
137138
..providers.queries
138139
};
139140
}
@@ -338,6 +339,8 @@ fn mir_promoted(
338339
tcx.ensure_with_value().coroutine_by_move_body_def_id(def);
339340
}
340341

342+
tcx.ensure_with_value().is_nounwind(def);
343+
341344
let mut body = tcx.mir_built(def).steal();
342345
if let Some(error_reported) = const_qualifs.tainted_by_errors {
343346
body.tainted_by_errors = Some(error_reported);
@@ -360,6 +363,24 @@ fn mir_promoted(
360363
(tcx.alloc_steal_mir(body), tcx.alloc_steal_promoted(promoted))
361364
}
362365

366+
fn is_nounwind<'tcx>(tcx: TyCtxt<'tcx>, local_def_id: LocalDefId) -> bool {
367+
if !tcx.is_mir_available(local_def_id) {
368+
return false;
369+
}
370+
371+
let def_id = local_def_id.to_def_id();
372+
let kind = tcx.def_kind(def_id);
373+
if !kind.is_fn_like() {
374+
return false;
375+
}
376+
377+
let body = &*tcx.mir_built(local_def_id).borrow();
378+
if body.basic_blocks.iter().all(|block| block.terminator().unwind().is_none()) {
379+
return true;
380+
}
381+
false
382+
}
383+
363384
/// Compute the MIR that is used during CTFE (and thus has no optimizations run on it)
364385
fn mir_for_ctfe(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &Body<'_> {
365386
tcx.arena.alloc(inner_mir_for_ctfe(tcx, def_id))

‎tests/codegen/drop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn droppy() {
2323
// FIXME(eddyb) the `void @` forces a match on the instruction, instead of the
2424
// comment, that's `; call core::ptr::drop_in_place::<drop::SomeUniqueName>`
2525
// for the `v0` mangling, should switch to matching on that once `legacy` is gone.
26-
// CHECK-COUNT-6: {{(call|invoke) void @.*}}drop_in_place{{.*}}SomeUniqueName
26+
// CHECK-COUNT-5: {{(call|invoke) void @.*}}drop_in_place{{.*}}SomeUniqueName
2727
// CHECK-NOT: {{(call|invoke) void @.*}}drop_in_place{{.*}}SomeUniqueName
2828
// The next line checks for the } that ends the function definition
2929
// CHECK-LABEL: {{^[}]}}

‎tests/codegen/personality_lifetimes.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ impl Drop for S {
1313
}
1414

1515
#[inline(never)]
16-
fn might_unwind() {}
16+
fn might_unwind() {
17+
panic!()
18+
}
1719

1820
// CHECK-LABEL: @test
1921
#[no_mangle]

‎tests/mir-opt/inline/caller_with_trivial_bound.foo.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
bb0: {
1212
StorageLive(_1);
13-
_1 = bar::<T>() -> [return: bb1, unwind continue];
13+
_1 = bar::<T>() -> [return: bb1, unwind unreachable];
1414
}
1515

1616
bb1: {

‎tests/mir-opt/inline/inline_coroutine.main.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
StorageLive(_2);
3232
StorageLive(_3);
3333
StorageLive(_4);
34-
- _4 = g() -> [return: bb1, unwind continue];
34+
- _4 = g() -> [return: bb1, unwind unreachable];
3535
+ _4 = {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8 (#0)};
3636
+ _3 = &mut _4;
3737
+ _2 = Pin::<&mut {coroutine@$DIR/inline_coroutine.rs:20:5: 20:8}> { __pointer: copy _3 };

‎tests/mir-opt/inline/inline_options.main.Inline.after.panic-unwind.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() -> () {
2121
StorageLive(_3);
2222
StorageLive(_4);
2323
StorageLive(_5);
24-
_3 = g() -> [return: bb3, unwind continue];
24+
_3 = g() -> [return: bb3, unwind unreachable];
2525
}
2626

2727
bb2: {
@@ -34,10 +34,10 @@ fn main() -> () {
3434
}
3535

3636
bb3: {
37-
_4 = g() -> [return: bb4, unwind continue];
37+
_4 = g() -> [return: bb4, unwind unreachable];
3838
}
3939

4040
bb4: {
41-
_5 = g() -> [return: bb2, unwind continue];
41+
_5 = g() -> [return: bb2, unwind unreachable];
4242
}
4343
}

‎tests/mir-opt/inline/inline_trait_method.test.Inline.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn test(_1: &dyn X) -> u32 {
88
bb0: {
99
StorageLive(_2);
1010
_2 = copy _1;
11-
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
11+
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1212
}
1313

1414
bb1: {

‎tests/mir-opt/inline/inline_trait_method_2.test2.Inline.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn test2(_1: &dyn X) -> bool {
1515
_3 = copy _1;
1616
_2 = move _3;
1717
StorageDead(_3);
18-
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind continue];
18+
_0 = <dyn X as X>::y(move _2) -> [return: bb1, unwind unreachable];
1919
}
2020

2121
bb1: {

‎tests/mir-opt/inline/issue_78442.bar.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
StorageLive(_2);
1616
StorageLive(_3);
1717
StorageLive(_4);
18-
- _4 = hide_foo() -> [return: bb1, unwind: bb4];
18+
- _4 = hide_foo() -> [return: bb1, unwind unreachable];
1919
- }
2020
-
2121
- bb1: {

‎tests/mir-opt/inline/rustc_no_mir_inline.caller.Inline.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
bb0: {
99
StorageLive(_1);
10-
_1 = callee() -> [return: bb1, unwind continue];
10+
_1 = callee() -> [return: bb1, unwind unreachable];
1111
}
1212

1313
bb1: {

‎tests/mir-opt/inline/rustc_no_mir_inline.caller.PreCodegen.after.panic-unwind.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn caller() -> () {
55
let _1: ();
66

77
bb0: {
8-
_1 = callee() -> [return: bb1, unwind continue];
8+
_1 = callee() -> [return: bb1, unwind unreachable];
99
}
1010

1111
bb1: {

‎tests/mir-opt/inline/unsized_argument.caller.Inline.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
StorageLive(_3);
1414
_3 = move _1;
1515
_4 = copy (((_3.0: std::ptr::Unique<[i32]>).0: std::ptr::NonNull<[i32]>).0: *const [i32]);
16-
_2 = callee(move (*_4)) -> [return: bb1, unwind: bb3];
16+
_2 = callee(move (*_4)) -> [return: bb1, unwind unreachable];
1717
}
1818

1919
bb1: {

‎tests/mir-opt/simplify_if.main.SimplifyConstCondition-after-const-prop.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313
bb1: {
14-
_1 = noop() -> [return: bb2, unwind continue];
14+
_1 = noop() -> [return: bb2, unwind unreachable];
1515
}
1616

1717
bb2: {

‎tests/mir-opt/simplify_match.main.GVN.panic-unwind.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
}
2424

2525
bb1: {
26-
_0 = noop() -> [return: bb2, unwind continue];
26+
_0 = noop() -> [return: bb2, unwind unreachable];
2727
}
2828

2929
bb2: {

‎tests/ui/lint/unused/lint-unused-variables.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unused variable: `a`
2-
--> $DIR/lint-unused-variables.rs:8:5
2+
--> $DIR/lint-unused-variables.rs:68:9
33
|
4-
LL | a: i32,
5-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
4+
LL | a: i32,
5+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
66
|
77
note: the lint level is defined here
88
--> $DIR/lint-unused-variables.rs:5:9
@@ -11,13 +11,13 @@ LL | #![deny(unused_variables)]
1111
| ^^^^^^^^^^^^^^^^
1212

1313
error: unused variable: `a`
14-
--> $DIR/lint-unused-variables.rs:22:9
14+
--> $DIR/lint-unused-variables.rs:8:5
1515
|
16-
LL | a: i32,
17-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
16+
LL | a: i32,
17+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
1818

1919
error: unused variable: `a`
20-
--> $DIR/lint-unused-variables.rs:68:9
20+
--> $DIR/lint-unused-variables.rs:22:9
2121
|
2222
LL | a: i32,
2323
| ^ help: if this is intentional, prefix it with an underscore: `_a`

‎tests/ui/rfcs/rfc-2565-param-attrs/param-attrs-cfg.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: unused variable: `a`
2-
--> $DIR/param-attrs-cfg.rs:24:23
2+
--> $DIR/param-attrs-cfg.rs:107:27
33
|
4-
LL | #[cfg(something)] a: i32,
5-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
4+
LL | #[cfg(something)] a: i32,
5+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
66
|
77
note: the lint level is defined here
88
--> $DIR/param-attrs-cfg.rs:5:9
@@ -11,13 +11,13 @@ LL | #![deny(unused_variables)]
1111
| ^^^^^^^^^^^^^^^^
1212

1313
error: unused variable: `a`
14-
--> $DIR/param-attrs-cfg.rs:41:27
14+
--> $DIR/param-attrs-cfg.rs:24:23
1515
|
16-
LL | #[cfg(something)] a: i32,
17-
| ^ help: if this is intentional, prefix it with an underscore: `_a`
16+
LL | #[cfg(something)] a: i32,
17+
| ^ help: if this is intentional, prefix it with an underscore: `_a`
1818

1919
error: unused variable: `a`
20-
--> $DIR/param-attrs-cfg.rs:107:27
20+
--> $DIR/param-attrs-cfg.rs:41:27
2121
|
2222
LL | #[cfg(something)] a: i32,
2323
| ^ help: if this is intentional, prefix it with an underscore: `_a`

0 commit comments

Comments
 (0)
Failed to load comments.