Skip to content
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

core: Print fp literals losslessly #3381

Merged
merged 13 commits into from
Nov 4, 2024
8 changes: 4 additions & 4 deletions tests/filecheck/dialects/arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ func.func @test_const_const() {

// CHECK-LABEL: @test_const_const
// CHECK-NEXT: %0 = arith.constant 6.139400e+00 : f32
// CHECK-NEXT: %1 = arith.constant -1.436000e-01 : f32
// CHECK-NEXT: %2 = arith.constant 9.417903e+00 : f32
// CHECK-NEXT: %3 = arith.constant 9.542894e-01 : f32
// CHECK-NEXT: %1 = arith.constant -0.14360000000000017 : f32
// CHECK-NEXT: %2 = arith.constant 9.41790285 : f32
// CHECK-NEXT: %3 = arith.constant 0.9542893522202769 : f32
// CHECK-NEXT: "test.op"(%0, %1, %2, %3) : (f32, f32, f32, f32) -> ()
}

Expand All @@ -57,7 +57,7 @@ func.func @test_const_var_const() {
// CHECK-NEXT: %b = arith.constant 3.141500e+00 : f32
// CHECK-NEXT: %2 = arith.mulf %0, %a : f32
// CHECK-NEXT: %3 = arith.mulf %2, %b : f32
// CHECK-NEXT: %4 = arith.constant 2.129352e+01 : f32
// CHECK-NEXT: %4 = arith.constant 21.29352225 : f32
// CHECK-NEXT: %5 = arith.mulf %4, %0 fastmath<fast> : f32
// CHECK-NEXT: "test.op"(%3, %5) : (f32, f32) -> ()
}
Expand Down
4 changes: 2 additions & 2 deletions tests/filecheck/parser-printer/builtin_attrs.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@

"func.func"() ({}) {function_type = () -> (), value = dense<"0xEEA7CC3DF47612BE2BA4173E8B75E8BDE0B915BDA3191CBE8388E0BDC826DB3DFE78273E6B037E3DEF140D3EF0B5803D4026693CD6B6E1BCE08B4DBDC3A9E63D943B163EE64E46BD808C253EB8F4893D30270CBE36696C3D045E1DBED06A703DA33EBBBD66D646BD36507BBD764D8FBD7010FA3DB6E1B53D9B83C8BDD33FA73D58AD293EB0A6123EAB2627BA40B4CB3C20E9B6BD805AB2BDE047BDBC809A743DE01ADD3D9B77D5BDCEE7043E00B8C1BDCBA80A3DBB03DA3D787C993D163968BC208510BDABFDB1BD8C07213EA34614BEAB06B73A0091413B8013B3BD768F193E7B6515BE7306833D363183BC36BC8B3CA016B7BD3E05D33DE67C28BDCABB0EBEDA2A013EA67DF6BD007EB5BA782A04BEAB69F73D16DD703D3B93A43D1BE45B3DEBAEE8BD8891F1BDF8B18F3D20EC923CE67101BE8382A8BDAB9EE7BA0006CA3AA3F224BE1B56A5BDC06B8A3DC3E6BE3D562310BB964B713C2CC11FBE4BC68F3DAEACD7BDFB093A3D00070F3EC3E4C93D5BCF0D3D1B01E13D9B7D7F3D537CD43D6BEDFBBC4BD9AEBD17BA023E569906BB86599CBD4E28073E1639F5BDF60909BE8B4727BEE4AD153EDF3C05BEB01913BEEB1A59BD03E8D4BD4BD3123D9EA381BD6058F03CD0EFF73D00747FBADBC5AEBD5054273E204DB4BD00CA683B1E28C93D3BCC2A3D9B0E683D4302923D9A3408BEABC89D3A565336BCC0A7F3BD76D1F93D68A3B93D44891C3E1685243E1B3FDBBD5E06A4BD2B4192BD2B19983C50C97B3D40A808BEC0994C3D4B3435BD0B88293D506749BDFC13063E2B7ADF3CF3B013BE"> : tensor<4x4x3x3xf32>, sym_name = "hex_f32_large_attr"} : () -> ()

// CHECK: "value" = dense<[[[[9.992968e-02, -1.430319e-01, 1.480872e-01], [-1.135054e-01, -3.655422e-02, -1.524415e-01], [-1.096354e-01, 1.070076e-01, 1.635475e-01]], [[6.201498e-02, 1.377752e-01, 6.284702e-02], [1.423031e-02, -2.755300e-02, -5.018222e-02], [1.126285e-01, 1.467116e-01, -4.841509e-02]], [[1.616688e-01, 6.736130e-02, -1.368682e-01], [5.771752e-02, -1.536790e-01, 5.869561e-02], [-9.142806e-02, -4.854431e-02, -6.135579e-02]], [[-6.997196e-02, 1.221017e-01, 8.880942e-02], [-9.790727e-02, 8.166470e-02, 1.657003e-01], [1.432140e-01, -6.376306e-04, 2.486622e-02]]], [[[-8.931184e-02, -8.708668e-02, -2.310556e-02], [5.971766e-02, 1.079614e-01, -1.042320e-01], [1.297905e-01, -9.458923e-02, 3.385238e-02]], [[1.064524e-01, 7.494444e-02, -1.417377e-02], [-3.528321e-02, -8.690961e-02, 1.572554e-01], [-1.448007e-01, 1.396378e-03, 2.953589e-03]], [[-8.743954e-02, 1.499613e-01, -1.458949e-01], [6.397714e-02, -1.601468e-02, 1.705752e-02], [-8.939862e-02, 1.030373e-01, -4.113474e-02]], [[-1.393882e-01, 1.261400e-01, -1.203568e-01], [-1.384676e-03, -1.290683e-01, 1.208070e-01], [5.880459e-02, 8.035894e-02, 5.368434e-02]]], [[[-1.136149e-01, -1.179534e-01, 7.016367e-02], [1.793486e-02, -1.264111e-01, -8.228018e-02], [-1.767119e-03, 1.541317e-03, -1.610818e-01]], [[-8.073064e-02, 6.758833e-02, 9.321358e-02], [-2.199372e-03, 1.472749e-02, -1.560103e-01], [7.020243e-02, -1.053098e-01, 4.541967e-02]], [[1.396751e-01, 9.858086e-02, 3.462158e-02], [1.098654e-01, 6.237565e-02, 1.037528e-01], [-3.075286e-02, -8.537539e-02, 1.276630e-01]], [[-2.053817e-03, -7.634263e-02, 1.319897e-01], [-1.197378e-01, -1.338271e-01, -1.633589e-01], [1.461712e-01, -1.301150e-01, -1.436527e-01]]], [[[-5.300419e-02, -1.039582e-01, 3.584604e-02], [-6.330036e-02, 2.933902e-02, 1.210629e-01], [-9.744763e-04, -8.533832e-02, 1.634076e-01]], [[-8.803773e-02, 3.552079e-03, 9.822105e-02], [4.169868e-02, 5.665455e-02, 7.129338e-02], [-1.330132e-01, 1.203795e-03, -1.112827e-02]], [[-1.189723e-01, 1.219815e-01, 9.064370e-02], [1.528674e-01, 1.606639e-01, -1.070540e-01], [-8.009027e-02, -7.141336e-02, 1.856669e-02]], [[6.147128e-02, -1.334543e-01, 4.995131e-02], [-4.423932e-02, 4.138951e-02, -4.917079e-02], [1.309356e-01, 2.727993e-02, -1.442297e-01]]]]> : tensor<4x4x3x3xf32>
// CHECK: "value" = dense<[[[[0.09992967545986176, -0.14303189516067505, 0.14808718860149384], [-0.11350544542074203, -0.03655421733856201, -0.15244154632091522], [-0.1096353754401207, 0.10700756311416626, 0.16354748606681824]], [[0.062014978379011154, 0.13777516782283783, 0.06284701824188232], [0.014230310916900635, -0.027553003281354904, -0.050182223320007324], [0.11262848228216171, 0.14671164751052856, -0.048415087163448334]], [[0.1616687774658203, 0.06736129522323608, -0.13686823844909668], [0.05771752446889877, -0.15367895364761353, 0.05869561433792114], [-0.09142806380987167, -0.04854431003332138, -0.06135579198598862]], [[-0.069971963763237, 0.12210166454315186, 0.08880941569805145], [-0.09790726751089096, 0.08166470378637314, 0.16570031642913818], [0.14321398735046387, -0.0006376306409947574, 0.024866223335266113]]], [[[-0.08931183815002441, -0.08708667755126953, -0.02310556173324585], [0.059717655181884766, 0.10796141624450684, -0.10423203557729721], [0.1297905147075653, -0.0945892333984375, 0.03385237976908684]], [[0.10645242780447006, 0.07494443655014038, -0.01417376659810543], [-0.03528320789337158, -0.08690961450338364, 0.1572553515434265], [-0.14480070769786835, 0.0013963779201731086, 0.0029535889625549316]], [[-0.08743953704833984, 0.14996132254600525, -0.14589492976665497], [0.06397714465856552, -0.01601467654109001, 0.017057519406080246], [-0.08939862251281738, 0.10303734242916107, -0.04113473743200302]], [[-0.13938823342323303, 0.1261400282382965, -0.12035684287548065], [-0.0013846755027770996, -0.1290682554244995, 0.12080701440572739], [0.05880459398031235, 0.08035894483327866, 0.053684335201978683]]], [[[-0.11361487954854965, -0.117953360080719, 0.07016366720199585], [0.017934858798980713, -0.1264110505580902, -0.08228018134832382], [-0.0017671188106760383, 0.0015413165092468262, -0.16108183562755585]], [[-0.08073063939809799, 0.06758832931518555, 0.09321358054876328], [-0.002199371811002493, 0.01472749374806881, -0.15601032972335815], [0.07020243257284164, -0.10530982911586761, 0.04541967436671257]], [[0.13967514038085938, 0.09858085960149765, 0.034621577709913254], [0.10986538976430893, 0.06237564608454704, 0.1037527546286583], [-0.03075285814702511, -0.08537539094686508, 0.1276630014181137]], [[-0.002053817268460989, -0.0763426274061203, 0.13198968768119812], [-0.11973778903484344, -0.1338270604610443, -0.16335885226726532], [0.14617115259170532, -0.13011501729488373, -0.14365267753601074]]], [[[-0.0530041866004467, -0.10395815223455429, 0.0358460359275341], [-0.06330035626888275, 0.0293390154838562, 0.12106287479400635], [-0.0009744763374328613, -0.08533831685781479, 0.163407564163208]], [[-0.08803772926330566, 0.003552079200744629, 0.09822104871273041], [0.041698675602674484, 0.05665455386042595, 0.07129337638616562], [-0.13301315903663635, 0.0012037953129038215, -0.011128267273306847]], [[-0.1189723014831543, 0.12198154628276825, 0.09064370393753052], [0.1528673768043518, 0.1606639325618744, -0.1070539578795433], [-0.08009026944637299, -0.07141336053609848, 0.018566688522696495]], [[0.06147128343582153, -0.1334543228149414, 0.04995131492614746], [-0.04423932358622551, 0.04138950631022453, -0.04917079210281372], [0.13093560934066772, 0.027279933914542198, -0.1442296952009201]]]]> : tensor<4x4x3x3xf32>

"func.func"() ({}) {function_type = () -> (), value = "foo", sym_name = "string_attr"} : () -> ()

Expand Down Expand Up @@ -141,7 +141,7 @@
value1 = dense<"0xCAFEBABE"> : tensor<2xf32>,
value2 = dense<"0xCAFEBABEB00BAABE"> : tensor<1xf64>,
sym_name = "dense_tensor_attr_hex_float"} : () -> ()
// CHECK: "value1" = dense<-3.652251e-01> : tensor<2xf32>, "value2" = dense<-7.762213e-07> : tensor<1xf64>
// CHECK: "value1" = dense<-0.3652251362800598> : tensor<2xf32>, "value2" = dense<-7.762213249592702e-07> : tensor<1xf64>

"func.func"() ({}) {function_type = () -> (),
value1 = dense<[0]> : vector<1xi32>,
Expand Down
4 changes: 4 additions & 0 deletions tests/filecheck/parser-printer/float_parsing.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@

"test.op"() {"value" = -34.e-12 : f32} : () -> ()
// CHECK-NEXT: "test.op"() {"value" = -3.400000e-11 : f32} : () -> ()

// this should print in full precision
"test.op"() {"value" = 3.141592653589793 : f64} : () -> ()
// CHECK-NEXT: "test.op"() {"value" = 3.141592653589793 : f64} : () -> ()
}) : () -> ()
4 changes: 2 additions & 2 deletions tests/filecheck/transforms/linalg-to-csl.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ builtin.module {
//CHECK-NEXT: "csl.fadds"(%0, %0, %11) : (memref<16xf32>, memref<16xf32>, f32) -> ()
//CHECK-NEXT: %12 = arith.constant 1.123400e-01 : f32
//CHECK-NEXT: "csl.fmuls"(%0, %12, %0) : (memref<16xf32>, f32, memref<16xf32>) -> ()
//CHECK-NEXT: %c = arith.constant dense<2.997925e+08> : memref<16xf32>
//CHECK-NEXT: %13 = arith.constant 2.997925e+08 : f32
//CHECK-NEXT: %c = arith.constant dense<299792458.0> : memref<16xf32>
//CHECK-NEXT: %13 = arith.constant 299792458.0 : f32
//CHECK-NEXT: "csl.fmacs"(%0, %2, %0, %13) : (memref<16xf32>, memref<16xf32>, memref<16xf32>, f32) -> ()
//CHECK-NEXT: }
13 changes: 11 additions & 2 deletions xdsl/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,12 @@ def print_attribute(self, attribute: Attribute) -> None:
attr_type = cast(
FloatAttr[Float16Type | Float32Type | Float64Type], attribute
).type
self.print_string(f"{value.data:.6e} : ")
# to mirror mlir-opt, attempt to print scientific notation iff the value parses losslessly
float_str = f"{value.data:.6e}"
if float(float_str) == value.data:
self.print_string(f"{float_str} : ")
else:
self.print_string(f"{repr(value.data)} : ")
self.print_attribute(attr_type)
return

Expand Down Expand Up @@ -598,7 +603,11 @@ def print_one_elem(val: Attribute):
if isinstance(val, IntegerAttr):
self.print_string(f"{val.value.data}")
elif isinstance(val, FloatAttr):
self.print_string(f"{val.value.data:.6e}")
float_str = f"{val.value.data:.6e}"
if float(float_str) == val.value.data:
self.print_string(float_str)
else:
self.print_string(f"{repr(val.value.data)}")
else:
raise Exception(
"unexpected attribute type "
Expand Down
Loading