2020-05-07 13:44:12 +08:00
|
|
|
//===- NpcompDialect.cpp - Custom dialect classes -------------------------===//
|
2020-05-07 09:24:51 +08:00
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-06-19 09:02:39 +08:00
|
|
|
#include "npcomp/Python/MlirIr.h"
|
|
|
|
#include "npcomp/Python/NpcompModule.h"
|
2020-05-07 09:24:51 +08:00
|
|
|
|
2020-06-10 10:22:24 +08:00
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyDialect.h"
|
|
|
|
#include "npcomp/Dialect/Basicpy/IR/BasicpyOps.h"
|
2020-06-29 09:42:08 +08:00
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyDialect.h"
|
|
|
|
#include "npcomp/Dialect/Numpy/IR/NumpyOps.h"
|
2020-05-07 09:24:51 +08:00
|
|
|
|
|
|
|
namespace mlir {
|
|
|
|
namespace NPCOMP {
|
|
|
|
|
|
|
|
class BasicpyDialectHelper : public PyDialectHelper {
|
|
|
|
public:
|
|
|
|
using PyDialectHelper::PyDialectHelper;
|
2020-06-09 07:10:51 +08:00
|
|
|
|
2020-05-07 09:24:51 +08:00
|
|
|
static void bind(py::module m) {
|
|
|
|
py::class_<BasicpyDialectHelper, PyDialectHelper>(m, "BasicpyDialectHelper")
|
2020-06-09 07:10:51 +08:00
|
|
|
.def(py::init<PyContext &, PyOpBuilder &>(), py::keep_alive<1, 2>(),
|
2020-06-10 09:34:07 +08:00
|
|
|
py::keep_alive<1, 3>())
|
2020-06-30 06:27:39 +08:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Basicpy dialect
|
|
|
|
// ---------------------------------------------------------------------
|
2020-06-08 06:15:19 +08:00
|
|
|
.def_property_readonly("basicpy_BoolType",
|
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::BoolType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-06-08 07:00:29 +08:00
|
|
|
})
|
|
|
|
.def_property_readonly("basicpy_BytesType",
|
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::BytesType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-06-08 06:15:19 +08:00
|
|
|
})
|
2020-05-07 10:07:50 +08:00
|
|
|
.def_property_readonly("basicpy_EllipsisType",
|
2020-05-07 09:24:51 +08:00
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::EllipsisType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-05-07 09:24:51 +08:00
|
|
|
})
|
2020-06-08 06:46:28 +08:00
|
|
|
.def_property_readonly("basicpy_NoneType",
|
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::NoneType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-06-08 06:46:28 +08:00
|
|
|
})
|
2020-05-07 09:24:51 +08:00
|
|
|
.def("basicpy_SlotObject_type",
|
|
|
|
[](BasicpyDialectHelper &self, std::string className,
|
|
|
|
py::args pySlotTypes) -> PyType {
|
|
|
|
SmallVector<Type, 4> slotTypes;
|
|
|
|
for (auto pySlotType : pySlotTypes) {
|
|
|
|
slotTypes.push_back(pySlotType.cast<PyType>());
|
|
|
|
}
|
|
|
|
auto classNameAttr =
|
2020-06-09 07:10:51 +08:00
|
|
|
StringAttr::get(className, self.getContext());
|
2020-05-07 09:24:51 +08:00
|
|
|
return Basicpy::SlotObjectType::get(classNameAttr, slotTypes);
|
|
|
|
},
|
|
|
|
py::arg("className"))
|
2020-06-08 06:46:28 +08:00
|
|
|
.def_property_readonly("basicpy_StrType",
|
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::StrType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-06-08 06:46:28 +08:00
|
|
|
})
|
|
|
|
.def_property_readonly("basicpy_UnknownType",
|
|
|
|
[](BasicpyDialectHelper &self) -> PyType {
|
|
|
|
return Basicpy::UnknownType::get(
|
2020-06-09 07:10:51 +08:00
|
|
|
self.getContext());
|
2020-06-08 06:46:28 +08:00
|
|
|
})
|
2020-06-10 09:34:07 +08:00
|
|
|
.def("basicpy_exec_op",
|
|
|
|
[](BasicpyDialectHelper &self) {
|
|
|
|
OpBuilder &opBuilder = self.pyOpBuilder.getBuilder(true);
|
|
|
|
Location loc = self.pyOpBuilder.getCurrentLoc();
|
|
|
|
auto op = opBuilder.create<Basicpy::ExecOp>(loc);
|
|
|
|
return py::make_tuple(PyOperationRef(op),
|
|
|
|
op.getBodyBuilder().saveInsertionPoint());
|
|
|
|
})
|
|
|
|
.def("basicpy_exec_discard_op",
|
|
|
|
[](BasicpyDialectHelper &self, std::vector<PyValue> pyOperands) {
|
|
|
|
OpBuilder &opBuilder = self.pyOpBuilder.getBuilder(true);
|
|
|
|
Location loc = self.pyOpBuilder.getCurrentLoc();
|
|
|
|
llvm::SmallVector<Value, 4> operands(pyOperands.begin(),
|
|
|
|
pyOperands.end());
|
|
|
|
auto op =
|
|
|
|
opBuilder.create<Basicpy::ExecDiscardOp>(loc, operands);
|
|
|
|
return PyOperationRef(op);
|
|
|
|
})
|
2020-05-07 09:24:51 +08:00
|
|
|
.def("basicpy_slot_object_get_op",
|
|
|
|
[](BasicpyDialectHelper &self, PyValue slotObject,
|
|
|
|
unsigned index) -> PyOperationRef {
|
|
|
|
auto slotObjectType = slotObject.value.getType()
|
|
|
|
.dyn_cast<Basicpy::SlotObjectType>();
|
|
|
|
if (!slotObjectType) {
|
|
|
|
throw py::raiseValueError("Operand must be a SlotObject");
|
|
|
|
}
|
|
|
|
if (index >= slotObjectType.getSlotCount()) {
|
|
|
|
throw py::raiseValueError("Out of range slot index");
|
|
|
|
}
|
|
|
|
auto resultType = slotObjectType.getSlotTypes()[index];
|
2020-06-10 09:34:07 +08:00
|
|
|
auto indexAttr =
|
|
|
|
IntegerAttr::get(IndexType::get(self.getContext()), index);
|
2020-05-07 09:24:51 +08:00
|
|
|
OpBuilder &opBuilder = self.pyOpBuilder.getBuilder(true);
|
2020-06-07 12:24:28 +08:00
|
|
|
Location loc = self.pyOpBuilder.getCurrentLoc();
|
2020-05-07 09:24:51 +08:00
|
|
|
auto op = opBuilder.create<Basicpy::SlotObjectGetOp>(
|
|
|
|
loc, resultType, slotObject, indexAttr);
|
|
|
|
return op.getOperation();
|
2020-06-29 09:42:08 +08:00
|
|
|
})
|
2020-06-30 06:27:39 +08:00
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Numpy dialect
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
.def("numpy_copy_to_tensor_op",
|
|
|
|
[](BasicpyDialectHelper &self, PyValue source) -> PyOperationRef {
|
|
|
|
auto sourceType =
|
|
|
|
source.value.getType().dyn_cast<Numpy::NdArrayType>();
|
|
|
|
if (!sourceType) {
|
|
|
|
source.value.dump();
|
|
|
|
throw py::raiseValueError("expected ndarray type for "
|
|
|
|
"numpy_copy_to_tensor_op");
|
|
|
|
}
|
|
|
|
auto dtype = sourceType.getDtype();
|
|
|
|
auto tensorType = UnrankedTensorType::get(dtype);
|
|
|
|
OpBuilder &opBuilder = self.pyOpBuilder.getBuilder(true);
|
|
|
|
Location loc = self.pyOpBuilder.getCurrentLoc();
|
|
|
|
auto op = opBuilder.create<Numpy::CopyToTensorOp>(
|
|
|
|
loc, tensorType, source.value);
|
|
|
|
return op.getOperation();
|
|
|
|
})
|
2020-06-29 09:42:08 +08:00
|
|
|
.def("numpy_create_array_from_tensor_op",
|
|
|
|
[](BasicpyDialectHelper &self, PyValue source) -> PyOperationRef {
|
|
|
|
auto sourceType = source.value.getType().dyn_cast<TensorType>();
|
|
|
|
if (!sourceType) {
|
|
|
|
throw py::raiseValueError("expected tensor type for "
|
|
|
|
"numpy_create_array_from_tensor_op");
|
|
|
|
}
|
|
|
|
auto dtype = sourceType.getElementType();
|
2020-06-30 06:27:39 +08:00
|
|
|
auto ndarrayType = Numpy::NdArrayType::get(dtype);
|
2020-06-29 09:42:08 +08:00
|
|
|
OpBuilder &opBuilder = self.pyOpBuilder.getBuilder(true);
|
|
|
|
Location loc = self.pyOpBuilder.getCurrentLoc();
|
|
|
|
auto op = opBuilder.create<Numpy::CreateArrayFromTensorOp>(
|
|
|
|
loc, ndarrayType, source.value);
|
|
|
|
return op.getOperation();
|
|
|
|
})
|
|
|
|
.def("numpy_NdArrayType",
|
|
|
|
[](BasicpyDialectHelper &self, PyType dtype) -> PyType {
|
2020-06-30 06:27:39 +08:00
|
|
|
return Numpy::NdArrayType::get(dtype.type);
|
2020-05-07 09:24:51 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace NPCOMP
|
|
|
|
} // namespace mlir
|
|
|
|
|
|
|
|
using namespace ::mlir::NPCOMP;
|
|
|
|
|
|
|
|
void mlir::npcomp::python::defineNpcompDialect(py::module m) {
|
|
|
|
BasicpyDialectHelper::bind(m);
|
|
|
|
}
|