Skip to content

[mlir][python] automatic location inference #151246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 12, 2025

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Jul 29, 2025

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 adapted from JAX's implementation 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 (cc @hawkinsp 😄).

@makslevental makslevental force-pushed the makslevental/auto-locs branch 3 times, most recently from 61b0669 to 9ea8751 Compare July 29, 2025 22:59
Copy link

github-actions bot commented Jul 29, 2025

✅ With the latest revision this PR passed the Python code formatter.

@makslevental makslevental force-pushed the makslevental/auto-locs branch 11 times, most recently from 34d5f14 to 7c53bf1 Compare July 30, 2025 03:25
@makslevental makslevental force-pushed the makslevental/auto-locs branch 14 times, most recently from 389db39 to f63eaf2 Compare July 30, 2025 05:05
@makslevental makslevental force-pushed the makslevental/auto-locs branch 5 times, most recently from 3ab9de1 to 2297849 Compare August 12, 2025 05:12
@makslevental makslevental force-pushed the makslevental/auto-locs branch from 2297849 to a4bc05a Compare August 12, 2025 05:13
Comment on lines +84 to +86
if _cext.ir.Location.current:
return _cext.ir.Location.current.context
return None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realized since we're now supporting loc inference we shouldn't require a location to be in the context; specifically Location.current shouldn't throw but just return None. See tests in context_manager.py below.

Copy link
Contributor Author

@makslevental makslevental Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpienaar @rolfmorel i'm asking for a re-review because I made this change which wasn't in the original PR and slightly changes semantics; see also the change to Location.current here below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM

Comment on lines +3130 to +3132
[](nb::object & /*class*/) -> std::optional<PyLocation *> {
auto *loc = PyThreadContextEntry::getDefaultLocation();
if (!loc)
throw nb::value_error("No current Location");
return std::nullopt;
Copy link
Contributor Author

@makslevental makslevental Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change Location.current to return None instead of throwing - see above

Comment on lines -195 to -204
/// 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();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by DCE (unused/no defn)

@makslevental makslevental force-pushed the makslevental/auto-locs branch from b0ff949 to 70244f3 Compare August 12, 2025 05:35
@makslevental makslevental merged commit a40f47c into llvm:main Aug 12, 2025
9 checks passed
@makslevental makslevental deleted the makslevental/auto-locs branch August 12, 2025 22:00
makslevental added a commit that referenced this pull request Aug 15, 2025
makslevental added a commit that referenced this pull request Aug 15, 2025
…d fix warning unused

Introduced (but omitted from this CMake) in #151246.
makslevental added a commit that referenced this pull request Aug 16, 2025
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Aug 16, 2025
…y nanobind (#153861)

Introduced (but omitted from this CMake) in
llvm/llvm-project#151246.
bump-llvm bot pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 17, 2025
bump-llvm bot pushed a commit to makslevental/python_bindings_fork that referenced this pull request Aug 17, 2025
@hanhanW
Copy link
Contributor

hanhanW commented Aug 19, 2025

Hi, I'm seeing the below error on macos build:

ld: warning: ignoring duplicate libraries: '-lm'
ld: Undefined symbols:
  _PyCode_Addr2Location, referenced from:
      (anonymous namespace)::maybeGetTracebackLocation(std::__1::optional<mlir::python::PyLocation> const&) in IRCore.cpp.o
  _PyFrame_GetLasti, referenced from:
      (anonymous namespace)::maybeGetTracebackLocation(std::__1::optional<mlir::python::PyLocation> const&) in IRCore.cpp.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I haven't tracked the change yet, but I'll try to provide a patch. Currently, we can only revert three commits together, otherwsie there are conflicts.

@hanhanW
Copy link
Contributor

hanhanW commented Aug 19, 2025

Oh, 6fc1deb seems to fix the issue. Let me give it a shot.

@hanhanW
Copy link
Contributor

hanhanW commented Aug 19, 2025

Yes, confirmed that it is fixed by the commit. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir:python MLIR Python bindings mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants