Skip to content

Commit 3db668e

Browse files
[mlir][SparseTensor][NFC] Migrate to OpAsmAttrInterface for ASM alias generation (#130483)
After the introduction of `OpAsmAttrInterface`, it is favorable to migrate code using `OpAsmDialectInterface` for ASM alias generation, which lives in `Dialect.cpp`, to use `OpAsmAttrInterface`, which lives in `Attrs.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.
1 parent 497a3df commit 3db668e

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

mlir/include/mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.td

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
include "mlir/IR/AttrTypeBase.td"
1313
include "mlir/IR/EnumAttr.td"
14+
include "mlir/IR/OpAsmInterface.td"
1415
include "mlir/Dialect/SparseTensor/IR/SparseTensorBase.td"
1516
include "mlir/IR/TensorEncoding.td"
1617

@@ -112,7 +113,7 @@ def SparseTensorDimSliceAttr : SparseTensor_Attr<"SparseTensorDimSlice", []> {
112113

113114
// Sparse tensor encoding attribute.
114115
def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
115-
[ DeclareAttrInterfaceMethods<VerifiableTensorEncoding> ] > {
116+
[ DeclareAttrInterfaceMethods<VerifiableTensorEncoding>, OpAsmAttrInterface ] > {
116117
let mnemonic = "encoding";
117118

118119
let description = [{
@@ -543,6 +544,15 @@ def SparseTensorEncodingAttr : SparseTensor_Attr<"SparseTensorEncoding",
543544
void printSymbols(AffineMap &map, AsmPrinter &printer) const;
544545
void printDimensions(AffineMap &map, AsmPrinter &printer, ArrayRef<::mlir::sparse_tensor::SparseTensorDimSliceAttr> dimSlices) const;
545546
void printLevels(AffineMap &map, AsmPrinter &printer, ArrayRef<::mlir::sparse_tensor::LevelType> lvlTypes) const;
547+
548+
//
549+
// OpAsmAttrInterface methods.
550+
//
551+
552+
::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const {
553+
os << "sparse";
554+
return ::mlir::OpAsmAliasResult::OverridableAlias;
555+
}
546556
}];
547557

548558
let genVerifyDecl = 1;

mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2778,22 +2778,7 @@ Operation *SparseTensorDialect::materializeConstant(OpBuilder &builder,
27782778
return nullptr;
27792779
}
27802780

2781-
namespace {
2782-
struct SparseTensorAsmDialectInterface : public OpAsmDialectInterface {
2783-
using OpAsmDialectInterface::OpAsmDialectInterface;
2784-
2785-
AliasResult getAlias(Attribute attr, raw_ostream &os) const override {
2786-
if (isa<SparseTensorEncodingAttr>(attr)) {
2787-
os << "sparse";
2788-
return AliasResult::OverridableAlias;
2789-
}
2790-
return AliasResult::NoAlias;
2791-
}
2792-
};
2793-
} // namespace
2794-
27952781
void SparseTensorDialect::initialize() {
2796-
addInterface<SparseTensorAsmDialectInterface>();
27972782
addAttributes<
27982783
#define GET_ATTRDEF_LIST
27992784
#include "mlir/Dialect/SparseTensor/IR/SparseTensorAttrDefs.cpp.inc"

0 commit comments

Comments
 (0)