Skip to content

Commit 42201e9

Browse files
committed
[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.
1 parent 11c0832 commit 42201e9

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

mlir/lib/IR/AsmPrinter.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2061,7 +2061,8 @@ void AsmPrinter::Impl::printLocationInternal(LocationAttr loc, bool pretty,
20612061

20622062
/// Print a floating point value in a way that the parser will be able to
20632063
/// round-trip losslessly.
2064-
static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
2064+
static void printFloatValue(const APFloat &apValue, raw_ostream &os,
2065+
bool *printedHex = nullptr) {
20652066
// We would like to output the FP constant value in exponential notation,
20662067
// but we cannot do this if doing so will lose precision. Check here to
20672068
// make sure that we only output it in exponential format if we can parse
@@ -2102,6 +2103,8 @@ static void printFloatValue(const APFloat &apValue, raw_ostream &os) {
21022103

21032104
// Print special values in hexadecimal format. The sign bit should be included
21042105
// in the literal.
2106+
if (printedHex)
2107+
*printedHex = true;
21052108
SmallVector<char, 16> str;
21062109
APInt apInt = apValue.bitcastToAPInt();
21072110
apInt.toString(str, /*Radix=*/16, /*Signed=*/false,
@@ -2275,10 +2278,12 @@ void AsmPrinter::Impl::printAttributeImpl(Attribute attr,
22752278
return;
22762279

22772280
} else if (auto floatAttr = llvm::dyn_cast<FloatAttr>(attr)) {
2278-
printFloatValue(floatAttr.getValue(), os);
2281+
bool printedHex = false;
2282+
printFloatValue(floatAttr.getValue(), os, &printedHex);
22792283

22802284
// FloatAttr elides the type if F64.
2281-
if (typeElision == AttrTypeElision::May && floatAttr.getType().isF64())
2285+
if (typeElision == AttrTypeElision::May && floatAttr.getType().isF64() &&
2286+
!printedHex)
22822287
return;
22832288

22842289
} else if (auto strAttr = llvm::dyn_cast<StringAttr>(attr)) {

mlir/test/IR/array-of-attr.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ test.array_of_attr_op
1212
// CHECK: test.array_of_attr_op
1313
// CHECK-SAME: a = [], b = [], c = []
1414
test.array_of_attr_op a = [], b = [], c = []
15+
16+
// CHECK: "test.test_array_float"
17+
// CHECK-SAME: 1.000000e+00 : f32, 1.000000e+00, 0x7FF0000000000000 : f64
18+
"test.test_array_float"() {test.float_arr = [1.0 : f32, 1.0 : f64, 0x7FF0000000000000 : f64]} : () -> ()

0 commit comments

Comments
 (0)