Skip to content

Commit c5e7e95

Browse files
authored
Rollup merge of #95473 - lqd:macro-expansion, r=petrochenkov
track individual proc-macro expansions in the self-profiler As described in [this zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Macro.20expansion.20performance.20on.20complex.20macros/near/275063190), users don't currently have a lot of information to diagnose macro expansion performance issues. That comment suggests using the macro names to add further timing information. This PR starts to do this for proc-macros which have the same issue, and performance problems happening in the wild in [this other zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/247081-t-compiler.2Fperformance/topic/Identifying.20proc-macro.20slowdowns) could be helped by such information. It uses the available proc-macro name to track their individual expansions with self-profiling events. r? `@Aaron1011` who mentioned this idea originally
2 parents d2e1e6d + 9ac8d2f commit c5e7e95

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

compiler/rustc_expand/src/base.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,12 @@ impl<'a> ExtCtxt<'a> {
10471047
self.current_expansion.id.expn_data().call_site
10481048
}
10491049

1050+
/// Returns the current expansion kind's description.
1051+
pub(crate) fn expansion_descr(&self) -> String {
1052+
let expn_data = self.current_expansion.id.expn_data();
1053+
expn_data.kind.descr()
1054+
}
1055+
10501056
/// Equivalent of `Span::def_site` from the proc macro API,
10511057
/// except that the location is taken from the span passed as an argument.
10521058
pub fn with_def_site_ctxt(&self, span: Span) -> Span {

compiler/rustc_expand/src/proc_macro.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ impl base::ProcMacro for BangProcMacro {
2424
span: Span,
2525
input: TokenStream,
2626
) -> Result<TokenStream, ErrorGuaranteed> {
27+
let _timer =
28+
ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
2729
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
2830
let server = proc_macro_server::Rustc::new(ecx);
2931
self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace).map_err(|e| {
@@ -48,6 +50,8 @@ impl base::AttrProcMacro for AttrProcMacro {
4850
annotation: TokenStream,
4951
annotated: TokenStream,
5052
) -> Result<TokenStream, ErrorGuaranteed> {
53+
let _timer =
54+
ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
5155
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
5256
let server = proc_macro_server::Rustc::new(ecx);
5357
self.client
@@ -97,17 +101,21 @@ impl MultiItemModifier for ProcMacroDerive {
97101
nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::No)
98102
};
99103

100-
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
101-
let server = proc_macro_server::Rustc::new(ecx);
102-
let stream = match self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace) {
103-
Ok(stream) => stream,
104-
Err(e) => {
105-
let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
106-
if let Some(s) = e.as_str() {
107-
err.help(&format!("message: {}", s));
104+
let stream = {
105+
let _timer =
106+
ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
107+
let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
108+
let server = proc_macro_server::Rustc::new(ecx);
109+
match self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace) {
110+
Ok(stream) => stream,
111+
Err(e) => {
112+
let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
113+
if let Some(s) = e.as_str() {
114+
err.help(&format!("message: {}", s));
115+
}
116+
err.emit();
117+
return ExpandResult::Ready(vec![]);
108118
}
109-
err.emit();
110-
return ExpandResult::Ready(vec![]);
111119
}
112120
};
113121

0 commit comments

Comments
 (0)