Skip to content

Commit 920d5bb

Browse files
authored
[mlir] Set implicit operation loc to start of split. (#151499)
1 parent f4aaf6f commit 920d5bb

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

mlir/lib/Parser/Parser.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,32 @@
1717

1818
using namespace mlir;
1919

20+
static std::pair<int64_t, int64_t>
21+
getLineAndColStart(const llvm::SourceMgr &sourceMgr) {
22+
unsigned lastFileID = sourceMgr.getNumBuffers();
23+
if (lastFileID == 1)
24+
return {0, 0};
25+
26+
auto bufferID = sourceMgr.getMainFileID();
27+
const llvm::MemoryBuffer *main = sourceMgr.getMemoryBuffer(bufferID);
28+
const llvm::MemoryBuffer *last = sourceMgr.getMemoryBuffer(lastFileID);
29+
// Exclude same start.
30+
if (main->getBufferStart() < last->getBufferStart() &&
31+
main->getBufferEnd() >= last->getBufferEnd()) {
32+
return sourceMgr.getLineAndColumn(
33+
llvm::SMLoc::getFromPointer(last->getBufferStart()), bufferID);
34+
}
35+
return {0, 0};
36+
}
37+
2038
LogicalResult mlir::parseSourceFile(const llvm::SourceMgr &sourceMgr,
2139
Block *block, const ParserConfig &config,
2240
LocationAttr *sourceFileLoc) {
2341
const auto *sourceBuf = sourceMgr.getMemoryBuffer(sourceMgr.getMainFileID());
2442
if (sourceFileLoc) {
25-
*sourceFileLoc = FileLineColLoc::get(config.getContext(),
26-
sourceBuf->getBufferIdentifier(),
27-
/*line=*/0, /*column=*/0);
43+
auto [line, column] = getLineAndColStart(sourceMgr);
44+
*sourceFileLoc = FileLineColLoc::get(
45+
config.getContext(), sourceBuf->getBufferIdentifier(), line, column);
2846
}
2947
if (isBytecode(*sourceBuf))
3048
return readBytecodeFile(*sourceBuf, block, config);
@@ -37,9 +55,9 @@ mlir::parseSourceFile(const std::shared_ptr<llvm::SourceMgr> &sourceMgr,
3755
const auto *sourceBuf =
3856
sourceMgr->getMemoryBuffer(sourceMgr->getMainFileID());
3957
if (sourceFileLoc) {
40-
*sourceFileLoc = FileLineColLoc::get(config.getContext(),
41-
sourceBuf->getBufferIdentifier(),
42-
/*line=*/0, /*column=*/0);
58+
auto [line, column] = getLineAndColStart(*sourceMgr);
59+
*sourceFileLoc = FileLineColLoc::get(
60+
config.getContext(), sourceBuf->getBufferIdentifier(), line, column);
4361
}
4462
if (isBytecode(*sourceBuf))
4563
return readBytecodeFile(sourceMgr, block, config);

mlir/test/IR/top-level.mlir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ func.func private @foo()
66

77
// -----
88

9-
// expected-error@-9 {{source must contain a single top-level operation, found: 2}}
9+
// expected-error@-2 {{source must contain a single top-level operation, found: 2}}
1010
func.func private @bar()
1111
func.func private @baz()
1212

1313
// -----
1414

15-
// expected-error@-15 {{source must contain a single top-level operation, found: 0}}
15+
// expected-error@-2 {{source must contain a single top-level operation, found: 0}}

0 commit comments

Comments
 (0)