-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[mlir][SparseTensor][NFC] Migrate to OpAsmAttrInterface for ASM alias generation #130483
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][SparseTensor][NFC] Migrate to OpAsmAttrInterface for ASM alias generation #130483
Conversation
@llvm/pr-subscribers-mlir-sparse @llvm/pr-subscribers-mlir Author: Hongren Zheng (ZenithalHourlyRate) ChangesAfter the introduction of See #124721 for the interface itself and #128191, #130479 and #130481 for prior migrations. Full diff: https://github.com/llvm/llvm-project/pull/130483.diff 2 Files Affected:
diff --git a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
index adcf6fac752fe..46fdff08466c1 100644
--- a/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
+++ b/mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td
@@ -11,6 +11,7 @@
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/EnumAttr.td"
+include "mlir/IR/OpAsmInterface.td"
include "mlir/Dialect/SparseTensor/IR/SparseTensorBase.td"
include "mlir/IR/TensorEncoding.td"
@@ -112,7 +113,7 @@ def SparseTensorDimSliceAttr : SparseTensor_Attr<"SparseTensorDimSlice", []> {
// Sparse tensor encoding attribute.
def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
- [ DeclareAttrInterfaceMethods<VerifiableTensorEncoding> ] > {
+ [ DeclareAttrInterfaceMethods<VerifiableTensorEncoding>, OpAsmAttrInterface ] > {
let mnemonic = "encoding";
let description = [{
@@ -543,6 +544,15 @@ def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
void printSymbols(AffineMap &map, AsmPrinter &printer) const;
void printDimensions(AffineMap &map, AsmPrinter &printer, ArrayRef<::mlir::sparse_tensor::SparseTensorDimSliceAttr> dimSlices) const;
void printLevels(AffineMap &map, AsmPrinter &printer, ArrayRef<::mlir::sparse_tensor::LevelType> lvlTypes) const;
+
+ //
+ // OpAsmAttrInterface methods.
+ //
+
+ ::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const {
+ os << "sparse";
+ return ::mlir::OpAsmAliasResult::OverridableAlias;
+ }
}];
let genVerifyDecl = 1;
diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
index 9854cfcc279b5..fcbef0c14739f 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
@@ -2778,22 +2778,7 @@ Operation *SparseTensorDialect::materializeConstant(OpBuilder &builder,
return nullptr;
}
-namespace {
-struct SparseTensorAsmDialectInterface : public OpAsmDialectInterface {
- using OpAsmDialectInterface::OpAsmDialectInterface;
-
- AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
- if (isa<SparseTensorEncodingAttr>(attr)) {
- os << "sparse";
- return AliasResult::OverridableAlias;
- }
- return AliasResult::NoAlias;
- }
-};
-} // namespace
-
void SparseTensorDialect::initialize() {
- addInterface<SparseTensorAsmDialectInterface>();
addAttributes<
#define GET_ATTRDEF_LIST
#include "mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.cpp.inc"
|
…face (#131504) Since the introduction of `OpAsm{Type,Attr}Interface` (#121187), it is possible to generate alias in AsmPrinter solely from the type/attribute itself without consulting the `OpAsmDialectInterface`. This means the behavior can be put in tablegen file near the type/attribute definition. A common pattern is to just use the type/attr mnemonic as the alias. Previously, like #130479/#130481/#130483, this means adding a default implementation to `extraClassDeclaration` in `LLVM_Attr` base class. However, as attribute definition may override `extraClassDeclaration`, it might be preferred to have a new field in tablegen to specify this behavior. This commit adds a `genMnemonicAlias` field to `AttrOrTypeDef`, when enabled, makes `mlir-tblgen` emit a default implementation of `getAlias` using mnemonic. When `OpAsm{Attr,Type}Interface` is not specified by the user, `tblgen` will automatically add the interface. For users wanting other alias behavior, they can ignore such field and still use `extraClassDeclaration` way.
After the introduction of
OpAsmAttrInterface
, it is favorable to migrate code usingOpAsmDialectInterface
for ASM alias generation, which lives inDialect.cpp
, to useOpAsmAttrInterface
, which lives inAttrs.td
. In this way, attribute behavior is placed near its tablegen definition and people won't need to go through other files to know what other (unexpected) hooks comes into play.See #124721 for the interface itself and #128191, #130479 and #130481 for prior migrations.