Skip to content

[MLIR] Fix generic assembly syntax for ArrayAttr containing hex float #94583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mlir/lib/IR/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2061,7 +2061,8 @@ 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
Expand Down Expand Up @@ -2102,6 +2103,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,
Expand Down Expand Up @@ -2275,10 +2278,12 @@ 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)) {
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/IR/array-of-attr.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ 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: "test.test_array_float"
// CHECK-SAME: 1.000000e+00 : f32, 1.000000e+00, 0x7FF0000000000000 : f64
"test.test_array_float"() {test.float_arr = [1.0 : f32, 1.0 : f64, 0x7FF0000000000000 : f64]} : () -> ()
Loading