Skip to content

Commit 2d1ff44

Browse files
committed
[mlir] implement verify-only-specified-diagnostics
1 parent adba14a commit 2d1ff44

File tree

6 files changed

+75
-17
lines changed

6 files changed

+75
-17
lines changed

mlir/examples/transform-opt/mlir-transform-opt.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ struct MlirTransformOptCLOptions {
4343
cl::desc("Check that emitted diagnostics match expected-* lines "
4444
"on the corresponding line"),
4545
cl::init(false)};
46+
cl::opt<bool> verifyOnlySpecifiedDiagnostics{
47+
"verify-only-specified-diagnostics",
48+
cl::desc("Check that emitted diagnostics match only specified expected-* "
49+
"lines "
50+
"on the corresponding line"),
51+
cl::init(false)};
4652

4753
cl::opt<std::string> payloadFilename{cl::Positional, cl::desc("<input file>"),
4854
cl::init("-")};
@@ -103,11 +109,13 @@ class DiagnosticHandlerWrapper {
103109
/// Constructs the diagnostic handler of the specified kind of the given
104110
/// source manager and context.
105111
DiagnosticHandlerWrapper(Kind kind, llvm::SourceMgr &mgr,
106-
mlir::MLIRContext *context) {
112+
mlir::MLIRContext *context,
113+
bool verifyOnlyExpectedDiagnostics = false) {
107114
if (kind == Kind::EmitDiagnostics)
108115
handler = new mlir::SourceMgrDiagnosticHandler(mgr, context);
109116
else
110-
handler = new mlir::SourceMgrDiagnosticVerifierHandler(mgr, context);
117+
handler = new mlir::SourceMgrDiagnosticVerifierHandler(
118+
mgr, context, verifyOnlyExpectedDiagnostics);
111119
}
112120

113121
/// This object is non-copyable but movable.
@@ -150,8 +158,10 @@ class TransformSourceMgr {
150158
public:
151159
/// Constructs the source manager indicating whether diagnostic messages will
152160
/// be verified later on.
153-
explicit TransformSourceMgr(bool verifyDiagnostics)
154-
: verifyDiagnostics(verifyDiagnostics) {}
161+
explicit TransformSourceMgr(bool verifyDiagnostics,
162+
bool verifyOnlyExpectedDiagnostics)
163+
: verifyDiagnostics(verifyDiagnostics),
164+
verifyOnlyExpectedDiagnostics(verifyOnlyExpectedDiagnostics) {}
155165

156166
/// Deconstructs the source manager. Note that `checkResults` must have been
157167
/// called on this instance before deconstructing it.
@@ -179,7 +189,8 @@ class TransformSourceMgr {
179189
// verification needs to happen and store it.
180190
if (verifyDiagnostics) {
181191
diagHandlers.emplace_back(
182-
DiagnosticHandlerWrapper::Kind::VerifyDiagnostics, mgr, &context);
192+
DiagnosticHandlerWrapper::Kind::VerifyDiagnostics, mgr, &context,
193+
verifyOnlyExpectedDiagnostics);
183194
} else {
184195
diagHandlers.emplace_back(DiagnosticHandlerWrapper::Kind::EmitDiagnostics,
185196
mgr, &context);
@@ -205,6 +216,9 @@ class TransformSourceMgr {
205216
private:
206217
/// Indicates whether diagnostic message verification is requested.
207218
const bool verifyDiagnostics;
219+
/// Indicates whether *only specified* diagnostic message verification is
220+
/// requested.
221+
const bool verifyOnlyExpectedDiagnostics;
208222

209223
/// Indicates that diagnostic message verification has taken place, and the
210224
/// deconstruction is therefore safe.
@@ -248,7 +262,8 @@ static llvm::LogicalResult processPayloadBuffer(
248262
context.allowUnregisteredDialects(clOptions->allowUnregisteredDialects);
249263
mlir::ParserConfig config(&context);
250264
TransformSourceMgr sourceMgr(
251-
/*verifyDiagnostics=*/clOptions->verifyDiagnostics);
265+
/*verifyDiagnostics=*/clOptions->verifyDiagnostics,
266+
clOptions->verifyOnlySpecifiedDiagnostics);
252267

253268
// Parse the input buffer that will be used as transform payload.
254269
mlir::OwningOpRef<mlir::Operation *> payloadRoot =

mlir/include/mlir/IR/Diagnostics.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,12 @@ struct SourceMgrDiagnosticVerifierHandlerImpl;
626626
/// corresponding line of the source file.
627627
class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler {
628628
public:
629-
SourceMgrDiagnosticVerifierHandler(llvm::SourceMgr &srcMgr, MLIRContext *ctx,
630-
raw_ostream &out);
631-
SourceMgrDiagnosticVerifierHandler(llvm::SourceMgr &srcMgr, MLIRContext *ctx);
629+
SourceMgrDiagnosticVerifierHandler(
630+
llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out,
631+
bool verifyOnlySpecifiedDiagnostics = false);
632+
SourceMgrDiagnosticVerifierHandler(
633+
llvm::SourceMgr &srcMgr, MLIRContext *ctx,
634+
bool verifyOnlySpecifiedDiagnostics = false);
632635
~SourceMgrDiagnosticVerifierHandler();
633636

634637
/// Returns the status of the handler and verifies that all expected
@@ -644,6 +647,10 @@ class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler {
644647
void process(FileLineColLoc loc, StringRef msg, DiagnosticSeverity kind);
645648

646649
std::unique_ptr<detail::SourceMgrDiagnosticVerifierHandlerImpl> impl;
650+
651+
/// Set whether to check that emitted diagnostics match *only specified*
652+
/// `expected-*` lines on the corresponding line.
653+
bool verifyOnlySpecifiedDiagnostics = false;
647654
};
648655

649656
//===----------------------------------------------------------------------===//

mlir/include/mlir/Tools/mlir-opt/MlirOptMain.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,16 @@ class MlirOptMainConfig {
191191
}
192192
bool shouldVerifyDiagnostics() const { return verifyDiagnosticsFlag; }
193193

194+
/// Set whether to check that emitted diagnostics match *only specified*
195+
/// `expected-*` lines on the corresponding line.
196+
MlirOptMainConfig &verifyOnlySpecifiedDiagnostics(bool verify) {
197+
verifyOnlySpecifiedDiagnosticsFlag = verify;
198+
return *this;
199+
}
200+
bool shouldverifyOnlySpecifiedDiagnostics() const {
201+
return verifyOnlySpecifiedDiagnosticsFlag;
202+
}
203+
194204
/// Set whether to run the verifier after each transformation pass.
195205
MlirOptMainConfig &verifyPasses(bool verify) {
196206
verifyPassesFlag = verify;
@@ -280,6 +290,9 @@ class MlirOptMainConfig {
280290
/// Set whether to check that emitted diagnostics match `expected-*` lines on
281291
/// the corresponding line. This is meant for implementing diagnostic tests.
282292
bool verifyDiagnosticsFlag = false;
293+
/// Set whether to check that emitted diagnostics match *only specified*
294+
/// `expected-*` lines on the corresponding line.
295+
bool verifyOnlySpecifiedDiagnosticsFlag = false;
283296

284297
/// Run the verifier after each transformation pass.
285298
bool verifyPassesFlag = true;

mlir/lib/IR/Diagnostics.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -795,9 +795,11 @@ SourceMgrDiagnosticVerifierHandlerImpl::computeExpectedDiags(
795795
}
796796

797797
SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
798-
llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out)
798+
llvm::SourceMgr &srcMgr, MLIRContext *ctx, raw_ostream &out,
799+
bool verifyOnlySpecifiedDiagnostics)
799800
: SourceMgrDiagnosticHandler(srcMgr, ctx, out),
800-
impl(new SourceMgrDiagnosticVerifierHandlerImpl()) {
801+
impl(new SourceMgrDiagnosticVerifierHandlerImpl()),
802+
verifyOnlySpecifiedDiagnostics(verifyOnlySpecifiedDiagnostics) {
801803
// Compute the expected diagnostics for each of the current files in the
802804
// source manager.
803805
for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i)
@@ -815,8 +817,10 @@ SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
815817
}
816818

817819
SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
818-
llvm::SourceMgr &srcMgr, MLIRContext *ctx)
819-
: SourceMgrDiagnosticVerifierHandler(srcMgr, ctx, llvm::errs()) {}
820+
llvm::SourceMgr &srcMgr, MLIRContext *ctx,
821+
bool verifyOnlySpecifiedDiagnostics)
822+
: SourceMgrDiagnosticVerifierHandler(srcMgr, ctx, llvm::errs(),
823+
verifyOnlySpecifiedDiagnostics) {}
820824

821825
SourceMgrDiagnosticVerifierHandler::~SourceMgrDiagnosticVerifierHandler() {
822826
// Ensure that all expected diagnostics were handled.
@@ -886,6 +890,9 @@ void SourceMgrDiagnosticVerifierHandler::process(FileLineColLoc loc,
886890
}
887891
}
888892

893+
if (verifyOnlySpecifiedDiagnostics)
894+
return;
895+
889896
// Otherwise, emit an error for the near miss.
890897
if (nearMiss)
891898
mgr.PrintMessage(os, nearMiss->fileLoc, llvm::SourceMgr::DK_Error,

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ struct MlirOptMainConfigCLOptions : public MlirOptMainConfig {
173173
cl::desc("Check that emitted diagnostics match "
174174
"expected-* lines on the corresponding line"),
175175
cl::location(verifyDiagnosticsFlag), cl::init(false));
176+
static cl::opt<bool> verifyOnlySpecifiedDiagnostics{
177+
"verify-only-specified-diagnostics",
178+
cl::desc(
179+
"Check that emitted diagnostics match only specified expected-* "
180+
"lines "
181+
"on the corresponding line"),
182+
cl::location(verifyOnlySpecifiedDiagnosticsFlag), cl::init(false)};
176183

177184
static cl::opt<bool, /*ExternalStorage=*/true> verifyPasses(
178185
"verify-each",
@@ -542,7 +549,8 @@ static LogicalResult processBuffer(raw_ostream &os,
542549
return performActions(os, sourceMgr, &context, config);
543550
}
544551

545-
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(*sourceMgr, &context);
552+
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(
553+
*sourceMgr, &context, config.shouldverifyOnlySpecifiedDiagnostics());
546554

547555
// Do any processing requested by command line flags. We don't care whether
548556
// these actions succeed or fail, we only care what diagnostics they produce

mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
5858

5959
static llvm::cl::opt<bool> allowUnregisteredDialects(
6060
"allow-unregistered-dialect",
61-
llvm::cl::desc("Allow operation with no registered dialects (discouraged: testing only!)"),
61+
llvm::cl::desc("Allow operation with no registered dialects "
62+
"(discouraged: testing only!)"),
6263
llvm::cl::init(false));
6364

6465
static llvm::cl::opt<std::string> inputSplitMarker{
@@ -77,6 +78,13 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
7778
llvm::cl::desc("Check that emitted diagnostics match "
7879
"expected-* lines on the corresponding line"),
7980
llvm::cl::init(false));
81+
static llvm::cl::opt<bool> verifyOnlySpecifiedDiagnostics{
82+
"verify-only-specified-diagnostics",
83+
llvm::cl::desc(
84+
"Check that emitted diagnostics match only specified expected-* "
85+
"lines "
86+
"on the corresponding line"),
87+
llvm::cl::init(false)};
8088

8189
static llvm::cl::opt<bool> errorDiagnosticsOnly(
8290
"error-diagnostics-only",
@@ -158,8 +166,8 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
158166
// translation failed (in most cases, it is expected to fail) and we do
159167
// not filter non-error diagnostics even if `errorDiagnosticsOnly` is
160168
// set. Instead, we check if the diagnostics were produced as expected.
161-
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(*sourceMgr,
162-
&context);
169+
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(
170+
*sourceMgr, &context, verifyOnlySpecifiedDiagnostics);
163171
(void)(*translationRequested)(sourceMgr, os, &context);
164172
result = sourceMgrHandler.verify();
165173
} else if (errorDiagnosticsOnly) {

0 commit comments

Comments
 (0)