Add metadata to Diagnostic. Motivation: we have a use case where we want to do some filtering in our customized Diagnostic Handler based on some customized info that is not `location` or `severity` or `diagnostic arguments` that are member variables of `Diagnostic`. Specifically, we want to add a unique ID to the `Diagnostic` for the handler to filter in a compiler pass that emits errors in async tasks with multithreading and the diagnostic handling is associated to the task. This patch adds a field of `metadata` to `mlir::Diagnostics` as a general solution. `metadata` is of type `SmallVector<DiagnosticArgument, 0>` to save memory size and reuse existing `DiagnosticArgument` for metadata type.
66 lines
2.3 KiB
C++
66 lines
2.3 KiB
C++
//===- TestDiagnosticsMetadata.cpp - Test Diagnostic Metatdata ------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file contains test passes for constructing and resolving dominance
|
|
// information.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/IR/SymbolTable.h"
|
|
#include "mlir/Pass/Pass.h"
|
|
#include "llvm/Support/SourceMgr.h"
|
|
|
|
using namespace mlir;
|
|
|
|
namespace {
|
|
struct TestDiagnosticMetadataPass
|
|
: public PassWrapper<TestDiagnosticMetadataPass,
|
|
InterfacePass<SymbolOpInterface>> {
|
|
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestDiagnosticMetadataPass)
|
|
|
|
StringRef getArgument() const final { return "test-diagnostic-metadata"; }
|
|
StringRef getDescription() const final { return "Test diagnostic metadata."; }
|
|
TestDiagnosticMetadataPass() = default;
|
|
TestDiagnosticMetadataPass(const TestDiagnosticMetadataPass &) {}
|
|
|
|
void runOnOperation() override {
|
|
llvm::errs() << "Test '" << getOperation().getName() << "'\n";
|
|
|
|
// Build a diagnostic handler that has filtering capabilities.
|
|
ScopedDiagnosticHandler handler(&getContext(), [](mlir::Diagnostic &diag) {
|
|
return mlir::success(
|
|
llvm::none_of(diag.getMetadata(), [](mlir::DiagnosticArgument &arg) {
|
|
return arg.getKind() == mlir::DiagnosticArgument::
|
|
DiagnosticArgumentKind::String &&
|
|
arg.getAsString().contains("hello");
|
|
}));
|
|
});
|
|
|
|
// Emit a diagnostic for every operation with a valid loc.
|
|
getOperation()->walk([&](Operation *op) {
|
|
if (StringAttr strAttr = op->getAttrOfType<StringAttr>("attr")) {
|
|
if (strAttr.getValue() == "emit_error")
|
|
emitError(op->getLoc(), "test diagnostic metadata")
|
|
.getUnderlyingDiagnostic()
|
|
->getMetadata()
|
|
.push_back(DiagnosticArgument("hello"));
|
|
}
|
|
});
|
|
}
|
|
};
|
|
|
|
} // namespace
|
|
|
|
namespace mlir {
|
|
namespace test {
|
|
void registerTestDiagnosticsMetadataPass() {
|
|
PassRegistration<TestDiagnosticMetadataPass>{};
|
|
}
|
|
} // namespace test
|
|
} // namespace mlir
|