@@ -508,20 +508,29 @@ performActions(raw_ostream &os,
508
508
509
509
// / Parses the memory buffer. If successfully, run a series of passes against
510
510
// / 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) {
516
517
// Tell sourceMgr about this buffer, which is what the parser will pick up.
517
518
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 ());
518
525
sourceMgr->AddNewSourceBuffer (std::move (ownedBuffer), SMLoc ());
519
526
520
527
// Create a context just for the current buffer. Disable threading on creation
521
528
// since we'll inject the thread-pool separately.
522
529
MLIRContext context (registry, MLIRContext::Threading::DISABLED);
523
530
if (threadPool)
524
531
context.setThreadPool (*threadPool);
532
+ if (verifyHandler)
533
+ verifyHandler->registerInContext (&context);
525
534
526
535
StringRef irdlFile = config.getIrdlFile ();
527
536
if (!irdlFile.empty () && failed (loadIRDLDialects (irdlFile, context)))
@@ -545,17 +554,12 @@ static LogicalResult processBuffer(raw_ostream &os,
545
554
return performActions (os, sourceMgr, &context, config);
546
555
}
547
556
548
- SourceMgrDiagnosticVerifierHandler sourceMgrHandler (
549
- *sourceMgr, &context, config.verifyDiagnosticsLevel ());
550
-
551
557
// Do any processing requested by command line flags. We don't care whether
552
558
// these actions succeed or fail, we only care what diagnostics they produce
553
559
// and whether they match our expectations.
554
560
(void )performActions (os, sourceMgr, &context, config);
555
561
556
- // Verify the diagnostic handler to make sure that each of the diagnostics
557
- // matched.
558
- return sourceMgrHandler.verify ();
562
+ return success ();
559
563
}
560
564
561
565
std::pair<std::string, std::string>
@@ -624,14 +628,31 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
624
628
if (threadPoolCtx.isMultithreadingEnabled ())
625
629
threadPool = &threadPoolCtx.getThreadPool ();
626
630
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 ());
627
641
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);
631
647
};
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;
635
656
}
636
657
637
658
LogicalResult mlir::MlirOptMain (int argc, char **argv,
0 commit comments