Skip to content

Commit c8184ff

Browse files
committed
Revert "[mlir] Report line number from file rather than split (llvm#150982)"
This reverts commit b6a98b9.
1 parent 8e9a0fc commit c8184ff

File tree

9 files changed

+37
-127
lines changed

9 files changed

+37
-127
lines changed

mlir/include/mlir/IR/Diagnostics.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,6 @@ class SourceMgrDiagnosticVerifierHandler : public SourceMgrDiagnosticHandler {
639639
/// verified correctly, failure otherwise.
640640
LogicalResult verify();
641641

642-
/// Register this handler with the given context. This is intended for use
643-
/// with the splitAndProcessBuffer function.
644-
void registerInContext(MLIRContext *ctx);
645-
646642
private:
647643
/// Process a single diagnostic.
648644
void process(Diagnostic &diag);

mlir/include/mlir/Support/ToolUtilities.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,10 @@
2121

2222
namespace llvm {
2323
class MemoryBuffer;
24-
class MemoryBufferRef;
2524
} // namespace llvm
2625

2726
namespace mlir {
28-
// A function that processes a chunk of a buffer and writes the result to an
29-
// output stream.
3027
using ChunkBufferHandler = function_ref<LogicalResult(
31-
std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
32-
const llvm::MemoryBufferRef &sourceBuffer, raw_ostream &os)>;
33-
using NoSourceChunkBufferHandler = function_ref<LogicalResult(
3428
std::unique_ptr<llvm::MemoryBuffer> chunkBuffer, raw_ostream &os)>;
3529

3630
extern inline const char *const kDefaultSplitMarker = "// -----";
@@ -51,15 +45,6 @@ splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
5145
ChunkBufferHandler processChunkBuffer, raw_ostream &os,
5246
llvm::StringRef inputSplitMarker = kDefaultSplitMarker,
5347
llvm::StringRef outputSplitMarker = "");
54-
55-
/// Same as above, but for case where the original buffer is not used while
56-
/// processing the chunk.
57-
LogicalResult
58-
splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
59-
NoSourceChunkBufferHandler processChunkBuffer,
60-
raw_ostream &os,
61-
llvm::StringRef inputSplitMarker = kDefaultSplitMarker,
62-
llvm::StringRef outputSplitMarker = "");
6348
} // namespace mlir
6449

6550
#endif // MLIR_SUPPORT_TOOLUTILITIES_H

mlir/lib/IR/Diagnostics.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,15 @@ SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
821821
for (unsigned i = 0, e = mgr.getNumBuffers(); i != e; ++i)
822822
(void)impl->computeExpectedDiags(out, mgr, mgr.getMemoryBuffer(i + 1));
823823

824-
registerInContext(ctx);
824+
// Register a handler to verify the diagnostics.
825+
setHandler([&](Diagnostic &diag) {
826+
// Process the main diagnostics.
827+
process(diag);
828+
829+
// Process each of the notes.
830+
for (auto &note : diag.getNotes())
831+
process(note);
832+
});
825833
}
826834

827835
SourceMgrDiagnosticVerifierHandler::SourceMgrDiagnosticVerifierHandler(
@@ -854,17 +862,6 @@ LogicalResult SourceMgrDiagnosticVerifierHandler::verify() {
854862
return impl->status;
855863
}
856864

857-
void SourceMgrDiagnosticVerifierHandler::registerInContext(MLIRContext *ctx) {
858-
ctx->getDiagEngine().registerHandler([&](Diagnostic &diag) {
859-
// Process the main diagnostics.
860-
process(diag);
861-
862-
// Process each of the notes.
863-
for (auto &note : diag.getNotes())
864-
process(note);
865-
});
866-
}
867-
868865
/// Process a single diagnostic.
869866
void SourceMgrDiagnosticVerifierHandler::process(Diagnostic &diag) {
870867
return process(diag.getLocation(), diag.str(), diag.getSeverity());

mlir/lib/Support/ToolUtilities.cpp

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
#include "mlir/Support/LLVM.h"
1515
#include "llvm/Support/SourceMgr.h"
1616
#include "llvm/Support/raw_ostream.h"
17-
#include <string>
18-
#include <utility>
1917

2018
using namespace mlir;
2119

@@ -24,18 +22,18 @@ mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
2422
ChunkBufferHandler processChunkBuffer,
2523
raw_ostream &os, llvm::StringRef inputSplitMarker,
2624
llvm::StringRef outputSplitMarker) {
27-
llvm::MemoryBufferRef originalBufferRef = originalBuffer->getMemBufferRef();
2825
// If splitting is disabled, we process the full input buffer.
2926
if (inputSplitMarker.empty())
30-
return processChunkBuffer(std::move(originalBuffer), originalBufferRef, os);
27+
return processChunkBuffer(std::move(originalBuffer), os);
3128

3229
const int inputSplitMarkerLen = inputSplitMarker.size();
3330

31+
auto *origMemBuffer = originalBuffer.get();
3432
SmallVector<StringRef, 8> rawSourceBuffers;
3533
const int checkLen = 2;
3634
// Split dropping the last checkLen chars to enable flagging near misses.
37-
originalBufferRef.getBuffer().split(rawSourceBuffers,
38-
inputSplitMarker.drop_back(checkLen));
35+
origMemBuffer->getBuffer().split(rawSourceBuffers,
36+
inputSplitMarker.drop_back(checkLen));
3937
if (rawSourceBuffers.empty())
4038
return success();
4139

@@ -81,17 +79,11 @@ mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
8179
auto interleaveFn = [&](StringRef subBuffer) {
8280
auto splitLoc = SMLoc::getFromPointer(subBuffer.data());
8381
unsigned splitLine = fileSourceMgr.getLineAndColumn(splitLoc).first;
84-
std::string name((Twine("within split at ") +
85-
originalBufferRef.getBufferIdentifier() + ":" +
86-
Twine(splitLine) + " offset ")
87-
.str());
88-
// Use MemoryBufferRef to avoid copying the buffer & keep at same location
89-
// relative to the original buffer.
90-
auto subMemBuffer =
91-
llvm::MemoryBuffer::getMemBuffer(llvm::MemoryBufferRef(subBuffer, name),
92-
/*RequiresNullTerminator=*/false);
93-
if (failed(
94-
processChunkBuffer(std::move(subMemBuffer), originalBufferRef, os)))
82+
auto subMemBuffer = llvm::MemoryBuffer::getMemBufferCopy(
83+
subBuffer, Twine("within split at ") +
84+
origMemBuffer->getBufferIdentifier() + ":" +
85+
Twine(splitLine) + " offset ");
86+
if (failed(processChunkBuffer(std::move(subMemBuffer), os)))
9587
hadFailure = true;
9688
};
9789
llvm::interleave(sourceBuffers, os, interleaveFn,
@@ -100,16 +92,3 @@ mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
10092
// If any fails, then return a failure of the tool.
10193
return failure(hadFailure);
10294
}
103-
104-
LogicalResult
105-
mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer> originalBuffer,
106-
NoSourceChunkBufferHandler processChunkBuffer,
107-
raw_ostream &os, llvm::StringRef inputSplitMarker,
108-
llvm::StringRef outputSplitMarker) {
109-
auto process = [&](std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
110-
const llvm::MemoryBufferRef &, raw_ostream &os) {
111-
return processChunkBuffer(std::move(chunkBuffer), os);
112-
};
113-
return splitAndProcessBuffer(std::move(originalBuffer), process, os,
114-
inputSplitMarker, outputSplitMarker);
115-
}

mlir/lib/Tools/mlir-opt/MlirOptMain.cpp

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -508,29 +508,20 @@ performActions(raw_ostream &os,
508508

509509
/// Parses the memory buffer. If successfully, run a series of passes against
510510
/// it and print the result.
511-
static LogicalResult
512-
processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
513-
llvm::MemoryBufferRef sourceBuffer,
514-
const MlirOptMainConfig &config, DialectRegistry &registry,
515-
SourceMgrDiagnosticVerifierHandler *verifyHandler,
516-
llvm::ThreadPoolInterface *threadPool) {
511+
static LogicalResult processBuffer(raw_ostream &os,
512+
std::unique_ptr<MemoryBuffer> ownedBuffer,
513+
const MlirOptMainConfig &config,
514+
DialectRegistry &registry,
515+
llvm::ThreadPoolInterface *threadPool) {
517516
// Tell sourceMgr about this buffer, which is what the parser will pick up.
518517
auto sourceMgr = std::make_shared<SourceMgr>();
519-
// Add the original buffer to the source manager to use for determining
520-
// locations.
521-
sourceMgr->AddNewSourceBuffer(
522-
llvm::MemoryBuffer::getMemBuffer(sourceBuffer,
523-
/*RequiresNullTerminator=*/false),
524-
SMLoc());
525518
sourceMgr->AddNewSourceBuffer(std::move(ownedBuffer), SMLoc());
526519

527520
// Create a context just for the current buffer. Disable threading on creation
528521
// since we'll inject the thread-pool separately.
529522
MLIRContext context(registry, MLIRContext::Threading::DISABLED);
530523
if (threadPool)
531524
context.setThreadPool(*threadPool);
532-
if (verifyHandler)
533-
verifyHandler->registerInContext(&context);
534525

535526
StringRef irdlFile = config.getIrdlFile();
536527
if (!irdlFile.empty() && failed(loadIRDLDialects(irdlFile, context)))
@@ -554,12 +545,17 @@ processBuffer(raw_ostream &os, std::unique_ptr<MemoryBuffer> ownedBuffer,
554545
return performActions(os, sourceMgr, &context, config);
555546
}
556547

548+
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(
549+
*sourceMgr, &context, config.verifyDiagnosticsLevel());
550+
557551
// Do any processing requested by command line flags. We don't care whether
558552
// these actions succeed or fail, we only care what diagnostics they produce
559553
// and whether they match our expectations.
560554
(void)performActions(os, sourceMgr, &context, config);
561555

562-
return success();
556+
// Verify the diagnostic handler to make sure that each of the diagnostics
557+
// matched.
558+
return sourceMgrHandler.verify();
563559
}
564560

565561
std::pair<std::string, std::string>
@@ -628,31 +624,14 @@ LogicalResult mlir::MlirOptMain(llvm::raw_ostream &outputStream,
628624
if (threadPoolCtx.isMultithreadingEnabled())
629625
threadPool = &threadPoolCtx.getThreadPool();
630626

631-
SourceMgr sourceMgr;
632-
sourceMgr.AddNewSourceBuffer(
633-
llvm::MemoryBuffer::getMemBuffer(buffer->getMemBufferRef(),
634-
/*RequiresNullTerminator=*/false),
635-
SMLoc());
636-
// Note: this creates a verifier handler independent of the the flag set, as
637-
// internally if the flag is not set, a new scoped diagnostic handler is
638-
// created which would intercept the diagnostics and verify them.
639-
SourceMgrDiagnosticVerifierHandler sourceMgrHandler(
640-
sourceMgr, &threadPoolCtx, config.verifyDiagnosticsLevel());
641627
auto chunkFn = [&](std::unique_ptr<MemoryBuffer> chunkBuffer,
642-
llvm::MemoryBufferRef sourceBuffer, raw_ostream &os) {
643-
return processBuffer(
644-
os, std::move(chunkBuffer), sourceBuffer, config, registry,
645-
config.shouldVerifyDiagnostics() ? &sourceMgrHandler : nullptr,
646-
threadPool);
628+
raw_ostream &os) {
629+
return processBuffer(os, std::move(chunkBuffer), config, registry,
630+
threadPool);
647631
};
648-
LogicalResult status = splitAndProcessBuffer(
649-
llvm::MemoryBuffer::getMemBuffer(buffer->getMemBufferRef(),
650-
/*RequiresNullTerminator=*/false),
651-
chunkFn, outputStream, config.inputSplitMarker(),
652-
config.outputSplitMarker());
653-
if (config.shouldVerifyDiagnostics() && failed(sourceMgrHandler.verify()))
654-
status = failure();
655-
return status;
632+
return splitAndProcessBuffer(std::move(buffer), chunkFn, outputStream,
633+
config.inputSplitMarker(),
634+
config.outputSplitMarker());
656635
}
657636

658637
LogicalResult mlir::MlirOptMain(int argc, char **argv,

mlir/lib/Tools/mlir-translate/MlirTranslateMain.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,6 @@ LogicalResult mlir::mlirTranslateMain(int argc, char **argv,
135135
// Processes the memory buffer with a new MLIRContext.
136136
auto processBuffer = [&](std::unique_ptr<llvm::MemoryBuffer> ownedBuffer,
137137
raw_ostream &os) {
138-
// Many of the translations expect a null-terminated buffer while splitting
139-
// the buffer does not guarantee null-termination. Make a copy of the buffer
140-
// to ensure null-termination.
141-
if (!ownedBuffer->getBuffer().ends_with('\0')) {
142-
ownedBuffer = llvm::MemoryBuffer::getMemBufferCopy(
143-
ownedBuffer->getBuffer(), ownedBuffer->getBufferIdentifier());
144-
}
145138
// Temporary buffers for chained translation processing.
146139
std::string dataIn;
147140
std::string dataOut;

mlir/test/IR/diagnostic-nosplit.mlir

Lines changed: 0 additions & 13 deletions
This file was deleted.

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@-3 {{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@-3 {{source must contain a single top-level operation, found: 0}}

mlir/tools/mlir-pdll/mlir-pdll.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,6 @@ int main(int argc, char **argv) {
201201
llvm::raw_string_ostream outputStrOS(outputStr);
202202
auto processFn = [&](std::unique_ptr<llvm::MemoryBuffer> chunkBuffer,
203203
raw_ostream &os) {
204-
// Split does not guarantee null-termination. Make a copy of the buffer to
205-
// ensure null-termination.
206-
if (!chunkBuffer->getBuffer().ends_with('\0')) {
207-
chunkBuffer = llvm::MemoryBuffer::getMemBufferCopy(
208-
chunkBuffer->getBuffer(), chunkBuffer->getBufferIdentifier());
209-
}
210204
return processBuffer(os, std::move(chunkBuffer), outputType, includeDirs,
211205
dumpODS, includedFiles);
212206
};

0 commit comments

Comments
 (0)