-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mlir] Add metadata to Diagnostic. (#99398)
Summary: 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. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250607
- Loading branch information
1 parent
43f9ee4
commit 958d170
Showing
6 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// RUN: mlir-opt %s -pass-pipeline="builtin.module(func.func(test-diagnostic-metadata))" -verify-diagnostics -o - 2>&1 | FileCheck %s | ||
// COM: This test verifies that diagnostic handler can filter the diagnostic based on its metadata | ||
// COM: whether to emit the errors. | ||
|
||
// CHECK-LABEL: Test 'test' | ||
func.func @test() { | ||
// expected-error @+1 {{test diagnostic metadata}} | ||
"test.emit_error"() { | ||
// CHECK: attr = "emit_error" | ||
attr = "emit_error" | ||
} : () -> () | ||
|
||
"test.do_not_emit_error"() { | ||
// CHECK: attr = "do_not_emit_error" | ||
attr = "do_not_emit_error" | ||
} : () -> () | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===- 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters