[Mlir-commits] [mlir] [MLIR] Fix generic assembly syntax for ArrayAttr containing hex float (PR #94583)
Mehdi Amini
llvmlistbot at llvm.org
Thu Jun 6 00:11:31 PDT 2024
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/94583
When a float attribute is printed with Hex, we should not elide the type because it is parsed back as i64 otherwise.
>From 480abfda103f7b047da4cc7eb898978172cc0126 Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Thu, 6 Jun 2024 00:10:12 -0700
Subject: [PATCH] [MLIR] Fix generic assembly syntax for ArrayAttr containing
hex float
When a float attribute is printed with Hex, we should not elide the type
because it is parsed back as i64 otherwise.
---
mlir/lib/IR/AsmPrinter.cpp | 9 ++++++---
mlir/test/IR/array-of-attr.mlir | 3 +++
2 files changed, 9 insertions(+), 3 deletions(-)
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index 29e36210f1270..ba47283e37bee 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -2061,7 +2061,7 @@ void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty,
/// Print a floating point value in a way that the parser will be able to
/// round-trip losslessly.
-static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
+static void printFloatValue(const APFloat &apValue, raw_ostream &os, bool *printedHex = nullptr) {
// We would like to output the FP constant value in exponential notation,
// but we cannot do this if doing so will lose precision. Check here to
// make sure that we only output it in exponential format if we can parse
@@ -2102,6 +2102,8 @@ static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
// Print special values in hexadecimal format. The sign bit should be included
// in the literal.
+ if (printedHex)
+ *printedHex = true;
SmallVector<char, 16> str;
APInt apInt = apValue.bitcastToAPInt();
apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
@@ -2275,10 +2277,11 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
return;
} else if (auto floatAttr = llvm::dyn_cast<FloatAttr>(attr)) {
- printFloatValue(floatAttr.getValue(), os);
+ bool printedHex = false;
+ printFloatValue(floatAttr.getValue(), os, &printedHex);
// FloatAttr elides the type if F64.
- if (typeElision == AttrTypeElision::May && floatAttr.getType().isF64())
+ if (typeElision == AttrTypeElision::May && floatAttr.getType().isF64() && !printedHex)
return;
} else if (auto strAttr = llvm::dyn_cast<StringAttr>(attr)) {
diff --git a/mlir/test/IR/array-of-attr.mlir b/mlir/test/IR/array-of-attr.mlir
index 1b6fe55205959..c64a02f230372 100644
--- a/mlir/test/IR/array-of-attr.mlir
+++ b/mlir/test/IR/array-of-attr.mlir
@@ -12,3 +12,6 @@ test.array_of_attr_op
// CHECK: test.array_of_attr_op
// CHECK-SAME: a = [], b = [], c = []
test.array_of_attr_op a = [], b = [], c = []
+
+// CHECK-SAME: 1.000000e+00 : f32, 1.000000e+00, 0x7FF0000000000000 : f64
+"test.test"() {test.float_arr = [1.0 : f32, 1.0 : f64, 0x7FF0000000000000 : f64]} : () -> ()
More information about the Mlir-commits
mailing list