[clang-tools-extra] [mlir][sparse] Print new syntax (PR #68130)
https://github.com/yinying-lisa-li updated https://github.com/llvm/llvm-project/pull/68130 >From 47b34bb327e1078678d3ba0c96ebce3fc89cf2ae Mon Sep 17 00:00:00 2001 From: Yinying Li Date: Tue, 3 Oct 2023 16:43:50 + Subject: [PATCH 1/5] [mlir][sparse] Print new syntax Printing changes from #sparse_tensor.encoding<{ lvlTypes = [ "compressed" ] }> to map = (d0) -> (d0 : compressed). Level properties, ELL and slice are also supported. --- .../mlir/Dialect/SparseTensor/IR/Enums.h | 20 +-- .../SparseTensor/IR/SparseTensorDialect.cpp | 64 --- mlir/test/Dialect/SparseTensor/codegen.mlir | 8 +- .../SparseTensor/roundtrip_encoding.mlir | 32 ++-- .../Dialect/SparseTensor/sparse_reshape.mlir | 8 +- .../SparseTensor/sparse_tensor_reshape.mlir | 2 +- .../python/dialects/sparse_tensor/dialect.py | 160 +- 7 files changed, 159 insertions(+), 135 deletions(-) diff --git a/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h index bc351ec52c0946b..2920ef79f461c6a 100644 --- a/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h +++ b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h @@ -215,29 +215,29 @@ constexpr const char *toMLIRString(DimLevelType dlt) { case DimLevelType::Compressed: return "compressed"; case DimLevelType::CompressedNu: -return "compressed_nu"; +return "compressed(nonunique)"; case DimLevelType::CompressedNo: -return "compressed_no"; +return "compressed(nonordered)"; case DimLevelType::CompressedNuNo: -return "compressed_nu_no"; +return "compressed(nonunique, nonordered)"; case DimLevelType::Singleton: return "singleton"; case DimLevelType::SingletonNu: -return "singleton_nu"; +return "singleton(nonunique)"; case DimLevelType::SingletonNo: -return "singleton_no"; +return "singleton(nonordered)"; case DimLevelType::SingletonNuNo: -return "singleton_nu_no"; +return "singleton(nonunique, nonordered)"; case DimLevelType::LooseCompressed: return "loose_compressed"; case DimLevelType::LooseCompressedNu: -return "loose_compressed_nu"; +return "loose_compressed(nonunique)"; case DimLevelType::LooseCompressedNo: -return "loose_compressed_no"; +return "loose_compressed(nonordered)"; case DimLevelType::LooseCompressedNuNo: -return "loose_compressed_nu_no"; +return "loose_compressed(nonunique, nonordered)"; case DimLevelType::TwoOutOfFour: -return "compressed24"; +return "block2_4"; } return ""; } diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp index 3897e1b9ea3597c..4c8dccdda6c0c7c 100644 --- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp +++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp @@ -586,30 +586,56 @@ Attribute SparseTensorEncodingAttr::parse(AsmParser , Type type) { } void SparseTensorEncodingAttr::print(AsmPrinter ) const { - // Print the struct-like storage in dictionary fashion. - printer << "<{ lvlTypes = [ "; - llvm::interleaveComma(getLvlTypes(), printer, [&](DimLevelType dlt) { -printer << "\"" << toMLIRString(dlt) << "\""; - }); - printer << " ]"; + auto map = static_cast(getDimToLvl()); + auto lvlTypes = getLvlTypes(); + // Empty affine map indicates identity map + if (!map) { +map = AffineMap::getMultiDimIdentityMap(getLvlTypes().size(), getContext()); + } + // Modified version of AsmPrinter::Impl::printAffineMap. + printer << "<{ map = "; + // Symbolic identifiers. + if (map.getNumSymbols() != 0) { +printer << '['; +for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i) + printer << 's' << i << ", "; +if (map.getNumSymbols() >= 1) + printer << 's' << map.getNumSymbols() - 1; +printer << ']'; + } + // Dimension identifiers. + printer << '('; + auto dimSlices = getDimSlices(); + if (!dimSlices.empty()) { +for (unsigned i = 0; i < map.getNumDims() - 1; ++i) + printer << 'd' << i << " : " << dimSlices[i] << ", "; +if (map.getNumDims() >= 1) + printer << 'd' << map.getNumDims() - 1 << " : " + << dimSlices[map.getNumDims() - 1]; + } else { +for (unsigned i = 0; i < map.getNumDims() - 1; ++i) + printer << 'd' << i << ", "; +if (map.getNumDims() >= 1) + printer << 'd' << map.getNumDims() - 1; + } + printer << ')'; + // Level format and properties. + printer << " -> ("; + for (unsigned i = 0; i < map.getNumResults() - 1; ++i) { +map.getResult(i).print(printer.getStream()); +printer << " : " << toMLIRString(lvlTypes[i]) << ", "; + } + if (map.getNumResults() >= 1) { +auto lastIndex = map.getNumResults() - 1; +map.getResult(lastIndex).print(printer.getStream()); +printer << " : " << toMLIRString(lvlTypes[lastIndex]); + } + printer << ')'; // Print remaining members only for non-default values. - if
[clang-tools-extra] [mlir][sparse] Print new syntax (PR #68130)
https://github.com/yinying-lisa-li updated https://github.com/llvm/llvm-project/pull/68130 >From 47b34bb327e1078678d3ba0c96ebce3fc89cf2ae Mon Sep 17 00:00:00 2001 From: Yinying Li Date: Tue, 3 Oct 2023 16:43:50 + Subject: [PATCH 1/4] [mlir][sparse] Print new syntax Printing changes from #sparse_tensor.encoding<{ lvlTypes = [ "compressed" ] }> to map = (d0) -> (d0 : compressed). Level properties, ELL and slice are also supported. --- .../mlir/Dialect/SparseTensor/IR/Enums.h | 20 +-- .../SparseTensor/IR/SparseTensorDialect.cpp | 64 --- mlir/test/Dialect/SparseTensor/codegen.mlir | 8 +- .../SparseTensor/roundtrip_encoding.mlir | 32 ++-- .../Dialect/SparseTensor/sparse_reshape.mlir | 8 +- .../SparseTensor/sparse_tensor_reshape.mlir | 2 +- .../python/dialects/sparse_tensor/dialect.py | 160 +- 7 files changed, 159 insertions(+), 135 deletions(-) diff --git a/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h index bc351ec52c0946b..2920ef79f461c6a 100644 --- a/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h +++ b/mlir/include/mlir/Dialect/SparseTensor/IR/Enums.h @@ -215,29 +215,29 @@ constexpr const char *toMLIRString(DimLevelType dlt) { case DimLevelType::Compressed: return "compressed"; case DimLevelType::CompressedNu: -return "compressed_nu"; +return "compressed(nonunique)"; case DimLevelType::CompressedNo: -return "compressed_no"; +return "compressed(nonordered)"; case DimLevelType::CompressedNuNo: -return "compressed_nu_no"; +return "compressed(nonunique, nonordered)"; case DimLevelType::Singleton: return "singleton"; case DimLevelType::SingletonNu: -return "singleton_nu"; +return "singleton(nonunique)"; case DimLevelType::SingletonNo: -return "singleton_no"; +return "singleton(nonordered)"; case DimLevelType::SingletonNuNo: -return "singleton_nu_no"; +return "singleton(nonunique, nonordered)"; case DimLevelType::LooseCompressed: return "loose_compressed"; case DimLevelType::LooseCompressedNu: -return "loose_compressed_nu"; +return "loose_compressed(nonunique)"; case DimLevelType::LooseCompressedNo: -return "loose_compressed_no"; +return "loose_compressed(nonordered)"; case DimLevelType::LooseCompressedNuNo: -return "loose_compressed_nu_no"; +return "loose_compressed(nonunique, nonordered)"; case DimLevelType::TwoOutOfFour: -return "compressed24"; +return "block2_4"; } return ""; } diff --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp index 3897e1b9ea3597c..4c8dccdda6c0c7c 100644 --- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp +++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp @@ -586,30 +586,56 @@ Attribute SparseTensorEncodingAttr::parse(AsmParser , Type type) { } void SparseTensorEncodingAttr::print(AsmPrinter ) const { - // Print the struct-like storage in dictionary fashion. - printer << "<{ lvlTypes = [ "; - llvm::interleaveComma(getLvlTypes(), printer, [&](DimLevelType dlt) { -printer << "\"" << toMLIRString(dlt) << "\""; - }); - printer << " ]"; + auto map = static_cast(getDimToLvl()); + auto lvlTypes = getLvlTypes(); + // Empty affine map indicates identity map + if (!map) { +map = AffineMap::getMultiDimIdentityMap(getLvlTypes().size(), getContext()); + } + // Modified version of AsmPrinter::Impl::printAffineMap. + printer << "<{ map = "; + // Symbolic identifiers. + if (map.getNumSymbols() != 0) { +printer << '['; +for (unsigned i = 0; i < map.getNumSymbols() - 1; ++i) + printer << 's' << i << ", "; +if (map.getNumSymbols() >= 1) + printer << 's' << map.getNumSymbols() - 1; +printer << ']'; + } + // Dimension identifiers. + printer << '('; + auto dimSlices = getDimSlices(); + if (!dimSlices.empty()) { +for (unsigned i = 0; i < map.getNumDims() - 1; ++i) + printer << 'd' << i << " : " << dimSlices[i] << ", "; +if (map.getNumDims() >= 1) + printer << 'd' << map.getNumDims() - 1 << " : " + << dimSlices[map.getNumDims() - 1]; + } else { +for (unsigned i = 0; i < map.getNumDims() - 1; ++i) + printer << 'd' << i << ", "; +if (map.getNumDims() >= 1) + printer << 'd' << map.getNumDims() - 1; + } + printer << ')'; + // Level format and properties. + printer << " -> ("; + for (unsigned i = 0; i < map.getNumResults() - 1; ++i) { +map.getResult(i).print(printer.getStream()); +printer << " : " << toMLIRString(lvlTypes[i]) << ", "; + } + if (map.getNumResults() >= 1) { +auto lastIndex = map.getNumResults() - 1; +map.getResult(lastIndex).print(printer.getStream()); +printer << " : " << toMLIRString(lvlTypes[lastIndex]); + } + printer << ')'; // Print remaining members only for non-default values. - if