LLVM  16.0.0git
RemarkLinker.h
Go to the documentation of this file.
1 //===-- llvm/Remarks/RemarkLinker.h -----------------------------*- C++/-*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides an interface to link together multiple remark files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_REMARKS_REMARKLINKER_H
14 #define LLVM_REMARKS_REMARKLINKER_H
15 
16 #include "llvm/Remarks/Remark.h"
19 #include "llvm/Support/Error.h"
20 #include <memory>
21 #include <set>
22 
23 namespace llvm {
24 
25 namespace object {
26 class ObjectFile;
27 }
28 
29 namespace remarks {
30 
31 struct RemarkLinker {
32 private:
33  /// Compare through the pointers.
34  struct RemarkPtrCompare {
35  bool operator()(const std::unique_ptr<Remark> &LHS,
36  const std::unique_ptr<Remark> &RHS) const {
37  assert(LHS && RHS && "Invalid pointers to compare.");
38  return *LHS < *RHS;
39  };
40  };
41 
42  /// The main string table for the remarks.
43  /// Note: all remarks should use the strings from this string table to avoid
44  /// dangling references.
45  StringTable StrTab;
46 
47  /// A set holding unique remarks.
48  /// FIXME: std::set is probably not the most appropriate data structure here.
49  /// Due to the limitation of having a move-only key, there isn't another
50  /// obvious choice for now.
51  std::set<std::unique_ptr<Remark>, RemarkPtrCompare> Remarks;
52 
53  /// A path to append before the external file path found in remark metadata.
54  Optional<std::string> PrependPath;
55 
56  /// Keep this remark. If it's already in the set, discard it.
57  Remark &keep(std::unique_ptr<Remark> Remark);
58 
59 public:
60  /// Set a path to prepend to the external file path.
61  void setExternalFilePrependPath(StringRef PrependPath);
62 
63  /// Link the remarks found in \p Buffer.
64  /// If \p RemarkFormat is not provided, try to deduce it from the metadata in
65  /// \p Buffer.
66  /// \p Buffer can be either a standalone remark container or just
67  /// metadata. This takes care of uniquing and merging the remarks.
68  Error link(StringRef Buffer, Optional<Format> RemarkFormat = None);
69 
70  /// Link the remarks found in \p Obj by looking for the right section and
71  /// calling the method above.
72  Error link(const object::ObjectFile &Obj,
73  Optional<Format> RemarkFormat = None);
74 
75  /// Serialize the linked remarks to the stream \p OS, using the format \p
76  /// RemarkFormat.
77  /// This clears internal state such as the string table.
78  /// Note: this implies that the serialization mode is standalone.
80 
81  /// Check whether there are any remarks linked.
82  bool empty() const { return Remarks.empty(); }
83 
84  /// Return a collection of the linked unique remarks to iterate on.
85  /// Ex:
86  /// for (const Remark &R : RL.remarks() { [...] }
87  using iterator = pointee_iterator<decltype(Remarks)::const_iterator>;
88 
90  return {Remarks.begin(), Remarks.end()};
91  }
92 };
93 
94 /// Returns a buffer with the contents of the remarks section depending on the
95 /// format of the file. If the section doesn't exist, this returns an empty
96 /// optional.
99 
100 } // end namespace remarks
101 } // end namespace llvm
102 
103 #endif // LLVM_REMARKS_REMARKLINKER_H
const_iterator
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::remarks::StringTable
The string table used for serializing remarks.
Definition: RemarkStringTable.h:36
llvm::remarks::RemarkLinker::setExternalFilePrependPath
void setExternalFilePrependPath(StringRef PrependPath)
Set a path to prepend to the external file path.
Definition: RemarkLinker.cpp:64
RemarkStringTable.h
llvm::remarks::RemarkLinker::serialize
Error serialize(raw_ostream &OS, Format RemarksFormat) const
Serialize the linked remarks to the stream OS, using the format RemarkFormat.
Definition: RemarkLinker.cpp:118
Error.h
llvm::Optional< std::string >
llvm::remarks::RemarkLinker::remarks
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:89
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
LHS
Value * LHS
Definition: X86PartialReduction.cpp:75
llvm::remarks::getRemarksSectionContents
Expected< Optional< StringRef > > getRemarksSectionContents(const object::ObjectFile &Obj)
Returns a buffer with the contents of the remarks section depending on the format of the file.
Definition: RemarkLinker.cpp:38
llvm::remarks::RemarkLinker
Definition: RemarkLinker.h:31
Remark.h
remarks
annotation remarks
Definition: AnnotationRemarks.cpp:114
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::remarks::Remark
A remark type used for both emission and parsing.
Definition: Remark.h:67
llvm::None
const NoneType None
Definition: None.h:24
RemarkFormat.h
llvm::RemarksFormat
cl::opt< std::string > RemarksFormat("lto-pass-remarks-format", cl::desc("The format used for serializing remarks (default: YAML)"), cl::value_desc("format"), cl::init("yaml"))
Definition: ThinLTOCodeGenerator.cpp:83
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
Remarks
annotation Annotation Remarks
Definition: AnnotationRemarks.cpp:115
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::remarks::RemarkLinker::empty
bool empty() const
Check whether there are any remarks linked.
Definition: RemarkLinker.h:82
llvm::codeview::CodeViewContainer::ObjectFile
@ ObjectFile
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::object::ObjectFile
This class is the base class for all object file types.
Definition: ObjectFile.h:228
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::remarks::RemarkLinker::link
Error link(StringRef Buffer, Optional< Format > RemarkFormat=None)
Link the remarks found in Buffer.
Definition: RemarkLinker.cpp:71
llvm::pointee_iterator
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:320
llvm::remarks::Format
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25