[PATCH] D147520: Fix a time-trace issue of incorrect header hierarchy when a header contains a template function for its last symbol.

Ying Yi via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 4 04:35:43 PDT 2023


MaggieYi added a comment.

Analysis the issue using the above simple example:

When clang parses the first line (`#include "1.h"`) in the `main.cpp`, a time section entry is created and put on the top of the time-trace stack. Assuming this time section entry is named `Source-1.h`, which has its name `Source` and detail `1.h`. Then, clang parses the template function `Zero` defined in the `1.h` header file. Clang will create a `TimeTraceScope` variable in which its name is `ParseTemplate` and its detail is `Zero`. A new time section entry named `ParseTemplate-Zero` is created and put on the top of the time-trace stack. `ParseTemplate-Zero` has its name `ParseTemplate` and detail `Zero`. Now, the top element of the time-trace stack is `ParseTemplate-Zero` and `Source-1.h` is under `ParseTemplate-Zero`.

Please note: since `ParseTemplate-Zero` is `TimeTraceScope` type variable. It should be popped out from the time-trace stack once the destructor of `TimeTraceScope` is called.

Since the template `Zero` is the last symbol defined in the `1.h` header, the `LexEndOfFile` is called, then `LexedFileChanged` is called. `LexedFileChanged` is invoked when the lexer hits the end of the current file. This either returns the EOF token or pops a level off the include stack and keeps going. In our case, it keeps going and notifies the client that we are in a new header file: `2.h`. The function of `timeTraceProfilerEnd` is called to pop the top element out from the time-trace stack and calculate the corresponding compilation time. `Source-1.h` is expected to pop out from the time-trace stack. However, since `ParseTemplate-Zero` is still alive and is on the top of the time-trace stack. `ParseTemplate-Zero` is popped out from the time trace stack instead of `Source-1.h`, which is wrong. Until now, the top of the time-trace stack is `Source-1.h`.

After that, a time section entry named `Source-2.h` is created and put on the top of the time-trace stack. Now, the top element of the time-trace stack is `Source-2.h`, and then `Source-1.h` is under `Source-2.h`. This results in incorrect header hierarchy in the time trace: the `2.h` shows up as an inclusion under `1.h`.

Currently, two available time profiling APIs cannot deal with this special case. I have modified `timeTraceProfilerEnd()` to handle the header file specially.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D147520/new/

https://reviews.llvm.org/D147520



More information about the cfe-commits mailing list