@@ -508,20 +508,29 @@ performActions(raw_ostream &os,
508508
509509// / Parses the memory buffer. If successfully, run a series of passes against
510510// / it and print the result.
511- static LogicalResult processBuffer (raw_ostream &os,
512- std::unique_ptr<MemoryBuffer> ownedBuffer,
513- const MlirOptMainConfig &config,
514- DialectRegistry ®istry,
515- llvm::ThreadPoolInterface *threadPool) {
511+ static LogicalResult
512+ processBuffer (raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
513+ llvm::MemoryBufferRef sourceBuffer,
514+ const MlirOptMainConfig &config, DialectRegistry ®istry,
515+ SourceMgrDiagnosticVerifierHandler *verifyHandler,
516+ llvm::ThreadPoolInterface *threadPool) {
516517 // Tell sourceMgr about this buffer, which is what the parser will pick up.
517518 auto sourceMgr = std::make_shared<SourceMgr>();
519+ // Add the original buffer to the source manager to use for determining
520+ // locations.
521+ sourceMgr->AddNewSourceBuffer (
522+ llvm::MemoryBuffer::getMemBuffer (sourceBuffer,
523+ /* RequiresNullTerminator=*/ false ),
524+ SMLoc ());
518525 sourceMgr->AddNewSourceBuffer (std::move (ownedBuffer), SMLoc ());
519526
520527 // Create a context just for the current buffer. Disable threading on creation
521528 // since we'll inject the thread-pool separately.
522529 MLIRContext context (registry, MLIRContext::Threading::DISABLED);
523530 if (threadPool)
524531 context.setThreadPool (*threadPool);
532+ if (verifyHandler)
533+ verifyHandler->registerInContext (&context);
525534
526535 StringRef irdlFile = config.getIrdlFile ();
527536 if (!irdlFile.empty () && failed (loadIRDLDialects (irdlFile, context)))
@@ -545,17 +554,12 @@ static LogicalResult processBuffer(raw_ostream &os,
545554 return performActions (os, sourceMgr, &context, config);
546555 }
547556
548- SourceMgrDiagnosticVerifierHandler sourceMgrHandler (
549- *sourceMgr, &context, config.verifyDiagnosticsLevel ());
550-
551557 // Do any processing requested by command line flags. We don't care whether
552558 // these actions succeed or fail, we only care what diagnostics they produce
553559 // and whether they match our expectations.
554560 (void )performActions (os, sourceMgr, &context, config);
555561
556- // Verify the diagnostic handler to make sure that each of the diagnostics
557- // matched.
558- return sourceMgrHandler.verify ();
562+ return success ();
559563}
560564
561565std::pair<std::string, std::string>
@@ -624,14 +628,31 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
624628 if (threadPoolCtx.isMultithreadingEnabled ())
625629 threadPool = &threadPoolCtx.getThreadPool ();
626630
631+ SourceMgr sourceMgr;
632+ sourceMgr.AddNewSourceBuffer (
633+ llvm::MemoryBuffer::getMemBuffer (buffer->getMemBufferRef (),
634+ /* RequiresNullTerminator=*/ false ),
635+ SMLoc ());
636+ // Note: this creates a verifier handler independent of the the flag set, as
637+ // internally if the flag is not set, a new scoped diagnostic handler is
638+ // created which would intercept the diagnostics and verify them.
639+ SourceMgrDiagnosticVerifierHandler sourceMgrHandler (
640+ sourceMgr, &threadPoolCtx, config.verifyDiagnosticsLevel ());
627641 auto chunkFn = [&](std::unique_ptr<MemoryBuffer> chunkBuffer,
628- raw_ostream &os) {
629- return processBuffer (os, std::move (chunkBuffer), config, registry,
630- threadPool);
642+ llvm::MemoryBufferRef sourceBuffer, raw_ostream &os) {
643+ return processBuffer (
644+ os, std::move (chunkBuffer), sourceBuffer, config, registry,
645+ config.shouldVerifyDiagnostics () ? &sourceMgrHandler : nullptr ,
646+ threadPool);
631647 };
632- return splitAndProcessBuffer (std::move (buffer), chunkFn, outputStream,
633- config.inputSplitMarker (),
634- config.outputSplitMarker ());
648+ LogicalResult status = splitAndProcessBuffer (
649+ llvm::MemoryBuffer::getMemBuffer (buffer->getMemBufferRef (),
650+ /* RequiresNullTerminator=*/ false ),
651+ chunkFn, outputStream, config.inputSplitMarker (),
652+ config.outputSplitMarker ());
653+ if (config.shouldVerifyDiagnostics () && failed (sourceMgrHandler.verify ()))
654+ status = failure ();
655+ return status;
635656}
636657
637658LogicalResult mlir::MlirOptMain (int argc, char **argv,
0 commit comments