Continue wiring up diagnostic reporting infrastructure, still WIP.
 - Implement a diagnostic hook in one of the paths in mlir-opt which
   captures and reports the diagnostics nicely.
 - Have the parser capture simple location information from the parser
   indicating where each op came from in the source .mlir file.
 - Add a verifyDominance() method to MLFuncVerifier to demo this, resolving b/112086163
 - Add some PrettyStackTrace handlers to make crashes in the testsuite easier
   to track down.

PiperOrigin-RevId: 207488548
diff --git a/lib/Parser/Parser.cpp b/lib/Parser/Parser.cpp
index c77f0f4..ff3811f 100644
--- a/lib/Parser/Parser.cpp
+++ b/lib/Parser/Parser.cpp
@@ -105,6 +105,7 @@
   MLIRContext *getContext() const { return state.context; }
   Module *getModule() { return state.module; }
   OperationSet &getOperationSet() const { return state.operationSet; }
+  llvm::SourceMgr &getSourceMgr() { return state.lex.getSourceMgr(); }
 
   /// Return the current token the parser is inspecting.
   const Token &getToken() const { return state.curToken; }
@@ -1553,6 +1554,19 @@
   if (!op)
     return ParseFailure;
 
+  // Apply location information to the instruction.
+  // TODO(clattner): make this more principled.  We shouldn't overwrite existing
+  // location info, we should use a better serialized form, and we shouldn't
+  // be using the :location attribute.  This is also pretty inefficient.
+  {
+    auto &sourceMgr = getSourceMgr();
+    auto fileID = sourceMgr.FindBufferContainingLoc(loc);
+    auto *srcBuffer = sourceMgr.getMemoryBuffer(fileID);
+    unsigned locationEncoding = loc.getPointer() - srcBuffer->getBufferStart();
+    op->setAttr(builder.getIdentifier(":location"),
+                builder.getIntegerAttr(locationEncoding));
+  }
+
   // We just parsed an operation.  If it is a recognized one, verify that it
   // is structurally as we expect.  If not, produce an error with a reasonable
   // source location.