[Mlir-commits] [mlir] 861d69a - [mlir] Speed up Lexer::getEncodedSourceLocation

River Riddle llvmlistbot at llvm.org
Tue May 18 17:11:09 PDT 2021


Author: River Riddle
Date: 2021-05-18T17:11:01-07:00
New Revision: 861d69a5259653f60d59795597493a7939b794fe

URL: https://github.com/llvm/llvm-project/commit/861d69a5259653f60d59795597493a7939b794fe
DIFF: https://github.com/llvm/llvm-project/commit/861d69a5259653f60d59795597493a7939b794fe.diff

LOG: [mlir] Speed up Lexer::getEncodedSourceLocation

We currently use SourceMgr::getLineAndColumn to get the line and column for an SMLoc, but this includes a call to StringRef::find_last_of that ends up dominating compile time. In D102567, we start creating locations from the input file for block arguments which resulted in an extreme performance regression for modules with very large amounts of block arguments. This revision switches to just using a pointer offset from the beginning of the line to calculate the column(all MLIR files are simple ascii), resulting in a compile time reduction from 4700 seconds (1 hour and 18 minutes) to 8 seconds.

Differential Revision: https://reviews.llvm.org/D102734

Added: 
    

Modified: 
    mlir/lib/Parser/Lexer.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Parser/Lexer.cpp b/mlir/lib/Parser/Lexer.cpp
index 763f154687ccc..a8ecc7162dc6a 100644
--- a/mlir/lib/Parser/Lexer.cpp
+++ b/mlir/lib/Parser/Lexer.cpp
@@ -41,11 +41,14 @@ Lexer::Lexer(const llvm::SourceMgr &sourceMgr, MLIRContext *context)
 Location Lexer::getEncodedSourceLocation(llvm::SMLoc loc) {
   auto &sourceMgr = getSourceMgr();
   unsigned mainFileID = sourceMgr.getMainFileID();
-  auto lineAndColumn = sourceMgr.getLineAndColumn(loc, mainFileID);
+  auto &bufferInfo = sourceMgr.getBufferInfo(mainFileID);
+  unsigned lineNo = bufferInfo.getLineNumber(loc.getPointer());
+  unsigned column =
+      (loc.getPointer() - bufferInfo.getPointerForLineNumber(lineNo)) + 1;
   auto *buffer = sourceMgr.getMemoryBuffer(mainFileID);
 
-  return FileLineColLoc::get(context, buffer->getBufferIdentifier(),
-                             lineAndColumn.first, lineAndColumn.second);
+  return FileLineColLoc::get(context, buffer->getBufferIdentifier(), lineNo,
+                             column);
 }
 
 /// emitError - Emit an error message and return an Token::error token.


        


More information about the Mlir-commits mailing list