forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#105119 - JakobDegen:inline-experiments, r=cjg…
…illot Disable top down MIR inlining The current MIR inliner has exponential behavior in some cases: <https://godbolt.org/z/7jnWah4fE>. The cause of this is top-down inlining, where we repeatedly do inlining like `call_a() => { call_b(); call_b(); }`. Each decision on its own seems to make sense, but the result is exponential. Disabling top-down inlining fundamentally prevents this. Each call site in the original, unoptimized source code is now considered for inlining exactly one time, which means that the total growth in MIR size is limited to number of call sites * inlining threshold. Top down inlining may be worth re-introducing at some point, but it needs to be accompanied with a principled way to prevent this kind of behavior.
- Loading branch information
Showing
10 changed files
with
243 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/test/mir-opt/inline/exponential_runtime.main.Inline.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
- // MIR for `main` before Inline | ||
+ // MIR for `main` after Inline | ||
|
||
fn main() -> () { | ||
let mut _0: (); // return place in scope 0 at $DIR/exponential_runtime.rs:+0:11: +0:11 | ||
let _1: (); // in scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 | ||
+ scope 1 (inlined <() as G>::call) { // at $DIR/exponential_runtime.rs:86:5: 86:22 | ||
+ let _2: (); // in scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 | ||
+ let _3: (); // in scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 | ||
+ let _4: (); // in scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 | ||
+ } | ||
|
||
bb0: { | ||
StorageLive(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 | ||
- _1 = <() as G>::call() -> bb1; // scope 0 at $DIR/exponential_runtime.rs:+1:5: +1:22 | ||
+ StorageLive(_2); // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 | ||
+ _2 = <() as F>::call() -> bb1; // scope 1 at $DIR/exponential_runtime.rs:73:9: 73:25 | ||
// mir::Constant | ||
- // + span: $DIR/exponential_runtime.rs:86:5: 86:20 | ||
- // + literal: Const { ty: fn() {<() as G>::call}, val: Value(<ZST>) } | ||
+ // + span: $DIR/exponential_runtime.rs:73:9: 73:23 | ||
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) } | ||
} | ||
|
||
bb1: { | ||
+ StorageDead(_2); // scope 1 at $DIR/exponential_runtime.rs:73:25: 73:26 | ||
+ StorageLive(_3); // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 | ||
+ _3 = <() as F>::call() -> bb2; // scope 1 at $DIR/exponential_runtime.rs:74:9: 74:25 | ||
+ // mir::Constant | ||
+ // + span: $DIR/exponential_runtime.rs:74:9: 74:23 | ||
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) } | ||
+ } | ||
+ | ||
+ bb2: { | ||
+ StorageDead(_3); // scope 1 at $DIR/exponential_runtime.rs:74:25: 74:26 | ||
+ StorageLive(_4); // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 | ||
+ _4 = <() as F>::call() -> bb3; // scope 1 at $DIR/exponential_runtime.rs:75:9: 75:25 | ||
+ // mir::Constant | ||
+ // + span: $DIR/exponential_runtime.rs:75:9: 75:23 | ||
+ // + literal: Const { ty: fn() {<() as F>::call}, val: Value(<ZST>) } | ||
+ } | ||
+ | ||
+ bb3: { | ||
+ StorageDead(_4); // scope 1 at $DIR/exponential_runtime.rs:75:25: 75:26 | ||
StorageDead(_1); // scope 0 at $DIR/exponential_runtime.rs:+1:22: +1:23 | ||
_0 = const (); // scope 0 at $DIR/exponential_runtime.rs:+0:11: +2:2 | ||
return; // scope 0 at $DIR/exponential_runtime.rs:+2:2: +2:2 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Checks that code with exponential runtime does not have exponential behavior in inlining. | ||
|
||
trait A { | ||
fn call(); | ||
} | ||
|
||
trait B { | ||
fn call(); | ||
} | ||
impl<T: A> B for T { | ||
#[inline] | ||
fn call() { | ||
<T as A>::call(); | ||
<T as A>::call(); | ||
<T as A>::call(); | ||
} | ||
} | ||
|
||
trait C { | ||
fn call(); | ||
} | ||
impl<T: B> C for T { | ||
#[inline] | ||
fn call() { | ||
<T as B>::call(); | ||
<T as B>::call(); | ||
<T as B>::call(); | ||
} | ||
} | ||
|
||
trait D { | ||
fn call(); | ||
} | ||
impl<T: C> D for T { | ||
#[inline] | ||
fn call() { | ||
<T as C>::call(); | ||
<T as C>::call(); | ||
<T as C>::call(); | ||
} | ||
} | ||
|
||
trait E { | ||
fn call(); | ||
} | ||
impl<T: D> E for T { | ||
#[inline] | ||
fn call() { | ||
<T as D>::call(); | ||
<T as D>::call(); | ||
<T as D>::call(); | ||
} | ||
} | ||
|
||
trait F { | ||
fn call(); | ||
} | ||
impl<T: E> F for T { | ||
#[inline] | ||
fn call() { | ||
<T as E>::call(); | ||
<T as E>::call(); | ||
<T as E>::call(); | ||
} | ||
} | ||
|
||
trait G { | ||
fn call(); | ||
} | ||
impl<T: F> G for T { | ||
#[inline] | ||
fn call() { | ||
<T as F>::call(); | ||
<T as F>::call(); | ||
<T as F>::call(); | ||
} | ||
} | ||
|
||
impl A for () { | ||
#[inline(never)] | ||
fn call() {} | ||
} | ||
|
||
// EMIT_MIR exponential_runtime.main.Inline.diff | ||
fn main() { | ||
<() as G>::call(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.