1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 19:52:54 +01:00
llvm-mirror/lib/IR/RemarkStreamer.cpp
Francis Visoiu Mistrih 75722a7d1f 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 00:13:51 +00:00

153 lines
5.2 KiB
C++

//===- llvm/IR/RemarkStreamer.cpp - Remark Streamer -*- C++ -------------*-===//
//
// 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 contains the implementation of the remark outputting as part of
// LLVMContext.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/RemarkStreamer.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/GlobalValue.h"
#include "llvm/Remarks/BitstreamRemarkSerializer.h"
#include "llvm/Remarks/RemarkFormat.h"
#include "llvm/Remarks/RemarkSerializer.h"
using namespace llvm;
RemarkStreamer::RemarkStreamer(
StringRef Filename,
std::unique_ptr<remarks::RemarkSerializer> RemarkSerializer)
: Filename(Filename), PassFilter(),
RemarkSerializer(std::move(RemarkSerializer)) {
assert(!Filename.empty() && "This needs to be a real filename.");
}
Error RemarkStreamer::setFilter(StringRef Filter) {
Regex R = Regex(Filter);
std::string RegexError;
if (!R.isValid(RegexError))
return createStringError(std::make_error_code(std::errc::invalid_argument),
RegexError.data());
PassFilter = std::move(R);
return Error::success();
}
/// DiagnosticKind -> remarks::Type
static remarks::Type toRemarkType(enum DiagnosticKind Kind) {
switch (Kind) {
default:
return remarks::Type::Unknown;
case DK_OptimizationRemark:
case DK_MachineOptimizationRemark:
return remarks::Type::Passed;
case DK_OptimizationRemarkMissed:
case DK_MachineOptimizationRemarkMissed:
return remarks::Type::Missed;
case DK_OptimizationRemarkAnalysis:
case DK_MachineOptimizationRemarkAnalysis:
return remarks::Type::Analysis;
case DK_OptimizationRemarkAnalysisFPCommute:
return remarks::Type::AnalysisFPCommute;
case DK_OptimizationRemarkAnalysisAliasing:
return remarks::Type::AnalysisAliasing;
case DK_OptimizationFailure:
return remarks::Type::Failure;
}
}
/// DiagnosticLocation -> remarks::RemarkLocation.
static Optional<remarks::RemarkLocation>
toRemarkLocation(const DiagnosticLocation &DL) {
if (!DL.isValid())
return None;
StringRef File = DL.getRelativePath();
unsigned Line = DL.getLine();
unsigned Col = DL.getColumn();
return remarks::RemarkLocation{File, Line, Col};
}
/// LLVM Diagnostic -> Remark
remarks::Remark
RemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) {
remarks::Remark R; // The result.
R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
R.PassName = Diag.getPassName();
R.RemarkName = Diag.getRemarkName();
R.FunctionName =
GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());
R.Loc = toRemarkLocation(Diag.getLocation());
R.Hotness = Diag.getHotness();
for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
R.Args.emplace_back();
R.Args.back().Key = Arg.Key;
R.Args.back().Val = Arg.Val;
R.Args.back().Loc = toRemarkLocation(Arg.Loc);
}
return R;
}
void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
if (Optional<Regex> &Filter = PassFilter)
if (!Filter->match(Diag.getPassName()))
return;
// First, convert the diagnostic to a remark.
remarks::Remark R = toRemark(Diag);
// Then, emit the remark through the serializer.
RemarkSerializer->emit(R);
}
char RemarkSetupFileError::ID = 0;
char RemarkSetupPatternError::ID = 0;
char RemarkSetupFormatError::ID = 0;
Expected<std::unique_ptr<ToolOutputFile>>
llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
StringRef RemarksPasses, StringRef RemarksFormat,
bool RemarksWithHotness,
unsigned RemarksHotnessThreshold) {
if (RemarksWithHotness)
Context.setDiagnosticsHotnessRequested(true);
if (RemarksHotnessThreshold)
Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
if (RemarksFilename.empty())
return nullptr;
std::error_code EC;
auto RemarksFile =
llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
// We don't use llvm::FileError here because some diagnostics want the file
// name separately.
if (EC)
return make_error<RemarkSetupFileError>(errorCodeToError(EC));
Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
if (Error E = Format.takeError())
return make_error<RemarkSetupFormatError>(std::move(E));
Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
remarks::createRemarkSerializer(*Format, remarks::SerializerMode::Separate, RemarksFile->os());
if (Error E = RemarkSerializer.takeError())
return make_error<RemarkSetupFormatError>(std::move(E));
Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
RemarksFilename, std::move(*RemarkSerializer)));
if (!RemarksPasses.empty())
if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))
return make_error<RemarkSetupPatternError>(std::move(E));
return std::move(RemarksFile);
}