Debian Codegraph @

llvm-project/mlir/lib/Bindings/Python/IRCore.cpp

raw
1 //===- IRModules.cpp - IR Submodules of pybind module ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "IRModule.h"
10
11 #include "Globals.h"
12 #include "PybindUtils.h"
13
14 #include "mlir-c/Bindings/Python/Interop.h"
15 #include "mlir-c/BuiltinAttributes.h"
16 #include "mlir-c/BuiltinTypes.h"
17 #include "mlir-c/Debug.h"
18 #include "mlir-c/IR.h"
19 //#include "mlir-c/Registration.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/SmallVector.h"
22
23 #include <utility>
24 #include <optional>
25
26 namespace py = pybind11;
27 using namespace mlir;
28 using namespace mlir::python;
29
30 using llvm::SmallVector;
31 using llvm::StringRef;
32 using llvm::Twine;
33
34 //------------------------------------------------------------------------------
35 // Docstrings (trivial, non-duplicated docstrings are included inline).
36 //------------------------------------------------------------------------------
37
38 static const char kContextParseTypeDocstring[] =
39 R"(Parses the assembly form of a type.
40
41 Returns a Type object or raises a ValueError if the type cannot be parsed.
42
43 See also: https://mlir.llvm.org/docs/LangRef/#type-system
44 )";
45
46 static const char kContextGetCallSiteLocationDocstring[] =
47 R"(Gets a Location representing a caller and callsite)";
48
49 static const char kContextGetFileLocationDocstring[] =
50 R"(Gets a Location representing a file, line and column)";
51
52 static const char kContextGetFusedLocationDocstring[] =
53 R"(Gets a Location representing a fused location with optional metadata)";
54
55 static const char kContextGetNameLocationDocString[] =
56 R"(Gets a Location representing a named location with optional child location)";
57
58 static const char kModuleParseDocstring[] =
59 R"(Parses a module's assembly format from a string.
60
61 Returns a new MlirModule or raises a ValueError if the parsing fails.
62
63 See also: https://mlir.llvm.org/docs/LangRef/
64 )";
65
66 static const char kOperationCreateDocstring[] =
67 R"(Creates a new operation.
68
69 Args:
70 name: Operation name (e.g. "dialect.operation").
71 results: Sequence of Type representing op result types.
72 attributes: Dict of str:Attribute.
73 successors: List of Block for the operation's successors.
74 regions: Number of regions to create.
75 location: A Location object (defaults to resolve from context manager).
76 ip: An InsertionPoint (defaults to resolve from context manager or set to
77 False to disable insertion, even with an insertion point set in the
78 context manager).
79 Returns:
80 A new "detached" Operation object. Detached operations can be added
81 to blocks, which causes them to become "attached."
82 )";
83
84 static const char kOperationPrintDocstring[] =
85 R"(Prints the assembly form of the operation to a file like object.
86
87 Args:
88 file: The file like object to write to. Defaults to sys.stdout.
89 binary: Whether to write bytes (True) or str (False). Defaults to False.
90 large_elements_limit: Whether to elide elements attributes above this
91 number of elements. Defaults to None (no limit).
92 enable_debug_info: Whether to print debug/location information. Defaults
93 to False.
94 pretty_debug_info: Whether to format debug information for easier reading
95 by a human (warning: the result is unparseable).
96 print_generic_op_form: Whether to print the generic assembly forms of all
97 ops. Defaults to False.
98 use_local_Scope: Whether to print in a way that is more optimized for
99 multi-threaded access but may not be consistent with how the overall
100 module prints.
101 assume_verified: By default, if not printing generic form, the verifier
102 will be run and if it fails, generic form will be printed with a comment
103 about failed verification. While a reasonable default for interactive use,
104 for systematic use, it is often better for the caller to verify explicitly
105 and report failures in a more robust fashion. Set this to True if doing this
106 in order to avoid running a redundant verification. If the IR is actually
107 invalid, behavior is undefined.
108 )";
109
110 static const char kOperationGetAsmDocstring[] =
111 R"(Gets the assembly form of the operation with all options available.
112
113 Args:
114 binary: Whether to return a bytes (True) or str (False) object. Defaults to
115 False.
116 ... others ...: See the print() method for common keyword arguments for
117 configuring the printout.
118 Returns:
119 Either a bytes or str object, depending on the setting of the 'binary'
120 argument.
121 )";
122
123 static const char kOperationPrintBytecodeDocstring[] =
124 R"(Write the bytecode form of the operation to a file like object.
125
126 Args:
127 file: The file like object to write to.
128 )";
129
130 static const char kOperationStrDunderDocstring[] =
131 R"(Gets the assembly form of the operation with default options.
132
133 If more advanced control over the assembly formatting or I/O options is needed,
134 use the dedicated print or get_asm method, which supports keyword arguments to
135 customize behavior.
136 )";
137
138 static const char kDumpDocstring[] =
139 R"(Dumps a debug representation of the object to stderr.)";
140
141 static const char kAppendBlockDocstring[] =
142 R"(Appends a new block, with argument types as positional args.
143
144 Returns:
145 The created block.
146 )";
147
148 static const char kValueDunderStrDocstring[] =
149 R"(Returns the string form of the value.
150
151 If the value is a block argument, this is the assembly form of its type and the
152 position in the argument list. If the value is an operation result, this is
153 equivalent to printing the operation that produced it.
154 )";
155
156 //------------------------------------------------------------------------------
157 // Utilities.
158 //------------------------------------------------------------------------------
159
160 /// Helper for creating an @classmethod.
161 template <class Func, typename... Args>
162 py::object classmethod(Func f, Args... args) {
163 py::object cf = py::cpp_function(f, args...);
164 return py::reinterpret_borrow<py::object>((PyClassMethod_New(cf.ptr())));
165 }
166
167 static py::object
168 createCustomDialectWrapper(const std::string &dialectNamespace,
169 py::object dialectDescriptor) {
170 auto dialectClass = PyGlobals::get().lookupDialectClass(dialectNamespace);
171 if (!dialectClass) {
172 // Use the base class.
173 return py::cast(PyDialect(std::move(dialectDescriptor)));
174 }
175
176 // Create the custom implementation.
177 return (*dialectClass)(std::move(dialectDescriptor));
178 }
179
180 static MlirStringRef toMlirStringRef(const std::string &s) {
181 return mlirStringRefCreate(s.data(), s.size());
182 }
183
184 /// Wrapper for the global LLVM debugging flag.
185 struct PyGlobalDebugFlag {
186 static void set(py::object &o, bool enable) { mlirEnableGlobalDebug(enable); }
187
188 static bool get(const py::object &) { return mlirIsGlobalDebugEnabled(); }
189
190 static void bind(py::module &m) {
191 // Debug flags.
192 py::class_<PyGlobalDebugFlag>(m, "_GlobalDebug", py::module_local())
193 .def_property_static("flag", &PyGlobalDebugFlag::get,
194 &PyGlobalDebugFlag::set, "LLVM-wide debug flag");
195 }
196 };
197
198 struct PyAttrBuilderMap {
199 static bool dunderContains(const std::string &attributeKind) {
200 return PyGlobals::get().lookupAttributeBuilder(attributeKind).has_value();
201 }
202 static py::function dundeGetItemNamed(const std::string &attributeKind) {
203 auto builder = PyGlobals::get().lookupAttributeBuilder(attributeKind);
204 if (!builder)
205 throw py::key_error();
206 return *builder;
207 }
208 static void dundeSetItemNamed(const std::string &attributeKind,
209 py::function func) {
210 PyGlobals::get().registerAttributeBuilder(attributeKind, std::move(func));
211 }
212
213 static void bind(py::module &m) {
214 py::class_<PyAttrBuilderMap>(m, "AttrBuilder", py::module_local())
215 .def_static("contains", &PyAttrBuilderMap::dunderContains)
216 .def_static("get", &PyAttrBuilderMap::dundeGetItemNamed)
217 .def_static("insert", &PyAttrBuilderMap::dundeSetItemNamed);
218 }
219 };
220
221 //------------------------------------------------------------------------------
222 // Collections.
223 //------------------------------------------------------------------------------
224
225 namespace {
226
227 class PyRegionIterator {
228 public:
229 PyRegionIterator(PyOperationRef operation)
230 : operation(std::move(operation)) {}
231
232 PyRegionIterator &dunderIter() { return *this; }
233
234 PyRegion dunderNext() {
235 operation->checkValid();
236 if (nextIndex >= mlirOperationGetNumRegions(operation->get())) {
237 throw py::stop_iteration();
238 }
239 MlirRegion region = mlirOperationGetRegion(operation->get(), nextIndex++);
240 return PyRegion(operation, region);
241 }
242
243 static void bind(py::module &m) {
244 py::class_<PyRegionIterator>(m, "RegionIterator", py::module_local())
245 .def("__iter__", &PyRegionIterator::dunderIter)
246 .def("__next__", &PyRegionIterator::dunderNext);
247 }
248
249 private:
250 PyOperationRef operation;
251 int nextIndex = 0;
252 };
253
254 /// Regions of an op are fixed length and indexed numerically so are represented
255 /// with a sequence-like container.
256 class PyRegionList {
257 public:
258 PyRegionList(PyOperationRef operation) : operation(std::move(operation)) {}
259
260 intptr_t dunderLen() {
261 operation->checkValid();
262 return mlirOperationGetNumRegions(operation->get());
263 }
264
265 PyRegion dunderGetItem(intptr_t index) {
266 // dunderLen checks validity.
267 if (index < 0 || index >= dunderLen()) {
268 throw SetPyError(PyExc_IndexError,
269 "attempt to access out of bounds region");
270 }
271 MlirRegion region = mlirOperationGetRegion(operation->get(), index);
272 return PyRegion(operation, region);
273 }
274
275 static void bind(py::module &m) {
276 py::class_<PyRegionList>(m, "RegionSequence", py::module_local())
277 .def("__len__", &PyRegionList::dunderLen)
278 .def("__getitem__", &PyRegionList::dunderGetItem);
279 }
280
281 private:
282 PyOperationRef operation;
283 };
284
285 class PyBlockIterator {
286 public:
287 PyBlockIterator(PyOperationRef operation, MlirBlock next)
288 : operation(std::move(operation)), next(next) {}
289
290 PyBlockIterator &dunderIter() { return *this; }
291
292 PyBlock dunderNext() {
293 operation->checkValid();
294 if (mlirBlockIsNull(next)) {
295 throw py::stop_iteration();
296 }
297
298 PyBlock returnBlock(operation, next);
299 next = mlirBlockGetNextInRegion(next);
300 return returnBlock;
301 }
302
303 static void bind(py::module &m) {
304 py::class_<PyBlockIterator>(m, "BlockIterator", py::module_local())
305 .def("__iter__", &PyBlockIterator::dunderIter)
306 .def("__next__", &PyBlockIterator::dunderNext);
307 }
308
309 private:
310 PyOperationRef operation;
311 MlirBlock next;
312 };
313
314 /// Blocks are exposed by the C-API as a forward-only linked list. In Python,
315 /// we present them as a more full-featured list-like container but optimize
316 /// it for forward iteration. Blocks are always owned by a region.
317 class PyBlockList {
318 public:
319 PyBlockList(PyOperationRef operation, MlirRegion region)
320 : operation(std::move(operation)), region(region) {}
321
322 PyBlockIterator dunderIter() {
323 operation->checkValid();
324 return PyBlockIterator(operation, mlirRegionGetFirstBlock(region));
325 }
326
327 intptr_t dunderLen() {
328 operation->checkValid();
329 intptr_t count = 0;
330 MlirBlock block = mlirRegionGetFirstBlock(region);
331 while (!mlirBlockIsNull(block)) {
332 count += 1;
333 block = mlirBlockGetNextInRegion(block);
334 }
335 return count;
336 }
337
338 PyBlock dunderGetItem(intptr_t index) {
339 operation->checkValid();
340 if (index < 0) {
341 throw SetPyError(PyExc_IndexError,
342 "attempt to access out of bounds block");
343 }
344 MlirBlock block = mlirRegionGetFirstBlock(region);
345 while (!mlirBlockIsNull(block)) {
346 if (index == 0) {
347 return PyBlock(operation, block);
348 }
349 block = mlirBlockGetNextInRegion(block);
350 index -= 1;
351 }
352 throw SetPyError(PyExc_IndexError, "attempt to access out of bounds block");
353 }
354
355 PyBlock appendBlock(const py::args &pyArgTypes) {
356 operation->checkValid();
357 llvm::SmallVector<MlirType, 4> argTypes;
358 llvm::SmallVector<MlirLocation, 4> argLocs;
359 argTypes.reserve(pyArgTypes.size());
360 argLocs.reserve(pyArgTypes.size());
361 for (auto &pyArg : pyArgTypes) {
362 argTypes.push_back(pyArg.cast<PyType &>());
363 // TODO: Pass in a proper location here.
364 argLocs.push_back(
365 mlirLocationUnknownGet(mlirTypeGetContext(argTypes.back())));
366 }
367
368 MlirBlock block =
369 mlirBlockCreate(argTypes.size(), argTypes.data(), argLocs.data());
370 mlirRegionAppendOwnedBlock(region, block);
371 return PyBlock(operation, block);
372 }
373
374 static void bind(py::module &m) {
375 py::class_<PyBlockList>(m, "BlockList", py::module_local())
376 .def("__getitem__", &PyBlockList::dunderGetItem)
377 .def("__iter__", &PyBlockList::dunderIter)
378 .def("__len__", &PyBlockList::dunderLen)
379 .def("append", &PyBlockList::appendBlock, kAppendBlockDocstring);
380 }
381
382 private:
383 PyOperationRef operation;
384 MlirRegion region;
385 };
386
387 class PyOperationIterator {
388 public:
389 PyOperationIterator(PyOperationRef parentOperation, MlirOperation next)
390 : parentOperation(std::move(parentOperation)), next(next) {}
391
392 PyOperationIterator &dunderIter() { return *this; }
393
394 py::object dunderNext() {
395 parentOperation->checkValid();
396 if (mlirOperationIsNull(next)) {
397 throw py::stop_iteration();
398 }
399
400 PyOperationRef returnOperation =
401 PyOperation::forOperation(parentOperation->getContext(), next);
402 next = mlirOperationGetNextInBlock(next);
403 return returnOperation->createOpView();
404 }
405
406 static void bind(py::module &m) {
407 py::class_<PyOperationIterator>(m, "OperationIterator", py::module_local())
408 .def("__iter__", &PyOperationIterator::dunderIter)
409 .def("__next__", &PyOperationIterator::dunderNext);
410 }
411
412 private:
413 PyOperationRef parentOperation;
414 MlirOperation next;
415 };
416
417 /// Operations are exposed by the C-API as a forward-only linked list. In
418 /// Python, we present them as a more full-featured list-like container but
419 /// optimize it for forward iteration. Iterable operations are always owned
420 /// by a block.
421 class PyOperationList {
422 public:
423 PyOperationList(PyOperationRef parentOperation, MlirBlock block)
424 : parentOperation(std::move(parentOperation)), block(block) {}
425
426 PyOperationIterator dunderIter() {
427 parentOperation->checkValid();
428 return PyOperationIterator(parentOperation,
429 mlirBlockGetFirstOperation(block));
430 }
431
432 intptr_t dunderLen() {
433 parentOperation->checkValid();
434 intptr_t count = 0;
435 MlirOperation childOp = mlirBlockGetFirstOperation(block);
436 while (!mlirOperationIsNull(childOp)) {
437 count += 1;
438 childOp = mlirOperationGetNextInBlock(childOp);
439 }
440 return count;
441 }
442
443 py::object dunderGetItem(intptr_t index) {
444 parentOperation->checkValid();
445 if (index < 0) {
446 throw SetPyError(PyExc_IndexError,
447 "attempt to access out of bounds operation");
448 }
449 MlirOperation childOp = mlirBlockGetFirstOperation(block);
450 while (!mlirOperationIsNull(childOp)) {
451 if (index == 0) {
452 return PyOperation::forOperation(parentOperation->getContext(), childOp)
453 ->createOpView();
454 }
455 childOp = mlirOperationGetNextInBlock(childOp);
456 index -= 1;
457 }
458 throw SetPyError(PyExc_IndexError,
459 "attempt to access out of bounds operation");
460 }
461
462 static void bind(py::module &m) {
463 py::class_<PyOperationList>(m, "OperationList", py::module_local())
464 .def("__getitem__", &PyOperationList::dunderGetItem)
465 .def("__iter__", &PyOperationList::dunderIter)
466 .def("__len__", &PyOperationList::dunderLen);
467 }
468
469 private:
470 PyOperationRef parentOperation;
471 MlirBlock block;
472 };
473
474 class PyOpOperand {
475 public:
476 PyOpOperand(MlirOpOperand opOperand) : opOperand(opOperand) {}
477
478 py::object getOwner() {
479 MlirOperation owner = mlirOpOperandGetOwner(opOperand);
480 PyMlirContextRef context =
481 PyMlirContext::forContext(mlirOperationGetContext(owner));
482 return PyOperation::forOperation(context, owner)->createOpView();
483 }
484
485 size_t getOperandNumber() { return mlirOpOperandGetOperandNumber(opOperand); }
486
487 static void bind(py::module &m) {
488 py::class_<PyOpOperand>(m, "OpOperand", py::module_local())
489 .def_property_readonly("owner", &PyOpOperand::getOwner)
490 .def_property_readonly("operand_number",
491 &PyOpOperand::getOperandNumber);
492 }
493
494 private:
495 MlirOpOperand opOperand;
496 };
497
498 class PyOpOperandIterator {
499 public:
500 PyOpOperandIterator(MlirOpOperand opOperand) : opOperand(opOperand) {}
501
502 PyOpOperandIterator &dunderIter() { return *this; }
503
504 PyOpOperand dunderNext() {
505 if (mlirOpOperandIsNull(opOperand))
506 throw py::stop_iteration();
507
508 PyOpOperand returnOpOperand(opOperand);
509 opOperand = mlirOpOperandGetNextUse(opOperand);
510 return returnOpOperand;
511 }
512
513 static void bind(py::module &m) {
514 py::class_<PyOpOperandIterator>(m, "OpOperandIterator", py::module_local())
515 .def("__iter__", &PyOpOperandIterator::dunderIter)
516 .def("__next__", &PyOpOperandIterator::dunderNext);
517 }
518
519 private:
520 MlirOpOperand opOperand;
521 };
522
523 } // namespace
524
525 //------------------------------------------------------------------------------
526 // PyMlirContext
527 //------------------------------------------------------------------------------
528
529 PyMlirContext::PyMlirContext(MlirContext context) : context(context) {
530 py::gil_scoped_acquire acquire;
531 auto &liveContexts = getLiveContexts();
532 liveContexts[context.ptr] = this;
533 }
534
535 PyMlirContext::~PyMlirContext() {
536 // Note that the only public way to construct an instance is via the
537 // forContext method, which always puts the associated handle into
538 // liveContexts.
539 py::gil_scoped_acquire acquire;
540 getLiveContexts().erase(context.ptr);
541 mlirContextDestroy(context);
542 }
543
544 py::object PyMlirContext::getCapsule() {
545 return py::reinterpret_steal<py::object>(mlirPythonContextToCapsule(get()));
546 }
547
548 py::object PyMlirContext::createFromCapsule(py::object capsule) {
549 MlirContext rawContext = mlirPythonCapsuleToContext(capsule.ptr());
550 if (mlirContextIsNull(rawContext))
551 throw py::error_already_set();
552 return forContext(rawContext).releaseObject();
553 }
554
555 PyMlirContext *PyMlirContext::createNewContextForInit() {
556 MlirContext context = mlirContextCreate();
557 return new PyMlirContext(context);
558 }
559
560 PyMlirContextRef PyMlirContext::forContext(MlirContext context) {
561 py::gil_scoped_acquire acquire;
562 auto &liveContexts = getLiveContexts();
563 auto it = liveContexts.find(context.ptr);
564 if (it == liveContexts.end()) {
565 // Create.
566 PyMlirContext *unownedContextWrapper = new PyMlirContext(context);
567 py::object pyRef = py::cast(unownedContextWrapper);
568 assert(pyRef && "cast to py::object failed");
569 liveContexts[context.ptr] = unownedContextWrapper;
570 return PyMlirContextRef(unownedContextWrapper, std::move(pyRef));
571 }
572 // Use existing.
573 py::object pyRef = py::cast(it->second);
574 return PyMlirContextRef(it->second, std::move(pyRef));
575 }
576
577 PyMlirContext::LiveContextMap &PyMlirContext::getLiveContexts() {
578 static LiveContextMap liveContexts;
579 return liveContexts;
580 }
581
582 size_t PyMlirContext::getLiveCount() { return getLiveContexts().size(); }
583
584 size_t PyMlirContext::getLiveOperationCount() { return liveOperations.size(); }
585
586 size_t PyMlirContext::clearLiveOperations() {
587 for (auto &op : liveOperations)
588 op.second.second->setInvalid();
589 size_t numInvalidated = liveOperations.size();
590 liveOperations.clear();
591 return numInvalidated;
592 }
593
594 size_t PyMlirContext::getLiveModuleCount() { return liveModules.size(); }
595
596 pybind11::object PyMlirContext::contextEnter() {
597 return PyThreadContextEntry::pushContext(*this);
598 }
599
600 void PyMlirContext::contextExit(const pybind11::object &excType,
601 const pybind11::object &excVal,
602 const pybind11::object &excTb) {
603 PyThreadContextEntry::popContext(*this);
604 }
605
606 py::object PyMlirContext::attachDiagnosticHandler(py::object callback) {
607 // Note that ownership is transferred to the delete callback below by way of
608 // an explicit inc_ref (borrow).
609 PyDiagnosticHandler *pyHandler =
610 new PyDiagnosticHandler(get(), std::move(callback));
611 py::object pyHandlerObject =
612 py::cast(pyHandler, py::return_value_policy::take_ownership);
613 pyHandlerObject.inc_ref();
614
615 // In these C callbacks, the userData is a PyDiagnosticHandler* that is
616 // guaranteed to be known to pybind.
617 auto handlerCallback =
618 +[](MlirDiagnostic diagnostic, void *userData) -> MlirLogicalResult {
619 PyDiagnostic *pyDiagnostic = new PyDiagnostic(diagnostic);
620 py::object pyDiagnosticObject =
621 py::cast(pyDiagnostic, py::return_value_policy::take_ownership);
622
623 auto *pyHandler = static_cast<PyDiagnosticHandler *>(userData);
624 bool result = false;
625 {
626 // Since this can be called from arbitrary C++ contexts, always get the
627 // gil.
628 py::gil_scoped_acquire gil;
629 try {
630 result = py::cast<bool>(pyHandler->callback(pyDiagnostic));
631 } catch (std::exception &e) {
632 fprintf(stderr, "MLIR Python Diagnostic handler raised exception: %s\n",
633 e.what());
634 pyHandler->hadError = true;
635 }
636 }
637
638 pyDiagnostic->invalidate();
639 return result ? mlirLogicalResultSuccess() : mlirLogicalResultFailure();
640 };
641 auto deleteCallback = +[](void *userData) {
642 auto *pyHandler = static_cast<PyDiagnosticHandler *>(userData);
643 assert(pyHandler->registeredID && "handler is not registered");
644 pyHandler->registeredID.reset();
645
646 // Decrement reference, balancing the inc_ref() above.
647 py::object pyHandlerObject =
648 py::cast(pyHandler, py::return_value_policy::reference);
649 pyHandlerObject.dec_ref();
650 };
651
652 pyHandler->registeredID = mlirContextAttachDiagnosticHandler(
653 get(), handlerCallback, static_cast<void *>(pyHandler), deleteCallback);
654 return pyHandlerObject;
655 }
656
657 PyMlirContext &DefaultingPyMlirContext::resolve() {
658 PyMlirContext *context = PyThreadContextEntry::getDefaultContext();
659 if (!context) {
660 throw SetPyError(
661 PyExc_RuntimeError,
662 "An MLIR function requires a Context but none was provided in the call "
663 "or from the surrounding environment. Either pass to the function with "
664 "a 'context=' argument or establish a default using 'with Context():'");
665 }
666 return *context;
667 }
668
669 //------------------------------------------------------------------------------
670 // PyThreadContextEntry management
671 //------------------------------------------------------------------------------
672
673 std::vector<PyThreadContextEntry> &PyThreadContextEntry::getStack() {
674 static thread_local std::vector<PyThreadContextEntry> stack;
675 return stack;
676 }
677
678 PyThreadContextEntry *PyThreadContextEntry::getTopOfStack() {
679 auto &stack = getStack();
680 if (stack.empty())
681 return nullptr;
682 return &stack.back();
683 }
684
685 void PyThreadContextEntry::push(FrameKind frameKind, py::object context,
686 py::object insertionPoint,
687 py::object location) {
688 auto &stack = getStack();
689 stack.emplace_back(frameKind, std::move(context), std::move(insertionPoint),
690 std::move(location));
691 // If the new stack has more than one entry and the context of the new top
692 // entry matches the previous, copy the insertionPoint and location from the
693 // previous entry if missing from the new top entry.
694 if (stack.size() > 1) {
695 auto &prev = *(stack.rbegin() + 1);
696 auto &current = stack.back();
697 if (current.context.is(prev.context)) {
698 // Default non-context objects from the previous entry.
699 if (!current.insertionPoint)
700 current.insertionPoint = prev.insertionPoint;
701 if (!current.location)
702 current.location = prev.location;
703 }
704 }
705 }
706
707 PyMlirContext *PyThreadContextEntry::getContext() {
708 if (!context)
709 return nullptr;
710 return py::cast<PyMlirContext *>(context);
711 }
712
713 PyInsertionPoint *PyThreadContextEntry::getInsertionPoint() {
714 if (!insertionPoint)
715 return nullptr;
716 return py::cast<PyInsertionPoint *>(insertionPoint);
717 }
718
719 PyLocation *PyThreadContextEntry::getLocation() {
720 if (!location)
721 return nullptr;
722 return py::cast<PyLocation *>(location);
723 }
724
725 PyMlirContext *PyThreadContextEntry::getDefaultContext() {
726 auto *tos = getTopOfStack();
727 return tos ? tos->getContext() : nullptr;
728 }
729
730 PyInsertionPoint *PyThreadContextEntry::getDefaultInsertionPoint() {
731 auto *tos = getTopOfStack();
732 return tos ? tos->getInsertionPoint() : nullptr;
733 }
734
735 PyLocation *PyThreadContextEntry::getDefaultLocation() {
736 auto *tos = getTopOfStack();
737 return tos ? tos->getLocation() : nullptr;
738 }
739
740 py::object PyThreadContextEntry::pushContext(PyMlirContext &context) {
741 py::object contextObj = py::cast(context);
742 push(FrameKind::Context, /*context=*/contextObj,
743 /*insertionPoint=*/py::object(),
744 /*location=*/py::object());
745 return contextObj;
746 }
747
748 void PyThreadContextEntry::popContext(PyMlirContext &context) {
749 auto &stack = getStack();
750 if (stack.empty())
751 throw SetPyError(PyExc_RuntimeError, "Unbalanced Context enter/exit");
752 auto &tos = stack.back();
753 if (tos.frameKind != FrameKind::Context && tos.getContext() != &context)
754 throw SetPyError(PyExc_RuntimeError, "Unbalanced Context enter/exit");
755 stack.pop_back();
756 }
757
758 py::object
759 PyThreadContextEntry::pushInsertionPoint(PyInsertionPoint &insertionPoint) {
760 py::object contextObj =
761 insertionPoint.getBlock().getParentOperation()->getContext().getObject();
762 py::object insertionPointObj = py::cast(insertionPoint);
763 push(FrameKind::InsertionPoint,
764 /*context=*/contextObj,
765 /*insertionPoint=*/insertionPointObj,
766 /*location=*/py::object());
767 return insertionPointObj;
768 }
769
770 void PyThreadContextEntry::popInsertionPoint(PyInsertionPoint &insertionPoint) {
771 auto &stack = getStack();
772 if (stack.empty())
773 throw SetPyError(PyExc_RuntimeError,
774 "Unbalanced InsertionPoint enter/exit");
775 auto &tos = stack.back();
776 if (tos.frameKind != FrameKind::InsertionPoint &&
777 tos.getInsertionPoint() != &insertionPoint)
778 throw SetPyError(PyExc_RuntimeError,
779 "Unbalanced InsertionPoint enter/exit");
780 stack.pop_back();
781 }
782
783 py::object PyThreadContextEntry::pushLocation(PyLocation &location) {
784 py::object contextObj = location.getContext().getObject();
785 py::object locationObj = py::cast(location);
786 push(FrameKind::Location, /*context=*/contextObj,
787 /*insertionPoint=*/py::object(),
788 /*location=*/locationObj);
789 return locationObj;
790 }
791
792 void PyThreadContextEntry::popLocation(PyLocation &location) {
793 auto &stack = getStack();
794 if (stack.empty())
795 throw SetPyError(PyExc_RuntimeError, "Unbalanced Location enter/exit");
796 auto &tos = stack.back();
797 if (tos.frameKind != FrameKind::Location && tos.getLocation() != &location)
798 throw SetPyError(PyExc_RuntimeError, "Unbalanced Location enter/exit");
799 stack.pop_back();
800 }
801
802 //------------------------------------------------------------------------------
803 // PyDiagnostic*
804 //------------------------------------------------------------------------------
805
806 void PyDiagnostic::invalidate() {
807 valid = false;
808 if (materializedNotes) {
809 for (auto &noteObject : *materializedNotes) {
810 PyDiagnostic *note = py::cast<PyDiagnostic *>(noteObject);
811 note->invalidate();
812 }
813 }
814 }
815
816 PyDiagnosticHandler::PyDiagnosticHandler(MlirContext context,
817 py::object callback)
818 : context(context), callback(std::move(callback)) {}
819
820 PyDiagnosticHandler::~PyDiagnosticHandler() = default;
821
822 void PyDiagnosticHandler::detach() {
823 if (!registeredID)
824 return;
825 MlirDiagnosticHandlerID localID = *registeredID;
826 mlirContextDetachDiagnosticHandler(context, localID);
827 assert(!registeredID && "should have unregistered");
828 // Not strictly necessary but keeps stale pointers from being around to cause
829 // issues.
830 context = {nullptr};
831 }
832
833 void PyDiagnostic::checkValid() {
834 if (!valid) {
835 throw std::invalid_argument(
836 "Diagnostic is invalid (used outside of callback)");
837 }
838 }
839
840 MlirDiagnosticSeverity PyDiagnostic::getSeverity() {
841 checkValid();
842 return mlirDiagnosticGetSeverity(diagnostic);
843 }
844
845 PyLocation PyDiagnostic::getLocation() {
846 checkValid();
847 MlirLocation loc = mlirDiagnosticGetLocation(diagnostic);
848 MlirContext context = mlirLocationGetContext(loc);
849 return PyLocation(PyMlirContext::forContext(context), loc);
850 }
851
852 py::str PyDiagnostic::getMessage() {
853 checkValid();
854 py::object fileObject = py::module::import("io").attr("StringIO")();
855 PyFileAccumulator accum(fileObject, /*binary=*/false);
856 mlirDiagnosticPrint(diagnostic, accum.getCallback(), accum.getUserData());
857 return fileObject.attr("getvalue")();
858 }
859
860 py::tuple PyDiagnostic::getNotes() {
861 checkValid();
862 if (materializedNotes)
863 return *materializedNotes;
864 intptr_t numNotes = mlirDiagnosticGetNumNotes(diagnostic);
865 materializedNotes = py::tuple(numNotes);
866 for (intptr_t i = 0; i < numNotes; ++i) {
867 MlirDiagnostic noteDiag = mlirDiagnosticGetNote(diagnostic, i);
868 (*materializedNotes)[i] = PyDiagnostic(noteDiag);
869 }
870 return *materializedNotes;
871 }
872
873 //------------------------------------------------------------------------------
874 // PyDialect, PyDialectDescriptor, PyDialects, PyDialectRegistry
875 //------------------------------------------------------------------------------
876
877 MlirDialect PyDialects::getDialectForKey(const std::string &key,
878 bool attrError) {
879 MlirDialect dialect = mlirContextGetOrLoadDialect(getContext()->get(),
880 {key.data(), key.size()});
881 if (mlirDialectIsNull(dialect)) {
882 throw SetPyError(attrError ? PyExc_AttributeError : PyExc_IndexError,
883 Twine("Dialect '") + key + "' not found");
884 }
885 return dialect;
886 }
887
888 py::object PyDialectRegistry::getCapsule() {
889 return py::reinterpret_steal<py::object>(
890 mlirPythonDialectRegistryToCapsule(*this));
891 }
892
893 PyDialectRegistry PyDialectRegistry::createFromCapsule(py::object capsule) {
894 MlirDialectRegistry rawRegistry =
895 mlirPythonCapsuleToDialectRegistry(capsule.ptr());
896 if (mlirDialectRegistryIsNull(rawRegistry))
897 throw py::error_already_set();
898 return PyDialectRegistry(rawRegistry);
899 }
900
901 //------------------------------------------------------------------------------
902 // PyLocation
903 //------------------------------------------------------------------------------
904
905 py::object PyLocation::getCapsule() {
906 return py::reinterpret_steal<py::object>(mlirPythonLocationToCapsule(*this));
907 }
908
909 PyLocation PyLocation::createFromCapsule(py::object capsule) {
910 MlirLocation rawLoc = mlirPythonCapsuleToLocation(capsule.ptr());
911 if (mlirLocationIsNull(rawLoc))
912 throw py::error_already_set();
913 return PyLocation(PyMlirContext::forContext(mlirLocationGetContext(rawLoc)),
914 rawLoc);
915 }
916
917 py::object PyLocation::contextEnter() {
918 return PyThreadContextEntry::pushLocation(*this);
919 }
920
921 void PyLocation::contextExit(const pybind11::object &excType,
922 const pybind11::object &excVal,
923 const pybind11::object &excTb) {
924 PyThreadContextEntry::popLocation(*this);
925 }
926
927 PyLocation &DefaultingPyLocation::resolve() {
928 auto *location = PyThreadContextEntry::getDefaultLocation();
929 if (!location) {
930 throw SetPyError(
931 PyExc_RuntimeError,
932 "An MLIR function requires a Location but none was provided in the "
933 "call or from the surrounding environment. Either pass to the function "
934 "with a 'loc=' argument or establish a default using 'with loc:'");
935 }
936 return *location;
937 }
938
939 //------------------------------------------------------------------------------
940 // PyModule
941 //------------------------------------------------------------------------------
942
943 PyModule::PyModule(PyMlirContextRef contextRef, MlirModule module)
944 : BaseContextObject(std::move(contextRef)), module(module) {}
945
946 PyModule::~PyModule() {
947 py::gil_scoped_acquire acquire;
948 auto &liveModules = getContext()->liveModules;
949 assert(liveModules.count(module.ptr) == 1 &&
950 "destroying module not in live map");
951 liveModules.erase(module.ptr);
952 mlirModuleDestroy(module);
953 }
954
955 PyModuleRef PyModule::forModule(MlirModule module) {
956 MlirContext context = mlirModuleGetContext(module);
957 PyMlirContextRef contextRef = PyMlirContext::forContext(context);
958
959 py::gil_scoped_acquire acquire;
960 auto &liveModules = contextRef->liveModules;
961 auto it = liveModules.find(module.ptr);
962 if (it == liveModules.end()) {
963 // Create.
964 PyModule *unownedModule = new PyModule(std::move(contextRef), module);
965 // Note that the default return value policy on cast is automatic_reference,
966 // which does not take ownership (delete will not be called).
967 // Just be explicit.
968 py::object pyRef =
969 py::cast(unownedModule, py::return_value_policy::take_ownership);
970 unownedModule->handle = pyRef;
971 liveModules[module.ptr] =
972 std::make_pair(unownedModule->handle, unownedModule);
973 return PyModuleRef(unownedModule, std::move(pyRef));
974 }
975 // Use existing.
976 PyModule *existing = it->second.second;
977 py::object pyRef = py::reinterpret_borrow<py::object>(it->second.first);
978 return PyModuleRef(existing, std::move(pyRef));
979 }
980
981 py::object PyModule::createFromCapsule(py::object capsule) {
982 MlirModule rawModule = mlirPythonCapsuleToModule(capsule.ptr());
983 if (mlirModuleIsNull(rawModule))
984 throw py::error_already_set();
985 return forModule(rawModule).releaseObject();
986 }
987
988 py::object PyModule::getCapsule() {
989 return py::reinterpret_steal<py::object>(mlirPythonModuleToCapsule(get()));
990 }
991
992 //------------------------------------------------------------------------------
993 // PyOperation
994 //------------------------------------------------------------------------------
995
996 PyOperation::PyOperation(PyMlirContextRef contextRef, MlirOperation operation)
997 : BaseContextObject(std::move(contextRef)), operation(operation) {}
998
999 PyOperation::~PyOperation() {
1000 // If the operation has already been invalidated there is nothing to do.
1001 if (!valid)
1002 return;
1003 auto &liveOperations = getContext()->liveOperations;
1004 assert(liveOperations.count(operation.ptr) == 1 &&
1005 "destroying operation not in live map");
1006 liveOperations.erase(operation.ptr);
1007 if (!isAttached()) {
1008 mlirOperationDestroy(operation);
1009 }
1010 }
1011
1012 PyOperationRef PyOperation::createInstance(PyMlirContextRef contextRef,
1013 MlirOperation operation,
1014 py::object parentKeepAlive) {
1015 auto &liveOperations = contextRef->liveOperations;
1016 // Create.
1017 PyOperation *unownedOperation =
1018 new PyOperation(std::move(contextRef), operation);
1019 // Note that the default return value policy on cast is automatic_reference,
1020 // which does not take ownership (delete will not be called).
1021 // Just be explicit.
1022 py::object pyRef =
1023 py::cast(unownedOperation, py::return_value_policy::take_ownership);
1024 unownedOperation->handle = pyRef;
1025 if (parentKeepAlive) {
1026 unownedOperation->parentKeepAlive = std::move(parentKeepAlive);
1027 }
1028 liveOperations[operation.ptr] = std::make_pair(pyRef, unownedOperation);
1029 return PyOperationRef(unownedOperation, std::move(pyRef));
1030 }
1031
1032 PyOperationRef PyOperation::forOperation(PyMlirContextRef contextRef,
1033 MlirOperation operation,
1034 py::object parentKeepAlive) {
1035 auto &liveOperations = contextRef->liveOperations;
1036 auto it = liveOperations.find(operation.ptr);
1037 if (it == liveOperations.end()) {
1038 // Create.
1039 return createInstance(std::move(contextRef), operation,
1040 std::move(parentKeepAlive));
1041 }
1042 // Use existing.
1043 PyOperation *existing = it->second.second;
1044 py::object pyRef = py::reinterpret_borrow<py::object>(it->second.first);
1045 return PyOperationRef(existing, std::move(pyRef));
1046 }
1047
1048 PyOperationRef PyOperation::createDetached(PyMlirContextRef contextRef,
1049 MlirOperation operation,
1050 py::object parentKeepAlive) {
1051 auto &liveOperations = contextRef->liveOperations;
1052 assert(liveOperations.count(operation.ptr) == 0 &&
1053 "cannot create detached operation that already exists");
1054 (void)liveOperations;
1055
1056 PyOperationRef created = createInstance(std::move(contextRef), operation,
1057 std::move(parentKeepAlive));
1058 created->attached = false;
1059 return created;
1060 }
1061
1062 void PyOperation::checkValid() const {
1063 if (!valid) {
1064 throw SetPyError(PyExc_RuntimeError, "the operation has been invalidated");
1065 }
1066 }
1067
1068 void PyOperationBase::print(py::object fileObject, bool binary,
1069 std::optional<int64_t> largeElementsLimit,
1070 bool enableDebugInfo, bool prettyDebugInfo,
1071 bool printGenericOpForm, bool useLocalScope,
1072 bool assumeVerified) {
1073 PyOperation &operation = getOperation();
1074 operation.checkValid();
1075 if (fileObject.is_none())
1076 fileObject = py::module::import("sys").attr("stdout");
1077
1078 if (!assumeVerified && !printGenericOpForm &&
1079 !mlirOperationVerify(operation)) {
1080 std::string message("// Verification failed, printing generic form\n");
1081 if (binary) {
1082 fileObject.attr("write")(py::bytes(message));
1083 } else {
1084 fileObject.attr("write")(py::str(message));
1085 }
1086 printGenericOpForm = true;
1087 }
1088
1089 MlirOpPrintingFlags flags = mlirOpPrintingFlagsCreate();
1090 if (largeElementsLimit)
1091 mlirOpPrintingFlagsElideLargeElementsAttrs(flags, *largeElementsLimit);
1092 if (enableDebugInfo)
1093 mlirOpPrintingFlagsEnableDebugInfo(flags, /*enable=*/true,
1094 /*prettyForm=*/prettyDebugInfo);
1095 if (printGenericOpForm)
1096 mlirOpPrintingFlagsPrintGenericOpForm(flags);
1097 if (useLocalScope)
1098 mlirOpPrintingFlagsUseLocalScope(flags);
1099
1100 PyFileAccumulator accum(fileObject, binary);
1101 mlirOperationPrintWithFlags(operation, flags, accum.getCallback(),
1102 accum.getUserData());
1103 mlirOpPrintingFlagsDestroy(flags);
1104 }
1105
1106 void PyOperationBase::writeBytecode(const py::object &fileObject) {
1107 PyOperation &operation = getOperation();
1108 operation.checkValid();
1109 PyFileAccumulator accum(fileObject, /*binary=*/true);
1110 mlirOperationWriteBytecode(operation, accum.getCallback(),
1111 accum.getUserData());
1112 }
1113
1114 py::object PyOperationBase::getAsm(bool binary,
1115 std::optional<int64_t> largeElementsLimit,
1116 bool enableDebugInfo, bool prettyDebugInfo,
1117 bool printGenericOpForm, bool useLocalScope,
1118 bool assumeVerified) {
1119 py::object fileObject;
1120 if (binary) {
1121 fileObject = py::module::import("io").attr("BytesIO")();
1122 } else {
1123 fileObject = py::module::import("io").attr("StringIO")();
1124 }
1125 print(fileObject, /*binary=*/binary,
1126 /*largeElementsLimit=*/largeElementsLimit,
1127 /*enableDebugInfo=*/enableDebugInfo,
1128 /*prettyDebugInfo=*/prettyDebugInfo,
1129 /*printGenericOpForm=*/printGenericOpForm,
1130 /*useLocalScope=*/useLocalScope,
1131 /*assumeVerified=*/assumeVerified);
1132
1133 return fileObject.attr("getvalue")();
1134 }
1135
1136 void PyOperationBase::moveAfter(PyOperationBase &other) {
1137 PyOperation &operation = getOperation();
1138 PyOperation &otherOp = other.getOperation();
1139 operation.checkValid();
1140 otherOp.checkValid();
1141 mlirOperationMoveAfter(operation, otherOp);
1142 operation.parentKeepAlive = otherOp.parentKeepAlive;
1143 }
1144
1145 void PyOperationBase::moveBefore(PyOperationBase &other) {
1146 PyOperation &operation = getOperation();
1147 PyOperation &otherOp = other.getOperation();
1148 operation.checkValid();
1149 otherOp.checkValid();
1150 mlirOperationMoveBefore(operation, otherOp);
1151 operation.parentKeepAlive = otherOp.parentKeepAlive;
1152 }
1153
1154 std::optional<PyOperationRef> PyOperation::getParentOperation() {
1155 checkValid();
1156 if (!isAttached())
1157 throw SetPyError(PyExc_ValueError, "Detached operations have no parent");
1158 MlirOperation operation = mlirOperationGetParentOperation(get());
1159 if (mlirOperationIsNull(operation))
1160 return {};
1161 return PyOperation::forOperation(getContext(), operation);
1162 }
1163
1164 PyBlock PyOperation::getBlock() {
1165 checkValid();
1166 std::optional<PyOperationRef> parentOperation = getParentOperation();
1167 MlirBlock block = mlirOperationGetBlock(get());
1168 assert(!mlirBlockIsNull(block) && "Attached operation has null parent");
1169 assert(parentOperation && "Operation has no parent");
1170 return PyBlock{std::move(*parentOperation), block};
1171 }
1172
1173 py::object PyOperation::getCapsule() {
1174 checkValid();
1175 return py::reinterpret_steal<py::object>(mlirPythonOperationToCapsule(get()));
1176 }
1177
1178 py::object PyOperation::createFromCapsule(py::object capsule) {
1179 MlirOperation rawOperation = mlirPythonCapsuleToOperation(capsule.ptr());
1180 if (mlirOperationIsNull(rawOperation))
1181 throw py::error_already_set();
1182 MlirContext rawCtxt = mlirOperationGetContext(rawOperation);
1183 return forOperation(PyMlirContext::forContext(rawCtxt), rawOperation)
1184 .releaseObject();
1185 }
1186
1187 static void maybeInsertOperation(PyOperationRef &op,
1188 const py::object &maybeIp) {
1189 // InsertPoint active?
1190 if (!maybeIp.is(py::cast(false))) {
1191 PyInsertionPoint *ip;
1192 if (maybeIp.is_none()) {
1193 ip = PyThreadContextEntry::getDefaultInsertionPoint();
1194 } else {
1195 ip = py::cast<PyInsertionPoint *>(maybeIp);
1196 }
1197 if (ip)
1198 ip->insert(*op.get());
1199 }
1200 }
1201
1202 py::object PyOperation::create(const std::string &name,
1203 std::optional<std::vector<PyType *>> results,
1204 std::optional<std::vector<PyValue *>> operands,
1205 std::optional<py::dict> attributes,
1206 std::optional<std::vector<PyBlock *>> successors,
1207 int regions, DefaultingPyLocation location,
1208 const py::object &maybeIp) {
1209 llvm::SmallVector<MlirValue, 4> mlirOperands;
1210 llvm::SmallVector<MlirType, 4> mlirResults;
1211 llvm::SmallVector<MlirBlock, 4> mlirSuccessors;
1212 llvm::SmallVector<std::pair<std::string, MlirAttribute>, 4> mlirAttributes;
1213
1214 // General parameter validation.
1215 if (regions < 0)
1216 throw SetPyError(PyExc_ValueError, "number of regions must be >= 0");
1217
1218 // Unpack/validate operands.
1219 if (operands) {
1220 mlirOperands.reserve(operands->size());
1221 for (PyValue *operand : *operands) {
1222 if (!operand)
1223 throw SetPyError(PyExc_ValueError, "operand value cannot be None");
1224 mlirOperands.push_back(operand->get());
1225 }
1226 }
1227
1228 // Unpack/validate results.
1229 if (results) {
1230 mlirResults.reserve(results->size());
1231 for (PyType *result : *results) {
1232 // TODO: Verify result type originate from the same context.
1233 if (!result)
1234 throw SetPyError(PyExc_ValueError, "result type cannot be None");
1235 mlirResults.push_back(*result);
1236 }
1237 }
1238 // Unpack/validate attributes.
1239 if (attributes) {
1240 mlirAttributes.reserve(attributes->size());
1241 for (auto &it : *attributes) {
1242 std::string key;
1243 try {
1244 key = it.first.cast<std::string>();
1245 } catch (py::cast_error &err) {
1246 std::string msg = "Invalid attribute key (not a string) when "
1247 "attempting to create the operation \"" +
1248 name + "\" (" + err.what() + ")";
1249 throw py::cast_error(msg);
1250 }
1251 try {
1252 auto &attribute = it.second.cast<PyAttribute &>();
1253 // TODO: Verify attribute originates from the same context.
1254 mlirAttributes.emplace_back(std::move(key), attribute);
1255 } catch (py::reference_cast_error &) {
1256 // This exception seems thrown when the value is "None".
1257 std::string msg =
1258 "Found an invalid (`None`?) attribute value for the key \"" + key +
1259 "\" when attempting to create the operation \"" + name + "\"";
1260 throw py::cast_error(msg);
1261 } catch (py::cast_error &err) {
1262 std::string msg = "Invalid attribute value for the key \"" + key +
1263 "\" when attempting to create the operation \"" +
1264 name + "\" (" + err.what() + ")";
1265 throw py::cast_error(msg);
1266 }
1267 }
1268 }
1269 // Unpack/validate successors.
1270 if (successors) {
1271 mlirSuccessors.reserve(successors->size());
1272 for (auto *successor : *successors) {
1273 // TODO: Verify successor originate from the same context.
1274 if (!successor)
1275 throw SetPyError(PyExc_ValueError, "successor block cannot be None");
1276 mlirSuccessors.push_back(successor->get());
1277 }
1278 }
1279
1280 // Apply unpacked/validated to the operation state. Beyond this
1281 // point, exceptions cannot be thrown or else the state will leak.
1282 MlirOperationState state =
1283 mlirOperationStateGet(toMlirStringRef(name), location);
1284 if (!mlirOperands.empty())
1285 mlirOperationStateAddOperands(&state, mlirOperands.size(),
1286 mlirOperands.data());
1287 if (!mlirResults.empty())
1288 mlirOperationStateAddResults(&state, mlirResults.size(),
1289 mlirResults.data());
1290 if (!mlirAttributes.empty()) {
1291 // Note that the attribute names directly reference bytes in
1292 // mlirAttributes, so that vector must not be changed from here
1293 // on.
1294 llvm::SmallVector<MlirNamedAttribute, 4> mlirNamedAttributes;
1295 mlirNamedAttributes.reserve(mlirAttributes.size());
1296 for (auto &it : mlirAttributes)
1297 mlirNamedAttributes.push_back(mlirNamedAttributeGet(
1298 mlirIdentifierGet(mlirAttributeGetContext(it.second),
1299 toMlirStringRef(it.first)),
1300 it.second));
1301 mlirOperationStateAddAttributes(&state, mlirNamedAttributes.size(),
1302 mlirNamedAttributes.data());
1303 }
1304 if (!mlirSuccessors.empty())
1305 mlirOperationStateAddSuccessors(&state, mlirSuccessors.size(),
1306 mlirSuccessors.data());
1307 if (regions) {
1308 llvm::SmallVector<MlirRegion, 4> mlirRegions;
1309 mlirRegions.resize(regions);
1310 for (int i = 0; i < regions; ++i)
1311 mlirRegions[i] = mlirRegionCreate();
1312 mlirOperationStateAddOwnedRegions(&state, mlirRegions.size(),
1313 mlirRegions.data());
1314 }
1315
1316 // Construct the operation.
1317 MlirOperation operation = mlirOperationCreate(&state);
1318 PyOperationRef created =
1319 PyOperation::createDetached(location->getContext(), operation);
1320 maybeInsertOperation(created, maybeIp);
1321
1322 return created->createOpView();
1323 }
1324
1325 py::object PyOperation::clone(const py::object &maybeIp) {
1326 MlirOperation clonedOperation = mlirOperationClone(operation);
1327 PyOperationRef cloned =
1328 PyOperation::createDetached(getContext(), clonedOperation);
1329 maybeInsertOperation(cloned, maybeIp);
1330
1331 return cloned->createOpView();
1332 }
1333
1334 py::object PyOperation::createOpView() {
1335 checkValid();
1336 MlirIdentifier ident = mlirOperationGetName(get());
1337 MlirStringRef identStr = mlirIdentifierStr(ident);
1338 auto opViewClass = PyGlobals::get().lookupRawOpViewClass(
1339 StringRef(identStr.data, identStr.length));
1340 if (opViewClass)
1341 return (*opViewClass)(getRef().getObject());
1342 return py::cast(PyOpView(getRef().getObject()));
1343 }
1344
1345 void PyOperation::erase() {
1346 checkValid();
1347 // TODO: Fix memory hazards when erasing a tree of operations for which a deep
1348 // Python reference to a child operation is live. All children should also
1349 // have their `valid` bit set to false.
1350 auto &liveOperations = getContext()->liveOperations;
1351 if (liveOperations.count(operation.ptr))
1352 liveOperations.erase(operation.ptr);
1353 mlirOperationDestroy(operation);
1354 valid = false;
1355 }
1356
1357 //------------------------------------------------------------------------------
1358 // PyOpView
1359 //------------------------------------------------------------------------------
1360
1361 py::object
1362 PyOpView::buildGeneric(const py::object &cls, py::list resultTypeList,
1363 py::list operandList, std::optional<py::dict> attributes,
1364 std::optional<std::vector<PyBlock *>> successors,
1365 std::optional<int> regions,
1366 DefaultingPyLocation location,
1367 const py::object &maybeIp) {
1368 PyMlirContextRef context = location->getContext();
1369 // Class level operation construction metadata.
1370 std::string name = py::cast<std::string>(cls.attr("OPERATION_NAME"));
1371 // Operand and result segment specs are either none, which does no
1372 // variadic unpacking, or a list of ints with segment sizes, where each
1373 // element is either a positive number (typically 1 for a scalar) or -1 to
1374 // indicate that it is derived from the length of the same-indexed operand
1375 // or result (implying that it is a list at that position).
1376 py::object operandSegmentSpecObj = cls.attr("_ODS_OPERAND_SEGMENTS");
1377 py::object resultSegmentSpecObj = cls.attr("_ODS_RESULT_SEGMENTS");
1378
1379 std::vector<int32_t> operandSegmentLengths;
1380 std::vector<int32_t> resultSegmentLengths;
1381
1382 // Validate/determine region count.
1383 auto opRegionSpec = py::cast<std::tuple<int, bool>>(cls.attr("_ODS_REGIONS"));
1384 int opMinRegionCount = std::get<0>(opRegionSpec);
1385 bool opHasNoVariadicRegions = std::get<1>(opRegionSpec);
1386 if (!regions) {
1387 regions = opMinRegionCount;
1388 }
1389 if (*regions < opMinRegionCount) {
1390 throw py::value_error(
1391 (llvm::Twine("Operation \"") + name + "\" requires a minimum of " +
1392 llvm::Twine(opMinRegionCount) +
1393 " regions but was built with regions=" + llvm::Twine(*regions))
1394 .str());
1395 }
1396 if (opHasNoVariadicRegions && *regions > opMinRegionCount) {
1397 throw py::value_error(
1398 (llvm::Twine("Operation \"") + name + "\" requires a maximum of " +
1399 llvm::Twine(opMinRegionCount) +
1400 " regions but was built with regions=" + llvm::Twine(*regions))
1401 .str());
1402 }
1403
1404 // Unpack results.
1405 std::vector<PyType *> resultTypes;
1406 resultTypes.reserve(resultTypeList.size());
1407 if (resultSegmentSpecObj.is_none()) {
1408 // Non-variadic result unpacking.
1409 for (const auto &it : llvm::enumerate(resultTypeList)) {
1410 try {
1411 resultTypes.push_back(py::cast<PyType *>(it.value()));
1412 if (!resultTypes.back())
1413 throw py::cast_error();
1414 } catch (py::cast_error &err) {
1415 throw py::value_error((llvm::Twine("Result ") +
1416 llvm::Twine(it.index()) + " of operation \"" +
1417 name + "\" must be a Type (" + err.what() + ")")
1418 .str());
1419 }
1420 }
1421 } else {
1422 // Sized result unpacking.
1423 auto resultSegmentSpec = py::cast<std::vector<int>>(resultSegmentSpecObj);
1424 if (resultSegmentSpec.size() != resultTypeList.size()) {
1425 throw py::value_error((llvm::Twine("Operation \"") + name +
1426 "\" requires " +
1427 llvm::Twine(resultSegmentSpec.size()) +
1428 " result segments but was provided " +
1429 llvm::Twine(resultTypeList.size()))
1430 .str());
1431 }
1432 resultSegmentLengths.reserve(resultTypeList.size());
1433 for (const auto &it :
1434 llvm::enumerate(llvm::zip(resultTypeList, resultSegmentSpec))) {
1435 int segmentSpec = std::get<1>(it.value());
1436 if (segmentSpec == 1 || segmentSpec == 0) {
1437 // Unpack unary element.
1438 try {
1439 auto *resultType = py::cast<PyType *>(std::get<0>(it.value()));
1440 if (resultType) {
1441 resultTypes.push_back(resultType);
1442 resultSegmentLengths.push_back(1);
1443 } else if (segmentSpec == 0) {
1444 // Allowed to be optional.
1445 resultSegmentLengths.push_back(0);
1446 } else {
1447 throw py::cast_error("was None and result is not optional");
1448 }
1449 } catch (py::cast_error &err) {
1450 throw py::value_error((llvm::Twine("Result ") +
1451 llvm::Twine(it.index()) + " of operation \"" +
1452 name + "\" must be a Type (" + err.what() +
1453 ")")
1454 .str());
1455 }
1456 } else if (segmentSpec == -1) {
1457 // Unpack sequence by appending.
1458 try {
1459 if (std::get<0>(it.value()).is_none()) {
1460 // Treat it as an empty list.
1461 resultSegmentLengths.push_back(0);
1462 } else {
1463 // Unpack the list.
1464 auto segment = py::cast<py::sequence>(std::get<0>(it.value()));
1465 for (py::object segmentItem : segment) {
1466 resultTypes.push_back(py::cast<PyType *>(segmentItem));
1467 if (!resultTypes.back()) {
1468 throw py::cast_error("contained a None item");
1469 }
1470 }
1471 resultSegmentLengths.push_back(segment.size());
1472 }
1473 } catch (std::exception &err) {
1474 // NOTE: Sloppy to be using a catch-all here, but there are at least
1475 // three different unrelated exceptions that can be thrown in the
1476 // above "casts". Just keep the scope above small and catch them all.
1477 throw py::value_error((llvm::Twine("Result ") +
1478 llvm::Twine(it.index()) + " of operation \"" +
1479 name + "\" must be a Sequence of Types (" +
1480 err.what() + ")")
1481 .str());
1482 }
1483 } else {
1484 throw py::value_error("Unexpected segment spec");
1485 }
1486 }
1487 }
1488
1489 // Unpack operands.
1490 std::vector<PyValue *> operands;
1491 operands.reserve(operands.size());
1492 if (operandSegmentSpecObj.is_none()) {
1493 // Non-sized operand unpacking.
1494 for (const auto &it : llvm::enumerate(operandList)) {
1495 try {
1496 operands.push_back(py::cast<PyValue *>(it.value()));
1497 if (!operands.back())
1498 throw py::cast_error();
1499 } catch (py::cast_error &err) {
1500 throw py::value_error((llvm::Twine("Operand ") +
1501 llvm::Twine(it.index()) + " of operation \"" +
1502 name + "\" must be a Value (" + err.what() + ")")
1503 .str());
1504 }
1505 }
1506 } else {
1507 // Sized operand unpacking.
1508 auto operandSegmentSpec = py::cast<std::vector<int>>(operandSegmentSpecObj);
1509 if (operandSegmentSpec.size() != operandList.size()) {
1510 throw py::value_error((llvm::Twine("Operation \"") + name +
1511 "\" requires " +
1512 llvm::Twine(operandSegmentSpec.size()) +
1513 "operand segments but was provided " +
1514 llvm::Twine(operandList.size()))
1515 .str());
1516 }
1517 operandSegmentLengths.reserve(operandList.size());
1518 for (const auto &it :
1519 llvm::enumerate(llvm::zip(operandList, operandSegmentSpec))) {
1520 int segmentSpec = std::get<1>(it.value());
1521 if (segmentSpec == 1 || segmentSpec == 0) {
1522 // Unpack unary element.
1523 try {
1524 auto *operandValue = py::cast<PyValue *>(std::get<0>(it.value()));
1525 if (operandValue) {
1526 operands.push_back(operandValue);
1527 operandSegmentLengths.push_back(1);
1528 } else if (segmentSpec == 0) {
1529 // Allowed to be optional.
1530 operandSegmentLengths.push_back(0);
1531 } else {
1532 throw py::cast_error("was None and operand is not optional");
1533 }
1534 } catch (py::cast_error &err) {
1535 throw py::value_error((llvm::Twine("Operand ") +
1536 llvm::Twine(it.index()) + " of operation \"" +
1537 name + "\" must be a Value (" + err.what() +
1538 ")")
1539 .str());
1540 }
1541 } else if (segmentSpec == -1) {
1542 // Unpack sequence by appending.
1543 try {
1544 if (std::get<0>(it.value()).is_none()) {
1545 // Treat it as an empty list.
1546 operandSegmentLengths.push_back(0);
1547 } else {
1548 // Unpack the list.
1549 auto segment = py::cast<py::sequence>(std::get<0>(it.value()));
1550 for (py::object segmentItem : segment) {
1551 operands.push_back(py::cast<PyValue *>(segmentItem));
1552 if (!operands.back()) {
1553 throw py::cast_error("contained a None item");
1554 }
1555 }
1556 operandSegmentLengths.push_back(segment.size());
1557 }
1558 } catch (std::exception &err) {
1559 // NOTE: Sloppy to be using a catch-all here, but there are at least
1560 // three different unrelated exceptions that can be thrown in the
1561 // above "casts". Just keep the scope above small and catch them all.
1562 throw py::value_error((llvm::Twine("Operand ") +
1563 llvm::Twine(it.index()) + " of operation \"" +
1564 name + "\" must be a Sequence of Values (" +
1565 err.what() + ")")
1566 .str());
1567 }
1568 } else {
1569 throw py::value_error("Unexpected segment spec");
1570 }
1571 }
1572 }
1573
1574 // Merge operand/result segment lengths into attributes if needed.
1575 if (!operandSegmentLengths.empty() || !resultSegmentLengths.empty()) {
1576 // Dup.
1577 if (attributes) {
1578 attributes = py::dict(*attributes);
1579 } else {
1580 attributes = py::dict();
1581 }
1582 if (attributes->contains("result_segment_sizes") ||
1583 attributes->contains("operand_segment_sizes")) {
1584 throw py::value_error("Manually setting a 'result_segment_sizes' or "
1585 "'operand_segment_sizes' attribute is unsupported. "
1586 "Use Operation.create for such low-level access.");
1587 }
1588
1589 // Add result_segment_sizes attribute.
1590 if (!resultSegmentLengths.empty()) {
1591 MlirAttribute segmentLengthAttr =
1592 mlirDenseI32ArrayGet(context->get(), resultSegmentLengths.size(),
1593 resultSegmentLengths.data());
1594 (*attributes)["result_segment_sizes"] =
1595 PyAttribute(context, segmentLengthAttr);
1596 }
1597
1598 // Add operand_segment_sizes attribute.
1599 if (!operandSegmentLengths.empty()) {
1600 MlirAttribute segmentLengthAttr =
1601 mlirDenseI32ArrayGet(context->get(), operandSegmentLengths.size(),
1602 operandSegmentLengths.data());
1603 (*attributes)["operand_segment_sizes"] =
1604 PyAttribute(context, segmentLengthAttr);
1605 }
1606 }
1607
1608 // Delegate to create.
1609 return PyOperation::create(name,
1610 /*results=*/std::move(resultTypes),
1611 /*operands=*/std::move(operands),
1612 /*attributes=*/std::move(attributes),
1613 /*successors=*/std::move(successors),
1614 /*regions=*/*regions, location, maybeIp);
1615 }
1616
1617 PyOpView::PyOpView(const py::object &operationObject)
1618 // Casting through the PyOperationBase base-class and then back to the
1619 // Operation lets us accept any PyOperationBase subclass.
1620 : operation(py::cast<PyOperationBase &>(operationObject).getOperation()),
1621 operationObject(operation.getRef().getObject()) {}
1622
1623 py::object PyOpView::createRawSubclass(const py::object &userClass) {
1624 // This is... a little gross. The typical pattern is to have a pure python
1625 // class that extends OpView like:
1626 // class AddFOp(_cext.ir.OpView):
1627 // def __init__(self, loc, lhs, rhs):
1628 // operation = loc.context.create_operation(
1629 // "addf", lhs, rhs, results=[lhs.type])
1630 // super().__init__(operation)
1631 //
1632 // I.e. The goal of the user facing type is to provide a nice constructor
1633 // that has complete freedom for the op under construction. This is at odds
1634 // with our other desire to sometimes create this object by just passing an
1635 // operation (to initialize the base class). We could do *arg and **kwargs
1636 // munging to try to make it work, but instead, we synthesize a new class
1637 // on the fly which extends this user class (AddFOp in this example) and
1638 // *give it* the base class's __init__ method, thus bypassing the
1639 // intermediate subclass's __init__ method entirely. While slightly,
1640 // underhanded, this is safe/legal because the type hierarchy has not changed
1641 // (we just added a new leaf) and we aren't mucking around with __new__.
1642 // Typically, this new class will be stored on the original as "_Raw" and will
1643 // be used for casts and other things that need a variant of the class that
1644 // is initialized purely from an operation.
1645 py::object parentMetaclass =
1646 py::reinterpret_borrow<py::object>((PyObject *)&PyType_Type);
1647 py::dict attributes;
1648 // TODO: pybind11 2.6 supports a more direct form. Upgrade many years from
1649 // now.
1650 // auto opViewType = py::type::of<PyOpView>();
1651 auto opViewType = py::detail::get_type_handle(typeid(PyOpView), true);
1652 attributes["__init__"] = opViewType.attr("__init__");
1653 py::str origName = userClass.attr("__name__");
1654 py::str newName = py::str("_") + origName;
1655 return parentMetaclass(newName, py::make_tuple(userClass), attributes);
1656 }
1657
1658 //------------------------------------------------------------------------------
1659 // PyInsertionPoint.
1660 //------------------------------------------------------------------------------
1661
1662 PyInsertionPoint::PyInsertionPoint(PyBlock &block) : block(block) {}
1663
1664 PyInsertionPoint::PyInsertionPoint(PyOperationBase &beforeOperationBase)
1665 : refOperation(beforeOperationBase.getOperation().getRef()),
1666 block((*refOperation)->getBlock()) {}
1667
1668 void PyInsertionPoint::insert(PyOperationBase &operationBase) {
1669 PyOperation &operation = operationBase.getOperation();
1670 if (operation.isAttached())
1671 throw SetPyError(PyExc_ValueError,
1672 "Attempt to insert operation that is already attached");
1673 block.getParentOperation()->checkValid();
1674 MlirOperation beforeOp = {nullptr};
1675 if (refOperation) {
1676 // Insert before operation.
1677 (*refOperation)->checkValid();
1678 beforeOp = (*refOperation)->get();
1679 } else {
1680 // Insert at end (before null) is only valid if the block does not
1681 // already end in a known terminator (violating this will cause assertion
1682 // failures later).
1683 if (!mlirOperationIsNull(mlirBlockGetTerminator(block.get()))) {
1684 throw py::index_error("Cannot insert operation at the end of a block "
1685 "that already has a terminator. Did you mean to "
1686 "use 'InsertionPoint.at_block_terminator(block)' "
1687 "versus 'InsertionPoint(block)'?");
1688 }
1689 }
1690 mlirBlockInsertOwnedOperationBefore(block.get(), beforeOp, operation);
1691 operation.setAttached();
1692 }
1693
1694 PyInsertionPoint PyInsertionPoint::atBlockBegin(PyBlock &block) {
1695 MlirOperation firstOp = mlirBlockGetFirstOperation(block.get());
1696 if (mlirOperationIsNull(firstOp)) {
1697 // Just insert at end.
1698 return PyInsertionPoint(block);
1699 }
1700
1701 // Insert before first op.
1702 PyOperationRef firstOpRef = PyOperation::forOperation(
1703 block.getParentOperation()->getContext(), firstOp);
1704 return PyInsertionPoint{block, std::move(firstOpRef)};
1705 }
1706
1707 PyInsertionPoint PyInsertionPoint::atBlockTerminator(PyBlock &block) {
1708 MlirOperation terminator = mlirBlockGetTerminator(block.get());
1709 if (mlirOperationIsNull(terminator))
1710 throw SetPyError(PyExc_ValueError, "Block has no terminator");
1711 PyOperationRef terminatorOpRef = PyOperation::forOperation(
1712 block.getParentOperation()->getContext(), terminator);
1713 return PyInsertionPoint{block, std::move(terminatorOpRef)};
1714 }
1715
1716 py::object PyInsertionPoint::contextEnter() {
1717 return PyThreadContextEntry::pushInsertionPoint(*this);
1718 }
1719
1720 void PyInsertionPoint::contextExit(const pybind11::object &excType,
1721 const pybind11::object &excVal,
1722 const pybind11::object &excTb) {
1723 PyThreadContextEntry::popInsertionPoint(*this);
1724 }
1725
1726 //------------------------------------------------------------------------------
1727 // PyAttribute.
1728 //------------------------------------------------------------------------------
1729
1730 bool PyAttribute::operator==(const PyAttribute &other) {
1731 return mlirAttributeEqual(attr, other.attr);
1732 }
1733
1734 py::object PyAttribute::getCapsule() {
1735 return py::reinterpret_steal<py::object>(mlirPythonAttributeToCapsule(*this));
1736 }
1737
1738 PyAttribute PyAttribute::createFromCapsule(py::object capsule) {
1739 MlirAttribute rawAttr = mlirPythonCapsuleToAttribute(capsule.ptr());
1740 if (mlirAttributeIsNull(rawAttr))
1741 throw py::error_already_set();
1742 return PyAttribute(
1743 PyMlirContext::forContext(mlirAttributeGetContext(rawAttr)), rawAttr);
1744 }
1745
1746 //------------------------------------------------------------------------------
1747 // PyNamedAttribute.
1748 //------------------------------------------------------------------------------
1749
1750 PyNamedAttribute::PyNamedAttribute(MlirAttribute attr, std::string ownedName)
1751 : ownedName(new std::string(std::move(ownedName))) {
1752 namedAttr = mlirNamedAttributeGet(
1753 mlirIdentifierGet(mlirAttributeGetContext(attr),
1754 toMlirStringRef(*this->ownedName)),
1755 attr);
1756 }
1757
1758 //------------------------------------------------------------------------------
1759 // PyType.
1760 //------------------------------------------------------------------------------
1761
1762 bool PyType::operator==(const PyType &other) {
1763 return mlirTypeEqual(type, other.type);
1764 }
1765
1766 py::object PyType::getCapsule() {
1767 return py::reinterpret_steal<py::object>(mlirPythonTypeToCapsule(*this));
1768 }
1769
1770 PyType PyType::createFromCapsule(py::object capsule) {
1771 MlirType rawType = mlirPythonCapsuleToType(capsule.ptr());
1772 if (mlirTypeIsNull(rawType))
1773 throw py::error_already_set();
1774 return PyType(PyMlirContext::forContext(mlirTypeGetContext(rawType)),
1775 rawType);
1776 }
1777
1778 //------------------------------------------------------------------------------
1779 // PyValue and subclases.
1780 //------------------------------------------------------------------------------
1781
1782 pybind11::object PyValue::getCapsule() {
1783 return py::reinterpret_steal<py::object>(mlirPythonValueToCapsule(get()));
1784 }
1785
1786 PyValue PyValue::createFromCapsule(pybind11::object capsule) {
1787 MlirValue value = mlirPythonCapsuleToValue(capsule.ptr());
1788 if (mlirValueIsNull(value))
1789 throw py::error_already_set();
1790 MlirOperation owner;
1791 if (mlirValueIsAOpResult(value))
1792 owner = mlirOpResultGetOwner(value);
1793 if (mlirValueIsABlockArgument(value))
1794 owner = mlirBlockGetParentOperation(mlirBlockArgumentGetOwner(value));
1795 if (mlirOperationIsNull(owner))
1796 throw py::error_already_set();
1797 MlirContext ctx = mlirOperationGetContext(owner);
1798 PyOperationRef ownerRef =
1799 PyOperation::forOperation(PyMlirContext::forContext(ctx), owner);
1800 return PyValue(ownerRef, value);
1801 }
1802
1803 //------------------------------------------------------------------------------
1804 // PySymbolTable.
1805 //------------------------------------------------------------------------------
1806
1807 PySymbolTable::PySymbolTable(PyOperationBase &operation)
1808 : operation(operation.getOperation().getRef()) {
1809 symbolTable = mlirSymbolTableCreate(operation.getOperation().get());
1810 if (mlirSymbolTableIsNull(symbolTable)) {
1811 throw py::cast_error("Operation is not a Symbol Table.");
1812 }
1813 }
1814
1815 py::object PySymbolTable::dunderGetItem(const std::string &name) {
1816 operation->checkValid();
1817 MlirOperation symbol = mlirSymbolTableLookup(
1818 symbolTable, mlirStringRefCreate(name.data(), name.length()));
1819 if (mlirOperationIsNull(symbol))
1820 throw py::key_error("Symbol '" + name + "' not in the symbol table.");
1821
1822 return PyOperation::forOperation(operation->getContext(), symbol,
1823 operation.getObject())
1824 ->createOpView();
1825 }
1826
1827 void PySymbolTable::erase(PyOperationBase &symbol) {
1828 operation->checkValid();
1829 symbol.getOperation().checkValid();
1830 mlirSymbolTableErase(symbolTable, symbol.getOperation().get());
1831 // The operation is also erased, so we must invalidate it. There may be Python
1832 // references to this operation so we don't want to delete it from the list of
1833 // live operations here.
1834 symbol.getOperation().valid = false;
1835 }
1836
1837 void PySymbolTable::dunderDel(const std::string &name) {
1838 py::object operation = dunderGetItem(name);
1839 erase(py::cast<PyOperationBase &>(operation));
1840 }
1841
1842 PyAttribute PySymbolTable::insert(PyOperationBase &symbol) {
1843 operation->checkValid();
1844 symbol.getOperation().checkValid();
1845 MlirAttribute symbolAttr = mlirOperationGetAttributeByName(
1846 symbol.getOperation().get(), mlirSymbolTableGetSymbolAttributeName());
1847 if (mlirAttributeIsNull(symbolAttr))
1848 throw py::value_error("Expected operation to have a symbol name.");
1849 return PyAttribute(
1850 symbol.getOperation().getContext(),
1851 mlirSymbolTableInsert(symbolTable, symbol.getOperation().get()));
1852 }
1853
1854 PyAttribute PySymbolTable::getSymbolName(PyOperationBase &symbol) {
1855 // Op must already be a symbol.
1856 PyOperation &operation = symbol.getOperation();
1857 operation.checkValid();
1858 MlirStringRef attrName = mlirSymbolTableGetSymbolAttributeName();
1859 MlirAttribute existingNameAttr =
1860 mlirOperationGetAttributeByName(operation.get(), attrName);
1861 if (mlirAttributeIsNull(existingNameAttr))
1862 throw py::value_error("Expected operation to have a symbol name.");
1863 return PyAttribute(symbol.getOperation().getContext(), existingNameAttr);
1864 }
1865
1866 void PySymbolTable::setSymbolName(PyOperationBase &symbol,
1867 const std::string &name) {
1868 // Op must already be a symbol.
1869 PyOperation &operation = symbol.getOperation();
1870 operation.checkValid();
1871 MlirStringRef attrName = mlirSymbolTableGetSymbolAttributeName();
1872 MlirAttribute existingNameAttr =
1873 mlirOperationGetAttributeByName(operation.get(), attrName);
1874 if (mlirAttributeIsNull(existingNameAttr))
1875 throw py::value_error("Expected operation to have a symbol name.");
1876 MlirAttribute newNameAttr =
1877 mlirStringAttrGet(operation.getContext()->get(), toMlirStringRef(name));
1878 mlirOperationSetAttributeByName(operation.get(), attrName, newNameAttr);
1879 }
1880
1881 PyAttribute PySymbolTable::getVisibility(PyOperationBase &symbol) {
1882 PyOperation &operation = symbol.getOperation();
1883 operation.checkValid();
1884 MlirStringRef attrName = mlirSymbolTableGetVisibilityAttributeName();
1885 MlirAttribute existingVisAttr =
1886 mlirOperationGetAttributeByName(operation.get(), attrName);
1887 if (mlirAttributeIsNull(existingVisAttr))
1888 throw py::value_error("Expected operation to have a symbol visibility.");
1889 return PyAttribute(symbol.getOperation().getContext(), existingVisAttr);
1890 }
1891
1892 void PySymbolTable::setVisibility(PyOperationBase &symbol,
1893 const std::string &visibility) {
1894 if (visibility != "public" && visibility != "private" &&
1895 visibility != "nested")
1896 throw py::value_error(
1897 "Expected visibility to be 'public', 'private' or 'nested'");
1898 PyOperation &operation = symbol.getOperation();
1899 operation.checkValid();
1900 MlirStringRef attrName = mlirSymbolTableGetVisibilityAttributeName();
1901 MlirAttribute existingVisAttr =
1902 mlirOperationGetAttributeByName(operation.get(), attrName);
1903 if (mlirAttributeIsNull(existingVisAttr))
1904 throw py::value_error("Expected operation to have a symbol visibility.");
1905 MlirAttribute newVisAttr = mlirStringAttrGet(operation.getContext()->get(),
1906 toMlirStringRef(visibility));
1907 mlirOperationSetAttributeByName(operation.get(), attrName, newVisAttr);
1908 }
1909
1910 void PySymbolTable::replaceAllSymbolUses(const std::string &oldSymbol,
1911 const std::string &newSymbol,
1912 PyOperationBase &from) {
1913 PyOperation &fromOperation = from.getOperation();
1914 fromOperation.checkValid();
1915 if (mlirLogicalResultIsFailure(mlirSymbolTableReplaceAllSymbolUses(
1916 toMlirStringRef(oldSymbol), toMlirStringRef(newSymbol),
1917 from.getOperation())))
1918
1919 throw py::value_error("Symbol rename failed");
1920 }
1921
1922 void PySymbolTable::walkSymbolTables(PyOperationBase &from,
1923 bool allSymUsesVisible,
1924 py::object callback) {
1925 PyOperation &fromOperation = from.getOperation();
1926 fromOperation.checkValid();
1927 struct UserData {
1928 PyMlirContextRef context;
1929 py::object callback;
1930 bool gotException;
1931 std::string exceptionWhat;
1932 py::object exceptionType;
1933 };
1934 UserData userData{
1935 fromOperation.getContext(), std::move(callback), false, {}, {}};
1936 mlirSymbolTableWalkSymbolTables(
1937 fromOperation.get(), allSymUsesVisible,
1938 [](MlirOperation foundOp, bool isVisible, void *calleeUserDataVoid) {
1939 UserData *calleeUserData = static_cast<UserData *>(calleeUserDataVoid);
1940 auto pyFoundOp =
1941 PyOperation::forOperation(calleeUserData->context, foundOp);
1942 if (calleeUserData->gotException)
1943 return;
1944 try {
1945 calleeUserData->callback(pyFoundOp.getObject(), isVisible);
1946 } catch (py::error_already_set &e) {
1947 calleeUserData->gotException = true;
1948 calleeUserData->exceptionWhat = e.what();
1949 calleeUserData->exceptionType = e.type();
1950 }
1951 },
1952 static_cast<void *>(&userData));
1953 if (userData.gotException) {
1954 std::string message("Exception raised in callback: ");
1955 message.append(userData.exceptionWhat);
1956 throw std::runtime_error(message);
1957 }
1958 }
1959
1960 namespace {
1961 /// CRTP base class for Python MLIR values that subclass Value and should be
1962 /// castable from it. The value hierarchy is one level deep and is not supposed
1963 /// to accommodate other levels unless core MLIR changes.
1964 template <typename DerivedTy>
1965 class PyConcreteValue : public PyValue {
1966 public:
1967 // Derived classes must define statics for:
1968 // IsAFunctionTy isaFunction
1969 // const char *pyClassName
1970 // and redefine bindDerived.
1971 using ClassTy = py::class_<DerivedTy, PyValue>;
1972 using IsAFunctionTy = bool (*)(MlirValue);
1973
1974 PyConcreteValue() = default;
1975 PyConcreteValue(PyOperationRef operationRef, MlirValue value)
1976 : PyValue(operationRef, value) {}
1977 PyConcreteValue(PyValue &orig)
1978 : PyConcreteValue(orig.getParentOperation(), castFrom(orig)) {}
1979
1980 /// Attempts to cast the original value to the derived type and throws on
1981 /// type mismatches.
1982 static MlirValue castFrom(PyValue &orig) {
1983 if (!DerivedTy::isaFunction(orig.get())) {
1984 auto origRepr = py::repr(py::cast(orig)).cast<std::string>();
1985 throw SetPyError(PyExc_ValueError, Twine("Cannot cast value to ") +
1986 DerivedTy::pyClassName +
1987 " (from " + origRepr + ")");
1988 }
1989 return orig.get();
1990 }
1991
1992 /// Binds the Python module objects to functions of this class.
1993 static void bind(py::module &m) {
1994 auto cls = ClassTy(m, DerivedTy::pyClassName, py::module_local());
1995 cls.def(py::init<PyValue &>(), py::keep_alive<0, 1>(), py::arg("value"));
1996 cls.def_static(
1997 "isinstance",
1998 [](PyValue &otherValue) -> bool {
1999 return DerivedTy::isaFunction(otherValue);
2000 },
2001 py::arg("other_value"));
2002 DerivedTy::bindDerived(cls);
2003 }
2004
2005 /// Implemented by derived classes to add methods to the Python subclass.
2006 static void bindDerived(ClassTy &m) {}
2007 };
2008
2009 /// Python wrapper for MlirBlockArgument.
2010 class PyBlockArgument : public PyConcreteValue<PyBlockArgument> {
2011 public:
2012 static constexpr IsAFunctionTy isaFunction = mlirValueIsABlockArgument;
2013 static constexpr const char *pyClassName = "BlockArgument";
2014 using PyConcreteValue::PyConcreteValue;
2015
2016 static void bindDerived(ClassTy &c) {
2017 c.def_property_readonly("owner", [](PyBlockArgument &self) {
2018 return PyBlock(self.getParentOperation(),
2019 mlirBlockArgumentGetOwner(self.get()));
2020 });
2021 c.def_property_readonly("arg_number", [](PyBlockArgument &self) {
2022 return mlirBlockArgumentGetArgNumber(self.get());
2023 });
2024 c.def(
2025 "set_type",
2026 [](PyBlockArgument &self, PyType type) {
2027 return mlirBlockArgumentSetType(self.get(), type);
2028 },
2029 py::arg("type"));
2030 }
2031 };
2032
2033 /// Python wrapper for MlirOpResult.
2034 class PyOpResult : public PyConcreteValue<PyOpResult> {
2035 public:
2036 static constexpr IsAFunctionTy isaFunction = mlirValueIsAOpResult;
2037 static constexpr const char *pyClassName = "OpResult";
2038 using PyConcreteValue::PyConcreteValue;
2039
2040 static void bindDerived(ClassTy &c) {
2041 c.def_property_readonly("owner", [](PyOpResult &self) {
2042 assert(
2043 mlirOperationEqual(self.getParentOperation()->get(),
2044 mlirOpResultGetOwner(self.get())) &&
2045 "expected the owner of the value in Python to match that in the IR");
2046 return self.getParentOperation().getObject();
2047 });
2048 c.def_property_readonly("result_number", [](PyOpResult &self) {
2049 return mlirOpResultGetResultNumber(self.get());
2050 });
2051 }
2052 };
2053
2054 /// Returns the list of types of the values held by container.
2055 template <typename Container>
2056 static std::vector<PyType> getValueTypes(Container &container,
2057 PyMlirContextRef &context) {
2058 std::vector<PyType> result;
2059 result.reserve(container.size());
2060 for (int i = 0, e = container.size(); i < e; ++i) {
2061 result.push_back(
2062 PyType(context, mlirValueGetType(container.getElement(i).get())));
2063 }
2064 return result;
2065 }
2066
2067 /// A list of block arguments. Internally, these are stored as consecutive
2068 /// elements, random access is cheap. The argument list is associated with the
2069 /// operation that contains the block (detached blocks are not allowed in
2070 /// Python bindings) and extends its lifetime.
2071 class PyBlockArgumentList
2072 : public Sliceable<PyBlockArgumentList, PyBlockArgument> {
2073 public:
2074 static constexpr const char *pyClassName = "BlockArgumentList";
2075
2076 PyBlockArgumentList(PyOperationRef operation, MlirBlock block,
2077 intptr_t startIndex = 0, intptr_t length = -1,
2078 intptr_t step = 1)
2079 : Sliceable(startIndex,
2080 length == -1 ? mlirBlockGetNumArguments(block) : length,
2081 step),
2082 operation(std::move(operation)), block(block) {}
2083
2084 static void bindDerived(ClassTy &c) {
2085 c.def_property_readonly("types", [](PyBlockArgumentList &self) {
2086 return getValueTypes(self, self.operation->getContext());
2087 });
2088 }
2089
2090 private:
2091 /// Give the parent CRTP class access to hook implementations below.
2092 friend class Sliceable<PyBlockArgumentList, PyBlockArgument>;
2093
2094 /// Returns the number of arguments in the list.
2095 intptr_t getRawNumElements() {
2096 operation->checkValid();
2097 return mlirBlockGetNumArguments(block);
2098 }
2099
2100 /// Returns `pos`-the element in the list.
2101 PyBlockArgument getRawElement(intptr_t pos) {
2102 MlirValue argument = mlirBlockGetArgument(block, pos);
2103 return PyBlockArgument(operation, argument);
2104 }
2105
2106 /// Returns a sublist of this list.
2107 PyBlockArgumentList slice(intptr_t startIndex, intptr_t length,
2108 intptr_t step) {
2109 return PyBlockArgumentList(operation, block, startIndex, length, step);
2110 }
2111
2112 PyOperationRef operation;
2113 MlirBlock block;
2114 };
2115
2116 /// A list of operation operands. Internally, these are stored as consecutive
2117 /// elements, random access is cheap. The result list is associated with the
2118 /// operation whose results these are, and extends the lifetime of this
2119 /// operation.
2120 class PyOpOperandList : public Sliceable<PyOpOperandList, PyValue> {
2121 public:
2122 static constexpr const char *pyClassName = "OpOperandList";
2123
2124 PyOpOperandList(PyOperationRef operation, intptr_t startIndex = 0,
2125 intptr_t length = -1, intptr_t step = 1)
2126 : Sliceable(startIndex,
2127 length == -1 ? mlirOperationGetNumOperands(operation->get())
2128 : length,
2129 step),
2130 operation(operation) {}
2131
2132 void dunderSetItem(intptr_t index, PyValue value) {
2133 index = wrapIndex(index);
2134 mlirOperationSetOperand(operation->get(), index, value.get());
2135 }
2136
2137 static void bindDerived(ClassTy &c) {
2138 c.def("__setitem__", &PyOpOperandList::dunderSetItem);
2139 }
2140
2141 private:
2142 /// Give the parent CRTP class access to hook implementations below.
2143 friend class Sliceable<PyOpOperandList, PyValue>;
2144
2145 intptr_t getRawNumElements() {
2146 operation->checkValid();
2147 return mlirOperationGetNumOperands(operation->get());
2148 }
2149
2150 PyValue getRawElement(intptr_t pos) {
2151 MlirValue operand = mlirOperationGetOperand(operation->get(), pos);
2152 MlirOperation owner;
2153 if (mlirValueIsAOpResult(operand))
2154 owner = mlirOpResultGetOwner(operand);
2155 else if (mlirValueIsABlockArgument(operand))
2156 owner = mlirBlockGetParentOperation(mlirBlockArgumentGetOwner(operand));
2157 else
2158 assert(false && "Value must be an block arg or op result.");
2159 PyOperationRef pyOwner =
2160 PyOperation::forOperation(operation->getContext(), owner);
2161 return PyValue(pyOwner, operand);
2162 }
2163
2164 PyOpOperandList slice(intptr_t startIndex, intptr_t length, intptr_t step) {
2165 return PyOpOperandList(operation, startIndex, length, step);
2166 }
2167
2168 PyOperationRef operation;
2169 };
2170
2171 /// A list of operation results. Internally, these are stored as consecutive
2172 /// elements, random access is cheap. The result list is associated with the
2173 /// operation whose results these are, and extends the lifetime of this
2174 /// operation.
2175 class PyOpResultList : public Sliceable<PyOpResultList, PyOpResult> {
2176 public:
2177 static constexpr const char *pyClassName = "OpResultList";
2178
2179 PyOpResultList(PyOperationRef operation, intptr_t startIndex = 0,
2180 intptr_t length = -1, intptr_t step = 1)
2181 : Sliceable(startIndex,
2182 length == -1 ? mlirOperationGetNumResults(operation->get())
2183 : length,
2184 step),
2185 operation(operation) {}
2186
2187 static void bindDerived(ClassTy &c) {
2188 c.def_property_readonly("types", [](PyOpResultList &self) {
2189 return getValueTypes(self, self.operation->getContext());
2190 });
2191 }
2192
2193 private:
2194 /// Give the parent CRTP class access to hook implementations below.
2195 friend class Sliceable<PyOpResultList, PyOpResult>;
2196
2197 intptr_t getRawNumElements() {
2198 operation->checkValid();
2199 return mlirOperationGetNumResults(operation->get());
2200 }
2201
2202 PyOpResult getRawElement(intptr_t index) {
2203 PyValue value(operation, mlirOperationGetResult(operation->get(), index));
2204 return PyOpResult(value);
2205 }
2206
2207 PyOpResultList slice(intptr_t startIndex, intptr_t length, intptr_t step) {
2208 return PyOpResultList(operation, startIndex, length, step);
2209 }
2210
2211 PyOperationRef operation;
2212 };
2213
2214 /// A list of operation attributes. Can be indexed by name, producing
2215 /// attributes, or by index, producing named attributes.
2216 class PyOpAttributeMap {
2217 public:
2218 PyOpAttributeMap(PyOperationRef operation)
2219 : operation(std::move(operation)) {}
2220
2221 PyAttribute dunderGetItemNamed(const std::string &name) {
2222 MlirAttribute attr = mlirOperationGetAttributeByName(operation->get(),
2223 toMlirStringRef(name));
2224 if (mlirAttributeIsNull(attr)) {
2225 throw SetPyError(PyExc_KeyError,
2226 "attempt to access a non-existent attribute");
2227 }
2228 return PyAttribute(operation->getContext(), attr);
2229 }
2230
2231 PyNamedAttribute dunderGetItemIndexed(intptr_t index) {
2232 if (index < 0 || index >= dunderLen()) {
2233 throw SetPyError(PyExc_IndexError,
2234 "attempt to access out of bounds attribute");
2235 }
2236 MlirNamedAttribute namedAttr =
2237 mlirOperationGetAttribute(operation->get(), index);
2238 return PyNamedAttribute(
2239 namedAttr.attribute,
2240 std::string(mlirIdentifierStr(namedAttr.name).data,
2241 mlirIdentifierStr(namedAttr.name).length));
2242 }
2243
2244 void dunderSetItem(const std::string &name, const PyAttribute &attr) {
2245 mlirOperationSetAttributeByName(operation->get(), toMlirStringRef(name),
2246 attr);
2247 }
2248
2249 void dunderDelItem(const std::string &name) {
2250 int removed = mlirOperationRemoveAttributeByName(operation->get(),
2251 toMlirStringRef(name));
2252 if (!removed)
2253 throw SetPyError(PyExc_KeyError,
2254 "attempt to delete a non-existent attribute");
2255 }
2256
2257 intptr_t dunderLen() {
2258 return mlirOperationGetNumAttributes(operation->get());
2259 }
2260
2261 bool dunderContains(const std::string &name) {
2262 return !mlirAttributeIsNull(mlirOperationGetAttributeByName(
2263 operation->get(), toMlirStringRef(name)));
2264 }
2265
2266 static void bind(py::module &m) {
2267 py::class_<PyOpAttributeMap>(m, "OpAttributeMap", py::module_local())
2268 .def("__contains__", &PyOpAttributeMap::dunderContains)
2269 .def("__len__", &PyOpAttributeMap::dunderLen)
2270 .def("__getitem__", &PyOpAttributeMap::dunderGetItemNamed)
2271 .def("__getitem__", &PyOpAttributeMap::dunderGetItemIndexed)
2272 .def("__setitem__", &PyOpAttributeMap::dunderSetItem)
2273 .def("__delitem__", &PyOpAttributeMap::dunderDelItem);
2274 }
2275
2276 private:
2277 PyOperationRef operation;
2278 };
2279
2280 } // namespace
2281
2282 //------------------------------------------------------------------------------
2283 // Populates the core exports of the 'ir' submodule.
2284 //------------------------------------------------------------------------------
2285
2286 void mlir::python::populateIRCore(py::module &m) {
2287 //----------------------------------------------------------------------------
2288 // Enums.
2289 //----------------------------------------------------------------------------
2290 py::enum_<MlirDiagnosticSeverity>(m, "DiagnosticSeverity", py::module_local())
2291 .value("ERROR", MlirDiagnosticError)
2292 .value("WARNING", MlirDiagnosticWarning)
2293 .value("NOTE", MlirDiagnosticNote)
2294 .value("REMARK", MlirDiagnosticRemark);
2295
2296 //----------------------------------------------------------------------------
2297 // Mapping of Diagnostics.
2298 //----------------------------------------------------------------------------
2299 py::class_<PyDiagnostic>(m, "Diagnostic", py::module_local())
2300 .def_property_readonly("severity", &PyDiagnostic::getSeverity)
2301 .def_property_readonly("location", &PyDiagnostic::getLocation)
2302 .def_property_readonly("message", &PyDiagnostic::getMessage)
2303 .def_property_readonly("notes", &PyDiagnostic::getNotes)
2304 .def("__str__", [](PyDiagnostic &self) -> py::str {
2305 if (!self.isValid())
2306 return "<Invalid Diagnostic>";
2307 return self.getMessage();
2308 });
2309
2310 py::class_<PyDiagnosticHandler>(m, "DiagnosticHandler", py::module_local())
2311 .def("detach", &PyDiagnosticHandler::detach)
2312 .def_property_readonly("attached", &PyDiagnosticHandler::isAttached)
2313 .def_property_readonly("had_error", &PyDiagnosticHandler::getHadError)
2314 .def("__enter__", &PyDiagnosticHandler::contextEnter)
2315 .def("__exit__", &PyDiagnosticHandler::contextExit);
2316
2317 //----------------------------------------------------------------------------
2318 // Mapping of MlirContext.
2319 // Note that this is exported as _BaseContext. The containing, Python level
2320 // __init__.py will subclass it with site-specific functionality and set a
2321 // "Context" attribute on this module.
2322 //----------------------------------------------------------------------------
2323 py::class_<PyMlirContext>(m, "_BaseContext", py::module_local())
2324 .def(py::init<>(&PyMlirContext::createNewContextForInit))
2325 .def_static("_get_live_count", &PyMlirContext::getLiveCount)
2326 .def("_get_context_again",
2327 [](PyMlirContext &self) {
2328 PyMlirContextRef ref = PyMlirContext::forContext(self.get());
2329 return ref.releaseObject();
2330 })
2331 .def("_get_live_operation_count", &PyMlirContext::getLiveOperationCount)
2332 .def("_clear_live_operations", &PyMlirContext::clearLiveOperations)
2333 .def("_get_live_module_count", &PyMlirContext::getLiveModuleCount)
2334 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
2335 &PyMlirContext::getCapsule)
2336 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyMlirContext::createFromCapsule)
2337 .def("__enter__", &PyMlirContext::contextEnter)
2338 .def("__exit__", &PyMlirContext::contextExit)
2339 .def_property_readonly_static(
2340 "current",
2341 [](py::object & /*class*/) {
2342 auto *context = PyThreadContextEntry::getDefaultContext();
2343 if (!context)
2344 throw SetPyError(PyExc_ValueError, "No current Context");
2345 return context;
2346 },
2347 "Gets the Context bound to the current thread or raises ValueError")
2348 .def_property_readonly(
2349 "dialects",
2350 [](PyMlirContext &self) { return PyDialects(self.getRef()); },
2351 "Gets a container for accessing dialects by name")
2352 .def_property_readonly(
2353 "d", [](PyMlirContext &self) { return PyDialects(self.getRef()); },
2354 "Alias for 'dialect'")
2355 .def(
2356 "get_dialect_descriptor",
2357 [=](PyMlirContext &self, std::string &name) {
2358 MlirDialect dialect = mlirContextGetOrLoadDialect(
2359 self.get(), {name.data(), name.size()});
2360 if (mlirDialectIsNull(dialect)) {
2361 throw SetPyError(PyExc_ValueError,
2362 Twine("Dialect '") + name + "' not found");
2363 }
2364 return PyDialectDescriptor(self.getRef(), dialect);
2365 },
2366 py::arg("dialect_name"),
2367 "Gets or loads a dialect by name, returning its descriptor object")
2368 .def_property(
2369 "allow_unregistered_dialects",
2370 [](PyMlirContext &self) -> bool {
2371 return mlirContextGetAllowUnregisteredDialects(self.get());
2372 },
2373 [](PyMlirContext &self, bool value) {
2374 mlirContextSetAllowUnregisteredDialects(self.get(), value);
2375 })
2376 .def("attach_diagnostic_handler", &PyMlirContext::attachDiagnosticHandler,
2377 py::arg("callback"),
2378 "Attaches a diagnostic handler that will receive callbacks")
2379 .def(
2380 "enable_multithreading",
2381 [](PyMlirContext &self, bool enable) {
2382 mlirContextEnableMultithreading(self.get(), enable);
2383 },
2384 py::arg("enable"))
2385 .def(
2386 "is_registered_operation",
2387 [](PyMlirContext &self, std::string &name) {
2388 return mlirContextIsRegisteredOperation(
2389 self.get(), MlirStringRef{name.data(), name.size()});
2390 },
2391 py::arg("operation_name"))
2392 .def(
2393 "append_dialect_registry",
2394 [](PyMlirContext &self, PyDialectRegistry &registry) {
2395 mlirContextAppendDialectRegistry(self.get(), registry);
2396 },
2397 py::arg("registry"))
2398 .def("load_all_available_dialects", [](PyMlirContext &self) {
2399 mlirContextLoadAllAvailableDialects(self.get());
2400 });
2401
2402 //----------------------------------------------------------------------------
2403 // Mapping of PyDialectDescriptor
2404 //----------------------------------------------------------------------------
2405 py::class_<PyDialectDescriptor>(m, "DialectDescriptor", py::module_local())
2406 .def_property_readonly("namespace",
2407 [](PyDialectDescriptor &self) {
2408 MlirStringRef ns =
2409 mlirDialectGetNamespace(self.get());
2410 return py::str(ns.data, ns.length);
2411 })
2412 .def("__repr__", [](PyDialectDescriptor &self) {
2413 MlirStringRef ns = mlirDialectGetNamespace(self.get());
2414 std::string repr("<DialectDescriptor ");
2415 repr.append(ns.data, ns.length);
2416 repr.append(">");
2417 return repr;
2418 });
2419
2420 //----------------------------------------------------------------------------
2421 // Mapping of PyDialects
2422 //----------------------------------------------------------------------------
2423 py::class_<PyDialects>(m, "Dialects", py::module_local())
2424 .def("__getitem__",
2425 [=](PyDialects &self, std::string keyName) {
2426 MlirDialect dialect =
2427 self.getDialectForKey(keyName, /*attrError=*/false);
2428 py::object descriptor =
2429 py::cast(PyDialectDescriptor{self.getContext(), dialect});
2430 return createCustomDialectWrapper(keyName, std::move(descriptor));
2431 })
2432 .def("__getattr__", [=](PyDialects &self, std::string attrName) {
2433 MlirDialect dialect =
2434 self.getDialectForKey(attrName, /*attrError=*/true);
2435 py::object descriptor =
2436 py::cast(PyDialectDescriptor{self.getContext(), dialect});
2437 return createCustomDialectWrapper(attrName, std::move(descriptor));
2438 });
2439
2440 //----------------------------------------------------------------------------
2441 // Mapping of PyDialect
2442 //----------------------------------------------------------------------------
2443 py::class_<PyDialect>(m, "Dialect", py::module_local())
2444 .def(py::init<py::object>(), py::arg("descriptor"))
2445 .def_property_readonly(
2446 "descriptor", [](PyDialect &self) { return self.getDescriptor(); })
2447 .def("__repr__", [](py::object self) {
2448 auto clazz = self.attr("__class__");
2449 return py::str("<Dialect ") +
2450 self.attr("descriptor").attr("namespace") + py::str(" (class ") +
2451 clazz.attr("__module__") + py::str(".") +
2452 clazz.attr("__name__") + py::str(")>");
2453 });
2454
2455 //----------------------------------------------------------------------------
2456 // Mapping of PyDialectRegistry
2457 //----------------------------------------------------------------------------
2458 py::class_<PyDialectRegistry>(m, "DialectRegistry", py::module_local())
2459 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
2460 &PyDialectRegistry::getCapsule)
2461 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyDialectRegistry::createFromCapsule)
2462 .def(py::init<>());
2463
2464 //----------------------------------------------------------------------------
2465 // Mapping of Location
2466 //----------------------------------------------------------------------------
2467 py::class_<PyLocation>(m, "Location", py::module_local())
2468 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyLocation::getCapsule)
2469 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyLocation::createFromCapsule)
2470 .def("__enter__", &PyLocation::contextEnter)
2471 .def("__exit__", &PyLocation::contextExit)
2472 .def("__eq__",
2473 [](PyLocation &self, PyLocation &other) -> bool {
2474 return mlirLocationEqual(self, other);
2475 })
2476 .def("__eq__", [](PyLocation &self, py::object other) { return false; })
2477 .def_property_readonly_static(
2478 "current",
2479 [](py::object & /*class*/) {
2480 auto *loc = PyThreadContextEntry::getDefaultLocation();
2481 if (!loc)
2482 throw SetPyError(PyExc_ValueError, "No current Location");
2483 return loc;
2484 },
2485 "Gets the Location bound to the current thread or raises ValueError")
2486 .def_static(
2487 "unknown",
2488 [](DefaultingPyMlirContext context) {
2489 return PyLocation(context->getRef(),
2490 mlirLocationUnknownGet(context->get()));
2491 },
2492 py::arg("context") = py::none(),
2493 "Gets a Location representing an unknown location")
2494 .def_static(
2495 "callsite",
2496 [](PyLocation callee, const std::vector<PyLocation> &frames,
2497 DefaultingPyMlirContext context) {
2498 if (frames.empty())
2499 throw py::value_error("No caller frames provided");
2500 MlirLocation caller = frames.back().get();
2501 for (const PyLocation &frame :
2502 llvm::reverse(llvm::ArrayRef(frames).drop_back()))
2503 caller = mlirLocationCallSiteGet(frame.get(), caller);
2504 return PyLocation(context->getRef(),
2505 mlirLocationCallSiteGet(callee.get(), caller));
2506 },
2507 py::arg("callee"), py::arg("frames"), py::arg("context") = py::none(),
2508 kContextGetCallSiteLocationDocstring)
2509 .def_static(
2510 "file",
2511 [](std::string filename, int line, int col,
2512 DefaultingPyMlirContext context) {
2513 return PyLocation(
2514 context->getRef(),
2515 mlirLocationFileLineColGet(
2516 context->get(), toMlirStringRef(filename), line, col));
2517 },
2518 py::arg("filename"), py::arg("line"), py::arg("col"),
2519 py::arg("context") = py::none(), kContextGetFileLocationDocstring)
2520 .def_static(
2521 "fused",
2522 [](const std::vector<PyLocation> &pyLocations,
2523 std::optional<PyAttribute> metadata,
2524 DefaultingPyMlirContext context) {
2525 llvm::SmallVector<MlirLocation, 4> locations;
2526 locations.reserve(pyLocations.size());
2527 for (auto &pyLocation : pyLocations)
2528 locations.push_back(pyLocation.get());
2529 MlirLocation location = mlirLocationFusedGet(
2530 context->get(), locations.size(), locations.data(),
2531 metadata ? metadata->get() : MlirAttribute{0});
2532 return PyLocation(context->getRef(), location);
2533 },
2534 py::arg("locations"), py::arg("metadata") = py::none(),
2535 py::arg("context") = py::none(), kContextGetFusedLocationDocstring)
2536 .def_static(
2537 "name",
2538 [](std::string name, std::optional<PyLocation> childLoc,
2539 DefaultingPyMlirContext context) {
2540 return PyLocation(
2541 context->getRef(),
2542 mlirLocationNameGet(
2543 context->get(), toMlirStringRef(name),
2544 childLoc ? childLoc->get()
2545 : mlirLocationUnknownGet(context->get())));
2546 },
2547 py::arg("name"), py::arg("childLoc") = py::none(),
2548 py::arg("context") = py::none(), kContextGetNameLocationDocString)
2549 .def_property_readonly(
2550 "context",
2551 [](PyLocation &self) { return self.getContext().getObject(); },
2552 "Context that owns the Location")
2553 .def(
2554 "emit_error",
2555 [](PyLocation &self, std::string message) {
2556 mlirEmitError(self, message.c_str());
2557 },
2558 py::arg("message"), "Emits an error at this location")
2559 .def("__repr__", [](PyLocation &self) {
2560 PyPrintAccumulator printAccum;
2561 mlirLocationPrint(self, printAccum.getCallback(),
2562 printAccum.getUserData());
2563 return printAccum.join();
2564 });
2565
2566 //----------------------------------------------------------------------------
2567 // Mapping of Module
2568 //----------------------------------------------------------------------------
2569 py::class_<PyModule>(m, "Module", py::module_local())
2570 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyModule::getCapsule)
2571 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyModule::createFromCapsule)
2572 .def_static(
2573 "parse",
2574 [](const std::string moduleAsm, DefaultingPyMlirContext context) {
2575 MlirModule module = mlirModuleCreateParse(
2576 context->get(), toMlirStringRef(moduleAsm));
2577 // TODO: Rework error reporting once diagnostic engine is exposed
2578 // in C API.
2579 if (mlirModuleIsNull(module)) {
2580 throw SetPyError(
2581 PyExc_ValueError,
2582 "Unable to parse module assembly (see diagnostics)");
2583 }
2584 return PyModule::forModule(module).releaseObject();
2585 },
2586 py::arg("asm"), py::arg("context") = py::none(),
2587 kModuleParseDocstring)
2588 .def_static(
2589 "create",
2590 [](DefaultingPyLocation loc) {
2591 MlirModule module = mlirModuleCreateEmpty(loc);
2592 return PyModule::forModule(module).releaseObject();
2593 },
2594 py::arg("loc") = py::none(), "Creates an empty module")
2595 .def_property_readonly(
2596 "context",
2597 [](PyModule &self) { return self.getContext().getObject(); },
2598 "Context that created the Module")
2599 .def_property_readonly(
2600 "operation",
2601 [](PyModule &self) {
2602 return PyOperation::forOperation(self.getContext(),
2603 mlirModuleGetOperation(self.get()),
2604 self.getRef().releaseObject())
2605 .releaseObject();
2606 },
2607 "Accesses the module as an operation")
2608 .def_property_readonly(
2609 "body",
2610 [](PyModule &self) {
2611 PyOperationRef moduleOp = PyOperation::forOperation(
2612 self.getContext(), mlirModuleGetOperation(self.get()),
2613 self.getRef().releaseObject());
2614 PyBlock returnBlock(moduleOp, mlirModuleGetBody(self.get()));
2615 return returnBlock;
2616 },
2617 "Return the block for this module")
2618 .def(
2619 "dump",
2620 [](PyModule &self) {
2621 mlirOperationDump(mlirModuleGetOperation(self.get()));
2622 },
2623 kDumpDocstring)
2624 .def(
2625 "__str__",
2626 [](py::object self) {
2627 // Defer to the operation's __str__.
2628 return self.attr("operation").attr("__str__")();
2629 },
2630 kOperationStrDunderDocstring);
2631
2632 //----------------------------------------------------------------------------
2633 // Mapping of Operation.
2634 //----------------------------------------------------------------------------
2635 py::class_<PyOperationBase>(m, "_OperationBase", py::module_local())
2636 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
2637 [](PyOperationBase &self) {
2638 return self.getOperation().getCapsule();
2639 })
2640 .def("__eq__",
2641 [](PyOperationBase &self, PyOperationBase &other) {
2642 return &self.getOperation() == &other.getOperation();
2643 })
2644 .def("__eq__",
2645 [](PyOperationBase &self, py::object other) { return false; })
2646 .def("__hash__",
2647 [](PyOperationBase &self) {
2648 return static_cast<size_t>(llvm::hash_value(&self.getOperation()));
2649 })
2650 .def_property_readonly("attributes",
2651 [](PyOperationBase &self) {
2652 return PyOpAttributeMap(
2653 self.getOperation().getRef());
2654 })
2655 .def_property_readonly("operands",
2656 [](PyOperationBase &self) {
2657 return PyOpOperandList(
2658 self.getOperation().getRef());
2659 })
2660 .def_property_readonly("regions",
2661 [](PyOperationBase &self) {
2662 return PyRegionList(
2663 self.getOperation().getRef());
2664 })
2665 .def_property_readonly(
2666 "results",
2667 [](PyOperationBase &self) {
2668 return PyOpResultList(self.getOperation().getRef());
2669 },
2670 "Returns the list of Operation results.")
2671 .def_property_readonly(
2672 "result",
2673 [](PyOperationBase &self) {
2674 auto &operation = self.getOperation();
2675 auto numResults = mlirOperationGetNumResults(operation);
2676 if (numResults != 1) {
2677 auto name = mlirIdentifierStr(mlirOperationGetName(operation));
2678 throw SetPyError(
2679 PyExc_ValueError,
2680 Twine("Cannot call .result on operation ") +
2681 StringRef(name.data, name.length) + " which has " +
2682 Twine(numResults) +
2683 " results (it is only valid for operations with a "
2684 "single result)");
2685 }
2686 return PyOpResult(operation.getRef(),
2687 mlirOperationGetResult(operation, 0));
2688 },
2689 "Shortcut to get an op result if it has only one (throws an error "
2690 "otherwise).")
2691 .def_property_readonly(
2692 "location",
2693 [](PyOperationBase &self) {
2694 PyOperation &operation = self.getOperation();
2695 return PyLocation(operation.getContext(),
2696 mlirOperationGetLocation(operation.get()));
2697 },
2698 "Returns the source location the operation was defined or derived "
2699 "from.")
2700 .def(
2701 "__str__",
2702 [](PyOperationBase &self) {
2703 return self.getAsm(/*binary=*/false,
2704 /*largeElementsLimit=*/std::nullopt,
2705 /*enableDebugInfo=*/false,
2706 /*prettyDebugInfo=*/false,
2707 /*printGenericOpForm=*/false,
2708 /*useLocalScope=*/false,
2709 /*assumeVerified=*/false);
2710 },
2711 "Returns the assembly form of the operation.")
2712 .def("print", &PyOperationBase::print,
2713 // Careful: Lots of arguments must match up with print method.
2714 py::arg("file") = py::none(), py::arg("binary") = false,
2715 py::arg("large_elements_limit") = py::none(),
2716 py::arg("enable_debug_info") = false,
2717 py::arg("pretty_debug_info") = false,
2718 py::arg("print_generic_op_form") = false,
2719 py::arg("use_local_scope") = false,
2720 py::arg("assume_verified") = false, kOperationPrintDocstring)
2721 .def("write_bytecode", &PyOperationBase::writeBytecode, py::arg("file"),
2722 kOperationPrintBytecodeDocstring)
2723 .def("get_asm", &PyOperationBase::getAsm,
2724 // Careful: Lots of arguments must match up with get_asm method.
2725 py::arg("binary") = false,
2726 py::arg("large_elements_limit") = py::none(),
2727 py::arg("enable_debug_info") = false,
2728 py::arg("pretty_debug_info") = false,
2729 py::arg("print_generic_op_form") = false,
2730 py::arg("use_local_scope") = false,
2731 py::arg("assume_verified") = false, kOperationGetAsmDocstring)
2732 .def(
2733 "verify",
2734 [](PyOperationBase &self) {
2735 return mlirOperationVerify(self.getOperation());
2736 },
2737 "Verify the operation and return true if it passes, false if it "
2738 "fails.")
2739 .def("move_after", &PyOperationBase::moveAfter, py::arg("other"),
2740 "Puts self immediately after the other operation in its parent "
2741 "block.")
2742 .def("move_before", &PyOperationBase::moveBefore, py::arg("other"),
2743 "Puts self immediately before the other operation in its parent "
2744 "block.")
2745 .def(
2746 "detach_from_parent",
2747 [](PyOperationBase &self) {
2748 PyOperation &operation = self.getOperation();
2749 operation.checkValid();
2750 if (!operation.isAttached())
2751 throw py::value_error("Detached operation has no parent.");
2752
2753 operation.detachFromParent();
2754 return operation.createOpView();
2755 },
2756 "Detaches the operation from its parent block.");
2757
2758 py::class_<PyOperation, PyOperationBase>(m, "Operation", py::module_local())
2759 .def_static("create", &PyOperation::create, py::arg("name"),
2760 py::arg("results") = py::none(),
2761 py::arg("operands") = py::none(),
2762 py::arg("attributes") = py::none(),
2763 py::arg("successors") = py::none(), py::arg("regions") = 0,
2764 py::arg("loc") = py::none(), py::arg("ip") = py::none(),
2765 kOperationCreateDocstring)
2766 .def_property_readonly("parent",
2767 [](PyOperation &self) -> py::object {
2768 auto parent = self.getParentOperation();
2769 if (parent)
2770 return parent->getObject();
2771 return py::none();
2772 })
2773 .def("erase", &PyOperation::erase)
2774 .def("clone", &PyOperation::clone, py::arg("ip") = py::none())
2775 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
2776 &PyOperation::getCapsule)
2777 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyOperation::createFromCapsule)
2778 .def_property_readonly("name",
2779 [](PyOperation &self) {
2780 self.checkValid();
2781 MlirOperation operation = self.get();
2782 MlirStringRef name = mlirIdentifierStr(
2783 mlirOperationGetName(operation));
2784 return py::str(name.data, name.length);
2785 })
2786 .def_property_readonly(
2787 "context",
2788 [](PyOperation &self) {
2789 self.checkValid();
2790 return self.getContext().getObject();
2791 },
2792 "Context that owns the Operation")
2793 .def_property_readonly("opview", &PyOperation::createOpView);
2794
2795 auto opViewClass =
2796 py::class_<PyOpView, PyOperationBase>(m, "OpView", py::module_local())
2797 .def(py::init<py::object>(), py::arg("operation"))
2798 .def_property_readonly("operation", &PyOpView::getOperationObject)
2799 .def_property_readonly(
2800 "context",
2801 [](PyOpView &self) {
2802 return self.getOperation().getContext().getObject();
2803 },
2804 "Context that owns the Operation")
2805 .def("__str__", [](PyOpView &self) {
2806 return py::str(self.getOperationObject());
2807 });
2808 opViewClass.attr("_ODS_REGIONS") = py::make_tuple(0, true);
2809 opViewClass.attr("_ODS_OPERAND_SEGMENTS") = py::none();
2810 opViewClass.attr("_ODS_RESULT_SEGMENTS") = py::none();
2811 opViewClass.attr("build_generic") = classmethod(
2812 &PyOpView::buildGeneric, py::arg("cls"), py::arg("results") = py::none(),
2813 py::arg("operands") = py::none(), py::arg("attributes") = py::none(),
2814 py::arg("successors") = py::none(), py::arg("regions") = py::none(),
2815 py::arg("loc") = py::none(), py::arg("ip") = py::none(),
2816 "Builds a specific, generated OpView based on class level attributes.");
2817
2818 //----------------------------------------------------------------------------
2819 // Mapping of PyRegion.
2820 //----------------------------------------------------------------------------
2821 py::class_<PyRegion>(m, "Region", py::module_local())
2822 .def_property_readonly(
2823 "blocks",
2824 [](PyRegion &self) {
2825 return PyBlockList(self.getParentOperation(), self.get());
2826 },
2827 "Returns a forward-optimized sequence of blocks.")
2828 .def_property_readonly(
2829 "owner",
2830 [](PyRegion &self) {
2831 return self.getParentOperation()->createOpView();
2832 },
2833 "Returns the operation owning this region.")
2834 .def(
2835 "__iter__",
2836 [](PyRegion &self) {
2837 self.checkValid();
2838 MlirBlock firstBlock = mlirRegionGetFirstBlock(self.get());
2839 return PyBlockIterator(self.getParentOperation(), firstBlock);
2840 },
2841 "Iterates over blocks in the region.")
2842 .def("__eq__",
2843 [](PyRegion &self, PyRegion &other) {
2844 return self.get().ptr == other.get().ptr;
2845 })
2846 .def("__eq__", [](PyRegion &self, py::object &other) { return false; });
2847
2848 //----------------------------------------------------------------------------
2849 // Mapping of PyBlock.
2850 //----------------------------------------------------------------------------
2851 py::class_<PyBlock>(m, "Block", py::module_local())
2852 .def_property_readonly(
2853 "owner",
2854 [](PyBlock &self) {
2855 return self.getParentOperation()->createOpView();
2856 },
2857 "Returns the owning operation of this block.")
2858 .def_property_readonly(
2859 "region",
2860 [](PyBlock &self) {
2861 MlirRegion region = mlirBlockGetParentRegion(self.get());
2862 return PyRegion(self.getParentOperation(), region);
2863 },
2864 "Returns the owning region of this block.")
2865 .def_property_readonly(
2866 "arguments",
2867 [](PyBlock &self) {
2868 return PyBlockArgumentList(self.getParentOperation(), self.get());
2869 },
2870 "Returns a list of block arguments.")
2871 .def_property_readonly(
2872 "operations",
2873 [](PyBlock &self) {
2874 return PyOperationList(self.getParentOperation(), self.get());
2875 },
2876 "Returns a forward-optimized sequence of operations.")
2877 .def_static(
2878 "create_at_start",
2879 [](PyRegion &parent, py::list pyArgTypes) {
2880 parent.checkValid();
2881 llvm::SmallVector<MlirType, 4> argTypes;
2882 llvm::SmallVector<MlirLocation, 4> argLocs;
2883 argTypes.reserve(pyArgTypes.size());
2884 argLocs.reserve(pyArgTypes.size());
2885 for (auto &pyArg : pyArgTypes) {
2886 argTypes.push_back(pyArg.cast<PyType &>());
2887 // TODO: Pass in a proper location here.
2888 argLocs.push_back(
2889 mlirLocationUnknownGet(mlirTypeGetContext(argTypes.back())));
2890 }
2891
2892 MlirBlock block = mlirBlockCreate(argTypes.size(), argTypes.data(),
2893 argLocs.data());
2894 mlirRegionInsertOwnedBlock(parent, 0, block);
2895 return PyBlock(parent.getParentOperation(), block);
2896 },
2897 py::arg("parent"), py::arg("arg_types") = py::list(),
2898 "Creates and returns a new Block at the beginning of the given "
2899 "region (with given argument types).")
2900 .def(
2901 "append_to",
2902 [](PyBlock &self, PyRegion &region) {
2903 MlirBlock b = self.get();
2904 if (!mlirRegionIsNull(mlirBlockGetParentRegion(b)))
2905 mlirBlockDetach(b);
2906 mlirRegionAppendOwnedBlock(region.get(), b);
2907 },
2908 "Append this block to a region, transferring ownership if necessary")
2909 .def(
2910 "create_before",
2911 [](PyBlock &self, py::args pyArgTypes) {
2912 self.checkValid();
2913 llvm::SmallVector<MlirType, 4> argTypes;
2914 llvm::SmallVector<MlirLocation, 4> argLocs;
2915 argTypes.reserve(pyArgTypes.size());
2916 argLocs.reserve(pyArgTypes.size());
2917 for (auto &pyArg : pyArgTypes) {
2918 argTypes.push_back(pyArg.cast<PyType &>());
2919 // TODO: Pass in a proper location here.
2920 argLocs.push_back(
2921 mlirLocationUnknownGet(mlirTypeGetContext(argTypes.back())));
2922 }
2923
2924 MlirBlock block = mlirBlockCreate(argTypes.size(), argTypes.data(),
2925 argLocs.data());
2926 MlirRegion region = mlirBlockGetParentRegion(self.get());
2927 mlirRegionInsertOwnedBlockBefore(region, self.get(), block);
2928 return PyBlock(self.getParentOperation(), block);
2929 },
2930 "Creates and returns a new Block before this block "
2931 "(with given argument types).")
2932 .def(
2933 "create_after",
2934 [](PyBlock &self, py::args pyArgTypes) {
2935 self.checkValid();
2936 llvm::SmallVector<MlirType, 4> argTypes;
2937 llvm::SmallVector<MlirLocation, 4> argLocs;
2938 argTypes.reserve(pyArgTypes.size());
2939 argLocs.reserve(pyArgTypes.size());
2940 for (auto &pyArg : pyArgTypes) {
2941 argTypes.push_back(pyArg.cast<PyType &>());
2942
2943 // TODO: Pass in a proper location here.
2944 argLocs.push_back(
2945 mlirLocationUnknownGet(mlirTypeGetContext(argTypes.back())));
2946 }
2947 MlirBlock block = mlirBlockCreate(argTypes.size(), argTypes.data(),
2948 argLocs.data());
2949 MlirRegion region = mlirBlockGetParentRegion(self.get());
2950 mlirRegionInsertOwnedBlockAfter(region, self.get(), block);
2951 return PyBlock(self.getParentOperation(), block);
2952 },
2953 "Creates and returns a new Block after this block "
2954 "(with given argument types).")
2955 .def(
2956 "__iter__",
2957 [](PyBlock &self) {
2958 self.checkValid();
2959 MlirOperation firstOperation =
2960 mlirBlockGetFirstOperation(self.get());
2961 return PyOperationIterator(self.getParentOperation(),
2962 firstOperation);
2963 },
2964 "Iterates over operations in the block.")
2965 .def("__eq__",
2966 [](PyBlock &self, PyBlock &other) {
2967 return self.get().ptr == other.get().ptr;
2968 })
2969 .def("__eq__", [](PyBlock &self, py::object &other) { return false; })
2970 .def("__hash__",
2971 [](PyBlock &self) {
2972 return static_cast<size_t>(llvm::hash_value(self.get().ptr));
2973 })
2974 .def(
2975 "__str__",
2976 [](PyBlock &self) {
2977 self.checkValid();
2978 PyPrintAccumulator printAccum;
2979 mlirBlockPrint(self.get(), printAccum.getCallback(),
2980 printAccum.getUserData());
2981 return printAccum.join();
2982 },
2983 "Returns the assembly form of the block.")
2984 .def(
2985 "append",
2986 [](PyBlock &self, PyOperationBase &operation) {
2987 if (operation.getOperation().isAttached())
2988 operation.getOperation().detachFromParent();
2989
2990 MlirOperation mlirOperation = operation.getOperation().get();
2991 mlirBlockAppendOwnedOperation(self.get(), mlirOperation);
2992 operation.getOperation().setAttached(
2993 self.getParentOperation().getObject());
2994 },
2995 py::arg("operation"),
2996 "Appends an operation to this block. If the operation is currently "
2997 "in another block, it will be moved.");
2998
2999 //----------------------------------------------------------------------------
3000 // Mapping of PyInsertionPoint.
3001 //----------------------------------------------------------------------------
3002
3003 py::class_<PyInsertionPoint>(m, "InsertionPoint", py::module_local())
3004 .def(py::init<PyBlock &>(), py::arg("block"),
3005 "Inserts after the last operation but still inside the block.")
3006 .def("__enter__", &PyInsertionPoint::contextEnter)
3007 .def("__exit__", &PyInsertionPoint::contextExit)
3008 .def_property_readonly_static(
3009 "current",
3010 [](py::object & /*class*/) {
3011 auto *ip = PyThreadContextEntry::getDefaultInsertionPoint();
3012 if (!ip)
3013 throw SetPyError(PyExc_ValueError, "No current InsertionPoint");
3014 return ip;
3015 },
3016 "Gets the InsertionPoint bound to the current thread or raises "
3017 "ValueError if none has been set")
3018 .def(py::init<PyOperationBase &>(), py::arg("beforeOperation"),
3019 "Inserts before a referenced operation.")
3020 .def_static("at_block_begin", &PyInsertionPoint::atBlockBegin,
3021 py::arg("block"), "Inserts at the beginning of the block.")
3022 .def_static("at_block_terminator", &PyInsertionPoint::atBlockTerminator,
3023 py::arg("block"), "Inserts before the block terminator.")
3024 .def("insert", &PyInsertionPoint::insert, py::arg("operation"),
3025 "Inserts an operation.")
3026 .def_property_readonly(
3027 "block", [](PyInsertionPoint &self) { return self.getBlock(); },
3028 "Returns the block that this InsertionPoint points to.");
3029
3030 //----------------------------------------------------------------------------
3031 // Mapping of PyAttribute.
3032 //----------------------------------------------------------------------------
3033 py::class_<PyAttribute>(m, "Attribute", py::module_local())
3034 // Delegate to the PyAttribute copy constructor, which will also lifetime
3035 // extend the backing context which owns the MlirAttribute.
3036 .def(py::init<PyAttribute &>(), py::arg("cast_from_type"),
3037 "Casts the passed attribute to the generic Attribute")
3038 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
3039 &PyAttribute::getCapsule)
3040 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyAttribute::createFromCapsule)
3041 .def_static(
3042 "parse",
3043 [](std::string attrSpec, DefaultingPyMlirContext context) {
3044 MlirAttribute type = mlirAttributeParseGet(
3045 context->get(), toMlirStringRef(attrSpec));
3046 // TODO: Rework error reporting once diagnostic engine is exposed
3047 // in C API.
3048 if (mlirAttributeIsNull(type)) {
3049 throw SetPyError(PyExc_ValueError,
3050 Twine("Unable to parse attribute: '") +
3051 attrSpec + "'");
3052 }
3053 return PyAttribute(context->getRef(), type);
3054 },
3055 py::arg("asm"), py::arg("context") = py::none(),
3056 "Parses an attribute from an assembly form")
3057 .def_property_readonly(
3058 "context",
3059 [](PyAttribute &self) { return self.getContext().getObject(); },
3060 "Context that owns the Attribute")
3061 .def_property_readonly("type",
3062 [](PyAttribute &self) {
3063 return PyType(self.getContext()->getRef(),
3064 mlirAttributeGetType(self));
3065 })
3066 .def(
3067 "get_named",
3068 [](PyAttribute &self, std::string name) {
3069 return PyNamedAttribute(self, std::move(name));
3070 },
3071 py::keep_alive<0, 1>(), "Binds a name to the attribute")
3072 .def("__eq__",
3073 [](PyAttribute &self, PyAttribute &other) { return self == other; })
3074 .def("__eq__", [](PyAttribute &self, py::object &other) { return false; })
3075 .def("__hash__",
3076 [](PyAttribute &self) {
3077 return static_cast<size_t>(llvm::hash_value(self.get().ptr));
3078 })
3079 .def(
3080 "dump", [](PyAttribute &self) { mlirAttributeDump(self); },
3081 kDumpDocstring)
3082 .def(
3083 "__str__",
3084 [](PyAttribute &self) {
3085 PyPrintAccumulator printAccum;
3086 mlirAttributePrint(self, printAccum.getCallback(),
3087 printAccum.getUserData());
3088 return printAccum.join();
3089 },
3090 "Returns the assembly form of the Attribute.")
3091 .def("__repr__", [](PyAttribute &self) {
3092 // Generally, assembly formats are not printed for __repr__ because
3093 // this can cause exceptionally long debug output and exceptions.
3094 // However, attribute values are generally considered useful and are
3095 // printed. This may need to be re-evaluated if debug dumps end up
3096 // being excessive.
3097 PyPrintAccumulator printAccum;
3098 printAccum.parts.append("Attribute(");
3099 mlirAttributePrint(self, printAccum.getCallback(),
3100 printAccum.getUserData());
3101 printAccum.parts.append(")");
3102 return printAccum.join();
3103 });
3104
3105 //----------------------------------------------------------------------------
3106 // Mapping of PyNamedAttribute
3107 //----------------------------------------------------------------------------
3108 py::class_<PyNamedAttribute>(m, "NamedAttribute", py::module_local())
3109 .def("__repr__",
3110 [](PyNamedAttribute &self) {
3111 PyPrintAccumulator printAccum;
3112 printAccum.parts.append("NamedAttribute(");
3113 printAccum.parts.append(
3114 py::str(mlirIdentifierStr(self.namedAttr.name).data,
3115 mlirIdentifierStr(self.namedAttr.name).length));
3116 printAccum.parts.append("=");
3117 mlirAttributePrint(self.namedAttr.attribute,
3118 printAccum.getCallback(),
3119 printAccum.getUserData());
3120 printAccum.parts.append(")");
3121 return printAccum.join();
3122 })
3123 .def_property_readonly(
3124 "name",
3125 [](PyNamedAttribute &self) {
3126 return py::str(mlirIdentifierStr(self.namedAttr.name).data,
3127 mlirIdentifierStr(self.namedAttr.name).length);
3128 },
3129 "The name of the NamedAttribute binding")
3130 .def_property_readonly(
3131 "attr",
3132 [](PyNamedAttribute &self) {
3133 // TODO: When named attribute is removed/refactored, also remove
3134 // this constructor (it does an inefficient table lookup).
3135 auto contextRef = PyMlirContext::forContext(
3136 mlirAttributeGetContext(self.namedAttr.attribute));
3137 return PyAttribute(std::move(contextRef), self.namedAttr.attribute);
3138 },
3139 py::keep_alive<0, 1>(),
3140 "The underlying generic attribute of the NamedAttribute binding");
3141
3142 //----------------------------------------------------------------------------
3143 // Mapping of PyType.
3144 //----------------------------------------------------------------------------
3145 py::class_<PyType>(m, "Type", py::module_local())
3146 // Delegate to the PyType copy constructor, which will also lifetime
3147 // extend the backing context which owns the MlirType.
3148 .def(py::init<PyType &>(), py::arg("cast_from_type"),
3149 "Casts the passed type to the generic Type")
3150 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyType::getCapsule)
3151 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyType::createFromCapsule)
3152 .def_static(
3153 "parse",
3154 [](std::string typeSpec, DefaultingPyMlirContext context) {
3155 MlirType type =
3156 mlirTypeParseGet(context->get(), toMlirStringRef(typeSpec));
3157 // TODO: Rework error reporting once diagnostic engine is exposed
3158 // in C API.
3159 if (mlirTypeIsNull(type)) {
3160 throw SetPyError(PyExc_ValueError,
3161 Twine("Unable to parse type: '") + typeSpec +
3162 "'");
3163 }
3164 return PyType(context->getRef(), type);
3165 },
3166 py::arg("asm"), py::arg("context") = py::none(),
3167 kContextParseTypeDocstring)
3168 .def_property_readonly(
3169 "context", [](PyType &self) { return self.getContext().getObject(); },
3170 "Context that owns the Type")
3171 .def("__eq__", [](PyType &self, PyType &other) { return self == other; })
3172 .def("__eq__", [](PyType &self, py::object &other) { return false; })
3173 .def("__hash__",
3174 [](PyType &self) {
3175 return static_cast<size_t>(llvm::hash_value(self.get().ptr));
3176 })
3177 .def(
3178 "dump", [](PyType &self) { mlirTypeDump(self); }, kDumpDocstring)
3179 .def(
3180 "__str__",
3181 [](PyType &self) {
3182 PyPrintAccumulator printAccum;
3183 mlirTypePrint(self, printAccum.getCallback(),
3184 printAccum.getUserData());
3185 return printAccum.join();
3186 },
3187 "Returns the assembly form of the type.")
3188 .def("__repr__", [](PyType &self) {
3189 // Generally, assembly formats are not printed for __repr__ because
3190 // this can cause exceptionally long debug output and exceptions.
3191 // However, types are an exception as they typically have compact
3192 // assembly forms and printing them is useful.
3193 PyPrintAccumulator printAccum;
3194 printAccum.parts.append("Type(");
3195 mlirTypePrint(self, printAccum.getCallback(), printAccum.getUserData());
3196 printAccum.parts.append(")");
3197 return printAccum.join();
3198 });
3199
3200 //----------------------------------------------------------------------------
3201 // Mapping of Value.
3202 //----------------------------------------------------------------------------
3203 py::class_<PyValue>(m, "Value", py::module_local())
3204 .def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR, &PyValue::getCapsule)
3205 .def(MLIR_PYTHON_CAPI_FACTORY_ATTR, &PyValue::createFromCapsule)
3206 .def_property_readonly(
3207 "context",
3208 [](PyValue &self) { return self.getParentOperation()->getContext(); },
3209 "Context in which the value lives.")
3210 .def(
3211 "dump", [](PyValue &self) { mlirValueDump(self.get()); },
3212 kDumpDocstring)
3213 .def_property_readonly(
3214 "owner",
3215 [](PyValue &self) -> py::object {
3216 MlirValue v = self.get();
3217 if (mlirValueIsAOpResult(v)) {
3218 assert(
3219 mlirOperationEqual(self.getParentOperation()->get(),
3220 mlirOpResultGetOwner(self.get())) &&
3221 "expected the owner of the value in Python to match that in "
3222 "the IR");
3223 return self.getParentOperation().getObject();
3224 }
3225
3226 if (mlirValueIsABlockArgument(v)) {
3227 MlirBlock block = mlirBlockArgumentGetOwner(self.get());
3228 return py::cast(PyBlock(self.getParentOperation(), block));
3229 }
3230
3231 assert(false && "Value must be a block argument or an op result");
3232 return py::none();
3233 })
3234 .def_property_readonly("uses",
3235 [](PyValue &self) {
3236 return PyOpOperandIterator(
3237 mlirValueGetFirstUse(self.get()));
3238 })
3239 .def("__eq__",
3240 [](PyValue &self, PyValue &other) {
3241 return self.get().ptr == other.get().ptr;
3242 })
3243 .def("__eq__", [](PyValue &self, py::object other) { return false; })
3244 .def("__hash__",
3245 [](PyValue &self) {
3246 return static_cast<size_t>(llvm::hash_value(self.get().ptr));
3247 })
3248 .def(
3249 "__str__",
3250 [](PyValue &self) {
3251 PyPrintAccumulator printAccum;
3252 printAccum.parts.append("Value(");
3253 mlirValuePrint(self.get(), printAccum.getCallback(),
3254 printAccum.getUserData());
3255 printAccum.parts.append(")");
3256 return printAccum.join();
3257 },
3258 kValueDunderStrDocstring)
3259 .def_property_readonly("type", [](PyValue &self) {
3260 return PyType(self.getParentOperation()->getContext(),
3261 mlirValueGetType(self.get()));
3262 });
3263 PyBlockArgument::bind(m);
3264 PyOpResult::bind(m);
3265 PyOpOperand::bind(m);
3266
3267 //----------------------------------------------------------------------------
3268 // Mapping of SymbolTable.
3269 //----------------------------------------------------------------------------
3270 py::class_<PySymbolTable>(m, "SymbolTable", py::module_local())
3271 .def(py::init<PyOperationBase &>())
3272 .def("__getitem__", &PySymbolTable::dunderGetItem)
3273 .def("insert", &PySymbolTable::insert, py::arg("operation"))
3274 .def("erase", &PySymbolTable::erase, py::arg("operation"))
3275 .def("__delitem__", &PySymbolTable::dunderDel)
3276 .def("__contains__",
3277 [](PySymbolTable &table, const std::string &name) {
3278 return !mlirOperationIsNull(mlirSymbolTableLookup(
3279 table, mlirStringRefCreate(name.data(), name.length())));
3280 })
3281 // Static helpers.
3282 .def_static("set_symbol_name", &PySymbolTable::setSymbolName,
3283 py::arg("symbol"), py::arg("name"))
3284 .def_static("get_symbol_name", &PySymbolTable::getSymbolName,
3285 py::arg("symbol"))
3286 .def_static("get_visibility", &PySymbolTable::getVisibility,
3287 py::arg("symbol"))
3288 .def_static("set_visibility", &PySymbolTable::setVisibility,
3289 py::arg("symbol"), py::arg("visibility"))
3290 .def_static("replace_all_symbol_uses",
3291 &PySymbolTable::replaceAllSymbolUses, py::arg("old_symbol"),
3292 py::arg("new_symbol"), py::arg("from_op"))
3293 .def_static("walk_symbol_tables", &PySymbolTable::walkSymbolTables,
3294 py::arg("from_op"), py::arg("all_sym_uses_visible"),
3295 py::arg("callback"));
3296
3297 // Container bindings.
3298 PyBlockArgumentList::bind(m);
3299 PyBlockIterator::bind(m);
3300 PyBlockList::bind(m);
3301 PyOperationIterator::bind(m);
3302 PyOperationList::bind(m);
3303 PyOpAttributeMap::bind(m);
3304 PyOpOperandIterator::bind(m);
3305 PyOpOperandList::bind(m);
3306 PyOpResultList::bind(m);
3307 PyRegionIterator::bind(m);
3308 PyRegionList::bind(m);
3309
3310 // Debug bindings.
3311 PyGlobalDebugFlag::bind(m);
3312
3313 // Attribute builder getter.
3314 PyAttrBuilderMap::bind(m);
3315 }