about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-04-05 22:58:55 +0200
committerGitHub <noreply@github.com>2022-04-05 22:58:55 +0200
commitc5e7e952925be74fc7dd6a2fac8e16df9e2044f6 (patch)
tree74472c12a12821573235732c3cbbf97bd9fdbcd2 /compiler
parentd2e1e6dc759ef6c56e68382a148ed7181ab3a18f (diff)
parent9ac8d2fe4ed0f5312bcd74c05f441d23536c1a8f (diff)
downloadrust-c5e7e952925be74fc7dd6a2fac8e16df9e2044f6.tar.gz
rust-c5e7e952925be74fc7dd6a2fac8e16df9e2044f6.zip
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
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_expand/src/base.rs6
-rw-r--r--compiler/rustc_expand/src/proc_macro.rs28
2 files changed, 24 insertions, 10 deletions
diff --git a/compiler/rustc_expand/src/base.rs b/compiler/rustc_expand/src/base.rs
index 556b2c6fbf3..06a90ab05ac 100644
--- a/compiler/rustc_expand/src/base.rs
+++ b/compiler/rustc_expand/src/base.rs
@@ -1047,6 +1047,12 @@ impl<'a> ExtCtxt<'a> {
         self.current_expansion.id.expn_data().call_site
     }
 
+    /// Returns the current expansion kind's description.
+    pub(crate) fn expansion_descr(&self) -> String {
+        let expn_data = self.current_expansion.id.expn_data();
+        expn_data.kind.descr()
+    }
+
     /// Equivalent of `Span::def_site` from the proc macro API,
     /// except that the location is taken from the span passed as an argument.
     pub fn with_def_site_ctxt(&self, span: Span) -> Span {
diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs
index a5afb7aa4fa..b4bae8ce5fb 100644
--- a/compiler/rustc_expand/src/proc_macro.rs
+++ b/compiler/rustc_expand/src/proc_macro.rs
@@ -24,6 +24,8 @@ impl base::ProcMacro for BangProcMacro {
         span: Span,
         input: TokenStream,
     ) -> Result<TokenStream, ErrorGuaranteed> {
+        let _timer =
+            ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
         let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
         let server = proc_macro_server::Rustc::new(ecx);
         self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace).map_err(|e| {
@@ -48,6 +50,8 @@ impl base::AttrProcMacro for AttrProcMacro {
         annotation: TokenStream,
         annotated: TokenStream,
     ) -> Result<TokenStream, ErrorGuaranteed> {
+        let _timer =
+            ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
         let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
         let server = proc_macro_server::Rustc::new(ecx);
         self.client
@@ -97,17 +101,21 @@ impl MultiItemModifier for ProcMacroDerive {
             nt_to_tokenstream(&item, &ecx.sess.parse_sess, CanSynthesizeMissingTokens::No)
         };
 
-        let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
-        let server = proc_macro_server::Rustc::new(ecx);
-        let stream = match self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace) {
-            Ok(stream) => stream,
-            Err(e) => {
-                let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
-                if let Some(s) = e.as_str() {
-                    err.help(&format!("message: {}", s));
+        let stream = {
+            let _timer =
+                ecx.sess.prof.generic_activity_with_arg("expand_proc_macro", ecx.expansion_descr());
+            let proc_macro_backtrace = ecx.ecfg.proc_macro_backtrace;
+            let server = proc_macro_server::Rustc::new(ecx);
+            match self.client.run(&EXEC_STRATEGY, server, input, proc_macro_backtrace) {
+                Ok(stream) => stream,
+                Err(e) => {
+                    let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
+                    if let Some(s) = e.as_str() {
+                        err.help(&format!("message: {}", s));
+                    }
+                    err.emit();
+                    return ExpandResult::Ready(vec![]);
                 }
-                err.emit();
-                return ExpandResult::Ready(vec![]);
             }
         };