LLVM 19.0.0git
RemarkLinker.cpp
Go to the documentation of this file.
1//===- RemarkLinker.cpp ---------------------------------------------------===//
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 implementation of the remark linker.
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Error.h"
20#include <optional>
21
22using namespace llvm;
23using namespace llvm::remarks;
24
25namespace llvm {
26class raw_ostream;
27}
28
31 if (Obj.isMachO())
32 return StringRef("__remarks");
33 // ELF -> .remarks, but there is no ELF support at this point.
34 return createStringError(std::errc::illegal_byte_sequence,
35 "Unsupported file format.");
36}
37
41 if (!SectionName)
42 return SectionName.takeError();
43
44 for (const object::SectionRef &Section : Obj.sections()) {
45 Expected<StringRef> MaybeName = Section.getName();
46 if (!MaybeName)
47 return MaybeName.takeError();
48 if (*MaybeName != *SectionName)
49 continue;
50
51 if (Expected<StringRef> Contents = Section.getContents())
52 return *Contents;
53 else
54 return Contents.takeError();
55 }
56 return std::optional<StringRef>{};
57}
58
59Remark &RemarkLinker::keep(std::unique_ptr<Remark> Remark) {
60 StrTab.internalize(*Remark);
61 auto Inserted = Remarks.insert(std::move(Remark));
62 return **Inserted.first;
63}
64
66 PrependPath = std::string(PrependPathIn);
67}
68
69Error RemarkLinker::link(StringRef Buffer, std::optional<Format> RemarkFormat) {
70 if (!RemarkFormat) {
71 Expected<Format> ParserFormat = magicToFormat(Buffer);
72 if (!ParserFormat)
73 return ParserFormat.takeError();
74 RemarkFormat = *ParserFormat;
75 }
76
79 *RemarkFormat, Buffer, /*StrTab=*/std::nullopt,
80 PrependPath ? std::optional<StringRef>(StringRef(*PrependPath))
81 : std::optional<StringRef>());
82 if (!MaybeParser)
83 return MaybeParser.takeError();
84
85 RemarkParser &Parser = **MaybeParser;
86
87 while (true) {
89 if (Error E = Next.takeError()) {
90 if (E.isA<EndOfFileError>()) {
91 consumeError(std::move(E));
92 break;
93 }
94 return E;
95 }
96
97 assert(*Next != nullptr);
98
99 if (shouldKeepRemark(**Next))
100 keep(std::move(*Next));
101 }
102 return Error::success();
103}
104
106 std::optional<Format> RemarkFormat) {
109 if (!SectionOrErr)
110 return SectionOrErr.takeError();
111
112 if (std::optional<StringRef> Section = *SectionOrErr)
113 return link(*Section, RemarkFormat);
114 return Error::success();
115}
116
120 std::move(const_cast<StringTable &>(StrTab)));
121 if (!MaybeSerializer)
122 return MaybeSerializer.takeError();
123
124 std::unique_ptr<remarks::RemarkSerializer> Serializer =
125 std::move(*MaybeSerializer);
126
127 for (const Remark &R : remarks())
128 Serializer->emit(R);
129 return Error::success();
130}
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static Expected< StringRef > getRemarksSectionName(const object::ObjectFile &Obj)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:334
Tagged union holding either a T or a Error.
Definition: Error.h:474
Error takeError()
Take ownership of the stored error.
Definition: Error.h:601
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
bool isMachO() const
Definition: Binary.h:127
This class is the base class for all object file types.
Definition: ObjectFile.h:229
section_iterator_range sections() const
Definition: ObjectFile.h:328
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
Expected< Format > magicToFormat(StringRef Magic)
Parse and validate a magic number to a remark format.
Expected< std::unique_ptr< RemarkSerializer > > createRemarkSerializer(Format RemarksFormat, SerializerMode Mode, raw_ostream &OS)
Create a remark serializer.
Format
The format used for serializing/deserializing remarks.
Definition: RemarkFormat.h:25
Expected< std::unique_ptr< RemarkParser > > createRemarkParserFromMeta(Format ParserFormat, StringRef Buf, std::optional< ParsedStringTable > StrTab=std::nullopt, std::optional< StringRef > ExternalFilePrependPath=std::nullopt)
Expected< std::optional< StringRef > > getRemarksSectionContents(const object::ObjectFile &Obj)
Returns a buffer with the contents of the remarks section depending on the format of the file.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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"))
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1258
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1041
void setExternalFilePrependPath(StringRef PrependPath)
Set a path to prepend to the external file path.
Error serialize(raw_ostream &OS, Format RemarksFormat) const
Serialize the linked remarks to the stream OS, using the format RemarkFormat.
Error link(StringRef Buffer, std::optional< Format > RemarkFormat=std::nullopt)
Link the remarks found in Buffer.
iterator_range< iterator > remarks() const
Definition: RemarkLinker.h:104
Parser used to parse a raw buffer to remarks::Remark objects.
Definition: RemarkParser.h:40
virtual Expected< std::unique_ptr< Remark > > next()=0
If no error occurs, this returns a valid Remark object.
A remark type used for both emission and parsing.
Definition: Remark.h:97
The string table used for serializing remarks.
void internalize(Remark &R)
Modify R to use strings from this string table.