Skip to content

Commit

Permalink
Check history earlier.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jun 30, 2022
1 parent 5999f34 commit 114c928
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
19 changes: 5 additions & 14 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,10 @@ impl<'tcx> Inliner<'tcx> {
return None;
}

if self.history.contains(&callee) {
return None;
}

let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, substs);

return Some(CallSite {
Expand Down Expand Up @@ -407,22 +411,9 @@ impl<'tcx> Inliner<'tcx> {
}

TerminatorKind::Call { func: Operand::Constant(ref f), cleanup, .. } => {
if let ty::FnDef(def_id, substs) =
if let ty::FnDef(def_id, _) =
*callsite.callee.subst_mir(self.tcx, &f.literal.ty()).kind()
{
if let Ok(substs) =
self.tcx.try_normalize_erasing_regions(self.param_env, substs)
{
if let Ok(Some(instance)) =
Instance::resolve(self.tcx, self.param_env, def_id, substs)
{
if callsite.callee.def_id() == instance.def_id() {
return Err("self-recursion");
} else if self.history.contains(&instance) {
return Err("already inlined");
}
}
}
// Don't give intrinsics the extra penalty for calls
if tcx.is_intrinsic(def_id) {
cost += INSTR_COST;
Expand Down
11 changes: 7 additions & 4 deletions src/test/mir-opt/inline/inline_cycle.one.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
let mut _0: (); // return place in scope 0 at $DIR/inline-cycle.rs:13:10: 13:10
let _1: (); // in scope 0 at $DIR/inline-cycle.rs:14:5: 14:24
+ scope 1 (inlined <C as Call>::call) { // at $DIR/inline-cycle.rs:14:5: 14:24
+ scope 2 (inlined <A<C> as Call>::call) { // at $DIR/inline-cycle.rs:43:9: 43:23
+ scope 3 (inlined <B<C> as Call>::call) { // at $DIR/inline-cycle.rs:28:9: 28:31
+ }
+ }
+ }

bb0: {
StorageLive(_1); // scope 0 at $DIR/inline-cycle.rs:14:5: 14:24
- _1 = <C as Call>::call() -> bb1; // scope 0 at $DIR/inline-cycle.rs:14:5: 14:24
+ _1 = <A<C> as Call>::call() -> bb1; // scope 1 at $DIR/inline-cycle.rs:43:9: 43:23
+ _1 = <C as Call>::call() -> bb1; // scope 3 at $DIR/inline-cycle.rs:36:9: 36:28
// mir::Constant
- // + span: $DIR/inline-cycle.rs:14:5: 14:22
- // + literal: Const { ty: fn() {<C as Call>::call}, val: Value(Scalar(<ZST>)) }
+ // + span: $DIR/inline-cycle.rs:43:9: 43:21
+ // + literal: Const { ty: fn() {<A<C> as Call>::call}, val: Value(Scalar(<ZST>)) }
+ // + span: $DIR/inline-cycle.rs:36:9: 36:26
// + literal: Const { ty: fn() {<C as Call>::call}, val: Value(Scalar(<ZST>)) }
}

bb1: {
Expand Down
20 changes: 15 additions & 5 deletions src/test/mir-opt/inline/inline_cycle.two.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
+ let _3: (); // in scope 1 at $DIR/inline-cycle.rs:54:5: 54:8
+ let mut _4: fn() {f}; // in scope 1 at $DIR/inline-cycle.rs:54:5: 54:6
+ scope 2 (inlined <fn() {f} as FnOnce<()>>::call_once - shim(fn() {f})) { // at $DIR/inline-cycle.rs:54:5: 54:8
+ scope 3 (inlined f) { // at $SRC_DIR/core/src/ops/function.rs:LL:COL
+ let _6: (); // in scope 3 at $DIR/inline-cycle.rs:59:5: 59:12
+ }
+ }
+ }

Expand All @@ -21,19 +24,26 @@
+ _2 = f; // scope 0 at $DIR/inline-cycle.rs:49:5: 49:12
// mir::Constant
- // + span: $DIR/inline-cycle.rs:49:5: 49:9
- // + literal: Const { ty: fn(fn() {f}) {call::<fn() {f}>}, val: Value(Scalar(<ZST>)) }
- // mir::Constant
// + span: $DIR/inline-cycle.rs:49:10: 49:11
// + literal: Const { ty: fn() {f}, val: Value(Scalar(<ZST>)) }
+ // + span: $DIR/inline-cycle.rs:49:10: 49:11
+ // + literal: Const { ty: fn() {f}, val: Value(Scalar(<ZST>)) }
+ StorageLive(_3); // scope 1 at $DIR/inline-cycle.rs:54:5: 54:8
+ StorageLive(_4); // scope 1 at $DIR/inline-cycle.rs:54:5: 54:6
+ _4 = move _2; // scope 1 at $DIR/inline-cycle.rs:54:5: 54:6
+ StorageLive(_5); // scope 1 at $DIR/inline-cycle.rs:54:5: 54:8
+ _5 = const (); // scope 1 at $DIR/inline-cycle.rs:54:5: 54:8
+ _3 = move _4() -> bb1; // scope 2 at $SRC_DIR/core/src/ops/function.rs:LL:COL
+ StorageLive(_6); // scope 3 at $DIR/inline-cycle.rs:59:5: 59:12
+ _6 = call::<fn() {f}>(f) -> bb1; // scope 3 at $DIR/inline-cycle.rs:59:5: 59:12
+ // mir::Constant
+ // + span: $DIR/inline-cycle.rs:59:5: 59:9
// + literal: Const { ty: fn(fn() {f}) {call::<fn() {f}>}, val: Value(Scalar(<ZST>)) }
// mir::Constant
- // + span: $DIR/inline-cycle.rs:49:10: 49:11
+ // + span: $DIR/inline-cycle.rs:59:10: 59:11
// + literal: Const { ty: fn() {f}, val: Value(Scalar(<ZST>)) }
}

bb1: {
+ StorageDead(_6); // scope 3 at $DIR/inline-cycle.rs:59:12: 59:13
+ StorageDead(_5); // scope 1 at $DIR/inline-cycle.rs:54:5: 54:8
+ StorageDead(_4); // scope 1 at $DIR/inline-cycle.rs:54:7: 54:8
+ StorageDead(_3); // scope 1 at $DIR/inline-cycle.rs:54:8: 54:9
Expand Down
18 changes: 16 additions & 2 deletions src/test/mir-opt/inline/inline_cycle_generic.main.Inline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@
fn main() -> () {
let mut _0: (); // return place in scope 0 at $DIR/inline-cycle-generic.rs:8:11: 8:11
let _1: (); // in scope 0 at $DIR/inline-cycle-generic.rs:9:5: 9:24
+ scope 1 (inlined <C as Call>::call) { // at $DIR/inline-cycle-generic.rs:9:5: 9:24
+ scope 2 (inlined <B<A> as Call>::call) { // at $DIR/inline-cycle-generic.rs:38:9: 38:31
+ scope 3 (inlined <A as Call>::call) { // at $DIR/inline-cycle-generic.rs:31:9: 31:28
+ scope 4 (inlined <B<C> as Call>::call) { // at $DIR/inline-cycle-generic.rs:23:9: 23:31
+ }
+ }
+ }
+ }

bb0: {
StorageLive(_1); // scope 0 at $DIR/inline-cycle-generic.rs:9:5: 9:24
_1 = <C as Call>::call() -> bb1; // scope 0 at $DIR/inline-cycle-generic.rs:9:5: 9:24
- _1 = <C as Call>::call() -> bb1; // scope 0 at $DIR/inline-cycle-generic.rs:9:5: 9:24
+ _1 = <C as Call>::call() -> bb1; // scope 4 at $DIR/inline-cycle-generic.rs:31:9: 31:28
// mir::Constant
// + span: $DIR/inline-cycle-generic.rs:9:5: 9:22
- // + span: $DIR/inline-cycle-generic.rs:9:5: 9:22
+ // + span: $DIR/inline-cycle-generic.rs:31:9: 31:26
// + literal: Const { ty: fn() {<C as Call>::call}, val: Value(Scalar(<ZST>)) }
}

bb1: {
StorageDead(_1); // scope 0 at $DIR/inline-cycle-generic.rs:9:24: 9:25
_0 = const (); // scope 0 at $DIR/inline-cycle-generic.rs:8:11: 10:2
return; // scope 0 at $DIR/inline-cycle-generic.rs:10:2: 10:2
+ }
+
+ bb2 (cleanup): {
+ resume; // scope 0 at $DIR/inline-cycle-generic.rs:8:1: 10:2
}
}

0 comments on commit 114c928

Please sign in to comment.