aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/IRModule.h
diff options
context:
space:
mode:
authorMaksim Levental <maksim.levental@gmail.com>2025-08-12 17:59:59 -0400
committerGitHub <noreply@github.com>2025-08-12 16:59:59 -0500
commita40f47c9725f3aed6752f050f3010f3701d0cff7 (patch)
treef009a39f6baf3a574e6525283f1541d6fa7b6257 /mlir/lib/Bindings/Python/IRModule.h
parentda3182a288bb25184594e9f2642f15fa42768f42 (diff)
downloadllvm-a40f47c9725f3aed6752f050f3010f3701d0cff7.zip
llvm-a40f47c9725f3aed6752f050f3010f3701d0cff7.tar.gz
llvm-a40f47c9725f3aed6752f050f3010f3701d0cff7.tar.bz2
[mlir][python] automatic location inference (#151246)
This PR implements "automatic" location inference in the bindings. The way it works is it walks the frame stack collecting source locations (Python captures these in the frame itself). It is inspired by JAX's [implementation](https://github.com/jax-ml/jax/blob/523ddcfbcad005deab5a7d542df4c706f5ee5e9c/jax/_src/interpreters/mlir.py#L462) but moves the frame stack traversal into the bindings for better performance. The system supports registering "included" and "excluded" filenames; frames originating from functions in included filenames **will not** be filtered and frames originating from functions in excluded filenames **will** be filtered (in that order). This allows excluding all the generated `*_ops_gen.py` files. The system is also "toggleable" and off by default to save people who have their own systems (such as JAX) from the added cost. Note, the system stores the entire stacktrace (subject to `locTracebackFramesLimit`) in the `Location` using specifically a `CallSiteLoc`. This can be useful for profiling tools (flamegraphs etc.). Shoutout to the folks at JAX for coming up with a good system. --------- Co-authored-by: Jacques Pienaar <jpienaar@google.com>
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r--mlir/lib/Bindings/Python/IRModule.h15
1 files changed, 2 insertions, 13 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 9c22dea..fa16ae3 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -192,16 +192,6 @@ public:
PyMlirContext(const PyMlirContext &) = delete;
PyMlirContext(PyMlirContext &&) = delete;
- /// For the case of a python __init__ (nanobind::init) method, pybind11 is
- /// quite strict about needing to return a pointer that is not yet associated
- /// to an nanobind::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();
-
/// Returns a context reference for the singleton PyMlirContext wrapper for
/// the given context.
static PyMlirContextRef forContext(MlirContext context);
@@ -722,8 +712,7 @@ public:
llvm::ArrayRef<MlirValue> operands,
std::optional<nanobind::dict> attributes,
std::optional<std::vector<PyBlock *>> successors, int regions,
- DefaultingPyLocation location, const nanobind::object &ip,
- bool inferType);
+ PyLocation &location, const nanobind::object &ip, bool inferType);
/// Creates an OpView suitable for this operation.
nanobind::object createOpView();
@@ -781,7 +770,7 @@ public:
nanobind::list operandList,
std::optional<nanobind::dict> attributes,
std::optional<std::vector<PyBlock *>> successors,
- std::optional<int> regions, DefaultingPyLocation location,
+ std::optional<int> regions, PyLocation &location,
const nanobind::object &maybeIp);
/// Construct an instance of a class deriving from OpView, bypassing its