Skip to content

Commit 969e7ed

Browse files
committed
[SourceMgr/MLIR diagnostics] Introduce a new method to speed things up
Summary: This introduces a new SourceMgr::FindLocForLineAndColumn method that uses the OffsetCache in SourceMgr::SrcBuffer to do do a constant time lookup for the line number (once the cache is populated). Use this method in MLIR's SourceMgrDiagnosticHandler::convertLocToSMLoc, replacing the O(n) scanning logic. This resolves a long standing TODO in MLIR, and makes one of my usecases go dramatically faster (which is currently producing many diagnostics in a 40MB SourceBuffer). NFC, this is just a performance speedup and cleanup. Reviewers: rriddle!, ftynse! Subscribers: hiraditya, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, liufengdb, Joonsoo, grosul1, frgossen, Kayjukh, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D78868
1 parent a0a55b7 commit 969e7ed

File tree

3 files changed

+194
-169
lines changed

3 files changed

+194
-169
lines changed

llvm/include/llvm/Support/SourceMgr.h

+28-27
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ class SourceMgr {
6161
/// into relatively small files (often smaller than 2^8 or 2^16 bytes),
6262
/// we select the offset vector element type dynamically based on the
6363
/// size of Buffer.
64-
using VariableSizeOffsets = PointerUnion<std::vector<uint8_t> *,
65-
std::vector<uint16_t> *,
66-
std::vector<uint32_t> *,
67-
std::vector<uint64_t> *>;
64+
using VariableSizeOffsets =
65+
PointerUnion<std::vector<uint8_t> *, std::vector<uint16_t> *,
66+
std::vector<uint32_t> *, std::vector<uint64_t> *>;
6867

6968
/// Vector of offsets into Buffer at which there are line-endings
7069
/// (lazily populated). Once populated, the '\n' that marks the end of
@@ -74,12 +73,17 @@ class SourceMgr {
7473
/// offset corresponding to a particular SMLoc).
7574
mutable VariableSizeOffsets OffsetCache;
7675

77-
/// Populate \c OffsetCache and look up a given \p Ptr in it, assuming
78-
/// it points somewhere into \c Buffer. The static type parameter \p T
79-
/// must be an unsigned integer type from uint{8,16,32,64}_t large
80-
/// enough to store offsets inside \c Buffer.
81-
template<typename T>
76+
/// Look up a given \p Ptr in in the buffer, determining which line it came
77+
/// from.
8278
unsigned getLineNumber(const char *Ptr) const;
79+
template <typename T>
80+
unsigned getLineNumberSpecialized(const char *Ptr) const;
81+
82+
/// Return a pointer to the first character of the specified line number or
83+
/// null if the line number is invalid.
84+
const char *getPointerForLineNumber(unsigned LineNo) const;
85+
template <typename T>
86+
const char *getPointerForLineNumberSpecialized(unsigned LineNo) const;
8387

8488
/// This is the location of the parent include, or null if at the top level.
8589
SMLoc IncludeLoc;
@@ -134,9 +138,7 @@ class SourceMgr {
134138
return Buffers[i - 1].Buffer.get();
135139
}
136140

137-
unsigned getNumBuffers() const {
138-
return Buffers.size();
139-
}
141+
unsigned getNumBuffers() const { return Buffers.size(); }
140142

141143
unsigned getMainFileID() const {
142144
assert(getNumBuffers());
@@ -184,12 +186,16 @@ class SourceMgr {
184186
std::pair<unsigned, unsigned> getLineAndColumn(SMLoc Loc,
185187
unsigned BufferID = 0) const;
186188

189+
/// Given a line and column number in a mapped buffer, turn it into an SMLoc.
190+
/// This will return a null SMLoc if the line/column location is invalid.
191+
SMLoc FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo,
192+
unsigned ColNo);
193+
187194
/// Emit a message about the specified location with the specified string.
188195
///
189196
/// \param ShowColors Display colored messages if output is a terminal and
190197
/// the default error handler is used.
191-
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind,
192-
const Twine &Msg,
198+
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg,
193199
ArrayRef<SMRange> Ranges = None,
194200
ArrayRef<SMFixIt> FixIts = None,
195201
bool ShowColors = true) const;
@@ -234,13 +240,13 @@ class SMFixIt {
234240
public:
235241
// FIXME: Twine.str() is not very efficient.
236242
SMFixIt(SMLoc Loc, const Twine &Insertion)
237-
: Range(Loc, Loc), Text(Insertion.str()) {
243+
: Range(Loc, Loc), Text(Insertion.str()) {
238244
assert(Loc.isValid());
239245
}
240246

241247
// FIXME: Twine.str() is not very efficient.
242248
SMFixIt(SMRange R, const Twine &Replacement)
243-
: Range(R), Text(Replacement.str()) {
249+
: Range(R), Text(Replacement.str()) {
244250
assert(R.isValid());
245251
}
246252

@@ -274,13 +280,12 @@ class SMDiagnostic {
274280
SMDiagnostic() = default;
275281
// Diagnostic with no location (e.g. file not found, command line arg error).
276282
SMDiagnostic(StringRef filename, SourceMgr::DiagKind Knd, StringRef Msg)
277-
: Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
283+
: Filename(filename), LineNo(-1), ColumnNo(-1), Kind(Knd), Message(Msg) {}
278284

279285
// Diagnostic with a location.
280-
SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN,
281-
int Line, int Col, SourceMgr::DiagKind Kind,
282-
StringRef Msg, StringRef LineStr,
283-
ArrayRef<std::pair<unsigned,unsigned>> Ranges,
286+
SMDiagnostic(const SourceMgr &sm, SMLoc L, StringRef FN, int Line, int Col,
287+
SourceMgr::DiagKind Kind, StringRef Msg, StringRef LineStr,
288+
ArrayRef<std::pair<unsigned, unsigned>> Ranges,
284289
ArrayRef<SMFixIt> FixIts = None);
285290

286291
const SourceMgr *getSourceMgr() const { return SM; }
@@ -293,13 +298,9 @@ class SMDiagnostic {
293298
StringRef getLineContents() const { return LineContents; }
294299
ArrayRef<std::pair<unsigned, unsigned>> getRanges() const { return Ranges; }
295300

296-
void addFixIt(const SMFixIt &Hint) {
297-
FixIts.push_back(Hint);
298-
}
301+
void addFixIt(const SMFixIt &Hint) { FixIts.push_back(Hint); }
299302

300-
ArrayRef<SMFixIt> getFixIts() const {
301-
return FixIts;
302-
}
303+
ArrayRef<SMFixIt> getFixIts() const { return FixIts; }
303304

304305
void print(const char *ProgName, raw_ostream &S, bool ShowColors = true,
305306
bool ShowKindLabel = true) const;

0 commit comments

Comments
 (0)