Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mlir] Add metadata to Diagnostic. #99398

Merged
merged 11 commits into from
Jul 25, 2024

Conversation

weiweichen
Copy link
Contributor

@weiweichen weiweichen commented Jul 17, 2024

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.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Jul 17, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 17, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: weiwei chen (weiweichen)

Changes

Add metadata to Diagnostic so that we can do some filtering downstream.


Full diff: https://github.com/llvm/llvm-project/pull/99398.diff

1 Files Affected:

  • (modified) mlir/include/mlir/IR/Diagnostics.h (+6)
diff --git a/mlir/include/mlir/IR/Diagnostics.h b/mlir/include/mlir/IR/Diagnostics.h
index bb2e1bb183e9b..1ef902ae5439c 100644
--- a/mlir/include/mlir/IR/Diagnostics.h
+++ b/mlir/include/mlir/IR/Diagnostics.h
@@ -271,6 +271,9 @@ class Diagnostic {
     return failure();
   }
 
+  /// Returns the current list of diagnostic metadata.
+  SmallVector<DiagnosticArgument, 1>& getMetaData() { return metadata; }
+
 private:
   Diagnostic(const Diagnostic &rhs) = delete;
   Diagnostic &operator=(const Diagnostic &rhs) = delete;
@@ -290,6 +293,9 @@ class Diagnostic {
 
   /// A list of attached notes.
   NoteVector notes;
+
+  /// A list of metadata attached to this Diagnostic.
+  SmallVector<DiagnosticArgument, 1> metadata;
 };
 
 inline raw_ostream &operator<<(raw_ostream &os, const Diagnostic &diag) {

Copy link

github-actions bot commented Jul 17, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contributions:

  • Can you please provide some tests? These also serve as examples for the feature.
  • Can you also elaborate on the use-cases in the PR description?

@@ -271,6 +271,9 @@ class Diagnostic {
return failure();
}

/// Returns the current list of diagnostic metadata.
SmallVector<DiagnosticArgument, 1>& getMetaData() { return metadata; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SmallVector<DiagnosticArgument, 1>& getMetaData() { return metadata; }
SmallVectorImpl<DiagnosticArgument>& getMetaData() { return metadata; }

@@ -290,6 +293,9 @@ class Diagnostic {

/// A list of attached notes.
NoteVector notes;

/// A list of metadata attached to this Diagnostic.
SmallVector<DiagnosticArgument, 1> metadata;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SmallVector<DiagnosticArgument, 1> metadata;
SmallVector<DiagnosticArgument, 0> metadata;

@weiweichen
Copy link
Contributor Author

Thanks for the contributions:

  • Can you please provide some tests? These also serve as examples for the feature.

Yep! Added mlir/test/IR/diagnostic-handler-metadata.mlir

  • Can you also elaborate on the use-cases in the PR description?

Yes, updated the PR description with a bit details on the downstream use case we have that needs this upstream change.

Thank you very much for the feedback!

@weiweichen
Copy link
Contributor Author

@joker-eph could you please take a look to see if there should be more changes with this PR? Thank you!

Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test!

Is there some documentation to update as well?


// CHECK-LABEL: Test 'test'
// CHECK-NEXT: 8:3: error: test diagnostic metadata
// CHECK-NOT: 13:3: error: test diagnostic metadata
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using FileCheck, can't we use the --verify-diagnostic mode of mlir-opt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I was following what diagnostic-handler-filter.mlir is done for checking the diagnostics.

-verify-diagnostic is a much better way, updated the test.

@weiweichen
Copy link
Contributor Author

Thanks for adding the test!

Is there some documentation to update as well?

Yep, added something to Diagnostic.md about metadata.

@weiweichen weiweichen merged commit 12dba4d into llvm:main Jul 25, 2024
7 checks passed
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants