[Mlir-commits] [mlir] [MLIR] Enable multi-buffer support in loadSourceFileBuffer (PR #187803)

Vinicius Silva llvmlistbot at llvm.org
Fri Mar 20 14:47:19 PDT 2026


https://github.com/viniciusfdasilva created https://github.com/llvm/llvm-project/pull/187803

This PR implements multi-buffer support in the `loadSourceFileBuffer` utility, transitioning the infrastructure from a single-input constraint to a system capable of managing multiple source files within a single `llvm::SourceMgr`.

* Removed the single-buffer constraint: Deleted the check that limited `loadSourceFileBuffer` to a single active buffer.
* Implemented `SMLoc` support: Added an optional `includeLoc` parameter, enabling the `SourceMgr` to track and display *"included from"* stack traces for nested files.
* Enhanced error reporting: Refactored the error handling to include system-level error messages (`std::error_code`), providing clearer diagnostics for file I/O failures.

>From 1c15966afafe490138ccd2a814206496812753f0 Mon Sep 17 00:00:00 2001
From: Vinicius da Silva <viniciusilva at ieee.org>
Date: Fri, 20 Mar 2026 18:43:32 -0300
Subject: [PATCH] [MLIR] Enable multi-buffer support in loadSourceFileBuffer

---
 mlir/lib/Parser/Parser.cpp | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

diff --git a/mlir/lib/Parser/Parser.cpp b/mlir/lib/Parser/Parser.cpp
index 310680bcfb34f..37dcc9b261a3c 100644
--- a/mlir/lib/Parser/Parser.cpp
+++ b/mlir/lib/Parser/Parser.cpp
@@ -73,19 +73,17 @@ LogicalResult mlir::parseSourceFile(llvm::StringRef filename, Block *block,
 
 static LogicalResult loadSourceFileBuffer(llvm::StringRef filename,
                                           llvm::SourceMgr &sourceMgr,
-                                          MLIRContext *ctx) {
-  if (sourceMgr.getNumBuffers() != 0) {
-    // TODO: Extend to support multiple buffers.
-    return emitError(mlir::UnknownLoc::get(ctx),
-                     "only main buffer parsed at the moment");
-  }
+                                          MLIRContext *ctx,
+                                          SMLoc includeLoc = SMLoc()) {
+
   auto fileOrErr = llvm::MemoryBuffer::getFileOrSTDIN(filename);
-  if (fileOrErr.getError())
+  if (std::error_code ec = fileOrErr.getError()) {
     return emitError(mlir::UnknownLoc::get(ctx),
-                     "could not open input file " + filename);
+                     "could not open input file '" + filename + "': " + ec.message());
+  }
 
-  // Load the MLIR source file.
-  sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
+  sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), includeLoc);
+  
   return success();
 }
 



More information about the Mlir-commits mailing list