[Mlir-commits] [mlir] 3b43226 - [Reland] [mlir] Speed up Lexer::getEncodedSourceLocation
River Riddle
llvmlistbot at llvm.org
Wed May 19 12:57:31 PDT 2021
Author: River Riddle
Date: 2021-05-19T12:57:18-07:00
New Revision: 3b43226032e3b1ce866c9a3aaf7eaa680fd2252a
URL: https://github.com/llvm/llvm-project/commit/3b43226032e3b1ce866c9a3aaf7eaa680fd2252a
DIFF: https://github.com/llvm/llvm-project/commit/3b43226032e3b1ce866c9a3aaf7eaa680fd2252a.diff
LOG: [Reland] [mlir] Speed up Lexer::getEncodedSourceLocation
Reland Note: This was accidentally reverted in 80d981eda69f1ada6d944ed89571456cad13b850, but is an important improvement even outside of the driving motivator in D102567.
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.
Added:
Modified:
mlir/lib/Parser/Lexer.cpp
Removed:
################################################################################
diff --git a/mlir/lib/Parser/Lexer.cpp b/mlir/lib/Parser/Lexer.cpp
index 763f154687ccc..129fb3e500c6f 100644
--- a/mlir/lib/Parser/Lexer.cpp
+++ b/mlir/lib/Parser/Lexer.cpp
@@ -41,11 +41,17 @@ 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);
+
+ // TODO: Fix performance issues in SourceMgr::getLineAndColumn so that we can
+ // use it here.
+ 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