Skip to content

Commit b53ccf2

Browse files
committed
Auto merge of #128118 - saethlin:closures-can-be-shared, r=<try>
Don't add inlinehint to big closures In an unoptimized incremental build we want as many items as possible to be lowered as GloballyShared, but right now monomorphization makes whole classes of instances all LocalCopy without inspecting the instance's MIR or the session settings. Right now I'm experimenting to see what kind of an impact this can have. I think there's a pretty core design flaw currently because we make monomorphization decisions about Instances, but `cross_crate_inlinable` takes a DefId. r? `@ghost`
2 parents 8bfcae7 + c47d3f4 commit b53ccf2

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,12 +348,14 @@ pub fn from_fn_attrs<'ll, 'tcx>(
348348
OptimizeAttr::Speed => {}
349349
}
350350

351-
let inline =
352-
if codegen_fn_attrs.inline == InlineAttr::None && instance.def.requires_inline(cx.tcx) {
353-
InlineAttr::Hint
354-
} else {
355-
codegen_fn_attrs.inline
356-
};
351+
let inline = if codegen_fn_attrs.inline == InlineAttr::None
352+
&& (cx.tcx.cross_crate_inlinable(instance.def_id())
353+
|| matches!(instance.def, rustc_middle::ty::InstanceKind::CloneShim(..)))
354+
{
355+
InlineAttr::Hint
356+
} else {
357+
codegen_fn_attrs.inline
358+
};
357359
to_add.extend(inline_attr(cx, inline));
358360

359361
// The `uwtable` attribute according to LLVM is:

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,7 @@ impl<'tcx> InstanceKind<'tcx> {
300300
ty::InstanceKind::ThreadLocalShim(_) => return false,
301301
_ => return true,
302302
};
303-
matches!(
304-
tcx.def_key(def_id).disambiguated_data.data,
305-
DefPathData::Ctor | DefPathData::Closure
306-
)
303+
matches!(tcx.def_key(def_id).disambiguated_data.data, DefPathData::Ctor)
307304
}
308305

309306
/// Returns `true` if the machine code for this instance is instantiated in

compiler/rustc_mir_transform/src/cross_crate_inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
2525

2626
// This just reproduces the logic from Instance::requires_inline.
2727
match tcx.def_kind(def_id) {
28-
DefKind::Ctor(..) | DefKind::Closure => return true,
29-
DefKind::Fn | DefKind::AssocFn => {}
28+
DefKind::Ctor(..) => return true,
29+
DefKind::Fn | DefKind::AssocFn | DefKind::Closure => {}
3030
_ => return false,
3131
}
3232

tests/coverage/closure.cov-map

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,16 @@ Number of file 0 mappings: 24
3131
= (c0 - c1)
3232
- Code(Counter(0)) at (prev + 1, 5) to (start + 3, 2)
3333

34-
Function name: closure::main::{closure#0}
35-
Raw bytes (26): 0x[01, 01, 01, 01, 05, 04, 01, 28, 05, 02, 14, 05, 02, 15, 02, 0a, 02, 02, 0a, 00, 0b, 01, 01, 09, 01, 06]
34+
Function name: closure::main::{closure#0} (unused)
35+
Raw bytes (24): 0x[01, 01, 00, 04, 00, 28, 05, 02, 14, 00, 02, 15, 02, 0a, 00, 02, 0a, 00, 0b, 00, 01, 09, 01, 06]
3636
Number of files: 1
3737
- file 0 => global file 1
38-
Number of expressions: 1
39-
- expression 0 operands: lhs = Counter(0), rhs = Counter(1)
38+
Number of expressions: 0
4039
Number of file 0 mappings: 4
41-
- Code(Counter(0)) at (prev + 40, 5) to (start + 2, 20)
42-
- Code(Counter(1)) at (prev + 2, 21) to (start + 2, 10)
43-
- Code(Expression(0, Sub)) at (prev + 2, 10) to (start + 0, 11)
44-
= (c0 - c1)
45-
- Code(Counter(0)) at (prev + 1, 9) to (start + 1, 6)
40+
- Code(Zero) at (prev + 40, 5) to (start + 2, 20)
41+
- Code(Zero) at (prev + 2, 21) to (start + 2, 10)
42+
- Code(Zero) at (prev + 2, 10) to (start + 0, 11)
43+
- Code(Zero) at (prev + 1, 9) to (start + 1, 6)
4644

4745
Function name: closure::main::{closure#10} (unused)
4846
Raw bytes (10): 0x[01, 01, 00, 01, 00, 9b, 01, 07, 00, 21]

0 commit comments

Comments
 (0)