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

Added maybe_downcast & hardened TT Attrs and Types to include better support #1253

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/ttmlir-c/TTAttrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ MLIR_CAPI_EXPORTED MlirAttribute ttmlirTTOperandConstraintArrayAttrGet(
MlirContext ctx, uint32_t *OperandConstraints,
size_t OperandConstraintsSize);

MLIR_CAPI_EXPORTED MlirAttribute ttmlirTTTileSizeAttrGet(MlirContext ctx,
int64_t y, int64_t x);

MLIR_CAPI_EXPORTED MlirAttribute ttmlirTTChipPhysicalCoresAttrGet(
MlirContext ctx, MlirAttribute *worker, size_t workerSize,
MlirAttribute *dram, size_t dramSize, MlirAttribute *eth, size_t ethSize,
MlirAttribute *eth_inactive, size_t eth_inactiveSize);

#ifdef __cplusplus
}
#endif
Expand Down
31 changes: 31 additions & 0 deletions include/ttmlir/Bindings/Python/TTMLIRModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,40 @@
#include "ttmlir/RegisterAll.h"
#include "llvm/Support/CommandLine.h"

#include <variant>

namespace py = pybind11;

namespace mlir::ttmlir::python {

template <typename T>
py::class_<T> tt_attribute_class(py::module &m, const char *class_name) {
py::class_<T> cls(m, class_name);
cls.def_static("maybe_downcast",
[](MlirAttribute attr) -> std::variant<T, py::object> {
auto res = mlir::dyn_cast<T>(unwrap(attr));
if (res) {
return res;
}
return py::none();
});
return cls;
}

template <typename T>
py::class_<T> tt_type_class(py::module &m, const char *class_name) {
py::class_<T> cls(m, class_name);
cls.def_static("maybe_downcast",
[](MlirType type) -> std::variant<T, py::object> {
auto res = mlir::dyn_cast<T>(unwrap(type));
if (res) {
return res;
}
return py::none();
});
return cls;
}

void populateTTModule(py::module &m);
void populateTTIRModule(py::module &m);
void populateTTKernelModule(py::module &m);
Expand Down
30 changes: 30 additions & 0 deletions lib/CAPI/TTAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,34 @@ ttmlirTTOperandConstraintArrayAttrGet(MlirContext ctx,
return wrap(ArrayAttr::get(unwrap(ctx), operandConstraintsArray));
}

MlirAttribute ttmlirTTTileSizeAttrGet(MlirContext ctx, int64_t y, int64_t x) {
return wrap(TileSizeAttr::get(unwrap(ctx), y, x));
}

MlirAttribute ttmlirTTChipPhysicalCoresAttrGet(
MlirContext ctx, MlirAttribute *worker, size_t workerSize,
MlirAttribute *dram, size_t dramSize, MlirAttribute *eth, size_t ethSize,
MlirAttribute *eth_inactive, size_t eth_inactiveSize) {
std::vector<CoreCoordAttr> workerVec, dramVec, ethVec, ethInactiveVec;
for (size_t i = 0; i < workerSize; i++) {
workerVec.push_back(mlir::cast<CoreCoordAttr>(unwrap(worker[i])));
}

for (size_t i = 0; i < dramSize; i++) {
dramVec.push_back(mlir::cast<CoreCoordAttr>(unwrap(dram[i])));
}

for (size_t i = 0; i < ethSize; i++) {
ethVec.push_back(mlir::cast<CoreCoordAttr>(unwrap(eth[i])));
}

for (size_t i = 0; i < eth_inactiveSize; i++) {
ethInactiveVec.push_back(
mlir::cast<CoreCoordAttr>(unwrap(eth_inactive[i])));
}

return wrap(ChipPhysicalCoresAttr::get(unwrap(ctx), workerVec, dramVec,
ethVec, ethInactiveVec));
}

} // namespace mlir::tt
1 change: 1 addition & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ declare_mlir_python_extension(TTMLIRPythonExtensions.Main

set(TTMLIR_PYTHON_SOURCES
MLIRPythonSources.Core
MLIRPythonSources.Dialects.affine
MLIRPythonSources.Dialects.arith
MLIRPythonSources.Dialects.func
MLIRPythonSources.Dialects.tensor
Expand Down
Loading
Loading