[PATCH] D74181: [mlir][AsmPrinter] Fix edge case when printing floating point values.

River Riddle via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 6 18:14:29 PST 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG58c18ca1354c: [mlir][AsmPrinter] Fix edge case when printing floating point values. (authored by rriddle).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D74181

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
@@ -1027,6 +1027,11 @@
   // CHECK: constant 0xFFF0000000000000 : f64
   %5 = constant 0xFFF0000000000000 : f64
 
+  // Check that values that can't be represented with the default format, use
+  // hex instead.
+  // CHECK: constant 0xC1CDC00000000000 : f64
+  %6 = constant 0xC1CDC00000000000 : f64
+
   return
 }
 
Index: mlir/lib/IR/AsmPrinter.cpp
===================================================================
--- mlir/lib/IR/AsmPrinter.cpp
+++ mlir/lib/IR/AsmPrinter.cpp
@@ -964,7 +964,8 @@
   bool isNaN = apValue.isNaN();
   if (!isInf && !isNaN) {
     SmallString<128> strValue;
-    apValue.toString(strValue, 6, 0, false);
+    apValue.toString(strValue, /*FormatPrecision=*/6, /*FormatMaxPadding=*/0,
+                     /*TruncateZero=*/false);
 
     // Check to make sure that the stringized number is not some string like
     // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
@@ -975,18 +976,26 @@
            "[-+]?[0-9] regex does not match!");
 
     // Parse back the stringized version and check that the value is equal
-    // (i.e., there is no precision loss). If it is not, use the default format
-    // of APFloat instead of the exponential notation.
-    if (!APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
-      strValue.clear();
-      apValue.toString(strValue);
+    // (i.e., there is no precision loss).
+    if (APFloat(apValue.getSemantics(), strValue).bitwiseIsEqual(apValue)) {
+      os << strValue;
+      return;
+    }
+
+    // If it is not, use the default format of APFloat instead of the
+    // exponential notation.
+    strValue.clear();
+    apValue.toString(strValue);
+
+    // Make sure that we can parse the default form as a float.
+    if (StringRef(strValue).contains('.')) {
+      os << strValue;
+      return;
     }
-    os << strValue;
-    return;
   }
 
-  // Print special values in hexadecimal format.  The sign bit should be
-  // included in the literal.
+  // Print special values in hexadecimal format. The sign bit should be included
+  // in the literal.
   SmallVector<char, 16> str;
   APInt apInt = apValue.bitcastToAPInt();
   apInt.toString(str, /*Radix=*/16, /*Signed=*/false,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74181.243066.patch
Type: text/x-patch
Size: 2367 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200207/0c2f81e7/attachment.bin>


More information about the llvm-commits mailing list