[PATCH] D78844: Sparse String Elements support.
Rob Suderman via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 24 17:53:53 PDT 2020
rsuderman created this revision.
Herald added subscribers: llvm-commits, Kayjukh, frgossen, grosul1, Joonsoo, liufengdb, lucyrfox, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, jpienaar, rriddle, mehdi_amini.
Herald added a reviewer: rriddle.
Herald added a project: LLVM.
rriddle added a comment.
You also need to add a `SparseElementsAttr::getZeroValue<StringRef>` so that getValues<T> works:
https://github.com/llvm/llvm-project/blob/5339029ff79c0b6a501ef6bb7baeb07cb099f0b7/mlir/include/mlir/IR/Attributes.h#L1257
================
Comment at: mlir/lib/IR/AsmPrinter.cpp:1491
+
+ auto numAttr = attr.dyn_cast<DenseIntOrFPElementsAttr>();
+ printDenseIntOrFPElementsAttr(numAttr, allowHex);
----------------
Use cast here as it is guaranteed to be DenseIntOrFPElementsAttr, dyn_cast implies that it may return null.
Added support for sparse strings elements. This is a follow up from the original DenseStringElements.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D78844
Files:
mlir/lib/IR/AsmPrinter.cpp
mlir/test/IR/parser.mlir
Index: mlir/test/IR/parser.mlir
===================================================================
--- mlir/test/IR/parser.mlir
+++ mlir/test/IR/parser.mlir
@@ -764,6 +764,11 @@
"foof320"(){bar = sparse<[], []> : tensor<0xf32>} : () -> ()
// CHECK: "foof321"() {bar = sparse<{{\[}}], {{\[}}]> : tensor<f32>} : () -> ()
"foof321"(){bar = sparse<[], []> : tensor<f32>} : () -> ()
+
+// CHECK: "foostr"() {bar = sparse<0, "foo"> : tensor<1x1x1x!unknown<"">>} : () -> ()
+ "foostr"(){bar = sparse<0, "foo"> : tensor<1x1x1x!unknown<"">>} : () -> ()
+// CHECK: "foostr"() {bar = sparse<{{\[\[}}1, 1, 0], {{\[}}0, 1, 0], {{\[}}0, 0, 1]], {{\[}}"a", "b", "c"]> : tensor<2x2x2x!unknown<"">>} : () -> ()
+ "foostr"(){bar = sparse<[[1, 1, 0], [0, 1, 0], [0, 0, 1]], ["a", "b", "c"]> : tensor<2x2x2x!unknown<"">>} : () -> ()
return
}
Index: mlir/lib/IR/AsmPrinter.cpp
===================================================================
--- mlir/lib/IR/AsmPrinter.cpp
+++ mlir/lib/IR/AsmPrinter.cpp
@@ -976,6 +976,11 @@
/// Print a dense string elements attribute.
void printDenseStringElementsAttr(DenseStringElementsAttr attr);
+ /// Print a dense elements attribute. If 'allowHex' is true, a hex string is
+ /// used instead of individual elements when the elements attr is large.
+ void printDenseIntOrFPElementsAttr(DenseIntOrFPElementsAttr attr,
+ bool allowHex);
+
void printDialectAttribute(Attribute attr);
void printDialectType(Type type);
@@ -1396,13 +1401,13 @@
break;
}
case StandardAttributes::DenseIntOrFPElements: {
- auto eltsAttr = attr.cast<DenseElementsAttr>();
+ auto eltsAttr = attr.cast<DenseIntOrFPElementsAttr>();
if (printerFlags.shouldElideElementsAttr(eltsAttr)) {
printElidedElementsAttr(os);
break;
}
os << "dense<";
- printDenseElementsAttr(eltsAttr, /*allowHex=*/true);
+ printDenseIntOrFPElementsAttr(eltsAttr, /*allowHex=*/true);
os << '>';
break;
}
@@ -1425,7 +1430,8 @@
break;
}
os << "sparse<";
- printDenseElementsAttr(elementsAttr.getIndices(), /*allowHex=*/false);
+ printDenseIntOrFPElementsAttr(elementsAttr.getIndices(),
+ /*allowHex=*/false);
os << ", ";
printDenseElementsAttr(elementsAttr.getValues(), /*allowHex=*/true);
os << '>';
@@ -1477,6 +1483,17 @@
void ModulePrinter::printDenseElementsAttr(DenseElementsAttr attr,
bool allowHex) {
+ if (auto stringAttr = attr.dyn_cast<DenseStringElementsAttr>()) {
+ printDenseStringElementsAttr(stringAttr);
+ return;
+ }
+
+ auto numAttr = attr.dyn_cast<DenseIntOrFPElementsAttr>();
+ printDenseIntOrFPElementsAttr(numAttr, allowHex);
+}
+
+void ModulePrinter::printDenseIntOrFPElementsAttr(DenseIntOrFPElementsAttr attr,
+ bool allowHex) {
auto type = attr.getType();
auto shape = type.getShape();
auto rank = type.getRank();
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D78844.260037.patch
Type: text/x-patch
Size: 3027 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200425/ce502e6c/attachment.bin>
More information about the llvm-commits
mailing list