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

dialects: (builtin) floating point hex literal support #3827

Open
bollu opened this issue Feb 4, 2025 · 2 comments
Open

dialects: (builtin) floating point hex literal support #3827

bollu opened this issue Feb 4, 2025 · 2 comments
Labels
bug Something isn't working dialects Changes on the dialects

Comments

@bollu
Copy link

bollu commented Feb 4, 2025

Description

The test InstCombine/win-fdim.ll does not round-trip via xdsl, due to what I believe is an issue with floating point hex literals such as fp128 0xL00000000000000000000000000000000.

Round-tripping fails with:
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/bug.py", line 82, in <module>
    p.print(module)
    ~~~~~~~^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 134, in print
    self.print_op(arg)
    ~~~~~~~~~~~~~^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 853, in print_op
    op.print(self)
    ~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/dialects/builtin.py", line 1746, in print
    printer.print(" ", self.body)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 127, in print
    self.print_region(arg)
    ~~~~~~~~~~~~~~~~~^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 275, in print_region
    self.print_block(
    ~~~~~~~~~~~~~~~~^
        entry_block,
        ^^^^^^^^^^^^
        print_block_args=print_entry_block_args,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        print_block_terminator=print_block_terminators,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 237, in print_block
    self.print_op(op)
    ~~~~~~~~~~~~~^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 855, in print_op
    self.print_op_with_default_format(op)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 745, in print_op_with_default_format
    self.print_regions(op.regions)
    ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 294, in print_regions
    self.print_list(regions, self.print_region)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/utils/base_printer.py", line 67, in print_list
    print_fn(elem)
    ~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 275, in print_region
    self.print_block(
    ~~~~~~~~~~~~~~~~^
        entry_block,
        ^^^^^^^^^^^^
        print_block_args=print_entry_block_args,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        print_block_terminator=print_block_terminators,
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 237, in print_block
    self.print_op(op)
    ~~~~~~~~~~~~~^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 853, in print_op
    op.print(self)
    ~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/dialects/llvm.py", line 1581, in print
    if isattr(self.value, AnyIntegerAttr) and self.result.type == IntegerType(64):
       ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/utils/isattr.py", line 28, in isattr
    return isa(arg, hint)
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/utils/hints.py", line 98, in isa
    constraint.verify(arg, ConstraintContext())
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/irdl/constraints.py", line 580, in verify
    f"{attr} should be of base attribute {self.base_attr.name}"
      ^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/ir/core.py", line 299, in __str__
    printer.print_attribute(self)
    ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 432, in print_attribute
    attribute.print_without_type(self)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/dialects/builtin.py", line 908, in print_without_type
    return printer.print_float_attr(self)
           ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 337, in print_float_attr
    self.print_float(attribute.value.data, attribute.type)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/printer.py", line 357, in print_float
    parsed_value = type.unpack(type.pack([float(float_str)]), 1)[0]
                               ~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/dialects/builtin.py", line 456, in pack
    fmt = self.format[0] + str(len(values)) + self.format[1:]
          ^^^^^^^^^^^
  File "/Users/bollu/25/michelmas/lean-mlir/SSA/Projects/InstCombine/scripts/venv/lib/python3.13/site-packages/xdsl/dialects/builtin.py", line 817, in format
    raise NotImplementedError()

Steps to Reproduce and a Minimal Working Example (MWE)

#!/usr/bin/env python3
from xdsl.dialects.llvm import ReturnOp, LLVMPointerType
from xdsl.utils.exceptions import ParseError
from xdsl.dialects.llvm import FuncOp
from xdsl.parser import Parser
from xdsl.printer import Printer
from xdsl.printer import Printer

# opt -passes=instcombine -S ~/now/llvm-project/llvm/test/Transforms/InstCombine/win-fdim.ll  | mlir-translate -import-llvm | mlir-opt --mlir-print-op-generic
module_contents="""
"builtin.module"() ({
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f64 ()>, linkage = #llvm.linkage<external>, sym_name = "fdim_double", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
    %2 = "llvm.mlir.constant"() <{value = 2.500000e+00 : f64}> : () -> f64
    "llvm.return"(%2) : (f64) -> ()
  }) : () -> ()
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f32 ()>, linkage = #llvm.linkage<external>, sym_name = "fdim_float", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
    %1 = "llvm.mlir.constant"() <{value = 0.000000e+00 : f32}> : () -> f32
    "llvm.return"(%1) : (f32) -> ()
  }) : () -> ()
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f128 ()>, linkage = #llvm.linkage<external>, sym_name = "fdim_long", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
    %0 = "llvm.mlir.constant"() <{value = 0.000000e+00 : f128}> : () -> f128
    "llvm.return"(%0) : (f128) -> ()
  }) : () -> ()
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f64 (f64, f64)>, linkage = #llvm.linkage<external>, memory_effects = #llvm.memory_effects<other = none, argMem = none, inaccessibleMem = none>, sym_name = "fdim", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
  }) : () -> ()
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f32 (f32, f32)>, linkage = #llvm.linkage<external>, memory_effects = #llvm.memory_effects<other = none, argMem = none, inaccessibleMem = none>, sym_name = "fdimf", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
  }) : () -> ()
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f128 (f128, f128)>, linkage = #llvm.linkage<external>, memory_effects = #llvm.memory_effects<other = none, argMem = none, inaccessibleMem = none>, sym_name = "fdiml", unnamed_addr = 0 : i64, visibility_ = 0 : i64}> ({
  }) : () -> ()
}) {dlti.dl_spec = #dlti.dl_spec<i32 = dense<32> : vector<2xi64>, i8 = dense<8> : vector<2xi64>, i16 = dense<16> : vector<2xi64>, !llvm.ptr = dense<64> : vector<4xi64>, i1 = dense<8> : vector<2xi64>, f128 = dense<128> : vector<2xi64>, f16 = dense<16> : vector<2xi64>, f64 = dense<64> : vector<2xi64>, i64 = dense<[32, 64]> : vector<2xi64>, "dlti.endianness" = "little">, llvm.target_triple = ""} : () -> ()
"""


parser = Parser(ctx, module_contents)
module = parser.parse_module()

output = io.StringIO()
p = Printer(stream=output)
p.print(module)
contents = output.getvalue()
output.close()

print(contents)

Additional Information

Any extra information that might help debug the issue.

@bollu bollu added the bug Something isn't working label Feb 4, 2025
@compor
Copy link
Collaborator

compor commented Feb 4, 2025

Hi @bollu, thanks for this, but I cannot run the provided MWE (undefined stuff like ctx etc.) and also not all props/attributes are there in our implementation of llvm.func, so I'm not sure how your example circumvents all these errors and goes to the parsing error you provided.

Nevertheless, I think I've reduced the above to this:

"builtin.module"() ({
  "llvm.func"() <{CConv = #llvm.cconv<ccc>, function_type = !llvm.func<f128 ()>, linkage = #llvm.linkage<external>, sym_name = "fdim_long", visibility_ = 0 : i64}> ({
    %0 = "llvm.mlir.constant"() <{value = 0.000000e+00 : f128}> : () -> f128
    "llvm.return"(%0) : (f128) -> ()
  }) : () -> ()
}) {} : () -> ()

@compor compor changed the title floating point hex literal support dialects: (builtin) floating point hex literal support Feb 4, 2025
@compor compor added the dialects Changes on the dialects label Feb 4, 2025
@compor
Copy link
Collaborator

compor commented Feb 7, 2025

Further reduced test case using #3610

%0="mlir.constant"()<{e=0.0:f128}>:()->f128

Pretty close to the hand-reduced above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dialects Changes on the dialects
Projects
None yet
Development

No branches or pull requests

2 participants