25
25
#include " llvm/ProfileData/ProfileCommon.h"
26
26
#include " llvm/Support/Endian.h"
27
27
#include " llvm/Support/ErrorHandling.h"
28
- #include " llvm/Support/ErrorOr.h"
29
28
#include " llvm/Support/MD5.h"
30
29
#include " llvm/Support/MathExtras.h"
31
30
#include < cstdint>
@@ -204,20 +203,17 @@ StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
204
203
// / third field is the uncompressed strings; otherwise it is the
205
204
// / compressed string. When the string compression is off, the
206
205
// / second field will have value zero.
207
- std::error_code
208
- collectPGOFuncNameStrings (const std::vector<std::string> &NameStrs,
209
- bool doCompression, std::string &Result);
206
+ Error collectPGOFuncNameStrings (const std::vector<std::string> &NameStrs,
207
+ bool doCompression, std::string &Result);
210
208
// / Produce \c Result string with the same format described above. The input
211
209
// / is vector of PGO function name variables that are referenced.
212
- std::error_code
213
- collectPGOFuncNameStrings (const std::vector<GlobalVariable *> &NameVars,
214
- std::string &Result, bool doCompression = true );
210
+ Error collectPGOFuncNameStrings (const std::vector<GlobalVariable *> &NameVars,
211
+ std::string &Result, bool doCompression = true );
215
212
class InstrProfSymtab ;
216
213
// / \c NameStrings is a string composed of one of more sub-strings encoded in
217
214
// / the format described above. The substrings are seperated by 0 or more zero
218
215
// / bytes. This method decodes the string and populates the \c Symtab.
219
- std::error_code readPGOFuncNameStrings (StringRef NameStrings,
220
- InstrProfSymtab &Symtab);
216
+ Error readPGOFuncNameStrings (StringRef NameStrings, InstrProfSymtab &Symtab);
221
217
222
218
enum InstrProfValueKind : uint32_t {
223
219
#define VALUE_PROF_KIND (Enumerator, Value ) Enumerator = Value,
@@ -284,6 +280,39 @@ inline std::error_code make_error_code(instrprof_error E) {
284
280
return std::error_code (static_cast <int >(E), instrprof_category ());
285
281
}
286
282
283
+ class InstrProfError : public ErrorInfo <InstrProfError> {
284
+ public:
285
+ InstrProfError (instrprof_error Err) : Err(Err) {
286
+ assert (Err != instrprof_error::success && " Not an error" );
287
+ }
288
+
289
+ std::string message () const override ;
290
+
291
+ void log (raw_ostream &OS) const override { OS << message (); }
292
+
293
+ std::error_code convertToErrorCode () const override {
294
+ return make_error_code (Err);
295
+ }
296
+
297
+ instrprof_error get () const { return Err; }
298
+
299
+ // / Consume an Error and return the raw enum value contained within it. The
300
+ // / Error must either be a success value, or contain a single InstrProfError.
301
+ static instrprof_error take (Error E) {
302
+ auto Err = instrprof_error::success;
303
+ handleAllErrors (std::move (E), [&Err](const InstrProfError &IPE) {
304
+ assert (Err == instrprof_error::success && " Multiple errors encountered" );
305
+ Err = IPE.get ();
306
+ });
307
+ return Err;
308
+ }
309
+
310
+ static char ID;
311
+
312
+ private:
313
+ instrprof_error Err;
314
+ };
315
+
287
316
class SoftInstrProfErrors {
288
317
// / Count the number of soft instrprof_errors encountered and keep track of
289
318
// / the first such error for reporting purposes.
@@ -309,6 +338,11 @@ class SoftInstrProfErrors {
309
338
NumCountMismatches (0 ), NumCounterOverflows(0 ),
310
339
NumValueSiteCountMismatches(0 ) {}
311
340
341
+ ~SoftInstrProfErrors () {
342
+ assert (FirstError == instrprof_error::success &&
343
+ " Unchecked soft error encountered" );
344
+ }
345
+
312
346
// / Track a soft error (\p IE) and increment its associated counter.
313
347
void addError (instrprof_error IE);
314
348
@@ -326,8 +360,15 @@ class SoftInstrProfErrors {
326
360
return NumValueSiteCountMismatches;
327
361
}
328
362
329
- // / Return an error code for the first encountered error.
330
- std::error_code getError () const { return make_error_code (FirstError); }
363
+ // / Return the first encountered error and reset FirstError to a success
364
+ // / value.
365
+ Error takeError () {
366
+ if (FirstError == instrprof_error::success)
367
+ return Error::success ();
368
+ auto E = make_error<InstrProfError>(FirstError);
369
+ FirstError = instrprof_error::success;
370
+ return E;
371
+ }
331
372
};
332
373
333
374
namespace object {
@@ -372,14 +413,14 @@ class InstrProfSymtab {
372
413
// / only initialize the symtab with reference to the data and
373
414
// / the section base address. The decompression will be delayed
374
415
// / until before it is used. See also \c create(StringRef) method.
375
- std::error_code create (object::SectionRef &Section);
416
+ Error create (object::SectionRef &Section);
376
417
// / This interface is used by reader of CoverageMapping test
377
418
// / format.
378
- inline std::error_code create (StringRef D, uint64_t BaseAddr);
419
+ inline Error create (StringRef D, uint64_t BaseAddr);
379
420
// / \c NameStrings is a string composed of one of more sub-strings
380
421
// / encoded in the format described in \c collectPGOFuncNameStrings.
381
422
// / This method is a wrapper to \c readPGOFuncNameStrings method.
382
- inline std::error_code create (StringRef NameStrings);
423
+ inline Error create (StringRef NameStrings);
383
424
// / A wrapper interface to populate the PGO symtab with functions
384
425
// / decls from module \c M. This interface is used by transformation
385
426
// / passes such as indirect function call promotion. Variable \c InLTO
@@ -424,13 +465,13 @@ class InstrProfSymtab {
424
465
inline StringRef getNameData () const { return Data; }
425
466
};
426
467
427
- std::error_code InstrProfSymtab::create (StringRef D, uint64_t BaseAddr) {
468
+ Error InstrProfSymtab::create (StringRef D, uint64_t BaseAddr) {
428
469
Data = D;
429
470
Address = BaseAddr;
430
- return std::error_code ();
471
+ return Error::success ();
431
472
}
432
473
433
- std::error_code InstrProfSymtab::create (StringRef NameStrings) {
474
+ Error InstrProfSymtab::create (StringRef NameStrings) {
434
475
return readPGOFuncNameStrings (NameStrings, *this );
435
476
}
436
477
@@ -572,7 +613,7 @@ struct InstrProfRecord {
572
613
}
573
614
574
615
// / Get the error contained within the record's soft error counter.
575
- std::error_code getError () const { return SIPE.getError (); }
616
+ Error takeError () { return SIPE.takeError (); }
576
617
577
618
private:
578
619
std::vector<InstrProfValueSiteRecord> IndirectCallSites;
@@ -869,9 +910,4 @@ struct Header {
869
910
870
911
} // end namespace llvm
871
912
872
- namespace std {
873
- template <>
874
- struct is_error_code_enum <llvm::instrprof_error> : std::true_type {};
875
- }
876
-
877
913
#endif // LLVM_PROFILEDATA_INSTRPROF_H
0 commit comments