1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
//===- RemarkCounter.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
//
//===----------------------------------------------------------------------===//
//
// Generic tool to count remarks based on properties
//
//===----------------------------------------------------------------------===//
#include "RemarkCounter.h"
#include "RemarkUtilRegistry.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Regex.h"
using namespace llvm;
using namespace remarks;
using namespace llvm::remarkutil;
static cl::SubCommand CountSub("count",
"Collect remarks based on specified criteria.");
INPUT_FORMAT_COMMAND_LINE_OPTIONS(CountSub)
INPUT_OUTPUT_COMMAND_LINE_OPTIONS(CountSub)
static cl::list<std::string>
Keys("args", cl::desc("Specify remark argument/s to count by."),
cl::value_desc("arguments"), cl::sub(CountSub), cl::ValueOptional);
static cl::list<std::string> RKeys(
"rargs",
cl::desc(
"Specify remark argument/s to count (accepts regular expressions)."),
cl::value_desc("arguments"), cl::sub(CountSub), cl::ValueOptional);
static cl::opt<std::string>
RemarkNameOpt("remark-name",
cl::desc("Optional remark name to filter collection by."),
cl::ValueOptional, cl::sub(CountSub));
static cl::opt<std::string>
PassNameOpt("pass-name", cl::ValueOptional,
cl::desc("Optional remark pass name to filter collection by."),
cl::sub(CountSub));
static cl::opt<std::string> RemarkFilterArgByOpt(
"filter-arg-by", cl::desc("Optional remark arg to filter collection by."),
cl::ValueOptional, cl::sub(CountSub));
static cl::opt<std::string>
RemarkNameOptRE("rremark-name",
cl::desc("Optional remark name to filter collection by "
"(accepts regular expressions)."),
cl::ValueOptional, cl::sub(CountSub));
static cl::opt<std::string>
RemarkArgFilterOptRE("rfilter-arg-by",
cl::desc("Optional remark arg to filter collection by "
"(accepts regular expressions)."),
cl::sub(CountSub), cl::ValueOptional);
static cl::opt<std::string>
PassNameOptRE("rpass-name", cl::ValueOptional,
cl::desc("Optional remark pass name to filter collection "
"by (accepts regular expressions)."),
cl::sub(CountSub));
static cl::opt<Type> RemarkTypeOpt(
"remark-type", cl::desc("Optional remark type to filter collection by."),
cl::values(clEnumValN(Type::Unknown, "unknown", "UNKOWN"),
clEnumValN(Type::Passed, "passed", "PASSED"),
clEnumValN(Type::Missed, "missed", "MISSED"),
clEnumValN(Type::Analysis, "analysis", "ANALYSIS"),
clEnumValN(Type::AnalysisFPCommute, "analysis-fp-commute",
"ANALYSIS_FP_COMMUTE"),
clEnumValN(Type::AnalysisAliasing, "analysis-aliasing",
"ANALYSIS_ALIASING"),
clEnumValN(Type::Failure, "failure", "FAILURE")),
cl::init(Type::Failure), cl::sub(CountSub));
static cl::opt<CountBy> CountByOpt(
"count-by", cl::desc("Specify the property to collect remarks by."),
cl::values(
clEnumValN(CountBy::REMARK, "remark-name",
"Counts individual remarks based on how many of the remark "
"exists."),
clEnumValN(CountBy::ARGUMENT, "arg",
"Counts based on the value each specified argument has. The "
"argument has to have a number value to be considered.")),
cl::init(CountBy::REMARK), cl::sub(CountSub));
static cl::opt<GroupBy> GroupByOpt(
"group-by", cl::desc("Specify the property to group remarks by."),
cl::values(
clEnumValN(
GroupBy::PER_SOURCE, "source",
"Display the count broken down by the filepath of each remark "
"emitted. Requires remarks to have DebugLoc information."),
clEnumValN(GroupBy::PER_FUNCTION, "function",
"Breakdown the count by function name."),
clEnumValN(
GroupBy::PER_FUNCTION_WITH_DEBUG_LOC, "function-with-loc",
"Breakdown the count by function name taking into consideration "
"the filepath info from the DebugLoc of the remark."),
clEnumValN(GroupBy::TOTAL, "total",
"Output the total number corresponding to the count for the "
"provided input file.")),
cl::init(GroupBy::PER_SOURCE), cl::sub(CountSub));
/// Look for matching argument with \p Key in \p Remark and return the parsed
/// integer value or 0 if it is has no integer value.
static unsigned getValForKey(StringRef Key, const Remark &Remark) {
auto *RemarkArg = find_if(Remark.Args, [&Key](const Argument &Arg) {
return Arg.Key == Key && Arg.isValInt();
});
if (RemarkArg == Remark.Args.end())
return 0;
return *RemarkArg->getValAsInt();
}
Error Filters::regexArgumentsValid() {
if (RemarkNameFilter && RemarkNameFilter->IsRegex)
if (auto E = checkRegex(RemarkNameFilter->FilterRE))
return E;
if (PassNameFilter && PassNameFilter->IsRegex)
if (auto E = checkRegex(PassNameFilter->FilterRE))
return E;
if (ArgFilter && ArgFilter->IsRegex)
if (auto E = checkRegex(ArgFilter->FilterRE))
return E;
return Error::success();
}
bool Filters::filterRemark(const Remark &Remark) {
if (RemarkNameFilter && !RemarkNameFilter->match(Remark.RemarkName))
return false;
if (PassNameFilter && !PassNameFilter->match(Remark.PassName))
return false;
if (RemarkTypeFilter)
return *RemarkTypeFilter == Remark.RemarkType;
if (ArgFilter) {
if (!any_of(Remark.Args,
[this](Argument Arg) { return ArgFilter->match(Arg.Val); }))
return false;
}
return true;
}
Error ArgumentCounter::getAllMatchingArgumentsInRemark(
StringRef Buffer, ArrayRef<FilterMatcher> Arguments, Filters &Filter) {
auto MaybeParser = createRemarkParser(InputFormat, Buffer);
if (!MaybeParser)
return MaybeParser.takeError();
auto &Parser = **MaybeParser;
auto MaybeRemark = Parser.next();
for (; MaybeRemark; MaybeRemark = Parser.next()) {
auto &Remark = **MaybeRemark;
// Only collect keys from remarks included in the filter.
if (!Filter.filterRemark(Remark))
continue;
for (auto &Key : Arguments) {
for (Argument Arg : Remark.Args)
if (Key.match(Arg.Key) && Arg.isValInt())
ArgumentSetIdxMap.insert({Arg.Key, ArgumentSetIdxMap.size()});
}
}
auto E = MaybeRemark.takeError();
if (!E.isA<EndOfFileError>())
return E;
consumeError(std::move(E));
return Error::success();
}
std::optional<std::string> Counter::getGroupByKey(const Remark &Remark) {
switch (Group) {
case GroupBy::PER_FUNCTION:
return Remark.FunctionName.str();
case GroupBy::TOTAL:
return "Total";
case GroupBy::PER_SOURCE:
case GroupBy::PER_FUNCTION_WITH_DEBUG_LOC:
if (!Remark.Loc.has_value())
return std::nullopt;
if (Group == GroupBy::PER_FUNCTION_WITH_DEBUG_LOC)
return Remark.Loc->SourceFilePath.str() + ":" + Remark.FunctionName.str();
return Remark.Loc->SourceFilePath.str();
}
llvm_unreachable("Fully covered switch above!");
}
void ArgumentCounter::collect(const Remark &Remark) {
SmallVector<unsigned, 4> Row(ArgumentSetIdxMap.size());
std::optional<std::string> GroupByKey = getGroupByKey(Remark);
// Early return if we don't have a value
if (!GroupByKey)
return;
auto GroupVal = *GroupByKey;
CountByKeysMap.insert({GroupVal, Row});
for (auto [Key, Idx] : ArgumentSetIdxMap) {
auto Count = getValForKey(Key, Remark);
CountByKeysMap[GroupVal][Idx] += Count;
}
}
void RemarkCounter::collect(const Remark &Remark) {
std::optional<std::string> Key = getGroupByKey(Remark);
if (!Key.has_value())
return;
auto Iter = CountedByRemarksMap.insert({*Key, 1});
if (!Iter.second)
Iter.first->second += 1;
}
Error ArgumentCounter::print(StringRef OutputFileName) {
auto MaybeOF =
getOutputFileWithFlags(OutputFileName, sys::fs::OF_TextWithCRLF);
if (!MaybeOF)
return MaybeOF.takeError();
auto OF = std::move(*MaybeOF);
OF->os() << groupByToStr(Group) << ",";
unsigned Idx = 0;
for (auto [Key, _] : ArgumentSetIdxMap) {
OF->os() << Key;
if (Idx != ArgumentSetIdxMap.size() - 1)
OF->os() << ",";
Idx++;
}
OF->os() << "\n";
for (auto [Header, CountVector] : CountByKeysMap) {
OF->os() << Header << ",";
unsigned Idx = 0;
for (auto Count : CountVector) {
OF->os() << Count;
if (Idx != ArgumentSetIdxMap.size() - 1)
OF->os() << ",";
Idx++;
}
OF->os() << "\n";
}
return Error::success();
}
Error RemarkCounter::print(StringRef OutputFileName) {
auto MaybeOF =
getOutputFileWithFlags(OutputFileName, sys::fs::OF_TextWithCRLF);
if (!MaybeOF)
return MaybeOF.takeError();
auto OF = std::move(*MaybeOF);
OF->os() << groupByToStr(Group) << ","
<< "Count\n";
for (auto [Key, Count] : CountedByRemarksMap)
OF->os() << Key << "," << Count << "\n";
OF->keep();
return Error::success();
}
Expected<Filters> getRemarkFilter() {
// Create Filter properties.
std::optional<FilterMatcher> RemarkNameFilter;
std::optional<FilterMatcher> PassNameFilter;
std::optional<FilterMatcher> RemarkArgFilter;
std::optional<Type> RemarkType;
if (!RemarkNameOpt.empty())
RemarkNameFilter = {RemarkNameOpt, false};
else if (!RemarkNameOptRE.empty())
RemarkNameFilter = {RemarkNameOptRE, true};
if (!PassNameOpt.empty())
PassNameFilter = {PassNameOpt, false};
else if (!PassNameOptRE.empty())
PassNameFilter = {PassNameOptRE, true};
if (RemarkTypeOpt != Type::Failure)
RemarkType = RemarkTypeOpt;
if (!RemarkFilterArgByOpt.empty())
RemarkArgFilter = {RemarkFilterArgByOpt, false};
else if (!RemarkArgFilterOptRE.empty())
RemarkArgFilter = {RemarkArgFilterOptRE, true};
// Create RemarkFilter.
return Filters::createRemarkFilter(std::move(RemarkNameFilter),
std::move(PassNameFilter),
std::move(RemarkArgFilter), RemarkType);
}
Error useCollectRemark(StringRef Buffer, Counter &Counter, Filters &Filter) {
// Create Parser.
auto MaybeParser = createRemarkParser(InputFormat, Buffer);
if (!MaybeParser)
return MaybeParser.takeError();
auto &Parser = **MaybeParser;
auto MaybeRemark = Parser.next();
for (; MaybeRemark; MaybeRemark = Parser.next()) {
const Remark &Remark = **MaybeRemark;
if (Filter.filterRemark(Remark))
Counter.collect(Remark);
}
if (auto E = Counter.print(OutputFileName))
return E;
auto E = MaybeRemark.takeError();
if (!E.isA<EndOfFileError>())
return E;
consumeError(std::move(E));
return Error::success();
}
static Error collectRemarks() {
// Create a parser for the user-specified input format.
auto MaybeBuf = getInputMemoryBuffer(InputFileName);
if (!MaybeBuf)
return MaybeBuf.takeError();
StringRef Buffer = (*MaybeBuf)->getBuffer();
auto MaybeFilter = getRemarkFilter();
if (!MaybeFilter)
return MaybeFilter.takeError();
auto &Filter = *MaybeFilter;
if (CountByOpt == CountBy::REMARK) {
RemarkCounter RC(GroupByOpt);
if (auto E = useCollectRemark(Buffer, RC, Filter))
return E;
} else if (CountByOpt == CountBy::ARGUMENT) {
SmallVector<FilterMatcher, 4> ArgumentsVector;
if (!Keys.empty()) {
for (auto &Key : Keys)
ArgumentsVector.push_back({Key, false});
} else if (!RKeys.empty())
for (auto Key : RKeys)
ArgumentsVector.push_back({Key, true});
else
ArgumentsVector.push_back({".*", true});
Expected<ArgumentCounter> AC = ArgumentCounter::createArgumentCounter(
GroupByOpt, ArgumentsVector, Buffer, Filter);
if (!AC)
return AC.takeError();
if (auto E = useCollectRemark(Buffer, *AC, Filter))
return E;
}
return Error::success();
}
static CommandRegistration CountReg(&CountSub, collectRemarks);
|