Skip to content

Commit 72cb5d0

Browse files
committed
Use FilterMatcher, and move it to RemarkUtilHelpers.h
1 parent dc263e1 commit 72cb5d0

File tree

5 files changed

+80
-54
lines changed

5 files changed

+80
-54
lines changed

llvm/test/tools/llvm-remarkutil/instruction-mix.test

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yam
22
RUN: llvm-remarkutil yaml2bitstream %p/Inputs/instruction-mix.yaml | llvm-remarkutil instruction-mix --parser=bitstream | FileCheck %s
33
RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --report_style=human | FileCheck %s
44
RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --report_style=csv | FileCheck %s --check-prefix=CSV
5-
RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --rfilter=meow | FileCheck %s --check-prefix=MEOW
5+
RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --rfilter=meow | FileCheck %s --check-prefix=MEOW-RE
6+
RUN: llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --filter=meow | FileCheck %s --check-prefix=MEOW-EXACT
67
RUN: not llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix.yaml --rfilter=* 2>&1 | FileCheck %s --check-prefix=ERROR
78

89
; CHECK-LABEL: Instruction Count
@@ -16,10 +17,16 @@ RUN: not llvm-remarkutil instruction-mix --parser=yaml %p/Inputs/instruction-mix
1617
; CSV-NEXT: add,15
1718
; CSV-NEXT: nop,10
1819

19-
; MEOW: Instruction Count
20-
; MEOW-NEXT: ----------- -----
21-
; MEOW-NEXT: mul 15
22-
; MEOW-NEXT: add 12
23-
; MEOW-NEXT: nop 9
20+
; MEOW-RE: Instruction Count
21+
; MEOW-RE-NEXT: ----------- -----
22+
; MEOW-RE-NEXT: mul 15
23+
; MEOW-RE-NEXT: add 12
24+
; MEOW-RE-NEXT: nop 9
25+
26+
; MEOW-EXACT: Instruction Count
27+
; MEOW-EXACT-NEXT: ----------- -----
28+
; MEOW-EXACT-NEXT: mul 9
29+
; MEOW-EXACT-NEXT: add 8
30+
; MEOW-EXACT-NEXT: nop 7
2431

2532
; ERROR: error: invalid argument '--rfilter=*': repetition-operator operand invalid

llvm/tools/llvm-remarkutil/RemarkCounter.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ static unsigned getValForKey(StringRef Key, const Remark &Remark) {
112112
}
113113

114114
Error Filters::regexArgumentsValid() {
115-
if (RemarkNameFilter && RemarkNameFilter->IsRegex)
116-
if (auto E = checkRegex(RemarkNameFilter->FilterRE))
115+
if (RemarkNameFilter)
116+
if (auto E = RemarkNameFilter->isValid())
117117
return E;
118-
if (PassNameFilter && PassNameFilter->IsRegex)
119-
if (auto E = checkRegex(PassNameFilter->FilterRE))
118+
if (PassNameFilter)
119+
if (auto E = PassNameFilter->isValid())
120120
return E;
121-
if (ArgFilter && ArgFilter->IsRegex)
122-
if (auto E = checkRegex(ArgFilter->FilterRE))
121+
if (ArgFilter)
122+
if (auto E = ArgFilter->isValid())
123123
return E;
124124
return Error::success();
125125
}
@@ -254,19 +254,19 @@ Expected<Filters> getRemarkFilter() {
254254
std::optional<FilterMatcher> RemarkArgFilter;
255255
std::optional<Type> RemarkType;
256256
if (!RemarkNameOpt.empty())
257-
RemarkNameFilter = {RemarkNameOpt, false};
257+
RemarkNameFilter = {RemarkNameOpt, "remark-name", false};
258258
else if (!RemarkNameOptRE.empty())
259-
RemarkNameFilter = {RemarkNameOptRE, true};
259+
RemarkNameFilter = {RemarkNameOptRE, "rremark-name", true};
260260
if (!PassNameOpt.empty())
261-
PassNameFilter = {PassNameOpt, false};
261+
PassNameFilter = {PassNameOpt, "pass-name", false};
262262
else if (!PassNameOptRE.empty())
263-
PassNameFilter = {PassNameOptRE, true};
263+
PassNameFilter = {PassNameOptRE, "rpass-name", true};
264264
if (RemarkTypeOpt != Type::Failure)
265265
RemarkType = RemarkTypeOpt;
266266
if (!RemarkFilterArgByOpt.empty())
267-
RemarkArgFilter = {RemarkFilterArgByOpt, false};
267+
RemarkArgFilter = {RemarkFilterArgByOpt, "filter-arg-by", false};
268268
else if (!RemarkArgFilterOptRE.empty())
269-
RemarkArgFilter = {RemarkArgFilterOptRE, true};
269+
RemarkArgFilter = {RemarkArgFilterOptRE, "rfilter-arg-by", true};
270270
// Create RemarkFilter.
271271
return Filters::createRemarkFilter(std::move(RemarkNameFilter),
272272
std::move(PassNameFilter),
@@ -313,12 +313,12 @@ static Error collectRemarks() {
313313
SmallVector<FilterMatcher, 4> ArgumentsVector;
314314
if (!Keys.empty()) {
315315
for (auto &Key : Keys)
316-
ArgumentsVector.push_back({Key, false});
316+
ArgumentsVector.push_back({Key, "count-by", false});
317317
} else if (!RKeys.empty())
318318
for (auto Key : RKeys)
319-
ArgumentsVector.push_back({Key, true});
319+
ArgumentsVector.push_back({Key, "count-by", true});
320320
else
321-
ArgumentsVector.push_back({".*", true});
321+
ArgumentsVector.push_back({".*", "count-by", true});
322322

323323
Expected<ArgumentCounter> AC = ArgumentCounter::createArgumentCounter(
324324
GroupByOpt, ArgumentsVector, Buffer, Filter);

llvm/tools/llvm-remarkutil/RemarkCounter.h

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,6 @@ inline std::string groupByToStr(GroupBy GroupBy) {
4545
}
4646
}
4747

48-
/// Filter object which can be either a string or a regex to match with the
49-
/// remark properties.
50-
struct FilterMatcher {
51-
Regex FilterRE;
52-
std::string FilterStr;
53-
bool IsRegex;
54-
FilterMatcher(std::string Filter, bool IsRegex) : IsRegex(IsRegex) {
55-
if (IsRegex)
56-
FilterRE = Regex(Filter);
57-
else
58-
FilterStr = Filter;
59-
}
60-
61-
bool match(StringRef StringToMatch) const {
62-
if (IsRegex)
63-
return FilterRE.match(StringToMatch);
64-
return FilterStr == StringToMatch.trim().str();
65-
}
66-
};
67-
6848
/// Filter out remarks based on remark properties based on name, pass name,
6949
/// argument and type.
7050
struct Filters {
@@ -160,12 +140,9 @@ struct ArgumentCounter : Counter {
160140
StringRef Buffer, Filters &Filter) {
161141
ArgumentCounter AC;
162142
AC.Group = Group;
163-
for (auto &Arg : Arguments) {
164-
if (Arg.IsRegex) {
165-
if (auto E = checkRegex(Arg.FilterRE))
166-
return std::move(E);
167-
}
168-
}
143+
for (auto &Arg : Arguments)
144+
if (auto E = Arg.isValid())
145+
return E;
169146
if (auto E = AC.getAllMatchingArgumentsInRemark(Buffer, Arguments, Filter))
170147
return std::move(E);
171148
return AC;

llvm/tools/llvm-remarkutil/RemarkInstructionMix.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ static cl::SubCommand
1919
"Instruction Mix (requires asm-printer remarks)");
2020

2121
static cl::opt<std::string>
22-
FunctionFilterRE("rfilter", cl::sub(InstructionMix), cl::init(".*"),
23-
cl::ValueOptional,
22+
FunctionFilter("filter", cl::sub(InstructionMix), cl::ValueOptional,
23+
cl::desc("Optional function name to filter collection by"));
24+
25+
static cl::opt<std::string>
26+
FunctionFilterRE("rfilter", cl::sub(InstructionMix), cl::ValueOptional,
2427
cl::desc("Optional function name to filter collection by "
2528
"(accepts regular expressions)"));
2629

@@ -35,6 +38,14 @@ static cl::opt<ReportStyleOptions> ReportStyle(
3538
INPUT_FORMAT_COMMAND_LINE_OPTIONS(InstructionMix)
3639
INPUT_OUTPUT_COMMAND_LINE_OPTIONS(InstructionMix)
3740

41+
static FilterMatcher getRemarkFilter() {
42+
if (FunctionFilter.getNumOccurrences())
43+
return {FunctionFilter, "filter", false};
44+
if (FunctionFilterRE.getNumOccurrences())
45+
return {FunctionFilterRE, "rfilter", true};
46+
return {".*", "<implicit>", true};
47+
}
48+
3849
static Error tryInstructionMix() {
3950
auto MaybeOF =
4051
getOutputFileWithFlags(OutputFileName, sys::fs::OF_TextWithCRLF);
@@ -49,11 +60,9 @@ static Error tryInstructionMix() {
4960
if (!MaybeParser)
5061
return MaybeParser.takeError();
5162

52-
Regex Filter(FunctionFilterRE);
53-
std::string Error;
54-
if (!Filter.isValid(Error))
55-
return createStringError(make_error_code(std::errc::invalid_argument),
56-
Twine("invalid argument '--rfilter=") + FunctionFilterRE + "': " + Error);
63+
FilterMatcher Filter = getRemarkFilter();
64+
if (auto E = Filter.isValid())
65+
return E;
5766

5867
// Collect the histogram of instruction counts.
5968
std::unordered_map<std::string, unsigned> Histogram;

llvm/tools/llvm-remarkutil/RemarkUtilHelpers.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Support/Error.h"
1919
#include "llvm/Support/FileSystem.h"
2020
#include "llvm/Support/MemoryBuffer.h"
21+
#include "llvm/Support/Regex.h"
2122
#include "llvm/Support/ToolOutputFile.h"
2223

2324
// Keep input + output help + names consistent across the various modes via a
@@ -55,5 +56,37 @@ Expected<std::unique_ptr<ToolOutputFile>>
5556
getOutputFileWithFlags(StringRef OutputFileName, sys::fs::OpenFlags Flags);
5657
Expected<std::unique_ptr<ToolOutputFile>>
5758
getOutputFileForRemarks(StringRef OutputFileName, Format OutputFormat);
59+
60+
/// Filter object which can be either a string or a regex to match with the
61+
/// remark properties.
62+
class FilterMatcher {
63+
Regex FilterRE;
64+
std::string FilterStr;
65+
std::string Argument;
66+
bool IsRegex;
67+
68+
public:
69+
FilterMatcher(StringRef Filter, StringRef Argument, bool IsRegex)
70+
: FilterRE(Filter), FilterStr(Filter), Argument(Argument),
71+
IsRegex(IsRegex) {}
72+
73+
bool match(StringRef StringToMatch) const {
74+
if (IsRegex)
75+
return FilterRE.match(StringToMatch);
76+
return FilterStr == StringToMatch.trim().str();
77+
}
78+
79+
Error isValid() const {
80+
if (!IsRegex)
81+
return Error::success();
82+
std::string Error;
83+
if (FilterRE.isValid(Error))
84+
return Error::success();
85+
return createStringError(make_error_code(std::errc::invalid_argument),
86+
"invalid argument '--" + Argument + "=" +
87+
FilterStr + "': " + Error);
88+
}
89+
};
90+
5891
} // namespace remarks
5992
} // namespace llvm

0 commit comments

Comments
 (0)