xdslproject / xdsl

A Python Compiler Design Toolkit
Other
219 stars 59 forks source link

dialects: (Arith) arith.const printing and parsing is broken #2390

Open AntonLydike opened 3 months ago

AntonLydike commented 3 months ago

The printing and parsing of arith.constant in custom format is broken, see:

%0 = arith.constant true

will not print the type. Similarly:

from xdsl.dialects import arith
from xdsl.dialects.builtin import *
print(arith.Constant(IntegerAttr(0, 32), IntegerType(64)))
# prints: %0 = arith.constant 0 : i32

(see the return value being subtly changed to i32, the type of the attribute and not of the value.

Both of these issues are due to the printing logic printing the value attribute, not the value's value and then the type.

PapyChacal commented 1 month ago

I'm having a walk around or issues, and I find this one very interesting!

What put me on that path is seeing arith.Constant(IntegerAttr(0, 32), IntegerType(64)) in the first place: you're defining a constant of type i64 with a value of type i32, what's going on and how is it supposed to make sense?

Looking at MLIR's arith.constant, it only has one attribute, value, being a TypedAttr (i.e., carrying its type (as an attribute)). Our has both value and type. Thing is, MLIR restrict arith.constant's value attribute to such TypedAttrs. I just recently implemented the equivalent concept in xDSL:

Caveat: I only used TypedAttribute on IntegerAttr for declarative known-type integer syntax support. This would require to match MLIR on which attributes types are actually typed, to make sure arith.constant keeps accepting everything we expect!