2019-05-31 05:45:59 +08:00
|
|
|
//===- YAMLRemarkSerializer.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 the implementation of the YAML remark serializer using
|
|
|
|
// LLVM's YAMLTraits.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-07-24 03:28:03 +08:00
|
|
|
#include "llvm/Remarks/YAMLRemarkSerializer.h"
|
2022-03-10 05:29:31 +08:00
|
|
|
#include "llvm/Remarks/Remark.h"
|
2021-04-21 22:00:30 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2019-05-31 05:45:59 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::remarks;
|
|
|
|
|
|
|
|
// Use the same keys whether we use a string table or not (respectively, T is an
|
|
|
|
// unsigned or a StringRef).
|
|
|
|
template <typename T>
|
|
|
|
static void mapRemarkHeader(yaml::IO &io, T PassName, T RemarkName,
|
|
|
|
Optional<RemarkLocation> RL, T FunctionName,
|
|
|
|
Optional<uint64_t> Hotness,
|
|
|
|
ArrayRef<Argument> Args) {
|
|
|
|
io.mapRequired("Pass", PassName);
|
|
|
|
io.mapRequired("Name", RemarkName);
|
|
|
|
io.mapOptional("DebugLoc", RL);
|
|
|
|
io.mapRequired("Function", FunctionName);
|
|
|
|
io.mapOptional("Hotness", Hotness);
|
|
|
|
io.mapOptional("Args", Args);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
|
|
|
|
template <> struct MappingTraits<remarks::Remark *> {
|
|
|
|
static void mapping(IO &io, remarks::Remark *&Remark) {
|
|
|
|
assert(io.outputting() && "input not yet implemented");
|
|
|
|
|
|
|
|
if (io.mapTag("!Passed", (Remark->RemarkType == Type::Passed)))
|
|
|
|
;
|
|
|
|
else if (io.mapTag("!Missed", (Remark->RemarkType == Type::Missed)))
|
|
|
|
;
|
|
|
|
else if (io.mapTag("!Analysis", (Remark->RemarkType == Type::Analysis)))
|
|
|
|
;
|
|
|
|
else if (io.mapTag("!AnalysisFPCommute",
|
|
|
|
(Remark->RemarkType == Type::AnalysisFPCommute)))
|
|
|
|
;
|
|
|
|
else if (io.mapTag("!AnalysisAliasing",
|
|
|
|
(Remark->RemarkType == Type::AnalysisAliasing)))
|
|
|
|
;
|
|
|
|
else if (io.mapTag("!Failure", (Remark->RemarkType == Type::Failure)))
|
|
|
|
;
|
|
|
|
else
|
|
|
|
llvm_unreachable("Unknown remark type");
|
|
|
|
|
2019-09-17 06:45:17 +08:00
|
|
|
if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
|
|
|
|
reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
|
2022-06-21 02:49:10 +08:00
|
|
|
assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
|
2019-09-17 06:45:17 +08:00
|
|
|
StringTable &StrTab = *Serializer->StrTab;
|
|
|
|
unsigned PassID = StrTab.add(Remark->PassName).first;
|
|
|
|
unsigned NameID = StrTab.add(Remark->RemarkName).first;
|
|
|
|
unsigned FunctionID = StrTab.add(Remark->FunctionName).first;
|
2019-05-31 05:45:59 +08:00
|
|
|
mapRemarkHeader(io, PassID, NameID, Remark->Loc, FunctionID,
|
|
|
|
Remark->Hotness, Remark->Args);
|
|
|
|
} else {
|
|
|
|
mapRemarkHeader(io, Remark->PassName, Remark->RemarkName, Remark->Loc,
|
|
|
|
Remark->FunctionName, Remark->Hotness, Remark->Args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct MappingTraits<RemarkLocation> {
|
|
|
|
static void mapping(IO &io, RemarkLocation &RL) {
|
|
|
|
assert(io.outputting() && "input not yet implemented");
|
|
|
|
|
|
|
|
StringRef File = RL.SourceFilePath;
|
|
|
|
unsigned Line = RL.SourceLine;
|
|
|
|
unsigned Col = RL.SourceColumn;
|
|
|
|
|
2019-09-17 06:45:17 +08:00
|
|
|
if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
|
|
|
|
reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
|
2022-06-21 02:49:10 +08:00
|
|
|
assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
|
2019-09-17 06:45:17 +08:00
|
|
|
StringTable &StrTab = *Serializer->StrTab;
|
|
|
|
unsigned FileID = StrTab.add(File).first;
|
2019-05-31 05:45:59 +08:00
|
|
|
io.mapRequired("File", FileID);
|
|
|
|
} else {
|
|
|
|
io.mapRequired("File", File);
|
|
|
|
}
|
|
|
|
|
|
|
|
io.mapRequired("Line", Line);
|
|
|
|
io.mapRequired("Column", Col);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const bool flow = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Helper struct for multiline string block literals. Use this type to preserve
|
|
|
|
/// newlines in strings.
|
|
|
|
struct StringBlockVal {
|
|
|
|
StringRef Value;
|
2019-10-08 01:05:09 +08:00
|
|
|
StringBlockVal(StringRef R) : Value(R) {}
|
2019-05-31 05:45:59 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct BlockScalarTraits<StringBlockVal> {
|
|
|
|
static void output(const StringBlockVal &S, void *Ctx, raw_ostream &OS) {
|
|
|
|
return ScalarTraits<StringRef>::output(S.Value, Ctx, OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StringRef input(StringRef Scalar, void *Ctx, StringBlockVal &S) {
|
|
|
|
return ScalarTraits<StringRef>::input(Scalar, Ctx, S.Value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// ArrayRef is not really compatible with the YAMLTraits. Everything should be
|
|
|
|
/// immutable in an ArrayRef, while the SequenceTraits expect a mutable version
|
|
|
|
/// for inputting, but we're only using the outputting capabilities here.
|
|
|
|
/// This is a hack, but still nicer than having to manually call the YAMLIO
|
|
|
|
/// internal methods.
|
|
|
|
/// Keep this in this file so that it doesn't get misused from YAMLTraits.h.
|
|
|
|
template <typename T> struct SequenceTraits<ArrayRef<T>> {
|
|
|
|
static size_t size(IO &io, ArrayRef<T> &seq) { return seq.size(); }
|
|
|
|
static Argument &element(IO &io, ArrayRef<T> &seq, size_t index) {
|
|
|
|
assert(io.outputting() && "input not yet implemented");
|
|
|
|
// The assert above should make this "safer" to satisfy the YAMLTraits.
|
|
|
|
return const_cast<T &>(seq[index]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Implement this as a mapping for now to get proper quotation for the value.
|
|
|
|
template <> struct MappingTraits<Argument> {
|
|
|
|
static void mapping(IO &io, Argument &A) {
|
|
|
|
assert(io.outputting() && "input not yet implemented");
|
|
|
|
|
2019-09-17 06:45:17 +08:00
|
|
|
if (auto *Serializer = dyn_cast<YAMLStrTabRemarkSerializer>(
|
|
|
|
reinterpret_cast<RemarkSerializer *>(io.getContext()))) {
|
2022-06-21 02:49:10 +08:00
|
|
|
assert(Serializer->StrTab && "YAMLStrTabSerializer with no StrTab.");
|
2019-09-17 06:45:17 +08:00
|
|
|
StringTable &StrTab = *Serializer->StrTab;
|
|
|
|
auto ValueID = StrTab.add(A.Val).first;
|
2019-05-31 05:45:59 +08:00
|
|
|
io.mapRequired(A.Key.data(), ValueID);
|
|
|
|
} else if (StringRef(A.Val).count('\n') > 1) {
|
|
|
|
StringBlockVal S(A.Val);
|
|
|
|
io.mapRequired(A.Key.data(), S);
|
|
|
|
} else {
|
|
|
|
io.mapRequired(A.Key.data(), A.Val);
|
|
|
|
}
|
|
|
|
io.mapOptional("DebugLoc", A.Loc);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace yaml
|
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
LLVM_YAML_IS_SEQUENCE_VECTOR(Argument)
|
|
|
|
|
2019-09-17 06:45:17 +08:00
|
|
|
YAMLRemarkSerializer::YAMLRemarkSerializer(raw_ostream &OS, SerializerMode Mode,
|
|
|
|
Optional<StringTable> StrTabIn)
|
|
|
|
: YAMLRemarkSerializer(Format::YAML, OS, Mode, std::move(StrTabIn)) {}
|
|
|
|
|
|
|
|
YAMLRemarkSerializer::YAMLRemarkSerializer(Format SerializerFormat,
|
|
|
|
raw_ostream &OS, SerializerMode Mode,
|
|
|
|
Optional<StringTable> StrTabIn)
|
|
|
|
: RemarkSerializer(SerializerFormat, OS, Mode),
|
|
|
|
YAMLOutput(OS, reinterpret_cast<void *>(this)) {
|
|
|
|
StrTab = std::move(StrTabIn);
|
|
|
|
}
|
2019-05-31 05:45:59 +08:00
|
|
|
|
2019-07-25 03:47:57 +08:00
|
|
|
void YAMLRemarkSerializer::emit(const Remark &Remark) {
|
2019-05-31 05:45:59 +08:00
|
|
|
// Again, YAMLTraits expect a non-const object for inputting, but we're not
|
|
|
|
// using that here.
|
|
|
|
auto R = const_cast<remarks::Remark *>(&Remark);
|
|
|
|
YAMLOutput << R;
|
|
|
|
}
|
2019-07-26 09:33:30 +08:00
|
|
|
|
|
|
|
std::unique_ptr<MetaSerializer>
|
|
|
|
YAMLRemarkSerializer::metaSerializer(raw_ostream &OS,
|
|
|
|
Optional<StringRef> ExternalFilename) {
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<YAMLMetaSerializer>(OS, ExternalFilename);
|
2019-07-26 09:33:30 +08:00
|
|
|
}
|
|
|
|
|
2019-09-06 02:30:20 +08:00
|
|
|
void YAMLStrTabRemarkSerializer::emit(const Remark &Remark) {
|
|
|
|
// In standalone mode, for the serializer with a string table, emit the
|
|
|
|
// metadata first and set DidEmitMeta to avoid emitting it again.
|
|
|
|
if (Mode == SerializerMode::Standalone && !DidEmitMeta) {
|
|
|
|
std::unique_ptr<MetaSerializer> MetaSerializer =
|
|
|
|
metaSerializer(OS, /*ExternalFilename=*/None);
|
|
|
|
MetaSerializer->emit();
|
|
|
|
DidEmitMeta = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then do the usual remark emission.
|
|
|
|
YAMLRemarkSerializer::emit(Remark);
|
|
|
|
}
|
|
|
|
|
2019-07-26 09:33:30 +08:00
|
|
|
std::unique_ptr<MetaSerializer> YAMLStrTabRemarkSerializer::metaSerializer(
|
|
|
|
raw_ostream &OS, Optional<StringRef> ExternalFilename) {
|
|
|
|
assert(StrTab);
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<YAMLStrTabMetaSerializer>(OS, ExternalFilename,
|
2019-09-17 06:45:17 +08:00
|
|
|
*StrTab);
|
2019-07-26 09:33:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitMagic(raw_ostream &OS) {
|
|
|
|
// Emit the magic number.
|
|
|
|
OS << remarks::Magic;
|
|
|
|
// Explicitly emit a '\0'.
|
|
|
|
OS.write('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void emitVersion(raw_ostream &OS) {
|
|
|
|
// Emit the version number: little-endian uint64_t.
|
|
|
|
std::array<char, 8> Version;
|
Reland: [Remarks] Add an LLVM-bitstream-based remark serializer
Add a new serializer, using a binary format based on the LLVM bitstream
format.
This format provides a way to serialize the remarks in two modes:
1) Separate mode: the metadata is separate from the remark entries.
2) Standalone mode: the metadata and the remark entries are in the same
file.
The format contains:
* a meta block: container version, container type, string table,
external file path, remark version
* a remark block: type, remark name, pass name, function name, debug
file, debug line, debug column, hotness, arguments (key, value, debug
file, debug line, debug column)
A string table is required for this format, which will be dumped in the
meta block to be consumed before parsing the remark blocks.
On clang itself, we noticed a size reduction of 13.4x compared to YAML,
and a compile-time reduction of between 1.7% and 3.5% on CTMark.
Differential Revision: https://reviews.llvm.org/D63466
Original llvm-svn: 367364
Revert llvm-svn: 367370
llvm-svn: 367372
2019-07-31 08:13:51 +08:00
|
|
|
support::endian::write64le(Version.data(), remarks::CurrentRemarkVersion);
|
2019-07-26 09:33:30 +08:00
|
|
|
OS.write(Version.data(), Version.size());
|
|
|
|
}
|
|
|
|
|
2019-09-17 06:45:17 +08:00
|
|
|
static void emitStrTab(raw_ostream &OS, Optional<const StringTable *> StrTab) {
|
2019-07-26 09:33:30 +08:00
|
|
|
// Emit the string table in the section.
|
2019-09-17 06:45:17 +08:00
|
|
|
uint64_t StrTabSize = StrTab ? (*StrTab)->SerializedSize : 0;
|
2019-07-26 09:33:30 +08:00
|
|
|
// Emit the total size of the string table (the size itself excluded):
|
|
|
|
// little-endian uint64_t.
|
|
|
|
// Note: even if no string table is used, emit 0.
|
|
|
|
std::array<char, 8> StrTabSizeBuf;
|
|
|
|
support::endian::write64le(StrTabSizeBuf.data(), StrTabSize);
|
|
|
|
OS.write(StrTabSizeBuf.data(), StrTabSizeBuf.size());
|
|
|
|
if (StrTab)
|
2019-09-17 06:45:17 +08:00
|
|
|
(*StrTab)->serialize(OS);
|
2019-07-26 09:33:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitExternalFile(raw_ostream &OS, StringRef Filename) {
|
|
|
|
// Emit the null-terminated absolute path to the remark file.
|
|
|
|
SmallString<128> FilenameBuf = Filename;
|
|
|
|
sys::fs::make_absolute(FilenameBuf);
|
|
|
|
assert(!FilenameBuf.empty() && "The filename can't be empty.");
|
|
|
|
OS.write(FilenameBuf.data(), FilenameBuf.size());
|
|
|
|
OS.write('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
void YAMLMetaSerializer::emit() {
|
|
|
|
emitMagic(OS);
|
|
|
|
emitVersion(OS);
|
|
|
|
emitStrTab(OS, None);
|
|
|
|
if (ExternalFilename)
|
|
|
|
emitExternalFile(OS, *ExternalFilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void YAMLStrTabMetaSerializer::emit() {
|
|
|
|
emitMagic(OS);
|
|
|
|
emitVersion(OS);
|
2019-09-17 06:45:17 +08:00
|
|
|
emitStrTab(OS, &StrTab);
|
2019-07-26 09:33:30 +08:00
|
|
|
if (ExternalFilename)
|
|
|
|
emitExternalFile(OS, *ExternalFilename);
|
|
|
|
}
|