[PATCH] D75133: [mlir][DenseElementsAttr] Fix storage size for bfloat16 when parsing from hex.

River Riddle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 25 12:10:54 PST 2020


rriddle created this revision.
rriddle added a reviewer: lucyrfox.
Herald added subscribers: llvm-commits, Joonsoo, liufengdb, mgester, arpith-jacob, nicolasvasilache, antiagainst, shauheen, burmako, jpienaar, mehdi_amini.
Herald added a project: LLVM.

bfloat16 is stored internally as a double, so we can't direct use Type::getIntOrFloatBitWidth.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75133

Files:
  mlir/lib/Parser/Parser.cpp
  mlir/test/IR/dense-elements-hex.mlir


Index: mlir/test/IR/dense-elements-hex.mlir
===================================================================
--- mlir/test/IR/dense-elements-hex.mlir
+++ mlir/test/IR/dense-elements-hex.mlir
@@ -7,6 +7,9 @@
 // CHECK: dense<[1.000000e+01, 5.000000e+00]> : tensor<2xf64>
 "foo.op"() {dense.attr = dense<"0x00000000000024400000000000001440"> : tensor<2xf64>} : () -> ()
 
+// CHECK: dense<[1.000000e+01, 5.000000e+00]> : tensor<2xbf16>
+"foo.op"() {dense.attr = dense<"0x00000000000024400000000000001440"> : tensor<2xbf16>} : () -> ()
+
 // -----
 
 // expected-error at +1 {{elements hex string should start with '0x'}}
Index: mlir/lib/Parser/Parser.cpp
===================================================================
--- mlir/lib/Parser/Parser.cpp
+++ mlir/lib/Parser/Parser.cpp
@@ -2118,8 +2118,12 @@
 
   // Check that the size of the hex data correpsonds to the size of the type, or
   // a splat of the type.
+  // TODO: bf16 is currently stored as a double, this should be removed when
+  // APFloat properly supports it.
+  int64_t elementWidth =
+      elementType.isBF16() ? 64 : elementType.getIntOrFloatBitWidth();
   if (static_cast<int64_t>(data.size() * CHAR_BIT) !=
-      (type.getNumElements() * elementType.getIntOrFloatBitWidth())) {
+      (type.getNumElements() * elementWidth)) {
     p.emitError(loc) << "elements hex data size is invalid for provided type: "
                      << type;
     return nullptr;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75133.246528.patch
Type: text/x-patch
Size: 1438 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200225/907d0f72/attachment.bin>


More information about the llvm-commits mailing list