Skip to content

dialects: (builtin) add get_{int/float}_values - #4367

Merged
alexarice merged 3 commits into
mainfrom
alexarice/dense-element/get-typed-values
May 12, 2025
Merged

alexarice merged 3 commits into
mainfrom
alexarice/dense-element/get-typed-values

Conversation

@alexarice

Copy link
Copy Markdown
Collaborator

Would like some help with the error here, seems to me that the test has a bug in line 96

Hoping that this will make @erick-xanadu 's life easier for adding complex values. In any case these avoid a few casts. They can be eventually replaced with overloads of get_values if microsoft/pyright#10232 is resolved.

@alexarice
alexarice requested review from math-fehr and superlopuh May 9, 2025 17:03
@alexarice alexarice self-assigned this May 9, 2025
@alexarice alexarice added the dialects Changes on the dialects label May 9, 2025
Comment thread xdsl/dialects/builtin.py
Comment on lines +2277 to +2294
def get_int_values(self) -> Sequence[int]:
"""
Return all the values of the elements in this DenseIntOrFPElementsAttr,
checking that the elements are integers.
"""
el_type = self.get_element_type()
assert isinstance(el_type, IntegerType | IndexType)
return el_type.unpack(self.data.data, len(self))

def get_float_values(self) -> Sequence[float]:
"""
Return all the values of the elements in this DenseIntOrFPElementsAttr,
checking that the elements are floats.
"""
el_type = self.get_element_type()
assert isinstance(el_type, AnyFloat)
return el_type.unpack(self.data.data, len(self))

@superlopuh superlopuh May 9, 2025 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A counter-proposal:

Suggested change
def get_int_values(self) -> Sequence[int]:
"""
Return all the values of the elements in this DenseIntOrFPElementsAttr,
checking that the elements are integers.
"""
el_type = self.get_element_type()
assert isinstance(el_type, IntegerType | IndexType)
return el_type.unpack(self.data.data, len(self))
def get_float_values(self) -> Sequence[float]:
"""
Return all the values of the elements in this DenseIntOrFPElementsAttr,
checking that the elements are floats.
"""
el_type = self.get_element_type()
assert isinstance(el_type, AnyFloat)
return el_type.unpack(self.data.data, len(self))
@overload
def get_values(self: DenseIntOrFPElementsAttr[IntegerType | IndexType]) -> Sequence[int]:
...
@overload
def get_values(self: DenseIntOrFPElementsAttr[AnyFloat]) -> Sequence[float]:
...

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm pretty sure I tried this and it gets weird with microsoft/pyright#10232

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I agree that this is the ideal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have wrestled with Pyright and declare myself defeated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What I'm really confused by is why it works for ComplexType

@alexarice
alexarice requested a review from superlopuh May 12, 2025 16:40
Comment thread xdsl/dialects/builtin.py
Comment on lines +2282 to +2284
el_type = self.get_element_type()
assert isinstance(el_type, IntegerType | IndexType), el_type
return el_type.unpack(self.data.data, len(self))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

WDYT? It's unlikely that the function is enough of an overhead, and it keeps the number of implementations low.

Suggested change
el_type = self.get_element_type()
assert isinstance(el_type, IntegerType | IndexType), el_type
return el_type.unpack(self.data.data, len(self))
assert isinstance(self.element_type, IntegerType | IndexType), el_type
return self.get_values()

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'm surprised your suggestion typechecks without a cast

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah you're right

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I am also unsure how you plan to remove the isinstance below?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That also didn't work when I tried it locally

@codecov

codecov Bot commented May 12, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.12%. Comparing base (7a7b560) to head (c8cee8c).
⚠️ Report is 561 commits behind head on main.

Files with missing lines Patch % Lines
.../backend/riscv/lowering/convert_memref_to_riscv.py 33.33% 2 Missing ⚠️
docs/Toy/toy/dialects/toy.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4367      +/-   ##
==========================================
+ Coverage   89.09%   89.12%   +0.03%     
==========================================
  Files         334      335       +1     
  Lines       45423    45409      -14     
  Branches     5579     5583       +4     
==========================================
+ Hits        40470    40473       +3     
+ Misses       3559     3537      -22     
- Partials     1394     1399       +5     

☔ 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
alexarice merged commit 44e1735 into main May 12, 2025
@alexarice
alexarice deleted the alexarice/dense-element/get-typed-values branch May 12, 2025 16:55
emmau678 pushed a commit that referenced this pull request May 23, 2025
Hoping that this will make @erick-xanadu 's life easier for adding
complex values. In any case these avoid a few casts. They can be
eventually replaced with overloads of `get_values` if
microsoft/pyright#10232 is resolved.

---------

Co-authored-by: Sasha Lopoukhine <superlopuh@gmail.com>
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.

2 participants