forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptRemarksParser.cpp
367 lines (309 loc) · 12 KB
/
OptRemarksParser.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//===- OptRemarksParser.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file provides utility methods used by clients that want to use the
// parser for optimization remarks in LLVM.
//
//===----------------------------------------------------------------------===//
#include "llvm-c/OptRemarks.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/YAMLTraits.h"
using namespace llvm;
namespace {
struct RemarkParser {
/// Source manager for better error messages.
SourceMgr SM;
/// Stream for yaml parsing.
yaml::Stream Stream;
/// Storage for the error stream.
std::string ErrorString;
/// The error stream.
raw_string_ostream ErrorStream;
/// Iterator in the YAML stream.
yaml::document_iterator DI;
/// The parsed remark (if any).
Optional<LLVMOptRemarkEntry> LastRemark;
/// Temporary parsing buffer for the arguments.
SmallVector<LLVMOptRemarkArg, 8> TmpArgs;
/// The state used by the parser to parse a remark entry. Invalidated with
/// every call to `parseYAMLElement`.
struct ParseState {
/// Temporary parsing buffer for the arguments.
SmallVectorImpl<LLVMOptRemarkArg> *Args;
StringRef Type;
StringRef Pass;
StringRef Name;
StringRef Function;
/// Optional.
Optional<StringRef> File;
Optional<unsigned> Line;
Optional<unsigned> Column;
Optional<unsigned> Hotness;
ParseState(SmallVectorImpl<LLVMOptRemarkArg> &Args) : Args(&Args) {}
/// Use Args only as a **temporary** buffer.
~ParseState() { Args->clear(); }
};
ParseState State;
/// Set to `true` if we had any errors during parsing.
bool HadAnyErrors = false;
RemarkParser(StringRef Buf)
: SM(), Stream(Buf, SM), ErrorString(), ErrorStream(ErrorString),
DI(Stream.begin()), LastRemark(), TmpArgs(), State(TmpArgs) {
SM.setDiagHandler(RemarkParser::HandleDiagnostic, this);
}
/// Parse a YAML element.
Error parseYAMLElement(yaml::Document &Remark);
private:
/// Parse one key to a string.
/// otherwise.
Error parseKey(StringRef &Result, yaml::KeyValueNode &Node);
/// Parse one value to a string.
Error parseValue(StringRef &Result, yaml::KeyValueNode &Node);
/// Parse one value to an unsigned.
Error parseValue(Optional<unsigned> &Result, yaml::KeyValueNode &Node);
/// Parse a debug location.
Error parseDebugLoc(Optional<StringRef> &File, Optional<unsigned> &Line,
Optional<unsigned> &Column, yaml::KeyValueNode &Node);
/// Parse an argument.
Error parseArg(SmallVectorImpl<LLVMOptRemarkArg> &TmpArgs, yaml::Node &Node);
/// Handle a diagnostic from the YAML stream. Records the error in the
/// RemarkParser class.
static void HandleDiagnostic(const SMDiagnostic &Diag, void *Ctx) {
assert(Ctx && "Expected non-null Ctx in diagnostic handler.");
auto *Parser = static_cast<RemarkParser *>(Ctx);
Diag.print(/*ProgName=*/nullptr, Parser->ErrorStream, /*ShowColors*/ false,
/*ShowKindLabels*/ true);
}
};
class ParseError : public ErrorInfo<ParseError> {
public:
static char ID;
ParseError(StringRef Message, yaml::Node &Node)
: Message(Message), Node(Node) {}
void log(raw_ostream &OS) const override { OS << Message; }
std::error_code convertToErrorCode() const override {
return inconvertibleErrorCode();
}
StringRef getMessage() const { return Message; }
yaml::Node &getNode() const { return Node; }
private:
StringRef Message; // No need to hold a full copy of the buffer.
yaml::Node &Node;
};
char ParseError::ID = 0;
static LLVMOptRemarkStringRef toOptRemarkStr(StringRef Str) {
return {Str.data(), static_cast<uint32_t>(Str.size())};
}
Error RemarkParser::parseKey(StringRef &Result, yaml::KeyValueNode &Node) {
auto *Key = dyn_cast<yaml::ScalarNode>(Node.getKey());
if (!Key)
return make_error<ParseError>("key is not a string.", Node);
Result = Key->getRawValue();
return Error::success();
}
Error RemarkParser::parseValue(StringRef &Result, yaml::KeyValueNode &Node) {
auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
if (!Value)
return make_error<ParseError>("expected a value of scalar type.", Node);
Result = Value->getRawValue();
if (Result.front() == '\'')
Result = Result.drop_front();
if (Result.back() == '\'')
Result = Result.drop_back();
return Error::success();
}
Error RemarkParser::parseValue(Optional<unsigned> &Result,
yaml::KeyValueNode &Node) {
SmallVector<char, 4> Tmp;
auto *Value = dyn_cast<yaml::ScalarNode>(Node.getValue());
if (!Value)
return make_error<ParseError>("expected a value of scalar type.", Node);
unsigned UnsignedValue = 0;
if (Value->getValue(Tmp).getAsInteger(10, UnsignedValue))
return make_error<ParseError>("expected a value of integer type.", *Value);
Result = UnsignedValue;
return Error::success();
}
Error RemarkParser::parseDebugLoc(Optional<StringRef> &File,
Optional<unsigned> &Line,
Optional<unsigned> &Column,
yaml::KeyValueNode &Node) {
auto *DebugLoc = dyn_cast<yaml::MappingNode>(Node.getValue());
if (!DebugLoc)
return make_error<ParseError>("expected a value of mapping type.", Node);
for (yaml::KeyValueNode &DLNode : *DebugLoc) {
StringRef KeyName;
if (Error E = parseKey(KeyName, DLNode))
return E;
if (KeyName == "File") {
File = StringRef(); // Set the optional to contain a default constructed
// value, to be passed to the parsing function.
if (Error E = parseValue(*File, DLNode))
return E;
} else if (KeyName == "Column") {
if (Error E = parseValue(Column, DLNode))
return E;
} else if (KeyName == "Line") {
if (Error E = parseValue(Line, DLNode))
return E;
} else {
return make_error<ParseError>("unknown entry in DebugLoc map.", DLNode);
}
}
// If any of the debug loc fields is missing, return an error.
if (!File || !Line || !Column)
return make_error<ParseError>("DebugLoc node incomplete.", Node);
return Error::success();
}
Error RemarkParser::parseArg(SmallVectorImpl<LLVMOptRemarkArg> &Args,
yaml::Node &Node) {
auto *ArgMap = dyn_cast<yaml::MappingNode>(&Node);
if (!ArgMap)
return make_error<ParseError>("expected a value of mapping type.", Node);
StringRef ValueStr;
StringRef KeyStr;
Optional<StringRef> File;
Optional<unsigned> Line;
Optional<unsigned> Column;
for (yaml::KeyValueNode &ArgEntry : *ArgMap) {
StringRef KeyName;
if (Error E = parseKey(KeyName, ArgEntry))
return E;
// Try to parse debug locs.
if (KeyName == "DebugLoc") {
// Can't have multiple DebugLoc entries per argument.
if (File || Line || Column)
return make_error<ParseError>(
"only one DebugLoc entry is allowed per argument.", ArgEntry);
if (Error E = parseDebugLoc(File, Line, Column, ArgEntry))
return E;
continue;
}
// If we already have a string, error out.
if (!ValueStr.empty())
return make_error<ParseError>(
"only one string entry is allowed per argument.", ArgEntry);
// Try to parse a string.
if (Error E = parseValue(ValueStr, ArgEntry))
return E;
// Keep the key from the string.
KeyStr = KeyName;
}
if (KeyStr.empty())
return make_error<ParseError>("argument key is missing.", *ArgMap);
if (ValueStr.empty())
return make_error<ParseError>("argument value is missing.", *ArgMap);
Args.push_back(LLVMOptRemarkArg{
toOptRemarkStr(KeyStr), toOptRemarkStr(ValueStr),
LLVMOptRemarkDebugLoc{toOptRemarkStr(File.getValueOr(StringRef())),
Line.getValueOr(0), Column.getValueOr(0)}});
return Error::success();
}
Error RemarkParser::parseYAMLElement(yaml::Document &Remark) {
// Parsing a new remark, clear the previous one.
LastRemark = None;
State = ParseState(TmpArgs);
auto *Root = dyn_cast<yaml::MappingNode>(Remark.getRoot());
if (!Root)
return make_error<ParseError>("document root is not of mapping type.",
*Remark.getRoot());
State.Type = Root->getRawTag();
for (yaml::KeyValueNode &RemarkField : *Root) {
StringRef KeyName;
if (Error E = parseKey(KeyName, RemarkField))
return E;
if (KeyName == "Pass") {
if (Error E = parseValue(State.Pass, RemarkField))
return E;
} else if (KeyName == "Name") {
if (Error E = parseValue(State.Name, RemarkField))
return E;
} else if (KeyName == "Function") {
if (Error E = parseValue(State.Function, RemarkField))
return E;
} else if (KeyName == "Hotness") {
if (Error E = parseValue(State.Hotness, RemarkField))
return E;
} else if (KeyName == "DebugLoc") {
if (Error E =
parseDebugLoc(State.File, State.Line, State.Column, RemarkField))
return E;
} else if (KeyName == "Args") {
auto *Args = dyn_cast<yaml::SequenceNode>(RemarkField.getValue());
if (!Args)
return make_error<ParseError>("wrong value type for key.", RemarkField);
for (yaml::Node &Arg : *Args)
if (Error E = parseArg(*State.Args, Arg))
return E;
} else {
return make_error<ParseError>("unknown key.", RemarkField);
}
}
// If the YAML parsing failed, don't even continue parsing. We might
// encounter malformed YAML.
if (Stream.failed())
return make_error<ParseError>("YAML parsing failed.", *Remark.getRoot());
// Check if any of the mandatory fields are missing.
if (State.Type.empty() || State.Pass.empty() || State.Name.empty() ||
State.Function.empty())
return make_error<ParseError>("Type, Pass, Name or Function missing.",
*Remark.getRoot());
LastRemark = LLVMOptRemarkEntry{
toOptRemarkStr(State.Type),
toOptRemarkStr(State.Pass),
toOptRemarkStr(State.Name),
toOptRemarkStr(State.Function),
LLVMOptRemarkDebugLoc{toOptRemarkStr(State.File.getValueOr(StringRef())),
State.Line.getValueOr(0),
State.Column.getValueOr(0)},
State.Hotness.getValueOr(0),
static_cast<uint32_t>(State.Args->size()),
State.Args->data()};
return Error::success();
}
} // namespace
// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(RemarkParser, LLVMOptRemarkParserRef)
extern "C" LLVMOptRemarkParserRef LLVMOptRemarkParserCreate(const void *Buf,
uint64_t Size) {
return wrap(
new RemarkParser(StringRef(static_cast<const char *>(Buf), Size)));
}
extern "C" LLVMOptRemarkEntry *
LLVMOptRemarkParserGetNext(LLVMOptRemarkParserRef Parser) {
RemarkParser &TheParser = *unwrap(Parser);
// Check for EOF.
if (TheParser.HadAnyErrors || TheParser.DI == TheParser.Stream.end())
return nullptr;
// Try to parse an entry.
if (Error E = TheParser.parseYAMLElement(*TheParser.DI)) {
handleAllErrors(std::move(E), [&](const ParseError &PE) {
TheParser.Stream.printError(&PE.getNode(),
Twine(PE.getMessage()) + Twine('\n'));
TheParser.HadAnyErrors = true;
});
return nullptr;
}
// Move on.
++TheParser.DI;
// Return the just-parsed remark.
if (Optional<LLVMOptRemarkEntry> &Entry = TheParser.LastRemark)
return &*Entry;
return nullptr;
}
extern "C" LLVMBool LLVMOptRemarkParserHasError(LLVMOptRemarkParserRef Parser) {
return unwrap(Parser)->HadAnyErrors;
}
extern "C" const char *
LLVMOptRemarkParserGetErrorMessage(LLVMOptRemarkParserRef Parser) {
return unwrap(Parser)->ErrorStream.str().c_str();
}
extern "C" void LLVMOptRemarkParserDispose(LLVMOptRemarkParserRef Parser) {
delete unwrap(Parser);
}