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: (arith) fix arith.constant construction #3916

Merged
merged 26 commits into from
Mar 6, 2025

Conversation

compor
Copy link
Collaborator

@compor compor commented Feb 14, 2025

I bumped into this recently, and it is documented in #2390, but it is not the parsing and printing that is broken.
We just use the value_type when provided, but don't adjust the type of the value attribute to it, ending up in having

%0 = "arith.constant"() <{value = 0 : i32}> : () -> i64

which is invalid IR.

I'd expect the TypedAttributeConstraint to kick in, but even though I tried to play with it, I'm not sure if I'm missing something obvious there. Happy to close this if I did.

@compor compor added the dialects Changes on the dialects label Feb 14, 2025
@compor compor self-assigned this Feb 14, 2025
Copy link

codecov bot commented Feb 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.20%. Comparing base (afd917c) to head (eba48c8).
Report is 2 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3916   +/-   ##
=======================================
  Coverage   90.20%   90.20%           
=======================================
  Files         458      458           
  Lines       58353    58356    +3     
  Branches     5694     5692    -2     
=======================================
+ Hits        52637    52642    +5     
  Misses       4325     4325           
+ Partials     1391     1389    -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@alexarice
Copy link
Collaborator

Constraints currently only deal with verification and inference for declarative assembly format

Copy link
Collaborator

@alexarice alexarice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm in general not in favour of this change.

value = cast(IntegerAttr | FloatAttr[AnyFloat], value)
value_type = value.type
elif isinstance(value, DenseIntOrFPElementsAttr):
value_type = value.type
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use value.get_type() to get the type associated to a TypedAttribute

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if isinstance(value, IntegerAttr):
value_type = cast(IntegerType, value_type)
value = IntegerAttr(value.value, value_type)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am against doing a silent coercion here. Personally I think we should either throw an error, or not allow a type in the constructor.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am in favour of not allowing the type, as this is what is the most clear IMHO. I didn't do it as I wasn't sure of the initial rationale for this API and this provides the minimum change against it.

As for throwing an error, I don't think we are doing it anywhere in any constructor.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the original API allowed passing in python ints? I would be in favour of a single constructor that takes a single attribute and infers the type from that attribute.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original API? I'm confused, the diff is clear, no? I can't see what you're saying. We have the from_int_and_width for that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant original as in "when arith.constant was first added to xDSL" original.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I can't answer this, and at the end of the day that was changed to the current one that uses a typed attribute and a type (for which I mentioned I don't know the rationale behind it, AFAIU when I checked a while back, it was another deprecation of similar helpers to from_int_and_width, e.g. from_attr).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think at this point we can just remove value_type from the argument

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There seem to be uses within the stencil part of xDSL that use the general Attribute overload and rely on passing a value_type; for the rest of the types the API is simpler without requiring a value_type provided, as discussed previously here.

assert c1.value.type == i32

c2 = ConstantOp(IntegerAttr(1, i32), i64)
assert c2.value.type == i64
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a use case where this behaviour is desirable

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What behaviour exactly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing in an typed attribute and a different type, and having the constructor make a new attribute with the new type

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some obscure uses (one in CSL).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it makes sense to support this

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patched the ones that were left (in experimental)

@math-fehr
Copy link
Collaborator

I am personally against this constructor, because I really don't understand why this should be a standard constructor on arith.constant.
Why do we want this as a constructor rather than asking the users to write ConstantOp(IntegerAttr(value.value, value_type))?

@compor
Copy link
Collaborator Author

compor commented Feb 14, 2025

I am personally against this constructor, because I really don't understand why this should be a standard constructor on arith.constant. Why do we want this as a constructor rather than asking the users to write ConstantOp(IntegerAttr(value.value, value_type))?

Yeah, I agree, as I said above to Alex. This is the natural thing to do, in my opinion.
Just waiting for @superlopuh to weigh in.

@superlopuh
Copy link
Member

I agree with Mathieu :)

@compor
Copy link
Collaborator Author

compor commented Feb 14, 2025

I agree with Mathieu :)

Great, consensus has been reached :) I'll adjust this PR as discussed above

Copy link
Member

@superlopuh superlopuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@compor compor merged commit 3881db5 into main Mar 6, 2025
17 checks passed
@compor compor deleted the christos/dialects/arith/fix-constant-init branch March 6, 2025 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dialects Changes on the dialects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants