Skip to content

Commit

Permalink
[OM] Add ListType C API and Python bindings. (#7490)
Browse files Browse the repository at this point in the history
We want to expose this type through the Python bindings for isinstance
queries, etc., so add the necessary boilerplate.
  • Loading branch information
mikeurbach authored Aug 9, 2024
1 parent 61d2719 commit 36a3a42
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 1 deletion.
9 changes: 9 additions & 0 deletions include/circt-c/Dialect/OM.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ MLIR_CAPI_EXPORTED bool omTypeIsAFrozenPathType(MlirType type);
/// Get the TypeID for a FrozenPathType.
MLIR_CAPI_EXPORTED MlirTypeID omFrozenPathTypeGetTypeID(void);

/// Is the Type a ListType.
MLIR_CAPI_EXPORTED bool omTypeIsAListType(MlirType type);

/// Get the TypeID for a ListType.
MLIR_CAPI_EXPORTED MlirTypeID omListTypeGetTypeID(void);

// Return a element type of a ListType.
MLIR_CAPI_EXPORTED MlirType omListTypeGetElementType(MlirType type);

/// Is the Type a MapType.
MLIR_CAPI_EXPORTED bool omTypeIsAMapType(MlirType type);

Expand Down
5 changes: 5 additions & 0 deletions integration_test/Bindings/Python/dialects/om.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,8 @@
# Test AnyType
any_type = Type.parse("!om.any")
assert isinstance(any_type, om.AnyType)

# Test ListType
list_type = Type.parse("!om.list<!om.any>")
assert isinstance(list_type, om.ListType)
assert isinstance(list_type.element_type, om.AnyType)
4 changes: 4 additions & 0 deletions lib/Bindings/Python/OMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,10 @@ void circt::python::populateDialectOMSubmodule(py::module &m) {
mlir_type_subclass(m, "BasePathType", omTypeIsAFrozenBasePathType,
omFrozenBasePathTypeGetTypeID);

// Add the ListType class definition.
mlir_type_subclass(m, "ListType", omTypeIsAListType, omListTypeGetTypeID)
.def_property_readonly("element_type", omListTypeGetElementType);

// Add the PathType class definition.
mlir_type_subclass(m, "PathType", omTypeIsAFrozenPathType,
omFrozenPathTypeGetTypeID);
Expand Down
2 changes: 1 addition & 1 deletion lib/Bindings/Python/dialects/om.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

from ._om_ops_gen import *
from .._mlir_libs._circt._om import AnyType, Evaluator as BaseEvaluator, Object as BaseObject, List as BaseList, Tuple as BaseTuple, Map as BaseMap, BasePath as BaseBasePath, BasePathType, Path, PathType, ClassType, ReferenceAttr, ListAttr, MapAttr, OMIntegerAttr
from .._mlir_libs._circt._om import AnyType, Evaluator as BaseEvaluator, Object as BaseObject, List as BaseList, Tuple as BaseTuple, Map as BaseMap, BasePath as BaseBasePath, BasePathType, Path, PathType, ClassType, ReferenceAttr, ListAttr, ListType, MapAttr, OMIntegerAttr

from ..ir import Attribute, Diagnostic, DiagnosticSeverity, Module, StringAttr, IntegerAttr, IntegerType
from ..support import attribute_to_var, var_to_attribute
Expand Down
11 changes: 11 additions & 0 deletions lib/CAPI/Dialect/OM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ MlirTypeID omFrozenPathTypeGetTypeID(void) {
return wrap(FrozenPathType::getTypeID());
}

/// Is the Type a ListType.
bool omTypeIsAListType(MlirType type) { return isa<ListType>(unwrap(type)); }

/// Get the TypeID for a ListType.
MlirTypeID omListTypeGetTypeID(void) { return wrap(ListType::getTypeID()); }

// Return a element type of a ListType.
MlirType omListTypeGetElementType(MlirType type) {
return wrap(cast<ListType>(unwrap(type)).getElementType());
}

/// Is the Type a StringType.
bool omTypeIsAStringType(MlirType type) {
return isa<StringType>(unwrap(type));
Expand Down

0 comments on commit 36a3a42

Please sign in to comment.