Skip to content

Commit

Permalink
[Python] Ensure attribute_to_var only accesses value when legal. (llv…
Browse files Browse the repository at this point in the history
…m#5612)

This codepath assumes when the input attribute is not an Attribute, it
has a value member. This is the case for things like StringAttr, but
not for ArrayAttr. This popped up because upstream MLIR has been
working to return downcasted concrete Attribute subclasses in more
situations, so APIs that previously returned an Attribute might now
return an ArrayAttr. A simpler testcase also reveals the issue. I've
updated this path to also ensure that a value member exists before
using it, and otherwise fall back to the generic code path.
  • Loading branch information
mikeurbach authored Jul 18, 2023
1 parent 95c011b commit 0fcb904
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions integration_test/Bindings/Python/support/conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# REQUIRES: bindings_python
# RUN: %PYTHON% %s | FileCheck %s

from circt.ir import ArrayAttr, Context, StringAttr
from circt.support import attribute_to_var

with Context():
string_attr = StringAttr.get("foo")
array_attr = ArrayAttr.get([string_attr])
array = attribute_to_var(array_attr)
# CHECK: ['foo']
print(array)
2 changes: 1 addition & 1 deletion lib/Bindings/Python/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def attribute_to_var(attr):

# If it's not the root type, assume it's already been downcasted and don't do
# the expensive probing below.
if attr.__class__ != ir.Attribute:
if attr.__class__ != ir.Attribute and hasattr(attr, "value"):
return attr.value

from .dialects import hw, om
Expand Down

0 comments on commit 0fcb904

Please sign in to comment.