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