29using namespace nb::literals;
34 R
"(Parses a module's assembly format from a string.
36Returns a new MlirModule or raises an MLIRError if the parsing fails.
38See also: https://mlir.llvm.org/docs/LangRef/
42 "Dumps a debug representation of the object to stderr.";
45 R
"(Replace all uses of this value with the `with` value, except for those
46in `exceptions`. `exceptions` can be either a single operation or a list of
56static size_t hash(
const T &value) {
57 return std::hash<T>{}(value);
62 nb::object dialectDescriptor) {
69 std::move(dialectDescriptor)));
73 return (*dialectClass)(std::move(dialectDescriptor));
81 const nb::typed<nb::sequence, PyType> &pyArgTypes,
82 const std::optional<nb::typed<nb::sequence, PyLocation>> &pyArgLocs) {
83 std::vector<MlirType> argTypes;
84 argTypes.reserve(nb::len(pyArgTypes));
85 for (nb::handle pyType : pyArgTypes)
87 nb::cast<python::MLIR_BINDINGS_PYTHON_DOMAIN::PyType &>(pyType));
89 std::vector<MlirLocation> argLocs;
91 argLocs.reserve(nb::len(*pyArgLocs));
92 for (nb::handle pyLoc : *pyArgLocs)
94 nb::cast<python::MLIR_BINDINGS_PYTHON_DOMAIN::PyLocation &>(pyLoc));
95 }
else if (!argTypes.empty()) {
101 if (argTypes.size() != argLocs.size())
102 throw nb::value_error(
103 join(
"Expected ", argTypes.size(),
" locations, got: ", argLocs.size())
105 return mlirBlockCreate(argTypes.size(), argTypes.data(), argLocs.data());
109 nb::ft_lock_guard lock(mutex);
114 nb::ft_lock_guard lock(mutex);
120 nb::class_<PyGlobalDebugFlag>(m,
"_GlobalDebug")
125 [](
const std::string &type) {
126 nb::ft_lock_guard lock(mutex);
129 "types"_a,
"Sets specific debug types to be produced by LLVM.")
132 [](
const std::vector<std::string> &types) {
133 std::vector<const char *> pointers;
134 pointers.reserve(types.size());
135 for (
const std::string &str : types)
136 pointers.push_back(str.c_str());
137 nb::ft_lock_guard lock(mutex);
141 "Sets multiple specific debug types to be produced by LLVM.");
144nb::ft_mutex PyGlobalDebugFlag::mutex;
154 throw nb::key_error(attributeKind.c_str());
159 nb::callable
func,
bool replace,
160 bool allow_existing) {
162 replace, allow_existing);
166 nb::class_<PyAttrBuilderMap>(m,
"AttrBuilder")
169 "Checks whether an attribute builder is registered for the "
170 "given attribute kind.")
173 "Gets the registered attribute builder for the given "
176 "attribute_kind"_a,
"attr_builder"_a,
"replace"_a =
false,
177 "allow_existing"_a =
false,
178 "Register an attribute builder for building MLIR "
179 "attributes from Python values.");
200 operation(std::move(operation)) {}
202intptr_t PyRegionList::getRawNumElements() {
218 operation->checkValid();
220 PyErr_SetNone(PyExc_StopIteration);
225 PyBlock returnBlock(operation, next);
227 return nb::cast(returnBlock);
231 nb::class_<PyBlockIterator>(m,
"BlockIterator")
233 "Returns an iterator over the blocks in the operation's region.")
235 "Returns the next block in the iteration.");
239 operation->checkValid();
244 operation->checkValid();
260 throw nb::index_error(
"attempt to access out of bounds block");
265 return PyBlock(operation, block);
270 throw nb::index_error(
"attempt to access out of bounds block");
274 const std::optional<nb::sequence> &pyArgLocs) {
276 MlirBlock block =
createBlock(nb::cast<nb::sequence>(pyArgTypes), pyArgLocs);
278 return PyBlock(operation, block);
282 nb::class_<PyBlockList>(m,
"BlockList")
284 "Returns the block at the specified index.")
286 "Returns an iterator over blocks in the operation's region.")
288 "Returns the number of blocks in the operation's region.")
291 Appends a new block, with argument types as positional args.
296 "args"_a, nb::kw_only(),
"arg_locs"_a = std::nullopt);
301 if (mlirOperationIsNull(next)) {
302 PyErr_SetNone(PyExc_StopIteration);
314 nb::class_<PyOperationIterator>(m,
"OperationIterator")
316 "Returns an iterator over the operations in an operation's block.")
318 "Returns the next operation in the iteration.");
322 parentOperation->checkValid();
331 while (!mlirOperationIsNull(childOp)) {
344 throw nb::index_error(
"attempt to access out of bounds operation");
347 while (!mlirOperationIsNull(childOp)) {
355 throw nb::index_error(
"attempt to access out of bounds operation");
359 nb::class_<PyOperationList>(m,
"OperationList")
361 "Returns the operation at the specified index.")
363 "Returns an iterator over operations in the list.")
365 "Returns the number of operations in the list.");
380 nb::class_<PyOpOperand>(m,
"OpOperand")
382 "Returns the operation that owns this operand.")
384 "Returns the operand number in the owning operation.");
389 PyErr_SetNone(PyExc_StopIteration);
396 return nb::cast(returnOpOperand);
400 nb::class_<PyOpOperandIterator>(m,
"OpOperandIterator")
402 "Returns an iterator over operands.")
404 "Returns the next operand in the iteration.");
423 std::stringstream ss;
424 ss << threadPool.ptr;
433 nb::gil_scoped_acquire acquire;
434 nb::ft_lock_guard lock(live_contexts_mutex);
435 auto &liveContexts = getLiveContexts();
436 liveContexts[context.ptr] =
this;
443 nb::gil_scoped_acquire acquire;
445 nb::ft_lock_guard lock(live_contexts_mutex);
446 getLiveContexts().erase(context.ptr);
462 throw nb::python_error();
467 nb::gil_scoped_acquire acquire;
468 nb::ft_lock_guard lock(live_contexts_mutex);
469 auto &liveContexts = getLiveContexts();
470 auto it = liveContexts.find(context.ptr);
471 if (it == liveContexts.end()) {
474 nb::object pyRef = nb::cast(unownedContextWrapper);
475 assert(pyRef &&
"cast to nb::object failed");
476 liveContexts[context.ptr] = unownedContextWrapper;
480 nb::object pyRef = nb::cast(it->second);
484nb::ft_mutex PyMlirContext::live_contexts_mutex;
486PyMlirContext::LiveContextMap &PyMlirContext::getLiveContexts() {
487 static LiveContextMap liveContexts;
492 nb::ft_lock_guard lock(live_contexts_mutex);
493 return getLiveContexts().size();
501 const nb::object &excVal,
502 const nb::object &excTb) {
511 nb::object pyHandlerObject =
512 nb::cast(pyHandler, nb::rv_policy::take_ownership);
513 (
void)pyHandlerObject.inc_ref();
517 auto handlerCallback =
520 nb::object pyDiagnosticObject =
521 nb::cast(pyDiagnostic, nb::rv_policy::take_ownership);
528 nb::gil_scoped_acquire gil;
530 result = nb::cast<bool>(pyHandler->callback(pyDiagnostic));
531 }
catch (std::exception &e) {
532 fprintf(stderr,
"MLIR Python Diagnostic handler raised exception: %s\n",
534 pyHandler->hadError =
true;
541 auto deleteCallback = +[](
void *userData) {
542 auto *pyHandler =
static_cast<PyDiagnosticHandler *
>(userData);
543 assert(pyHandler->registeredID &&
"handler is not registered");
544 pyHandler->registeredID.reset();
547 nb::object pyHandlerObject = nb::cast(pyHandler, nb::rv_policy::reference);
548 pyHandlerObject.dec_ref();
552 get(), handlerCallback,
static_cast<void *
>(pyHandler), deleteCallback);
553 return pyHandlerObject;
560 if (self->ctx->emitErrorDiagnostics)
564 MlirDiagnosticSeverity::MlirDiagnosticError)
574 throw std::runtime_error(
575 "An MLIR function requires a Context but none was provided in the call "
576 "or from the surrounding environment. Either pass to the function with "
577 "a 'context=' argument or establish a default using 'with Context():'");
587 static thread_local std::vector<PyThreadContextEntry> stack;
592 auto &stack = getStack();
595 return &stack.back();
598void PyThreadContextEntry::push(FrameKind frameKind, nb::object context,
599 nb::object insertionPoint,
600 nb::object location) {
601 auto &stack = getStack();
602 stack.emplace_back(frameKind, std::move(context), std::move(insertionPoint),
603 std::move(location));
607 if (stack.size() > 1) {
608 auto &prev = *(stack.rbegin() + 1);
609 auto ¤t = stack.back();
610 if (current.context.is(prev.context)) {
612 if (!current.insertionPoint)
613 current.insertionPoint = prev.insertionPoint;
614 if (!current.location)
615 current.location = prev.location;
623 return nb::cast<PyMlirContext *>(context);
629 return nb::cast<PyInsertionPoint *>(insertionPoint);
635 return nb::cast<PyLocation *>(location);
640 return tos ? tos->getContext() :
nullptr;
645 return tos ? tos->getInsertionPoint() :
nullptr;
650 return tos ? tos->getLocation() :
nullptr;
654 push(FrameKind::Context, context,
661 auto &stack = getStack();
663 throw std::runtime_error(
"Unbalanced Context enter/exit");
664 auto &tos = stack.back();
665 if (tos.frameKind != FrameKind::Context && tos.getContext() != &context)
666 throw std::runtime_error(
"Unbalanced Context enter/exit");
673 nb::cast<PyInsertionPoint &>(insertionPointObj);
674 nb::object contextObj =
676 push(FrameKind::InsertionPoint,
680 return insertionPointObj;
684 auto &stack = getStack();
686 throw std::runtime_error(
"Unbalanced InsertionPoint enter/exit");
687 auto &tos = stack.back();
688 if (tos.frameKind != FrameKind::InsertionPoint &&
689 tos.getInsertionPoint() != &insertionPoint)
690 throw std::runtime_error(
"Unbalanced InsertionPoint enter/exit");
695 PyLocation &location = nb::cast<PyLocation &>(locationObj);
697 push(FrameKind::Location, contextObj,
704 auto &stack = getStack();
706 throw std::runtime_error(
"Unbalanced Location enter/exit");
707 auto &tos = stack.back();
708 if (tos.frameKind != FrameKind::Location && tos.getLocation() != &location)
709 throw std::runtime_error(
"Unbalanced Location enter/exit");
719 if (materializedNotes) {
720 for (nb::handle noteObject : *materializedNotes) {
721 PyDiagnostic *note = nb::cast<PyDiagnostic *>(noteObject);
729 : context(context), callback(std::move(callback)) {}
738 assert(!registeredID &&
"should have unregistered");
744void PyDiagnostic::checkValid() {
746 throw std::invalid_argument(
747 "Diagnostic is invalid (used outside of callback)");
766 nb::object fileObject = nb::module_::import_(
"io").attr(
"StringIO")();
769 return nb::cast<nb::str>(fileObject.attr(
"getvalue")());
774 if (materializedNotes)
775 return *materializedNotes;
777 nb::tuple notes = nb::steal<nb::tuple>(PyTuple_New(numNotes));
778 for (
intptr_t i = 0; i < numNotes; ++i) {
780 nb::object diagnostic = nb::cast(
PyDiagnostic(noteDiag));
781 PyTuple_SetItem(notes.ptr(), i, diagnostic.release().ptr());
783 materializedNotes = std::move(notes);
785 return *materializedNotes;
789 std::vector<DiagnosticInfo> notes;
791 notes.emplace_back(nb::cast<PyDiagnostic>(n).
getInfo());
803 {key.data(), key.size()});
805 std::string msg =
join(
"Dialect '", key,
"' not found");
807 throw nb::attribute_error(msg.c_str());
808 throw nb::index_error(msg.c_str());
818 MlirDialectRegistry rawRegistry =
821 throw nb::python_error();
836 throw nb::python_error();
846 const nb::object &excVal,
847 const nb::object &excTb) {
854 throw std::runtime_error(
855 "An MLIR function requires a Location but none was provided in the "
856 "call or from the surrounding environment. Either pass to the function "
857 "with a 'loc=' argument or establish a default using 'with loc:'");
870 nb::gil_scoped_acquire acquire;
871 auto &liveModules =
getContext()->liveModules;
872 assert(liveModules.count(module.ptr) == 1 &&
873 "destroying module not in live map");
874 liveModules.erase(module.ptr);
882 nb::gil_scoped_acquire acquire;
883 auto &liveModules = contextRef->liveModules;
884 auto it = liveModules.find(module.ptr);
885 if (it == liveModules.end()) {
891 nb::object pyRef = nb::cast(unownedModule, nb::rv_policy::take_ownership);
892 unownedModule->handle = pyRef;
893 liveModules[module.ptr] =
894 std::make_pair(unownedModule->handle, unownedModule);
895 return PyModuleRef(unownedModule, std::move(pyRef));
899 nb::object pyRef = nb::borrow<nb::object>(it->second.first);
905 if (mlirModuleIsNull(rawModule))
906 throw nb::python_error();
938template <
typename T,
class... Args>
940 nb::handle type = nb::type<T>();
941 nb::object instance = nb::inst_alloc(type);
942 T *
ptr = nb::inst_ptr<T>(instance);
943 new (
ptr) T(std::forward<Args>(args)...);
944 nb::inst_mark_ready(instance);
951 MlirOperation operation,
952 nb::object parentKeepAlive) {
955 makeObjectRef<PyOperation>(std::move(contextRef), operation);
956 unownedOperation->handle = unownedOperation.
getObject();
957 if (parentKeepAlive) {
958 unownedOperation->parentKeepAlive = std::move(parentKeepAlive);
960 return unownedOperation;
964 MlirOperation operation,
965 nb::object parentKeepAlive) {
966 return createInstance(std::move(contextRef), operation,
967 std::move(parentKeepAlive));
971 MlirOperation operation,
972 nb::object parentKeepAlive) {
973 PyOperationRef created = createInstance(std::move(contextRef), operation,
974 std::move(parentKeepAlive));
975 created->attached =
false;
980 const std::string &sourceStr,
981 const std::string &sourceName) {
986 if (mlirOperationIsNull(op))
987 throw MLIRError(
"Unable to parse operation assembly", errors.take());
994 parentKeepAlive = nb::object();
1007 assert(!attached &&
"operation already attached");
1012 assert(attached &&
"operation already detached");
1018 throw std::runtime_error(
"the operation has been invalidated");
1023 std::optional<int64_t> largeResourceLimit,
1024 bool enableDebugInfo,
bool prettyDebugInfo,
1025 bool printGenericOpForm,
bool useLocalScope,
1026 bool useNameLocAsPrefix,
bool assumeVerified,
1027 nb::object fileObject,
bool binary,
1031 if (fileObject.is_none())
1032 fileObject = nb::module_::import_(
"sys").attr(
"stdout");
1035 if (largeElementsLimit)
1037 if (largeResourceLimit)
1039 if (enableDebugInfo)
1042 if (printGenericOpForm)
1050 if (useNameLocAsPrefix)
1055 accum.getUserData());
1063 if (fileObject.is_none())
1064 fileObject = nb::module_::import_(
"sys").attr(
"stdout");
1067 accum.getUserData());
1071 std::optional<int64_t> bytecodeVersion) {
1076 if (!bytecodeVersion.has_value())
1078 accum.getUserData());
1083 operation, config, accum.getCallback(), accum.getUserData());
1086 throw nb::value_error(
1087 join(
"Unable to honor desired bytecode version ", *bytecodeVersion)
1098 std::string exceptionWhat;
1099 nb::object exceptionType;
1101 UserData userData{callback,
false, {}, {}};
1104 UserData *calleeUserData =
static_cast<UserData *
>(userData);
1106 return static_cast<MlirWalkResult>((calleeUserData->callback)(op));
1107 }
catch (nb::python_error &e) {
1108 calleeUserData->gotException =
true;
1109 calleeUserData->exceptionWhat = std::string(e.what());
1110 calleeUserData->exceptionType = nb::borrow(e.type());
1111 return MlirWalkResult::MlirWalkResultInterrupt;
1116 if (userData.gotException) {
1117 std::string message(
"Exception raised in callback: ");
1118 message.append(userData.exceptionWhat);
1119 throw std::runtime_error(message);
1124 std::optional<int64_t> largeElementsLimit,
1125 std::optional<int64_t> largeResourceLimit,
1126 bool enableDebugInfo,
bool prettyDebugInfo,
1127 bool printGenericOpForm,
bool useLocalScope,
1128 bool useNameLocAsPrefix,
bool assumeVerified,
1130 nb::object fileObject;
1132 fileObject = nb::module_::import_(
"io").attr(
"BytesIO")();
1134 fileObject = nb::module_::import_(
"io").attr(
"StringIO")();
1136 print(largeElementsLimit,
1148 return fileObject.attr(
"getvalue")();
1157 operation.parentKeepAlive = otherOp.parentKeepAlive;
1166 operation.parentKeepAlive = otherOp.parentKeepAlive;
1181 throw MLIRError(
"Verification failed", errors.take());
1188 throw nb::value_error(
"Detached operations have no parent");
1190 if (mlirOperationIsNull(operation))
1200 assert(parentOperation &&
"Operation has no parent");
1201 return PyBlock{std::move(*parentOperation), block};
1211 if (mlirOperationIsNull(rawOperation))
1212 throw nb::python_error();
1219 const nb::object &maybeIp) {
1221 if (!maybeIp.is(nb::cast(
false))) {
1223 if (maybeIp.is_none()) {
1226 ip = nb::cast<PyInsertionPoint *>(maybeIp);
1234 std::optional<std::vector<PyType *>> results,
1235 const MlirValue *operands,
size_t numOperands,
1236 std::optional<nb::dict> attributes,
1237 std::optional<std::vector<PyBlock *>> successors,
1239 const nb::object &maybeIp,
bool inferType) {
1240 std::vector<MlirType> mlirResults;
1241 std::vector<MlirBlock> mlirSuccessors;
1242 std::vector<std::pair<std::string, MlirAttribute>> mlirAttributes;
1246 throw nb::value_error(
"number of regions must be >= 0");
1250 mlirResults.reserve(results->size());
1254 throw nb::value_error(
"result type cannot be None");
1255 mlirResults.push_back(*
result);
1260 mlirAttributes.reserve(attributes->size());
1261 for (std::pair<nb::handle, nb::handle> it : *attributes) {
1264 key = nb::cast<std::string>(it.first);
1265 }
catch (nb::cast_error &err) {
1266 std::string msg =
join(
"Invalid attribute key (not a string) when "
1267 "attempting to create the operation \"",
1268 name,
"\" (", err.what(),
")");
1269 throw nb::type_error(msg.c_str());
1272 auto &attribute = nb::cast<PyAttribute &>(it.second);
1274 mlirAttributes.emplace_back(std::move(key), attribute);
1275 }
catch (nb::cast_error &err) {
1276 std::string msg =
join(
"Invalid attribute value for the key \"", key,
1277 "\" when attempting to create the operation \"",
1278 name,
"\" (", err.what(),
")");
1279 throw nb::type_error(msg.c_str());
1280 }
catch (std::runtime_error &) {
1282 std::string msg =
join(
1283 "Found an invalid (`None`?) attribute value for the key \"", key,
1284 "\" when attempting to create the operation \"", name,
"\"");
1285 throw std::runtime_error(msg);
1291 mlirSuccessors.reserve(successors->size());
1292 for (PyBlock *successor : *successors) {
1295 throw nb::value_error(
"successor block cannot be None");
1296 mlirSuccessors.push_back(successor->get());
1302 MlirOperationState state =
1304 if (numOperands > 0)
1306 state.enableResultTypeInference = inferType;
1307 if (!mlirResults.empty())
1309 mlirResults.data());
1310 if (!mlirAttributes.empty()) {
1314 std::vector<MlirNamedAttribute> mlirNamedAttributes;
1315 mlirNamedAttributes.reserve(mlirAttributes.size());
1316 for (
const std::pair<std::string, MlirAttribute> &it : mlirAttributes)
1322 mlirNamedAttributes.data());
1324 if (!mlirSuccessors.empty())
1326 mlirSuccessors.data());
1328 std::vector<MlirRegion> mlirRegions;
1329 mlirRegions.resize(regions);
1330 for (
int i = 0; i < regions; ++i)
1333 mlirRegions.data());
1337 PyMlirContext::ErrorCapture errors(location.
getContext());
1340 throw MLIRError(
"Operation creation failed", errors.take());
1362 std::string_view(identStr.
data, identStr.
length));
1377 [](
PyOpResult &self) -> nb::typed<nb::object, PyOpView> {
1380 "expected the owner of the value in Python to match that in "
1384 "Returns the operation that produces this result.");
1388 "Returns the position of this result in the operation's result list.");
1392template <
typename Container>
1393static std::vector<nb::typed<nb::object, PyType>>
1395 std::vector<nb::typed<nb::object, PyType>>
result;
1396 result.reserve(container.size());
1397 for (
int i = 0, e = container.size(); i < e; ++i) {
1411 operation(std::move(operation)) {}
1419 "Returns a list of types for all results in this result list.");
1425 "Returns the operation that owns this result list.");
1428intptr_t PyOpResultList::getRawNumElements() {
1429 operation->checkValid();
1433PyOpResult PyOpResultList::getRawElement(intptr_t index) {
1435 return PyOpResult(value);
1438PyOpResultList PyOpResultList::slice(intptr_t startIndex, intptr_t length,
1439 intptr_t step)
const {
1448 nb::sequence resultTypeList,
1449 const nb::object &resultSegmentSpecObj,
1450 std::vector<int32_t> &resultSegmentLengths,
1451 std::vector<PyType *> &resultTypes) {
1452 resultTypes.reserve(nb::len(resultTypeList));
1453 if (resultSegmentSpecObj.is_none()) {
1456 for (nb::handle resultType : resultTypeList) {
1458 resultTypes.push_back(nb::cast<PyType *>(resultType));
1459 if (!resultTypes.back())
1460 throw nb::cast_error();
1461 }
catch (nb::cast_error &err) {
1462 throw nb::value_error(
join(
"Result ",
index,
" of operation \"", name,
1463 "\" must be a Type (", err.what(),
")")
1470 auto resultSegmentSpec = nb::cast<std::vector<int>>(resultSegmentSpecObj);
1471 if (resultSegmentSpec.size() != nb::len(resultTypeList)) {
1472 throw nb::value_error(
1473 join(
"Operation \"", name,
"\" requires ", resultSegmentSpec.size(),
1474 " result segments but was provided ", nb::len(resultTypeList))
1477 resultSegmentLengths.reserve(nb::len(resultTypeList));
1478 for (
size_t i = 0, e = resultSegmentSpec.size(); i < e; ++i) {
1479 int segmentSpec = resultSegmentSpec[i];
1480 if (segmentSpec == 1 || segmentSpec == 0) {
1483 auto *resultType = nb::cast<PyType *>(resultTypeList[i]);
1485 resultTypes.push_back(resultType);
1486 resultSegmentLengths.push_back(1);
1487 }
else if (segmentSpec == 0) {
1489 resultSegmentLengths.push_back(0);
1491 throw nb::value_error(
1492 join(
"Result ", i,
" of operation \"", name,
1493 "\" must be a Type (was None and result is not optional)")
1496 }
catch (nb::cast_error &err) {
1497 throw nb::value_error(
join(
"Result ", i,
" of operation \"", name,
1498 "\" must be a Type (", err.what(),
")")
1501 }
else if (segmentSpec == -1) {
1504 if (resultTypeList[i].is_none()) {
1506 resultSegmentLengths.push_back(0);
1509 auto segment = nb::cast<nb::sequence>(resultTypeList[i]);
1510 for (nb::handle segmentItem : segment) {
1511 resultTypes.push_back(nb::cast<PyType *>(segmentItem));
1512 if (!resultTypes.back()) {
1513 throw nb::type_error(
"contained a None item");
1516 resultSegmentLengths.push_back(nb::len(segment));
1518 }
catch (std::exception &err) {
1522 throw nb::value_error(
join(
"Result ", i,
" of operation \"", name,
1523 "\" must be a Sequence of Types (",
1528 throw nb::value_error(
"Unexpected segment spec");
1536 if (numResults != 1) {
1538 throw nb::value_error(
1539 join(
"Cannot call .result on operation ",
1540 std::string_view(name.data, name.length),
" which has ",
1542 " results (it is only valid for operations with a "
1550 if (operand.is_none()) {
1551 throw nb::value_error(
"contained a None item");
1554 if (nb::try_cast<PyOperationBase *>(operand, op)) {
1558 if (nb::try_cast<PyOpResultList *>(operand, opResultList)) {
1562 if (nb::try_cast<PyValue *>(operand, value)) {
1565 throw nb::value_error(
"is not a Value");
1569 std::string_view name, std::tuple<int, bool> opRegionSpec,
1570 nb::object operandSegmentSpecObj, nb::object resultSegmentSpecObj,
1571 std::optional<nb::sequence> resultTypeList, nb::sequence operandList,
1572 std::optional<nb::dict> attributes,
1573 std::optional<std::vector<PyBlock *>> successors,
1574 std::optional<int> regions,
PyLocation &location,
1575 const nb::object &maybeIp) {
1584 std::vector<int32_t> operandSegmentLengths;
1585 std::vector<int32_t> resultSegmentLengths;
1588 int opMinRegionCount = std::get<0>(opRegionSpec);
1589 bool opHasNoVariadicRegions = std::get<1>(opRegionSpec);
1591 regions = opMinRegionCount;
1593 if (*regions < opMinRegionCount) {
1594 throw nb::value_error(
join(
"Operation \"", name,
1595 "\" requires a minimum of ", opMinRegionCount,
1596 " regions but was built with regions=", *regions)
1599 if (opHasNoVariadicRegions && *regions > opMinRegionCount) {
1600 throw nb::value_error(
join(
"Operation \"", name,
1601 "\" requires a maximum of ", opMinRegionCount,
1602 " regions but was built with regions=", *regions)
1607 std::vector<PyType *> resultTypes;
1608 if (resultTypeList.has_value()) {
1610 resultSegmentLengths, resultTypes);
1614 std::vector<MlirValue> operands;
1615 operands.reserve(operands.size());
1617 if (operandSegmentSpecObj.is_none()) {
1619 for (nb::handle operand : operandList) {
1622 }
catch (nb::builtin_exception &err) {
1623 throw nb::value_error(
join(
"Operand ", index,
" of operation \"", name,
1624 "\" must be a Value (", err.what(),
")")
1631 auto operandSegmentSpec = nb::cast<std::vector<int>>(operandSegmentSpecObj);
1632 if (operandSegmentSpec.size() != nb::len(operandList)) {
1633 throw nb::value_error(
1634 join(
"Operation \"", name,
"\" requires ", operandSegmentSpec.size(),
1635 "operand segments but was provided ", nb::len(operandList))
1638 operandSegmentLengths.reserve(nb::len(operandList));
1639 for (
size_t i = 0, e = operandSegmentSpec.size(); i < e; ++i) {
1640 int segmentSpec = operandSegmentSpec[i];
1641 if (segmentSpec == 1 || segmentSpec == 0) {
1643 const nanobind::handle operand = operandList[i];
1644 if (!operand.is_none()) {
1647 }
catch (nb::builtin_exception &err) {
1648 throw nb::value_error(
join(
"Operand ", i,
" of operation \"", name,
1649 "\" must be a Value (", err.what(),
")")
1653 operandSegmentLengths.push_back(1);
1654 }
else if (segmentSpec == 0) {
1656 operandSegmentLengths.push_back(0);
1658 throw nb::value_error(
1659 join(
"Operand ", i,
" of operation \"", name,
1660 "\" must be a Value (was None and operand is not optional)")
1663 }
else if (segmentSpec == -1) {
1666 if (operandList[i].is_none()) {
1668 operandSegmentLengths.push_back(0);
1671 auto segment = nb::cast<nb::sequence>(operandList[i]);
1672 for (nb::handle segmentItem : segment) {
1675 operandSegmentLengths.push_back(nb::len(segment));
1677 }
catch (std::exception &err) {
1681 throw nb::value_error(
join(
"Operand ", i,
" of operation \"", name,
1682 "\" must be a Sequence of Values (",
1687 throw nb::value_error(
"Unexpected segment spec");
1693 if (!operandSegmentLengths.empty() || !resultSegmentLengths.empty()) {
1696 attributes = nb::dict(*attributes);
1698 attributes = nb::dict();
1700 if (attributes->contains(
"resultSegmentSizes") ||
1701 attributes->contains(
"operandSegmentSizes")) {
1702 throw nb::value_error(
"Manually setting a 'resultSegmentSizes' or "
1703 "'operandSegmentSizes' attribute is unsupported. "
1704 "Use Operation.create for such low-level access.");
1708 if (!resultSegmentLengths.empty()) {
1709 MlirAttribute segmentLengthAttr =
1711 resultSegmentLengths.data());
1712 (*attributes)[
"resultSegmentSizes"] =
1713 PyAttribute(context, segmentLengthAttr);
1717 if (!operandSegmentLengths.empty()) {
1718 MlirAttribute segmentLengthAttr =
1720 operandSegmentLengths.data());
1721 (*attributes)[
"operandSegmentSizes"] =
1722 PyAttribute(context, segmentLengthAttr);
1728 std::move(resultTypes),
1731 std::move(attributes),
1732 std::move(successors),
1733 *regions, location, maybeIp,
1738 const nb::object &operation) {
1739 nb::handle opViewType = nb::type<PyOpView>();
1740 nb::object instance = cls.attr(
"__new__")(cls);
1741 opViewType.attr(
"__init__")(instance, operation);
1749 operationObject(operation.getRef().getObject()) {}
1780 : refOperation(beforeOperationBase.getOperation().getRef()),
1784 : refOperation(beforeOperationRef), block((*refOperation)->getBlock()) {}
1789 throw nb::value_error(
1790 "Attempt to insert operation that is already attached");
1791 block.getParentOperation()->checkValid();
1792 MlirOperation beforeOp = {
nullptr};
1795 (*refOperation)->checkValid();
1796 beforeOp = (*refOperation)->get();
1802 throw nb::index_error(
"Cannot insert operation at the end of a block "
1803 "that already has a terminator. Did you mean to "
1804 "use 'InsertionPoint.at_block_terminator(block)' "
1805 "versus 'InsertionPoint(block)'?");
1814 if (mlirOperationIsNull(firstOp)) {
1821 block.getParentOperation()->getContext(), firstOp);
1827 if (mlirOperationIsNull(terminator))
1828 throw nb::value_error(
"Block has no terminator");
1830 block.getParentOperation()->getContext(), terminator);
1838 if (mlirOperationIsNull(nextOperation))
1852 const nb::object &excVal,
1853 const nb::object &excTb) {
1871 if (mlirAttributeIsNull(rawAttr))
1872 throw nb::python_error();
1880 "mlirTypeID was expected to be non-null.");
1885 nb::object thisObj = nb::cast(
this, nb::rv_policy::move);
1888 return typeCaster.value()(thisObj);
1896 : ownedName(new std::string(std::move(ownedName))) {
1918 throw nb::python_error();
1926 "mlirTypeID was expected to be non-null.");
1931 nb::object thisObj = nb::cast(
this, nb::rv_policy::move);
1934 return typeCaster.value()(thisObj);
1948 throw nb::python_error();
1964 MlirOperation owner;
1970 assert(
false &&
"Value must be an block arg or op result.");
1971 if (mlirOperationIsNull(owner))
1972 throw nb::python_error();
1977nb::typed<nb::object, std::variant<PyBlockArgument, PyOpResult, PyValue>>
1982 "mlirTypeID was expected to be non-null.");
1983 std::optional<nb::callable> valueCaster =
1989 thisObj = nb::cast<PyOpResult>(*
this, nb::rv_policy::move);
1991 thisObj = nb::cast<PyBlockArgument>(*
this, nb::rv_policy::move);
1993 assert(
false &&
"Value must be an block arg or op result.");
1995 return valueCaster.value()(thisObj);
2001 if (mlirValueIsNull(value))
2002 throw nb::python_error();
2004 return PyValue(ownerRef, value);
2012 : operation(operation.getOperation().getRef()) {
2015 throw nb::type_error(
"Operation is not a Symbol Table.");
2020 operation->checkValid();
2023 if (mlirOperationIsNull(symbol))
2024 throw nb::key_error(
2025 join(
"Symbol '", name,
"' not in the symbol table.").c_str());
2028 operation.getObject())
2033 operation->checkValid();
2044 erase(nb::cast<PyOperationBase &>(operation));
2048 operation->checkValid();
2052 if (mlirAttributeIsNull(symbolAttr))
2053 throw nb::value_error(
"Expected operation to have a symbol name.");
2064 MlirAttribute existingNameAttr =
2066 if (mlirAttributeIsNull(existingNameAttr))
2067 throw nb::value_error(
"Expected operation to have a symbol name.");
2073 const std::string &name) {
2078 MlirAttribute existingNameAttr =
2080 if (mlirAttributeIsNull(existingNameAttr))
2081 throw nb::value_error(
"Expected operation to have a symbol name.");
2082 MlirAttribute newNameAttr =
2091 MlirAttribute existingVisAttr =
2093 if (mlirAttributeIsNull(existingVisAttr))
2094 throw nb::value_error(
"Expected operation to have a symbol visibility.");
2099 const std::string &visibility) {
2100 if (visibility !=
"public" && visibility !=
"private" &&
2101 visibility !=
"nested")
2102 throw nb::value_error(
2103 "Expected visibility to be 'public', 'private' or 'nested'");
2107 MlirAttribute existingVisAttr =
2109 if (mlirAttributeIsNull(existingVisAttr))
2110 throw nb::value_error(
"Expected operation to have a symbol visibility.");
2117 const std::string &newSymbol,
2125 throw nb::value_error(
"Symbol rename failed");
2129 bool allSymUsesVisible,
2130 nb::object callback) {
2135 nb::object callback;
2137 std::string exceptionWhat;
2138 nb::object exceptionType;
2141 fromOperation.
getContext(), std::move(callback),
false, {}, {}};
2143 fromOperation.
get(), allSymUsesVisible,
2144 [](MlirOperation foundOp,
bool isVisible,
void *calleeUserDataVoid) {
2145 UserData *calleeUserData = static_cast<UserData *>(calleeUserDataVoid);
2147 PyOperation::forOperation(calleeUserData->context, foundOp);
2148 if (calleeUserData->gotException)
2151 calleeUserData->callback(pyFoundOp.getObject(), isVisible);
2152 } catch (nb::python_error &e) {
2153 calleeUserData->gotException =
true;
2154 calleeUserData->exceptionWhat = e.what();
2155 calleeUserData->exceptionType = nb::borrow(e.type());
2158 static_cast<void *
>(&userData));
2159 if (userData.gotException) {
2160 std::string message(
"Exception raised in callback: ");
2161 message.append(userData.exceptionWhat);
2162 throw std::runtime_error(message);
2173 "Returns the block that owns this argument.");
2179 "Returns the position of this argument in the block's argument list.");
2185 "type"_a,
"Sets the type of this block argument.");
2188 [](PyBlockArgument &self, PyLocation loc) {
2191 "loc"_a,
"Sets the location of this block argument.");
2195 MlirBlock block,
intptr_t startIndex,
2199 operation(std::move(operation)), block(block) {}
2207 "Returns a list of types for all arguments in this argument list.");
2210intptr_t PyBlockArgumentList::getRawNumElements() {
2232 operation(operation) {}
2241 "Sets the operand at the specified index to a new value.");
2244intptr_t PyOpOperandList::getRawNumElements() {
2252 return PyValue(pyOwner, operand);
2266 static constexpr const char *
pyClassName =
"OpOperands";
2275 operation(operation) {}
2282 operation->checkValid();
2304 operation(operation) {}
2313 "Sets the successor block at the specified index.");
2316intptr_t PyOpSuccessors::getRawNumElements() {
2323 return PyBlock(operation, block);
2337 operation(operation), block(block) {}
2339intptr_t PyBlockSuccessors::getRawNumElements() {
2346 return PyBlock(operation, block);
2362 operation(operation), block(block) {}
2364intptr_t PyBlockPredecessors::getRawNumElements() {
2371 return PyBlock(operation, block);
2380nb::typed<nb::object, PyAttribute>
2382 MlirAttribute attr =
2384 if (mlirAttributeIsNull(attr)) {
2385 throw nb::key_error(
"attempt to access a non-existent attribute");
2390nb::typed<nb::object, std::optional<PyAttribute>>
2392 MlirAttribute attr =
2394 if (mlirAttributeIsNull(attr))
2395 return defaultValue;
2403 if (index < 0 || index >=
dunderLen()) {
2404 throw nb::index_error(
"attempt to access out of bounds attribute");
2424 throw nb::key_error(
"attempt to delete a non-existent attribute");
2432 return !mlirAttributeIsNull(
2437 MlirOperation op, std::function<
void(
MlirStringRef, MlirAttribute)> fn) {
2447 nb::class_<PyOpAttributeMap>(m,
"OpAttributeMap")
2449 "Checks if an attribute with the given name exists in the map.")
2451 "Returns the number of attributes in the map.")
2453 "Gets an attribute by name.")
2455 "Gets a named attribute by index.")
2457 "Sets an attribute with the given name.")
2459 "Deletes an attribute with the given name.")
2461 nb::arg(
"default") = nb::none(),
2462 "Gets an attribute by name or the default value, if it does not "
2470 keys.append(nb::str(name.data, name.length));
2472 return nb::iter(keys);
2474 "Iterates over attribute names.")
2481 out.append(nb::str(name.data, name.length));
2485 "Returns a list of attribute names.")
2492 out.append(PyAttribute(self.operation->getContext(), attr)
2497 "Returns a list of attribute values.")
2501 -> nb::typed<nb::list,
2502 nb::typed<nb::tuple, nb::str, PyAttribute>> {
2505 self.operation->
get(),
2507 out.append(nb::make_tuple(
2508 nb::str(name.data, name.length),
2509 PyAttribute(self.operation->getContext(), attr)
2514 "Returns a list of `(name, attribute)` tuples.");
2518 nb::class_<PyOpAdaptor>(m,
"OpAdaptor")
2520 "Creates an OpAdaptor with the given operands and attributes.",
2521 "operands"_a,
"attributes"_a)
2522 .def(nb::init<nb::typed<nb::list, PyValue>,
PyOpView &>(),
2523 "Creates an OpAdaptor with the given operands and operation view.",
2524 "operands"_a,
"opview"_a)
2526 "operands", [](
PyOpAdaptor &self) {
return self.operands; },
2527 "Returns the operands of the adaptor.")
2529 "attributes", [](
PyOpAdaptor &self) {
return self.attributes; },
2530 "Returns the attributes of the adaptor.");
2534 const char *methodName) {
2535 nb::handle targetObj(
static_cast<PyObject *
>(userData));
2536 if (!nb::hasattr(targetObj, methodName))
2540 bool success = nb::cast<bool>(targetObj.attr(methodName)(opView));
2544static bool attachOpTrait(
const nb::object &opName, MlirDynamicOpTrait trait,
2546 std::string opNameStr;
2547 if (opName.is_type()) {
2548 opNameStr = nb::cast<std::string>(opName.attr(
"OPERATION_NAME"));
2549 }
else if (nb::isinstance<nb::str>(opName)) {
2550 opNameStr = nb::cast<std::string>(opName);
2552 throw nb::type_error(
"the root argument must be a type or a string");
2560 const nb::object &
target,
2562 if (!nb::hasattr(
target,
"verify_invariants") &&
2563 !nb::hasattr(
target,
"verify_region_invariants"))
2564 throw nb::type_error(
2565 "the target object must have at least one of 'verify_invariants' or "
2566 "'verify_region_invariants' methods");
2569 callbacks.
construct = [](
void *userData) {
2570 nb::handle(
static_cast<PyObject *
>(userData)).inc_ref();
2572 callbacks.
destruct = [](
void *userData) {
2573 nb::handle(
static_cast<PyObject *
>(userData)).dec_ref();
2593 static_cast<void *
>(
target.ptr()));
2598 nb::class_<PyDynamicOpTrait> cls(m,
"DynamicOpTrait");
2600 [](
const nb::object &cls,
const nb::object &opName, nb::object
target,
2606 nb::arg(
"cls"), nb::arg(
"op_name"), nb::arg(
"target").none() = nb::none(),
2607 nb::arg(
"context").none() = nb::none(),
2608 "Attach the dynamic op trait subclass to the given operation name.");
2618 nb::class_<PyDynamicOpTraits::IsTerminator, PyDynamicOpTrait> cls(
2619 m,
"IsTerminatorTrait");
2622 [](
const nb::object &cls,
const nb::object &opName,
2626 "Attach IsTerminator trait to the given operation name.", nb::arg(
"cls"),
2627 nb::arg(
"op_name"), nb::arg(
"context").none() = nb::none());
2637 nb::class_<PyDynamicOpTraits::NoTerminator, PyDynamicOpTrait> cls(
2638 m,
"NoTerminatorTrait");
2641 [](
const nb::object &cls,
const nb::object &opName,
2645 "Attach NoTerminator trait to the given operation name.", nb::arg(
"cls"),
2646 nb::arg(
"op_name"), nb::arg(
"context").none() = nb::none());
2655using namespace mlir::python::MLIR_BINDINGS_PYTHON_DOMAIN;
2657MlirLocation tracebackToLocation(MlirContext ctx) {
2658#if defined(Py_LIMITED_API)
2663 size_t framesLimit =
2666 thread_local std::array<MlirLocation, PyGlobals::TracebackLoc::kMaxFrames>
2670 nb::gil_scoped_acquire acquire;
2672 PyThreadState *tstate = PyThreadState_GET();
2673 PyFrameObject *next;
2674 PyFrameObject *pyFrame = PyThreadState_GetFrame(tstate);
2680 for (; pyFrame !=
nullptr && count < framesLimit;
2681 next = PyFrame_GetBack(pyFrame), Py_XDECREF(pyFrame), pyFrame = next) {
2682 PyCodeObject *code = PyFrame_GetCode(pyFrame);
2684 nb::cast<std::string>(nb::borrow<nb::str>(code->co_filename));
2685 std::string_view fileName(fileNameStr);
2686 if (!
PyGlobals::get().getTracebackLoc().isUserTracebackFilename(fileName))
2690#if PY_VERSION_HEX < 0x030B00F0
2692 nb::cast<std::string>(nb::borrow<nb::str>(code->co_name));
2693 std::string_view funcName(name);
2694 int startLine = PyFrame_GetLineNumber(pyFrame);
2700 nb::cast<std::string>(nb::borrow<nb::str>(code->co_qualname));
2701 std::string_view funcName(name);
2702 int startLine, startCol, endLine, endCol;
2703 int lasti = PyFrame_GetLasti(pyFrame);
2704 if (!PyCode_Addr2Location(code, lasti, &startLine, &startCol, &endLine,
2706 throw nb::python_error();
2710 startCol, endLine, endCol);
2719 Py_XDECREF(pyFrame);
2724 MlirLocation callee = frames[0];
2729 MlirLocation caller = frames[count - 1];
2731 for (
int i = count - 2; i >= 1; i--)
2740applyCurrentLocAction(MlirContext ctx, MlirLocation baseLoc,
2743 if (action == Action::Fallback)
2750 "Location.current must belong to the current MLIR context");
2756 thread_local std::vector<MlirStringRef> scopeNames;
2758 MlirLocation
walk = currentLoc->get();
2763 for (
auto it = scopeNames.rbegin(); it != scopeNames.rend(); ++it)
2769maybeGetTracebackLocation(
const std::optional<PyLocation> &location) {
2774 if (!tbl.locTracebacksEnabled())
2775 return location.has_value() ? location.value()
2781 MlirLocation baseLoc;
2784 if (location.has_value()) {
2785 switch (tbl.tracebackActionOnExplicitLoc()) {
2786 case OnExplicit::UseExplicit:
2787 baseLoc = location->get();
2789 case OnExplicit::UseTraceback:
2790 baseLoc = tracebackToLocation(ctx.
get());
2794 baseLoc = tracebackToLocation(ctx.
get());
2798 baseLoc = applyCurrentLocAction(ctx.
get(), baseLoc,
2799 tbl.tracebackActionOnCurrentLoc());
2802 return {ref, baseLoc};
2814 std::string s = nb::cast<std::string>(nb::str(accum.
join()));
2815 std::string_view sv(s);
2816 if (sv.size() > 5) {
2817 sv.remove_prefix(4);
2818 sv.remove_suffix(1);
2820 return std::string(sv);
2822 auto indent = [](std::string s) {
2824 while ((pos = s.find(
'\n', pos)) != std::string::npos) {
2825 s.replace(pos, 1,
"\n ");
2831 std::ostringstream os;
2836 os <<
"\nerror: " << locStr(
diag.location) <<
": " << indent(
diag.message);
2837 for (
const auto ¬e :
diag.notes) {
2838 os <<
"\n note: " << locStr(note.location) <<
": "
2839 << indent(note.message);
2846 auto cls = nb::exception<MLIRError>(m,
"MLIRError", PyExc_Exception);
2847 nb::register_exception_translator(
2848 [](
const std::exception_ptr &p,
void *payload) {
2851 std::rethrow_exception(p);
2854 nb::object ty = nb::borrow(
static_cast<PyObject *
>(payload));
2855 nb::object obj = ty(formatted);
2856 obj.attr(
"_message") = nb::cast(std::move(e.
message));
2857 obj.attr(
"_error_diagnostics") =
2859 PyErr_SetObject(
static_cast<PyObject *
>(payload), obj.ptr());
2863 auto propertyType = nb::borrow<nb::type_object>(
2864 reinterpret_cast<PyObject *
>(&PyProperty_Type));
2867 propertyType(nb::cpp_function(
2868 [](nb::object self) -> nb::str {
return self.attr(
"_message"); },
2870 nb::setattr(cls,
"error_diagnostics",
2871 propertyType(nb::cpp_function(
2873 -> nb::typed<nb::list, PyDiagnostic::DiagnosticInfo> {
2874 return self.attr(
"_error_diagnostics");
2880 m.attr(
"T") = nb::type_var(
"T");
2881 m.attr(
"U") = nb::type_var(
"U");
2885 nb::enum_<PyGlobals::TracebackLoc::OnExplicitAction>(m,
"OnExplicitAction")
2886 .value(
"USE_EXPLICIT",
2888 .value(
"USE_TRACEBACK",
2891 nb::enum_<PyGlobals::TracebackLoc::CurrentLocAction>(m,
"CurrentLocAction")
2893 .value(
"NAMELOC_WRAP",
2896 nb::class_<PyGlobals>(m,
"_Globals")
2897 .def_prop_rw(
"dialect_search_modules",
2903 "_check_dialect_module_loaded",
2904 [](
PyGlobals &self,
const std::string &dialectNamespace) {
2907 "dialect_namespace"_a)
2909 "dialect_namespace"_a,
"dialect_class"_a, nb::kw_only(),
2910 "replace"_a =
false,
2911 "Testing hook for directly registering a dialect")
2913 "operation_name"_a,
"operation_class"_a, nb::kw_only(),
2914 "replace"_a =
false,
2915 "Testing hook for directly registering an operation")
2916 .def(
"loc_tracebacks_enabled",
2920 .def(
"set_loc_tracebacks_enabled",
2924 .def(
"loc_tracebacks_frame_limit",
2928 .def(
"set_loc_tracebacks_frame_limit",
2929 [](
PyGlobals &self, std::optional<int> n) {
2933 .def(
"register_traceback_file_inclusion",
2934 [](
PyGlobals &self,
const std::string &filename) {
2937 .def(
"register_traceback_file_exclusion",
2938 [](
PyGlobals &self,
const std::string &filename) {
2941 .def(
"traceback_action_on_explicit_loc",
2945 .def(
"set_traceback_action_on_explicit_loc",
2950 .def(
"traceback_action_on_current_loc",
2954 .def(
"set_traceback_action_on_current_loc",
2963 m.attr(
"globals") = nb::cast(
new PyGlobals, nb::rv_policy::take_ownership);
2968 [](nb::type_object pyClass) {
2969 std::string dialectNamespace =
2970 nb::cast<std::string>(pyClass.attr(
"DIALECT_NAMESPACE"));
2975 "Class decorator for registering a custom Dialect wrapper");
2977 "register_operation",
2978 [](
const nb::type_object &dialectClass,
bool replace) -> nb::object {
2979 return nb::cpp_function(
2981 replace](nb::type_object opClass) -> nb::type_object {
2982 std::string operationName =
2983 nb::cast<std::string>(opClass.attr(
"OPERATION_NAME"));
2987 nb::object opClassName = opClass.attr(
"__name__");
2988 dialectClass.attr(opClassName) = opClass;
2993 nb::sig(
"def register_operation(dialect_class: type, *, replace: bool = False) "
2994 "-> typing.Callable[[type[T]], type[T]]"),
2996 "dialect_class"_a, nb::kw_only(),
"replace"_a =
false,
2997 "Produce a class decorator for registering an Operation class as part of "
3000 "register_op_adaptor",
3001 [](
const nb::type_object &opClass,
bool replace) -> nb::object {
3002 return nb::cpp_function(
3004 replace](nb::type_object adaptorClass) -> nb::type_object {
3005 std::string operationName =
3006 nb::cast<std::string>(adaptorClass.attr(
"OPERATION_NAME"));
3008 adaptorClass, replace);
3010 opClass.attr(
"Adaptor") = adaptorClass;
3011 return adaptorClass;
3015 nb::sig(
"def register_op_adaptor(op_class: type, *, replace: bool = False) "
3016 "-> typing.Callable[[type[T]], type[T]]"),
3018 "op_class"_a, nb::kw_only(),
"replace"_a =
false,
3019 "Produce a class decorator for registering an OpAdaptor class for an "
3023 [](
PyTypeID mlirTypeID,
bool replace) -> nb::object {
3024 return nb::cpp_function([mlirTypeID, replace](
3025 nb::callable typeCaster) -> nb::object {
3031 nb::sig(
"def register_type_caster(typeid: _mlir.ir.TypeID, *, replace: bool = False) "
3032 "-> typing.Callable[[typing.Callable[[T], U]], typing.Callable[[T], U]]"),
3034 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
3035 "Register a type caster for casting MLIR types to custom user types.");
3038 [](
PyTypeID mlirTypeID,
bool replace) -> nb::object {
3039 return nb::cpp_function(
3040 [mlirTypeID, replace](nb::callable valueCaster) -> nb::object {
3047 nb::sig(
"def register_value_caster(typeid: _mlir.ir.TypeID, *, replace: bool = False) "
3048 "-> typing.Callable[[typing.Callable[[T], U]], typing.Callable[[T], U]]"),
3050 "typeid"_a, nb::kw_only(),
"replace"_a =
false,
3051 "Register a value caster for casting MLIR values to custom user values.");
3061 nb::enum_<PyDiagnosticSeverity>(m,
"DiagnosticSeverity")
3067 nb::enum_<PyWalkOrder>(m,
"WalkOrder")
3070 nb::enum_<PyWalkResult>(m,
"WalkResult")
3078 nb::class_<PyDiagnostic>(m,
"Diagnostic")
3080 "Returns the severity of the diagnostic.")
3082 "Returns the location associated with the diagnostic.")
3084 "Returns the message text of the diagnostic.")
3086 "Returns a tuple of attached note diagnostics.")
3091 return nb::str(
"<Invalid Diagnostic>");
3094 "Returns the diagnostic message as a string.");
3096 nb::class_<PyDiagnostic::DiagnosticInfo>(m,
"DiagnosticInfo")
3102 "diag"_a,
"Creates a DiagnosticInfo from a Diagnostic.")
3104 "The severity level of the diagnostic.")
3106 "The location associated with the diagnostic.")
3108 "The message text of the diagnostic.")
3110 "List of attached note diagnostics.")
3114 "Returns the diagnostic message as a string.");
3116 nb::class_<PyDiagnosticHandler>(m,
"DiagnosticHandler")
3118 "Detaches the diagnostic handler from the context.")
3120 "Returns True if the handler is attached to a context.")
3122 "Returns True if an error was encountered during diagnostic "
3125 "Enters the diagnostic handler as a context manager.",
3126 nb::sig(
"def __enter__(self, /) -> DiagnosticHandler"))
3128 "exc_value"_a.none(),
"traceback"_a.none(),
3129 "Exits the diagnostic handler context manager.");
3132 nb::class_<PyThreadPool>(m,
"ThreadPool")
3135 "Creates a new thread pool with default concurrency.")
3137 "Returns the maximum number of threads in the pool.")
3139 "Returns the raw pointer to the LLVM thread pool as a string.");
3141 nb::class_<PyMlirContext>(m,
"Context")
3149 Creates a new MLIR context.
3151 The context is the top-level container for all MLIR objects. It owns the storage
3152 for types, attributes, locations, and other core IR objects. A context can be
3153 configured to allow or disallow unregistered dialects and can have dialects
3154 loaded on-demand.)")
3156 "Gets the number of live Context objects.")
3158 "_get_context_again",
3159 [](
PyMlirContext &self) -> nb::typed<nb::object, PyMlirContext> {
3163 "Gets another reference to the same context.")
3165 "Gets the number of live modules owned by this context.")
3167 "Gets a capsule wrapping the MlirContext.")
3170 "Creates a Context from a capsule wrapping MlirContext.")
3172 "Enters the context as a context manager.",
3173 nb::sig(
"def __enter__(self, /) -> Context"))
3175 "exc_value"_a.none(),
"traceback"_a.none(),
3176 "Exits the context manager.")
3177 .def_prop_ro_static(
3180 -> std::optional<nb::typed<nb::object, PyMlirContext>> {
3184 return nb::cast(context);
3186 nb::sig(
"def current(/) -> Context | None"),
3187 "Gets the Context bound to the current thread or returns None if no "
3192 "Gets a container for accessing dialects by name.")
3195 "Alias for `dialects`.")
3197 "get_dialect_descriptor",
3200 self.get(), {name.data(), name.size()});
3202 throw nb::value_error(
3203 join(
"Dialect '", name,
"' not found").c_str());
3208 "Gets or loads a dialect by name, returning its descriptor object.")
3210 "allow_unregistered_dialects",
3217 "Controls whether unregistered dialects are allowed in this context.")
3220 "Attaches a diagnostic handler that will receive callbacks.")
3222 "enable_multithreading",
3228 Enables or disables multi-threading support in the context.
3231 enable: Whether to enable (True) or disable (False) multi-threading.
3243 Sets a custom thread pool for the context to use.
3246 pool: A ThreadPool object to use for parallel operations.
3249 Multi-threading is automatically disabled before setting the thread pool.)")
3255 "Gets the number of threads in the context's thread pool.")
3257 "_mlir_thread_pool_ptr",
3260 std::stringstream ss;
3264 "Gets the raw pointer to the LLVM thread pool as a string.")
3266 "is_registered_operation",
3273 Checks whether an operation with the given name is registered.
3276 operation_name: The fully qualified name of the operation (e.g., `arith.addf`).
3279 True if the operation is registered, False otherwise.)")
3281 "append_dialect_registry",
3287 Appends the contents of a dialect registry to the context.
3290 registry: A DialectRegistry containing dialects to append.)")
3291 .def_prop_rw("emit_error_diagnostics",
3295 Controls whether error diagnostics are emitted to diagnostic handlers.
3297 By default, error diagnostics are captured and reported through MLIRError exceptions.)")
3299 "load_all_available_dialects",
3304 Loads all dialects available in the registry into the context.
3306 This eagerly loads all dialects that have been registered, making them
3307 immediately available for use.)");
3312 nb::class_<PyDialectDescriptor>(m,
"DialectDescriptor")
3319 "Returns the namespace of the dialect.")
3324 std::string repr(
"<DialectDescriptor ");
3329 nb::sig(
"def __repr__(self) -> str"),
3330 "Returns a string representation of the dialect descriptor.");
3335 nb::class_<PyDialects>(m,
"Dialects")
3339 MlirDialect dialect =
3340 self.getDialectForKey(keyName,
false);
3341 nb::object descriptor =
3345 "Gets a dialect by name using subscript notation.")
3348 [=](
PyDialects &self, std::string attrName) {
3349 MlirDialect dialect =
3350 self.getDialectForKey(attrName,
true);
3351 nb::object descriptor =
3355 "Gets a dialect by name using attribute notation.");
3360 nb::class_<PyDialect>(m,
"Dialect")
3361 .def(nb::init<nb::object>(),
"descriptor"_a,
3362 "Creates a Dialect from a DialectDescriptor.")
3364 "descriptor", [](
PyDialect &self) {
return self.getDescriptor(); },
3365 "Returns the DialectDescriptor for this dialect.")
3368 [](
const nb::object &self) {
3369 auto clazz = self.attr(
"__class__");
3370 return nb::str(
"<Dialect ") +
3371 self.attr(
"descriptor").attr(
"namespace") +
3372 nb::str(
" (class ") + clazz.attr(
"__module__") +
3373 nb::str(
".") + clazz.attr(
"__name__") + nb::str(
")>");
3375 nb::sig(
"def __repr__(self) -> str"),
3376 "Returns a string representation of the dialect.");
3381 nb::class_<PyDialectRegistry>(m,
"DialectRegistry")
3383 "Gets a capsule wrapping the MlirDialectRegistry.")
3386 "Creates a DialectRegistry from a capsule wrapping "
3387 "`MlirDialectRegistry`.")
3388 .def(nb::init<>(),
"Creates a new empty dialect registry.");
3393 nb::class_<PyLocation>(m,
"Location")
3395 "Gets a capsule wrapping the MlirLocation.")
3397 "Creates a Location from a capsule wrapping MlirLocation.")
3399 "Enters the location as a context manager.",
3400 nb::sig(
"def __enter__(self, /) -> Location"))
3402 "exc_value"_a.none(),
"traceback"_a.none(),
3403 "Exits the location context manager.")
3409 "Compares two locations for equality.")
3411 "__eq__", [](
PyLocation &self, nb::object other) {
return false; },
3412 "Compares location with non-location object (always returns False).")
3413 .def_prop_ro_static(
3415 [](nb::object & ) -> std::optional<PyLocation *> {
3418 return std::nullopt;
3422 nb::sig(
"def current(/) -> Location | None"),
3424 "Gets the Location bound to the current thread or raises ValueError.")
3431 "context"_a = nb::none(),
3432 "Gets a Location representing an unknown location.")
3435 [](
PyLocation callee,
const std::vector<PyLocation> &frames,
3438 throw nb::value_error(
"No caller frames provided.");
3439 MlirLocation caller = frames.back().get();
3440 for (
size_t index = frames.size() - 1;
index-- > 0;) {
3446 "callee"_a,
"frames"_a,
"context"_a = nb::none(),
3447 "Gets a Location representing a caller and callsite.")
3449 "Returns True if this location is a CallSiteLoc.")
3456 "Gets the callee location from a CallSiteLoc.")
3463 "Gets the caller location from a CallSiteLoc.")
3466 [](std::string filename,
int line,
int col,
3473 "filename"_a,
"line"_a,
"col"_a,
"context"_a = nb::none(),
3474 "Gets a Location representing a file, line and column.")
3477 [](std::string filename,
int startLine,
int startCol,
int endLine,
3482 startLine, startCol, endLine, endCol));
3484 "filename"_a,
"start_line"_a,
"start_col"_a,
"end_line"_a,
3485 "end_col"_a,
"context"_a = nb::none(),
3486 "Gets a Location representing a file, line and column range.")
3488 "Returns True if this location is a FileLineColLoc.")
3495 "Gets the filename from a FileLineColLoc.")
3497 "Gets the start line number from a `FileLineColLoc`.")
3499 "Gets the start column number from a `FileLineColLoc`.")
3501 "Gets the end line number from a `FileLineColLoc`.")
3503 "Gets the end column number from a `FileLineColLoc`.")
3506 [](
const std::vector<PyLocation> &pyLocations,
3507 std::optional<PyAttribute> metadata,
3509 std::vector<MlirLocation> locations;
3510 locations.reserve(pyLocations.size());
3511 for (
const PyLocation &pyLocation : pyLocations)
3512 locations.push_back(pyLocation.get());
3514 context->
get(), locations.size(), locations.data(),
3515 metadata ? metadata->
get() : MlirAttribute{0});
3516 return PyLocation(context->getRef(), location);
3518 "locations"_a,
"metadata"_a = nb::none(),
"context"_a = nb::none(),
3519 "Gets a Location representing a fused location with optional "
3522 "Returns True if this location is a `FusedLoc`.")
3527 std::vector<MlirLocation> locations(numLocations);
3530 std::vector<PyLocation> pyLocations{};
3531 pyLocations.reserve(numLocations);
3532 for (
unsigned i = 0; i < numLocations; ++i)
3533 pyLocations.emplace_back(self.getContext(), locations[i]);
3536 "Gets the list of locations from a `FusedLoc`.")
3539 [](std::string name, std::optional<PyLocation> childLoc,
3545 childLoc ? childLoc->get()
3548 "name"_a,
"childLoc"_a = nb::none(),
"context"_a = nb::none(),
3549 "Gets a Location representing a named location with optional child "
3552 "Returns True if this location is a `NameLoc`.")
3558 "Gets the name string from a `NameLoc`.")
3565 "Gets the child location from a `NameLoc`.")
3572 "attribute"_a,
"context"_a = nb::none(),
3573 "Gets a Location from a `LocationAttr`.")
3576 [](
PyLocation &self) -> nb::typed<nb::object, PyMlirContext> {
3577 return self.getContext().getObject();
3579 "Context that owns the `Location`.")
3586 "Get the underlying `LocationAttr`.")
3594 Emits an error diagnostic at this location.
3597 message: The error message to emit.)")
3604 return printAccum.
join();
3606 "Returns the assembly representation of the location.");
3611 nb::class_<PyModule>(m,
"Module", nb::is_weak_referenceable())
3613 "Gets a capsule wrapping the MlirModule.")
3616 Creates a Module from a `MlirModule` wrapped by a capsule (i.e. `module._CAPIPtr`).
3618 This returns a new object **BUT** `_clear_mlir_module(module)` must be called to
3619 prevent double-frees (of the underlying `mlir::Module`).)")
3622 Clears the internal MLIR module reference.
3624 This is used internally to prevent double-free when ownership is transferred
3625 via the C API capsule mechanism. Not intended for normal use.)")
3629 -> nb::typed<nb::object, PyModule> {
3633 if (mlirModuleIsNull(module))
3634 throw MLIRError(
"Unable to parse module assembly", errors.
take());
3641 -> nb::typed<nb::object, PyModule> {
3645 if (mlirModuleIsNull(module))
3646 throw MLIRError(
"Unable to parse module assembly", errors.
take());
3653 -> nb::typed<nb::object, PyModule> {
3657 if (mlirModuleIsNull(module))
3658 throw MLIRError(
"Unable to parse module assembly", errors.
take());
3664 [](
const std::optional<PyLocation> &loc)
3665 -> nb::typed<nb::object, PyModule> {
3666 PyLocation pyLoc = maybeGetTracebackLocation(loc);
3670 "loc"_a = nb::none(),
"Creates an empty module.")
3673 [](
PyModule &self) -> nb::typed<nb::object, PyMlirContext> {
3674 return self.getContext().getObject();
3676 "Context that created the `Module`.")
3679 [](
PyModule &self) -> nb::typed<nb::object, PyOperation> {
3682 self.getRef().releaseObject())
3685 "Accesses the module as an operation.")
3691 self.getRef().releaseObject());
3695 "Return the block for this module.")
3704 [](
const nb::object &self) {
3706 return self.attr(
"operation").attr(
"__str__")();
3708 nb::sig(
"def __str__(self) -> str"),
3710 Gets the assembly form of the operation with default options.
3712 If more advanced control over the assembly formatting or I/O options is needed,
3713 use the dedicated print or get_asm method, which supports keyword arguments to
3721 "other"_a,
"Compares two modules for equality.")
3725 "Returns the hash value of the module.");
3730 nb::class_<PyOperationBase>(m,
"_OperationBase")
3734 return self.getOperation().getCapsule();
3736 "Gets a capsule wrapping the `MlirOperation`.")
3741 other.getOperation().
get());
3743 "Compares two operations for equality.")
3747 "Compares operation with non-operation object (always returns "
3754 "Returns the hash value of the operation.")
3760 "Returns a dictionary-like map of operation attributes.")
3764 PyOperation &concreteOperation = self.getOperation();
3768 "Context that owns the operation.")
3772 auto &concreteOperation = self.getOperation();
3773 concreteOperation.checkValid();
3774 MlirOperation operation = concreteOperation.
get();
3777 "Returns the fully qualified name of the operation.")
3783 "Returns the list of operation operands.")
3789 "Returns the list of op operands.")
3795 "Returns the list of operation regions.")
3801 "Returns the list of Operation results.")
3805 auto &operation = self.getOperation();
3809 "Shortcut to get an op result if it has only one (throws an error "
3822 nb::for_getter(
"Returns the source location the operation was "
3823 "defined or derived from."),
3824 nb::for_setter(
"Sets the source location the operation was defined "
3825 "or derived from."))
3829 -> std::optional<nb::typed<nb::object, PyOperation>> {
3830 auto parent = self.getOperation().getParentOperation();
3832 return parent->getObject();
3835 "Returns the parent operation, or `None` if at top level.")
3839 return self.getAsm(
false,
3850 nb::sig(
"def __str__(self) -> str"),
3851 "Returns the assembly form of the operation.")
3853 nb::overload_cast<PyAsmState &, nb::object, bool>(
3855 "state"_a,
"file"_a = nb::none(),
"binary"_a =
false,
3857 Prints the assembly form of the operation to a file like object.
3860 state: `AsmState` capturing the operation numbering and flags.
3861 file: Optional file like object to write to. Defaults to sys.stdout.
3862 binary: Whether to write `bytes` (True) or `str` (False). Defaults to False.)")
3864 nb::overload_cast<std::optional<int64_t>, std::optional<int64_t>,
3865 bool,
bool,
bool,
bool,
bool,
bool, nb::object,
3868 "large_elements_limit"_a = nb::none(),
3869 "large_resource_limit"_a = nb::none(),
"enable_debug_info"_a =
false,
3870 "pretty_debug_info"_a =
false,
"print_generic_op_form"_a =
false,
3871 "use_local_scope"_a =
false,
"use_name_loc_as_prefix"_a =
false,
3872 "assume_verified"_a =
false,
"file"_a = nb::none(),
3873 "binary"_a =
false,
"skip_regions"_a =
false,
3875 Prints the assembly form of the operation to a file like object.
3878 large_elements_limit: Whether to elide elements attributes above this
3879 number of elements. Defaults to None (no limit).
3880 large_resource_limit: Whether to elide resource attributes above this
3881 number of characters. Defaults to None (no limit). If large_elements_limit
3882 is set and this is None, the behavior will be to use large_elements_limit
3883 as large_resource_limit.
3884 enable_debug_info: Whether to print debug/location information. Defaults
3886 pretty_debug_info: Whether to format debug information for easier reading
3887 by a human (warning: the result is unparseable). Defaults to False.
3888 print_generic_op_form: Whether to print the generic assembly forms of all
3889 ops. Defaults to False.
3890 use_local_scope: Whether to print in a way that is more optimized for
3891 multi-threaded access but may not be consistent with how the overall
3893 use_name_loc_as_prefix: Whether to use location attributes (NameLoc) as
3894 prefixes for the SSA identifiers. Defaults to False.
3895 assume_verified: By default, if not printing generic form, the verifier
3896 will be run and if it fails, generic form will be printed with a comment
3897 about failed verification. While a reasonable default for interactive use,
3898 for systematic use, it is often better for the caller to verify explicitly
3899 and report failures in a more robust fashion. Set this to True if doing this
3900 in order to avoid running a redundant verification. If the IR is actually
3901 invalid, behavior is undefined.
3902 file: The file like object to write to. Defaults to sys.stdout.
3903 binary: Whether to write bytes (True) or str (False). Defaults to False.
3904 skip_regions: Whether to skip printing regions. Defaults to False.)")
3906 "desired_version"_a = nb::none(),
3908 Write the bytecode form of the operation to a file like object.
3911 file: The file like object to write to.
3912 desired_version: Optional version of bytecode to emit.
3914 The bytecode writer status.)")
3917 "binary"_a =
false,
"large_elements_limit"_a = nb::none(),
3918 "large_resource_limit"_a = nb::none(),
"enable_debug_info"_a =
false,
3919 "pretty_debug_info"_a =
false,
"print_generic_op_form"_a =
false,
3920 "use_local_scope"_a =
false,
"use_name_loc_as_prefix"_a =
false,
3921 "assume_verified"_a =
false,
"skip_regions"_a =
false,
3923 Gets the assembly form of the operation with all options available.
3926 binary: Whether to return a bytes (True) or str (False) object. Defaults to
3928 ... others ...: See the print() method for common keyword arguments for
3929 configuring the printout.
3931 Either a bytes or str object, depending on the setting of the `binary`
3934 "Verify the operation. Raises MLIRError if verification fails, and "
3935 "returns true otherwise.")
3937 "Puts self immediately after the other operation in its parent "
3940 "Puts self immediately before the other operation in its parent "
3944 Checks if this operation is before another in the same block.
3947 other: Another operation in the same parent block.
3950 True if this operation is before `other` in the operation list of the parent block.)")
3954 const nb::object &ip) -> nb::typed<nb::object, PyOperation> {
3955 return self.getOperation().clone(ip);
3957 "ip"_a = nb::none(),
3959 Creates a deep copy of the operation.
3962 ip: Optional insertion point where the cloned operation should be inserted.
3963 If None, the current insertion point is used. If False, the operation
3967 A new Operation that is a clone of this operation.)")
3969 "detach_from_parent",
3974 throw nb::value_error(
"Detached operation has no parent.");
3979 "Detaches the operation from its parent block.")
3987 "Reports if the operation is attached to its parent block.")
3991 Erases the operation and frees its memory.
3994 After erasing, any Python references to the operation become invalid.)")
3999 PyWalkOrder walkOrder, std::optional<nb::object> opClass) {
4001 return self.walk(callback, walkOrder);
4006 self.getOperation().getContext(), mlirOp)
4008 if (nb::isinstance(opview, *opClass))
4009 return callback(mlirOp);
4015 "op_class"_a = nb::none(),
4017 nb::sig(
"def walk(self, callback: Callable[[Operation], WalkResult], walk_order: WalkOrder = ..., op_class: type[OpView] | None = None) -> None"),
4020 Walks the operation tree with a callback function.
4022 If op_class is provided, the callback is only invoked on operations
4023 of that type; all other operations are skipped silently.
4026 callback: A callable that takes an Operation and returns a WalkResult.
4027 walk_order: The order of traversal (PRE_ORDER or POST_ORDER).
4028 op_class: If provided, only operations of this type are passed to the callback.)")
4034 MlirIdentifier opName =
4038 self.getOperation().getContext()->get());
4040 "trait_cls"_a,
"Checks if the operation has a given trait.");
4042 nb::class_<PyOperation, PyOperationBase>(m,
"Operation")
4045 [](std::string_view name,
4046 std::optional<std::vector<PyType *>> results,
4047 std::optional<std::vector<PyValue *>> operands,
4048 std::optional<nb::typed<nb::dict, nb::str, PyAttribute>>
4050 std::optional<std::vector<PyBlock *>> successors,
int regions,
4051 const std::optional<PyLocation> &location,
4052 const nb::object &maybeIp,
4053 bool inferType) -> nb::typed<nb::object, PyOperation> {
4055 std::vector<MlirValue> mlirOperands;
4057 mlirOperands.reserve(operands->size());
4058 for (
PyValue *operand : *operands) {
4060 throw nb::value_error(
"operand value cannot be None");
4061 mlirOperands.push_back(operand->get());
4065 PyLocation pyLoc = maybeGetTracebackLocation(location);
4067 name, results, mlirOperands.data(), mlirOperands.size(),
4068 attributes, successors, regions, pyLoc, maybeIp, inferType);
4070 "name"_a,
"results"_a = nb::none(),
"operands"_a = nb::none(),
4071 "attributes"_a = nb::none(),
"successors"_a = nb::none(),
4072 "regions"_a = 0,
"loc"_a = nb::none(),
"ip"_a = nb::none(),
4073 "infer_type"_a =
false,
4075 Creates a new operation.
4078 name: Operation name (e.g. `dialect.operation`).
4079 results: Optional sequence of Type representing op result types.
4080 operands: Optional operands of the operation.
4081 attributes: Optional Dict of {str: Attribute}.
4082 successors: Optional List of Block for the operation's successors.
4083 regions: Number of regions to create (default = 0).
4084 location: Optional Location object (defaults to resolve from context manager).
4085 ip: Optional InsertionPoint (defaults to resolve from context manager or set to False to disable insertion, even with an insertion point set in the context manager).
4086 infer_type: Whether to infer result types (default = False).
4088 A new detached Operation object. Detached operations can be added to blocks, which causes them to become attached.)")
4091 [](
const std::string &sourceStr,
const std::string &sourceName,
4093 -> nb::typed<nb::object, PyOpView> {
4097 "source"_a, nb::kw_only(),
"source_name"_a =
"",
4098 "context"_a = nb::none(),
4099 "Parses an operation. Supports both text assembly format and binary "
4102 "Gets a capsule wrapping the MlirOperation.")
4105 "Creates an Operation from a capsule wrapping MlirOperation.")
4108 [](nb::object self) -> nb::typed<nb::object, PyOperation> {
4111 "Returns self (the operation).")
4114 [](
PyOperation &self) -> nb::typed<nb::object, PyOpView> {
4115 return self.createOpView();
4118 Returns an OpView of this operation.
4121 If the operation has a registered and loaded dialect then this OpView will
4122 be concrete wrapper class.)")
4124 "Returns the block containing this operation.")
4130 "Returns the list of Operation successors.")
4132 "replace_uses_of_with",
4137 "Replaces uses of the 'of' value with the 'with' value inside the "
4140 "Invalidate the operation.");
4143 nb::class_<PyOpView, PyOperationBase>(m,
"OpView")
4144 .def(nb::init<nb::typed<nb::object, PyOperation>>(),
"operation"_a)
4147 [](
PyOpView *self, std::string_view name,
4148 std::tuple<int, bool> opRegionSpec,
4149 nb::object operandSegmentSpecObj,
4150 nb::object resultSegmentSpecObj,
4151 std::optional<nb::sequence> resultTypeList,
4152 nb::sequence operandList,
4153 std::optional<nb::typed<nb::dict, nb::str, PyAttribute>>
4155 std::optional<std::vector<PyBlock *>> successors,
4156 std::optional<int> regions,
4157 const std::optional<PyLocation> &location,
4158 const nb::object &maybeIp) {
4159 PyLocation pyLoc = maybeGetTracebackLocation(location);
4161 name, opRegionSpec, operandSegmentSpecObj,
4162 resultSegmentSpecObj, resultTypeList, operandList,
4163 attributes, successors, regions, pyLoc, maybeIp));
4165 "name"_a,
"opRegionSpec"_a,
4166 "operandSegmentSpecObj"_a = nb::none(),
4167 "resultSegmentSpecObj"_a = nb::none(),
"results"_a = nb::none(),
4168 "operands"_a = nb::none(),
"attributes"_a = nb::none(),
4169 "successors"_a = nb::none(),
"regions"_a = nb::none(),
4170 "loc"_a = nb::none(),
"ip"_a = nb::none())
4173 [](
PyOpView &self) -> nb::typed<nb::object, PyOperation> {
4174 return self.getOperationObject();
4176 .def_prop_ro(
"opview",
4177 [](nb::object self) -> nb::typed<nb::object, PyOpView> {
4182 [](
PyOpView &self) {
return nb::str(self.getOperationObject()); })
4188 "Returns the list of Operation successors.")
4191 [](
PyOpView &self) { self.getOperation().setInvalid(); },
4192 "Invalidate the operation.");
4193 opViewClass.attr(
"_ODS_REGIONS") = nb::make_tuple(0,
true);
4194 opViewClass.attr(
"_ODS_OPERAND_SEGMENTS") = nb::none();
4195 opViewClass.attr(
"_ODS_RESULT_SEGMENTS") = nb::none();
4200 [](nb::handle cls, std::optional<nb::sequence> resultTypeList,
4201 nb::sequence operandList,
4202 std::optional<nb::typed<nb::dict, nb::str, PyAttribute>> attributes,
4203 std::optional<std::vector<PyBlock *>> successors,
4204 std::optional<int> regions, std::optional<PyLocation> location,
4205 const nb::object &maybeIp) {
4206 std::string name = nb::cast<std::string>(cls.attr(
"OPERATION_NAME"));
4207 std::tuple<int, bool> opRegionSpec =
4208 nb::cast<std::tuple<int, bool>>(cls.attr(
"_ODS_REGIONS"));
4209 nb::object operandSegmentSpec = cls.attr(
"_ODS_OPERAND_SEGMENTS");
4210 nb::object resultSegmentSpec = cls.attr(
"_ODS_RESULT_SEGMENTS");
4211 PyLocation pyLoc = maybeGetTracebackLocation(location);
4213 resultSegmentSpec, resultTypeList,
4214 operandList, attributes, successors,
4215 regions, pyLoc, maybeIp);
4217 "cls"_a,
"results"_a = nb::none(),
"operands"_a = nb::none(),
4218 "attributes"_a = nb::none(),
"successors"_a = nb::none(),
4219 "regions"_a = nb::none(),
"loc"_a = nb::none(),
"ip"_a = nb::none(),
4221 nb::sig(
"def build_generic(cls, results: Sequence[Type] | None = None, operands: Sequence[Value] | None = None, attributes: dict[str, Attribute] | None = None, successors: Sequence[Block] | None = None, regions: int | None = None, loc: Location | None = None, ip: InsertionPoint | None = None) -> typing.Self"),
4223 "Builds a specific, generated OpView based on class level attributes.");
4225 [](
const nb::object &cls,
const std::string &sourceStr,
4226 const std::string &sourceName,
4236 std::string clsOpName =
4237 nb::cast<std::string>(cls.attr(
"OPERATION_NAME"));
4240 std::string_view parsedOpName(identifier.
data, identifier.
length);
4241 if (clsOpName != parsedOpName)
4242 throw MLIRError(
join(
"Expected a '", clsOpName,
"' op, got: '",
4243 parsedOpName,
"'"));
4246 "cls"_a,
"source"_a, nb::kw_only(),
"source_name"_a =
"",
4247 "context"_a = nb::none(),
4249 nb::sig(
"def parse(cls, source: str, *, source_name: str = '', context: Context | None = None) -> typing.Self"),
4251 "Parses a specific, generated OpView based on class level attributes.");
4253 [](nb::object &self, nb::type_object &traitCls,
4257 std::string opName = nb::cast<std::string>(self.attr(
"OPERATION_NAME"));
4260 traitTypeID.
get(), context->
get());
4262 "cls"_a,
"trait_cls"_a,
"context"_a = nb::none(),
4263 "Checks if the operation has a given trait.");
4270 nb::class_<PyRegion>(m,
"Region")
4274 return PyBlockList(self.getParentOperation(), self.get());
4276 "Returns a forward-optimized sequence of blocks.")
4279 [](
PyRegion &self) -> nb::typed<nb::object, PyOpView> {
4280 return self.getParentOperation()->createOpView();
4282 "Returns the operation owning this region.")
4290 "Iterates over blocks in the region.")
4294 return self.get().ptr == other.
get().ptr;
4296 "Compares two regions for pointer equality.")
4298 "__eq__", [](
PyRegion &self, nb::object &other) {
return false; },
4299 "Compares region with non-region object (always returns False).");
4304 nb::class_<PyBlock>(m,
"Block")
4306 "Gets a capsule wrapping the MlirBlock.")
4309 [](
PyBlock &self) -> nb::typed<nb::object, PyOpView> {
4310 return self.getParentOperation()->createOpView();
4312 "Returns the owning operation of this block.")
4317 return PyRegion(self.getParentOperation(), region);
4319 "Returns the owning region of this block.")
4325 "Returns a list of block arguments.")
4334 Appends an argument of the specified type to the block.
4337 type: The type of the argument to add.
4338 loc: The source location for the argument.
4341 The newly added block argument.)")
4349 Erases the argument at the specified index.
4352 index: The index of the argument to erase.)")
4358 "Returns a forward-optimized sequence of operations.")
4361 [](
PyRegion &parent, nb::typed<nb::sequence, PyType> pyArgTypes,
4362 const std::optional<nb::typed<nb::sequence, PyLocation>>
4365 MlirBlock block =
createBlock(pyArgTypes, pyArgLocs);
4369 "parent"_a,
"arg_types"_a = nb::list(),
"arg_locs"_a = std::nullopt,
4370 "Creates and returns a new Block at the beginning of the given "
4371 "region (with given argument types and locations).")
4375 MlirBlock
b = self.get();
4382 Appends this block to a region.
4384 Transfers ownership if the block is currently owned by another region.
4387 region: The region to append the block to.)")
4390 [](
PyBlock &self,
const nb::args &pyArgTypes,
4391 const std::optional<nb::typed<nb::sequence, PyLocation>>
4395 createBlock(nb::cast<nb::sequence>(pyArgTypes), pyArgLocs);
4398 return PyBlock(self.getParentOperation(), block);
4400 "arg_types"_a, nb::kw_only(),
"arg_locs"_a = std::nullopt,
4401 "Creates and returns a new Block before this block "
4402 "(with given argument types and locations).")
4405 [](
PyBlock &self,
const nb::args &pyArgTypes,
4406 const std::optional<nb::typed<nb::sequence, PyLocation>>
4410 createBlock(nb::cast<nb::sequence>(pyArgTypes), pyArgLocs);
4413 return PyBlock(self.getParentOperation(), block);
4415 "arg_types"_a, nb::kw_only(),
"arg_locs"_a = std::nullopt,
4416 "Creates and returns a new Block after this block "
4417 "(with given argument types and locations).")
4422 MlirOperation firstOperation =
4427 "Iterates over operations in the block.")
4431 return self.get().ptr == other.
get().ptr;
4433 "Compares two blocks for pointer equality.")
4435 "__eq__", [](
PyBlock &self, nb::object &other) {
return false; },
4436 "Compares block with non-block object (always returns False).")
4438 "__hash__", [](
PyBlock &self) {
return hash(self.get().ptr); },
4439 "Returns the hash value of the block.")
4447 return printAccum.
join();
4449 "Returns the assembly form of the block.")
4459 self.getParentOperation().getObject());
4463 Appends an operation to this block.
4465 If the operation is currently in another block, it will be moved.
4468 operation: The operation to append to the block.)")
4474 "Returns the list of Block successors.")
4480 "Returns the list of Block predecessors.");
4486 nb::class_<PyInsertionPoint>(m,
"InsertionPoint")
4487 .def(nb::init<PyBlock &>(),
"block"_a,
4488 "Inserts after the last operation but still inside the block.")
4490 "Enters the insertion point as a context manager.",
4491 nb::sig(
"def __enter__(self, /) -> InsertionPoint"))
4493 "exc_value"_a.none(),
"traceback"_a.none(),
4494 "Exits the insertion point context manager.")
4495 .def_prop_ro_static(
4500 throw nb::value_error(
"No current InsertionPoint");
4503 nb::sig(
"def current(/) -> InsertionPoint"),
4504 "Gets the InsertionPoint bound to the current thread or raises "
4505 "ValueError if none has been set.")
4506 .def(nb::init<PyOperationBase &>(),
"beforeOperation"_a,
4507 "Inserts before a referenced operation.")
4510 Creates an insertion point at the beginning of a block.
4513 block: The block at whose beginning operations should be inserted.
4516 An InsertionPoint at the block's beginning.)")
4520 Creates an insertion point before a block's terminator.
4523 block: The block whose terminator to insert before.
4526 An InsertionPoint before the terminator.
4529 ValueError: If the block has no terminator.)")
4532 Creates an insertion point immediately after an operation.
4535 operation: The operation after which to insert.
4538 An InsertionPoint after the operation.)")
4541 Inserts an operation at this insertion point.
4544 operation: The operation to insert.)")
4547 "Returns the block that this `InsertionPoint` points to.")
4551 -> std::optional<nb::typed<nb::object, PyOperation>> {
4552 auto refOperation = self.getRefOperation();
4554 return refOperation->getObject();
4557 "The reference operation before which new operations are "
4558 "inserted, or None if the insertion point is at the end of "
4564 nb::class_<PyAttribute>(m,
"Attribute")
4567 .def(nb::init<PyAttribute &>(),
"cast_from_type"_a,
4568 "Casts the passed attribute to the generic `Attribute`.")
4570 "Gets a capsule wrapping the MlirAttribute.")
4573 "Creates an Attribute from a capsule wrapping `MlirAttribute`.")
4577 -> nb::typed<nb::object, PyAttribute> {
4581 if (mlirAttributeIsNull(attr))
4582 throw MLIRError(
"Unable to parse attribute", errors.
take());
4585 "asm"_a,
"context"_a = nb::none(),
4586 "Parses an attribute from an assembly form. Raises an `MLIRError` on "
4590 [](
PyAttribute &self) -> nb::typed<nb::object, PyMlirContext> {
4591 return self.getContext().getObject();
4593 "Context that owns the `Attribute`.")
4596 [](
PyAttribute &self) -> nb::typed<nb::object, PyType> {
4600 "Returns the type of the `Attribute`.")
4606 nb::keep_alive<0, 1>(),
4608 Binds a name to the attribute, creating a `NamedAttribute`.
4611 name: The name to bind to the `Attribute`.
4614 A `NamedAttribute` with the given name and this attribute.)")
4618 "Compares two attributes for equality.")
4620 "__eq__", [](
PyAttribute &self, nb::object &other) {
return false; },
4621 "Compares attribute with non-attribute object (always returns "
4625 "Returns the hash value of the attribute.")
4635 return printAccum.
join();
4637 "Returns the assembly form of the Attribute.")
4647 printAccum.
parts.append(
"Attribute(");
4650 printAccum.
parts.append(
")");
4651 return printAccum.
join();
4653 "Returns a string representation of the attribute.")
4659 "mlirTypeID was expected to be non-null.");
4662 "Returns the `TypeID` of the attribute.")
4665 [](
PyAttribute &self) -> nb::typed<nb::object, PyAttribute> {
4666 return self.maybeDownCast();
4668 "Downcasts the attribute to a more specific attribute if possible.");
4673 nb::class_<PyNamedAttribute>(m,
"NamedAttribute")
4678 printAccum.
parts.append(
"NamedAttribute(");
4679 printAccum.
parts.append(
4682 printAccum.
parts.append(
"=");
4686 printAccum.
parts.append(
")");
4687 return printAccum.
join();
4689 "Returns a string representation of the named attribute.")
4695 "The name of the `NamedAttribute` binding.")
4699 nb::keep_alive<0, 1>(), nb::sig(
"def attr(self) -> Attribute"),
4700 "The underlying generic attribute of the `NamedAttribute` binding.");
4705 nb::class_<PyType>(m,
"Type")
4708 .def(nb::init<PyType &>(),
"cast_from_type"_a,
4709 "Casts the passed type to the generic `Type`.")
4711 "Gets a capsule wrapping the `MlirType`.")
4713 "Creates a Type from a capsule wrapping `MlirType`.")
4716 [](std::string typeSpec,
4725 "asm"_a,
"context"_a = nb::none(),
4727 Parses the assembly form of a type.
4729 Returns a Type object or raises an `MLIRError` if the type cannot be parsed.
4731 See also: https://mlir.llvm.org/docs/LangRef/#type-system)")
4734 [](
PyType &self) -> nb::typed<nb::object, PyMlirContext> {
4735 return self.getContext().getObject();
4737 "Context that owns the `Type`.")
4739 "__eq__", [](
PyType &self,
PyType &other) {
return self == other; },
4740 "Compares two types for equality.")
4742 "__eq__", [](
PyType &self, nb::object &other) {
return false; },
4744 "Compares type with non-type object (always returns False).")
4746 "__hash__", [](
PyType &self) {
return hash(self.get().ptr); },
4747 "Returns the hash value of the `Type`.")
4756 return printAccum.
join();
4758 "Returns the assembly form of the `Type`.")
4767 printAccum.
parts.append(
"Type(");
4770 printAccum.
parts.append(
")");
4771 return printAccum.
join();
4773 "Returns a string representation of the `Type`.")
4776 [](
PyType &self) -> nb::typed<nb::object, PyType> {
4777 return self.maybeDownCast();
4779 "Downcasts the Type to a more specific `Type` if possible.")
4786 auto origRepr = nb::cast<std::string>(nb::repr(nb::cast(self)));
4787 throw nb::value_error(
join(origRepr,
" has no typeid.").c_str());
4789 "Returns the `TypeID` of the `Type`, or raises `ValueError` if "
4796 nb::class_<PyTypeID>(m,
"TypeID")
4798 "Gets a capsule wrapping the `MlirTypeID`.")
4800 "Creates a `TypeID` from a capsule wrapping `MlirTypeID`.")
4807 "Compares two `TypeID`s for equality.")
4810 [](
PyTypeID &self,
const nb::object &other) {
return false; },
4811 "Compares TypeID with non-TypeID object (always returns False).")
4820 "Returns the hash value of the `TypeID`.");
4825 m.attr(
"_T") = nb::type_var(
"_T",
"bound"_a = m.attr(
"Type"));
4827 nb::class_<PyValue>(m,
"Value", nb::is_generic(),
4828 nb::sig(
"class Value(typing.Generic[_T])"))
4829 .def(nb::init<PyValue &>(), nb::keep_alive<0, 1>(),
"value"_a,
4830 "Creates a Value reference from another `Value`.")
4832 "Gets a capsule wrapping the `MlirValue`.")
4834 "Creates a `Value` from a capsule wrapping `MlirValue`.")
4837 [](
PyValue &self) -> nb::typed<nb::object, PyMlirContext> {
4838 return self.getParentOperation()->getContext().getObject();
4840 "Context in which the value lives.")
4847 -> nb::typed<nb::object, std::variant<PyOpView, PyBlock>> {
4848 MlirValue v = self.get();
4852 "expected the owner of the value in Python to match "
4855 return self.getParentOperation()->createOpView();
4860 return nb::cast(
PyBlock(self.getParentOperation(), block));
4863 assert(
false &&
"Value must be a block argument or an op result");
4866 "Returns the owner of the value (`Operation` for results, `Block` "
4874 "Returns an iterator over uses of this value.")
4878 return self.get().ptr == other.
get().ptr;
4880 "Compares two values for pointer equality.")
4882 "__eq__", [](
PyValue &self, nb::object other) {
return false; },
4883 "Compares value with non-value object (always returns False).")
4885 "__hash__", [](
PyValue &self) {
return hash(self.get().ptr); },
4886 "Returns the hash value of the value.")
4891 printAccum.
parts.append(
"Value(");
4894 printAccum.
parts.append(
")");
4895 return printAccum.
join();
4898 Returns the string form of the value.
4900 If the value is a block argument, this is the assembly form of its type and the
4901 position in the argument list. If the value is an operation result, this is
4902 equivalent to printing the operation that produced it.
4906 [](
PyValue &self,
bool useLocalScope,
bool useNameLocAsPrefix) {
4911 if (useNameLocAsPrefix)
4913 MlirAsmState valueState =
4920 return printAccum.
join();
4922 "use_local_scope"_a =
false,
"use_name_loc_as_prefix"_a =
false,
4924 Returns the string form of value as an operand.
4927 use_local_scope: Whether to use local scope for naming.
4928 use_name_loc_as_prefix: Whether to use the location attribute (NameLoc) as prefix.
4931 The value's name as it appears in IR (e.g., `%0`, `%arg0`).)")
4936 MlirAsmState valueState = state.
get();
4940 return printAccum.
join();
4943 "Returns the string form of value as an operand (i.e., the ValueID).")
4947 return PyType(self.getParentOperation()->getContext(),
4951 "Returns the type of the value.", nb::sig(
"def type(self) -> _T"))
4957 "type"_a,
"Sets the type of the value.",
4958 nb::sig(
"def set_type(self, type: _T)"))
4960 "replace_all_uses_with",
4964 "Replace all uses of value with the new value, updating anything in "
4965 "the IR that uses `self` to use the other value instead.")
4967 "replace_all_uses_except",
4969 MlirOperation exceptedUser = exception.
get();
4974 "replace_all_uses_except",
4976 std::vector<PyOperation> &exceptions) {
4978 std::vector<MlirOperation> exceptionOps;
4980 exceptionOps.push_back(exception);
4982 self, with,
static_cast<intptr_t>(exceptionOps.size()),
4983 exceptionOps.data());
4988 [](
PyValue &self) {
return self.maybeDownCast(); },
4989 "Downcasts the `Value` to a more specific kind if possible.")
4997 "Returns the source location of the value.");
5003 nb::class_<PyAsmState>(m,
"AsmState")
5004 .def(nb::init<PyValue &, bool>(),
"value"_a,
"use_local_scope"_a =
false,
5006 Creates an `AsmState` for consistent SSA value naming.
5009 value: The value to create state for.
5010 use_local_scope: Whether to use local scope for naming.)")
5011 .def(nb::init<PyOperationBase &, bool>(), "op"_a,
5012 "use_local_scope"_a =
false,
5014 Creates an AsmState for consistent SSA value naming.
5017 op: The operation to create state for.
5018 use_local_scope: Whether to use local scope for naming.)");
5023 nb::class_<PySymbolTable>(m,
"SymbolTable")
5024 .def(nb::init<PyOperationBase &>(),
5026 Creates a symbol table for an operation.
5029 operation: The `Operation` that defines a symbol table (e.g., a `ModuleOp`).
5032 TypeError: If the operation is not a symbol table.)")
5036 const std::string &name) -> nb::typed<nb::object, PyOpView> {
5037 return self.dunderGetItem(name);
5040 Looks up a symbol by name in the symbol table.
5043 name: The name of the symbol to look up.
5046 The operation defining the symbol.
5049 KeyError: If the symbol is not found.)")
5052 Inserts a symbol operation into the symbol table.
5055 operation: An operation with a symbol name to insert.
5058 The symbol name attribute of the inserted operation.
5061 ValueError: If the operation does not have a symbol name.)")
5064 Erases a symbol operation from the symbol table.
5067 operation: The symbol operation to erase.
5070 The operation is also erased from the IR and invalidated.)")
5072 "Deletes a symbol by name from the symbol table.")
5079 "Checks if a symbol with the given name exists in the table.")
5082 "name"_a,
"Sets the symbol name for a symbol operation.")
5084 "Gets the symbol name from a symbol operation.")
5086 "Gets the visibility attribute of a symbol operation.")
5089 "Sets the visibility attribute of a symbol operation.")
5090 .def_static(
"replace_all_symbol_uses",
5092 "new_symbol"_a,
"from_op"_a,
5093 "Replaces all uses of a symbol with a new symbol name within "
5094 "the given operation.")
5096 "from_op"_a,
"all_sym_uses_visible"_a,
"callback"_a,
5097 "Walks symbol tables starting from an operation with a "
5098 "callback function.");
void mlirSetGlobalDebugTypes(const char **types, intptr_t n)
MLIR_CAPI_EXPORTED void mlirSetGlobalDebugType(const char *type)
Sets the current debug type, similarly to -debug-only=type in the command-line tools.
MLIR_CAPI_EXPORTED bool mlirIsGlobalDebugEnabled()
Retuns true if the global debugging flag is set, false otherwise.
MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable)
Sets the global debugging flag.
static const char kDumpDocstring[]
static const char kModuleParseDocstring[]
static size_t hash(const T &value)
Local helper to compute std::hash for a value.
static nb::object createCustomDialectWrapper(const std::string &dialectNamespace, nb::object dialectDescriptor)
std::string join(const Ts &...args)
Helper function to concatenate arguments into a std::string.
static const char kValueReplaceAllUsesExceptDocstring[]
MlirContext mlirModuleGetContext(MlirModule module)
size_t mlirModuleHashValue(MlirModule mod)
intptr_t mlirBlockGetNumPredecessors(MlirBlock block)
MlirIdentifier mlirOperationGetName(MlirOperation op)
bool mlirValueIsABlockArgument(MlirValue value)
intptr_t mlirOperationGetNumRegions(MlirOperation op)
MlirBlock mlirOperationGetBlock(MlirOperation op)
void mlirBlockArgumentSetType(MlirValue value, MlirType type)
void mlirOperationStateAddAttributes(MlirOperationState *state, intptr_t n, MlirNamedAttribute const *attributes)
MlirValue mlirOperationGetResult(MlirOperation op, intptr_t pos)
MlirModule mlirModuleCreateParseFromFile(MlirContext context, MlirStringRef fileName)
bool mlirOperationNameHasTrait(MlirStringRef opName, MlirTypeID traitTypeID, MlirContext context)
MlirAsmState mlirAsmStateCreateForValue(MlirValue value, MlirOpPrintingFlags flags)
intptr_t mlirOperationGetNumResults(MlirOperation op)
void mlirOperationDestroy(MlirOperation op)
MlirContext mlirAttributeGetContext(MlirAttribute attribute)
MlirType mlirValueGetType(MlirValue value)
void mlirBlockPrint(MlirBlock block, MlirStringCallback callback, void *userData)
MlirOpPrintingFlags mlirOpPrintingFlagsCreate()
bool mlirModuleEqual(MlirModule lhs, MlirModule rhs)
void mlirOpPrintingFlagsElideLargeElementsAttrs(MlirOpPrintingFlags flags, intptr_t largeElementLimit)
void mlirOperationSetSuccessor(MlirOperation op, intptr_t pos, MlirBlock block)
MlirOperation mlirOperationGetNextInBlock(MlirOperation op)
void mlirOpPrintingFlagsEnableDebugInfo(MlirOpPrintingFlags flags, bool enable, bool prettyForm)
MlirOperation mlirModuleGetOperation(MlirModule module)
void mlirOpPrintingFlagsElideLargeResourceString(MlirOpPrintingFlags flags, intptr_t largeResourceLimit)
void mlirOpPrintingFlagsUseLocalScope(MlirOpPrintingFlags flags)
intptr_t mlirBlockArgumentGetArgNumber(MlirValue value)
MlirBlock mlirOperationGetSuccessor(MlirOperation op, intptr_t pos)
bool mlirAttributeEqual(MlirAttribute a1, MlirAttribute a2)
MlirAsmState mlirAsmStateCreateForOperation(MlirOperation op, MlirOpPrintingFlags flags)
bool mlirOperationEqual(MlirOperation op, MlirOperation other)
void mlirOpPrintingFlagsAssumeVerified(MlirOpPrintingFlags flags)
void mlirBytecodeWriterConfigDestroy(MlirBytecodeWriterConfig config)
MlirBlock mlirBlockGetSuccessor(MlirBlock block, intptr_t pos)
void mlirModuleDestroy(MlirModule module)
MlirModule mlirModuleCreateEmpty(MlirLocation location)
void mlirOpPrintingFlagsPrintGenericOpForm(MlirOpPrintingFlags flags)
MlirOperation mlirOperationGetParentOperation(MlirOperation op)
void mlirValueSetType(MlirValue value, MlirType type)
intptr_t mlirOperationGetNumSuccessors(MlirOperation op)
MlirDialect mlirAttributeGetDialect(MlirAttribute attr)
void mlirLocationPrint(MlirLocation location, MlirStringCallback callback, void *userData)
void mlirOperationSetAttributeByName(MlirOperation op, MlirStringRef name, MlirAttribute attr)
void mlirOperationSetOperand(MlirOperation op, intptr_t pos, MlirValue newValue)
MlirOperation mlirOpResultGetOwner(MlirValue value)
MlirModule mlirModuleCreateParse(MlirContext context, MlirStringRef module)
size_t mlirOperationHashValue(MlirOperation op)
void mlirOperationStateAddResults(MlirOperationState *state, intptr_t n, MlirType const *results)
MlirOperation mlirOperationClone(MlirOperation op)
MlirBlock mlirBlockArgumentGetOwner(MlirValue value)
void mlirBlockArgumentSetLocation(MlirValue value, MlirLocation loc)
MlirValue mlirOperationGetOperand(MlirOperation op, intptr_t pos)
MlirOpOperand mlirOperationGetOpOperand(MlirOperation op, intptr_t pos)
MlirLocation mlirOperationGetLocation(MlirOperation op)
MlirAttribute mlirOperationGetAttributeByName(MlirOperation op, MlirStringRef name)
MlirTypeID mlirAttributeGetTypeID(MlirAttribute attr)
void mlirOperationStateAddOwnedRegions(MlirOperationState *state, intptr_t n, MlirRegion const *regions)
void mlirOperationSetLocation(MlirOperation op, MlirLocation loc)
MlirType mlirAttributeGetType(MlirAttribute attribute)
bool mlirOperationRemoveAttributeByName(MlirOperation op, MlirStringRef name)
bool mlirValueIsAOpResult(MlirValue value)
MlirBlock mlirBlockGetPredecessor(MlirBlock block, intptr_t pos)
MlirRegion mlirOperationGetRegion(MlirOperation op, intptr_t pos)
MlirOperation mlirOperationCreate(MlirOperationState *state)
void mlirBytecodeWriterConfigDesiredEmitVersion(MlirBytecodeWriterConfig flags, int64_t version)
MlirAttribute mlirAttributeParseGet(MlirContext context, MlirStringRef attr)
void mlirOperationRemoveFromParent(MlirOperation op)
intptr_t mlirBlockGetNumSuccessors(MlirBlock block)
MlirNamedAttribute mlirOperationGetAttribute(MlirOperation op, intptr_t pos)
void mlirOpPrintingFlagsDestroy(MlirOpPrintingFlags flags)
void mlirValueDump(MlirValue value)
void mlirTypePrint(MlirType type, MlirStringCallback callback, void *userData)
MlirBlock mlirModuleGetBody(MlirModule module)
MlirOperation mlirOperationCreateParse(MlirContext context, MlirStringRef sourceStr, MlirStringRef sourceName)
void mlirAsmStateDestroy(MlirAsmState state)
Destroys printing flags created with mlirAsmStateCreate.
MlirContext mlirOperationGetContext(MlirOperation op)
intptr_t mlirOpResultGetResultNumber(MlirValue value)
void mlirOperationStateAddSuccessors(MlirOperationState *state, intptr_t n, MlirBlock const *successors)
MlirBytecodeWriterConfig mlirBytecodeWriterConfigCreate()
void mlirOpPrintingFlagsPrintNameLocAsPrefix(MlirOpPrintingFlags flags)
void mlirOpPrintingFlagsSkipRegions(MlirOpPrintingFlags flags)
void mlirOperationStateAddOperands(MlirOperationState *state, intptr_t n, MlirValue const *operands)
MlirOperationState mlirOperationStateGet(MlirStringRef name, MlirLocation loc)
intptr_t mlirOperationGetNumOperands(MlirOperation op)
void mlirTypeDump(MlirType type)
intptr_t mlirOperationGetNumAttributes(MlirOperation op)
static PyObject * mlirPythonTypeIDToCapsule(MlirTypeID typeID)
Creates a capsule object encapsulating the raw C-API MlirTypeID.
static PyObject * mlirPythonContextToCapsule(MlirContext context)
Creates a capsule object encapsulating the raw C-API MlirContext.
#define MLIR_PYTHON_MAYBE_DOWNCAST_ATTR
Attribute on MLIR Python objects that expose a function for downcasting the corresponding Python obje...
static MlirOperation mlirPythonCapsuleToOperation(PyObject *capsule)
Extracts an MlirOperations from a capsule as produced from mlirPythonOperationToCapsule.
#define MLIR_PYTHON_CAPI_PTR_ATTR
Attribute on MLIR Python objects that expose their C-API pointer.
static MlirAttribute mlirPythonCapsuleToAttribute(PyObject *capsule)
Extracts an MlirAttribute from a capsule as produced from mlirPythonAttributeToCapsule.
static PyObject * mlirPythonTypeToCapsule(MlirType type)
Creates a capsule object encapsulating the raw C-API MlirType.
static PyObject * mlirPythonOperationToCapsule(MlirOperation operation)
Creates a capsule object encapsulating the raw C-API MlirOperation.
static PyObject * mlirPythonAttributeToCapsule(MlirAttribute attribute)
Creates a capsule object encapsulating the raw C-API MlirAttribute.
#define MLIR_PYTHON_CAPI_FACTORY_ATTR
Attribute on MLIR Python objects that exposes a factory function for constructing the corresponding P...
static MlirModule mlirPythonCapsuleToModule(PyObject *capsule)
Extracts an MlirModule from a capsule as produced from mlirPythonModuleToCapsule.
static MlirContext mlirPythonCapsuleToContext(PyObject *capsule)
Extracts a MlirContext from a capsule as produced from mlirPythonContextToCapsule.
static MlirTypeID mlirPythonCapsuleToTypeID(PyObject *capsule)
Extracts an MlirTypeID from a capsule as produced from mlirPythonTypeIDToCapsule.
#define MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the value caster registration bindin...
static PyObject * mlirPythonBlockToCapsule(MlirBlock block)
Creates a capsule object encapsulating the raw C-API MlirBlock.
static PyObject * mlirPythonLocationToCapsule(MlirLocation loc)
Creates a capsule object encapsulating the raw C-API MlirLocation.
static MlirDialectRegistry mlirPythonCapsuleToDialectRegistry(PyObject *capsule)
Extracts an MlirDialectRegistry from a capsule as produced from mlirPythonDialectRegistryToCapsule.
static MlirType mlirPythonCapsuleToType(PyObject *capsule)
Extracts an MlirType from a capsule as produced from mlirPythonTypeToCapsule.
static MlirValue mlirPythonCapsuleToValue(PyObject *capsule)
Extracts an MlirValue from a capsule as produced from mlirPythonValueToCapsule.
static PyObject * mlirPythonValueToCapsule(MlirValue value)
Creates a capsule object encapsulating the raw C-API MlirValue.
static PyObject * mlirPythonModuleToCapsule(MlirModule module)
Creates a capsule object encapsulating the raw C-API MlirModule.
static MlirLocation mlirPythonCapsuleToLocation(PyObject *capsule)
Extracts an MlirLocation from a capsule as produced from mlirPythonLocationToCapsule.
#define MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR
Attribute on main C extension module (_mlir) that corresponds to the type caster registration binding...
static PyObject * mlirPythonDialectRegistryToCapsule(MlirDialectRegistry registry)
Creates a capsule object encapsulating the raw C-API MlirDialectRegistry.
static std::string diag(const llvm::Value &value)
Accumulates into a file, either writing text (default) or binary.
A CRTP base class for pseudo-containers willing to support Python-type slicing access on top of index...
static void bind(nanobind::module_ &m)
Sliceable(intptr_t startIndex, intptr_t length, intptr_t step)
intptr_t wrapIndex(intptr_t index)
ReferrentTy * get() const
PyMlirContextRef & getContext()
Accesses the context reference.
BaseContextObject(PyMlirContextRef ref)
static PyLocation & resolve()
Used in function arguments when None should resolve to the current context manager set instance.
static PyMlirContext & resolve()
Wrapper around an MlirAsmState.
PyAsmState(MlirValue value, bool useLocalScope)
Wrapper around the generic MlirAttribute.
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirAttribute.
MlirAttribute get() const
bool operator==(const PyAttribute &other) const
static PyAttribute createFromCapsule(const nanobind::object &capsule)
Creates a PyAttribute from the MlirAttribute wrapped by a capsule.
nanobind::typed< nanobind::object, PyAttribute > maybeDownCast()
A list of block arguments.
PyBlockArgumentList(PyOperationRef operation, MlirBlock block, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
static void bindDerived(ClassTy &c)
Python wrapper for MlirBlockArgument.
static void bindDerived(ClassTy &c)
PyBlockIterator & dunderIter()
nanobind::typed< nanobind::object, PyBlock > dunderNext()
static void bind(nanobind::module_ &m)
Blocks are exposed by the C-API as a forward-only linked list.
static void bind(nanobind::module_ &m)
PyBlockIterator dunderIter()
PyBlock appendBlock(const nanobind::args &pyArgTypes, const std::optional< nanobind::sequence > &pyArgLocs)
PyBlock dunderGetItem(intptr_t index)
A list of block predecessors.
PyBlockPredecessors(PyBlock block, PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
A list of block successors.
PyBlockSuccessors(PyBlock block, PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
Wrapper around an MlirBlock.
PyOperationRef & getParentOperation()
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirBlock.
static void bind(nanobind::module_ &m)
Represents a diagnostic handler attached to the context.
void detach()
Detaches the handler. Does nothing if not attached.
nanobind::object contextEnter()
PyDiagnosticHandler(MlirContext context, nanobind::object callback)
void contextExit(const nanobind::object &excType, const nanobind::object &excVal, const nanobind::object &excTb)
Python class mirroring the C MlirDiagnostic struct.
PyDiagnostic(MlirDiagnostic diagnostic)
nanobind::str getMessage()
PyDiagnosticSeverity getSeverity()
nanobind::typed< nanobind::tuple, PyDiagnostic > getNotes()
Wrapper around an MlirDialect.
Wrapper around an MlirDialectRegistry.
static PyDialectRegistry createFromCapsule(nanobind::object capsule)
nanobind::object getCapsule()
User-level dialect object.
User-level object for accessing dialects with dotted syntax such as: ctx.dialect.std.
MlirDialect getDialectForKey(const std::string &key, bool attrError)
static void bind(nanobind::module_ &m)
static const char * typeIDAttr
static bool attach(const nanobind::object &opName, const nanobind::object &target, PyMlirContext &context)
static bool attach(const nanobind::object &opName, PyMlirContext &context)
static void bind(nanobind::module_ &m)
static void bind(nanobind::module_ &m)
static bool attach(const nanobind::object &opName, PyMlirContext &context)
size_t locTracebackFramesLimit()
void setLocTracebackFramesLimit(size_t value)
void registerTracebackFileExclusion(const std::string &file)
void registerTracebackFileInclusion(const std::string &file)
OnExplicitAction tracebackActionOnExplicitLoc()
CurrentLocAction tracebackActionOnCurrentLoc()
static constexpr size_t kMaxFrames
void setLocTracebacksEnabled(bool value)
CurrentLocAction
Policy for composing Location.current with the computed location.
void setTracebackActionOnCurrentLoc(CurrentLocAction action)
OnExplicitAction
Policy for handling explicit loc= when loc_tracebacks() is active.
bool locTracebacksEnabled()
void setTracebackActionOnExplicitLoc(OnExplicitAction action)
Globals that are always accessible once the extension has been initialized.
void registerOpAdaptorImpl(const std::string &operationName, nanobind::object pyClass, bool replace=false)
Adds an operation adaptor class.
std::optional< nanobind::callable > lookupValueCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom value caster for MlirTypeID mlirTypeID.
bool loadDialectModule(std::string_view dialectNamespace)
Loads a python module corresponding to the given dialect namespace.
void addDialectSearchPrefix(std::string value)
void registerDialectImpl(const std::string &dialectNamespace, nanobind::object pyClass, bool replace=false)
Adds a concrete implementation dialect class.
static PyGlobals & get()
Most code should get the globals via this static accessor.
std::optional< nanobind::object > lookupOperationClass(std::string_view operationName)
Looks up a registered operation class (deriving from OpView) by operation name.
void registerTypeCaster(MlirTypeID mlirTypeID, nanobind::callable typeCaster, bool replace=false)
Adds a user-friendly type caster.
void registerOperationImpl(const std::string &operationName, nanobind::object pyClass, bool replace=false)
Adds a concrete implementation operation class.
void setDialectSearchPrefixes(std::vector< std::string > newValues)
std::optional< nanobind::callable > lookupTypeCaster(MlirTypeID mlirTypeID, MlirDialect dialect)
Returns the custom type caster for MlirTypeID mlirTypeID.
void registerValueCaster(MlirTypeID mlirTypeID, nanobind::callable valueCaster, bool replace=false)
Adds a user-friendly value caster.
std::optional< nanobind::callable > lookupAttributeBuilder(const std::string &attributeKind)
Returns the custom Attribute builder for Attribute kind.
TracebackLoc & getTracebackLoc()
std::optional< nanobind::object > lookupDialectClass(const std::string &dialectNamespace)
Looks up a registered dialect class by namespace.
std::vector< std::string > getDialectSearchPrefixes()
Get and set the list of parent modules to search for dialect implementation classes.
void registerAttributeBuilder(const std::string &attributeKind, nanobind::callable pyFunc, bool replace=false, bool allow_existing=false)
Adds a user-friendly Attribute builder.
An insertion point maintains a pointer to a Block and a reference operation.
void insert(PyOperationBase &operationBase)
Inserts an operation.
void contextExit(const nanobind::object &excType, const nanobind::object &excVal, const nanobind::object &excTb)
static PyInsertionPoint atBlockTerminator(PyBlock &block)
Shortcut to create an insertion point before the block terminator.
static PyInsertionPoint after(PyOperationBase &op)
Shortcut to create an insertion point to the node after the specified operation.
static PyInsertionPoint atBlockBegin(PyBlock &block)
Shortcut to create an insertion point at the beginning of the block.
PyInsertionPoint(const PyBlock &block)
Creates an insertion point positioned after the last operation in the block, but still inside the blo...
static nanobind::object contextEnter(nanobind::object insertionPoint)
Enter and exit the context manager.
Wrapper around an MlirLocation.
static nanobind::object contextEnter(nanobind::object location)
Enter and exit the context manager.
static PyLocation createFromCapsule(nanobind::object capsule)
Creates a PyLocation from the MlirLocation wrapped by a capsule.
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirLocation.
void contextExit(const nanobind::object &excType, const nanobind::object &excVal, const nanobind::object &excTb)
PyLocation(PyMlirContextRef contextRef, MlirLocation loc)
static PyMlirContextRef forContext(MlirContext context)
Returns a context reference for the singleton PyMlirContext wrapper for the given context.
static size_t getLiveCount()
Gets the count of live context objects. Used for testing.
static nanobind::object createFromCapsule(nanobind::object capsule)
Creates a PyMlirContext from the MlirContext wrapped by a capsule.
nanobind::object attachDiagnosticHandler(nanobind::object callback)
Attaches a Python callback as a diagnostic handler, returning a registration object (internally a PyD...
void contextExit(const nanobind::object &excType, const nanobind::object &excVal, const nanobind::object &excTb)
MlirContext get()
Accesses the underlying MlirContext.
PyMlirContextRef getRef()
Gets a strong reference to this context, which will ensure it is kept alive for the life of the refer...
bool getEmitErrorDiagnostics()
void setEmitErrorDiagnostics(bool value)
Controls whether error diagnostics should be propagated to diagnostic handlers, instead of being capt...
static nanobind::object contextEnter(nanobind::object context)
Enter and exit the context manager.
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirContext.
size_t getLiveModuleCount()
Gets the count of live modules associated with this context.
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirModule.
PyModule(PyModule &)=delete
MlirModule get()
Gets the backing MlirModule.
static PyModuleRef forModule(MlirModule module)
Returns a PyModule reference for the given MlirModule.
static nanobind::object createFromCapsule(nanobind::object capsule)
Creates a PyModule from the MlirModule wrapped by a capsule.
Represents a Python MlirNamedAttr, carrying an optional owned name.
PyNamedAttribute(MlirAttribute attr, std::string ownedName)
Constructs a PyNamedAttr that retains an owned name.
MlirNamedAttribute namedAttr
Template for a reference to a concrete type which captures a python reference to its underlying pytho...
nanobind::object releaseObject()
Releases the object held by this instance, returning it.
nanobind::object getObject()
Base class of operation adaptors.
static void bind(nanobind::module_ &m)
A list of operation attributes.
PyOpAttributeMap(PyOperationRef operation)
void dunderDelItem(const std::string &name)
static void bind(nanobind::module_ &m)
void dunderSetItem(const std::string &name, const PyAttribute &attr)
bool dunderContains(const std::string &name)
nanobind::typed< nanobind::object, PyAttribute > dunderGetItemNamed(const std::string &name)
nanobind::typed< nanobind::object, std::optional< PyAttribute > > get(const std::string &key, nanobind::object defaultValue)
static void forEachAttr(MlirOperation op, std::function< void(MlirStringRef, MlirAttribute)> fn)
PyNamedAttribute dunderGetItemIndexed(intptr_t index)
nanobind::typed< nanobind::object, PyOpOperand > dunderNext()
PyOpOperandIterator & dunderIter()
static void bind(nanobind::module_ &m)
A list of operation operands.
static void bindDerived(ClassTy &c)
PyOpOperandList(PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
void dunderSetItem(intptr_t index, PyValue value)
size_t getOperandNumber() const
static void bind(nanobind::module_ &m)
nanobind::typed< nanobind::object, PyOpView > getOwner() const
PyOpOperands(PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
static constexpr const char * pyClassName
Sliceable< PyOpOperandList, PyOpOperand > SliceableT
A list of operation results.
static void bindDerived(ClassTy &c)
PyOpResultList(PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
PyOperationRef & getOperation()
Python wrapper for MlirOpResult.
static void bindDerived(ClassTy &c)
A list of operation successors.
static void bindDerived(ClassTy &c)
PyOpSuccessors(PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
void dunderSetItem(intptr_t index, PyBlock block)
A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for providing more instance-sp...
PyOpView(const nanobind::object &operationObject)
static nanobind::typed< nanobind::object, PyOperation > buildGeneric(std::string_view name, std::tuple< int, bool > opRegionSpec, nanobind::object operandSegmentSpecObj, nanobind::object resultSegmentSpecObj, std::optional< nanobind::sequence > resultTypeList, nanobind::sequence operandList, std::optional< nanobind::dict > attributes, std::optional< std::vector< PyBlock * > > successors, std::optional< int > regions, PyLocation &location, const nanobind::object &maybeIp)
static nanobind::object constructDerived(const nanobind::object &cls, const nanobind::object &operation)
Construct an instance of a class deriving from OpView, bypassing its __init__ method.
Base class for PyOperation and PyOpView which exposes the primary, user visible methods for manipulat...
bool isBeforeInBlock(PyOperationBase &other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
nanobind::object getAsm(bool binary, std::optional< int64_t > largeElementsLimit, std::optional< int64_t > largeResourceLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, bool useNameLocAsPrefix, bool assumeVerified, bool skipRegions)
void print(std::optional< int64_t > largeElementsLimit, std::optional< int64_t > largeResourceLimit, bool enableDebugInfo, bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope, bool useNameLocAsPrefix, bool assumeVerified, nanobind::object fileObject, bool binary, bool skipRegions)
Implements the bound 'print' method and helps with others.
void writeBytecode(const nanobind::object &fileObject, std::optional< int64_t > bytecodeVersion)
bool verify()
Verify the operation.
virtual PyOperation & getOperation()=0
Each must provide access to the raw Operation.
void moveAfter(PyOperationBase &other)
Moves the operation before or after the other operation.
void moveBefore(PyOperationBase &other)
void walk(std::function< PyWalkResult(MlirOperation)> callback, PyWalkOrder walkOrder)
static void bind(nanobind::module_ &m)
PyOperationIterator & dunderIter()
nanobind::typed< nanobind::object, PyOpView > dunderNext()
Operations are exposed by the C-API as a forward-only linked list.
static void bind(nanobind::module_ &m)
nanobind::typed< nanobind::object, PyOpView > dunderGetItem(intptr_t index)
PyOperationIterator dunderIter()
MlirOperation get() const
static nanobind::object create(std::string_view name, std::optional< std::vector< PyType * > > results, const MlirValue *operands, size_t numOperands, std::optional< nanobind::dict > attributes, std::optional< std::vector< PyBlock * > > successors, int regions, PyLocation &location, const nanobind::object &ip, bool inferType)
Creates an operation. See corresponding python docstring.
void setInvalid()
Invalidate the operation.
PyOperation & getOperation() override
Each must provide access to the raw Operation.
static PyOperationRef parse(PyMlirContextRef contextRef, const std::string &sourceStr, const std::string &sourceName)
Parses a source string (either text assembly or bytecode), creating a detached operation.
nanobind::object clone(const nanobind::object &ip)
Clones this operation.
static nanobind::object createFromCapsule(const nanobind::object &capsule)
Creates a PyOperation from the MlirOperation wrapped by a capsule.
std::optional< PyOperationRef > getParentOperation()
Gets the parent operation or raises an exception if the operation has no parent.
nanobind::object createOpView()
Creates an OpView suitable for this operation.
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirOperation.
static PyOperationRef forOperation(PyMlirContextRef contextRef, MlirOperation operation, nanobind::object parentKeepAlive=nanobind::object())
Returns a PyOperation for the given MlirOperation, optionally associating it with a parentKeepAlive.
void detachFromParent()
Detaches the operation from its parent block and updates its state accordingly.
void erase()
Erases the underlying MlirOperation, removes its pointer from the parent context's live operations ma...
PyBlock getBlock()
Gets the owning block or raises an exception if the operation has no owning block.
static PyOperationRef createDetached(PyMlirContextRef contextRef, MlirOperation operation, nanobind::object parentKeepAlive=nanobind::object())
Creates a detached operation.
PyOperation(PyMlirContextRef contextRef, MlirOperation operation)
void setAttached(const nanobind::object &parent=nanobind::object())
Regions of an op are fixed length and indexed numerically so are represented with a sequence-like con...
PyRegionList(PyOperationRef operation, intptr_t startIndex=0, intptr_t length=-1, intptr_t step=1)
Wrapper around an MlirRegion.
PyOperationRef & getParentOperation()
Bindings for MLIR symbol tables.
PyStringAttribute insert(PyOperationBase &symbol)
Inserts the given operation into the symbol table.
PySymbolTable(PyOperationBase &operation)
Constructs a symbol table for the given operation.
static PyStringAttribute getVisibility(PyOperationBase &symbol)
Gets and sets the visibility of a symbol op.
void erase(PyOperationBase &symbol)
Removes the given operation from the symbol table and erases it.
static void walkSymbolTables(PyOperationBase &from, bool allSymUsesVisible, nanobind::object callback)
Walks all symbol tables under and including 'from'.
nanobind::object dunderGetItem(const std::string &name)
Returns the symbol (opview) with the given name, throws if there is no such symbol in the table.
static void replaceAllSymbolUses(const std::string &oldSymbol, const std::string &newSymbol, PyOperationBase &from)
Replaces all symbol uses within an operation.
static PyStringAttribute getSymbolName(PyOperationBase &symbol)
Gets and sets the name of a symbol op.
void dunderDel(const std::string &name)
Removes the operation with the given name from the symbol table and erases it, throws if there is no ...
static void setSymbolName(PyOperationBase &symbol, const std::string &name)
static void setVisibility(PyOperationBase &symbol, const std::string &visibility)
Tracks an entry in the thread context stack.
static PyInsertionPoint * getDefaultInsertionPoint()
Gets the top of stack insertion point and return nullptr if not defined.
static nanobind::object pushInsertionPoint(nanobind::object insertionPoint)
PyLocation * getLocation()
static void popInsertionPoint(PyInsertionPoint &insertionPoint)
static PyLocation * getDefaultLocation()
Gets the top of stack location and returns nullptr if not defined.
static PyThreadContextEntry * getTopOfStack()
Stack management.
PyInsertionPoint * getInsertionPoint()
static nanobind::object pushLocation(nanobind::object location)
static void popLocation(PyLocation &location)
static nanobind::object pushContext(nanobind::object context)
PyMlirContext * getContext()
static void popContext(PyMlirContext &context)
static PyMlirContext * getDefaultContext()
Gets the top of stack context and return nullptr if not defined.
static std::vector< PyThreadContextEntry > & getStack()
Gets the thread local stack.
Wrapper around MlirLlvmThreadPool Python object owns the C++ thread pool.
int getMaxConcurrency() const
std::string _mlir_thread_pool_ptr() const
A TypeID provides an efficient and unique identifier for a specific C++ type.
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirTypeID.
PyTypeID(MlirTypeID typeID)
bool operator==(const PyTypeID &other) const
static PyTypeID createFromCapsule(nanobind::object capsule)
Creates a PyTypeID from the MlirTypeID wrapped by a capsule.
Wrapper around the generic MlirType.
PyType(PyMlirContextRef contextRef, MlirType type)
bool operator==(const PyType &other) const
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirType.
static PyType createFromCapsule(nanobind::object capsule)
Creates a PyType from the MlirType wrapped by a capsule.
nanobind::typed< nanobind::object, PyType > maybeDownCast()
nanobind::object getCapsule()
Gets a capsule wrapping the void* within the MlirValue.
PyOperationRef & getParentOperation()
PyValue(PyOperationRef parentOperation, MlirValue value)
nanobind::typed< nanobind::object, std::variant< PyBlockArgument, PyOpResult, PyValue > > maybeDownCast()
static PyValue createFromCapsule(nanobind::object capsule)
Creates a PyValue from the MlirValue wrapped by a capsule.
MLIR_CAPI_EXPORTED intptr_t mlirDiagnosticGetNumNotes(MlirDiagnostic diagnostic)
Returns the number of notes attached to the diagnostic.
MLIR_CAPI_EXPORTED MlirDiagnosticSeverity mlirDiagnosticGetSeverity(MlirDiagnostic diagnostic)
Returns the severity of the diagnostic.
MLIR_CAPI_EXPORTED void mlirDiagnosticPrint(MlirDiagnostic diagnostic, MlirStringCallback callback, void *userData)
Prints a diagnostic using the provided callback.
MLIR_CAPI_EXPORTED MlirDiagnostic mlirDiagnosticGetNote(MlirDiagnostic diagnostic, intptr_t pos)
Returns pos-th note attached to the diagnostic.
MLIR_CAPI_EXPORTED void mlirEmitError(MlirLocation location, const char *message)
Emits an error at the given location through the diagnostics engine.
MLIR_CAPI_EXPORTED MlirDiagnosticHandlerID mlirContextAttachDiagnosticHandler(MlirContext context, MlirDiagnosticHandler handler, void *userData, void(*deleteUserData)(void *))
Attaches the diagnostic handler to the context.
struct MlirDiagnostic MlirDiagnostic
MLIR_CAPI_EXPORTED void mlirContextDetachDiagnosticHandler(MlirContext context, MlirDiagnosticHandlerID id)
Detaches an attached diagnostic handler from the context given its identifier.
uint64_t MlirDiagnosticHandlerID
Opaque identifier of a diagnostic handler, useful to detach a handler.
MLIR_CAPI_EXPORTED MlirLocation mlirDiagnosticGetLocation(MlirDiagnostic diagnostic)
Returns the location at which the diagnostic is reported.
MLIR_CAPI_EXPORTED MlirAttribute mlirDenseI32ArrayGet(MlirContext ctx, intptr_t size, int32_t const *values)
MLIR_CAPI_EXPORTED MlirAttribute mlirStringAttrGet(MlirContext ctx, MlirStringRef str)
Creates a string attribute in the given context containing the given string.
MLIR_CAPI_EXPORTED MlirDynamicOpTrait mlirDynamicOpTraitIsTerminatorCreate(void)
Get the dynamic op trait that indicates the operation is a terminator.
MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicOpTraitIsTerminatorGetTypeID(void)
Get the type ID of the dynamic op trait that indicates the operation is a terminator.
MLIR_CAPI_EXPORTED MlirDynamicOpTrait mlirDynamicOpTraitCreate(MlirTypeID typeID, MlirDynamicOpTraitCallbacks callbacks, void *userData)
Create a custom dynamic op trait with the given type ID and callbacks.
MLIR_CAPI_EXPORTED bool mlirDynamicOpTraitAttach(MlirDynamicOpTrait dynamicOpTrait, MlirStringRef opName, MlirContext context)
Attach a dynamic op trait to the given operation name.
MLIR_CAPI_EXPORTED MlirTypeID mlirDynamicOpTraitNoTerminatorGetTypeID(void)
Get the type ID of the dynamic op trait that indicates regions have no terminator.
MLIR_CAPI_EXPORTED MlirDynamicOpTrait mlirDynamicOpTraitNoTerminatorCreate(void)
Get the dynamic op trait that indicates regions have no terminator.
MLIR_CAPI_EXPORTED MlirAttribute mlirLocationGetAttribute(MlirLocation location)
Returns the underlying location attribute of this location.
MlirWalkResult(* MlirOperationWalkCallback)(MlirOperation, void *userData)
Operation walker type.
MLIR_CAPI_EXPORTED MlirLocation mlirValueGetLocation(MlirValue v)
Gets the location of the value.
MLIR_CAPI_EXPORTED unsigned mlirContextGetNumThreads(MlirContext context)
Gets the number of threads of the thread pool of the context when multithreading is enabled.
MLIR_CAPI_EXPORTED void mlirOperationWriteBytecode(MlirOperation op, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but writing the bytecode format.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColGet(MlirContext context, MlirStringRef filename, unsigned line, unsigned col)
Creates an File/Line/Column location owned by the given context.
MLIR_CAPI_EXPORTED void mlirSymbolTableWalkSymbolTables(MlirOperation from, bool allSymUsesVisible, void(*callback)(MlirOperation, bool, void *userData), void *userData)
Walks all symbol table operations nested within, and including, op.
MLIR_CAPI_EXPORTED MlirStringRef mlirDialectGetNamespace(MlirDialect dialect)
Returns the namespace of the given dialect.
MLIR_CAPI_EXPORTED int mlirLocationFileLineColRangeGetEndColumn(MlirLocation location)
Getter for end_column of FileLineColRange.
MLIR_CAPI_EXPORTED MlirAttribute mlirSymbolTableInsert(MlirSymbolTable symbolTable, MlirOperation operation)
Inserts the given operation into the given symbol table.
MlirWalkOrder
Traversal order for operation walk.
MLIR_CAPI_EXPORTED MlirNamedAttribute mlirNamedAttributeGet(MlirIdentifier name, MlirAttribute attr)
Associates an attribute with the name. Takes ownership of neither.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGetChildLoc(MlirLocation location)
Getter for childLoc of Name.
MLIR_CAPI_EXPORTED void mlirSymbolTableErase(MlirSymbolTable symbolTable, MlirOperation operation)
Removes the given operation from the symbol table and erases it.
MLIR_CAPI_EXPORTED void mlirContextAppendDialectRegistry(MlirContext ctx, MlirDialectRegistry registry)
Append the contents of the given dialect registry to the registry associated with the context.
MLIR_CAPI_EXPORTED MlirStringRef mlirIdentifierStr(MlirIdentifier ident)
Gets the string value of the identifier.
MLIR_CAPI_EXPORTED MlirType mlirTypeParseGet(MlirContext context, MlirStringRef type)
Parses a type. The type is owned by the context.
MLIR_CAPI_EXPORTED MlirOpOperand mlirOpOperandGetNextUse(MlirOpOperand opOperand)
Returns an op operand representing the next use of the value, or a null op operand if there is no nex...
MLIR_CAPI_EXPORTED void mlirContextSetAllowUnregisteredDialects(MlirContext context, bool allow)
Sets whether unregistered dialects are allowed in this context.
MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockBefore(MlirRegion region, MlirBlock reference, MlirBlock block)
Takes a block owned by the caller and inserts it before the (non-owned) reference block in the given ...
MLIR_CAPI_EXPORTED bool mlirLocationIsAFileLineColRange(MlirLocation location)
Checks whether the given location is an FileLineColRange.
MLIR_CAPI_EXPORTED unsigned mlirLocationFusedGetNumLocations(MlirLocation location)
Getter for number of locations fused together.
MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesOfWith(MlirValue of, MlirValue with)
Replace all uses of 'of' value with the 'with' value, updating anything in the IR that uses 'of' to u...
MLIR_CAPI_EXPORTED void mlirValuePrintAsOperand(MlirValue value, MlirAsmState state, MlirStringCallback callback, void *userData)
Prints a value as an operand (i.e., the ValueID).
MLIR_CAPI_EXPORTED MlirLocation mlirLocationUnknownGet(MlirContext context)
Creates a location with unknown position owned by the given context.
MLIR_CAPI_EXPORTED MlirOperation mlirOpOperandGetOwner(MlirOpOperand opOperand)
Returns the owner operation of an op operand.
MLIR_CAPI_EXPORTED MlirIdentifier mlirLocationFileLineColRangeGetFilename(MlirLocation location)
Getter for filename of FileLineColRange.
MLIR_CAPI_EXPORTED void mlirLocationFusedGetLocations(MlirLocation location, MlirLocation *locationsCPtr)
Getter for locations of Fused.
MLIR_CAPI_EXPORTED void mlirAttributePrint(MlirAttribute attr, MlirStringCallback callback, void *userData)
Prints a location by sending chunks of the string representation and forwarding userData to callback`...
MLIR_CAPI_EXPORTED MlirRegion mlirBlockGetParentRegion(MlirBlock block)
Returns the region that contains this block.
MLIR_CAPI_EXPORTED void mlirOperationMoveBefore(MlirOperation op, MlirOperation other)
Moves the given operation immediately before the other operation in its parent block.
MLIR_CAPI_EXPORTED void mlirValueReplaceAllUsesExcept(MlirValue of, MlirValue with, intptr_t numExceptions, MlirOperation *exceptions)
Replace all uses of 'of' value with 'with' value, updating anything in the IR that uses 'of' to use '...
MLIR_CAPI_EXPORTED void mlirOperationPrintWithState(MlirOperation op, MlirAsmState state, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts AsmState controlling the printing behavior as well as caching ...
MlirWalkResult
Operation walk result.
MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlock(MlirRegion region, intptr_t pos, MlirBlock block)
Takes a block owned by the caller and inserts it at pos to the given region.
static bool mlirTypeIsNull(MlirType type)
Checks whether a type is null.
MLIR_CAPI_EXPORTED bool mlirContextIsRegisteredOperation(MlirContext context, MlirStringRef name)
Returns whether the given fully-qualified operation (i.e.
MLIR_CAPI_EXPORTED intptr_t mlirBlockGetNumArguments(MlirBlock block)
Returns the number of arguments of the block.
MLIR_CAPI_EXPORTED int mlirLocationFileLineColRangeGetStartLine(MlirLocation location)
Getter for start_line of FileLineColRange.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFusedGet(MlirContext ctx, intptr_t nLocations, MlirLocation const *locations, MlirAttribute metadata)
Creates a fused location with an array of locations and metadata.
MLIR_CAPI_EXPORTED void mlirBlockInsertOwnedOperationBefore(MlirBlock block, MlirOperation reference, MlirOperation operation)
Takes an operation owned by the caller and inserts it before the (non-owned) reference operation in t...
static bool mlirContextIsNull(MlirContext context)
Checks whether a context is null.
MLIR_CAPI_EXPORTED MlirDialect mlirContextGetOrLoadDialect(MlirContext context, MlirStringRef name)
Gets the dialect instance owned by the given context using the dialect namespace to identify it,...
MLIR_CAPI_EXPORTED bool mlirLocationIsACallSite(MlirLocation location)
Checks whether the given location is an CallSite.
MLIR_CAPI_EXPORTED void mlirRegionInsertOwnedBlockAfter(MlirRegion region, MlirBlock reference, MlirBlock block)
Takes a block owned by the caller and inserts it after the (non-owned) reference block in the given r...
MLIR_CAPI_EXPORTED MlirBlock mlirBlockCreate(intptr_t nArgs, MlirType const *args, MlirLocation const *locs)
Creates a new empty block with the given argument types and transfers ownership to the caller.
static bool mlirBlockIsNull(MlirBlock block)
Checks whether a block is null.
MLIR_CAPI_EXPORTED void mlirBlockAppendOwnedOperation(MlirBlock block, MlirOperation operation)
Takes an operation owned by the caller and appends it to the block.
MLIR_CAPI_EXPORTED MlirValue mlirBlockGetArgument(MlirBlock block, intptr_t pos)
Returns pos-th argument of the block.
MLIR_CAPI_EXPORTED MlirOperation mlirSymbolTableLookup(MlirSymbolTable symbolTable, MlirStringRef name)
Looks up a symbol with the given name in the given symbol table and returns the operation that corres...
MLIR_CAPI_EXPORTED MlirContext mlirTypeGetContext(MlirType type)
Gets the context that a type was created with.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFileLineColRangeGet(MlirContext context, MlirStringRef filename, unsigned start_line, unsigned start_col, unsigned end_line, unsigned end_col)
Creates an File/Line/Column range location owned by the given context.
MLIR_CAPI_EXPORTED bool mlirOpOperandIsNull(MlirOpOperand opOperand)
Returns whether the op operand is null.
MLIR_CAPI_EXPORTED MlirSymbolTable mlirSymbolTableCreate(MlirOperation operation)
Creates a symbol table for the given operation.
MLIR_CAPI_EXPORTED bool mlirLocationEqual(MlirLocation l1, MlirLocation l2)
Checks if two locations are equal.
MLIR_CAPI_EXPORTED int mlirLocationFileLineColRangeGetStartColumn(MlirLocation location)
Getter for start_column of FileLineColRange.
MLIR_CAPI_EXPORTED bool mlirLocationIsAFused(MlirLocation location)
Checks whether the given location is an Fused.
static bool mlirLocationIsNull(MlirLocation location)
Checks if the location is null.
MLIR_CAPI_EXPORTED MlirValue mlirBlockAddArgument(MlirBlock block, MlirType type, MlirLocation loc)
Appends an argument of the specified type to the block.
MLIR_CAPI_EXPORTED void mlirOperationPrintWithFlags(MlirOperation op, MlirOpPrintingFlags flags, MlirStringCallback callback, void *userData)
Same as mlirOperationPrint but accepts flags controlling the printing behavior.
MLIR_CAPI_EXPORTED MlirOpOperand mlirValueGetFirstUse(MlirValue value)
Returns an op operand representing the first use of the value, or a null op operand if there are no u...
MLIR_CAPI_EXPORTED void mlirContextSetThreadPool(MlirContext context, MlirLlvmThreadPool threadPool)
Sets the thread pool of the context explicitly, enabling multithreading in the process.
MLIR_CAPI_EXPORTED bool mlirOperationVerify(MlirOperation op)
Verify the operation and return true if it passes, false if it fails.
MLIR_CAPI_EXPORTED bool mlirTypeEqual(MlirType t1, MlirType t2)
Checks if two types are equal.
MLIR_CAPI_EXPORTED unsigned mlirOpOperandGetOperandNumber(MlirOpOperand opOperand)
Returns the operand number of an op operand.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGetCaller(MlirLocation location)
Getter for caller of CallSite.
MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetTerminator(MlirBlock block)
Returns the terminator operation in the block or null if no terminator.
MLIR_CAPI_EXPORTED MlirIdentifier mlirLocationNameGetName(MlirLocation location)
Getter for name of Name.
MLIR_CAPI_EXPORTED bool mlirOperationIsBeforeInBlock(MlirOperation op, MlirOperation other)
Given an operation 'other' that is within the same parent block, return whether the current operation...
MLIR_CAPI_EXPORTED MlirLocation mlirLocationFromAttribute(MlirAttribute attribute)
Creates a location from a location attribute.
MLIR_CAPI_EXPORTED MlirTypeID mlirTypeGetTypeID(MlirType type)
Gets the type ID of the type.
MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolTableGetVisibilityAttributeName(void)
Returns the name of the attribute used to store symbol visibility.
static bool mlirDialectIsNull(MlirDialect dialect)
Checks if the dialect is null.
MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block)
Returns the block immediately following the given block in its parent region.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGet(MlirLocation callee, MlirLocation caller)
Creates a call site location with a callee and a caller.
MLIR_CAPI_EXPORTED bool mlirLocationIsAName(MlirLocation location)
Checks whether the given location is an Name.
static bool mlirDialectRegistryIsNull(MlirDialectRegistry registry)
Checks if the dialect registry is null.
MLIR_CAPI_EXPORTED void mlirOperationWalk(MlirOperation op, MlirOperationWalkCallback callback, void *userData, MlirWalkOrder walkOrder)
Walks operation op in walkOrder and calls callback on that operation.
MLIR_CAPI_EXPORTED MlirContext mlirContextCreateWithThreading(bool threadingEnabled)
Creates an MLIR context with an explicit setting of the multithreading setting and transfers its owne...
MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock)
Returns the closest surrounding operation that contains this block.
MLIR_CAPI_EXPORTED MlirContext mlirLocationGetContext(MlirLocation location)
Gets the context that a location was created with.
MLIR_CAPI_EXPORTED void mlirBlockEraseArgument(MlirBlock block, unsigned index)
Erase the argument at 'index' and remove it from the argument list.
MLIR_CAPI_EXPORTED void mlirAttributeDump(MlirAttribute attr)
Prints the attribute to the standard error stream.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirSymbolTableReplaceAllSymbolUses(MlirStringRef oldSymbol, MlirStringRef newSymbol, MlirOperation from)
Attempt to replace all uses that are nested within the given operation of the given symbol 'oldSymbol...
MLIR_CAPI_EXPORTED void mlirRegionAppendOwnedBlock(MlirRegion region, MlirBlock block)
Takes a block owned by the caller and appends it to the given region.
MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetFirstOperation(MlirBlock block)
Returns the first operation in the block.
static bool mlirRegionIsNull(MlirRegion region)
Checks whether a region is null.
MLIR_CAPI_EXPORTED MlirDialect mlirTypeGetDialect(MlirType type)
Gets the dialect a type belongs to.
MLIR_CAPI_EXPORTED MlirIdentifier mlirIdentifierGet(MlirContext context, MlirStringRef str)
Gets an identifier with the given string value.
MLIR_CAPI_EXPORTED void mlirContextLoadAllAvailableDialects(MlirContext context)
Eagerly loads all available dialects registered with a context, making them available for use for IR ...
MLIR_CAPI_EXPORTED MlirLlvmThreadPool mlirContextGetThreadPool(MlirContext context)
Gets the thread pool of the context when enabled multithreading, otherwise an assertion is raised.
MLIR_CAPI_EXPORTED int mlirLocationFileLineColRangeGetEndLine(MlirLocation location)
Getter for end_line of FileLineColRange.
MLIR_CAPI_EXPORTED MlirLocation mlirLocationNameGet(MlirContext context, MlirStringRef name, MlirLocation childLoc)
Creates a name location owned by the given context.
MLIR_CAPI_EXPORTED void mlirContextEnableMultithreading(MlirContext context, bool enable)
Set threading mode (must be set to false to mlir-print-ir-after-all).
MLIR_CAPI_EXPORTED MlirLocation mlirLocationCallSiteGetCallee(MlirLocation location)
Getter for callee of CallSite.
MLIR_CAPI_EXPORTED MlirContext mlirValueGetContext(MlirValue v)
Gets the context that a value was created with.
MLIR_CAPI_EXPORTED MlirStringRef mlirSymbolTableGetSymbolAttributeName(void)
Returns the name of the attribute used to store symbol names compatible with symbol tables.
MLIR_CAPI_EXPORTED MlirRegion mlirRegionCreate(void)
Creates a new empty region and transfers ownership to the caller.
MLIR_CAPI_EXPORTED void mlirBlockDetach(MlirBlock block)
Detach a block from the owning region and assume ownership.
MLIR_CAPI_EXPORTED void mlirOperationDump(MlirOperation op)
Prints an operation to stderr.
static bool mlirSymbolTableIsNull(MlirSymbolTable symbolTable)
Returns true if the symbol table is null.
MLIR_CAPI_EXPORTED bool mlirContextGetAllowUnregisteredDialects(MlirContext context)
Returns whether the context allows unregistered dialects.
MLIR_CAPI_EXPORTED void mlirOperationReplaceUsesOfWith(MlirOperation op, MlirValue of, MlirValue with)
Replace uses of 'of' value with the 'with' value inside the 'op' operation.
MLIR_CAPI_EXPORTED void mlirOperationMoveAfter(MlirOperation op, MlirOperation other)
Moves the given operation immediately after the other operation in its parent block.
MLIR_CAPI_EXPORTED void mlirValuePrint(MlirValue value, MlirStringCallback callback, void *userData)
Prints a block by sending chunks of the string representation and forwarding userData to callback`.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirOperationWriteBytecodeWithConfig(MlirOperation op, MlirBytecodeWriterConfig config, MlirStringCallback callback, void *userData)
Same as mlirOperationWriteBytecode but with writer config and returns failure only if desired bytecod...
MLIR_CAPI_EXPORTED void mlirContextDestroy(MlirContext context)
Takes an MLIR context owned by the caller and destroys it.
MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region)
Gets the first block in the region.
static MlirStringRef mlirStringRefCreate(const char *str, size_t length)
Constructs a string reference from the pointer and length.
static MlirLogicalResult mlirLogicalResultFailure(void)
Creates a logical result representing a failure.
struct MlirLogicalResult MlirLogicalResult
MLIR_CAPI_EXPORTED int mlirLlvmThreadPoolGetMaxConcurrency(MlirLlvmThreadPool pool)
Returns the maximum number of threads in the thread pool.
MLIR_CAPI_EXPORTED void mlirLlvmThreadPoolDestroy(MlirLlvmThreadPool pool)
Destroy an LLVM thread pool.
MLIR_CAPI_EXPORTED MlirLlvmThreadPool mlirLlvmThreadPoolCreate(void)
Create an LLVM thread pool.
MLIR_CAPI_EXPORTED size_t mlirTypeIDHashValue(MlirTypeID typeID)
Returns the hash value of the type id.
static MlirLogicalResult mlirLogicalResultSuccess(void)
Creates a logical result representing a success.
struct MlirStringRef MlirStringRef
static bool mlirLogicalResultIsFailure(MlirLogicalResult res)
Checks if the given logical result represents a failure.
static bool mlirTypeIDIsNull(MlirTypeID typeID)
Checks whether a type id is null.
MLIR_CAPI_EXPORTED bool mlirTypeIDEqual(MlirTypeID typeID1, MlirTypeID typeID2)
Checks if two type ids are equal.
void walk(Operation *op, function_ref< void(Region *)> callback, WalkOrder order)
Walk all of the regions, blocks, or operations nested under (and including) the given operation.
MLIR_PYTHON_API_EXPORTED MlirValue getUniqueResult(MlirOperation operation)
static std::string formatMLIRError(const MLIRError &e)
MLIR_PYTHON_API_EXPORTED void populateRoot(nanobind::module_ &m)
static void maybeInsertOperation(PyOperationRef &op, const nb::object &maybeIp)
static void populateResultTypes(std::string_view name, nb::sequence resultTypeList, const nb::object &resultSegmentSpecObj, std::vector< int32_t > &resultSegmentLengths, std::vector< PyType * > &resultTypes)
PyObjectRef< PyMlirContext > PyMlirContextRef
Wrapper around MlirContext.
static MlirValue getOpResultOrValue(nb::handle operand)
PyObjectRef< PyOperation > PyOperationRef
MlirStringRef toMlirStringRef(const std::string &s)
static bool attachOpTrait(const nb::object &opName, MlirDynamicOpTrait trait, PyMlirContext &context)
PyObjectRef< PyModule > PyModuleRef
static MlirLogicalResult verifyTraitByMethod(MlirOperation op, void *userData, const char *methodName)
static PyOperationRef getValueOwnerRef(MlirValue value)
MlirBlock MLIR_PYTHON_API_EXPORTED createBlock(const nanobind::typed< nanobind::sequence, PyType > &pyArgTypes, const std::optional< nanobind::typed< nanobind::sequence, PyLocation > > &pyArgLocs)
Create a block, using the current location context if no locations are specified.
static std::vector< nb::typed< nb::object, PyType > > getValueTypes(Container &container, PyMlirContextRef &context)
Returns the list of types of the values held by container.
PyWalkOrder
Traversal order for operation walk.
MLIR_PYTHON_API_EXPORTED void populateIRCore(nanobind::module_ &m)
nanobind::object classmethod(Func f, Args... args)
Helper for creating an @classmethod.
Action
The actions performed by @newSparseTensor.
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
std::string join(const Ts &...args)
Helper function to concatenate arguments into a std::string.
An opaque reference to a diagnostic, always owned by the diagnostics engine (context).
MlirLogicalResult(* verifyTrait)(MlirOperation op, void *userData)
The callback function to verify the operation.
void(* construct)(void *userData)
Optional constructor for the user data.
void(* destruct)(void *userData)
Optional destructor for the user data.
MlirLogicalResult(* verifyRegionTrait)(MlirOperation op, void *userData)
The callback function to verify the operation with access to regions.
A logical result value, essentially a boolean with named states.
A pointer to a sized fragment of a string, not necessarily null-terminated.
const char * data
Pointer to the first symbol.
size_t length
Length of the fragment.
Accumulates into a python string from a method that accepts an MlirStringCallback.
MlirStringCallback getCallback()
Custom exception that allows access to error diagnostic information.
MLIRError(std::string message, std::vector< PyDiagnostic::DiagnosticInfo > &&errorDiagnostics={})
std::vector< PyDiagnostic::DiagnosticInfo > errorDiagnostics
static void bind(nanobind::module_ &m)
Bind the MLIRError exception class to the given module.
static bool dunderContains(const std::string &attributeKind)
static nanobind::callable dunderGetItemNamed(const std::string &attributeKind)
static void bind(nanobind::module_ &m)
static void dunderSetItemNamed(const std::string &attributeKind, nanobind::callable func, bool replace, bool allow_existing)
Materialized diagnostic information.
std::vector< DiagnosticInfo > notes
PyDiagnosticSeverity severity
static void set(nanobind::object &o, bool enable)
static bool get(const nanobind::object &)
static void bind(nanobind::module_ &m)
RAII object that captures any error diagnostics emitted to the provided context.
std::vector< PyDiagnostic::DiagnosticInfo > take()
ErrorCapture(PyMlirContextRef ctx)