2019-10-29 05:53:31 +08:00
|
|
|
//===- llvm/IR/LLVMRemarkStreamer.cpp - Remark Streamer -*- C++ ---------*-===//
|
2019-03-06 23:20:13 +08:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2019-10-29 05:53:31 +08:00
|
|
|
// This file contains the implementation of the conversion between IR
|
|
|
|
// Diagnostics and serializable remarks::Remark objects.
|
2019-03-06 23:20:13 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
#include "llvm/IR/LLVMRemarkStreamer.h"
|
2019-05-31 05:45:59 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2020-03-01 02:23:54 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2019-03-06 23:20:13 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2019-05-31 05:45:59 +08:00
|
|
|
/// 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
|
2019-10-29 05:53:31 +08:00
|
|
|
LLVMRemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) const {
|
2019-05-31 05:45:59 +08:00
|
|
|
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()) {
|
2019-07-16 23:25:05 +08:00
|
|
|
R.Args.emplace_back();
|
|
|
|
R.Args.back().Key = Arg.Key;
|
|
|
|
R.Args.back().Val = Arg.Val;
|
|
|
|
R.Args.back().Loc = toRemarkLocation(Arg.Loc);
|
2019-05-31 05:45:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return R;
|
|
|
|
}
|
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
void LLVMRemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
|
|
|
|
if (!RS.matchesFilter(Diag.getPassName()))
|
2019-03-13 05:22:27 +08:00
|
|
|
return;
|
|
|
|
|
2019-05-31 05:45:59 +08:00
|
|
|
// First, convert the diagnostic to a remark.
|
|
|
|
remarks::Remark R = toRemark(Diag);
|
|
|
|
// Then, emit the remark through the serializer.
|
2019-10-29 05:53:31 +08:00
|
|
|
RS.getSerializer().emit(R);
|
2019-03-06 23:20:13 +08:00
|
|
|
}
|
2019-06-15 00:20:51 +08:00
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
char LLVMRemarkSetupFileError::ID = 0;
|
|
|
|
char LLVMRemarkSetupPatternError::ID = 0;
|
|
|
|
char LLVMRemarkSetupFormatError::ID = 0;
|
2019-10-29 02:10:07 +08:00
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
Expected<std::unique_ptr<ToolOutputFile>> llvm::setupLLVMOptimizationRemarks(
|
|
|
|
LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
|
|
|
|
StringRef RemarksFormat, bool RemarksWithHotness,
|
2020-11-18 02:37:59 +08:00
|
|
|
Optional<uint64_t> RemarksHotnessThreshold) {
|
2019-06-15 00:20:51 +08:00
|
|
|
if (RemarksWithHotness)
|
|
|
|
Context.setDiagnosticsHotnessRequested(true);
|
|
|
|
|
2020-11-18 02:43:02 +08:00
|
|
|
Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
|
2019-06-15 00:20:51 +08:00
|
|
|
|
|
|
|
if (RemarksFilename.empty())
|
|
|
|
return nullptr;
|
|
|
|
|
2019-10-08 16:21:20 +08:00
|
|
|
Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
|
|
|
|
if (Error E = Format.takeError())
|
2019-10-29 05:53:31 +08:00
|
|
|
return make_error<LLVMRemarkSetupFormatError>(std::move(E));
|
2019-10-08 16:21:20 +08:00
|
|
|
|
2019-06-15 00:20:51 +08:00
|
|
|
std::error_code EC;
|
2019-10-08 16:21:20 +08:00
|
|
|
auto Flags = *Format == remarks::Format::YAML ? sys::fs::OF_Text
|
|
|
|
: sys::fs::OF_None;
|
2019-06-15 00:20:51 +08:00
|
|
|
auto RemarksFile =
|
2019-10-08 16:21:20 +08:00
|
|
|
std::make_unique<ToolOutputFile>(RemarksFilename, EC, Flags);
|
2019-06-15 00:20:51 +08:00
|
|
|
// We don't use llvm::FileError here because some diagnostics want the file
|
|
|
|
// name separately.
|
|
|
|
if (EC)
|
2019-10-29 05:53:31 +08:00
|
|
|
return make_error<LLVMRemarkSetupFileError>(errorCodeToError(EC));
|
2019-06-15 00:20:51 +08:00
|
|
|
|
2019-07-25 03:47:57 +08:00
|
|
|
Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
|
2019-09-18 09:04:45 +08:00
|
|
|
remarks::createRemarkSerializer(
|
|
|
|
*Format, remarks::SerializerMode::Separate, RemarksFile->os());
|
2019-07-25 03:47:57 +08:00
|
|
|
if (Error E = RemarkSerializer.takeError())
|
2019-10-29 05:53:31 +08:00
|
|
|
return make_error<LLVMRemarkSetupFormatError>(std::move(E));
|
2019-07-25 00:36:35 +08:00
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
// Create the main remark streamer.
|
|
|
|
Context.setMainRemarkStreamer(std::make_unique<remarks::RemarkStreamer>(
|
2019-09-18 09:04:45 +08:00
|
|
|
std::move(*RemarkSerializer), RemarksFilename));
|
2019-06-15 00:20:51 +08:00
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
// Create LLVM's optimization remarks streamer.
|
|
|
|
Context.setLLVMRemarkStreamer(
|
|
|
|
std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
|
|
|
|
|
2019-06-15 00:20:51 +08:00
|
|
|
if (!RemarksPasses.empty())
|
2019-10-29 05:53:31 +08:00
|
|
|
if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
|
|
|
|
return make_error<LLVMRemarkSetupPatternError>(std::move(E));
|
2019-06-15 00:20:51 +08:00
|
|
|
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(RemarksFile);
|
2019-06-15 00:20:51 +08:00
|
|
|
}
|
2019-09-18 09:04:45 +08:00
|
|
|
|
2020-11-18 02:37:59 +08:00
|
|
|
Error llvm::setupLLVMOptimizationRemarks(
|
|
|
|
LLVMContext &Context, raw_ostream &OS, StringRef RemarksPasses,
|
|
|
|
StringRef RemarksFormat, bool RemarksWithHotness,
|
|
|
|
Optional<uint64_t> RemarksHotnessThreshold) {
|
2019-09-18 09:04:45 +08:00
|
|
|
if (RemarksWithHotness)
|
|
|
|
Context.setDiagnosticsHotnessRequested(true);
|
|
|
|
|
2020-11-18 02:43:02 +08:00
|
|
|
Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
|
2019-09-18 09:04:45 +08:00
|
|
|
|
|
|
|
Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
|
|
|
|
if (Error E = Format.takeError())
|
2019-10-29 05:53:31 +08:00
|
|
|
return make_error<LLVMRemarkSetupFormatError>(std::move(E));
|
2019-09-18 09:04:45 +08:00
|
|
|
|
|
|
|
Expected<std::unique_ptr<remarks::RemarkSerializer>> RemarkSerializer =
|
|
|
|
remarks::createRemarkSerializer(*Format,
|
|
|
|
remarks::SerializerMode::Separate, OS);
|
|
|
|
if (Error E = RemarkSerializer.takeError())
|
2019-10-29 05:53:31 +08:00
|
|
|
return make_error<LLVMRemarkSetupFormatError>(std::move(E));
|
|
|
|
|
|
|
|
// Create the main remark streamer.
|
|
|
|
Context.setMainRemarkStreamer(
|
|
|
|
std::make_unique<remarks::RemarkStreamer>(std::move(*RemarkSerializer)));
|
2019-09-18 09:04:45 +08:00
|
|
|
|
2019-10-29 05:53:31 +08:00
|
|
|
// Create LLVM's optimization remarks streamer.
|
|
|
|
Context.setLLVMRemarkStreamer(
|
|
|
|
std::make_unique<LLVMRemarkStreamer>(*Context.getMainRemarkStreamer()));
|
2019-09-18 09:04:45 +08:00
|
|
|
|
|
|
|
if (!RemarksPasses.empty())
|
2019-10-29 05:53:31 +08:00
|
|
|
if (Error E = Context.getMainRemarkStreamer()->setFilter(RemarksPasses))
|
|
|
|
return make_error<LLVMRemarkSetupPatternError>(std::move(E));
|
2019-09-18 09:04:45 +08:00
|
|
|
|
|
|
|
return Error::success();
|
|
|
|
}
|