[PATCH] D72667: [mlir] Use double format when parsing bfloat16 hexadecimal values

River Riddle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 14 13:58:09 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG1bd14ce39293: [mlir] Use double format when parsing bfloat16 hexadecimal values (authored by rriddle).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72667/new/

https://reviews.llvm.org/D72667

Files:
  mlir/lib/Parser/Parser.cpp
  mlir/test/IR/invalid.mlir
  mlir/test/IR/parser.mlir


Index: mlir/test/IR/parser.mlir
===================================================================
--- mlir/test/IR/parser.mlir
+++ mlir/test/IR/parser.mlir
@@ -1030,6 +1030,32 @@
   return
 }
 
+// FIXME: bfloat16 currently uses f64 as a storage format. This test should be
+// changed when that gets fixed.
+// CHECK-LABEL: @bfloat16_special_values
+func @bfloat16_special_values() {
+  // bfloat16 signaling NaNs.
+  // CHECK: constant 0x7FF0000000000001 : bf16
+  %0 = constant 0x7FF0000000000001 : bf16
+  // CHECK: constant 0x7FF8000000000000 : bf16
+  %1 = constant 0x7FF8000000000000 : bf16
+
+  // bfloat16 quiet NaNs.
+  // CHECK: constant 0x7FF0000001000000 : bf16
+  %2 = constant 0x7FF0000001000000 : bf16
+  // CHECK: constant 0xFFF0000001000000 : bf16
+  %3 = constant 0xFFF0000001000000 : bf16
+
+  // bfloat16 positive infinity.
+  // CHECK: constant 0x7FF0000000000000 : bf16
+  %4 = constant 0x7FF0000000000000 : bf16
+  // bfloat16 negative infinity.
+  // CHECK: constant 0xFFF0000000000000 : bf16
+  %5 = constant 0xFFF0000000000000 : bf16
+
+  return
+}
+
 // We want to print floats in exponential notation with 6 significant digits,
 // but it may lead to precision loss when parsing back, in which case we print
 // the decimal form instead.
Index: mlir/test/IR/invalid.mlir
===================================================================
--- mlir/test/IR/invalid.mlir
+++ mlir/test/IR/invalid.mlir
@@ -1120,13 +1120,6 @@
 
 // -----
 
-func @hexadecimal_bf16() {
-  // expected-error @+1 {{hexadecimal float literal not supported for bfloat16}}
-  "foo"() {value = 0xffff : bf16} : () -> ()
-}
-
-// -----
-
 func @hexadecimal_float_leading_minus() {
   // expected-error @+1 {{hexadecimal float literal should not have a leading minus}}
   "foo"() {value = -0x7fff : f16} : () -> ()
Index: mlir/lib/Parser/Parser.cpp
===================================================================
--- mlir/lib/Parser/Parser.cpp
+++ mlir/lib/Parser/Parser.cpp
@@ -1709,8 +1709,14 @@
 /// Construct a float attribute bitwise equivalent to the integer literal.
 static FloatAttr buildHexadecimalFloatLiteral(Parser *p, FloatType type,
                                               uint64_t value) {
-  int width = type.getIntOrFloatBitWidth();
-  APInt apInt(width, value);
+  // FIXME: bfloat is currently stored as a double internally because it doesn't
+  // have valid APFloat semantics.
+  if (type.isF64() || type.isBF16()) {
+    APFloat apFloat(type.getFloatSemantics(), APInt(/*numBits=*/64, value));
+    return p->builder.getFloatAttr(type, apFloat);
+  }
+
+  APInt apInt(type.getWidth(), value);
   if (apInt != value) {
     p->emitError("hexadecimal float constant out of range for type");
     return nullptr;
@@ -1741,11 +1747,6 @@
   }
 
   if (auto floatType = type.dyn_cast<FloatType>()) {
-    // TODO(zinenko): Update once hex format for bfloat16 is supported.
-    if (type.isBF16())
-      return emitError(loc,
-                       "hexadecimal float literal not supported for bfloat16"),
-             nullptr;
     if (isNegative)
       return emitError(
                  loc,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72667.238096.patch
Type: text/x-patch
Size: 3128 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200114/d6b95102/attachment.bin>


More information about the llvm-commits mailing list