Skip to content

[mlir][python] automatic location inference - #151246

Merged
makslevental merged 12 commits into
llvm:mainfrom
makslevental:makslevental/auto-locs
Aug 12, 2025
Merged

makslevental merged 12 commits into
llvm:mainfrom
makslevental:makslevental/auto-locs

Conversation

@makslevental

@makslevental makslevental commented Jul 29, 2025 •

Copy link
Copy Markdown
Contributor

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
@github-actions

github-actions Bot commented Jul 29, 2025 •

Copy link
Copy Markdown

✅ 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
@llvmbot llvmbot added the mlir:python MLIR Python bindings label Aug 12, 2025
@makslevental
makslevental force-pushed the makslevental/auto-locs branch 6 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
Copy Markdown
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.

@makslevental makslevental Aug 12, 2025 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Member

Choose a reason for hiding this comment

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

SGTM

Comment thread mlir/lib/Bindings/Python/IRCore.cpp Outdated
Comment on lines 3130 to 3132

@makslevental makslevental Aug 12, 2025 •

Copy link
Copy Markdown
Contributor Author

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 thread mlir/lib/Bindings/Python/IRModule.h Outdated
Comment on lines 195 to 204

Copy link
Copy Markdown
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
@hanhanW

hanhanW commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

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

hanhanW commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

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

@hanhanW

hanhanW commented Aug 19, 2025

Copy link
Copy Markdown
Contributor

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

two = arith.constant(IndexType.get(), 2)

# fmt: off
# CHECK: loc(callsite("testInferLocations"("{{.*}}[[SEP:[/\\]]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":31:13 to :43) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @makslevental

I recently faced the following error: ******************** TEST 'MLIR :: python/ir/auto_location.py' FAILED ******************** when LLVM was compiled for windows in OpenAI Triton project: https://github.com/triton-lang/triton/actions/runs/17419518063/job/49454994952.

More logs:

******************** TEST 'MLIR :: python/ir/auto_location.py' FAILED ********************
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
"C:/hostedtoolcache/windows/Python/3.11.9/x64/python3.exe" D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py | d:\a\triton\triton\llvm-project\build\bin\filecheck.exe D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py
# executed command: C:/hostedtoolcache/windows/Python/3.11.9/x64/python3.exe 'D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py'
# executed command: 'd:\a\triton\triton\llvm-project\build\bin\filecheck.exe' 'D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py'
# .---command stderr------------
# | D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py:37:11: error: CHECK: expected string not found in input
# |  # CHECK: loc(callsite("testInferLocations"("{{.*}}[[SEP:[/\\]]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":31:13 to :43) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))
# |           ^
# | <stdin>:2:25: note: scanning from here
# | TEST: testInferLocations
# |                         ^
# | 
# | Input file: <stdin>
# | Check file: D:\a\triton\triton\llvm-project\mlir\test\python\ir\auto_location.py
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |           1:  
# |           2: TEST: testInferLocations 
# | check:37                             X error: no match found
# |           3: loc(callsite("testInferLocations"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":31:13 to :43) at callsite("run"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":13:4 to :7) at "<module>"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":26:1 to :4)))) 
# | check:37     

Probably this line should be changed [[SEP:[/\\]]] -> [[SEP]]?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Um no [[SEP:[/\\]]] defines SEP as the regex [/\\] so that's correct so I'm not sure what's going on there.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the fast response! I'll try to debug it further.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That pattern should work and we do have windows tests here but actually I think they're not running because the CI (here, on Windows) doesn't have the minimum version of Python. I didn't have a Windows box handy when I put this together so it's true I wasn't able to test this thoroughly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I managed to fix it with the following regexp: [[SEP:(/|\\\\)]]

In the previous approach, only one \ character was written to SEP.

I checked it with lit and the following file:

// RUN: cat %s | FileCheck %s



# CHECK: loc(callsite("testInferLocations"("{{.*}}[[SEP:(/|\\\\)]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":31:13 to :43) at callsite("run"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":13:4 to :7) at "<module>"("{{.*}}[[SEP]]test[[SEP]]python[[SEP]]ir[[SEP]]auto_location.py":26:1 to :4))))

1:  
2: TEST: testInferLocations 
3: loc(callsite("testInferLocations"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":31:13 to :43) at callsite("run"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":13:4 to :7) at "<module>"("D:\\a\\triton\\triton\\llvm-project\\mlir\\test\\python\\ir\\auto_location.py":26:1 to :4)))) 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok that makes sense but just for sanity check can you try SEP:[/\\]+

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

But btw if you send a PR I'll happily approve it (otherwise I can send one soon).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SEP:[/\]+

yes, it also works. Do you prefer this way?

But btw if you send a PR I'll happily approve it (otherwise I can send one soon).

I'll open a PR for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

6 participants