Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/IR/RemarkStreamer.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- llvm/IR/RemarkStreamer.cpp - Remark Streamer -*- 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 contains the implementation of the remark outputting as part of
10
// LLVMContext.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/IR/RemarkStreamer.h"
15
#include "llvm/IR/DiagnosticInfo.h"
16
#include "llvm/IR/Function.h"
17
#include "llvm/IR/GlobalValue.h"
18
#include "llvm/Remarks/RemarkFormat.h"
19
#include "llvm/Remarks/YAMLRemarkSerializer.h"
20
21
using namespace llvm;
22
23
RemarkStreamer::RemarkStreamer(StringRef Filename,
24
                               std::unique_ptr<remarks::Serializer> Serializer)
25
91
    : Filename(Filename), PassFilter(), Serializer(std::move(Serializer)) {
26
91
  assert(!Filename.empty() && "This needs to be a real filename.");
27
91
}
28
29
9
Error RemarkStreamer::setFilter(StringRef Filter) {
30
9
  Regex R = Regex(Filter);
31
9
  std::string RegexError;
32
9
  if (!R.isValid(RegexError))
33
1
    return createStringError(std::make_error_code(std::errc::invalid_argument),
34
1
                             RegexError.data());
35
8
  PassFilter = std::move(R);
36
8
  return Error::success();
37
8
}
38
39
/// DiagnosticKind -> remarks::Type
40
425
static remarks::Type toRemarkType(enum DiagnosticKind Kind) {
41
425
  switch (Kind) {
42
425
  default:
43
0
    return remarks::Type::Unknown;
44
425
  case DK_OptimizationRemark:
45
121
  case DK_MachineOptimizationRemark:
46
121
    return remarks::Type::Passed;
47
121
  case DK_OptimizationRemarkMissed:
48
110
  case DK_MachineOptimizationRemarkMissed:
49
110
    return remarks::Type::Missed;
50
183
  case DK_OptimizationRemarkAnalysis:
51
183
  case DK_MachineOptimizationRemarkAnalysis:
52
183
    return remarks::Type::Analysis;
53
183
  case DK_OptimizationRemarkAnalysisFPCommute:
54
0
    return remarks::Type::AnalysisFPCommute;
55
183
  case DK_OptimizationRemarkAnalysisAliasing:
56
1
    return remarks::Type::AnalysisAliasing;
57
183
  case DK_OptimizationFailure:
58
10
    return remarks::Type::Failure;
59
425
  }
60
425
}
61
62
/// DiagnosticLocation -> remarks::RemarkLocation.
63
static Optional<remarks::RemarkLocation>
64
1.85k
toRemarkLocation(const DiagnosticLocation &DL) {
65
1.85k
  if (!DL.isValid())
66
1.63k
    return None;
67
226
  StringRef File = DL.getRelativePath();
68
226
  unsigned Line = DL.getLine();
69
226
  unsigned Col = DL.getColumn();
70
226
  return remarks::RemarkLocation{File, Line, Col};
71
226
}
72
73
/// LLVM Diagnostic -> Remark
74
remarks::Remark
75
425
RemarkStreamer::toRemark(const DiagnosticInfoOptimizationBase &Diag) {
76
425
  remarks::Remark R; // The result.
77
425
  R.RemarkType = toRemarkType(static_cast<DiagnosticKind>(Diag.getKind()));
78
425
  R.PassName = Diag.getPassName();
79
425
  R.RemarkName = Diag.getRemarkName();
80
425
  R.FunctionName =
81
425
      GlobalValue::dropLLVMManglingEscape(Diag.getFunction().getName());
82
425
  R.Loc = toRemarkLocation(Diag.getLocation());
83
425
  R.Hotness = Diag.getHotness();
84
425
85
1.43k
  for (const DiagnosticInfoOptimizationBase::Argument &Arg : Diag.getArgs()) {
86
1.43k
    R.Args.emplace_back();
87
1.43k
    R.Args.back().Key = Arg.Key;
88
1.43k
    R.Args.back().Val = Arg.Val;
89
1.43k
    R.Args.back().Loc = toRemarkLocation(Arg.Loc);
90
1.43k
  }
91
425
92
425
  return R;
93
425
}
94
95
465
void RemarkStreamer::emit(const DiagnosticInfoOptimizationBase &Diag) {
96
465
  if (Optional<Regex> &Filter = PassFilter)
97
50
    if (!Filter->match(Diag.getPassName()))
98
40
      return;
99
425
100
425
  // First, convert the diagnostic to a remark.
101
425
  remarks::Remark R = toRemark(Diag);
102
425
  // Then, emit the remark through the serializer.
103
425
  Serializer->emit(R);
104
425
}
105
106
char RemarkSetupFileError::ID = 0;
107
char RemarkSetupPatternError::ID = 0;
108
char RemarkSetupFormatError::ID = 0;
109
110
static std::unique_ptr<remarks::Serializer>
111
91
formatToSerializer(remarks::Format RemarksFormat, raw_ostream &OS) {
112
91
  switch (RemarksFormat) {
113
91
  case remarks::Format::Unknown:
114
0
    llvm_unreachable("Unknown remark serializer format.");
115
91
    
return nullptr0
;
116
91
  case remarks::Format::YAML:
117
90
    return llvm::make_unique<remarks::YAMLSerializer>(OS);
118
91
  case remarks::Format::YAMLStrTab:
119
1
    return llvm::make_unique<remarks::YAMLStrTabSerializer>(OS);
120
0
  };
121
0
  llvm_unreachable("Unknown remarks::Format enum");
122
0
}
123
124
Expected<std::unique_ptr<ToolOutputFile>>
125
llvm::setupOptimizationRemarks(LLVMContext &Context, StringRef RemarksFilename,
126
                               StringRef RemarksPasses, StringRef RemarksFormat,
127
                               bool RemarksWithHotness,
128
54.1k
                               unsigned RemarksHotnessThreshold) {
129
54.1k
  if (RemarksWithHotness)
130
59
    Context.setDiagnosticsHotnessRequested(true);
131
54.1k
132
54.1k
  if (RemarksHotnessThreshold)
133
18
    Context.setDiagnosticsHotnessThreshold(RemarksHotnessThreshold);
134
54.1k
135
54.1k
  if (RemarksFilename.empty())
136
54.0k
    return nullptr;
137
94
138
94
  std::error_code EC;
139
94
  auto RemarksFile =
140
94
      llvm::make_unique<ToolOutputFile>(RemarksFilename, EC, sys::fs::F_None);
141
94
  // We don't use llvm::FileError here because some diagnostics want the file
142
94
  // name separately.
143
94
  if (EC)
144
0
    return make_error<RemarkSetupFileError>(errorCodeToError(EC));
145
94
146
94
  Expected<remarks::Format> Format = remarks::parseFormat(RemarksFormat);
147
94
  if (Error E = Format.takeError())
148
1
    return make_error<RemarkSetupFormatError>(std::move(E));
149
93
150
93
  Context.setRemarkStreamer(llvm::make_unique<RemarkStreamer>(
151
93
      RemarksFilename, formatToSerializer(*Format, RemarksFile->os())));
152
93
153
93
  if (!RemarksPasses.empty())
154
9
    if (Error E = Context.getRemarkStreamer()->setFilter(RemarksPasses))
155
1
      return make_error<RemarkSetupPatternError>(std::move(E));
156
92
157
92
  return std::move(RemarksFile);
158
92
}