2020-08-16 18:49:28 -07:00
|
|
|
//===- IRModules.h - IR Submodules of pybind module -----------------------===//
|
|
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#ifndef MLIR_BINDINGS_PYTHON_IRMODULES_H
|
|
|
|
|
#define MLIR_BINDINGS_PYTHON_IRMODULES_H
|
|
|
|
|
|
2022-11-08 16:55:06 -05:00
|
|
|
#include <optional>
|
2022-01-14 01:35:55 +00:00
|
|
|
#include <utility>
|
2020-10-28 23:16:36 -07:00
|
|
|
#include <vector>
|
|
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
#include "PybindUtils.h"
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2021-01-07 11:09:09 +01:00
|
|
|
#include "mlir-c/AffineExpr.h"
|
2020-12-14 18:43:05 +08:00
|
|
|
#include "mlir-c/AffineMap.h"
|
2022-01-03 16:39:58 -08:00
|
|
|
#include "mlir-c/Diagnostics.h"
|
2020-08-16 18:49:28 -07:00
|
|
|
#include "mlir-c/IR.h"
|
2021-01-25 18:17:19 +01:00
|
|
|
#include "mlir-c/IntegerSet.h"
|
2020-09-18 00:21:09 -07:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-08-16 20:53:45 -07:00
|
|
|
namespace mlir {
|
|
|
|
|
namespace python {
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-09-20 21:25:46 -07:00
|
|
|
class PyBlock;
|
2022-01-03 16:39:58 -08:00
|
|
|
class PyDiagnostic;
|
|
|
|
|
class PyDiagnosticHandler;
|
2020-10-28 23:16:36 -07:00
|
|
|
class PyInsertionPoint;
|
2020-09-20 21:25:46 -07:00
|
|
|
class PyLocation;
|
2020-10-31 23:40:25 -07:00
|
|
|
class DefaultingPyLocation;
|
2020-08-16 18:49:28 -07:00
|
|
|
class PyMlirContext;
|
2020-10-31 23:40:25 -07:00
|
|
|
class DefaultingPyMlirContext;
|
2020-08-18 17:23:46 -07:00
|
|
|
class PyModule;
|
2020-09-18 18:38:21 -07:00
|
|
|
class PyOperation;
|
2020-09-20 21:25:46 -07:00
|
|
|
class PyType;
|
2021-11-02 12:39:36 +01:00
|
|
|
class PySymbolTable;
|
2020-10-20 11:21:05 +02:00
|
|
|
class PyValue;
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Template for a reference to a concrete type which captures a python
|
|
|
|
|
/// reference to its underlying python object.
|
|
|
|
|
template <typename T>
|
|
|
|
|
class PyObjectRef {
|
2020-09-18 00:21:09 -07:00
|
|
|
public:
|
2020-09-18 18:38:21 -07:00
|
|
|
PyObjectRef(T *referrent, pybind11::object object)
|
|
|
|
|
: referrent(referrent), object(std::move(object)) {
|
|
|
|
|
assert(this->referrent &&
|
|
|
|
|
"cannot construct PyObjectRef with null referrent");
|
|
|
|
|
assert(this->object && "cannot construct PyObjectRef with null object");
|
|
|
|
|
}
|
|
|
|
|
PyObjectRef(PyObjectRef &&other)
|
|
|
|
|
: referrent(other.referrent), object(std::move(other.object)) {
|
|
|
|
|
other.referrent = nullptr;
|
|
|
|
|
assert(!other.object);
|
|
|
|
|
}
|
|
|
|
|
PyObjectRef(const PyObjectRef &other)
|
|
|
|
|
: referrent(other.referrent), object(other.object /* copies */) {}
|
2022-01-14 01:35:35 +00:00
|
|
|
~PyObjectRef() = default;
|
2020-09-18 00:21:09 -07:00
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
int getRefCount() {
|
|
|
|
|
if (!object)
|
|
|
|
|
return 0;
|
|
|
|
|
return object.ref_count();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Releases the object held by this instance, returning it.
|
|
|
|
|
/// This is the proper thing to return from a function that wants to return
|
|
|
|
|
/// the reference. Note that this does not work from initializers.
|
|
|
|
|
pybind11::object releaseObject() {
|
|
|
|
|
assert(referrent && object);
|
|
|
|
|
referrent = nullptr;
|
|
|
|
|
auto stolen = std::move(object);
|
|
|
|
|
return stolen;
|
|
|
|
|
}
|
2020-09-18 00:21:09 -07:00
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
T *get() { return referrent; }
|
2020-09-18 18:38:21 -07:00
|
|
|
T *operator->() {
|
|
|
|
|
assert(referrent && object);
|
|
|
|
|
return referrent;
|
|
|
|
|
}
|
|
|
|
|
pybind11::object getObject() {
|
|
|
|
|
assert(referrent && object);
|
|
|
|
|
return object;
|
|
|
|
|
}
|
|
|
|
|
operator bool() const { return referrent && object; }
|
2020-09-18 00:21:09 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-09-18 18:38:21 -07:00
|
|
|
T *referrent;
|
2020-09-18 00:21:09 -07:00
|
|
|
pybind11::object object;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
/// Tracks an entry in the thread context stack. New entries are pushed onto
|
2020-10-31 23:40:25 -07:00
|
|
|
/// here for each with block that activates a new InsertionPoint, Context or
|
|
|
|
|
/// Location.
|
|
|
|
|
///
|
|
|
|
|
/// Pushing either a Location or InsertionPoint also pushes its associated
|
|
|
|
|
/// Context. Pushing a Context will not modify the Location or InsertionPoint
|
|
|
|
|
/// unless if they are from a different context, in which case, they are
|
|
|
|
|
/// cleared.
|
2020-10-28 23:16:36 -07:00
|
|
|
class PyThreadContextEntry {
|
|
|
|
|
public:
|
2020-10-31 23:40:25 -07:00
|
|
|
enum class FrameKind {
|
|
|
|
|
Context,
|
|
|
|
|
InsertionPoint,
|
|
|
|
|
Location,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
PyThreadContextEntry(FrameKind frameKind, pybind11::object context,
|
|
|
|
|
pybind11::object insertionPoint,
|
|
|
|
|
pybind11::object location)
|
|
|
|
|
: context(std::move(context)), insertionPoint(std::move(insertionPoint)),
|
|
|
|
|
location(std::move(location)), frameKind(frameKind) {}
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Gets the top of stack context and return nullptr if not defined.
|
2020-10-31 23:40:25 -07:00
|
|
|
static PyMlirContext *getDefaultContext();
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Gets the top of stack insertion point and return nullptr if not defined.
|
2020-10-31 23:40:25 -07:00
|
|
|
static PyInsertionPoint *getDefaultInsertionPoint();
|
|
|
|
|
|
|
|
|
|
/// Gets the top of stack location and returns nullptr if not defined.
|
|
|
|
|
static PyLocation *getDefaultLocation();
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
PyMlirContext *getContext();
|
|
|
|
|
PyInsertionPoint *getInsertionPoint();
|
2020-10-31 23:40:25 -07:00
|
|
|
PyLocation *getLocation();
|
|
|
|
|
FrameKind getFrameKind() { return frameKind; }
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Stack management.
|
2020-10-31 23:40:25 -07:00
|
|
|
static PyThreadContextEntry *getTopOfStack();
|
|
|
|
|
static pybind11::object pushContext(PyMlirContext &context);
|
|
|
|
|
static void popContext(PyMlirContext &context);
|
|
|
|
|
static pybind11::object pushInsertionPoint(PyInsertionPoint &insertionPoint);
|
|
|
|
|
static void popInsertionPoint(PyInsertionPoint &insertionPoint);
|
|
|
|
|
static pybind11::object pushLocation(PyLocation &location);
|
|
|
|
|
static void popLocation(PyLocation &location);
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Gets the thread local stack.
|
|
|
|
|
static std::vector<PyThreadContextEntry> &getStack();
|
|
|
|
|
|
|
|
|
|
private:
|
2020-10-31 23:40:25 -07:00
|
|
|
static void push(FrameKind frameKind, pybind11::object context,
|
|
|
|
|
pybind11::object insertionPoint, pybind11::object location);
|
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
/// An object reference to the PyContext.
|
|
|
|
|
pybind11::object context;
|
|
|
|
|
/// An object reference to the current insertion point.
|
|
|
|
|
pybind11::object insertionPoint;
|
2020-10-31 23:40:25 -07:00
|
|
|
/// An object reference to the current location.
|
|
|
|
|
pybind11::object location;
|
|
|
|
|
// The kind of push that was performed.
|
|
|
|
|
FrameKind frameKind;
|
2020-10-28 23:16:36 -07:00
|
|
|
};
|
2020-09-18 18:38:21 -07:00
|
|
|
|
2020-08-16 18:49:28 -07:00
|
|
|
/// Wrapper around MlirContext.
|
2020-10-28 23:16:36 -07:00
|
|
|
using PyMlirContextRef = PyObjectRef<PyMlirContext>;
|
2020-08-16 18:49:28 -07:00
|
|
|
class PyMlirContext {
|
|
|
|
|
public:
|
2020-09-18 00:21:09 -07:00
|
|
|
PyMlirContext() = delete;
|
|
|
|
|
PyMlirContext(const PyMlirContext &) = delete;
|
|
|
|
|
PyMlirContext(PyMlirContext &&) = delete;
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// For the case of a python __init__ (py::init) method, pybind11 is quite
|
|
|
|
|
/// strict about needing to return a pointer that is not yet associated to
|
|
|
|
|
/// an py::object. Since the forContext() method acts like a pool, possibly
|
|
|
|
|
/// returning a recycled context, it does not satisfy this need. The usual
|
|
|
|
|
/// way in python to accomplish such a thing is to override __new__, but
|
|
|
|
|
/// that is also not supported by pybind11. Instead, we use this entry
|
|
|
|
|
/// point which always constructs a fresh context (which cannot alias an
|
|
|
|
|
/// existing one because it is fresh).
|
|
|
|
|
static PyMlirContext *createNewContextForInit();
|
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
/// Returns a context reference for the singleton PyMlirContext wrapper for
|
|
|
|
|
/// the given context.
|
|
|
|
|
static PyMlirContextRef forContext(MlirContext context);
|
|
|
|
|
~PyMlirContext();
|
|
|
|
|
|
|
|
|
|
/// Accesses the underlying MlirContext.
|
|
|
|
|
MlirContext get() { return context; }
|
|
|
|
|
|
|
|
|
|
/// Gets a strong reference to this context, which will ensure it is kept
|
|
|
|
|
/// alive for the life of the reference.
|
|
|
|
|
PyMlirContextRef getRef() {
|
2020-09-18 18:38:21 -07:00
|
|
|
return PyMlirContextRef(this, pybind11::cast(this));
|
2020-09-18 00:21:09 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-28 09:08:09 -07:00
|
|
|
/// Gets a capsule wrapping the void* within the MlirContext.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyMlirContext from the MlirContext wrapped by a capsule.
|
|
|
|
|
/// Note that PyMlirContext instances are uniqued, so the returned object
|
2020-10-12 21:19:13 -07:00
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirContext
|
|
|
|
|
/// is taken by calling this function.
|
2020-09-28 09:08:09 -07:00
|
|
|
static pybind11::object createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
/// Gets the count of live context objects. Used for testing.
|
|
|
|
|
static size_t getLiveCount();
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Gets the count of live operations associated with this context.
|
|
|
|
|
/// Used for testing.
|
|
|
|
|
size_t getLiveOperationCount();
|
|
|
|
|
|
2022-04-19 15:03:15 -07:00
|
|
|
/// Clears the live operations map, returning the number of entries which were
|
|
|
|
|
/// invalidated. To be used as a safety mechanism so that API end-users can't
|
|
|
|
|
/// corrupt by holding references they shouldn't have accessed in the first
|
|
|
|
|
/// place.
|
|
|
|
|
size_t clearLiveOperations();
|
|
|
|
|
|
2020-10-12 21:19:13 -07:00
|
|
|
/// Gets the count of live modules associated with this context.
|
|
|
|
|
/// Used for testing.
|
|
|
|
|
size_t getLiveModuleCount();
|
|
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
/// Enter and exit the context manager.
|
|
|
|
|
pybind11::object contextEnter();
|
2022-01-02 01:26:44 +00:00
|
|
|
void contextExit(const pybind11::object &excType,
|
|
|
|
|
const pybind11::object &excVal,
|
|
|
|
|
const pybind11::object &excTb);
|
2020-09-20 21:25:46 -07:00
|
|
|
|
2022-01-03 16:39:58 -08:00
|
|
|
/// Attaches a Python callback as a diagnostic handler, returning a
|
|
|
|
|
/// registration object (internally a PyDiagnosticHandler).
|
|
|
|
|
pybind11::object attachDiagnosticHandler(pybind11::object callback);
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// Controls whether error diagnostics should be propagated to diagnostic
|
|
|
|
|
/// handlers, instead of being captured by `ErrorCapture`.
|
|
|
|
|
void setEmitErrorDiagnostics(bool value) { emitErrorDiagnostics = value; }
|
|
|
|
|
struct ErrorCapture;
|
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
private:
|
|
|
|
|
PyMlirContext(MlirContext context);
|
|
|
|
|
// Interns the mapping of live MlirContext::ptr to PyMlirContext instances,
|
|
|
|
|
// preserving the relationship that an MlirContext maps to a single
|
|
|
|
|
// PyMlirContext wrapper. This could be replaced in the future with an
|
|
|
|
|
// extension mechanism on the MlirContext for stashing user pointers.
|
|
|
|
|
// Note that this holds a handle, which does not imply ownership.
|
|
|
|
|
// Mappings will be removed when the context is destructed.
|
2020-09-18 18:38:21 -07:00
|
|
|
using LiveContextMap = llvm::DenseMap<void *, PyMlirContext *>;
|
2020-09-18 00:21:09 -07:00
|
|
|
static LiveContextMap &getLiveContexts();
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-10-12 21:19:13 -07:00
|
|
|
// Interns all live modules associated with this context. Modules tracked
|
|
|
|
|
// in this map are valid. When a module is invalidated, it is removed
|
|
|
|
|
// from this map, and while it still exists as an instance, any
|
|
|
|
|
// attempt to access it will raise an error.
|
|
|
|
|
using LiveModuleMap =
|
|
|
|
|
llvm::DenseMap<const void *, std::pair<pybind11::handle, PyModule *>>;
|
|
|
|
|
LiveModuleMap liveModules;
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
// Interns all live operations associated with this context. Operations
|
|
|
|
|
// tracked in this map are valid. When an operation is invalidated, it is
|
|
|
|
|
// removed from this map, and while it still exists as an instance, any
|
|
|
|
|
// attempt to access it will raise an error.
|
|
|
|
|
using LiveOperationMap =
|
|
|
|
|
llvm::DenseMap<void *, std::pair<pybind11::handle, PyOperation *>>;
|
|
|
|
|
LiveOperationMap liveOperations;
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
bool emitErrorDiagnostics = false;
|
|
|
|
|
|
2020-08-16 18:49:28 -07:00
|
|
|
MlirContext context;
|
2020-10-12 21:19:13 -07:00
|
|
|
friend class PyModule;
|
2020-09-18 18:38:21 -07:00
|
|
|
friend class PyOperation;
|
2020-09-18 00:21:09 -07:00
|
|
|
};
|
|
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
/// Used in function arguments when None should resolve to the current context
|
|
|
|
|
/// manager set instance.
|
|
|
|
|
class DefaultingPyMlirContext
|
|
|
|
|
: public Defaulting<DefaultingPyMlirContext, PyMlirContext> {
|
|
|
|
|
public:
|
|
|
|
|
using Defaulting::Defaulting;
|
2021-11-28 14:08:06 -08:00
|
|
|
static constexpr const char kTypeDescription[] = "mlir.ir.Context";
|
2020-10-31 23:40:25 -07:00
|
|
|
static PyMlirContext &resolve();
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-18 00:21:09 -07:00
|
|
|
/// Base class for all objects that directly or indirectly depend on an
|
|
|
|
|
/// MlirContext. The lifetime of the context will extend at least to the
|
|
|
|
|
/// lifetime of these instances.
|
|
|
|
|
/// Immutable objects that depend on a context extend this directly.
|
|
|
|
|
class BaseContextObject {
|
|
|
|
|
public:
|
2020-09-18 18:38:21 -07:00
|
|
|
BaseContextObject(PyMlirContextRef ref) : contextRef(std::move(ref)) {
|
|
|
|
|
assert(this->contextRef &&
|
|
|
|
|
"context object constructed with null context ref");
|
|
|
|
|
}
|
2020-09-18 00:21:09 -07:00
|
|
|
|
|
|
|
|
/// Accesses the context reference.
|
|
|
|
|
PyMlirContextRef &getContext() { return contextRef; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PyMlirContextRef contextRef;
|
2020-08-16 18:49:28 -07:00
|
|
|
};
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// Wrapper around an MlirLocation.
|
|
|
|
|
class PyLocation : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyLocation(PyMlirContextRef contextRef, MlirLocation loc)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), loc(loc) {}
|
|
|
|
|
|
|
|
|
|
operator MlirLocation() const { return loc; }
|
|
|
|
|
MlirLocation get() const { return loc; }
|
|
|
|
|
|
|
|
|
|
/// Enter and exit the context manager.
|
|
|
|
|
pybind11::object contextEnter();
|
|
|
|
|
void contextExit(const pybind11::object &excType,
|
|
|
|
|
const pybind11::object &excVal,
|
|
|
|
|
const pybind11::object &excTb);
|
|
|
|
|
|
|
|
|
|
/// Gets a capsule wrapping the void* within the MlirLocation.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyLocation from the MlirLocation wrapped by a capsule.
|
|
|
|
|
/// Note that PyLocation instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirLocation
|
|
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static PyLocation createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirLocation loc;
|
|
|
|
|
};
|
|
|
|
|
|
2022-01-03 16:39:58 -08:00
|
|
|
/// Python class mirroring the C MlirDiagnostic struct. Note that these structs
|
|
|
|
|
/// are only valid for the duration of a diagnostic callback and attempting
|
|
|
|
|
/// to access them outside of that will raise an exception. This applies to
|
|
|
|
|
/// nested diagnostics (in the notes) as well.
|
|
|
|
|
class PyDiagnostic {
|
|
|
|
|
public:
|
|
|
|
|
PyDiagnostic(MlirDiagnostic diagnostic) : diagnostic(diagnostic) {}
|
|
|
|
|
void invalidate();
|
|
|
|
|
bool isValid() { return valid; }
|
|
|
|
|
MlirDiagnosticSeverity getSeverity();
|
|
|
|
|
PyLocation getLocation();
|
|
|
|
|
pybind11::str getMessage();
|
|
|
|
|
pybind11::tuple getNotes();
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// Materialized diagnostic information. This is safe to access outside the
|
|
|
|
|
/// diagnostic callback.
|
|
|
|
|
struct DiagnosticInfo {
|
|
|
|
|
MlirDiagnosticSeverity severity;
|
|
|
|
|
PyLocation location;
|
|
|
|
|
std::string message;
|
|
|
|
|
std::vector<DiagnosticInfo> notes;
|
|
|
|
|
};
|
|
|
|
|
DiagnosticInfo getInfo();
|
|
|
|
|
|
2022-01-03 16:39:58 -08:00
|
|
|
private:
|
|
|
|
|
MlirDiagnostic diagnostic;
|
|
|
|
|
|
|
|
|
|
void checkValid();
|
|
|
|
|
/// If notes have been materialized from the diagnostic, then this will
|
|
|
|
|
/// be populated with the corresponding objects (all castable to
|
|
|
|
|
/// PyDiagnostic).
|
2022-12-19 04:28:55 +00:00
|
|
|
std::optional<pybind11::tuple> materializedNotes;
|
2022-01-03 16:39:58 -08:00
|
|
|
bool valid = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Represents a diagnostic handler attached to the context. The handler's
|
|
|
|
|
/// callback will be invoked with PyDiagnostic instances until the detach()
|
|
|
|
|
/// method is called or the context is destroyed. A diagnostic handler can be
|
|
|
|
|
/// the subject of a `with` block, which will detach it when the block exits.
|
|
|
|
|
///
|
|
|
|
|
/// Since diagnostic handlers can call back into Python code which can do
|
|
|
|
|
/// unsafe things (i.e. recursively emitting diagnostics, raising exceptions,
|
|
|
|
|
/// etc), this is generally not deemed to be a great user-level API. Users
|
|
|
|
|
/// should generally use some form of DiagnosticCollector. If the handler raises
|
|
|
|
|
/// any exceptions, they will just be emitted to stderr and dropped.
|
|
|
|
|
///
|
|
|
|
|
/// The unique usage of this class means that its lifetime management is
|
|
|
|
|
/// different from most other parts of the API. Instances are always created
|
|
|
|
|
/// in an attached state and can transition to a detached state by either:
|
|
|
|
|
/// a) The context being destroyed and unregistering all handlers.
|
|
|
|
|
/// b) An explicit call to detach().
|
|
|
|
|
/// The object may remain live from a Python perspective for an arbitrary time
|
|
|
|
|
/// after detachment, but there is nothing the user can do with it (since there
|
|
|
|
|
/// is no way to attach an existing handler object).
|
|
|
|
|
class PyDiagnosticHandler {
|
|
|
|
|
public:
|
|
|
|
|
PyDiagnosticHandler(MlirContext context, pybind11::object callback);
|
|
|
|
|
~PyDiagnosticHandler();
|
|
|
|
|
|
2022-08-07 15:28:18 -07:00
|
|
|
bool isAttached() { return registeredID.has_value(); }
|
2022-01-03 16:39:58 -08:00
|
|
|
bool getHadError() { return hadError; }
|
|
|
|
|
|
|
|
|
|
/// Detaches the handler. Does nothing if not attached.
|
|
|
|
|
void detach();
|
|
|
|
|
|
|
|
|
|
pybind11::object contextEnter() { return pybind11::cast(this); }
|
2022-01-14 01:35:55 +00:00
|
|
|
void contextExit(const pybind11::object &excType,
|
|
|
|
|
const pybind11::object &excVal,
|
|
|
|
|
const pybind11::object &excTb) {
|
2022-01-03 16:39:58 -08:00
|
|
|
detach();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirContext context;
|
|
|
|
|
pybind11::object callback;
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<MlirDiagnosticHandlerID> registeredID;
|
2022-01-03 16:39:58 -08:00
|
|
|
bool hadError = false;
|
|
|
|
|
friend class PyMlirContext;
|
|
|
|
|
};
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// RAII object that captures any error diagnostics emitted to the provided
|
|
|
|
|
/// context.
|
|
|
|
|
struct PyMlirContext::ErrorCapture {
|
|
|
|
|
ErrorCapture(PyMlirContextRef ctx)
|
|
|
|
|
: ctx(ctx), handlerID(mlirContextAttachDiagnosticHandler(
|
|
|
|
|
ctx->get(), handler, /*userData=*/this,
|
|
|
|
|
/*deleteUserData=*/nullptr)) {}
|
|
|
|
|
~ErrorCapture() {
|
|
|
|
|
mlirContextDetachDiagnosticHandler(ctx->get(), handlerID);
|
|
|
|
|
assert(errors.empty() && "unhandled captured errors");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<PyDiagnostic::DiagnosticInfo> take() {
|
|
|
|
|
return std::move(errors);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PyMlirContextRef ctx;
|
|
|
|
|
MlirDiagnosticHandlerID handlerID;
|
|
|
|
|
std::vector<PyDiagnostic::DiagnosticInfo> errors;
|
|
|
|
|
|
|
|
|
|
static MlirLogicalResult handler(MlirDiagnostic diag, void *userData);
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
/// Wrapper around an MlirDialect. This is exported as `DialectDescriptor` in
|
|
|
|
|
/// order to differentiate it from the `Dialect` base class which is extended by
|
|
|
|
|
/// plugins which extend dialect functionality through extension python code.
|
|
|
|
|
/// This should be seen as the "low-level" object and `Dialect` as the
|
|
|
|
|
/// high-level, user facing object.
|
|
|
|
|
class PyDialectDescriptor : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyDialectDescriptor(PyMlirContextRef contextRef, MlirDialect dialect)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), dialect(dialect) {}
|
|
|
|
|
|
|
|
|
|
MlirDialect get() { return dialect; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirDialect dialect;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// User-level object for accessing dialects with dotted syntax such as:
|
|
|
|
|
/// ctx.dialect.std
|
|
|
|
|
class PyDialects : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyDialects(PyMlirContextRef contextRef)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)) {}
|
|
|
|
|
|
|
|
|
|
MlirDialect getDialectForKey(const std::string &key, bool attrError);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// User-level dialect object. For dialects that have a registered extension,
|
|
|
|
|
/// this will be the base class of the extension dialect type. For un-extended,
|
|
|
|
|
/// objects of this type will be returned directly.
|
|
|
|
|
class PyDialect {
|
|
|
|
|
public:
|
|
|
|
|
PyDialect(pybind11::object descriptor) : descriptor(std::move(descriptor)) {}
|
|
|
|
|
|
|
|
|
|
pybind11::object getDescriptor() { return descriptor; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
pybind11::object descriptor;
|
|
|
|
|
};
|
|
|
|
|
|
[mlir] Overhaul C/Python registration APIs to properly scope registration/loading activities.
Since the very first commits, the Python and C MLIR APIs have had mis-placed registration/load functionality for dialects, extensions, etc. This was done pragmatically in order to get bootstrapped and then just grew in. Downstreams largely bypass and do their own thing by providing various APIs to register things they need. Meanwhile, the C++ APIs have stabilized around this and it would make sense to follow suit.
The thing we have observed in canonical usage by downstreams is that each downstream tends to have native entry points that configure its installation to its preferences with one-stop APIs. This patch leans in to this approach with `RegisterEverything.h` and `mlir._mlir_libs._mlirRegisterEverything` being the one-stop entry points for the "upstream packages". The `_mlir_libs.__init__.py` now allows customization of the environment and Context by adding "initialization modules" to the `_mlir_libs` package. If present, `_mlirRegisterEverything` is treated as such a module. Others can be added by downstreams by adding a `_site_initialize_{i}.py` module, where '{i}' is a number starting with zero. The number will be incremented and corresponding module loaded until one is not found. Initialization modules can:
* Perform load time customization to the global environment (i.e. registering passes, hooks, etc).
* Define a `register_dialects(registry: DialectRegistry)` function that can extend the `DialectRegistry` that will be used to bootstrap the `Context`.
* Define a `context_init_hook(context: Context)` function that will be added to a list of callbacks which will be invoked after dialect registration during `Context` initialization.
Note that the `MLIRPythonExtension.RegisterEverything` is not included by default when building a downstream (its corresponding behavior was prior). For downstreams which need the default MLIR initialization to take place, they must add this back in to their Python CMake build just like they add their own components (i.e. to `add_mlir_python_common_capi_library` and `add_mlir_python_modules`). It is perfectly valid to not do this, in which case, only the things explicitly depended on and initialized by downstreams will be built/packaged. If the downstream has not been set up for this, it is recommended to simply add this back for the time being and pay the build time/package size cost.
CMake changes:
* `MLIRCAPIRegistration` -> `MLIRCAPIRegisterEverything` (renamed to signify what it does and force an evaluation: a number of places were incidentally linking this very expensive target)
* `MLIRPythonSoure.Passes` removed (without replacement: just drop)
* `MLIRPythonExtension.AllPassesRegistration` removed (without replacement: just drop)
* `MLIRPythonExtension.Conversions` removed (without replacement: just drop)
* `MLIRPythonExtension.Transforms` removed (without replacement: just drop)
Header changes:
* `mlir-c/Registration.h` is deleted. Dialect registration functionality is now in `IR.h`. Registration of upstream features are in `mlir-c/RegisterEverything.h`. When updating MLIR and a couple of downstreams, I found that proper usage was commingled so required making a choice vs just blind S&R.
Python APIs removed:
* mlir.transforms and mlir.conversions (previously only had an __init__.py which indirectly triggered `mlirRegisterTransformsPasses()` and `mlirRegisterConversionPasses()` respectively). Downstream impact: Remove these imports if present (they now happen as part of default initialization).
* mlir._mlir_libs._all_passes_registration, mlir._mlir_libs._mlirTransforms, mlir._mlir_libs._mlirConversions. Downstream impact: None expected (these were internally used).
C-APIs changed:
* mlirRegisterAllDialects(MlirContext) now takes an MlirDialectRegistry instead. It also used to trigger loading of all dialects, which was already marked with a TODO to remove -- it no longer does, and for direct use, dialects must be explicitly loaded. Downstream impact: Direct C-API users must ensure that needed dialects are loaded or call `mlirContextLoadAllAvailableDialects(MlirContext)` to emulate the prior behavior. Also see the `ir.c` test case (e.g. ` mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("func"));`).
* mlirDialectHandle* APIs were moved from Registration.h (which now is restricted to just global/upstream registration) to IR.h, arguably where it should have been. Downstream impact: include correct header (likely already doing so).
C-APIs added:
* mlirContextLoadAllAvailableDialects(MlirContext): Corresponds to C++ API with the same purpose.
Python APIs added:
* mlir.ir.DialectRegistry: Mapping for an MlirDialectRegistry.
* mlir.ir.Context.append_dialect_registry(MlirDialectRegistry)
* mlir.ir.Context.load_all_available_dialects()
* mlir._mlir_libs._mlirAllRegistration: New native extension that exposes a `register_dialects(MlirDialectRegistry)` entry point and performs all upstream pass/conversion/transforms registration on init. In this first step, we eagerly load this as part of the __init__.py and use it to monkey patch the Context to emulate prior behavior.
* Type caster and capsule support for MlirDialectRegistry
This should make it possible to build downstream Python dialects that only depend on a subset of MLIR. See: https://github.com/llvm/llvm-project/issues/56037
Here is an example PR, minimally adapting IREE to these changes: https://github.com/iree-org/iree/pull/9638/files In this situation, IREE is opting to not link everything, since it is already configuring the Context to its liking. For projects that would just like to not think about it and pull in everything, add `MLIRPythonExtension.RegisterEverything` to the list of Python sources getting built, and the old behavior will continue.
Reviewed By: mehdi_amini, ftynse
Differential Revision: https://reviews.llvm.org/D128593
2022-07-16 16:09:03 -07:00
|
|
|
/// Wrapper around an MlirDialectRegistry.
|
|
|
|
|
/// Upon construction, the Python wrapper takes ownership of the
|
|
|
|
|
/// underlying MlirDialectRegistry.
|
|
|
|
|
class PyDialectRegistry {
|
|
|
|
|
public:
|
|
|
|
|
PyDialectRegistry() : registry(mlirDialectRegistryCreate()) {}
|
|
|
|
|
PyDialectRegistry(MlirDialectRegistry registry) : registry(registry) {}
|
|
|
|
|
~PyDialectRegistry() {
|
|
|
|
|
if (!mlirDialectRegistryIsNull(registry))
|
|
|
|
|
mlirDialectRegistryDestroy(registry);
|
|
|
|
|
}
|
|
|
|
|
PyDialectRegistry(PyDialectRegistry &) = delete;
|
|
|
|
|
PyDialectRegistry(PyDialectRegistry &&other) : registry(other.registry) {
|
|
|
|
|
other.registry = {nullptr};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
operator MlirDialectRegistry() const { return registry; }
|
|
|
|
|
MlirDialectRegistry get() const { return registry; }
|
|
|
|
|
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
static PyDialectRegistry createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirDialectRegistry registry;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
/// Used in function arguments when None should resolve to the current context
|
|
|
|
|
/// manager set instance.
|
|
|
|
|
class DefaultingPyLocation
|
|
|
|
|
: public Defaulting<DefaultingPyLocation, PyLocation> {
|
|
|
|
|
public:
|
|
|
|
|
using Defaulting::Defaulting;
|
2021-11-28 14:08:06 -08:00
|
|
|
static constexpr const char kTypeDescription[] = "mlir.ir.Location";
|
2020-10-31 23:40:25 -07:00
|
|
|
static PyLocation &resolve();
|
2020-11-29 13:30:23 -08:00
|
|
|
|
|
|
|
|
operator MlirLocation() const { return *get(); }
|
2020-10-31 23:40:25 -07:00
|
|
|
};
|
|
|
|
|
|
2020-08-16 18:49:28 -07:00
|
|
|
/// Wrapper around MlirModule.
|
2020-09-18 18:38:21 -07:00
|
|
|
/// This is the top-level, user-owned object that contains regions/ops/blocks.
|
|
|
|
|
class PyModule;
|
|
|
|
|
using PyModuleRef = PyObjectRef<PyModule>;
|
2020-09-18 00:21:09 -07:00
|
|
|
class PyModule : public BaseContextObject {
|
2020-08-16 18:49:28 -07:00
|
|
|
public:
|
2020-10-12 21:19:13 -07:00
|
|
|
/// Returns a PyModule reference for the given MlirModule. This may return
|
|
|
|
|
/// a pre-existing or new object.
|
|
|
|
|
static PyModuleRef forModule(MlirModule module);
|
2020-08-18 17:23:46 -07:00
|
|
|
PyModule(PyModule &) = delete;
|
2020-10-12 21:19:13 -07:00
|
|
|
PyModule(PyMlirContext &&) = delete;
|
|
|
|
|
~PyModule();
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Gets the backing MlirModule.
|
|
|
|
|
MlirModule get() { return module; }
|
|
|
|
|
|
|
|
|
|
/// Gets a strong reference to this module.
|
|
|
|
|
PyModuleRef getRef() {
|
|
|
|
|
return PyModuleRef(this,
|
|
|
|
|
pybind11::reinterpret_borrow<pybind11::object>(handle));
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 09:08:09 -07:00
|
|
|
/// Gets a capsule wrapping the void* within the MlirModule.
|
|
|
|
|
/// Note that the module does not (yet) provide a corresponding factory for
|
|
|
|
|
/// constructing from a capsule as that would require uniquing PyModule
|
|
|
|
|
/// instances, which is not currently done.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
2020-10-12 21:19:13 -07:00
|
|
|
/// Creates a PyModule from the MlirModule wrapped by a capsule.
|
|
|
|
|
/// Note that PyModule instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirModule
|
|
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static pybind11::object createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
private:
|
2020-10-12 21:19:13 -07:00
|
|
|
PyModule(PyMlirContextRef contextRef, MlirModule module);
|
2020-08-16 18:49:28 -07:00
|
|
|
MlirModule module;
|
2020-09-18 18:38:21 -07:00
|
|
|
pybind11::handle handle;
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-01 23:05:36 -08:00
|
|
|
/// Base class for PyOperation and PyOpView which exposes the primary, user
|
|
|
|
|
/// visible methods for manipulating it.
|
|
|
|
|
class PyOperationBase {
|
|
|
|
|
public:
|
|
|
|
|
virtual ~PyOperationBase() = default;
|
|
|
|
|
/// Implements the bound 'print' method and helps with others.
|
|
|
|
|
void print(pybind11::object fileObject, bool binary,
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<int64_t> largeElementsLimit, bool enableDebugInfo,
|
2021-11-28 15:33:03 -08:00
|
|
|
bool prettyDebugInfo, bool printGenericOpForm, bool useLocalScope,
|
|
|
|
|
bool assumeVerified);
|
2020-11-01 23:05:36 -08:00
|
|
|
pybind11::object getAsm(bool binary,
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<int64_t> largeElementsLimit,
|
2020-11-01 23:05:36 -08:00
|
|
|
bool enableDebugInfo, bool prettyDebugInfo,
|
2021-11-28 15:33:03 -08:00
|
|
|
bool printGenericOpForm, bool useLocalScope,
|
|
|
|
|
bool assumeVerified);
|
2020-11-01 23:05:36 -08:00
|
|
|
|
2022-09-05 11:54:19 +00:00
|
|
|
// Implement the bound 'writeBytecode' method.
|
2022-10-06 18:21:47 +00:00
|
|
|
void writeBytecode(const pybind11::object &fileObject);
|
2022-09-05 11:54:19 +00:00
|
|
|
|
2021-10-31 09:37:20 +01:00
|
|
|
/// Moves the operation before or after the other operation.
|
|
|
|
|
void moveAfter(PyOperationBase &other);
|
|
|
|
|
void moveBefore(PyOperationBase &other);
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// Verify the operation. Throws `MLIRError` if verification fails, and
|
|
|
|
|
/// returns `true` otherwise.
|
|
|
|
|
bool verify();
|
|
|
|
|
|
2020-11-01 23:05:36 -08:00
|
|
|
/// Each must provide access to the raw Operation.
|
|
|
|
|
virtual PyOperation &getOperation() = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Wrapper around PyOperation.
|
|
|
|
|
/// Operations exist in either an attached (dependent) or detached (top-level)
|
|
|
|
|
/// state. In the detached state (as on creation), an operation is owned by
|
|
|
|
|
/// the creator and its lifetime extends either until its reference count
|
|
|
|
|
/// drops to zero or it is attached to a parent, at which point its lifetime
|
|
|
|
|
/// is bounded by its top-level parent reference.
|
|
|
|
|
class PyOperation;
|
|
|
|
|
using PyOperationRef = PyObjectRef<PyOperation>;
|
2020-11-01 23:05:36 -08:00
|
|
|
class PyOperation : public PyOperationBase, public BaseContextObject {
|
2020-09-18 18:38:21 -07:00
|
|
|
public:
|
2022-01-14 01:35:38 +00:00
|
|
|
~PyOperation() override;
|
2020-11-01 23:05:36 -08:00
|
|
|
PyOperation &getOperation() override { return *this; }
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Returns a PyOperation for the given MlirOperation, optionally associating
|
2020-10-28 23:16:36 -07:00
|
|
|
/// it with a parentKeepAlive.
|
2020-09-18 18:38:21 -07:00
|
|
|
static PyOperationRef
|
|
|
|
|
forOperation(PyMlirContextRef contextRef, MlirOperation operation,
|
|
|
|
|
pybind11::object parentKeepAlive = pybind11::object());
|
|
|
|
|
|
|
|
|
|
/// Creates a detached operation. The operation must not be associated with
|
|
|
|
|
/// any existing live operation.
|
|
|
|
|
static PyOperationRef
|
|
|
|
|
createDetached(PyMlirContextRef contextRef, MlirOperation operation,
|
|
|
|
|
pybind11::object parentKeepAlive = pybind11::object());
|
|
|
|
|
|
2022-11-08 16:55:06 -05:00
|
|
|
/// Parses a source string (either text assembly or bytecode), creating a
|
|
|
|
|
/// detached operation.
|
|
|
|
|
static PyOperationRef parse(PyMlirContextRef contextRef,
|
|
|
|
|
const std::string &sourceStr,
|
|
|
|
|
const std::string &sourceName);
|
|
|
|
|
|
2021-10-31 09:37:20 +01:00
|
|
|
/// Detaches the operation from its parent block and updates its state
|
|
|
|
|
/// accordingly.
|
|
|
|
|
void detachFromParent() {
|
|
|
|
|
mlirOperationRemoveFromParent(getOperation());
|
|
|
|
|
setDetached();
|
|
|
|
|
parentKeepAlive = pybind11::object();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
/// Gets the backing operation.
|
2020-11-29 13:52:11 -08:00
|
|
|
operator MlirOperation() const { return get(); }
|
|
|
|
|
MlirOperation get() const {
|
2020-09-18 18:38:21 -07:00
|
|
|
checkValid();
|
|
|
|
|
return operation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PyOperationRef getRef() {
|
|
|
|
|
return PyOperationRef(
|
|
|
|
|
this, pybind11::reinterpret_borrow<pybind11::object>(handle));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool isAttached() { return attached; }
|
2022-01-14 01:35:55 +00:00
|
|
|
void setAttached(const pybind11::object &parent = pybind11::object()) {
|
2020-09-20 21:25:46 -07:00
|
|
|
assert(!attached && "operation already attached");
|
|
|
|
|
attached = true;
|
|
|
|
|
}
|
2021-10-31 09:37:20 +01:00
|
|
|
void setDetached() {
|
|
|
|
|
assert(attached && "operation already detached");
|
|
|
|
|
attached = false;
|
|
|
|
|
}
|
2020-11-29 13:52:11 -08:00
|
|
|
void checkValid() const;
|
2020-09-18 18:38:21 -07:00
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
/// Gets the owning block or raises an exception if the operation has no
|
|
|
|
|
/// owning block.
|
|
|
|
|
PyBlock getBlock();
|
|
|
|
|
|
|
|
|
|
/// Gets the parent operation or raises an exception if the operation has
|
|
|
|
|
/// no parent.
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<PyOperationRef> getParentOperation();
|
2020-10-28 23:16:36 -07:00
|
|
|
|
2021-04-06 14:15:22 -07:00
|
|
|
/// Gets a capsule wrapping the void* within the MlirOperation.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyOperation from the MlirOperation wrapped by a capsule.
|
|
|
|
|
/// Ownership of the underlying MlirOperation is taken by calling this
|
|
|
|
|
/// function.
|
|
|
|
|
static pybind11::object createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
/// Creates an operation. See corresponding python docstring.
|
|
|
|
|
static pybind11::object
|
2023-01-14 01:25:58 -08:00
|
|
|
create(const std::string &name, std::optional<std::vector<PyType *>> results,
|
|
|
|
|
std::optional<std::vector<PyValue *>> operands,
|
|
|
|
|
std::optional<pybind11::dict> attributes,
|
|
|
|
|
std::optional<std::vector<PyBlock *>> successors, int regions,
|
2022-01-02 01:26:44 +00:00
|
|
|
DefaultingPyLocation location, const pybind11::object &ip);
|
2020-10-31 23:40:25 -07:00
|
|
|
|
2020-11-01 23:05:36 -08:00
|
|
|
/// Creates an OpView suitable for this operation.
|
|
|
|
|
pybind11::object createOpView();
|
|
|
|
|
|
2021-04-23 20:54:04 -06:00
|
|
|
/// Erases the underlying MlirOperation, removes its pointer from the
|
|
|
|
|
/// parent context's live operations map, and sets the valid bit false.
|
|
|
|
|
void erase();
|
|
|
|
|
|
2022-04-19 15:03:15 -07:00
|
|
|
/// Invalidate the operation.
|
|
|
|
|
void setInvalid() { valid = false; }
|
|
|
|
|
|
2022-03-28 15:45:40 +02:00
|
|
|
/// Clones this operation.
|
|
|
|
|
pybind11::object clone(const pybind11::object &ip);
|
|
|
|
|
|
2020-09-18 18:38:21 -07:00
|
|
|
private:
|
|
|
|
|
PyOperation(PyMlirContextRef contextRef, MlirOperation operation);
|
|
|
|
|
static PyOperationRef createInstance(PyMlirContextRef contextRef,
|
|
|
|
|
MlirOperation operation,
|
|
|
|
|
pybind11::object parentKeepAlive);
|
|
|
|
|
|
|
|
|
|
MlirOperation operation;
|
|
|
|
|
pybind11::handle handle;
|
|
|
|
|
// Keeps the parent alive, regardless of whether it is an Operation or
|
|
|
|
|
// Module.
|
|
|
|
|
// TODO: As implemented, this facility is only sufficient for modeling the
|
|
|
|
|
// trivial module parent back-reference. Generalize this to also account for
|
|
|
|
|
// transitions from detached to attached and address TODOs in the
|
|
|
|
|
// ir_operation.py regarding testing corresponding lifetime guarantees.
|
|
|
|
|
pybind11::object parentKeepAlive;
|
|
|
|
|
bool attached = true;
|
|
|
|
|
bool valid = true;
|
2021-10-31 09:37:20 +01:00
|
|
|
|
|
|
|
|
friend class PyOperationBase;
|
2021-11-02 12:39:36 +01:00
|
|
|
friend class PySymbolTable;
|
2020-08-16 18:49:28 -07:00
|
|
|
};
|
|
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
/// A PyOpView is equivalent to the C++ "Op" wrappers: these are the basis for
|
|
|
|
|
/// providing more instance-specific accessors and serve as the base class for
|
|
|
|
|
/// custom ODS-style operation classes. Since this class is subclass on the
|
|
|
|
|
/// python side, it must present an __init__ method that operates in pure
|
|
|
|
|
/// python types.
|
2020-11-01 23:05:36 -08:00
|
|
|
class PyOpView : public PyOperationBase {
|
2020-10-21 23:34:01 -07:00
|
|
|
public:
|
2022-01-02 01:26:44 +00:00
|
|
|
PyOpView(const pybind11::object &operationObject);
|
2020-11-01 23:05:36 -08:00
|
|
|
PyOperation &getOperation() override { return operation; }
|
2020-10-21 23:34:01 -07:00
|
|
|
|
|
|
|
|
pybind11::object getOperationObject() { return operationObject; }
|
|
|
|
|
|
2021-01-18 11:27:19 -08:00
|
|
|
static pybind11::object
|
2022-01-02 01:26:44 +00:00
|
|
|
buildGeneric(const pybind11::object &cls, pybind11::list resultTypeList,
|
2021-01-24 14:46:56 -08:00
|
|
|
pybind11::list operandList,
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<pybind11::dict> attributes,
|
|
|
|
|
std::optional<std::vector<PyBlock *>> successors,
|
|
|
|
|
std::optional<int> regions, DefaultingPyLocation location,
|
2022-01-02 22:02:20 +00:00
|
|
|
const pybind11::object &maybeIp);
|
2021-01-18 11:27:19 -08:00
|
|
|
|
2023-01-22 23:31:18 -05:00
|
|
|
/// Construct an instance of a class deriving from OpView, bypassing its
|
|
|
|
|
/// `__init__` method. The derived class will typically define a constructor
|
|
|
|
|
/// that provides a convenient builder, but we need to side-step this when
|
|
|
|
|
/// constructing an `OpView` for an already-built operation.
|
|
|
|
|
///
|
|
|
|
|
/// The caller is responsible for verifying that `operation` is a valid
|
|
|
|
|
/// operation to construct `cls` with.
|
|
|
|
|
static pybind11::object constructDerived(const pybind11::object &cls,
|
|
|
|
|
const PyOperation &operation);
|
|
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
private:
|
2020-11-01 23:05:36 -08:00
|
|
|
PyOperation &operation; // For efficient, cast-free access from C++
|
2020-10-21 23:34:01 -07:00
|
|
|
pybind11::object operationObject; // Holds the reference.
|
|
|
|
|
};
|
|
|
|
|
|
2020-08-26 23:48:42 -07:00
|
|
|
/// Wrapper around an MlirRegion.
|
2020-09-19 22:02:32 -07:00
|
|
|
/// Regions are managed completely by their containing operation. Unlike the
|
|
|
|
|
/// C++ API, the python API does not support detached regions.
|
2020-08-26 23:48:42 -07:00
|
|
|
class PyRegion {
|
|
|
|
|
public:
|
2020-09-19 22:02:32 -07:00
|
|
|
PyRegion(PyOperationRef parentOperation, MlirRegion region)
|
|
|
|
|
: parentOperation(std::move(parentOperation)), region(region) {
|
|
|
|
|
assert(!mlirRegionIsNull(region) && "python region cannot be null");
|
2020-08-26 23:48:42 -07:00
|
|
|
}
|
2021-10-11 18:24:48 +02:00
|
|
|
operator MlirRegion() const { return region; }
|
2020-08-26 23:48:42 -07:00
|
|
|
|
2020-09-19 22:02:32 -07:00
|
|
|
MlirRegion get() { return region; }
|
|
|
|
|
PyOperationRef &getParentOperation() { return parentOperation; }
|
2020-08-26 23:48:42 -07:00
|
|
|
|
2020-09-19 22:02:32 -07:00
|
|
|
void checkValid() { return parentOperation->checkValid(); }
|
2020-08-26 23:48:42 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-09-19 22:02:32 -07:00
|
|
|
PyOperationRef parentOperation;
|
|
|
|
|
MlirRegion region;
|
2020-08-26 23:48:42 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Wrapper around an MlirBlock.
|
2020-09-19 22:02:32 -07:00
|
|
|
/// Blocks are managed completely by their containing operation. Unlike the
|
|
|
|
|
/// C++ API, the python API does not support detached blocks.
|
2020-08-26 23:48:42 -07:00
|
|
|
class PyBlock {
|
|
|
|
|
public:
|
2020-09-19 22:02:32 -07:00
|
|
|
PyBlock(PyOperationRef parentOperation, MlirBlock block)
|
|
|
|
|
: parentOperation(std::move(parentOperation)), block(block) {
|
|
|
|
|
assert(!mlirBlockIsNull(block) && "python block cannot be null");
|
2020-08-26 23:48:42 -07:00
|
|
|
}
|
|
|
|
|
|
2020-09-19 22:02:32 -07:00
|
|
|
MlirBlock get() { return block; }
|
|
|
|
|
PyOperationRef &getParentOperation() { return parentOperation; }
|
2020-08-26 23:48:42 -07:00
|
|
|
|
2020-09-19 22:02:32 -07:00
|
|
|
void checkValid() { return parentOperation->checkValid(); }
|
2020-08-26 23:48:42 -07:00
|
|
|
|
|
|
|
|
private:
|
2020-09-19 22:02:32 -07:00
|
|
|
PyOperationRef parentOperation;
|
|
|
|
|
MlirBlock block;
|
2020-08-26 23:48:42 -07:00
|
|
|
};
|
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
/// An insertion point maintains a pointer to a Block and a reference operation.
|
|
|
|
|
/// Calls to insert() will insert a new operation before the
|
|
|
|
|
/// reference operation. If the reference operation is null, then appends to
|
|
|
|
|
/// the end of the block.
|
|
|
|
|
class PyInsertionPoint {
|
|
|
|
|
public:
|
|
|
|
|
/// Creates an insertion point positioned after the last operation in the
|
|
|
|
|
/// block, but still inside the block.
|
|
|
|
|
PyInsertionPoint(PyBlock &block);
|
|
|
|
|
/// Creates an insertion point positioned before a reference operation.
|
2020-11-01 23:05:36 -08:00
|
|
|
PyInsertionPoint(PyOperationBase &beforeOperationBase);
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Shortcut to create an insertion point at the beginning of the block.
|
|
|
|
|
static PyInsertionPoint atBlockBegin(PyBlock &block);
|
|
|
|
|
/// Shortcut to create an insertion point before the block terminator.
|
|
|
|
|
static PyInsertionPoint atBlockTerminator(PyBlock &block);
|
|
|
|
|
|
|
|
|
|
/// Inserts an operation.
|
2020-11-01 23:05:36 -08:00
|
|
|
void insert(PyOperationBase &operationBase);
|
2020-10-28 23:16:36 -07:00
|
|
|
|
|
|
|
|
/// Enter and exit the context manager.
|
|
|
|
|
pybind11::object contextEnter();
|
2022-01-02 01:26:44 +00:00
|
|
|
void contextExit(const pybind11::object &excType,
|
|
|
|
|
const pybind11::object &excVal,
|
|
|
|
|
const pybind11::object &excTb);
|
2020-10-28 23:16:36 -07:00
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
PyBlock &getBlock() { return block; }
|
|
|
|
|
|
2020-10-28 23:16:36 -07:00
|
|
|
private:
|
|
|
|
|
// Trampoline constructor that avoids null initializing members while
|
|
|
|
|
// looking up parents.
|
2023-01-14 01:25:58 -08:00
|
|
|
PyInsertionPoint(PyBlock block, std::optional<PyOperationRef> refOperation)
|
2020-11-01 23:05:36 -08:00
|
|
|
: refOperation(std::move(refOperation)), block(std::move(block)) {}
|
2020-10-28 23:16:36 -07:00
|
|
|
|
2023-01-14 01:25:58 -08:00
|
|
|
std::optional<PyOperationRef> refOperation;
|
2020-11-01 23:05:36 -08:00
|
|
|
PyBlock block;
|
2020-10-28 23:16:36 -07:00
|
|
|
};
|
2021-10-14 17:19:06 +02:00
|
|
|
/// Wrapper around the generic MlirType.
|
|
|
|
|
/// The lifetime of a type is bound by the PyContext that created it.
|
|
|
|
|
class PyType : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyType(PyMlirContextRef contextRef, MlirType type)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), type(type) {}
|
2023-03-28 11:05:00 -04:00
|
|
|
bool operator==(const PyType &other) const;
|
2021-10-14 17:19:06 +02:00
|
|
|
operator MlirType() const { return type; }
|
|
|
|
|
MlirType get() const { return type; }
|
|
|
|
|
|
|
|
|
|
/// Gets a capsule wrapping the void* within the MlirType.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyType from the MlirType wrapped by a capsule.
|
|
|
|
|
/// Note that PyType instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirType
|
|
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static PyType createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirType type;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// CRTP base classes for Python types that subclass Type and should be
|
|
|
|
|
/// castable from it (i.e. via something like IntegerType(t)).
|
|
|
|
|
/// By default, type class hierarchies are one level deep (i.e. a
|
|
|
|
|
/// concrete type class extends PyType); however, intermediate python-visible
|
|
|
|
|
/// base classes can be modeled by specifying a BaseTy.
|
|
|
|
|
template <typename DerivedTy, typename BaseTy = PyType>
|
|
|
|
|
class PyConcreteType : public BaseTy {
|
|
|
|
|
public:
|
|
|
|
|
// Derived classes must define statics for:
|
|
|
|
|
// IsAFunctionTy isaFunction
|
|
|
|
|
// const char *pyClassName
|
|
|
|
|
using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
|
|
|
|
|
using IsAFunctionTy = bool (*)(MlirType);
|
|
|
|
|
|
|
|
|
|
PyConcreteType() = default;
|
|
|
|
|
PyConcreteType(PyMlirContextRef contextRef, MlirType t)
|
|
|
|
|
: BaseTy(std::move(contextRef), t) {}
|
|
|
|
|
PyConcreteType(PyType &orig)
|
|
|
|
|
: PyConcreteType(orig.getContext(), castFrom(orig)) {}
|
|
|
|
|
|
|
|
|
|
static MlirType castFrom(PyType &orig) {
|
|
|
|
|
if (!DerivedTy::isaFunction(orig)) {
|
|
|
|
|
auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
|
|
|
|
|
throw SetPyError(PyExc_ValueError, llvm::Twine("Cannot cast type to ") +
|
|
|
|
|
DerivedTy::pyClassName +
|
|
|
|
|
" (from " + origRepr + ")");
|
|
|
|
|
}
|
|
|
|
|
return orig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bind(pybind11::module &m) {
|
|
|
|
|
auto cls = ClassTy(m, DerivedTy::pyClassName, pybind11::module_local());
|
2021-11-28 14:08:06 -08:00
|
|
|
cls.def(pybind11::init<PyType &>(), pybind11::keep_alive<0, 1>(),
|
|
|
|
|
pybind11::arg("cast_from_type"));
|
|
|
|
|
cls.def_static(
|
|
|
|
|
"isinstance",
|
|
|
|
|
[](PyType &otherType) -> bool {
|
|
|
|
|
return DerivedTy::isaFunction(otherType);
|
|
|
|
|
},
|
|
|
|
|
pybind11::arg("other"));
|
2021-10-14 17:19:06 +02:00
|
|
|
DerivedTy::bindDerived(cls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Implemented by derived classes to add methods to the Python subclass.
|
|
|
|
|
static void bindDerived(ClassTy &m) {}
|
|
|
|
|
};
|
2020-10-28 23:16:36 -07:00
|
|
|
|
2020-08-19 15:33:02 -07:00
|
|
|
/// Wrapper around the generic MlirAttribute.
|
|
|
|
|
/// The lifetime of a type is bound by the PyContext that created it.
|
2020-09-18 00:21:09 -07:00
|
|
|
class PyAttribute : public BaseContextObject {
|
2020-08-19 15:33:02 -07:00
|
|
|
public:
|
2020-09-18 00:21:09 -07:00
|
|
|
PyAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), attr(attr) {}
|
2023-03-28 11:05:00 -04:00
|
|
|
bool operator==(const PyAttribute &other) const;
|
2020-11-29 13:30:23 -08:00
|
|
|
operator MlirAttribute() const { return attr; }
|
|
|
|
|
MlirAttribute get() const { return attr; }
|
2020-08-19 15:33:02 -07:00
|
|
|
|
2021-01-26 10:40:21 +08:00
|
|
|
/// Gets a capsule wrapping the void* within the MlirAttribute.
|
2020-11-29 13:30:23 -08:00
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
2021-01-26 10:40:21 +08:00
|
|
|
/// Creates a PyAttribute from the MlirAttribute wrapped by a capsule.
|
|
|
|
|
/// Note that PyAttribute instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirAttribute
|
2020-11-29 13:30:23 -08:00
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static PyAttribute createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
2020-08-19 15:33:02 -07:00
|
|
|
MlirAttribute attr;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Represents a Python MlirNamedAttr, carrying an optional owned name.
|
|
|
|
|
/// TODO: Refactor this and the C-API to be based on an Identifier owned
|
|
|
|
|
/// by the context so as to avoid ownership issues here.
|
|
|
|
|
class PyNamedAttribute {
|
|
|
|
|
public:
|
|
|
|
|
/// Constructs a PyNamedAttr that retains an owned name. This should be
|
|
|
|
|
/// used in any code that originates an MlirNamedAttribute from a python
|
|
|
|
|
/// string.
|
|
|
|
|
/// The lifetime of the PyNamedAttr must extend to the lifetime of the
|
|
|
|
|
/// passed attribute.
|
|
|
|
|
PyNamedAttribute(MlirAttribute attr, std::string ownedName);
|
|
|
|
|
|
|
|
|
|
MlirNamedAttribute namedAttr;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// Since the MlirNamedAttr contains an internal pointer to the actual
|
|
|
|
|
// memory of the owned string, it must be heap allocated to remain valid.
|
|
|
|
|
// Otherwise, strings that fit within the small object optimization threshold
|
|
|
|
|
// will have their memory address change as the containing object is moved,
|
|
|
|
|
// resulting in an invalid aliased pointer.
|
|
|
|
|
std::unique_ptr<std::string> ownedName;
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-22 15:52:01 +02:00
|
|
|
/// CRTP base classes for Python attributes that subclass Attribute and should
|
|
|
|
|
/// be castable from it (i.e. via something like StringAttr(attr)).
|
|
|
|
|
/// By default, attribute class hierarchies are one level deep (i.e. a
|
|
|
|
|
/// concrete attribute class extends PyAttribute); however, intermediate
|
|
|
|
|
/// python-visible base classes can be modeled by specifying a BaseTy.
|
|
|
|
|
template <typename DerivedTy, typename BaseTy = PyAttribute>
|
|
|
|
|
class PyConcreteAttribute : public BaseTy {
|
|
|
|
|
public:
|
|
|
|
|
// Derived classes must define statics for:
|
|
|
|
|
// IsAFunctionTy isaFunction
|
|
|
|
|
// const char *pyClassName
|
|
|
|
|
using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
|
|
|
|
|
using IsAFunctionTy = bool (*)(MlirAttribute);
|
|
|
|
|
|
|
|
|
|
PyConcreteAttribute() = default;
|
|
|
|
|
PyConcreteAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
|
|
|
|
|
: BaseTy(std::move(contextRef), attr) {}
|
|
|
|
|
PyConcreteAttribute(PyAttribute &orig)
|
|
|
|
|
: PyConcreteAttribute(orig.getContext(), castFrom(orig)) {}
|
|
|
|
|
|
|
|
|
|
static MlirAttribute castFrom(PyAttribute &orig) {
|
|
|
|
|
if (!DerivedTy::isaFunction(orig)) {
|
|
|
|
|
auto origRepr = pybind11::repr(pybind11::cast(orig)).cast<std::string>();
|
|
|
|
|
throw SetPyError(PyExc_ValueError,
|
|
|
|
|
llvm::Twine("Cannot cast attribute to ") +
|
|
|
|
|
DerivedTy::pyClassName + " (from " + origRepr + ")");
|
|
|
|
|
}
|
|
|
|
|
return orig;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void bind(pybind11::module &m) {
|
2021-09-14 21:55:54 +00:00
|
|
|
auto cls = ClassTy(m, DerivedTy::pyClassName, pybind11::buffer_protocol(),
|
|
|
|
|
pybind11::module_local());
|
2021-11-28 14:08:06 -08:00
|
|
|
cls.def(pybind11::init<PyAttribute &>(), pybind11::keep_alive<0, 1>(),
|
|
|
|
|
pybind11::arg("cast_from_attr"));
|
|
|
|
|
cls.def_static(
|
|
|
|
|
"isinstance",
|
|
|
|
|
[](PyAttribute &otherAttr) -> bool {
|
|
|
|
|
return DerivedTy::isaFunction(otherAttr);
|
|
|
|
|
},
|
|
|
|
|
pybind11::arg("other"));
|
2021-10-14 17:19:06 +02:00
|
|
|
cls.def_property_readonly("type", [](PyAttribute &attr) {
|
|
|
|
|
return PyType(attr.getContext(), mlirAttributeGetType(attr));
|
2021-04-28 16:16:45 -07:00
|
|
|
});
|
|
|
|
|
DerivedTy::bindDerived(cls);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Implemented by derived classes to add methods to the Python subclass.
|
|
|
|
|
static void bindDerived(ClassTy &m) {}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-20 11:21:05 +02:00
|
|
|
/// Wrapper around the generic MlirValue.
|
|
|
|
|
/// Values are managed completely by the operation that resulted in their
|
|
|
|
|
/// definition. For op result value, this is the operation that defines the
|
|
|
|
|
/// value. For block argument values, this is the operation that contains the
|
|
|
|
|
/// block to which the value is an argument (blocks cannot be detached in Python
|
|
|
|
|
/// bindings so such operation always exists).
|
|
|
|
|
class PyValue {
|
|
|
|
|
public:
|
|
|
|
|
PyValue(PyOperationRef parentOperation, MlirValue value)
|
2022-01-14 01:35:55 +00:00
|
|
|
: parentOperation(std::move(parentOperation)), value(value) {}
|
2021-10-11 18:24:48 +02:00
|
|
|
operator MlirValue() const { return value; }
|
2020-10-20 11:21:05 +02:00
|
|
|
|
|
|
|
|
MlirValue get() { return value; }
|
|
|
|
|
PyOperationRef &getParentOperation() { return parentOperation; }
|
|
|
|
|
|
|
|
|
|
void checkValid() { return parentOperation->checkValid(); }
|
|
|
|
|
|
2021-04-22 00:07:30 -06:00
|
|
|
/// Gets a capsule wrapping the void* within the MlirValue.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyValue from the MlirValue wrapped by a capsule. Ownership of
|
|
|
|
|
/// the underlying MlirValue is still tied to the owning operation.
|
|
|
|
|
static PyValue createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
2020-10-20 11:21:05 +02:00
|
|
|
private:
|
|
|
|
|
PyOperationRef parentOperation;
|
|
|
|
|
MlirValue value;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-07 11:09:09 +01:00
|
|
|
/// Wrapper around MlirAffineExpr. Affine expressions are owned by the context.
|
|
|
|
|
class PyAffineExpr : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyAffineExpr(PyMlirContextRef contextRef, MlirAffineExpr affineExpr)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), affineExpr(affineExpr) {}
|
2023-03-28 11:05:00 -04:00
|
|
|
bool operator==(const PyAffineExpr &other) const;
|
2021-01-07 11:09:09 +01:00
|
|
|
operator MlirAffineExpr() const { return affineExpr; }
|
|
|
|
|
MlirAffineExpr get() const { return affineExpr; }
|
|
|
|
|
|
|
|
|
|
/// Gets a capsule wrapping the void* within the MlirAffineExpr.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyAffineExpr from the MlirAffineExpr wrapped by a capsule.
|
|
|
|
|
/// Note that PyAffineExpr instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirAffineExpr
|
|
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static PyAffineExpr createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
PyAffineExpr add(const PyAffineExpr &other) const;
|
|
|
|
|
PyAffineExpr mul(const PyAffineExpr &other) const;
|
|
|
|
|
PyAffineExpr floorDiv(const PyAffineExpr &other) const;
|
|
|
|
|
PyAffineExpr ceilDiv(const PyAffineExpr &other) const;
|
|
|
|
|
PyAffineExpr mod(const PyAffineExpr &other) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirAffineExpr affineExpr;
|
|
|
|
|
};
|
|
|
|
|
|
2020-12-14 18:43:05 +08:00
|
|
|
class PyAffineMap : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyAffineMap(PyMlirContextRef contextRef, MlirAffineMap affineMap)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), affineMap(affineMap) {}
|
2023-03-28 11:05:00 -04:00
|
|
|
bool operator==(const PyAffineMap &other) const;
|
2020-12-14 18:43:05 +08:00
|
|
|
operator MlirAffineMap() const { return affineMap; }
|
|
|
|
|
MlirAffineMap get() const { return affineMap; }
|
|
|
|
|
|
|
|
|
|
/// Gets a capsule wrapping the void* within the MlirAffineMap.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyAffineMap from the MlirAffineMap wrapped by a capsule.
|
|
|
|
|
/// Note that PyAffineMap instances are uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Ownership of the underlying MlirAffineMap
|
|
|
|
|
/// is taken by calling this function.
|
|
|
|
|
static PyAffineMap createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirAffineMap affineMap;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-25 18:17:19 +01:00
|
|
|
class PyIntegerSet : public BaseContextObject {
|
|
|
|
|
public:
|
|
|
|
|
PyIntegerSet(PyMlirContextRef contextRef, MlirIntegerSet integerSet)
|
|
|
|
|
: BaseContextObject(std::move(contextRef)), integerSet(integerSet) {}
|
2023-03-28 11:05:00 -04:00
|
|
|
bool operator==(const PyIntegerSet &other) const;
|
2021-01-25 18:17:19 +01:00
|
|
|
operator MlirIntegerSet() const { return integerSet; }
|
|
|
|
|
MlirIntegerSet get() const { return integerSet; }
|
|
|
|
|
|
|
|
|
|
/// Gets a capsule wrapping the void* within the MlirIntegerSet.
|
|
|
|
|
pybind11::object getCapsule();
|
|
|
|
|
|
|
|
|
|
/// Creates a PyIntegerSet from the MlirAffineMap wrapped by a capsule.
|
|
|
|
|
/// Note that PyIntegerSet instances may be uniqued, so the returned object
|
|
|
|
|
/// may be a pre-existing object. Integer sets are owned by the context.
|
|
|
|
|
static PyIntegerSet createFromCapsule(pybind11::object capsule);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
MlirIntegerSet integerSet;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-02 12:39:36 +01:00
|
|
|
/// Bindings for MLIR symbol tables.
|
|
|
|
|
class PySymbolTable {
|
|
|
|
|
public:
|
|
|
|
|
/// Constructs a symbol table for the given operation.
|
|
|
|
|
explicit PySymbolTable(PyOperationBase &operation);
|
|
|
|
|
|
|
|
|
|
/// Destroys the symbol table.
|
|
|
|
|
~PySymbolTable() { mlirSymbolTableDestroy(symbolTable); }
|
|
|
|
|
|
|
|
|
|
/// Returns the symbol (opview) with the given name, throws if there is no
|
|
|
|
|
/// such symbol in the table.
|
|
|
|
|
pybind11::object dunderGetItem(const std::string &name);
|
|
|
|
|
|
|
|
|
|
/// Removes the given operation from the symbol table and erases it.
|
|
|
|
|
void erase(PyOperationBase &symbol);
|
|
|
|
|
|
|
|
|
|
/// Removes the operation with the given name from the symbol table and erases
|
|
|
|
|
/// it, throws if there is no such symbol in the table.
|
|
|
|
|
void dunderDel(const std::string &name);
|
|
|
|
|
|
|
|
|
|
/// Inserts the given operation into the symbol table. The operation must have
|
|
|
|
|
/// the symbol trait.
|
|
|
|
|
PyAttribute insert(PyOperationBase &symbol);
|
|
|
|
|
|
2021-11-28 20:30:18 -08:00
|
|
|
/// Gets and sets the name of a symbol op.
|
|
|
|
|
static PyAttribute getSymbolName(PyOperationBase &symbol);
|
|
|
|
|
static void setSymbolName(PyOperationBase &symbol, const std::string &name);
|
|
|
|
|
|
|
|
|
|
/// Gets and sets the visibility of a symbol op.
|
|
|
|
|
static PyAttribute getVisibility(PyOperationBase &symbol);
|
|
|
|
|
static void setVisibility(PyOperationBase &symbol,
|
|
|
|
|
const std::string &visibility);
|
|
|
|
|
|
|
|
|
|
/// Replaces all symbol uses within an operation. See the API
|
|
|
|
|
/// mlirSymbolTableReplaceAllSymbolUses for all caveats.
|
|
|
|
|
static void replaceAllSymbolUses(const std::string &oldSymbol,
|
|
|
|
|
const std::string &newSymbol,
|
|
|
|
|
PyOperationBase &from);
|
|
|
|
|
|
|
|
|
|
/// Walks all symbol tables under and including 'from'.
|
|
|
|
|
static void walkSymbolTables(PyOperationBase &from, bool allSymUsesVisible,
|
|
|
|
|
pybind11::object callback);
|
|
|
|
|
|
2021-11-02 12:39:36 +01:00
|
|
|
/// Casts the bindings class into the C API structure.
|
|
|
|
|
operator MlirSymbolTable() { return symbolTable; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
PyOperationRef operation;
|
|
|
|
|
MlirSymbolTable symbolTable;
|
|
|
|
|
};
|
|
|
|
|
|
[mlir][python] Capture error diagnostics in exceptions
This updates most (all?) error-diagnostic-emitting python APIs to
capture error diagnostics and include them in the raised exception's
message:
```
>>> Operation.parse('"arith.addi"() : () -> ()'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
mlir._mlir_libs.MLIRError: Unable to parse operation assembly:
error: "-":1:1: 'arith.addi' op requires one result
note: "-":1:1: see current operation: "arith.addi"() : () -> ()
```
The diagnostic information is available on the exception for users who
may want to customize the error message:
```
>>> try:
... Operation.parse('"arith.addi"() : () -> ()')
... except MLIRError as e:
... print(e.message)
... print(e.error_diagnostics)
... print(e.error_diagnostics[0].message)
...
Unable to parse operation assembly
[<mlir._mlir_libs._mlir.ir.DiagnosticInfo object at 0x7fed32bd6b70>]
'arith.addi' op requires one result
```
Error diagnostics captured in exceptions aren't propagated to diagnostic
handlers, to avoid double-reporting of errors. The context-level
`emit_error_diagnostics` option can be used to revert to the old
behaviour, causing error diagnostics to be reported to handlers instead
of as part of exceptions.
API changes:
- `Operation.verify` now raises an exception on verification failure,
instead of returning `false`
- The exception raised by the following methods has been changed to
`MLIRError`:
- `PassManager.run`
- `{Module,Operation,Type,Attribute}.parse`
- `{RankedTensorType,UnrankedTensorType}.get`
- `{MemRefType,UnrankedMemRefType}.get`
- `VectorType.get`
- `FloatAttr.get`
closes #60595
depends on D144804, D143830
Reviewed By: stellaraccident
Differential Revision: https://reviews.llvm.org/D143869
2023-02-07 16:07:50 -05:00
|
|
|
/// Custom exception that allows access to error diagnostic information. This is
|
|
|
|
|
/// converted to the `ir.MLIRError` python exception when thrown.
|
|
|
|
|
struct MLIRError {
|
|
|
|
|
MLIRError(llvm::Twine message,
|
|
|
|
|
std::vector<PyDiagnostic::DiagnosticInfo> &&errorDiagnostics = {})
|
|
|
|
|
: message(message.str()), errorDiagnostics(std::move(errorDiagnostics)) {}
|
|
|
|
|
std::string message;
|
|
|
|
|
std::vector<PyDiagnostic::DiagnosticInfo> errorDiagnostics;
|
|
|
|
|
};
|
|
|
|
|
|
2021-03-19 11:57:01 -07:00
|
|
|
void populateIRAffine(pybind11::module &m);
|
|
|
|
|
void populateIRAttributes(pybind11::module &m);
|
|
|
|
|
void populateIRCore(pybind11::module &m);
|
2021-10-14 17:18:28 +02:00
|
|
|
void populateIRInterfaces(pybind11::module &m);
|
2021-03-19 11:57:01 -07:00
|
|
|
void populateIRTypes(pybind11::module &m);
|
2020-08-16 20:53:45 -07:00
|
|
|
|
|
|
|
|
} // namespace python
|
|
|
|
|
} // namespace mlir
|
2020-08-16 18:49:28 -07:00
|
|
|
|
2020-10-31 23:40:25 -07:00
|
|
|
namespace pybind11 {
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
|
|
template <>
|
|
|
|
|
struct type_caster<mlir::python::DefaultingPyMlirContext>
|
|
|
|
|
: MlirDefaultingCaster<mlir::python::DefaultingPyMlirContext> {};
|
|
|
|
|
template <>
|
|
|
|
|
struct type_caster<mlir::python::DefaultingPyLocation>
|
|
|
|
|
: MlirDefaultingCaster<mlir::python::DefaultingPyLocation> {};
|
|
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
} // namespace pybind11
|
|
|
|
|
|
2020-08-16 18:49:28 -07:00
|
|
|
#endif // MLIR_BINDINGS_PYTHON_IRMODULES_H
|