Skip to content

Commit

Permalink
[Python][OM] Handle BoolAttr's before IntegerAttr's.
Browse files Browse the repository at this point in the history
BoolAttr's are IntegerAttr's, check them first.

IntegerAttr's that happen to have the characteristics of
BoolAttr will accordingly become Python boolean values.

Unclear where these come from but we do lower booleans
to MLIR bool constants so make sure to handle that.
  • Loading branch information
dtzSiFive committed Aug 5, 2024
1 parent e1993f3 commit 2d9a025
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/Bindings/Python/OMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,15 +366,6 @@ Map::dunderGetItem(std::variant<intptr_t, std::string, MlirAttribute> key) {
// Convert a generic MLIR Attribute to a PythonValue. This is basically a C++
// fast path of the parts of attribute_to_var that we use in the OM dialect.
static PythonPrimitive omPrimitiveToPythonValue(MlirAttribute attr) {
if (mlirAttributeIsAInteger(attr)) {
MlirType type = mlirAttributeGetType(attr);
if (mlirTypeIsAIndex(type) || mlirIntegerTypeIsSignless(type))
return py::int_(mlirIntegerAttrGetValueInt(attr));
if (mlirIntegerTypeIsSigned(type))
return py::int_(mlirIntegerAttrGetValueSInt(attr));
return py::int_(mlirIntegerAttrGetValueUInt(attr));
}

if (omAttrIsAIntegerAttr(attr)) {
auto strRef = omIntegerAttrToString(attr);
return py::int_(py::str(strRef.data, strRef.length));
Expand All @@ -389,10 +380,20 @@ static PythonPrimitive omPrimitiveToPythonValue(MlirAttribute attr) {
return py::str(strRef.data, strRef.length);
}

// BoolAttr's are IntegerAttr's, check this first.
if (mlirAttributeIsABool(attr)) {
return py::bool_(mlirBoolAttrGetValue(attr));
}

if (mlirAttributeIsAInteger(attr)) {
MlirType type = mlirAttributeGetType(attr);
if (mlirTypeIsAIndex(type) || mlirIntegerTypeIsSignless(type))
return py::int_(mlirIntegerAttrGetValueInt(attr));
if (mlirIntegerTypeIsSigned(type))
return py::int_(mlirIntegerAttrGetValueSInt(attr));
return py::int_(mlirIntegerAttrGetValueUInt(attr));
}

if (omAttrIsAReferenceAttr(attr)) {
auto innerRef = omReferenceAttrGetInnerRef(attr);
auto moduleStrRef =
Expand Down

0 comments on commit 2d9a025

Please sign in to comment.