[clang] [CIR] Add MLIR diagnostic handler ahead of verification (PR #165838)

Morris Hafner via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 31 01:39:05 PDT 2025


https://github.com/mmha created https://github.com/llvm/llvm-project/pull/165838

This enables clang to print out diagnostics that point to both the C++ source in the error and the incorrect CIR in the notes.

>From ef62e9eabc4ad0a4586e2ab3af6021da84114ff8 Mon Sep 17 00:00:00 2001
From: Morris Hafner <mhafner at nvidia.com>
Date: Fri, 31 Oct 2025 14:07:45 +0800
Subject: [PATCH] [CIR] Add MLIR diagnostic handler ahead of verification

This enables clang to print out diagnostics that point to both the C++ source in the error and the incorrect CIR in the notes.
---
 clang/lib/CIR/FrontendAction/CIRGenAction.cpp | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
index 67bb5657d4001..ae244d785631b 100644
--- a/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
+++ b/clang/lib/CIR/FrontendAction/CIRGenAction.cpp
@@ -95,6 +95,18 @@ class CIRGenConsumer : public clang::ASTConsumer {
   void HandleTranslationUnit(ASTContext &C) override {
     Gen->HandleTranslationUnit(C);
 
+    mlir::ModuleOp MlirModule = Gen->getModule();
+    mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
+
+    SourceManager &ClangSourceMgr = C.getSourceManager();
+    FileID MainFileID = ClangSourceMgr.getMainFileID();
+    std::unique_ptr<llvm::MemoryBuffer> FileBuf =
+        llvm::MemoryBuffer::getMemBuffer(
+            ClangSourceMgr.getBufferOrFake(MainFileID));
+    llvm::SourceMgr MlirSourceMgr;
+    MlirSourceMgr.AddNewSourceBuffer(std::move(FileBuf), llvm::SMLoc());
+    mlir::SourceMgrDiagnosticHandler DiagnosticHandler(MlirSourceMgr, &MlirCtx);
+
     if (!FEOptions.ClangIRDisableCIRVerifier) {
       if (!Gen->verifyModule()) {
         CI.getDiagnostics().Report(
@@ -105,9 +117,6 @@ class CIRGenConsumer : public clang::ASTConsumer {
       }
     }
 
-    mlir::ModuleOp MlirModule = Gen->getModule();
-    mlir::MLIRContext &MlirCtx = Gen->getMLIRContext();
-
     if (!FEOptions.ClangIRDisablePasses) {
       // Setup and run CIR pipeline.
       if (runCIRToCIRPasses(MlirModule, MlirCtx, C,



More information about the cfe-commits mailing list