forked from mikael-s-persson/templight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templight_clang_patch.diff
209 lines (191 loc) · 7.57 KB
/
templight_clang_patch.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
diff --git a/include/clang/Driver/CC1Options.td b/include/clang/Driver/CC1Options.td
index 03b43ddd..f22af830 100644
--- a/include/clang/Driver/CC1Options.td
+++ b/include/clang/Driver/CC1Options.td
@@ -501,6 +501,8 @@ def ftest_module_file_extension_EQ :
"The argument is parsed as blockname:major:minor:hashed:user info">;
def fconcepts_ts : Flag<["-"], "fconcepts-ts">,
HelpText<"Enable C++ Extensions for Concepts.">;
+def templight_profile : Flag<["-"], "templight-profile">,
+ HelpText<"Add timestamps to the templight-dump output">;
let Group = Action_Group in {
diff --git a/include/clang/Frontend/FrontendOptions.h b/include/clang/Frontend/FrontendOptions.h
index 768957ac..e1d4908c 100644
--- a/include/clang/Frontend/FrontendOptions.h
+++ b/include/clang/Frontend/FrontendOptions.h
@@ -301,6 +301,10 @@ public:
/// Whether timestamps should be written to the produced PCH file.
unsigned IncludeTimestamps : 1;
+
+ /// Whether to add additional
+ /// information to Templight dumps.
+ unsigned TemplightProfile : 1;
CodeCompleteOptions CodeCompleteOpts;
@@ -447,7 +451,8 @@ public:
SkipFunctionBodies(false), UseGlobalModuleIndex(true),
GenerateGlobalModuleIndex(true), ASTDumpDecls(false),
ASTDumpLookups(false), BuildingImplicitModule(false),
- ModulesEmbedAllFiles(false), IncludeTimestamps(true) {}
+ ModulesEmbedAllFiles(false), IncludeTimestamps(true),
+ TemplightProfile(false) {}
/// getInputKindForExtension - Return the appropriate input kind for a file
/// extension. For example, "c" would return InputKind::C.
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index 67e15b41..19ef1194 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -1509,6 +1509,7 @@ static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args,
Opts.ModulesEmbedFiles = Args.getAllArgValues(OPT_fmodules_embed_file_EQ);
Opts.ModulesEmbedAllFiles = Args.hasArg(OPT_fmodules_embed_all_files);
Opts.IncludeTimestamps = !Args.hasArg(OPT_fno_pch_timestamp);
+ Opts.TemplightProfile = Args.hasArg(OPT_templight_profile);
Opts.CodeCompleteOpts.IncludeMacros
= Args.hasArg(OPT_code_completion_macros);
diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp
index 8cb6a042..5cb70482 100644
--- a/lib/Frontend/FrontendActions.cpp
+++ b/lib/Frontend/FrontendActions.cpp
@@ -26,6 +26,7 @@
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/YAMLTraits.h"
+#include <chrono>
#include <memory>
#include <system_error>
@@ -288,19 +289,14 @@ struct TemplightEntry {
std::string Event;
std::string DefinitionLocation;
std::string PointOfInstantiation;
+ Optional<std::chrono::high_resolution_clock::rep> TimeStamp;
};
} // namespace
namespace llvm {
namespace yaml {
template <> struct MappingTraits<TemplightEntry> {
- static void mapping(IO &io, TemplightEntry &fields) {
- io.mapRequired("name", fields.Name);
- io.mapRequired("kind", fields.Kind);
- io.mapRequired("event", fields.Event);
- io.mapRequired("orig", fields.DefinitionLocation);
- io.mapRequired("poi", fields.PointOfInstantiation);
- }
+ static void mapping(IO &io, TemplightEntry &fields);
};
} // namespace yaml
} // namespace llvm
@@ -312,19 +308,46 @@ class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
public:
void initialize(const Sema &) override {}
- void finalize(const Sema &) override {}
+ void finalize(const Sema &) override {
+ if(isProfilingEnabled())
+ for(auto &Entry : *TemplightEntries)
+ displayTemplightEntry(llvm::outs(), Entry);
+ }
void atTemplateBegin(const Sema &TheSema,
const CodeSynthesisContext &Inst) override {
- displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
+ TemplightEntry Entry = getTemplightEntry<true>(TheSema, Inst);
+
+ if(isProfilingEnabled())
+ TemplightEntries->push_back(std::move(Entry));
+ else
+ displayTemplightEntry(llvm::outs(), Entry);
}
void atTemplateEnd(const Sema &TheSema,
const CodeSynthesisContext &Inst) override {
- displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
+ TemplightEntry Entry = getTemplightEntry<false>(TheSema, Inst);
+
+ if(isProfilingEnabled())
+ TemplightEntries->push_back(std::move(Entry));
+ else
+ displayTemplightEntry(llvm::outs(), Entry);
+ }
+
+ void enableProfiling() {
+ TemplightEntries = std::vector<TemplightEntry>();
}
+
+ bool isProfilingEnabled() {
+ return TemplightEntries.hasValue();
+ }
+
+ static const std::chrono::time_point<std::chrono::high_resolution_clock> start;
private:
+
+ Optional<std::vector<TemplightEntry>> TemplightEntries = None;
+
static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
switch (Kind) {
case CodeSynthesisContext::TemplateInstantiation:
@@ -353,15 +376,12 @@ private:
return "";
}
- template <bool BeginInstantiation>
- static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
- const CodeSynthesisContext &Inst) {
+ void displayTemplightEntry(llvm::raw_ostream &Out,
+ TemplightEntry& Entry) {
std::string YAML;
{
llvm::raw_string_ostream OS(YAML);
llvm::yaml::Output YO(OS);
- TemplightEntry Entry =
- getTemplightEntry<BeginInstantiation>(TheSema, Inst);
llvm::yaml::EmptyContext Context;
llvm::yaml::yamlize(YO, Entry, true, Context);
}
@@ -369,9 +389,13 @@ private:
}
template <bool BeginInstantiation>
- static TemplightEntry getTemplightEntry(const Sema &TheSema,
+ TemplightEntry getTemplightEntry(const Sema &TheSema,
const CodeSynthesisContext &Inst) {
TemplightEntry Entry;
+ if (isProfilingEnabled()){
+ auto end = std::chrono::high_resolution_clock::now();
+ Entry.TimeStamp = std::chrono::nanoseconds(end-start).count();
+ }
Entry.Kind = toString(Inst.Kind);
Entry.Event = BeginInstantiation ? "Begin" : "End";
if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
@@ -394,8 +418,27 @@ private:
return Entry;
}
};
+
+const std::chrono::time_point<std::chrono::high_resolution_clock>
+ DefaultTemplateInstCallback::start = std::chrono::high_resolution_clock::now();
+
} // namespace
+namespace llvm {
+namespace yaml {
+void MappingTraits<TemplightEntry>::mapping(IO &io, TemplightEntry &fields)
+{
+ io.mapRequired("name", fields.Name);
+ io.mapRequired("kind", fields.Kind);
+ io.mapRequired("event", fields.Event);
+ io.mapRequired("orig", fields.DefinitionLocation);
+ io.mapRequired("poi", fields.PointOfInstantiation);
+ if(fields.TimeStamp)
+ io.mapRequired("stamp", fields.TimeStamp.getValue());
+}
+} // namespace yaml
+} // namespace llvm
+
std::unique_ptr<ASTConsumer>
TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
return llvm::make_unique<ASTConsumer>();
@@ -410,8 +453,11 @@ void TemplightDumpAction::ExecuteAction() {
// here so the source manager would be initialized.
EnsureSemaIsCreated(CI, *this);
- CI.getSema().TemplateInstCallbacks.push_back(
- llvm::make_unique<DefaultTemplateInstCallback>());
+ auto D = llvm::make_unique<DefaultTemplateInstCallback>();
+ if(CI.getFrontendOpts().TemplightProfile)
+ D->enableProfiling();
+
+ CI.getSema().TemplateInstCallbacks.push_back(std::move(D));
ASTFrontendAction::ExecuteAction();
}